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