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