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