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