]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
improved search - ensure you can see all your own content (logged in members). Visito...
[friendica.git] / mod / contacts.php
1 <?php
2
3 require_once('include/Contact.php');
4
5 function contacts_init(&$a) {
6         require_once('include/group.php');
7         if(! x($a->page,'aside'))
8                 $a->page['aside'] = '';
9         $a->page['aside'] .= group_side();
10
11         if($a->config['register_policy'] != REGISTER_CLOSED)
12                 $a->page['aside'] .= '<div class="side-link" id="side-invite-link" ><a href="invite" >' . t("Invite Friends") . '</a></div>';
13
14
15         $a->page['aside'] .= '<div class="side-link" id="side-match-link"><a href="match" >' 
16                 . t('Find People With Shared Interests') . '</a></div>';
17
18         $tpl = get_markup_template('follow.tpl');
19         $a->page['aside'] .= replace_macros($tpl,array(
20                 '$label' => t('Connect/Follow'),
21                 '$hint' => t('Example: bob@example.com, http://example.com/barbara'),
22                 '$follow' => t('Follow')
23         ));
24
25
26
27 }
28
29 function contacts_post(&$a) {
30         
31         if(! local_user())
32                 return;
33
34         $contact_id = intval($a->argv[1]);
35         if(! $contact_id)
36                 return;
37
38         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
39                 intval($contact_id),
40                 intval(local_user())
41         );
42
43         if(! count($orig_record)) {
44                 notice( t('Could not access contact record.') . EOL);
45                 goaway($a->get_baseurl() . '/contacts');
46                 return; // NOTREACHED
47         }
48
49         call_hooks('contact_edit_post', $_POST);
50
51         $profile_id = intval($_POST['profile-assign']);
52         if($profile_id) {
53                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
54                         intval($profile_id),
55                         intval(local_user())
56                 );
57                 if(! count($r)) {
58                         notice( t('Could not locate selected profile.') . EOL);
59                         return;
60                 }
61         }
62
63
64         $priority = intval($_POST['poll']);
65         if($priority == (-1))
66                 
67         if($priority > 5 || $priority < 0)
68                 $priority = 0;
69
70         $rating = intval($_POST['reputation']);
71         if($rating > 5 || $rating < 0)
72                 $rating = 0;
73
74         $reason = notags(trim($_POST['reason']));
75
76         $info = escape_tags(trim($_POST['info']));
77
78         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `rating` = %d, `reason` = '%s', `info` = '%s'
79                 WHERE `id` = %d AND `uid` = %d LIMIT 1",
80                 intval($profile_id),
81                 intval($priority),
82                 intval($rating),
83                 dbesc($reason),
84                 dbesc($info),
85                 intval($contact_id),
86                 intval(local_user())
87         );
88         if($r)
89                 info( t('Contact updated.') . EOL);
90         else
91                 notice( t('Failed to update contact record.') . EOL);
92         return;
93
94 }
95
96
97
98 function contacts_content(&$a) {
99
100         $sort_type = 0;
101         $o = '';
102         $o .= '<script> $(document).ready(function() { $(\'#nav-contacts-link\').addClass(\'nav-selected\'); });</script>';
103
104         $_SESSION['return_url'] = $a->get_baseurl() . '/' . $a->cmd;
105
106         if(! local_user()) {
107                 notice( t('Permission denied.') . EOL);
108                 return;
109         }
110
111         if($a->argc == 3) {
112
113                 $contact_id = intval($a->argv[1]);
114                 if(! $contact_id)
115                         return;
116
117                 $cmd = $a->argv[2];
118
119                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
120                         intval($contact_id),
121                         intval(local_user())
122                 );
123
124                 if(! count($orig_record)) {
125                         notice( t('Could not access contact record.') . EOL);
126                         goaway($a->get_baseurl() . '/contacts');
127                         return; // NOTREACHED
128                 }
129
130                 if($cmd === 'update') {
131
132                         // pull feed and consume it, which should subscribe to the hub.
133                         proc_run('php',"include/poller.php","$contact_id");
134                         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
135                         // NOTREACHED
136                 }
137
138                 if($cmd === 'block') {
139                         $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
140                         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
141                                         intval($blocked),
142                                         intval($contact_id),
143                                         intval(local_user())
144                         );
145                         if($r) {
146                                 //notice( t('Contact has been ') . (($blocked) ? t('blocked') : t('unblocked')) . EOL );
147                                 info( (($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')) . EOL );
148                         }
149                         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
150                         return; // NOTREACHED
151                 }
152
153                 if($cmd === 'ignore') {
154                         $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
155                         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
156                                         intval($readonly),
157                                         intval($contact_id),
158                                         intval(local_user())
159                         );
160                         if($r) {
161                                 info( (($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')) . EOL );
162                         }
163                         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
164                         return; // NOTREACHED
165                 }
166
167                 if($cmd === 'drop') {
168
169                         // create an unfollow slap
170
171                         if($orig_record[0]['network'] === 'stat') {
172                                 $tpl = get_markup_template('follow_slap.tpl');
173                                 $slap = replace_macros($tpl, array(
174                                         '$name' => $a->user['username'],
175                                         '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
176                                         '$photo' => $a->contact['photo'],
177                                         '$thumb' => $a->contact['thumb'],
178                                         '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
179                                         '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':unfollow:' . random_string(),
180                                         '$title' => '',
181                                         '$type' => 'text',
182                                         '$content' => t('stopped following'),
183                                         '$nick' => $a->user['nickname'],
184                                         '$verb' => 'http://ostatus.org/schema/1.0/unfollow', // ACTIVITY_UNFOLLOW,
185                                         '$ostat_follow' => '' // '<as:verb>http://ostatus.org/schema/1.0/unfollow</as:verb>' . "\r\n"
186                                 ));
187
188                                 if((x($orig_record[0],'notify')) && (strlen($orig_record[0]['notify']))) {
189                                         require_once('include/salmon.php');
190                                         slapper($a->user,$orig_record[0]['notify'],$slap);
191                                 }
192                         }
193
194                         if($orig_record[0]['network'] === 'dfrn') {
195                                 require_once('include/items.php');
196                                 dfrn_deliver($a->user,$orig_record[0],'placeholder', 1);
197                         }
198
199
200                         contact_remove($contact_id);
201                         info( t('Contact has been removed.') . EOL );
202                         goaway($a->get_baseurl() . '/contacts');
203                         return; // NOTREACHED
204                 }
205         }
206
207         if(($a->argc == 2) && intval($a->argv[1])) {
208
209                 $contact_id = intval($a->argv[1]);
210                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
211                         intval(local_user()),
212                         intval($contact_id)
213                 );
214                 if(! count($r)) {
215                         notice( t('Contact not found.') . EOL);
216                         return;
217                 }
218
219                 $tpl = get_markup_template('contact_head.tpl');
220                 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
221
222                 require_once('include/contact_selectors.php');
223
224                 $tpl = get_markup_template("contact_edit.tpl");
225
226                 switch($r[0]['rel']) {
227                         case REL_BUD:
228                                 $dir_icon = 'images/lrarrow.gif';
229                                 $alt_text = t('Mutual Friendship');
230                                 break;
231                         case REL_VIP;
232                                 $dir_icon = 'images/larrow.gif';
233                                 $alt_text = t('is a fan of yours');
234                                 break;
235         
236                         case REL_FAN;
237                                 $dir_icon = 'images/rarrow.gif';
238                                 $alt_text = t('you are a fan of');
239                                 break;
240                         default:
241                                 break;
242                 }
243
244                 if(($r[0]['network'] === 'dfrn') && ($r[0]['rel'])) {
245                         $url = "redir/{$r[0]['id']}";
246                         $sparkle = ' class="sparkle" ';
247                 }
248                 else { 
249                         $url = $r[0]['url'];
250                         $sparkle = '';
251                 }
252
253                 $grps = '';
254                 $member_of = member_of($r[0]['id']);
255                 if(is_array($member_of) && count($member_of)) {
256                         $grps = t('Member of: ') . EOL . '<ul>';
257                         foreach($member_of as $member)
258                                 $grps .= '<li><a href="group/' . $member['id'] . '" title="' . t('Edit') . '" ><img src="images/spencil.gif" alt="' . t('Edit') . '" /></a> <a href="network/' . $member['id'] . '">' . $member['name'] . '</a></li>';
259                         $grps .= '</ul>';
260                 }
261
262                 $insecure = '<div id="profile-edit-insecure"><p><img src="images/unlock_icon.gif" alt="' . t('Privacy Unavailable') . '" />&nbsp;'
263                         . t('Private communications are not available for this contact.') . '</p></div>';
264
265                 $last_update = (($r[0]['last-update'] == '0000-00-00 00:00:00') 
266                                 ? t('Never') 
267                                 : datetime_convert('UTC',date_default_timezone_get(),$r[0]['last-update'],'D, j M Y, g:i A'));
268
269                 if($r[0]['last-update'] !== '0000-00-00 00:00:00')
270                         $last_update .= ' ' . (($r[0]['last-update'] == $r[0]['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
271
272                 $lblsuggest = (($r[0]['network'] === NETWORK_DFRN) 
273                         ? '<div id="contact-suggest-wrapper"><a href="fsuggest/' . $r[0]['id'] . '" id="contact-suggest">' . t('Suggest friends') . '</a></div>' : '');
274
275
276                 $o .= replace_macros($tpl,array(
277                         '$header' => t('Contact Editor'),
278                         '$submit' => t('Submit'),
279                         '$lbl_vis1' => t('Profile Visibility'),
280                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $r[0]['name']),
281                         '$lbl_info1' => t('Contact Information / Notes'),
282                         '$lbl_rep1' => t('Online Reputation'),
283                         '$lbl_rep2' => t('Occasionally your friends may wish to inquire about this person\'s online legitimacy.'),
284                         '$lbl_rep3' => t('You may help them choose whether or not to interact with this person by providing a <em>reputation</em> to guide them.'),
285                         '$lbl_rep4' => t('Please take a moment to elaborate on this selection if you feel it could be helpful to others.'),
286                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$r[0]['name'],$r[0]['url']),
287                         '$blockunblock' => t('Block/Unblock contact'),
288                         '$ignorecont' => t('Ignore contact'),
289                         '$altcrepair' => t('Repair contact URL settings'),
290                         '$lblcrepair' => t("Repair contact URL settings \x28WARNING: Advanced\x29"),
291                         '$lblrecent' => t('View conversations'),
292                         '$lblsuggest' => $lblsuggest,
293                         '$grps' => $grps,
294                         '$delete' => t('Delete contact'),
295                         '$poll_interval' => contact_poll_interval($r[0]['priority']),
296                         '$lastupdtext' => t('Last updated: '),
297                         '$updpub' => t('Update public posts: '),
298                         '$last_update' => $last_update,
299                         '$udnow' => t('Update now'),
300                         '$profile_select' => contact_profile_assign($r[0]['profile-id'],(($r[0]['network'] !== 'dfrn') ? true : false)),
301                         '$contact_id' => $r[0]['id'],
302                         '$block_text' => (($r[0]['blocked']) ? t('Unblock this contact') : t('Block this contact') ),
303                         '$ignore_text' => (($r[0]['readonly']) ? t('Unignore this contact') : t('Ignore this contact') ),
304                         '$insecure' => (($r[0]['network'] !== NETWORK_DFRN && $r[0]['network'] !== NETWORK_MAIL && $r[0]['network'] !== NETWORK_FACEBOOK) ? $insecure : ''),
305                         '$info' => $r[0]['info'],
306                         '$blocked' => (($r[0]['blocked']) ? '<div id="block-message">' . t('Currently blocked') . '</div>' : ''),
307                         '$ignored' => (($r[0]['readonly']) ? '<div id="ignore-message">' . t('Currently ignored') . '</div>' : ''),
308                         '$rating' => contact_reputation($r[0]['rating']),
309                         '$reason' => $r[0]['reason'],
310                         '$groups' => '', // group_selector(),
311                         '$photo' => $r[0]['photo'],
312                         '$name' => $r[0]['name'],
313                         '$dir_icon' => $dir_icon,
314                         '$alt_text' => $alt_text,
315                         '$sparkle' => $sparkle,
316                         '$url' => $url
317
318                 ));
319
320                 $arr = array('contact' => $r[0],'output' => $o);
321
322                 call_hooks('contact_edit', $arr);
323
324                 return $arr['output'];
325
326         }
327
328
329         if(($a->argc == 2) && ($a->argv[1] === 'all'))
330                 $sql_extra = '';
331         else
332                 $sql_extra = " AND `blocked` = 0 ";
333
334         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
335
336         $tpl = get_markup_template("contacts-top.tpl");
337         $o .= replace_macros($tpl,array(
338                 '$header' => t('Contacts'),
339                 '$hide_url' => ((strlen($sql_extra)) ? 'contacts/all' : 'contacts' ),
340                 '$hide_text' => ((strlen($sql_extra)) ? t('Show Blocked Connections') : t('Hide Blocked Connections')),
341                 '$search' => $search,
342                 '$finding' => (strlen($search) ? '<h4>' . t('Finding: ') . "'" . $search . "'" . '</h4>' : ""),
343                 '$submit' => t('Find'),
344                 '$cmd' => $a->cmd
345
346
347         )); 
348
349         if($search)
350                 $search = dbesc($search.'*');
351         $sql_extra .= ((strlen($search)) ? " AND MATCH `name` AGAINST ('$search' IN BOOLEAN MODE) " : "");
352
353         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= REL_BUD)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : ''); 
354
355         
356         $r = q("SELECT COUNT(*) AS `total` FROM `contact` 
357                 WHERE `uid` = %d AND `pending` = 0 $sql_extra $sql_extra2 ",
358                 intval($_SESSION['uid']));
359         if(count($r))
360                 $a->set_pager_total($r[0]['total']);
361
362         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `pending` = 0 $sql_extra $sql_extra2 ORDER BY `name` ASC LIMIT %d , %d ",
363                 intval($_SESSION['uid']),
364                 intval($a->pager['start']),
365                 intval($a->pager['itemspage'])
366         );
367
368         if(count($r)) {
369
370                 $tpl = get_markup_template("contact_template.tpl");
371
372                 foreach($r as $rr) {
373                         if($rr['self'])
374                                 continue;
375
376                         switch($rr['rel']) {
377                                 case REL_BUD:
378                                         $dir_icon = 'images/lrarrow.gif';
379                                         $alt_text = t('Mutual Friendship');
380                                         break;
381                                 case  REL_VIP;
382                                         $dir_icon = 'images/larrow.gif';
383                                         $alt_text = t('is a fan of yours');
384                                         break;
385                                 case REL_FAN;
386                                         $dir_icon = 'images/rarrow.gif';
387                                         $alt_text = t('you are a fan of');
388                                         break;
389                                 default:
390                                         break;
391                         }
392                         if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
393                                 $url = "redir/{$rr['id']}";
394                                 $sparkle = ' class="sparkle" ';
395                         }
396                         else { 
397                                 $url = $rr['url'];
398                                 $sparkle = '';
399                         }
400
401
402                         $o .= replace_macros($tpl, array(
403                                 '$img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
404                                 '$edit_hover' => t('Edit contact'),
405                                 '$id' => $rr['id'],
406                                 '$alt_text' => $alt_text,
407                                 '$dir_icon' => $dir_icon,
408                                 '$thumb' => $rr['thumb'], 
409                                 '$name' => substr($rr['name'],0,20),
410                                 '$username' => $rr['name'],
411                                 '$sparkle' => $sparkle,
412                                 '$url' => $url
413                         ));
414                 }
415
416                 $o .= '<div id="contact-edit-end"></div>';
417
418         }
419         $o .= paginate($a);
420         return $o;
421 }