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