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