]> git.mxchange.org Git - friendica.git/blob - mod/network.php
post/comment order options
[friendica.git] / mod / network.php
1 <?php
2
3
4 function network_init(&$a) {
5         if(! local_user()) {
6                 notice( t('Permission denied.') . EOL);
7                 return;
8         }
9   
10         $group_id = (($a->argc > 1 && intval($a->argv[1])) ? intval($a->argv[1]) : 0);
11                   
12         require_once('include/group.php');
13         if(! x($a->page,'aside'))
14                 $a->page['aside'] = '';
15
16         $search = ((x($_GET,'search')) ? escape_tags($_GET['search']) : '');
17
18         // We need a better way of managing a growing argument list
19
20         // moved into savedsearches()
21         // $srchurl = '/network' 
22         //              . ((x($_GET,'cid')) ? '?cid=' . $_GET['cid'] : '') 
23         //              . ((x($_GET,'star')) ? '?star=' . $_GET['star'] : '')
24         //              . ((x($_GET,'bmark')) ? '?bmark=' . $_GET['bmark'] : '');
25         
26         if(x($_GET,'save')) {
27                 $r = q("select * from `search` where `uid` = %d and `term` = '%s' limit 1",
28                         intval(local_user()),
29                         dbesc($search)
30                 );
31                 if(! count($r)) {
32                         q("insert into `search` ( `uid`,`term` ) values ( %d, '%s') ",
33                                 intval(local_user()),
34                                 dbesc($search)
35                         );
36                 }
37         }
38         if(x($_GET,'remove')) {
39                 q("delete from `search` where `uid` = %d and `term` = '%s' limit 1",
40                         intval(local_user()),
41                         dbesc($search)
42                 );
43         }
44
45
46         
47         // search terms header
48         if(x($_GET,'search')) {
49                 $a->page['content'] .= '<h2>Search Results For: '  . $search . '</h2>';
50         }
51         
52         $a->page['aside'] .= group_side('network','network',true,$group_id);
53         
54         // moved to saved searches to have it in the same div
55         //$a->page['aside'] .= search($search,'netsearch-box',$srchurl,true);
56
57         $a->page['aside'] .= saved_searches($search);
58
59 }
60
61 function saved_searches($search) {
62
63         $srchurl = '/network' 
64                 . ((x($_GET,'cid')) ? '?cid=' . $_GET['cid'] : '') 
65                 . ((x($_GET,'star')) ? '?star=' . $_GET['star'] : '')
66                 . ((x($_GET,'bmark')) ? '?bmark=' . $_GET['bmark'] : '');
67         
68         $o = '';
69
70         $r = q("select `term` from `search` WHERE `uid` = %d",
71                 intval(local_user())
72         );
73
74         $o .= '<div id="saved-search-list" class="widget">';
75         $o .= '<h3 id="search">' . t('Saved Searches') . '</h3>' . "\r\n";
76         $o .= search($search,'netsearch-box',$srchurl,true);
77         
78         if(count($r)) {
79                 $o .= '<ul id="saved-search-ul">' . "\r\n";
80                 foreach($r as $rr) {
81                         $o .= '<li class="saved-search-li clear"><a href="network/?f=&remove=1&search=' . $rr['term'] . '" class="icon drophide savedsearchdrop" title="' . t('Remove term') . '" onclick="return confirmDelete();" onmouseover="imgbright(this);" onmouseout="imgdull(this);" ></a> <a href="network/?f=&search=' . urlencode($rr['term']) . '" class="savedsearchterm" >' . $rr['term'] . '</a></li>' . "\r\n";
82                 }
83                 $o .= '</ul>';
84         }               
85
86         $o .= '<div class="clear"></div>';
87
88         $o .= '</div>' . "\r\n";
89         return $o;
90
91 }
92
93
94 function network_content(&$a, $update = 0) {
95
96         require_once('include/conversation.php');
97
98         if(! local_user())
99         return login(false);
100
101         $o = '';
102
103         // item filter tabs
104         // TODO: fix this logic, reduce duplication
105         $a->page['content'] .= '<div class="tabs-wrapper">';
106         
107         $starred_active = '';
108         $new_active = '';
109         $bookmarked_active = '';
110         $all_active = '';
111         $search_active = '';
112         
113         if(($a->argc > 1 && $a->argv[1] === 'new') 
114                 || ($a->argc > 2 && $a->argv[2] === 'new')) {
115                         $new_active = 'active';
116         }
117         
118         if(x($_GET,'search')) {
119                 $search_active = 'active';
120         }
121         
122         if(x($_GET,'star')) {
123                 $starred_active = 'active';
124         }
125         
126         if($_GET['bmark']) {
127                 $bookmarked_active = 'active';
128         }
129         
130         if (($new_active == '') 
131                 && ($starred_active == '') 
132                 && ($bookmarked_active == '')
133                 && ($search_active == '')) {
134                         $all_active = 'active';
135         }
136         $postord_active = '';
137
138         if($all_active && x($_GET,'order') && $_GET['order'] !== 'comment') {
139                 $all_active = '';
140                 $postord_active = 'active';
141         }
142                          
143
144         
145         // tabs
146         $tabs = array(
147                 array(
148                         'label' => t('Commented Order'),
149                         'url'=>$a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '?cid=' . $_GET['cid'] : ''), 
150                         'sel'=>$all_active,
151                 ),
152                 array(
153                         'label' => t('Posted Order'),
154                         'url'=>$a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . '?order=post' . ((x($_GET,'cid')) ? '?cid=' . $_GET['cid'] : ''), 
155                         'sel'=>$postord_active,
156                 ),
157
158                 array(
159                         'label' => t('New'),
160                         'url' => $a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . '/new' . ((x($_GET,'cid')) ? '/?cid=' . $_GET['cid'] : ''),
161                         'sel' => $new_active,
162                 ),
163                 array(
164                         'label' => t('Starred'),
165                         'url'=>$a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '/?cid=' . $_GET['cid'] : '') . '&star=1',
166                         'sel'=>$starred_active,
167                 ),
168                 array(
169                         'label' => t('Bookmarks'),
170                         'url'=>$a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '/?cid=' . $_GET['cid'] : '') . '&bmark=1',
171                         'sel'=>$bookmarked_active,
172                 ),      
173         );
174         $tpl = get_markup_template('common_tabs.tpl');
175         $o .= replace_macros($tpl, array('$tabs'=>$tabs));
176         // --- end item filter tabs
177
178
179
180         
181
182         $contact_id = $a->cid;
183
184         $group = 0;
185
186         $nouveau = false;
187         require_once('include/acl_selectors.php');
188
189         $cid = ((x($_GET,'cid')) ? intval($_GET['cid']) : 0);
190         $star = ((x($_GET,'star')) ? intval($_GET['star']) : 0);
191         $bmark = ((x($_GET,'bmark')) ? intval($_GET['bmark']) : 0);
192         $order = ((x($_GET,'order')) ? notags($_GET['order']) : 'comment');
193         $liked = ((x($_GET,'liked')) ? intval($_GET['liked']) : 0);
194
195
196         if(($a->argc > 2) && $a->argv[2] === 'new')
197                 $nouveau = true;
198
199         if($a->argc > 1) {
200                 if($a->argv[1] === 'new')
201                         $nouveau = true;
202                 else {
203                         $group = intval($a->argv[1]);
204                         $def_acl = array('allow_gid' => '<' . $group . '>');
205                 }
206         }
207
208         if(x($_GET,'search'))
209                 $nouveau = true;
210         if($cid)
211                 $def_acl = array('allow_cid' => '<' . intval($cid) . '>');
212
213         if(! $update) {
214                 if(group) {
215                         if(($t = group_public_members($group)) && (! get_pconfig(local_user(),'system','nowarn_insecure'))) {
216                                 notice( sprintf( tt('Warning: This group contains %s member from an insecure network.',
217                                                                         'Warning: This group contains %s members from an insecure network.',
218                                                                         $t), $t ) . EOL);
219                                 notice( t('Private messages to this group are at risk of public disclosure.') . EOL);
220                         }
221                 }
222
223                 nav_set_selected('network');
224
225                 $_SESSION['return_url'] = $a->cmd;
226
227                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
228
229                 $x = array(
230                         'is_owner' => true,
231                         'allow_location' => $a->user['allow_location'],
232                         'default_location' => $a->user['default_location'],
233                         'nickname' => $a->user['nickname'],
234                         'lockstate' => ((($group) || (is_array($a->user) && ((strlen($a->user['allow_cid'])) || (strlen($a->user['allow_gid'])) || (strlen($a->user['deny_cid'])) || (strlen($a->user['deny_gid']))))) ? 'lock' : 'unlock'),
235                         'acl' => populate_acl((($group || $cid) ? $def_acl : $a->user), $celeb),
236                         'bang' => (($group || $cid) ? '!' : ''),
237                         'visitor' => 'block',
238                         'profile_uid' => local_user()
239                 );
240
241                 $o .= status_editor($a,$x);
242
243                 // The special div is needed for liveUpdate to kick in for this page.
244                 // We only launch liveUpdate if you are on the front page, you aren't
245                 // filtering by group and also you aren't writing a comment (the last
246                 // criteria is discovered in javascript).
247
248                         $o .= '<div id="live-network"></div>' . "\r\n";
249                         $o .= "<script> var profile_uid = " . $_SESSION['uid'] 
250                                 . "; var netargs = '" . substr($a->cmd,8)
251                                 . '?f='
252                                 . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : '')
253                                 . ((x($_GET,'search')) ? '&search=' . $_GET['search'] : '') 
254                                 . ((x($_GET,'star')) ? '&star=' . $_GET['star'] : '') 
255                                 . ((x($_GET,'order')) ? '&order=' . $_GET['order'] : '') 
256                                 . ((x($_GET,'bmark')) ? '&bmark=' . $_GET['bmark'] : '') 
257                                 . ((x($_GET,'liked')) ? '&liked=' . $_GET['liked'] : '') 
258                                 . "'; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
259
260         }
261
262         // We aren't going to try and figure out at the item, group, and page
263         // level which items you've seen and which you haven't. If you're looking
264         // at the top level network page just mark everything seen. 
265         
266         if((! $group) && (! $cid) && (! $star)) {
267                 $r = q("UPDATE `item` SET `unseen` = 0 
268                         WHERE `unseen` = 1 AND `uid` = %d",
269                         intval($_SESSION['uid'])
270                 );
271         }
272
273         // We don't have to deal with ACL's on this page. You're looking at everything
274         // that belongs to you, hence you can see all of it. We will filter by group if
275         // desired. 
276
277         $star_sql = (($star) ?  " AND `starred` = 1 " : '');
278
279         if($bmark)
280                 $star_sql .= " AND `bookmark` = 1 ";
281
282         $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $star_sql ) ";
283
284         if($group) {
285                 $r = q("SELECT `name`, `id` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
286                         intval($group),
287                         intval($_SESSION['uid'])
288                 );
289                 if(! count($r)) {
290                         if($update)
291                                 killme();
292                         notice( t('No such group') . EOL );
293                         goaway($a->get_baseurl() . '/network');
294                         // NOTREACHED
295                 }
296
297                 $contacts = expand_groups(array($group));
298                 if((is_array($contacts)) && count($contacts)) {
299                         $contact_str = implode(',',$contacts);
300                 }
301                 else {
302                                 $contact_str = ' 0 ';
303                                 info( t('Group is empty'));
304                 }
305
306
307                 $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $star_sql AND ( `contact-id` IN ( $contact_str ) OR `allow_gid` REGEXP '<" . intval($group) . ">' )) ";
308                 $o = '<h2>' . t('Group: ') . $r[0]['name'] . '</h2>' . $o;
309         }
310         elseif($cid) {
311
312                 $r = q("SELECT `id`,`name`,`network`,`writable` FROM `contact` WHERE `id` = %d 
313                                 AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
314                         intval($cid)
315                 );
316                 if(count($r)) {
317                         $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $star_sql AND `contact-id` IN ( " . intval($cid) . " )) ";
318                         $o = '<h2>' . t('Contact: ') . $r[0]['name'] . '</h2>' . $o;
319                         if($r[0]['network'] !== NETWORK_MAIL && $r[0]['network'] !== NETWORK_DFRN && $r[0]['network'] !== NETWORK_FACEBOOK && $r[0]['network'] !== NETWORK_DIASPORA && $r[0]['writable'] && (! get_pconfig(local_user(),'system','nowarn_insecure'))) {
320                                 notice( t('Private messages to this person are at risk of public disclosure.') . EOL);
321                         }
322
323                 }
324                 else {
325                         notice( t('Invalid contact.') . EOL);
326                         goaway($a->get_baseurl() . '/network');
327                         // NOTREACHED
328                 }
329         }
330
331         if((! $group) && (! $cid) && (! $update))
332                 $o .= get_birthdays();
333
334         $sql_extra2 = (($nouveau) ? '' : " AND `item`.`parent` = `item`.`id` ");
335
336         if(x($_GET,'search'))
337                 $sql_extra .= " AND `item`.`body` REGEXP '" . dbesc(escape_tags($_GET['search'])) . "' ";
338
339         
340         $r = q("SELECT COUNT(*) AS `total`
341                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
342                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
343                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
344                 $sql_extra2
345                 $sql_extra ",
346                 intval($_SESSION['uid'])
347         );
348
349         if(count($r)) {
350                 $a->set_pager_total($r[0]['total']);
351                 $a->set_pager_itemspage(40);
352         }
353
354
355         if($nouveau) {
356
357                 // "New Item View" - show all items unthreaded in reverse created date order
358
359                 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
360                         `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`writable`,
361                         `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
362                         `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
363                         FROM `item`, `contact`
364                         WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
365                         AND `contact`.`id` = `item`.`contact-id`
366                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
367                         $sql_extra
368                         ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
369                         intval($_SESSION['uid']),
370                         intval($a->pager['start']),
371                         intval($a->pager['itemspage'])
372                 );
373                 
374         }
375         else {
376
377                 // Normal conversation view
378
379
380                 if($order === 'post')
381                                 $ordering = "`created`";
382                 else
383                                 $ordering = "`commented`";
384
385                 // Fetch a page full of parent items for this page
386
387                 $r = q("SELECT `item`.`id` AS `item_id`, `contact`.`uid` AS `contact_uid`
388                         FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
389                         WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
390                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
391                         AND `item`.`parent` = `item`.`id`
392                         $sql_extra
393                         ORDER BY `item`.$ordering DESC LIMIT %d ,%d ",
394                         intval(local_user()),
395                         intval($a->pager['start']),
396                         intval($a->pager['itemspage'])
397                 );
398
399                 // Then fetch all the children of the parents that are on this page
400
401                 $parents_arr = array();
402                 $parents_str = '';
403
404                 if(count($r)) {
405                         foreach($r as $rr)
406                                 $parents_arr[] = $rr['item_id'];
407                         $parents_str = implode(', ', $parents_arr);
408
409                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
410                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`writable`,
411                                 `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
412                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
413                                 FROM `item`, (SELECT `p`.`id`,`p`.`created`,`p`.`commented` FROM `item` AS `p` WHERE `p`.`parent`=`p`.`id`) as `parentitem`, `contact`
414                                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
415                                 AND `contact`.`id` = `item`.`contact-id`
416                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
417                                 AND `item`.`parent` = `parentitem`.`id` AND `item`.`parent` IN ( %s )
418                                 $sql_extra
419                                 ORDER BY `parentitem`.$ordering DESC, `parentitem`.`id` ASC, `item`.`gravity` ASC, `item`.`created` ASC ",
420                                 intval(local_user()),
421                                 dbesc($parents_str)
422                         );
423                 }       
424         }
425
426         // Set this so that the conversation function can find out contact info for our wall-wall items
427         $a->page_contact = $a->contact;
428
429         $mode = (($nouveau) ? 'network-new' : 'network');
430
431         $o .= conversation($a,$r,$mode,$update);
432
433         if(! $update) {
434                 $o .= paginate($a);
435         }
436
437         return $o;
438 }