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