]> git.mxchange.org Git - friendica.git/blob - mod/network.php
more work on quattro. remove html from php for groups and saved searches widgets.
[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         
68         $o = '';
69
70         $r = q("select `term` from `search` WHERE `uid` = %d",
71                 intval(local_user())
72         );
73
74         $saved = array();
75
76
77         
78         if(count($r)) {
79                 foreach($r as $rr) {
80                         $saved[] = array(
81                                 'term'                  => $rr['term'],
82                                 'encodedterm'   => urlencode($rr['term']),
83                                 'delete'                => t('Remove term'),
84                                 'selected'              => ($search==$rr['term']),
85                         );
86                 }
87         }               
88
89         
90         $tpl = get_markup_template("saved_searches_aside.tpl");
91         $o = replace_macros($tpl, array(
92                 '$title'         => t('Saved Searches'),
93                 '$add'           => t('add'),
94                 '$searchbox' => search($search,'netsearch-box',$srchurl,true),
95                 '$saved'         => $saved,
96         ));
97         
98         return $o;
99
100 }
101
102
103 function network_content(&$a, $update = 0) {
104
105         require_once('include/conversation.php');
106
107         if(! local_user())
108         return login(false);
109
110         $o = '';
111
112         // item filter tabs
113         // TODO: fix this logic, reduce duplication
114         //$a->page['content'] .= '<div class="tabs-wrapper">';
115         
116         $starred_active = '';
117         $new_active = '';
118         $bookmarked_active = '';
119         $all_active = '';
120         $search_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($_GET['bmark']) {
136                 $bookmarked_active = 'active';
137         }
138         
139         if (($new_active == '') 
140                 && ($starred_active == '') 
141                 && ($bookmarked_active == '')
142                 && ($search_active == '')) {
143                         $all_active = 'active';
144         }
145         $postord_active = '';
146
147         if($all_active && x($_GET,'order') && $_GET['order'] !== 'comment') {
148                 $all_active = '';
149                 $postord_active = 'active';
150         }
151                          
152
153         
154         // tabs
155         $tabs = array(
156                 array(
157                         'label' => t('Commented Order'),
158                         'url'=>$a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '?cid=' . $_GET['cid'] : ''), 
159                         'sel'=>$all_active,
160                 ),
161                 array(
162                         'label' => t('Posted Order'),
163                         'url'=>$a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . '?order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''), 
164                         'sel'=>$postord_active,
165                 ),
166
167                 array(
168                         'label' => t('New'),
169                         'url' => $a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . '/new' . ((x($_GET,'cid')) ? '/?cid=' . $_GET['cid'] : ''),
170                         'sel' => $new_active,
171                 ),
172                 array(
173                         'label' => t('Starred'),
174                         'url'=>$a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '/?cid=' . $_GET['cid'] : '') . '&star=1',
175                         'sel'=>$starred_active,
176                 ),
177                 array(
178                         'label' => t('Bookmarks'),
179                         'url'=>$a->get_baseurl() . '/' . str_replace('/new', '', $a->cmd) . ((x($_GET,'cid')) ? '/?cid=' . $_GET['cid'] : '') . '&bmark=1',
180                         'sel'=>$bookmarked_active,
181                 ),      
182         );
183         $tpl = get_markup_template('common_tabs.tpl');
184         $o .= replace_macros($tpl, array('$tabs'=>$tabs));
185         // --- end item filter tabs
186
187
188
189         
190
191         $contact_id = $a->cid;
192
193         $group = 0;
194
195         $nouveau = false;
196         require_once('include/acl_selectors.php');
197
198         $cid = ((x($_GET,'cid')) ? intval($_GET['cid']) : 0);
199         $star = ((x($_GET,'star')) ? intval($_GET['star']) : 0);
200         $bmark = ((x($_GET,'bmark')) ? intval($_GET['bmark']) : 0);
201         $order = ((x($_GET,'order')) ? notags($_GET['order']) : 'comment');
202         $liked = ((x($_GET,'liked')) ? intval($_GET['liked']) : 0);
203
204
205         if(($a->argc > 2) && $a->argv[2] === 'new')
206                 $nouveau = true;
207
208         if($a->argc > 1) {
209                 if($a->argv[1] === 'new')
210                         $nouveau = true;
211                 else {
212                         $group = intval($a->argv[1]);
213                         $def_acl = array('allow_gid' => '<' . $group . '>');
214                 }
215         }
216
217         if(x($_GET,'search'))
218                 $nouveau = true;
219         if($cid)
220                 $def_acl = array('allow_cid' => '<' . intval($cid) . '>');
221
222         if(! $update) {
223                 if(group) {
224                         if(($t = group_public_members($group)) && (! get_pconfig(local_user(),'system','nowarn_insecure'))) {
225                                 notice( sprintf( tt('Warning: This group contains %s member from an insecure network.',
226                                                                         'Warning: This group contains %s members from an insecure network.',
227                                                                         $t), $t ) . EOL);
228                                 notice( t('Private messages to this group are at risk of public disclosure.') . EOL);
229                         }
230                 }
231
232                 nav_set_selected('network');
233
234                 $_SESSION['return_url'] = $a->cmd;
235
236                 $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
237
238                 $x = array(
239                         'is_owner' => true,
240                         'allow_location' => $a->user['allow_location'],
241                         'default_location' => $a->user['default_location'],
242                         'nickname' => $a->user['nickname'],
243                         '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'),
244                         'acl' => populate_acl((($group || $cid) ? $def_acl : $a->user), $celeb),
245                         'bang' => (($group || $cid) ? '!' : ''),
246                         'visitor' => 'block',
247                         'profile_uid' => local_user()
248                 );
249
250                 $o .= status_editor($a,$x);
251
252                 // The special div is needed for liveUpdate to kick in for this page.
253                 // We only launch liveUpdate if you are on the front page, you aren't
254                 // filtering by group and also you aren't writing a comment (the last
255                 // criteria is discovered in javascript).
256
257                         $o .= '<div id="live-network"></div>' . "\r\n";
258                         $o .= "<script> var profile_uid = " . $_SESSION['uid'] 
259                                 . "; var netargs = '" . substr($a->cmd,8)
260                                 . '?f='
261                                 . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : '')
262                                 . ((x($_GET,'search')) ? '&search=' . $_GET['search'] : '') 
263                                 . ((x($_GET,'star')) ? '&star=' . $_GET['star'] : '') 
264                                 . ((x($_GET,'order')) ? '&order=' . $_GET['order'] : '') 
265                                 . ((x($_GET,'bmark')) ? '&bmark=' . $_GET['bmark'] : '') 
266                                 . ((x($_GET,'liked')) ? '&liked=' . $_GET['liked'] : '') 
267                                 . "'; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
268
269         }
270
271         // We aren't going to try and figure out at the item, group, and page
272         // level which items you've seen and which you haven't. If you're looking
273         // at the top level network page just mark everything seen. 
274         
275         if((! $group) && (! $cid) && (! $star)) {
276                 $r = q("UPDATE `item` SET `unseen` = 0 
277                         WHERE `unseen` = 1 AND `uid` = %d",
278                         intval($_SESSION['uid'])
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         $star_sql = (($star) ?  " AND `starred` = 1 " : '');
287
288         if($bmark)
289                 $star_sql .= " AND `bookmark` = 1 ";
290
291         $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` $star_sql ) ";
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
316                 $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) . ">' )) ";
317                 $o = '<h2>' . t('Group: ') . $r[0]['name'] . '</h2>' . $o;
318         }
319         elseif($cid) {
320
321                 $r = q("SELECT `id`,`name`,`network`,`writable` 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 `parent` FROM `item` WHERE `id` = `parent` $star_sql AND `contact-id` IN ( " . intval($cid) . " )) ";
327                         $o = '<h2>' . t('Contact: ') . $r[0]['name'] . '</h2>' . $o;
328                         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'))) {
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         $sql_extra2 = (($nouveau) ? '' : " AND `item`.`parent` = `item`.`id` ");
346
347         if(x($_GET,'search')) {
348                 $search = escape_tags($_GET['search']);
349                 $sql_extra .= sprintf(" AND ( `item`.`body` REGEXP '%s' OR `item`.`tag` REGEXP '%s' ) ",
350                         dbesc($search),
351                         dbesc('\\]' . $search . '\\[')
352                 );
353         }
354
355         $r = q("SELECT COUNT(*) AS `total`
356                 FROM `item` LEFT JOIN `contact` ON `contact`.`id` = `item`.`contact-id`
357                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
358                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
359                 $sql_extra2
360                 $sql_extra ",
361                 intval($_SESSION['uid'])
362         );
363
364         if(count($r)) {
365                 $a->set_pager_total($r[0]['total']);
366                 $a->set_pager_itemspage(40);
367         }
368
369
370         if($nouveau) {
371
372                 // "New Item View" - show all items unthreaded in reverse created date order
373
374                 $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
375                         `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`, `contact`.`writable`,
376                         `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
377                         `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
378                         FROM `item`, `contact`
379                         WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
380                         AND `contact`.`id` = `item`.`contact-id`
381                         AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
382                         $sql_extra
383                         ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
384                         intval($_SESSION['uid']),
385                         intval($a->pager['start']),
386                         intval($a->pager['itemspage'])
387                 );
388                 
389         }
390         else {
391
392                 // Normal conversation view
393
394
395                 if($order === 'post')
396                                 $ordering = "`created`";
397                 else
398                                 $ordering = "`commented`";
399
400                 // Fetch a page full of parent items for this page
401
402                 $r = q("SELECT `item`.`id` AS `item_id`, `contact`.`uid` AS `contact_uid`
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                         AND `item`.`parent` = `item`.`id`
407                         $sql_extra
408                         ORDER BY `item`.$ordering DESC LIMIT %d ,%d ",
409                         intval(local_user()),
410                         intval($a->pager['start']),
411                         intval($a->pager['itemspage'])
412                 );
413
414                 // Then fetch all the children of the parents that are on this page
415
416                 $parents_arr = array();
417                 $parents_str = '';
418
419                 if(count($r)) {
420                         foreach($r as $rr)
421                                 $parents_arr[] = $rr['item_id'];
422                         $parents_str = implode(', ', $parents_arr);
423
424                         $r = 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`, (SELECT `p`.`id`,`p`.`created`,`p`.`commented` FROM `item` AS `p` WHERE `p`.`parent`=`p`.`id`) as `parentitem`, `contact`
429                                 WHERE `item`.`uid` = %d AND `item`.`visible` = 1 AND `item`.`deleted` = 0
430                                 AND `contact`.`id` = `item`.`contact-id`
431                                 AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
432                                 AND `item`.`parent` = `parentitem`.`id` AND `item`.`parent` IN ( %s )
433                                 $sql_extra
434                                 ORDER BY `parentitem`.$ordering DESC, `parentitem`.`id` ASC, `item`.`gravity` ASC, `item`.`created` ASC ",
435                                 intval(local_user()),
436                                 dbesc($parents_str)
437                         );
438                 }       
439         }
440
441         // Set this so that the conversation function can find out contact info for our wall-wall items
442         $a->page_contact = $a->contact;
443
444         $mode = (($nouveau) ? 'network-new' : 'network');
445
446         $o .= conversation($a,$r,$mode,$update);
447
448         if(! $update) {
449                 $o .= paginate($a);
450         }
451
452         return $o;
453 }