]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
754350ae56187f9fe0fbd0f7688a4567bed4e6d4
[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                         ),
294                         array(
295                                 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
296                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
297                                 'sel'   => '',
298                         ),
299
300                         array(
301                                 'label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
302                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
303                                 'sel'   => '',
304                         ),
305                         array(
306                                 'label' => t('Repair'),
307                                 'url'   => $a->get_baseurl(true) . '/crepair/' . $contact_id,
308                                 'sel'   => '',
309                         )
310                 );
311                 $tab_tpl = get_markup_template('common_tabs.tpl');
312                 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
313
314
315                 $o .= replace_macros($tpl,array(
316                         '$header' => t('Contact Editor'),
317                         '$tab_str' => $tab_str,
318                         '$submit' => t('Submit'),
319                         '$lbl_vis1' => t('Profile Visibility'),
320                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
321                         '$lbl_info1' => t('Contact Information / Notes'),
322                         '$infedit' => t('Edit contact notes'),
323                         '$common_text' => $common_text,
324                         '$common_link' => $a->get_baseurl(true) . '/common/' . $contact['id'],
325                         '$all_friends' => $all_friends,
326                         '$relation_text' => $relation_text,
327                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
328                         '$blockunblock' => t('Block/Unblock contact'),
329                         '$ignorecont' => t('Ignore contact'),
330                         '$lblcrepair' => t("Repair URL settings"),
331                         '$lblrecent' => t('View conversations'),
332                         '$lblsuggest' => $lblsuggest,
333                         '$delete' => t('Delete contact'),
334                         '$nettype' => $nettype,
335                         '$poll_interval' => contact_poll_interval($contact['priority'],(! $poll_enabled)),
336                         '$poll_enabled' => $poll_enabled,
337                         '$lastupdtext' => t('Last update:'),
338                         '$updpub' => t('Update public posts'),
339                         '$last_update' => $last_update,
340                         '$udnow' => t('Update now'),
341                         '$profile_select' => contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false)),
342                         '$contact_id' => $contact['id'],
343                         '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
344                         '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
345                         '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
346                         '$info' => $contact['info'],
347                         '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
348                         '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
349                         '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
350                         '$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')),
351                         '$photo' => $contact['photo'],
352                         '$name' => $contact['name'],
353                         '$dir_icon' => $dir_icon,
354                         '$alt_text' => $alt_text,
355                         '$sparkle' => $sparkle,
356                         '$url' => $url
357
358                 ));
359
360                 $arr = array('contact' => $contact,'output' => $o);
361
362                 call_hooks('contact_edit', $arr);
363
364                 return $arr['output'];
365
366         }
367
368         $blocked = false;
369         $hidden = false;
370         $ignored = false;
371         $all = false;
372
373         $_SESSION['return_url'] = $a->query_string;
374
375         if(($a->argc == 2) && ($a->argv[1] === 'all')) {
376                 $sql_extra = '';
377                 $all = true;
378         }
379         elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
380                 $sql_extra = " AND `blocked` = 1 ";
381                 $blocked = true;
382         }
383         elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
384                 $sql_extra = " AND `hidden` = 1 ";
385                 $hidden = true;
386         }
387         elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
388                 $sql_extra = " AND `readonly` = 1 ";
389                 $ignored = true;
390         }
391         elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
392                 $sql_extra = " AND `archive` = 1 ";
393                 $archived = true;
394         }
395         else
396                 $sql_extra = " AND `blocked` = 0 ";
397
398         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
399         $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
400
401         $tabs = array(
402                 array(
403                         'label' => t('Suggestions'),
404                         'url'   => $a->get_baseurl(true) . '/suggest', 
405                         'sel'   => '',
406                 ),
407                 array(
408                         'label' => t('All Contacts'),
409                         'url'   => $a->get_baseurl(true) . '/contacts/all', 
410                         'sel'   => ($all) ? 'active' : '',
411                 ),
412                 array(
413                         'label' => t('Unblocked'),
414                         'url'   => $a->get_baseurl(true) . '/contacts',
415                         'sel'   => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
416                 ),
417
418                 array(
419                         'label' => t('Blocked'),
420                         'url'   => $a->get_baseurl(true) . '/contacts/blocked',
421                         'sel'   => ($blocked) ? 'active' : '',
422                 ),
423
424                 array(
425                         'label' => t('Ignored'),
426                         'url'   => $a->get_baseurl(true) . '/contacts/ignored',
427                         'sel'   => ($ignored) ? 'active' : '',
428                 ),
429
430                 array(
431                         'label' => t('Archived'),
432                         'url'   => $a->get_baseurl(true) . '/contacts/archived',
433                         'sel'   => ($archived) ? 'active' : '',
434                 ),
435
436                 array(
437                         'label' => t('Hidden'),
438                         'url'   => $a->get_baseurl(true) . '/contacts/hidden',
439                         'sel'   => ($hidden) ? 'active' : '',
440                 ),
441
442         );
443
444         $tab_tpl = get_markup_template('common_tabs.tpl');
445         $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
446
447
448
449
450         if($search) {
451                 $search_hdr = $search;
452                 $search = dbesc($search.'*');
453         }
454         $sql_extra .= ((strlen($search)) ? " AND MATCH `name` AGAINST ('$search' IN BOOLEAN MODE) " : "");
455
456         if($nets)
457                 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
458  
459         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : ''); 
460
461         
462         $r = q("SELECT COUNT(*) AS `total` FROM `contact` 
463                 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
464                 intval($_SESSION['uid']));
465         if(count($r)) {
466                 $a->set_pager_total($r[0]['total']);
467                 $total = $r[0]['total'];
468         }
469
470
471
472         $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 ",
473                 intval($_SESSION['uid']),
474                 intval($a->pager['start']),
475                 intval($a->pager['itemspage'])
476         );
477
478         $contacts = array();
479
480         if(count($r)) {
481
482                 foreach($r as $rr) {
483
484                         switch($rr['rel']) {
485                                 case CONTACT_IS_FRIEND:
486                                         $dir_icon = 'images/lrarrow.gif';
487                                         $alt_text = t('Mutual Friendship');
488                                         break;
489                                 case  CONTACT_IS_FOLLOWER;
490                                         $dir_icon = 'images/larrow.gif';
491                                         $alt_text = t('is a fan of yours');
492                                         break;
493                                 case CONTACT_IS_SHARING;
494                                         $dir_icon = 'images/rarrow.gif';
495                                         $alt_text = t('you are a fan of');
496                                         break;
497                                 default:
498                                         break;
499                         }
500                         if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
501                                 $url = "redir/{$rr['id']}";
502                                 $sparkle = ' class="sparkle" ';
503                         }
504                         else { 
505                                 $url = $rr['url'];
506                                 $sparkle = '';
507                         }
508
509
510                         $contacts[] = array(
511                                 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
512                                 'edit_hover' => t('Edit contact'),
513                                 'photo_menu' => contact_photo_menu($rr),
514                                 'id' => $rr['id'],
515                                 'alt_text' => $alt_text,
516                                 'dir_icon' => $dir_icon,
517                                 'thumb' => $rr['thumb'], 
518                                 'name' => $rr['name'],
519                                 'username' => $rr['name'],
520                                 'sparkle' => $sparkle,
521                                 'itemurl' => $rr['url'],
522                                 'url' => $url,
523                                 'network' => network_to_name($rr['network']),
524                         );
525                 }
526
527                 
528
529         }
530         
531         $tpl = get_markup_template("contacts-template.tpl");
532         $o .= replace_macros($tpl,array(
533                 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
534                 '$tabs' => $t,
535                 '$total' => $total,
536                 '$search' => $search_hdr,
537                 '$desc' => t('Search your contacts'),
538                 '$finding' => (strlen($search) ? t('Finding: ') . "'" . $search . "'" : ""),
539                 '$submit' => t('Find'),
540                 '$cmd' => $a->cmd,
541                 '$contacts' => $contacts,
542                 '$paginate' => paginate($a),
543
544         )); 
545         
546         return $o;
547 }