]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
Bugfix: The user id wasn't fetched correctly.
[friendica.git] / mod / contacts.php
1 <?php
2
3 require_once('include/Contact.php');
4 require_once('include/socgraph.php');
5 require_once('include/contact_selectors.php');
6 require_once('include/Scrape.php');
7 require_once('mod/proxy.php');
8 require_once('include/Photo.php');
9
10 function contacts_init(&$a) {
11         if(! local_user())
12                 return;
13
14         $contact_id = 0;
15
16         if(($a->argc == 2) && intval($a->argv[1])) {
17                 $contact_id = intval($a->argv[1]);
18                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
19                         intval(local_user()),
20                         intval($contact_id)
21                 );
22                 if(! count($r)) {
23                         $contact_id = 0;
24                 }
25         }
26
27         require_once('include/group.php');
28         require_once('include/contact_widgets.php');
29
30         if(! x($a->page,'aside'))
31                 $a->page['aside'] = '';
32
33         if($contact_id) {
34                         $a->data['contact'] = $r[0];
35                         $vcard_widget = replace_macros(get_markup_template("vcard-widget.tpl"),array(
36                                 '$name' => $a->data['contact']['name'],
37                                 '$photo' => $a->data['contact']['photo'],
38                                 '$url' => ($a->data['contact']['network'] == NETWORK_DFRN) ? $a->get_baseurl()."/redir/".$a->data['contact']['id'] : $a->data['contact']['url']
39                         ));
40                         $follow_widget = '';
41         }
42         else {
43                 $vcard_widget = '';
44                 if (isset($_GET['add']))
45                         $follow_widget = follow_widget($_GET['add']);
46                 else
47                         $follow_widget = follow_widget();
48         }
49
50         if ($_GET['nets'] == "all")
51                 $_GET['nets'] = "";
52
53         $groups_widget .= group_side('contacts','group',false,0,$contact_id);
54         $findpeople_widget .= findpeople_widget();
55         $networks_widget .= networks_widget('contacts',$_GET['nets']);
56         $a->page['aside'] .= replace_macros(get_markup_template("contacts-widget-sidebar.tpl"),array(
57                 '$vcard_widget' => $vcard_widget,
58                 '$follow_widget' => $follow_widget,
59                 '$groups_widget' => $groups_widget,
60                 '$findpeople_widget' => $findpeople_widget,
61                 '$networks_widget' => $networks_widget
62         ));
63
64         $base = $a->get_baseurl();
65         $tpl = get_markup_template("contacts-head.tpl");
66         $a->page['htmlhead'] .= replace_macros($tpl,array(
67                 '$baseurl' => $a->get_baseurl(true),
68                 '$base' => $base
69         ));
70
71         $tpl = get_markup_template("contacts-end.tpl");
72         $a->page['end'] .= replace_macros($tpl,array(
73                 '$baseurl' => $a->get_baseurl(true),
74                 '$base' => $base
75         ));
76
77
78 }
79
80 function contacts_batch_actions(&$a){
81         $contacts_id = $_POST['contact_batch'];
82         if (!is_array($contacts_id)) return;
83
84         $orig_records = q("SELECT * FROM `contact` WHERE `id` IN (%s) AND `uid` = %d AND `self` = 0",
85                 implode(",", $contacts_id),
86                 intval(local_user())
87         );
88
89         $count_actions=0;
90         foreach($orig_records as $orig_record) {
91                 $contact_id = $orig_record['id'];
92                 if (x($_POST, 'contacts_batch_update')) {
93                         _contact_update($contact_id);
94                         $count_actions++;
95                 }
96                 if (x($_POST, 'contacts_batch_block')) {
97                         $r  = _contact_block($contact_id, $orig_record);
98                         if ($r) $count_actions++;
99                 }
100                 if (x($_POST, 'contacts_batch_ignore')) {
101                         $r = _contact_ignore($contact_id, $orig_record);
102                         if ($r) $count_actions++;
103                 }
104                 if (x($_POST, 'contacts_batch_archive')) {
105                         $r = _contact_archive($contact_id, $orig_record);
106                         if ($r) $count_actions++;
107                 }
108                 if (x($_POST, 'contacts_batch_drop')) {
109                         _contact_drop($contact_id, $orig_record);
110                         $count_actions++;
111                 }
112         }
113         if ($count_actions>0) {
114                 info ( sprintf( tt("%d contact edited.", "%d contacts edited", $count_actions), $count_actions) );
115         }
116
117         if(x($_SESSION,'return_url'))
118                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
119         else
120                 goaway($a->get_baseurl(true) . '/contacts');
121
122 }
123
124
125 function contacts_post(&$a) {
126
127         if(! local_user())
128                 return;
129
130         if ($a->argv[1]==="batch") {
131                 contacts_batch_actions($a);
132                 return;
133         }
134
135         $contact_id = intval($a->argv[1]);
136         if(! $contact_id)
137                 return;
138
139         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
140                 intval($contact_id),
141                 intval(local_user())
142         );
143
144         if(! count($orig_record)) {
145                 notice( t('Could not access contact record.') . EOL);
146                 goaway($a->get_baseurl(true) . '/contacts');
147                 return; // NOTREACHED
148         }
149
150         call_hooks('contact_edit_post', $_POST);
151
152         $profile_id = intval($_POST['profile-assign']);
153         if($profile_id) {
154                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
155                         intval($profile_id),
156                         intval(local_user())
157                 );
158                 if(! count($r)) {
159                         notice( t('Could not locate selected profile.') . EOL);
160                         return;
161                 }
162         }
163
164         $hidden = intval($_POST['hidden']);
165
166         $notify = intval($_POST['notify']);
167
168         $fetch_further_information = intval($_POST['fetch_further_information']);
169
170         $ffi_keyword_blacklist = fix_mce_lf(escape_tags(trim($_POST['ffi_keyword_blacklist'])));
171
172         $priority = intval($_POST['poll']);
173         if($priority > 5 || $priority < 0)
174                 $priority = 0;
175
176         $info = fix_mce_lf(escape_tags(trim($_POST['info'])));
177
178         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
179                 `hidden` = %d, `notify_new_posts` = %d, `fetch_further_information` = %d,
180                 `ffi_keyword_blacklist` = '%s' WHERE `id` = %d AND `uid` = %d",
181                 intval($profile_id),
182                 intval($priority),
183                 dbesc($info),
184                 intval($hidden),
185                 intval($notify),
186                 intval($fetch_further_information),
187                 dbesc($ffi_keyword_blacklist),
188                 intval($contact_id),
189                 intval(local_user())
190         );
191         if($r)
192                 info( t('Contact updated.') . EOL);
193         else
194                 notice( t('Failed to update contact record.') . EOL);
195
196         $r = q("select * from contact where id = %d and uid = %d limit 1",
197                 intval($contact_id),
198                 intval(local_user())
199         );
200         if($r && count($r))
201                 $a->data['contact'] = $r[0];
202
203         return;
204
205 }
206
207 /*contact actions*/
208 function _contact_update($contact_id) {
209         // pull feed and consume it, which should subscribe to the hub.
210         proc_run('php',"include/poller.php","$contact_id");
211 }
212
213 function _contact_update_profile($contact_id) {
214         $r = q("SELECT `url`, `network` FROM `contact` WHERE `id` = %d", intval($contact_id));
215         if (!$r)
216                 return;
217
218         $data = probe_url($r[0]["url"]);
219
220         // "Feed" is mostly a sign of communication problems
221         if (($data["network"] == NETWORK_FEED) AND ($data["network"] != $r[0]["network"]))
222                 return;
223
224         $updatefields = array("name", "nick", "url", "addr", "batch", "notify", "poll", "request", "confirm",
225                                 "poco", "network", "alias", "pubkey");
226         $update = array();
227
228         foreach($updatefields AS $field)
229                 if (isset($data[$field]) AND ($data[$field] != ""))
230                         $update[$field] = $data[$field];
231
232         $update["nurl"] = normalise_link($data["url"]);
233
234         $query = "";
235
236         if (isset($data["priority"]) AND ($data["priority"] != 0))
237                 $query = "`priority` = ".intval($data["priority"]);
238
239         foreach($update AS $key => $value) {
240                 if ($query != "")
241                         $query .= ", ";
242
243                 $query .= "`".$key."` = '".dbesc($value)."'";
244         }
245
246         if ($query == "")
247                 return;
248
249         $r = q("UPDATE `contact` SET $query WHERE `id` = %d AND `uid` = %d",
250                 intval($contact_id),
251                 intval(local_user())
252         );
253
254         $photos = import_profile_photo($data['photo'], local_user(), $contact_id);
255
256         $r = q("UPDATE `contact` SET `photo` = '%s',
257                         `thumb` = '%s',
258                         `micro` = '%s',
259                         `name-date` = '%s',
260                         `uri-date` = '%s',
261                         `avatar-date` = '%s'
262                         WHERE `id` = %d",
263                         dbesc($photos[0]),
264                         dbesc($photos[1]),
265                         dbesc($photos[2]),
266                         dbesc(datetime_convert()),
267                         dbesc(datetime_convert()),
268                         dbesc(datetime_convert()),
269                         intval($contact_id)
270                 );
271
272 }
273
274 function _contact_block($contact_id, $orig_record) {
275         $blocked = (($orig_record['blocked']) ? 0 : 1);
276         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d",
277                 intval($blocked),
278                 intval($contact_id),
279                 intval(local_user())
280         );
281         return $r;
282
283 }
284 function _contact_ignore($contact_id, $orig_record) {
285         $readonly = (($orig_record['readonly']) ? 0 : 1);
286         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d",
287                 intval($readonly),
288                 intval($contact_id),
289                 intval(local_user())
290         );
291         return $r;
292 }
293 function _contact_archive($contact_id, $orig_record) {
294         $archived = (($orig_record['archive']) ? 0 : 1);
295         $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d",
296                 intval($archived),
297                 intval($contact_id),
298                 intval(local_user())
299         );
300         if ($archived) {
301                 q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact_id), intval(local_user()));
302         }
303         return $r;
304 }
305 function _contact_drop($contact_id, $orig_record) {
306         require_once('include/Contact.php');
307         $a = get_app();
308
309         terminate_friendship($a->user,$a->contact,$orig_record);
310         contact_remove($orig_record['id']);
311 }
312
313
314 function contacts_content(&$a) {
315
316         $sort_type = 0;
317         $o = '';
318         nav_set_selected('contacts');
319
320
321         if(! local_user()) {
322                 notice( t('Permission denied.') . EOL);
323                 return;
324         }
325
326         if($a->argc == 3) {
327
328                 $contact_id = intval($a->argv[1]);
329                 if(! $contact_id)
330                         return;
331
332                 $cmd = $a->argv[2];
333
334                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
335                         intval($contact_id),
336                         intval(local_user())
337                 );
338
339                 if(! count($orig_record)) {
340                         notice( t('Could not access contact record.') . EOL);
341                         goaway($a->get_baseurl(true) . '/contacts');
342                         return; // NOTREACHED
343                 }
344
345                 if($cmd === 'update') {
346                         _contact_update($contact_id);
347                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
348                         // NOTREACHED
349                 }
350
351                 if($cmd === 'updateprofile') {
352                         _contact_update_profile($contact_id);
353                         goaway($a->get_baseurl(true) . '/crepair/' . $contact_id);
354                         // NOTREACHED
355                 }
356
357                 if($cmd === 'block') {
358                         $r = _contact_block($contact_id, $orig_record[0]);
359                         if($r) {
360                                 $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
361                                 info((($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')).EOL);
362                         }
363
364                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
365                         return; // NOTREACHED
366                 }
367
368                 if($cmd === 'ignore') {
369                         $r = _contact_ignore($contact_id, $orig_record[0]);
370                         if($r) {
371                                 $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
372                                 info((($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')).EOL);
373                         }
374
375                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
376                         return; // NOTREACHED
377                 }
378
379
380                 if($cmd === 'archive') {
381                         $r = _contact_archive($contact_id, $orig_record[0]);
382                         if($r) {
383                                 $archived = (($orig_record[0]['archive']) ? 0 : 1);
384                                 info((($archived) ? t('Contact has been archived') : t('Contact has been unarchived')).EOL);
385                         }
386
387                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
388                         return; // NOTREACHED
389                 }
390
391                 if($cmd === 'drop') {
392
393                         // Check if we should do HTML-based delete confirmation
394                         if($_REQUEST['confirm']) {
395                                 // <form> can't take arguments in its "action" parameter
396                                 // so add any arguments as hidden inputs
397                                 $query = explode_querystring($a->query_string);
398                                 $inputs = array();
399                                 foreach($query['args'] as $arg) {
400                                         if(strpos($arg, 'confirm=') === false) {
401                                                 $arg_parts = explode('=', $arg);
402                                                 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
403                                         }
404                                 }
405
406                                 $a->page['aside'] = '';
407                                 return replace_macros(get_markup_template('confirm.tpl'), array(
408                                         '$method' => 'get',
409                                         '$message' => t('Do you really want to delete this contact?'),
410                                         '$extra_inputs' => $inputs,
411                                         '$confirm' => t('Yes'),
412                                         '$confirm_url' => $query['base'],
413                                         '$confirm_name' => 'confirmed',
414                                         '$cancel' => t('Cancel'),
415                                 ));
416                         }
417                         // Now check how the user responded to the confirmation query
418                         if($_REQUEST['canceled']) {
419                                 if(x($_SESSION,'return_url'))
420                                         goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
421                                 else
422                                         goaway($a->get_baseurl(true) . '/contacts');
423                         }
424
425                         _contact_drop($contact_id, $orig_record[0]);
426                         info( t('Contact has been removed.') . EOL );
427                         if(x($_SESSION,'return_url'))
428                                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
429                         else
430                                 goaway($a->get_baseurl(true) . '/contacts');
431                         return; // NOTREACHED
432                 }
433         }
434
435
436
437         $_SESSION['return_url'] = $a->query_string;
438
439         if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
440
441                 $contact_id = $a->data['contact']['id'];
442                 $contact = $a->data['contact'];
443
444                 $editselect = 'none';
445                 if( feature_enabled(local_user(),'richtext') )
446                         $editselect = 'exact';
447
448                 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
449                         '$baseurl' => $a->get_baseurl(true),
450                         '$editselect' => $editselect,
451                 ));
452                 $a->page['end'] .= replace_macros(get_markup_template('contact_end.tpl'), array(
453                         '$baseurl' => $a->get_baseurl(true),
454                         '$editselect' => $editselect,
455                 ));
456
457                 require_once('include/contact_selectors.php');
458
459                 $tpl = get_markup_template("contact_edit.tpl");
460
461                 switch($contact['rel']) {
462                         case CONTACT_IS_FRIEND:
463                                 $dir_icon = 'images/lrarrow.gif';
464                                 $relation_text = t('You are mutual friends with %s');
465                                 break;
466                         case CONTACT_IS_FOLLOWER;
467                                 $dir_icon = 'images/larrow.gif';
468                                 $relation_text = t('You are sharing with %s');
469                                 break;
470
471                         case CONTACT_IS_SHARING;
472                                 $dir_icon = 'images/rarrow.gif';
473                                 $relation_text = t('%s is sharing with you');
474                                 break;
475                         default:
476                                 break;
477                 }
478
479                 if(!in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
480                                 $relation_text = "";
481
482                 $relation_text = sprintf($relation_text,$contact['name']);
483
484                 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
485                         $url = "redir/{$contact['id']}";
486                         $sparkle = ' class="sparkle" ';
487                 }
488                 else {
489                         $url = $contact['url'];
490                         $sparkle = '';
491                 }
492
493                 $insecure = t('Private communications are not available for this contact.');
494
495                 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
496                                 ? t('Never')
497                                 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
498
499                 if($contact['last-update'] !== '0000-00-00 00:00:00')
500                         $last_update .= ' ' . (($contact['last-update'] <= $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
501
502                 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
503
504                 $poll_enabled = in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2));
505
506                 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network']));
507
508                 $common = count_common_friends(local_user(),$contact['id']);
509                 $common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
510
511                 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : '');
512
513                 $x = count_all_friends(local_user(), $contact['id']);
514                 $all_friends = (($x) ? t('View all contacts') : '');
515
516                 // tabs
517                 $tabs = array(
518                         array(
519                                 'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
520                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
521                                 'sel'   => '',
522                                 'title' => t('Toggle Blocked status'),
523                         ),
524                         array(
525                                 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
526                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
527                                 'sel'   => '',
528                                 'title' => t('Toggle Ignored status'),
529                         ),
530
531                         array(
532                                 'label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
533                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
534                                 'sel'   => '',
535                                 'title' => t('Toggle Archive status'),
536                         ),
537                         array(
538                                 'label' => t('Repair'),
539                                 'url'   => $a->get_baseurl(true) . '/crepair/' . $contact_id,
540                                 'sel'   => '',
541                                 'title' => t('Advanced Contact Settings'),
542                         )
543                 );
544                 $tab_tpl = get_markup_template('common_tabs.tpl');
545                 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
546
547                 $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!') : '');
548
549                 if ($contact['network'] == NETWORK_FEED)
550                         $fetch_further_information = array('fetch_further_information', t('Fetch further information for feeds'), $contact['fetch_further_information'], t('Fetch further information for feeds'),
551                                                                         array('0'=>t('Disabled'), '1'=>t('Fetch information'), '2'=>t('Fetch information and keywords')));
552
553                 if (in_array($contact['network'], array(NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2)))
554                         $poll_interval = contact_poll_interval($contact['priority'],(! $poll_enabled));
555
556                 if ($contact['network'] == NETWORK_DFRN)
557                         $profile_select = contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false));
558
559                 $o .= replace_macros($tpl, array(
560                         '$header' => t('Contact Editor'),
561                         '$tab_str' => $tab_str,
562                         '$submit' => t('Submit'),
563                         '$lbl_vis1' => t('Profile Visibility'),
564                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
565                         '$lbl_info1' => t('Contact Information / Notes'),
566                         '$infedit' => t('Edit contact notes'),
567                         '$common_text' => $common_text,
568                         '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
569                         '$all_friends' => $all_friends,
570                         '$relation_text' => $relation_text,
571                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
572                         '$blockunblock' => t('Block/Unblock contact'),
573                         '$ignorecont' => t('Ignore contact'),
574                         '$lblcrepair' => t("Repair URL settings"),
575                         '$lblrecent' => t('View conversations'),
576                         '$lblsuggest' => $lblsuggest,
577                         '$delete' => t('Delete contact'),
578                         '$nettype' => $nettype,
579                         '$poll_interval' => $poll_interval,
580                         '$poll_enabled' => $poll_enabled,
581                         '$lastupdtext' => t('Last update:'),
582                         '$lost_contact' => $lost_contact,
583                         '$updpub' => t('Update public posts'),
584                         '$last_update' => $last_update,
585                         '$udnow' => t('Update now'),
586                         '$profile_select' => $profile_select,
587                         '$contact_id' => $contact['id'],
588                         '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
589                         '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
590                         '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
591                         '$info' => $contact['info'],
592                         '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
593                         '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
594                         '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
595                         '$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')),
596                         '$notify' => array('notify', t('Notification for new posts'), ($contact['notify_new_posts'] == 1), t('Send a notification of every new post of this contact')),
597                         '$fetch_further_information' => $fetch_further_information,
598                         '$ffi_keyword_blacklist' => $contact['ffi_keyword_blacklist'],
599                         '$ffi_keyword_blacklist' => array('ffi_keyword_blacklist', t('Blacklisted keywords'), $contact['ffi_keyword_blacklist'], t('Comma separated list of keywords that should not be converted to hashtags, when "Fetch information and keywords" is selected')),
600                         '$photo' => $contact['photo'],
601                         '$name' => $contact['name'],
602                         '$dir_icon' => $dir_icon,
603                         '$alt_text' => $alt_text,
604                         '$sparkle' => $sparkle,
605                         '$url' => $url,
606
607                 ));
608
609                 $arr = array('contact' => $contact,'output' => $o);
610
611                 call_hooks('contact_edit', $arr);
612
613                 return $arr['output'];
614
615         }
616
617         $blocked = false;
618         $hidden = false;
619         $ignored = false;
620         $all = false;
621
622         if(($a->argc == 2) && ($a->argv[1] === 'all')) {
623                 $sql_extra = '';
624                 $all = true;
625         }
626         elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
627                 $sql_extra = " AND `blocked` = 1 ";
628                 $blocked = true;
629         }
630         elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
631                 $sql_extra = " AND `hidden` = 1 ";
632                 $hidden = true;
633         }
634         elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
635                 $sql_extra = " AND `readonly` = 1 ";
636                 $ignored = true;
637         }
638         elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
639                 $sql_extra = " AND `archive` = 1 ";
640                 $archived = true;
641         }
642         else
643                 $sql_extra = " AND `blocked` = 0 ";
644
645         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
646         $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
647
648         $tabs = array(
649                 array(
650                         'label' => t('Suggestions'),
651                         'url'   => $a->get_baseurl(true) . '/suggest', 
652                         'sel'   => '',
653                         'title' => t('Suggest potential friends'),
654                 ),
655                 array(
656                         'label' => t('All Contacts'),
657                         'url'   => $a->get_baseurl(true) . '/contacts/all', 
658                         'sel'   => ($all) ? 'active' : '',
659                         'title' => t('Show all contacts'),
660                 ),
661                 array(
662                         'label' => t('Unblocked'),
663                         'url'   => $a->get_baseurl(true) . '/contacts',
664                         'sel'   => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
665                         'title' => t('Only show unblocked contacts'),
666                 ),
667
668                 array(
669                         'label' => t('Blocked'),
670                         'url'   => $a->get_baseurl(true) . '/contacts/blocked',
671                         'sel'   => ($blocked) ? 'active' : '',
672                         'title' => t('Only show blocked contacts'),
673                 ),
674
675                 array(
676                         'label' => t('Ignored'),
677                         'url'   => $a->get_baseurl(true) . '/contacts/ignored',
678                         'sel'   => ($ignored) ? 'active' : '',
679                         'title' => t('Only show ignored contacts'),
680                 ),
681
682                 array(
683                         'label' => t('Archived'),
684                         'url'   => $a->get_baseurl(true) . '/contacts/archived',
685                         'sel'   => ($archived) ? 'active' : '',
686                         'title' => t('Only show archived contacts'),
687                 ),
688
689                 array(
690                         'label' => t('Hidden'),
691                         'url'   => $a->get_baseurl(true) . '/contacts/hidden',
692                         'sel'   => ($hidden) ? 'active' : '',
693                         'title' => t('Only show hidden contacts'),
694                 ),
695
696         );
697
698         $tab_tpl = get_markup_template('common_tabs.tpl');
699         $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
700
701
702
703         $searching = false;
704         if($search) {
705                 $search_hdr = $search;
706                 $search_txt = dbesc(protect_sprintf(preg_quote($search)));
707                 $searching = true;
708         }
709         $sql_extra .= (($searching) ? " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt'  OR nick REGEXP '$search_txt') " : "");
710
711         if($nets)
712                 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
713
714         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : '');
715
716
717         $r = q("SELECT COUNT(*) AS `total` FROM `contact`
718                 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
719                 intval($_SESSION['uid']));
720         if(count($r)) {
721                 $a->set_pager_total($r[0]['total']);
722                 $total = $r[0]['total'];
723         }
724
725
726         $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 ",
727                 intval($_SESSION['uid']),
728                 intval($a->pager['start']),
729                 intval($a->pager['itemspage'])
730         );
731
732         $contacts = array();
733
734         if(count($r)) {
735
736                 foreach($r as $rr) {
737
738                         switch($rr['rel']) {
739                                 case CONTACT_IS_FRIEND:
740                                         $dir_icon = 'images/lrarrow.gif';
741                                         $alt_text = t('Mutual Friendship');
742                                         break;
743                                 case  CONTACT_IS_FOLLOWER;
744                                         $dir_icon = 'images/larrow.gif';
745                                         $alt_text = t('is a fan of yours');
746                                         break;
747                                 case CONTACT_IS_SHARING;
748                                         $dir_icon = 'images/rarrow.gif';
749                                         $alt_text = t('you are a fan of');
750                                         break;
751                                 default:
752                                         break;
753                         }
754                         if(($rr['network'] === NETWORK_DFRN) && ($rr['rel'])) {
755                                 $url = "redir/{$rr['id']}";
756                                 $sparkle = ' class="sparkle" ';
757                         }
758                         else {
759                                 $url = $rr['url'];
760                                 $sparkle = '';
761                         }
762
763
764                         $contacts[] = array(
765                                 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
766                                 'edit_hover' => t('Edit contact'),
767                                 'photo_menu' => contact_photo_menu($rr),
768                                 'id' => $rr['id'],
769                                 'alt_text' => $alt_text,
770                                 'dir_icon' => $dir_icon,
771                                 'thumb' => proxy_url($rr['thumb']),
772                                 'name' => $rr['name'],
773                                 'username' => $rr['name'],
774                                 'sparkle' => $sparkle,
775                                 'itemurl' => $rr['url'],
776                                 'url' => $url,
777                                 'network' => network_to_name($rr['network']),
778                         );
779                 }
780
781
782
783         }
784
785         $tpl = get_markup_template("contacts-template.tpl");
786         $o .= replace_macros($tpl, array(
787                 '$baseurl' => $a->get_baseurl(),
788                 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
789                 '$tabs' => $t,
790                 '$total' => $total,
791                 '$search' => $search_hdr,
792                 '$desc' => t('Search your contacts'),
793                 '$finding' => (($searching) ? t('Finding: ') . "'" . $search . "'" : ""),
794                 '$submit' => t('Find'),
795                 '$cmd' => $a->cmd,
796                 '$contacts' => $contacts,
797                 '$contact_drop_confirm' => t('Do you really want to delete this contact?'),
798                 '$batch_actions' => array(
799                         'contacts_batch_update' => t('Update'),
800                         'contacts_batch_block' => t('Block')."/".t("Unblock"),
801                         "contacts_batch_ignore" => t('Ignore')."/".t("Unignore"),
802                         "contacts_batch_archive" => t('Archive')."/".t("Unarchive"),
803                         "contacts_batch_drop" => t('Delete'),
804                 ),
805                 '$paginate' => paginate($a),
806
807         ));
808
809         return $o;
810 }