]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
quattro post preview, style contacts pages, style for "shiny" class, fix contacts...
[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() . '/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 = 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() . '/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() . '/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() . '/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() . '/contacts/' . $contact_id);
182                         return; // NOTREACHED
183                 }
184
185                 if($cmd === 'drop') {
186
187                         // create an unfollow slap
188
189                         if($orig_record[0]['network'] === NETWORK_OSTATUS) {
190                                 $tpl = get_markup_template('follow_slap.tpl');
191                                 $slap = replace_macros($tpl, array(
192                                         '$name' => $a->user['username'],
193                                         '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
194                                         '$photo' => $a->contact['photo'],
195                                         '$thumb' => $a->contact['thumb'],
196                                         '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
197                                         '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':unfollow:' . random_string(),
198                                         '$title' => '',
199                                         '$type' => 'text',
200                                         '$content' => t('stopped following'),
201                                         '$nick' => $a->user['nickname'],
202                                         '$verb' => 'http://ostatus.org/schema/1.0/unfollow', // ACTIVITY_UNFOLLOW,
203                                         '$ostat_follow' => '' // '<as:verb>http://ostatus.org/schema/1.0/unfollow</as:verb>' . "\r\n"
204                                 ));
205
206                                 if((x($orig_record[0],'notify')) && (strlen($orig_record[0]['notify']))) {
207                                         require_once('include/salmon.php');
208                                         slapper($a->user,$orig_record[0]['notify'],$slap);
209                                 }
210                         }
211                         elseif($orig_record[0]['network'] === NETWORK_DIASPORA) {
212                                 require_once('include/diaspora.php');
213                                 diaspora_unshare($a->user,$orig_record[0]);
214                         }
215                         elseif($orig_record[0]['network'] === NETWORK_DFRN) {
216                                 require_once('include/items.php');
217                                 dfrn_deliver($a->user,$orig_record[0],'placeholder', 1);
218                         }
219
220                         contact_remove($orig_record[0]['id']);
221                         info( t('Contact has been removed.') . EOL );
222                         if(x($_SESSION,'return_url'))
223                                 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
224                         else
225                                 goaway($a->get_baseurl() . '/contacts');
226                         return; // NOTREACHED
227                 }
228         }
229
230         if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
231
232                 $contact_id = $a->data['contact']['id'];
233                 $contact = $a->data['contact'];
234
235                 $tpl = get_markup_template('contact_head.tpl');
236                 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
237
238                 require_once('include/contact_selectors.php');
239
240                 $tpl = get_markup_template("contact_edit.tpl");
241
242                 switch($contact['rel']) {
243                         case CONTACT_IS_FRIEND:
244                                 $dir_icon = 'images/lrarrow.gif';
245                                 $relation_text = t('You are mutual friends with %s');
246                                 break;
247                         case CONTACT_IS_FOLLOWER;
248                                 $dir_icon = 'images/larrow.gif';
249                                 $relation_text = t('You are sharing with %s');
250                                 break;
251         
252                         case CONTACT_IS_SHARING;
253                                 $dir_icon = 'images/rarrow.gif';
254                                 $relation_text = t('%s is sharing with you');
255                                 break;
256                         default:
257                                 break;
258                 }
259
260                 $relation_text = sprintf($relation_text,$contact['name']);
261
262                 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
263                         $url = "redir/{$contact['id']}";
264                         $sparkle = ' class="sparkle" ';
265                 }
266                 else { 
267                         $url = $contact['url'];
268                         $sparkle = '';
269                 }
270
271                 $insecure = t('Private communications are not available for this contact.');
272
273                 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00') 
274                                 ? t('Never') 
275                                 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
276
277                 if($contact['last-update'] !== '0000-00-00 00:00:00')
278                         $last_update .= ' ' . (($contact['last-update'] == $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
279
280                 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
281
282                 $poll_enabled = (($contact['network'] !== NETWORK_DIASPORA) ? true : false);
283
284                 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network']));
285
286                 $common = count_common_friends(local_user(),$contact['id']);
287                 $common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
288
289                 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : ''); 
290
291                 $x = count_all_friends(local_user(), $contact['id']);
292                 $all_friends = (($x) ? t('View all contacts') : '');
293
294                 // tabs
295                 $tabs = array(
296                         array(
297                                 'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
298                                 'url'   => $a->get_baseurl() . '/contacts/' . $contact_id . '/block',
299                                 'sel'   => '',
300                         ),
301                         array(
302                                 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
303                                 'url'   => $a->get_baseurl() . '/contacts/' . $contact_id . '/ignore',
304                                 'sel'   => '',
305                         ),
306                         array(
307                                 'label' => t('Repair'),
308                                 'url'   => $a->get_baseurl() . '/crepair/' . $contact_id,
309                                 'sel'   => '',
310                         )
311                 );
312                 $tab_tpl = get_markup_template('common_tabs.tpl');
313                 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
314
315
316                 $o .= replace_macros($tpl,array(
317                         '$header' => t('Contact Editor'),
318                         '$tab_str' => $tab_str,
319                         '$submit' => t('Submit'),
320                         '$lbl_vis1' => t('Profile Visibility'),
321                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
322                         '$lbl_info1' => t('Contact Information / Notes'),
323                         '$infedit' => t('Edit contact notes'),
324                         '$common_text' => $common_text,
325                         '$common_link' => $a->get_baseurl() . '/common/' . $contact['id'],
326                         '$all_friends' => $all_friends,
327                         '$relation_text' => $relation_text,
328                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
329                         '$blockunblock' => t('Block/Unblock contact'),
330                         '$ignorecont' => t('Ignore contact'),
331                         '$lblcrepair' => t("Repair URL settings"),
332                         '$lblrecent' => t('View conversations'),
333                         '$lblsuggest' => $lblsuggest,
334                         '$delete' => t('Delete contact'),
335                         '$nettype' => $nettype,
336                         '$poll_interval' => contact_poll_interval($contact['priority'],(! $poll_enabled)),
337                         '$poll_enabled' => $poll_enabled,
338                         '$lastupdtext' => t('Last update:'),
339                         '$updpub' => t('Update public posts'),
340                         '$last_update' => $last_update,
341                         '$udnow' => t('Update now'),
342                         '$profile_select' => contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false)),
343                         '$contact_id' => $contact['id'],
344                         '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
345                         '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
346                         '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
347                         '$info' => $contact['info'],
348                         '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
349                         '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
350                         '$hidden' => array('hidden', t('Hide this contact from others'), ($contact['hidden'] == 1), t('Replies/likes to your public posts <strong>may</strong> still be visible')),
351                         '$photo' => $contact['photo'],
352                         '$name' => $contact['name'],
353                         '$dir_icon' => $dir_icon,
354                         '$alt_text' => $alt_text,
355                         '$sparkle' => $sparkle,
356                         '$url' => $url
357
358                 ));
359
360                 $arr = array('contact' => $contact,'output' => $o);
361
362                 call_hooks('contact_edit', $arr);
363
364                 return $arr['output'];
365
366         }
367
368         $blocked = false;
369         $hidden = false;
370         $ignored = false;
371         $all = false;
372
373         $_SESSION['return_url'] = $a->query_string;
374
375         if(($a->argc == 2) && ($a->argv[1] === 'all')) {
376                 $sql_extra = '';
377                 $all = true;
378         }
379         elseif(($a->argc == 2) && ($a->argv[1] === 'blocked')) {
380                 $sql_extra = " AND `blocked` = 1 ";
381                 $blocked = true;
382         }
383         elseif(($a->argc == 2) && ($a->argv[1] === 'hidden')) {
384                 $sql_extra = " AND `hidden` = 1 ";
385                 $hidden = true;
386         }
387         elseif(($a->argc == 2) && ($a->argv[1] === 'ignored')) {
388                 $sql_extra = " AND `readonly` = 1 ";
389                 $ignored = true;
390         }
391         else
392                 $sql_extra = " AND `blocked` = 0 ";
393
394         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
395         $nets = ((x($_GET,'nets')) ? notags(trim($_GET['nets'])) : '');
396
397         $tabs = array(
398                 array(
399                         'label' => t('All Contacts'),
400                         'url'   => $a->get_baseurl() . '/contacts/all', 
401                         'sel'   => ($all) ? 'active' : '',
402                 ),
403                 array(
404                         'label' => t('Unblocked Contacts'),
405                         'url'   => $a->get_baseurl() . '/contacts',
406                         'sel'   => ((! $all) && (! $blocked) && (! $hidden) && (! $search) && (! $nets) && (! $ignored)) ? 'active' : '',
407                 ),
408
409                 array(
410                         'label' => t('Blocked Contacts'),
411                         'url'   => $a->get_baseurl() . '/contacts/blocked',
412                         'sel'   => ($blocked) ? 'active' : '',
413                 ),
414
415                 array(
416                         'label' => t('Ignored Contacts'),
417                         'url'   => $a->get_baseurl() . '/contacts/ignored',
418                         'sel'   => ($ignored) ? 'active' : '',
419                 ),
420
421                 array(
422                         'label' => t('Hidden Contacts'),
423                         'url'   => $a->get_baseurl() . '/contacts/hidden',
424                         'sel'   => ($hidden) ? 'active' : '',
425                 ),
426
427         );
428
429         $tab_tpl = get_markup_template('common_tabs.tpl');
430         $t = replace_macros($tab_tpl, array('$tabs'=>$tabs));
431
432
433
434
435         if($search) {
436                 $search_hdr = $search;
437                 $search = dbesc($search.'*');
438         }
439         $sql_extra .= ((strlen($search)) ? " AND MATCH `name` AGAINST ('$search' IN BOOLEAN MODE) " : "");
440
441         if($nets)
442                 $sql_extra .= sprintf(" AND network = '%s' ", dbesc($nets));
443  
444         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : ''); 
445
446         
447         $r = q("SELECT COUNT(*) AS `total` FROM `contact` 
448                 WHERE `uid` = %d AND `pending` = 0 $sql_extra $sql_extra2 ",
449                 intval($_SESSION['uid']));
450         if(count($r))
451                 $a->set_pager_total($r[0]['total']);
452
453
454
455
456         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `pending` = 0 $sql_extra $sql_extra2 ORDER BY `name` ASC LIMIT %d , %d ",
457                 intval($_SESSION['uid']),
458                 intval($a->pager['start']),
459                 intval($a->pager['itemspage'])
460         );
461
462         $contacts = array();
463
464         if(count($r)) {
465
466                 foreach($r as $rr) {
467                         if($rr['self'])
468                                 continue;
469
470                         switch($rr['rel']) {
471                                 case CONTACT_IS_FRIEND:
472                                         $dir_icon = 'images/lrarrow.gif';
473                                         $alt_text = t('Mutual Friendship');
474                                         break;
475                                 case  CONTACT_IS_FOLLOWER;
476                                         $dir_icon = 'images/larrow.gif';
477                                         $alt_text = t('is a fan of yours');
478                                         break;
479                                 case CONTACT_IS_SHARING;
480                                         $dir_icon = 'images/rarrow.gif';
481                                         $alt_text = t('you are a fan of');
482                                         break;
483                                 default:
484                                         break;
485                         }
486                         if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
487                                 $url = "redir/{$rr['id']}";
488                                 $sparkle = ' class="sparkle" ';
489                         }
490                         else { 
491                                 $url = $rr['url'];
492                                 $sparkle = '';
493                         }
494
495
496                         $contacts[] = array(
497                                 'img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
498                                 'edit_hover' => t('Edit contact'),
499                                 'photo_menu' => contact_photo_menu($rr),
500                                 'id' => $rr['id'],
501                                 'alt_text' => $alt_text,
502                                 'dir_icon' => $dir_icon,
503                                 'thumb' => $rr['thumb'], 
504                                 'name' => $rr['name'],
505                                 'username' => $rr['name'],
506                                 'sparkle' => $sparkle,
507                                 'url' => $url,
508                                 'item' => $rr,
509                         );
510                 }
511
512                 
513
514         }
515         
516         $tpl = get_markup_template("contacts-template.tpl");
517         $o .= replace_macros($tpl,array(
518                 '$header' => t('Contacts') . (($nets) ? ' - ' . network_to_name($nets) : ''),
519                 '$tabs' => $t,
520                 '$total' => $r[0]['total'],
521                 '$search' => $search_hdr,
522                 '$desc' => t('Search your contacts'),
523                 '$finding' => (strlen($search) ? t('Finding: ') . "'" . $search . "'" : ""),
524                 '$submit' => t('Find'),
525                 '$cmd' => $a->cmd,
526                 '$contacts' => $contacts,
527                 '$paginate' => paginate($a),
528
529         )); 
530         
531         return $o;
532 }