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