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