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