]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
Merge remote branch 'friendica/master' into bid-prov
[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         $tpl = get_markup_template("contacts-head.tpl");
49         $a->page['htmlhead'] .= replace_macros($tpl,array(
50                 '$baseurl' => $a->get_baseurl(true),
51                 '$base' => $base
52         ));
53         $tpl = get_markup_template("contacts-end.tpl");
54         $a->page['end'] .= replace_macros($tpl,array(
55                 '$baseurl' => $a->get_baseurl(true),
56                 '$base' => $base
57         ));
58
59
60 }
61
62 function contacts_post(&$a) {
63         
64         if(! local_user())
65                 return;
66
67         $contact_id = intval($a->argv[1]);
68         if(! $contact_id)
69                 return;
70
71         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
72                 intval($contact_id),
73                 intval(local_user())
74         );
75
76         if(! count($orig_record)) {
77                 notice( t('Could not access contact record.') . EOL);
78                 goaway($a->get_baseurl(true) . '/contacts');
79                 return; // NOTREACHED
80         }
81
82         call_hooks('contact_edit_post', $_POST);
83
84         $profile_id = intval($_POST['profile-assign']);
85         if($profile_id) {
86                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
87                         intval($profile_id),
88                         intval(local_user())
89                 );
90                 if(! count($r)) {
91                         notice( t('Could not locate selected profile.') . EOL);
92                         return;
93                 }
94         }
95
96         $hidden = intval($_POST['hidden']);
97
98         $priority = intval($_POST['poll']);
99         if($priority > 5 || $priority < 0)
100                 $priority = 0;
101
102         $info = fix_mce_lf(escape_tags(trim($_POST['info'])));
103
104         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
105                 `hidden` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
106                 intval($profile_id),
107                 intval($priority),
108                 dbesc($info),
109                 intval($hidden),
110                 intval($contact_id),
111                 intval(local_user())
112         );
113         if($r)
114                 info( t('Contact updated.') . EOL);
115         else
116                 notice( t('Failed to update contact record.') . EOL);
117
118         $r = q("select * from contact where id = %d and uid = %d limit 1",
119                 intval($contact_id),
120                 intval(local_user())
121         );
122         if($r && count($r))
123                 $a->data['contact'] = $r[0];
124
125         return;
126
127 }
128
129
130
131 function contacts_content(&$a) {
132
133         $sort_type = 0;
134         $o = '';
135         nav_set_selected('contacts');
136
137
138         if(! local_user()) {
139                 notice( t('Permission denied.') . EOL);
140                 return;
141         }
142
143         if($a->argc == 3) {
144
145                 $contact_id = intval($a->argv[1]);
146                 if(! $contact_id)
147                         return;
148
149                 $cmd = $a->argv[2];
150
151                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
152                         intval($contact_id),
153                         intval(local_user())
154                 );
155
156                 if(! count($orig_record)) {
157                         notice( t('Could not access contact record.') . EOL);
158                         goaway($a->get_baseurl(true) . '/contacts');
159                         return; // NOTREACHED
160                 }
161                 
162                 if($cmd === 'update') {
163
164                         // pull feed and consume it, which should subscribe to the hub.
165                         proc_run('php',"include/poller.php","$contact_id");
166                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
167                         // NOTREACHED
168                 }
169
170                 if($cmd === 'block') {
171                         $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
172                         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
173                                 intval($blocked),
174                                 intval($contact_id),
175                                 intval(local_user())
176                         );
177                         if($r) {
178                                 //notice( t('Contact has been ') . (($blocked) ? t('blocked') : t('unblocked')) . EOL );
179                                 info( (($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')) . EOL );
180                         }
181                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
182                         return; // NOTREACHED
183                 }
184
185                 if($cmd === 'ignore') {
186                         $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
187                         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
188                                 intval($readonly),
189                                 intval($contact_id),
190                                 intval(local_user())
191                         );
192                         if($r) {
193                                 info( (($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')) . EOL );
194                         }
195                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
196                         return; // NOTREACHED
197                 }
198
199
200                 if($cmd === 'archive') {
201                         $archived = (($orig_record[0]['archive']) ? 0 : 1);
202                         $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
203                                 intval($archived),
204                                 intval($contact_id),
205                                 intval(local_user())
206                         );
207                         if($r) {
208                                 //notice( t('Contact has been ') . (($archived) ? t('archived') : t('unarchived')) . EOL );
209                                 info( (($archived) ? t('Contact has been archived') : t('Contact has been unarchived')) . EOL );
210                         }
211                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
212                         return; // NOTREACHED
213                 }
214
215                 if($cmd === 'drop') {
216
217                         require_once('include/Contact.php');
218
219                         terminate_friendship($a->user,$a->contact,$orig_record[0]);
220
221                         contact_remove($orig_record[0]['id']);
222                         info( t('Contact has been removed.') . EOL );
223                         if(x($_SESSION,'return_url'))
224                                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
225                         else
226                                 goaway($a->get_baseurl(true) . '/contacts');
227                         return; // NOTREACHED
228                 }
229         }
230
231         if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
232
233                 $contact_id = $a->data['contact']['id'];
234                 $contact = $a->data['contact'];
235
236                 $editselect = 'exact';
237                 if(intval(get_pconfig(local_user(),'system','plaintext')))
238                         $editselect = 'none';
239
240                 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
241                         '$baseurl' => $a->get_baseurl(true),
242                         '$editselect' => $editselect,
243                 ));
244                 $a->page['end'] .= replace_macros(get_markup_template('contact_end.tpl'), array(
245                         '$baseurl' => $a->get_baseurl(true),
246                         '$editselect' => $editselect,
247                 ));
248
249                 require_once('include/contact_selectors.php');
250
251                 $tpl = get_markup_template("contact_edit.tpl");
252
253                 switch($contact['rel']) {
254                         case CONTACT_IS_FRIEND:
255                                 $dir_icon = 'images/lrarrow.gif';
256                                 $relation_text = t('You are mutual friends with %s');
257                                 break;
258                         case CONTACT_IS_FOLLOWER;
259                                 $dir_icon = 'images/larrow.gif';
260                                 $relation_text = t('You are sharing with %s');
261                                 break;
262         
263                         case CONTACT_IS_SHARING;
264                                 $dir_icon = 'images/rarrow.gif';
265                                 $relation_text = t('%s is sharing with you');
266                                 break;
267                         default:
268                                 break;
269                 }
270
271                 $relation_text = sprintf($relation_text,$contact['name']);
272
273                 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
274                         $url = "redir/{$contact['id']}";
275                         $sparkle = ' class="sparkle" ';
276                 }
277                 else { 
278                         $url = $contact['url'];
279                         $sparkle = '';
280                 }
281
282                 $insecure = t('Private communications are not available for this contact.');
283
284                 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00') 
285                                 ? t('Never') 
286                                 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
287
288                 if($contact['last-update'] !== '0000-00-00 00:00:00')
289                         $last_update .= ' ' . (($contact['last-update'] == $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
290
291                 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
292
293                 $poll_enabled = (($contact['network'] !== NETWORK_DIASPORA) ? true : false);
294
295                 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network']));
296
297                 $common = count_common_friends(local_user(),$contact['id']);
298                 $common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
299
300                 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : ''); 
301
302                 $x = count_all_friends(local_user(), $contact['id']);
303                 $all_friends = (($x) ? t('View all contacts') : '');
304
305                 // tabs
306                 $tabs = array(
307                         array(
308                                 'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
309                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
310                                 'sel'   => '',
311                                 'title' => t('Toggle Blocked status'),
312                         ),
313                         array(
314                                 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
315                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
316                                 'sel'   => '',
317                                 'title' => t('Toggle Ignored status'),
318                         ),
319
320                         array(
321                                 'label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
322                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
323                                 'sel'   => '',
324                                 'title' => t('Toggle Archive status'),
325                         ),
326                         array(
327                                 'label' => t('Repair'),
328                                 'url'   => $a->get_baseurl(true) . '/crepair/' . $contact_id,
329                                 'sel'   => '',
330                                 'title' => t('Advanced Contact Settings'),
331                         )
332                 );
333                 $tab_tpl = get_markup_template('common_tabs.tpl');
334                 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
335
336                 $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!') : '');
337
338                 $o .= replace_macros($tpl,array(
339                         '$header' => t('Contact Editor'),
340                         '$tab_str' => $tab_str,
341                         '$submit' => t('Submit'),
342                         '$lbl_vis1' => t('Profile Visibility'),
343                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
344                         '$lbl_info1' => t('Contact Information / Notes'),
345                         '$infedit' => t('Edit contact notes'),
346                         '$common_text' => $common_text,
347                         '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
348                         '$all_friends' => $all_friends,
349                         '$relation_text' => $relation_text,
350                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
351                         '$blockunblock' => t('Block/Unblock contact'),
352                         '$ignorecont' => t('Ignore contact'),
353                         '$lblcrepair' => t("Repair URL settings"),
354                         '$lblrecent' => t('View conversations'),
355                         '$lblsuggest' => $lblsuggest,
356                         '$delete' => t('Delete contact'),
357                         '$nettype' => $nettype,
358                         '$poll_interval' => contact_poll_interval($contact['priority'],(! $poll_enabled)),
359                         '$poll_enabled' => $poll_enabled,
360                         '$lastupdtext' => t('Last update:'),
361                         '$lost_contact' => $lost_contact,
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         $searching = false;
481         if($search) {
482                 $search_hdr = $search;
483                 $search_txt = dbesc(protect_sprintf(preg_quote($search)));
484                 $searching = true;
485         }
486         $sql_extra .= (($searching) ? " AND `name` REGEXP '$search_txt' " : "");
487
488         if($nets)
489                 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
490  
491         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : ''); 
492
493         
494         $r = q("SELECT COUNT(*) AS `total` FROM `contact` 
495                 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
496                 intval($_SESSION['uid']));
497         if(count($r)) {
498                 $a->set_pager_total($r[0]['total']);
499                 $total = $r[0]['total'];
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' => (($searching) ? 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 }