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