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