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