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