]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
"show more" friends in common
[friendica.git] / mod / contacts.php
1 <?php
2
3 require_once('include/Contact.php');
4 require_once('include/socgraph.php');
5
6 function contacts_init(&$a) {
7         if(! local_user())
8                 return;
9
10         $contact_id = 0;
11
12         if(($a->argc == 2) && intval($a->argv[1])) {
13                 $contact_id = intval($a->argv[1]);
14                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
15                         intval(local_user()),
16                         intval($contact_id)
17                 );
18                 if(! count($r)) {
19                         $contact_id = 0;
20                 }
21         }
22
23         require_once('include/group.php');
24         require_once('include/contact_widgets.php');
25
26         if(! x($a->page,'aside'))
27                 $a->page['aside'] = '';
28
29         if($contact_id) {
30                         $a->data['contact'] = $r[0];
31                         $o .= '<div class="vcard">';
32                         $o .= '<div class="fn">' . $a->data['contact']['name'] . '</div>';
33                         $o .= '<div id="profile-photo-wrapper"><img class="photo" style="width: 175px; height: 175px;" src="' . $a->data['contact']['photo'] . '" alt="' . $a->data['contact']['name'] . '" /></div>';
34                         $o .= '</div>';
35                         $a->page['aside'] .= $o;
36
37         }       
38         else
39                 $a->page['aside'] .= follow_widget();
40
41         $a->page['aside'] .= group_side('contacts','group',false,0,$contact_id);
42
43         $a->page['aside'] .= findpeople_widget();
44
45         $a->page['aside'] .= networks_widget('contacts',$_GET['nets']);
46 }
47
48 function contacts_post(&$a) {
49         
50         if(! local_user())
51                 return;
52
53         $contact_id = intval($a->argv[1]);
54         if(! $contact_id)
55                 return;
56
57         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
58                 intval($contact_id),
59                 intval(local_user())
60         );
61
62         if(! count($orig_record)) {
63                 notice( t('Could not access contact record.') . EOL);
64                 goaway($a->get_baseurl(true) . '/contacts');
65                 return; // NOTREACHED
66         }
67
68         call_hooks('contact_edit_post', $_POST);
69
70         $profile_id = intval($_POST['profile-assign']);
71         if($profile_id) {
72                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
73                         intval($profile_id),
74                         intval(local_user())
75                 );
76                 if(! count($r)) {
77                         notice( t('Could not locate selected profile.') . EOL);
78                         return;
79                 }
80         }
81
82         $hidden = intval($_POST['hidden']);
83
84         $priority = intval($_POST['poll']);
85         if($priority > 5 || $priority < 0)
86                 $priority = 0;
87
88         $info = fix_mce_lf(escape_tags(trim($_POST['info'])));
89
90         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
91                 `hidden` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
92                 intval($profile_id),
93                 intval($priority),
94                 dbesc($info),
95                 intval($hidden),
96                 intval($contact_id),
97                 intval(local_user())
98         );
99         if($r)
100                 info( t('Contact updated.') . EOL);
101         else
102                 notice( t('Failed to update contact record.') . EOL);
103
104         $r = q("select * from contact where id = %d and uid = %d limit 1",
105                 intval($contact_id),
106                 intval(local_user())
107         );
108         if($r && count($r))
109                 $a->data['contact'] = $r[0];
110
111         return;
112
113 }
114
115
116
117 function contacts_content(&$a) {
118
119         $sort_type = 0;
120         $o = '';
121         nav_set_selected('contacts');
122
123
124         if(! local_user()) {
125                 notice( t('Permission denied.') . EOL);
126                 return;
127         }
128
129         if($a->argc == 3) {
130
131                 $contact_id = intval($a->argv[1]);
132                 if(! $contact_id)
133                         return;
134
135                 $cmd = $a->argv[2];
136
137                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
138                         intval($contact_id),
139                         intval(local_user())
140                 );
141
142                 if(! count($orig_record)) {
143                         notice( t('Could not access contact record.') . EOL);
144                         goaway($a->get_baseurl(true) . '/contacts');
145                         return; // NOTREACHED
146                 }
147                 
148                 if($cmd === 'update') {
149
150                         // pull feed and consume it, which should subscribe to the hub.
151                         proc_run('php',"include/poller.php","$contact_id");
152                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
153                         // NOTREACHED
154                 }
155
156                 if($cmd === 'block') {
157                         $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
158                         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
159                                 intval($blocked),
160                                 intval($contact_id),
161                                 intval(local_user())
162                         );
163                         if($r) {
164                                 //notice( t('Contact has been ') . (($blocked) ? t('blocked') : t('unblocked')) . EOL );
165                                 info( (($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')) . EOL );
166                         }
167                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
168                         return; // NOTREACHED
169                 }
170
171                 if($cmd === 'ignore') {
172                         $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
173                         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
174                                 intval($readonly),
175                                 intval($contact_id),
176                                 intval(local_user())
177                         );
178                         if($r) {
179                                 info( (($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')) . EOL );
180                         }
181                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
182                         return; // NOTREACHED
183                 }
184
185
186                 if($cmd === 'archive') {
187                         $archived = (($orig_record[0]['archive']) ? 0 : 1);
188                         $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
189                                 intval($archived),
190                                 intval($contact_id),
191                                 intval(local_user())
192                         );
193                         if($r) {
194                                 //notice( t('Contact has been ') . (($archived) ? t('archived') : t('unarchived')) . EOL );
195                                 info( (($archived) ? t('Contact has been archived') : t('Contact has been unarchived')) . EOL );
196                         }
197                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
198                         return; // NOTREACHED
199                 }
200
201                 if($cmd === 'drop') {
202
203                         require_once('include/Contact.php');
204
205                         terminate_friendship($a->user,$a->contact,$orig_record[0]);
206
207                         contact_remove($orig_record[0]['id']);
208                         info( t('Contact has been removed.') . EOL );
209                         if(x($_SESSION,'return_url'))
210                                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
211                         else
212                                 goaway($a->get_baseurl(true) . '/contacts');
213                         return; // NOTREACHED
214                 }
215         }
216
217         if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
218
219                 $contact_id = $a->data['contact']['id'];
220                 $contact = $a->data['contact'];
221
222                 $editselect = 'exact';
223                 if(intval(get_pconfig(local_user(),'system','plaintext')))
224                         $editselect = 'none';
225
226                 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
227                         '$baseurl' => $a->get_baseurl(true),
228                         '$editselect' => $editselect,
229                 ));
230
231                 require_once('include/contact_selectors.php');
232
233                 $tpl = get_markup_template("contact_edit.tpl");
234
235                 switch($contact['rel']) {
236                         case CONTACT_IS_FRIEND:
237                                 $dir_icon = 'images/lrarrow.gif';
238                                 $relation_text = t('You are mutual friends with %s');
239                                 break;
240                         case CONTACT_IS_FOLLOWER;
241                                 $dir_icon = 'images/larrow.gif';
242                                 $relation_text = t('You are sharing with %s');
243                                 break;
244         
245                         case CONTACT_IS_SHARING;
246                                 $dir_icon = 'images/rarrow.gif';
247                                 $relation_text = t('%s is sharing with you');
248                                 break;
249                         default:
250                                 break;
251                 }
252
253                 $relation_text = sprintf($relation_text,$contact['name']);
254
255                 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
256                         $url = "redir/{$contact['id']}";
257                         $sparkle = ' class="sparkle" ';
258                 }
259                 else { 
260                         $url = $contact['url'];
261                         $sparkle = '';
262                 }
263
264                 $insecure = t('Private communications are not available for this contact.');
265
266                 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00') 
267                                 ? t('Never') 
268                                 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
269
270                 if($contact['last-update'] !== '0000-00-00 00:00:00')
271                         $last_update .= ' ' . (($contact['last-update'] == $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
272
273                 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
274
275                 $poll_enabled = (($contact['network'] !== NETWORK_DIASPORA) ? true : false);
276
277                 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network']));
278
279                 $common = count_common_friends(local_user(),$contact['id']);
280                 $common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
281
282                 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : ''); 
283
284                 $x = count_all_friends(local_user(), $contact['id']);
285                 $all_friends = (($x) ? t('View all contacts') : '');
286
287                 // tabs
288                 $tabs = array(
289                         array(
290                                 'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
291                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
292                                 'sel'   => '',
293                                 'title' => t('Toggle Blocked status'),
294                         ),
295                         array(
296                                 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
297                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
298                                 'sel'   => '',
299                                 'title' => t('Toggle Ignored status'),
300                         ),
301
302                         array(
303                                 'label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
304                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
305                                 'sel'   => '',
306                                 'title' => t('Toggle Archive status'),
307                         ),
308                         array(
309                                 'label' => t('Repair'),
310                                 'url'   => $a->get_baseurl(true) . '/crepair/' . $contact_id,
311                                 'sel'   => '',
312                                 'title' => t('Advanced Contact Settings'),
313                         )
314                 );
315                 $tab_tpl = get_markup_template('common_tabs.tpl');
316                 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
317
318
319                 $o .= replace_macros($tpl,array(
320                         '$header' => t('Contact Editor'),
321                         '$tab_str' => $tab_str,
322                         '$submit' => t('Submit'),
323                         '$lbl_vis1' => t('Profile Visibility'),
324                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
325                         '$lbl_info1' => t('Contact Information / Notes'),
326                         '$infedit' => t('Edit contact notes'),
327                         '$common_text' => $common_text,
328                         '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
329                         '$all_friends' => $all_friends,
330                         '$relation_text' => $relation_text,
331                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
332                         '$blockunblock' => t('Block/Unblock contact'),
333                         '$ignorecont' => t('Ignore contact'),
334                         '$lblcrepair' => t("Repair URL settings"),
335                         '$lblrecent' => t('View conversations'),
336                         '$lblsuggest' => $lblsuggest,
337                         '$delete' => t('Delete contact'),
338                         '$nettype' => $nettype,
339                         '$poll_interval' => contact_poll_interval($contact['priority'],(! $poll_enabled)),
340                         '$poll_enabled' => $poll_enabled,
341                         '$lastupdtext' => t('Last update:'),
342                         '$updpub' => t('Update public posts'),
343                         '$last_update' => $last_update,
344                         '$udnow' => t('Update now'),
345                         '$profile_select' => contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false)),
346                         '$contact_id' => $contact['id'],
347                         '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
348                         '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
349                         '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
350                         '$info' => $contact['info'],
351                         '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
352                         '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
353                         '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
354                         '$hidden' => array('hidden', t('Hide this contact from others'), ($contact['hidden'] == 1), t('Replies/likes to your public posts <strong>may</strong> still be visible')),
355                         '$photo' => $contact['photo'],
356                         '$name' => $contact['name'],
357                         '$dir_icon' => $dir_icon,
358                         '$alt_text' => $alt_text,
359                         '$sparkle' => $sparkle,
360                         '$url' => $url
361
362                 ));
363
364                 $arr = array('contact' => $contact,'output' => $o);
365
366                 call_hooks('contact_edit', $arr);
367
368                 return $arr['output'];
369
370         }
371
372         $blocked = false;
373         $hidden = false;
374         $ignored = false;
375         $all = false;
376
377         $_SESSION['return_url'] = $a->query_string;
378
379         if(($a->argc == 2) && ($a->argv[1] === 'all')) {
380                 $sql_extra = '';
381                 $all = true;
382         }
383         elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
384                 $sql_extra = " AND `blocked` = 1 ";
385                 $blocked = true;
386         }
387         elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
388                 $sql_extra = " AND `hidden` = 1 ";
389                 $hidden = true;
390         }
391         elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
392                 $sql_extra = " AND `readonly` = 1 ";
393                 $ignored = true;
394         }
395         elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
396                 $sql_extra = " AND `archive` = 1 ";
397                 $archived = true;
398         }
399         else
400                 $sql_extra = " AND `blocked` = 0 ";
401
402         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
403         $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
404
405         $tabs = array(
406                 array(
407                         'label' => t('Suggestions'),
408                         'url'   => $a->get_baseurl(true) . '/suggest', 
409                         'sel'   => '',
410                         'title' => t('Suggest potential friends'),
411                 ),
412                 array(
413                         'label' => t('All Contacts'),
414                         'url'   => $a->get_baseurl(true) . '/contacts/all', 
415                         'sel'   => ($all) ? 'active' : '',
416                         'title' => t('Show all contacts'),
417                 ),
418                 array(
419                         'label' => t('Unblocked'),
420                         'url'   => $a->get_baseurl(true) . '/contacts',
421                         'sel'   => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
422                         'title' => t('Only show unblocked contacts'),
423                 ),
424
425                 array(
426                         'label' => t('Blocked'),
427                         'url'   => $a->get_baseurl(true) . '/contacts/blocked',
428                         'sel'   => ($blocked) ? 'active' : '',
429                         'title' => t('Only show blocked contacts'),
430                 ),
431
432                 array(
433                         'label' => t('Ignored'),
434                         'url'   => $a->get_baseurl(true) . '/contacts/ignored',
435                         'sel'   => ($ignored) ? 'active' : '',
436                         'title' => t('Only show ignored contacts'),
437                 ),
438
439                 array(
440                         'label' => t('Archived'),
441                         'url'   => $a->get_baseurl(true) . '/contacts/archived',
442                         'sel'   => ($archived) ? 'active' : '',
443                         'title' => t('Only show archived contacts'),
444                 ),
445
446                 array(
447                         'label' => t('Hidden'),
448                         'url'   => $a->get_baseurl(true) . '/contacts/hidden',
449                         'sel'   => ($hidden) ? 'active' : '',
450                         'title' => t('Only show hidden contacts'),
451                 ),
452
453         );
454
455         $tab_tpl = get_markup_template('common_tabs.tpl');
456         $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
457
458
459
460
461         if($search) {
462                 $search_hdr = $search;
463                 $search = dbesc($search.'*');
464         }
465         $sql_extra .= ((strlen($search)) ? " AND MATCH `name` AGAINST ('$search' IN BOOLEAN MODE) " : "");
466
467         if($nets)
468                 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
469  
470         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : ''); 
471
472         
473         $r = q("SELECT COUNT(*) AS `total` FROM `contact` 
474                 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
475                 intval($_SESSION['uid']));
476         if(count($r)) {
477                 $a->set_pager_total($r[0]['total']);
478                 $total = $r[0]['total'];
479         }
480
481
482
483         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ORDER BY `name` ASC LIMIT %d , %d ",
484                 intval($_SESSION['uid']),
485                 intval($a->pager['start']),
486                 intval($a->pager['itemspage'])
487         );
488
489         $contacts = array();
490
491         if(count($r)) {
492
493                 foreach($r as $rr) {
494
495                         switch($rr['rel']) {
496                                 case CONTACT_IS_FRIEND:
497                                         $dir_icon = 'images/lrarrow.gif';
498                                         $alt_text = t('Mutual Friendship');
499                                         break;
500                                 case  CONTACT_IS_FOLLOWER;
501                                         $dir_icon = 'images/larrow.gif';
502                                         $alt_text = t('is a fan of yours');
503                                         break;
504                                 case CONTACT_IS_SHARING;
505                                         $dir_icon = 'images/rarrow.gif';
506                                         $alt_text = t('you are a fan of');
507                                         break;
508                                 default:
509                                         break;
510                         }
511                         if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
512                                 $url = "redir/{$rr['id']}";
513                                 $sparkle = ' class="sparkle" ';
514                         }
515                         else { 
516                                 $url = $rr['url'];
517                                 $sparkle = '';
518                         }
519
520
521                         $contacts[] = array(
522                                 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
523                                 'edit_hover' => t('Edit contact'),
524                                 'photo_menu' => contact_photo_menu($rr),
525                                 'id' => $rr['id'],
526                                 'alt_text' => $alt_text,
527                                 'dir_icon' => $dir_icon,
528                                 'thumb' => $rr['thumb'], 
529                                 'name' => $rr['name'],
530                                 'username' => $rr['name'],
531                                 'sparkle' => $sparkle,
532                                 'itemurl' => $rr['url'],
533                                 'url' => $url,
534                                 'network' => network_to_name($rr['network']),
535                         );
536                 }
537
538                 
539
540         }
541         
542         $tpl = get_markup_template("contacts-template.tpl");
543         $o .= replace_macros($tpl,array(
544                 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
545                 '$tabs' => $t,
546                 '$total' => $total,
547                 '$search' => $search_hdr,
548                 '$desc' => t('Search your contacts'),
549                 '$finding' => (strlen($search) ? t('Finding: ') . "'" . $search . "'" : ""),
550                 '$submit' => t('Find'),
551                 '$cmd' => $a->cmd,
552                 '$contacts' => $contacts,
553                 '$paginate' => paginate($a),
554
555         )); 
556         
557         return $o;
558 }