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