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