]> git.mxchange.org Git - friendica.git/blob - mod/network.php
Merge remote-tracking branch 'upstream/master'
[friendica.git] / mod / network.php
1 <?php
2
3
4 function network_init(&$a) {
5         if(! local_user()) {
6                 notice( t('Permission denied.') . EOL);
7                 return;
8         }
9   
10         $group_id = (($a->argc > 1 && intval($a->argv[1])) ? intval($a->argv[1]) : 0);
11                   
12         require_once('include/group.php');
13         if(! x($a->page,'aside'))
14                 $a->page['aside'] = '';
15
16         $search = ((x($_GET,'search')) ? escape_tags($_GET['search']) : '');
17
18         // We need a better way of managing a growing argument list
19
20         // moved into savedsearches()
21         // $srchurl = '/network' 
22         //              . ((x($_GET,'cid')) ? '?cid=' . $_GET['cid'] : '') 
23         //              . ((x($_GET,'star')) ? '?star=' . $_GET['star'] : '')
24         //              . ((x($_GET,'bmark')) ? '?bmark=' . $_GET['bmark'] : '');
25         
26         if(x($_GET,'save')) {
27                 $r = q("select * from `search` where `uid` = %d and `term` = '%s' limit 1",
28                         intval(local_user()),
29                         dbesc($search)
30                 );
31                 if(! count($r)) {
32                         q("insert into `search` ( `uid`,`term` ) values ( %d, '%s') ",
33                                 intval(local_user()),
34                                 dbesc($search)
35                         );
36                 }
37         }
38         if(x($_GET,'remove')) {
39                 q("delete from `search` where `uid` = %d and `term` = '%s' limit 1",
40                         intval(local_user()),
41                         dbesc($search)
42                 );
43         }
44
45         // item filter tabs
46         // TODO: fix this logic, reduce duplication
47         $a->page['content'] .= '<div class="tabs-wrapper">';
48         
49         $starred_active = '';
50         $new_active = '';
51         $bookmarked_active = '';
52         $all_active = '';
53         $search_active = '';
54         
55         if(($a->argc > 1 && $a->argv[1] === 'new') 
56                 || ($a->argc > 2 && $a->argv[2] === 'new')) {
57                         $new_active = 'active';
58         }
59         
60         if(x($_GET,'search')) {
61                 $search_active = 'active';
62         }
63         
64         if(x($_GET,'star')) {
65                 $starred_active = 'active';
66         }
67         
68         if($_GET['bmark']) {
69                 $bookmarked_active = 'active';
70         }
71         
72         if (($new_active == '') 
73                 && ($starred_active == '') 
74                 && ($bookmarked_active == '')
75                 && ($search_active == '')) {
76                         $all_active = 'active';
77         }
78         
79         // network links moved to content to match other pages
80         // all
81         $a->page['content'] .= '<a class="tabs ' . $all_active . '" href="' . $a->get_baseurl() . '/' 
82                 . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '?cid=' . $_GET['cid'] : '') . '">' 
83                 . t('All') . '</a>';
84                 
85         // new
86         $a->page['content'] .= '<a class="tabs ' . $new_active . '" href="' . $a->get_baseurl() . '/' 
87                 . str_replace('/new', '', $a->cmd) . '/new' 
88                 . ((x($_GET,'cid')) ? '/?cid=' . $_GET['cid'] : '') . '">' 
89                 . t('New') . '</a>';
90         
91         // starred
92         $a->page['content'] .= '<a class="tabs ' . $starred_active . '" href="' . $a->get_baseurl() . '/'
93                 . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '/?cid=' . $_GET['cid'] : '') . '&star=1" >' 
94                 . t('Starred') . '</a>';
95         
96         // bookmarks
97         $a->page['content'] .= '<a class="tabs ' . $bookmarked_active . '" href="' . $a->get_baseurl() . '/'
98                 . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '/?cid=' . $_GET['cid'] : '') . '&bmark=1" >' 
99                 . t('Bookmarks') . '</a>';
100         
101         $a->page['content'] .= '</div>';
102         // --- end item filter tabs
103         
104         // search terms header
105         if(x($_GET,'search')) {
106                 $a->page['content'] .= '<h2>Search Results For: '  . $search . '</h2>';
107         }
108         
109         $a->page['aside'] .= group_side('network','network',true,$group_id);
110         
111         // moved to saved searches to have it in the same div
112         //$a->page['aside'] .= search($search,'netsearch-box',$srchurl,true);
113
114         $a->page['aside'] .= saved_searches($search);
115
116 }
117
118 function saved_searches($search) {
119
120         $srchurl = '/network' 
121                 . ((x($_GET,'cid')) ? '?cid=' . $_GET['cid'] : '') 
122                 . ((x($_GET,'star')) ? '?star=' . $_GET['star'] : '')
123                 . ((x($_GET,'bmark')) ? '?bmark=' . $_GET['bmark'] : '');
124         
125         $o = '';
126
127         $r = q("select `term` from `search` WHERE `uid` = %d",
128                 intval(local_user())
129         );
130
131         $o .= '<div id="saved-search-list" class="widget">';
132         $o .= '<h3 id="search">' . t('Saved Searches') . '</h3>' . "\r\n";
133         $o .= search($search,'netsearch-box',$srchurl,true);
134         
135         if(count($r)) {
136                 $o .= '<ul id="saved-search-ul">' . "\r\n";
137                 foreach($r as $rr) {
138                         $o .= '<li class="saved-search-li clear"><a href="network/?f=&remove=1&search=' . $rr['term'] . '" class="icon drophide savedsearchdrop" title="' . t('Remove term') . '" onclick="return confirmDelete();" onmouseover="imgbright(this);" onmouseout="imgdull(this);" ></a> <a href="network/?f&search=' . $rr['term'] . '" class="savedsearchterm" >' . $rr['term'] . '</a></li>' . "\r\n";
139                 }
140                 $o .= '</ul>';
141         }               
142
143         $o .= '</div>' . "\r\n";
144         return $o;
145
146 }
147
148
149 function network_content(&$a, $update = 0) {
150
151         require_once('include/conversation.php');
152
153         if(! local_user())
154         return login(false);
155
156         $o = '';
157
158         $contact_id = $a->cid;
159
160         $group = 0;
161
162         $nouveau = false;
163         require_once('include/acl_selectors.php');
164
165         $cid = ((x($_GET,'cid')) ? intval($_GET['cid']) : 0);
166         $star = ((x($_GET,'star')) ? intval($_GET['star']) : 0);
167         $bmark = ((x($_GET,'bmark')) ? intval($_GET['bmark']) : 0);
168         $order = ((x($_GET,'order')) ? notags($_GET['order']) : 'comment');
169         $liked = ((x($_GET,'liked')) ? intval($_GET['liked']) : 0);
170
171
172         if(($a->argc > 2) && $a->argv[2] === 'new')
173                 $nouveau = true;
174
175         if($a->argc > 1) {
176                 if($a->argv[1] === 'new')
177                         $nouveau = true;
178                 else {
179                         $group = intval($a->argv[1]);
180                         $def_acl = array('allow_gid' => '<' . $group . '>');
181                 }
182         }
183
184         if(x($_GET,'search'))
185                 $nouveau = true;
186         if($cid)
187                 $def_acl = array('allow_cid' => '<' . intval($cid) . '>');
188
189         if(! $update) {
190                 if(group) {
191                         if(($t = group_public_members($group)) && (! get_pconfig(local_user(),'system','nowarn_insecure'))) {
192                                 notice( sprintf( tt('Warning: This group contains %s member from an insecure network.',
193                                                                         'Warning: This group contains %s members from an insecure network.',
194                                                                         $t), $t ) . EOL);
195                                 notice( t('Private messages to this group are at risk of public disclosure.') . EOL);
196                         }
197                 }
198
199                 $o .= '<script> $(document).ready(function() { $(\'#nav-network-link\').addClass(\'nav-selected\'); });</script>';
200
201                 $_SESSION['return_url'] = $a->cmd;
202
203                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
204
205                 $x = array(
206                         'is_owner' => true,
207                         'allow_location' => $a->user['allow_location'],
208                         'default_location' => $a->user['default_location'],
209                         'nickname' => $a->user['nickname'],
210                         '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'),
211                         'acl' => populate_acl((($group || $cid) ? $def_acl : $a->user), $celeb),
212                         'bang' => (($group || $cid) ? '!' : ''),
213                         'visitor' => 'block',
214                         'profile_uid' => local_user()
215                 );
216
217                 $o .= status_editor($a,$x);
218
219                 // The special div is needed for liveUpdate to kick in for this page.
220                 // We only launch liveUpdate if you are on the front page, you aren't
221                 // filtering by group and also you aren't writing a comment (the last
222                 // criteria is discovered in javascript).
223
224                         $o .= '<div id="live-network"></div>' . "\r\n";
225                         $o .= "<script> var profile_uid = " . $_SESSION['uid'] 
226                                 . "; var netargs = '" . substr($a->cmd,8)
227                                 . '?f='
228                                 . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : '')
229                                 . ((x($_GET,'search')) ? '&search=' . $_GET['search'] : '') 
230                                 . ((x($_GET,'star')) ? '&star=' . $_GET['star'] : '') 
231                                 . ((x($_GET,'order')) ? '&order=' . $_GET['order'] : '') 
232                                 . ((x($_GET,'bmark')) ? '&bmark=' . $_GET['bmark'] : '') 
233                                 . ((x($_GET,'liked')) ? '&liked=' . $_GET['liked'] : '') 
234                                 . "'; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
235
236         }
237
238         // We aren't going to try and figure out at the item, group, and page
239         // level which items you've seen and which you haven't. If you're looking
240         // at the top level network page just mark everything seen. 
241         
242         if((! $group) && (! $cid) && (! $star)) {
243                 $r = q("UPDATE `item` SET `unseen` = 0 
244                         WHERE `unseen` = 1 AND `uid` = %d",
245                         intval($_SESSION['uid'])
246                 );
247         }
248
249         // We don't have to deal with ACL's on this page. You're looking at everything
250         // that belongs to you, hence you can see all of it. We will filter by group if
251         // desired. 
252
253         $star_sql = (($star) ?  " AND `starred` = 1 " : '');
254
255         if($bmark)
256                 $star_sql .= " AND `bookmark` = 1 ";
257
258         $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $star_sql ) ";
259
260         if($group) {
261                 $r = q("SELECT `name`, `id` FROM `group` WHERE `id` = %d AND `uid` = %d LIMIT 1",
262                         intval($group),
263                         intval($_SESSION['uid'])
264                 );
265                 if(! count($r)) {
266                         if($update)
267                                 killme();
268                         notice( t('No such group') . EOL );
269                         goaway($a->get_baseurl() . '/network');
270                         // NOTREACHED
271                 }
272
273                 $contacts = expand_groups(array($group));
274                 if((is_array($contacts)) && count($contacts)) {
275                         $contact_str = implode(',',$contacts);
276                 }
277                 else {
278                                 $contact_str = ' 0 ';
279                                 info( t('Group is empty'));
280                 }
281
282
283                 $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) . ">' )) ";
284                 $o = '<h2>' . t('Group: ') . $r[0]['name'] . '</h2>' . $o;
285         }
286         elseif($cid) {
287
288                 $r = q("SELECT `id`,`name`,`network`,`writable` FROM `contact` WHERE `id` = %d 
289                                 AND `blocked` = 0 AND `pending` = 0 LIMIT 1",
290                         intval($cid)
291                 );
292                 if(count($r)) {
293                         $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $star_sql AND `contact-id` IN ( " . intval($cid) . " )) ";
294                         $o = '<h2>' . t('Contact: ') . $r[0]['name'] . '</h2>' . $o;
295                         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'))) {
296                                 notice( t('Private messages to this person are at risk of public disclosure.') . EOL);
297                         }
298
299                 }
300                 else {
301                         notice( t('Invalid contact.') . EOL);
302                         goaway($a->get_baseurl() . '/network');
303                         // NOTREACHED
304                 }
305         }
306
307         if((! $group) && (! $cid) && (! $update))
308                 $o .= get_birthdays();
309
310         $sql_extra2 = (($nouveau) ? '' : " AND `item`.`parent` = `item`.`id` ");
311
312         if(x($_GET,'search'))
313                 $sql_extra .= " AND `item`.`body` REGEXP '" . dbesc(escape_tags($_GET['search'])) . "' ";
314
315         
316         $r = q("SELECT COUNT(*) AS `total`
317                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
318                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
319                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
320                 $sql_extra2
321                 $sql_extra ",
322                 intval($_SESSION['uid'])
323         );
324
325         if(count($r)) {
326                 $a->set_pager_total($r[0]['total']);
327                 $a->set_pager_itemspage(40);
328         }
329
330
331         if($nouveau) {
332
333                 // "New Item View" - show all items unthreaded in reverse created date order
334
335                 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
336                         `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`writable`,
337                         `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
338                         `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
339                         FROM `item`, `contact`
340                         WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
341                         AND `contact`.`id` = `item`.`contact-id`
342                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
343                         $sql_extra
344                         ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
345                         intval($_SESSION['uid']),
346                         intval($a->pager['start']),
347                         intval($a->pager['itemspage'])
348                 );
349                 
350         }
351         else {
352
353                 // Normal conversation view
354
355
356                 if($order === 'post')
357                                 $ordering = "`created`";
358                 else
359                                 $ordering = "`commented`";
360
361                 // Fetch a page full of parent items for this page
362
363                 $r = q("SELECT `item`.`id` AS `item_id`, `contact`.`uid` AS `contact_uid`
364                         FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
365                         WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
366                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
367                         AND `item`.`parent` = `item`.`id`
368                         $sql_extra
369                         ORDER BY `item`.$ordering DESC LIMIT %d ,%d ",
370                         intval(local_user()),
371                         intval($a->pager['start']),
372                         intval($a->pager['itemspage'])
373                 );
374
375                 // Then fetch all the children of the parents that are on this page
376
377                 $parents_arr = array();
378                 $parents_str = '';
379
380                 if(count($r)) {
381                         foreach($r as $rr)
382                                 $parents_arr[] = $rr['item_id'];
383                         $parents_str = implode(', ', $parents_arr);
384
385                         $r = q("SELECT `item`.*, `item`.`id` AS `item_id`,
386                                 `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`writable`,
387                                 `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
388                                 `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
389                                 FROM `item`, (SELECT `p`.`id`,`p`.`created`,`p`.`commented` FROM `item` AS `p` WHERE `p`.`parent`=`p`.`id`) as `parentitem`, `contact`
390                                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
391                                 AND `contact`.`id` = `item`.`contact-id`
392                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
393                                 AND `item`.`parent` = `parentitem`.`id` AND `item`.`parent` IN ( %s )
394                                 $sql_extra
395                                 ORDER BY `parentitem`.$ordering DESC, `parentitem`.`id` ASC, `item`.`gravity` ASC, `item`.`created` ASC ",
396                                 intval(local_user()),
397                                 dbesc($parents_str)
398                         );
399                 }       
400         }
401
402         // Set this so that the conversation function can find out contact info for our wall-wall items
403         $a->page_contact = $a->contact;
404
405         $mode = (($nouveau) ? 'network-new' : 'network');
406
407         $o .= conversation($a,$r,$mode,$update);
408
409         if(! $update) {
410                 $o .= paginate($a);
411         }
412
413         return $o;
414 }