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