]> git.mxchange.org Git - friendica.git/blob - mod/network.php
don't make -desc so obnoxious looking
[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         require_once('include/contact_widgets.php');
14
15         if(! x($a->page,'aside'))
16                 $a->page['aside'] = '';
17
18         $search = ((x($_GET,'search')) ? escape_tags($_GET['search']) : '');
19
20         if(x($_GET,'save')) {
21                 $r = q("select * from `search` where `uid` = %d and `term` = '%s' limit 1",
22                         intval(local_user()),
23                         dbesc($search)
24                 );
25                 if(! count($r)) {
26                         q("insert into `search` ( `uid`,`term` ) values ( %d, '%s') ",
27                                 intval(local_user()),
28                                 dbesc($search)
29                         );
30                 }
31         }
32         if(x($_GET,'remove')) {
33                 q("delete from `search` where `uid` = %d and `term` = '%s' limit 1",
34                         intval(local_user()),
35                         dbesc($search)
36                 );
37         }
38
39
40         
41         // search terms header
42         if(x($_GET,'search')) {
43                 $a->page['content'] .= '<h2>' . t('Search Results For:') . ' '  . $search . '</h2>';
44         }
45         
46         $a->page['aside'] .= group_side('network','network',true,$group_id);
47         $a->page['aside'] .= networks_widget($a->get_baseurl(true) . '/network',(x($_GET, 'nets') ? $_GET['nets'] : ''));
48         $a->page['aside'] .= saved_searches($search);
49         $a->page['aside'] .= fileas_widget($a->get_baseurl(true) . '/network',(x($_GET, 'file') ? $_GET['file'] : ''));
50
51 }
52
53 function saved_searches($search) {
54
55         $a = get_app();
56
57         $srchurl = '/network?f=' 
58                 . ((x($_GET,'cid'))   ? '&cid='   . $_GET['cid']   : '') 
59                 . ((x($_GET,'star'))  ? '&star='  . $_GET['star']  : '')
60                 . ((x($_GET,'bmark')) ? '&bmark=' . $_GET['bmark'] : '')
61                 . ((x($_GET,'conv'))  ? '&conv='  . $_GET['conv']  : '')
62                 . ((x($_GET,'nets'))  ? '&nets='  . $_GET['nets']  : '')
63                 . ((x($_GET,'cmin'))  ? '&cmin='  . $_GET['cmin']  : '')
64                 . ((x($_GET,'cmax'))  ? '&cmax='  . $_GET['cmax']  : '')
65                 . ((x($_GET,'file'))  ? '&file='  . $_GET['file']  : '');
66         ;
67         
68         $o = '';
69
70         $r = q("select `id`,`term` from `search` WHERE `uid` = %d",
71                 intval(local_user())
72         );
73
74         $saved = array();
75
76         if(count($r)) {
77                 foreach($r as $rr) {
78                         $saved[] = array(
79                                 'id'            => $rr['id'],
80                                 'term'                  => $rr['term'],
81                                 'encodedterm'   => urlencode($rr['term']),
82                                 'delete'                => t('Remove term'),
83                                 'selected'              => ($search==$rr['term']),
84                         );
85                 }
86         }               
87
88         
89         $tpl = get_markup_template("saved_searches_aside.tpl");
90         $o = replace_macros($tpl, array(
91                 '$title'         => t('Saved Searches'),
92                 '$add'           => t('add'),
93                 '$searchbox' => search($search,'netsearch-box',$srchurl,true),
94                 '$saved'         => $saved,
95         ));
96         
97         return $o;
98
99 }
100
101
102 function network_content(&$a, $update = 0) {
103
104         require_once('include/conversation.php');
105
106         if(! local_user()) {
107                 $_SESSION['return_url'] = $a->query_string;
108         return login(false);
109         }
110
111         $o = '';
112
113         // item filter tabs
114         // TODO: fix this logic, reduce duplication
115         //$a->page['content'] .= '<div class="tabs-wrapper">';
116         
117         $starred_active = '';
118         $new_active = '';
119         $bookmarked_active = '';
120         $all_active = '';
121         $search_active = '';
122         $conv_active = '';
123         $spam_active = '';
124
125         if(($a->argc > 1 && $a->argv[1] === 'new') 
126                 || ($a->argc > 2 && $a->argv[2] === 'new')) {
127                         $new_active = 'active';
128         }
129         
130         if(x($_GET,'search')) {
131                 $search_active = 'active';
132         }
133         
134         if(x($_GET,'star')) {
135                 $starred_active = 'active';
136         }
137         
138         if(x($_GET,'bmark')) {
139                 $bookmarked_active = 'active';
140         }
141
142         if(x($_GET,'conv')) {
143                 $conv_active = 'active';
144         }
145
146         if(x($_GET,'spam')) {
147                 $spam_active = 'active';
148         }
149
150         
151         if (($new_active == '') 
152                 && ($starred_active == '') 
153                 && ($bookmarked_active == '')
154                 && ($conv_active == '')
155                 && ($search_active == '')
156                 && ($spam_active == '')) {
157                         $all_active = 'active';
158         }
159
160
161         $postord_active = '';
162
163         if($all_active && x($_GET,'order') && $_GET['order'] !== 'comment') {
164                 $all_active = '';
165                 $postord_active = 'active';
166         }
167                                 
168         // tabs
169         $tabs = array(
170                 array(
171                         'label' => t('Commented Order'),
172                         'url'=>$a->get_baseurl(true) . '/' . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '?f=&cid=' . $_GET['cid'] : ''), 
173                         'sel'=>$all_active,
174                         'title'=> t('Sort by Comment Date'),
175                 ),
176                 array(
177                         'label' => t('Posted Order'),
178                         'url'=>$a->get_baseurl(true) . '/' . str_replace('/new', '', $a->cmd) . '?f=&order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''), 
179                         'sel'=>$postord_active,
180                         'title' => t('Sort by Post Date'),
181                 ),
182
183                 array(
184                         'label' => t('Personal'),
185                         'url' => $a->get_baseurl(true) . '/' . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&conv=1',
186                         'sel' => $conv_active,
187                         'title' => t('Posts that mention or involve you'),
188                 ),
189                 array(
190                         'label' => t('New'),
191                         'url' => $a->get_baseurl(true) . '/' . str_replace('/new', '', $a->cmd) . '/new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''),
192                         'sel' => $new_active,
193                         'title' => t('Activity Stream - by date'),
194                 ),
195                 array(
196                         'label' => t('Starred'),
197                         'url'=>$a->get_baseurl(true) . '/' . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&star=1',
198                         'sel'=>$starred_active,
199                         'title' => t('Favourite Posts'),
200                 ),
201                 array(
202                         'label' => t('Shared Links'),
203                         'url'=>$a->get_baseurl(true) . '/' . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '') . '&bmark=1',
204                         'sel'=>$bookmarked_active,
205                         'title'=> t('Interesting Links'),
206                 ),      
207 //              array(
208 //                      'label' => t('Spam'),
209 //                      'url'=>$a->get_baseurl(true) . '/network?f=&spam=1'
210 //                      'sel'=> $spam_active,
211 //                      'title' => t('Posts flagged as SPAM'),
212 //              ),      
213
214
215         );
216
217         $arr = array('tabs' => $tabs);
218         call_hooks('network_tabs', $arr);
219
220         $o .= replace_macros(get_markup_template('common_tabs.tpl'), array('$tabs'=> $arr['tabs']));
221
222         // --- end item filter tabs
223
224
225
226         
227
228         $contact_id = $a->cid;
229
230         $group = 0;
231
232         $nouveau = false;
233         require_once('include/acl_selectors.php');
234
235         $cid = ((x($_GET,'cid')) ? intval($_GET['cid']) : 0);
236         $star = ((x($_GET,'star')) ? intval($_GET['star']) : 0);
237         $bmark = ((x($_GET,'bmark')) ? intval($_GET['bmark']) : 0);
238         $order = ((x($_GET,'order')) ? notags($_GET['order']) : 'comment');
239         $liked = ((x($_GET,'liked')) ? intval($_GET['liked']) : 0);
240         $conv = ((x($_GET,'conv')) ? intval($_GET['conv']) : 0);
241         $spam = ((x($_GET,'spam')) ? intval($_GET['spam']) : 0);
242         $nets = ((x($_GET,'nets')) ? $_GET['nets'] : '');
243         $cmin = ((x($_GET,'cmin')) ? intval($_GET['cmin']) : 0);
244         $cmax = ((x($_GET,'cmax')) ? intval($_GET['cmax']) : 99);
245         $file = ((x($_GET,'file')) ? $_GET['file'] : '');
246
247         if(($a->argc > 2) && $a->argv[2] === 'new')
248                 $nouveau = true;
249
250         if($a->argc > 1) {
251                 if($a->argv[1] === 'new')
252                         $nouveau = true;
253                 else {
254                         $group = intval($a->argv[1]);
255                         $def_acl = array('allow_gid' => '<' . $group . '>');
256                 }
257         }
258
259         if(x($_GET,'search') || x($_GET,'file'))
260                 $nouveau = true;
261         if($cid)
262                 $def_acl = array('allow_cid' => '<' . intval($cid) . '>');
263
264         if($nets) {
265                 $r = q("select id from contact where uid = %d and network = '%s' and self = 0",
266                         intval(local_user()),
267                         dbesc($nets)
268                 );
269
270                 $str = '';
271                 if(count($r))
272                         foreach($r as $rr)
273                                 $str .= '<' . $rr['id'] . '>';
274                 if(strlen($str))
275                         $def_acl = array('allow_cid' => $str);
276         }
277
278         if(! $update) {
279                 if($group) {
280                         if(($t = group_public_members($group)) && (! get_pconfig(local_user(),'system','nowarn_insecure'))) {
281                                 notice( sprintf( tt('Warning: This group contains %s member from an insecure network.',
282                                                                         'Warning: This group contains %s members from an insecure network.',
283                                                                         $t), $t ) . EOL);
284                                 notice( t('Private messages to this group are at risk of public disclosure.') . EOL);
285                         }
286                 }
287
288                 nav_set_selected('network');
289
290                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
291
292                 $x = array(
293                         'is_owner' => true,
294                         'allow_location' => $a->user['allow_location'],
295                         'default_location' => $a->user['default-location'],
296                         'nickname' => $a->user['nickname'],
297                         'lockstate' => ((($group) || ($cid) || ($nets) || (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'),
298                         'acl' => populate_acl((($group || $cid || $nets) ? $def_acl : $a->user), $celeb),
299                         'bang' => (($group || $cid || $nets) ? '!' : ''),
300                         'visitor' => 'block',
301                         'profile_uid' => local_user()
302                 );
303
304                 $o .= status_editor($a,$x);
305
306         }
307
308
309         // We don't have to deal with ACL's on this page. You're looking at everything
310         // that belongs to you, hence you can see all of it. We will filter by group if
311         // desired. 
312
313         
314         $sql_options  = (($star) ? " and starred = 1 " : '');
315         $sql_options .= (($bmark) ? " and bookmark = 1 " : '');
316
317         $sql_nets = (($nets) ? sprintf(" and `contact`.`network` = '%s' ", dbesc($nets)) : '');
318
319         $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $sql_options ) ";
320
321         if($group) {
322                 $r = q("SELECT `name`, `id` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
323                         intval($group),
324                         intval($_SESSION['uid'])
325                 );
326                 if(! count($r)) {
327                         if($update)
328                                 killme();
329                         notice( t('No such group') . EOL );
330                         goaway($a->get_baseurl(true) . '/network');
331                         // NOTREACHED
332                 }
333
334                 $contacts = expand_groups(array($group));
335                 if((is_array($contacts)) && count($contacts)) {
336                         $contact_str = implode(',',$contacts);
337                 }
338                 else {
339                                 $contact_str = ' 0 ';
340                                 info( t('Group is empty'));
341                 }
342
343                 $sql_extra = " AND `item`.`parent` IN ( SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND ( `contact-id` IN ( $contact_str ) OR `allow_gid` like '" . protect_sprintf('%<' . intval($group) . '>%') . "' ) and deleted = 0 ) ";
344                 $o = '<h2>' . t('Group: ') . $r[0]['name'] . '</h2>' . $o;
345         }
346         elseif($cid) {
347
348                 $r = q("SELECT `id`,`name`,`network`,`writable`,`nurl` FROM `contact` WHERE `id` = %d 
349                                 AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
350                         intval($cid)
351                 );
352                 if(count($r)) {
353                         $sql_extra = " AND `item`.`parent` IN ( SELECT DISTINCT(`parent`) FROM `item` WHERE 1 $sql_options AND `contact-id` = " . intval($cid) . " and deleted = 0 ) ";
354                         $o = '<h2>' . t('Contact: ') . $r[0]['name'] . '</h2>' . $o;
355                         if($r[0]['network'] === NETWORK_OSTATUS && $r[0]['writable'] && (! get_pconfig(local_user(),'system','nowarn_insecure'))) {
356                                 notice( t('Private messages to this person are at risk of public disclosure.') . EOL);
357                         }
358
359                 }
360                 else {
361                         notice( t('Invalid contact.') . EOL);
362                         goaway($a->get_baseurl(true) . '/network');
363                         // NOTREACHED
364                 }
365         }
366
367         if((! $group) && (! $cid) && (! $update)) {
368                 $o .= get_birthdays();
369                 $o .= get_events();
370         }
371
372         if(! $update) {
373                 // The special div is needed for liveUpdate to kick in for this page.
374                 // We only launch liveUpdate if you aren't filtering in some incompatible 
375                 // way and also you aren't writing a comment (discovered in javascript).
376
377                 $o .= '<div id="live-network"></div>' . "\r\n";
378                 $o .= "<script> var profile_uid = " . $_SESSION['uid'] 
379                         . "; var netargs = '" . substr($a->cmd,8)
380                         . '?f='
381                         . ((x($_GET,'cid'))    ? '&cid='    . $_GET['cid']    : '')
382                         . ((x($_GET,'search')) ? '&search=' . $_GET['search'] : '') 
383                         . ((x($_GET,'star'))   ? '&star='   . $_GET['star']   : '') 
384                         . ((x($_GET,'order'))  ? '&order='  . $_GET['order']  : '') 
385                         . ((x($_GET,'bmark'))  ? '&bmark='  . $_GET['bmark']  : '') 
386                         . ((x($_GET,'liked'))  ? '&liked='  . $_GET['liked']  : '') 
387                         . ((x($_GET,'conv'))   ? '&conv='   . $_GET['conv']   : '') 
388                         . ((x($_GET,'spam'))   ? '&spam='   . $_GET['spam']   : '') 
389                         . ((x($_GET,'nets'))   ? '&nets='   . $_GET['nets']   : '') 
390                         . ((x($_GET,'cmin'))   ? '&cmin='   . $_GET['cmin']   : '') 
391                         . ((x($_GET,'cmax'))   ? '&cmax='   . $_GET['cmax']   : '') 
392                         . ((x($_GET,'file'))   ? '&file='   . $_GET['file']   : '') 
393
394                         . "'; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
395         }
396
397         $sql_extra2 = (($nouveau) ? '' : " AND `item`.`parent` = `item`.`id` ");
398
399         if(x($_GET,'search')) {
400                 $search = escape_tags($_GET['search']);
401                 $sql_extra .= sprintf(" AND ( `item`.`body` like '%s' OR `item`.`tag` like '%s' ) ",
402                         dbesc(protect_sprintf('%' . $search . '%')),
403                         dbesc(protect_sprintf('%]' . $search . '[%'))
404                 );
405         }
406         if(strlen($file)) {
407                 $sql_extra .= file_tag_file_query('item',unxmlify($file));
408         }
409
410         if($conv) {
411                 $myurl = $a->get_baseurl() . '/profile/'. $a->user['nickname'];
412                 $myurl = substr($myurl,strpos($myurl,'://')+3);
413                 $myurl = str_replace(array('www.','.'),array('','\\.'),$myurl);
414                 $diasp_url = str_replace('/profile/','/u/',$myurl);
415                 $sql_extra .= sprintf(" AND `item`.`parent` IN (SELECT distinct(`parent`) from item where ( `author-link` like '%s' or `tag` like '%s' or tag like '%s' )) ",
416                         dbesc(protect_sprintf('%s' . $myurl)),
417                         dbesc(protect_sprintf('%' . $myurl . '\\]%')),
418                         dbesc(protect_sprintf('%' . $diasp_url . '\\]%'))
419                 );
420         }
421
422
423         if($update) {
424
425                 // only setup pagination on initial page view
426                 $pager_sql = '';
427
428         }
429         else {
430                 $r = q("SELECT COUNT(*) AS `total`
431                         FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
432                         WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
433                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
434                         $sql_extra2
435                         $sql_extra $sql_nets ",
436                         intval($_SESSION['uid'])
437                 );
438
439                 if(count($r)) {
440                         $a->set_pager_total($r[0]['total']);
441                         $itemspage_network = get_pconfig(local_user(),'system','itemspage_network');
442                         $a->set_pager_itemspage(((intval($itemspage_network)) ? $itemspage_network : 40));
443                 }
444                 $pager_sql = sprintf(" LIMIT %d, %d ",intval($a->pager['start']), intval($a->pager['itemspage']));
445         }
446
447         $simple_update = (($update) ? " and `item`.`unseen` = 1 " : '');
448
449         if($nouveau) {
450                 // "New Item View" - show all items unthreaded in reverse created date order
451
452                 $items = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
453                         `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`writable`,
454                         `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
455                         `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
456                         FROM `item`, `contact`
457                         WHERE `item`.`uid` = %d AND `item`.`visible` = 1 
458                         AND `item`.`deleted` = 0 and `item`.`moderated` = 0
459                         $simple_update
460                         AND `contact`.`id` = `item`.`contact-id`
461                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
462                         $sql_extra $sql_nets
463                         ORDER BY `item`.`received` DESC $pager_sql ",
464                         intval($_SESSION['uid'])
465                 );
466
467         }
468         else {
469
470                 // Normal conversation view
471
472
473                 if($order === 'post')
474                                 $ordering = "`created`";
475                 else
476                                 $ordering = "`commented`";
477
478                 // Fetch a page full of parent items for this page
479
480                 if($update) {
481                         $r = q("SELECT `parent` AS `item_id`, `contact`.`uid` AS `contact_uid`
482                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
483                                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
484                                 and `item`.`moderated` = 0 and `item`.`unseen` = 1
485                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
486                                 $sql_extra $sql_nets ",
487                                 intval(local_user())
488                         );
489                 }
490                 else {
491                         $r = q("SELECT `item`.`id` AS `item_id`, `contact`.`uid` AS `contact_uid`
492                                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
493                                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
494                                 AND `item`.`moderated` = 0 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
495                                 AND `item`.`parent` = `item`.`id`
496                                 $sql_extra $sql_nets
497                                 ORDER BY `item`.$ordering DESC $pager_sql ",
498                                 intval(local_user())
499                         );
500                 }
501
502                 // Then fetch all the children of the parents that are on this page
503
504                 $parents_arr = array();
505                 $parents_str = '';
506
507                 if(count($r)) {
508                         foreach($r as $rr)
509                                 if(! in_array($rr['item_id'],$parents_arr))
510                                         $parents_arr[] = $rr['item_id'];
511                         $parents_str = implode(', ', $parents_arr);
512
513                         $items = q("SELECT `item`.*, `item`.`id` AS `item_id`,
514                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`writable`,
515                                 `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
516                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
517                                 FROM `item`, `contact`
518                                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
519                                 AND `item`.`moderated` = 0 AND `contact`.`id` = `item`.`contact-id`
520                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
521                                 AND `item`.`parent` IN ( %s )
522                                 $sql_extra ",
523                                 intval(local_user()),
524                                 dbesc($parents_str)
525                         );
526
527                         $items = conv_sort($items,$ordering);
528
529                 } else {
530                         $items = array();
531                 }
532         }
533
534
535         // We aren't going to try and figure out at the item, group, and page
536         // level which items you've seen and which you haven't. If you're looking
537         // at the top level network page just mark everything seen. 
538         
539         if((! $group) && (! $cid) && (! $star)) {
540                 $r = q("UPDATE `item` SET `unseen` = 0 
541                         WHERE `unseen` = 1 AND `uid` = %d",
542                         intval(local_user())
543                 );
544         }
545
546         // Set this so that the conversation function can find out contact info for our wall-wall items
547         $a->page_contact = $a->contact;
548
549         $mode = (($nouveau) ? 'network-new' : 'network');
550
551         $o .= conversation($a,$items,$mode,$update);
552
553         if(! $update) {
554                 $o .= paginate($a);
555         }
556
557         return $o;
558 }