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