]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
There is now a new possibility to update contact data.
[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'] == '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` FROM `contact` WHERE `id` = %d", intval($contact_id));
215         if (!$r)
216                 return;
217
218         $data = probe_url($r[0]["url"]);
219
220         $updatefields = array("name", "nick", "url", "addr", "batch", "notify", "poll", "request", "confirm",
221                                 "poco", "network", "alias", "pubkey");
222         $update = array();
223
224         foreach($updatefields AS $field)
225                 if (isset($data[$field]) AND ($data[$field] != ""))
226                         $update[$field] = $data[$field];
227
228         $query = "";
229
230         if (isset($data["priority"]) AND ($data["priority"] != 0))
231                 $query = "`priority` = ".intval($data["priority"]);
232
233         foreach($update AS $key => $value) {
234                 if ($query != "")
235                         $query .= ", ";
236
237                 $query .= "`".$key."` = '".dbesc($value)."'";
238         }
239
240         if ($query == "")
241                 return;
242
243         $r = q("UPDATE `contact` SET $query WHERE `id` = %d AND `uid` = %d",
244                 intval($contact_id),
245                 intval(local_user())
246         );
247
248         $photos = import_profile_photo($data['photo'], local_user(), $contact_id);
249
250         $r = q("UPDATE `contact` SET `photo` = '%s',
251                         `thumb` = '%s',
252                         `micro` = '%s',
253                         `name-date` = '%s',
254                         `uri-date` = '%s',
255                         `avatar-date` = '%s'
256                         WHERE `id` = %d",
257                         dbesc($photos[0]),
258                         dbesc($photos[1]),
259                         dbesc($photos[2]),
260                         dbesc(datetime_convert()),
261                         dbesc(datetime_convert()),
262                         dbesc(datetime_convert()),
263                         intval($contact_id)
264                 );
265
266 }
267
268 function _contact_block($contact_id, $orig_record) {
269         $blocked = (($orig_record['blocked']) ? 0 : 1);
270         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d",
271                 intval($blocked),
272                 intval($contact_id),
273                 intval(local_user())
274         );
275         return $r;
276
277 }
278 function _contact_ignore($contact_id, $orig_record) {
279         $readonly = (($orig_record['readonly']) ? 0 : 1);
280         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d",
281                 intval($readonly),
282                 intval($contact_id),
283                 intval(local_user())
284         );
285         return $r;
286 }
287 function _contact_archive($contact_id, $orig_record) {
288         $archived = (($orig_record['archive']) ? 0 : 1);
289         $r = q("UPDATE `contact` SET `archive` = %d WHERE `id` = %d AND `uid` = %d",
290                 intval($archived),
291                 intval($contact_id),
292                 intval(local_user())
293         );
294         if ($archived) {
295                 q("UPDATE `item` SET `private` = 2 WHERE `contact-id` = %d AND `uid` = %d", intval($contact_id), intval(local_user()));
296         }
297         return $r;
298 }
299 function _contact_drop($contact_id, $orig_record) {
300         require_once('include/Contact.php');
301         $a = get_app();
302
303         terminate_friendship($a->user,$a->contact,$orig_record);
304         contact_remove($orig_record['id']);
305 }
306
307
308 function contacts_content(&$a) {
309
310         $sort_type = 0;
311         $o = '';
312         nav_set_selected('contacts');
313
314
315         if(! local_user()) {
316                 notice( t('Permission denied.') . EOL);
317                 return;
318         }
319
320         if($a->argc == 3) {
321
322                 $contact_id = intval($a->argv[1]);
323                 if(! $contact_id)
324                         return;
325
326                 $cmd = $a->argv[2];
327
328                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
329                         intval($contact_id),
330                         intval(local_user())
331                 );
332
333                 if(! count($orig_record)) {
334                         notice( t('Could not access contact record.') . EOL);
335                         goaway($a->get_baseurl(true) . '/contacts');
336                         return; // NOTREACHED
337                 }
338
339                 if($cmd === 'update') {
340                         _contact_update($contact_id);
341                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
342                         // NOTREACHED
343                 }
344
345                 if($cmd === 'updateprofile') {
346                         _contact_update_profile($contact_id);
347                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
348                         // NOTREACHED
349                 }
350
351                 if($cmd === 'block') {
352                         $r = _contact_block($contact_id, $orig_record[0]);
353                         if($r) {
354                                 $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
355                                 info((($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')).EOL);
356                         }
357
358                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
359                         return; // NOTREACHED
360                 }
361
362                 if($cmd === 'ignore') {
363                         $r = _contact_ignore($contact_id, $orig_record[0]);
364                         if($r) {
365                                 $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
366                                 info((($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')).EOL);
367                         }
368
369                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
370                         return; // NOTREACHED
371                 }
372
373
374                 if($cmd === 'archive') {
375                         $r = _contact_archive($contact_id, $orig_record[0]);
376                         if($r) {
377                                 $archived = (($orig_record[0]['archive']) ? 0 : 1);
378                                 info((($archived) ? t('Contact has been archived') : t('Contact has been unarchived')).EOL);
379                         }
380
381                         goaway($a->get_baseurl(true) . '/contacts/' . $contact_id);
382                         return; // NOTREACHED
383                 }
384
385                 if($cmd === 'drop') {
386
387                         // Check if we should do HTML-based delete confirmation
388                         if($_REQUEST['confirm']) {
389                                 // <form> can't take arguments in its "action" parameter
390                                 // so add any arguments as hidden inputs
391                                 $query = explode_querystring($a->query_string);
392                                 $inputs = array();
393                                 foreach($query['args'] as $arg) {
394                                         if(strpos($arg, 'confirm=') === false) {
395                                                 $arg_parts = explode('=', $arg);
396                                                 $inputs[] = array('name' => $arg_parts[0], 'value' => $arg_parts[1]);
397                                         }
398                                 }
399
400                                 $a->page['aside'] = '';
401                                 return replace_macros(get_markup_template('confirm.tpl'), array(
402                                         '$method' => 'get',
403                                         '$message' => t('Do you really want to delete this contact?'),
404                                         '$extra_inputs' => $inputs,
405                                         '$confirm' => t('Yes'),
406                                         '$confirm_url' => $query['base'],
407                                         '$confirm_name' => 'confirmed',
408                                         '$cancel' => t('Cancel'),
409                                 ));
410                         }
411                         // Now check how the user responded to the confirmation query
412                         if($_REQUEST['canceled']) {
413                                 if(x($_SESSION,'return_url'))
414                                         goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
415                                 else
416                                         goaway($a->get_baseurl(true) . '/contacts');
417                         }
418
419                         _contact_drop($contact_id, $orig_record[0]);
420                         info( t('Contact has been removed.') . EOL );
421                         if(x($_SESSION,'return_url'))
422                                 goaway($a->get_baseurl(true) . '/' . $_SESSION['return_url']);
423                         else
424                                 goaway($a->get_baseurl(true) . '/contacts');
425                         return; // NOTREACHED
426                 }
427         }
428
429
430
431         $_SESSION['return_url'] = $a->query_string;
432
433         if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
434
435                 $contact_id = $a->data['contact']['id'];
436                 $contact = $a->data['contact'];
437
438                 $editselect = 'none';
439                 if( feature_enabled(local_user(),'richtext') )
440                         $editselect = 'exact';
441
442                 $a->page['htmlhead'] .= replace_macros(get_markup_template('contact_head.tpl'), array(
443                         '$baseurl' => $a->get_baseurl(true),
444                         '$editselect' => $editselect,
445                 ));
446                 $a->page['end'] .= replace_macros(get_markup_template('contact_end.tpl'), array(
447                         '$baseurl' => $a->get_baseurl(true),
448                         '$editselect' => $editselect,
449                 ));
450
451                 require_once('include/contact_selectors.php');
452
453                 $tpl = get_markup_template("contact_edit.tpl");
454
455                 switch($contact['rel']) {
456                         case CONTACT_IS_FRIEND:
457                                 $dir_icon = 'images/lrarrow.gif';
458                                 $relation_text = t('You are mutual friends with %s');
459                                 break;
460                         case CONTACT_IS_FOLLOWER;
461                                 $dir_icon = 'images/larrow.gif';
462                                 $relation_text = t('You are sharing with %s');
463                                 break;
464
465                         case CONTACT_IS_SHARING;
466                                 $dir_icon = 'images/rarrow.gif';
467                                 $relation_text = t('%s is sharing with you');
468                                 break;
469                         default:
470                                 break;
471                 }
472
473                 if(!in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
474                                 $relation_text = "";
475
476                 $relation_text = sprintf($relation_text,$contact['name']);
477
478                 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
479                         $url = "redir/{$contact['id']}";
480                         $sparkle = ' class="sparkle" ';
481                 }
482                 else {
483                         $url = $contact['url'];
484                         $sparkle = '';
485                 }
486
487                 $insecure = t('Private communications are not available for this contact.');
488
489                 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00')
490                                 ? t('Never')
491                                 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
492
493                 if($contact['last-update'] !== '0000-00-00 00:00:00')
494                         $last_update .= ' ' . (($contact['last-update'] <= $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
495
496                 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
497
498                 $poll_enabled = in_array($contact['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2));
499
500                 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network']));
501
502                 $common = count_common_friends(local_user(),$contact['id']);
503                 $common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
504
505                 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : '');
506
507                 $x = count_all_friends(local_user(), $contact['id']);
508                 $all_friends = (($x) ? t('View all contacts') : '');
509
510                 // tabs
511                 $tabs = array(
512                         array(
513                                 'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
514                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/block',
515                                 'sel'   => '',
516                                 'title' => t('Toggle Blocked status'),
517                         ),
518                         array(
519                                 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
520                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/ignore',
521                                 'sel'   => '',
522                                 'title' => t('Toggle Ignored status'),
523                         ),
524
525                         array(
526                                 'label' => (($contact['archive']) ? t('Unarchive') : t('Archive') ),
527                                 'url'   => $a->get_baseurl(true) . '/contacts/' . $contact_id . '/archive',
528                                 'sel'   => '',
529                                 'title' => t('Toggle Archive status'),
530                         ),
531                         array(
532                                 'label' => t('Repair'),
533                                 'url'   => $a->get_baseurl(true) . '/crepair/' . $contact_id,
534                                 'sel'   => '',
535                                 'title' => t('Advanced Contact Settings'),
536                         )
537                 );
538                 $tab_tpl = get_markup_template('common_tabs.tpl');
539                 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
540
541                 $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!') : '');
542
543                 if ($contact['network'] == NETWORK_FEED)
544                         $fetch_further_information = array('fetch_further_information', t('Fetch further information for feeds'), $contact['fetch_further_information'], t('Fetch further information for feeds'),
545                                                                         array('0'=>t('Disabled'), '1'=>t('Fetch information'), '2'=>t('Fetch information and keywords')));
546
547                 if (in_array($contact['network'], array(NETWORK_FEED, NETWORK_MAIL, NETWORK_MAIL2)))
548                         $poll_interval = contact_poll_interval($contact['priority'],(! $poll_enabled));
549
550                 if ($contact['network'] == NETWORK_DFRN)
551                         $profile_select = contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false));
552
553                 $o .= replace_macros($tpl, array(
554                         '$header' => t('Contact Editor'),
555                         '$tab_str' => $tab_str,
556                         '$submit' => t('Submit'),
557                         '$lbl_vis1' => t('Profile Visibility'),
558                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
559                         '$lbl_info1' => t('Contact Information / Notes'),
560                         '$infedit' => t('Edit contact notes'),
561                         '$common_text' => $common_text,
562                         '$common_link' => $a->get_baseurl(true) . '/common/loc/' . local_user() . '/' . $contact['id'],
563                         '$all_friends' => $all_friends,
564                         '$relation_text' => $relation_text,
565                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
566                         '$blockunblock' => t('Block/Unblock contact'),
567                         '$ignorecont' => t('Ignore contact'),
568                         '$lblcrepair' => t("Repair URL settings"),
569                         '$lblrecent' => t('View conversations'),
570                         '$lblsuggest' => $lblsuggest,
571                         '$delete' => t('Delete contact'),
572                         '$nettype' => $nettype,
573                         '$poll_interval' => $poll_interval,
574                         '$poll_enabled' => $poll_enabled,
575                         '$lastupdtext' => t('Last update:'),
576                         '$lost_contact' => $lost_contact,
577                         '$updpub' => t('Update public posts'),
578                         '$last_update' => $last_update,
579                         '$udnow' => t('Update now'),
580                         '$profile_select' => $profile_select,
581                         '$contact_id' => $contact['id'],
582                         '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
583                         '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
584                         '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
585                         '$info' => $contact['info'],
586                         '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
587                         '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
588                         '$archived' => (($contact['archive']) ? t('Currently archived') : ''),
589                         '$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')),
590                         '$notify' => array('notify', t('Notification for new posts'), ($contact['notify_new_posts'] == 1), t('Send a notification of every new post of this contact')),
591                         '$fetch_further_information' => $fetch_further_information,
592                         '$ffi_keyword_blacklist' => $contact['ffi_keyword_blacklist'],
593                         '$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')),
594                         '$photo' => $contact['photo'],
595                         '$name' => $contact['name'],
596                         '$dir_icon' => $dir_icon,
597                         '$alt_text' => $alt_text,
598                         '$sparkle' => $sparkle,
599                         '$url' => $url,
600
601                 ));
602
603                 $arr = array('contact' => $contact,'output' => $o);
604
605                 call_hooks('contact_edit', $arr);
606
607                 return $arr['output'];
608
609         }
610
611         $blocked = false;
612         $hidden = false;
613         $ignored = false;
614         $all = false;
615
616         if(($a->argc == 2) && ($a->argv[1] === 'all')) {
617                 $sql_extra = '';
618                 $all = true;
619         }
620         elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
621                 $sql_extra = " AND `blocked` = 1 ";
622                 $blocked = true;
623         }
624         elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
625                 $sql_extra = " AND `hidden` = 1 ";
626                 $hidden = true;
627         }
628         elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
629                 $sql_extra = " AND `readonly` = 1 ";
630                 $ignored = true;
631         }
632         elseif(($a->argc == 2) && ($a->argv[1] === 'archived')) {
633                 $sql_extra = " AND `archive` = 1 ";
634                 $archived = true;
635         }
636         else
637                 $sql_extra = " AND `blocked` = 0 ";
638
639         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
640         $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
641
642         $tabs = array(
643                 array(
644                         'label' => t('Suggestions'),
645                         'url'   => $a->get_baseurl(true) . '/suggest', 
646                         'sel'   => '',
647                         'title' => t('Suggest potential friends'),
648                 ),
649                 array(
650                         'label' => t('All Contacts'),
651                         'url'   => $a->get_baseurl(true) . '/contacts/all', 
652                         'sel'   => ($all) ? 'active' : '',
653                         'title' => t('Show all contacts'),
654                 ),
655                 array(
656                         'label' => t('Unblocked'),
657                         'url'   => $a->get_baseurl(true) . '/contacts',
658                         'sel'   => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored) && (! $archived)) ? 'active' : '',
659                         'title' => t('Only show unblocked contacts'),
660                 ),
661
662                 array(
663                         'label' => t('Blocked'),
664                         'url'   => $a->get_baseurl(true) . '/contacts/blocked',
665                         'sel'   => ($blocked) ? 'active' : '',
666                         'title' => t('Only show blocked contacts'),
667                 ),
668
669                 array(
670                         'label' => t('Ignored'),
671                         'url'   => $a->get_baseurl(true) . '/contacts/ignored',
672                         'sel'   => ($ignored) ? 'active' : '',
673                         'title' => t('Only show ignored contacts'),
674                 ),
675
676                 array(
677                         'label' => t('Archived'),
678                         'url'   => $a->get_baseurl(true) . '/contacts/archived',
679                         'sel'   => ($archived) ? 'active' : '',
680                         'title' => t('Only show archived contacts'),
681                 ),
682
683                 array(
684                         'label' => t('Hidden'),
685                         'url'   => $a->get_baseurl(true) . '/contacts/hidden',
686                         'sel'   => ($hidden) ? 'active' : '',
687                         'title' => t('Only show hidden contacts'),
688                 ),
689
690         );
691
692         $tab_tpl = get_markup_template('common_tabs.tpl');
693         $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
694
695
696
697         $searching = false;
698         if($search) {
699                 $search_hdr = $search;
700                 $search_txt = dbesc(protect_sprintf(preg_quote($search)));
701                 $searching = true;
702         }
703         $sql_extra .= (($searching) ? " AND (name REGEXP '$search_txt' OR url REGEXP '$search_txt'  OR nick REGEXP '$search_txt') " : "");
704
705         if($nets)
706                 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
707
708         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : '');
709
710
711         $r = q("SELECT COUNT(*) AS `total` FROM `contact`
712                 WHERE `uid` = %d AND `self` = 0 AND `pending` = 0 $sql_extra $sql_extra2 ",
713                 intval($_SESSION['uid']));
714         if(count($r)) {
715                 $a->set_pager_total($r[0]['total']);
716                 $total = $r[0]['total'];
717         }
718
719
720         $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 ",
721                 intval($_SESSION['uid']),
722                 intval($a->pager['start']),
723                 intval($a->pager['itemspage'])
724         );
725
726         $contacts = array();
727
728         if(count($r)) {
729
730                 foreach($r as $rr) {
731
732                         switch($rr['rel']) {
733                                 case CONTACT_IS_FRIEND:
734                                         $dir_icon = 'images/lrarrow.gif';
735                                         $alt_text = t('Mutual Friendship');
736                                         break;
737                                 case  CONTACT_IS_FOLLOWER;
738                                         $dir_icon = 'images/larrow.gif';
739                                         $alt_text = t('is a fan of yours');
740                                         break;
741                                 case CONTACT_IS_SHARING;
742                                         $dir_icon = 'images/rarrow.gif';
743                                         $alt_text = t('you are a fan of');
744                                         break;
745                                 default:
746                                         break;
747                         }
748                         if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
749                                 $url = "redir/{$rr['id']}";
750                                 $sparkle = ' class="sparkle" ';
751                         }
752                         else {
753                                 $url = $rr['url'];
754                                 $sparkle = '';
755                         }
756
757
758                         $contacts[] = array(
759                                 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
760                                 'edit_hover' => t('Edit contact'),
761                                 'photo_menu' => contact_photo_menu($rr),
762                                 'id' => $rr['id'],
763                                 'alt_text' => $alt_text,
764                                 'dir_icon' => $dir_icon,
765                                 'thumb' => proxy_url($rr['thumb']),
766                                 'name' => $rr['name'],
767                                 'username' => $rr['name'],
768                                 'sparkle' => $sparkle,
769                                 'itemurl' => $rr['url'],
770                                 'url' => $url,
771                                 'network' => network_to_name($rr['network']),
772                         );
773                 }
774
775
776
777         }
778
779         $tpl = get_markup_template("contacts-template.tpl");
780         $o .= replace_macros($tpl, array(
781                 '$baseurl' => $a->get_baseurl(),
782                 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
783                 '$tabs' => $t,
784                 '$total' => $total,
785                 '$search' => $search_hdr,
786                 '$desc' => t('Search your contacts'),
787                 '$finding' => (($searching) ? t('Finding: ') . "'" . $search . "'" : ""),
788                 '$submit' => t('Find'),
789                 '$cmd' => $a->cmd,
790                 '$contacts' => $contacts,
791                 '$contact_drop_confirm' => t('Do you really want to delete this contact?'),
792                 '$batch_actions' => array(
793                         'contacts_batch_update' => t('Update'),
794                         'contacts_batch_block' => t('Block')."/".t("Unblock"),
795                         "contacts_batch_ignore" => t('Ignore')."/".t("Unignore"),
796                         "contacts_batch_archive" => t('Archive')."/".t("Unarchive"),
797                         "contacts_batch_drop" => t('Delete'),
798                 ),
799                 '$paginate' => paginate($a),
800
801         ));
802
803         return $o;
804 }