]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
Merge branch 'pull'
[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 }
46
47 function contacts_post(&$a) {
48         
49         if(! local_user())
50                 return;
51
52         $contact_id = intval($a->argv[1]);
53         if(! $contact_id)
54                 return;
55
56         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
57                 intval($contact_id),
58                 intval(local_user())
59         );
60
61         if(! count($orig_record)) {
62                 notice( t('Could not access contact record.') . EOL);
63                 goaway($a->get_baseurl() . '/contacts');
64                 return; // NOTREACHED
65         }
66
67         call_hooks('contact_edit_post', $_POST);
68
69         $profile_id = intval($_POST['profile-assign']);
70         if($profile_id) {
71                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
72                         intval($profile_id),
73                         intval(local_user())
74                 );
75                 if(! count($r)) {
76                         notice( t('Could not locate selected profile.') . EOL);
77                         return;
78                 }
79         }
80
81         $hidden = intval($_POST['hidden']);
82
83         $priority = intval($_POST['poll']);
84         if($priority > 5 || $priority < 0)
85                 $priority = 0;
86
87         $info = escape_tags(trim($_POST['info']));
88
89         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s',
90                 `hidden` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
91                 intval($profile_id),
92                 intval($priority),
93                 dbesc($info),
94                 intval($hidden),
95                 intval($contact_id),
96                 intval(local_user())
97         );
98         if($r)
99                 info( t('Contact updated.') . EOL);
100         else
101                 notice( t('Failed to update contact record.') . EOL);
102         return;
103
104 }
105
106
107
108 function contacts_content(&$a) {
109
110         $sort_type = 0;
111         $o = '';
112         nav_set_selected('contacts');
113
114
115         if(! local_user()) {
116                 notice( t('Permission denied.') . EOL);
117                 return;
118         }
119
120         if($a->argc == 3) {
121
122                 $contact_id = intval($a->argv[1]);
123                 if(! $contact_id)
124                         return;
125
126                 $cmd = $a->argv[2];
127
128                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d AND `self` = 0 LIMIT 1",
129                         intval($contact_id),
130                         intval(local_user())
131                 );
132
133                 if(! count($orig_record)) {
134                         notice( t('Could not access contact record.') . EOL);
135                         goaway($a->get_baseurl() . '/contacts');
136                         return; // NOTREACHED
137                 }
138
139                 if($cmd === 'update') {
140
141                         // pull feed and consume it, which should subscribe to the hub.
142                         proc_run('php',"include/poller.php","$contact_id");
143                         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
144                         // NOTREACHED
145                 }
146
147                 if($cmd === 'block') {
148                         $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
149                         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
150                                 intval($blocked),
151                                 intval($contact_id),
152                                 intval(local_user())
153                         );
154                         if($r) {
155                                 //notice( t('Contact has been ') . (($blocked) ? t('blocked') : t('unblocked')) . EOL );
156                                 info( (($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')) . EOL );
157                         }
158                         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
159                         return; // NOTREACHED
160                 }
161
162                 if($cmd === 'ignore') {
163                         $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
164                         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
165                                 intval($readonly),
166                                 intval($contact_id),
167                                 intval(local_user())
168                         );
169                         if($r) {
170                                 info( (($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')) . EOL );
171                         }
172                         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
173                         return; // NOTREACHED
174                 }
175
176                 if($cmd === 'drop') {
177
178                         // create an unfollow slap
179
180                         if($orig_record[0]['network'] === NETWORK_OSTATUS) {
181                                 $tpl = get_markup_template('follow_slap.tpl');
182                                 $slap = replace_macros($tpl, array(
183                                         '$name' => $a->user['username'],
184                                         '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
185                                         '$photo' => $a->contact['photo'],
186                                         '$thumb' => $a->contact['thumb'],
187                                         '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
188                                         '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':unfollow:' . random_string(),
189                                         '$title' => '',
190                                         '$type' => 'text',
191                                         '$content' => t('stopped following'),
192                                         '$nick' => $a->user['nickname'],
193                                         '$verb' => 'http://ostatus.org/schema/1.0/unfollow', // ACTIVITY_UNFOLLOW,
194                                         '$ostat_follow' => '' // '<as:verb>http://ostatus.org/schema/1.0/unfollow</as:verb>' . "\r\n"
195                                 ));
196
197                                 if((x($orig_record[0],'notify')) && (strlen($orig_record[0]['notify']))) {
198                                         require_once('include/salmon.php');
199                                         slapper($a->user,$orig_record[0]['notify'],$slap);
200                                 }
201                         }
202                         elseif($orig_record[0]['network'] === NETWORK_DIASPORA) {
203                                 require_once('include/diaspora.php');
204                                 diaspora_unshare($a->user,$orig_record[0]);
205                         }
206                         elseif($orig_record[0]['network'] === NETWORK_DFRN) {
207                                 require_once('include/items.php');
208                                 dfrn_deliver($a->user,$orig_record[0],'placeholder', 1);
209                         }
210
211                         contact_remove($orig_record[0]['id']);
212                         info( t('Contact has been removed.') . EOL );
213                         if(x($_SESSION,'return_url'))
214                                 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
215                         else
216                                 goaway($a->get_baseurl() . '/contacts');
217                         return; // NOTREACHED
218                 }
219         }
220
221         if((x($a->data,'contact')) && (is_array($a->data['contact']))) {
222
223                 $contact_id = $a->data['contact']['id'];
224                 $contact = $a->data['contact'];
225
226                 $tpl = get_markup_template('contact_head.tpl');
227                 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
228
229                 require_once('include/contact_selectors.php');
230
231                 $tpl = get_markup_template("contact_edit.tpl");
232
233                 switch($contact['rel']) {
234                         case CONTACT_IS_FRIEND:
235                                 $dir_icon = 'images/lrarrow.gif';
236                                 $relation_text = t('You are mutual friends with %s');
237                                 break;
238                         case CONTACT_IS_FOLLOWER;
239                                 $dir_icon = 'images/larrow.gif';
240                                 $relation_text = t('You are sharing with %s');
241                                 break;
242         
243                         case CONTACT_IS_SHARING;
244                                 $dir_icon = 'images/rarrow.gif';
245                                 $relation_text = t('%s is sharing with you');
246                                 break;
247                         default:
248                                 break;
249                 }
250
251                 $relation_text = sprintf($relation_text,$contact['name']);
252
253                 if(($contact['network'] === NETWORK_DFRN) && ($contact['rel'])) {
254                         $url = "redir/{$contact['id']}";
255                         $sparkle = ' class="sparkle" ';
256                 }
257                 else { 
258                         $url = $contact['url'];
259                         $sparkle = '';
260                 }
261
262                 $insecure = t('Private communications are not available for this contact.');
263
264                 $last_update = (($contact['last-update'] == '0000-00-00 00:00:00') 
265                                 ? t('Never') 
266                                 : datetime_convert('UTC',date_default_timezone_get(),$contact['last-update'],'D, j M Y, g:i A'));
267
268                 if($contact['last-update'] !== '0000-00-00 00:00:00')
269                         $last_update .= ' ' . (($contact['last-update'] == $contact['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
270
271                 $lblsuggest = (($contact['network'] === NETWORK_DFRN) ? t('Suggest friends') : '');
272
273                 $poll_enabled = (($contact['network'] !== NETWORK_DIASPORA) ? true : false);
274
275                 $nettype = sprintf( t('Network type: %s'),network_to_name($contact['network']));
276
277                 $common = count_common_friends(local_user(),$contact['id']);
278                 $common_text = (($common) ? sprintf( tt('%d contact in common','%d contacts in common', $common),$common) : '');
279
280                 $polling = (($contact['network'] === NETWORK_MAIL | $contact['network'] === NETWORK_FEED) ? 'polling' : ''); 
281
282                 $x = count_all_friends(local_user(), $contact['id']);
283                 $all_friends = (($x) ? t('View all contacts') : '');
284
285                 // tabs
286                 $tabs = array(
287                         array(
288                                 'label' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
289                                 'url'   => $a->get_baseurl() . '/contacts/' . $contact_id . '/block',
290                                 'sel'   => '',
291                         ),
292                         array(
293                                 'label' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
294                                 'url'   => $a->get_baseurl() . '/contacts/' . $contact_id . '/ignore',
295                                 'sel'   => '',
296                         ),
297                         array(
298                                 'label' => t('Repair'),
299                                 'url'   => $a->get_baseurl() . '/crepair/' . $contact_id,
300                                 'sel'   => '',
301                         )
302                 );
303                 $tab_tpl = get_markup_template('common_tabs.tpl');
304                 $tab_str = replace_macros($tab_tpl, array('$tabs' => $tabs));
305
306
307                 $o .= replace_macros($tpl,array(
308                         '$header' => t('Contact Editor'),
309                         '$tab_str' => $tab_str,
310                         '$submit' => t('Submit'),
311                         '$lbl_vis1' => t('Profile Visibility'),
312                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $contact['name']),
313                         '$lbl_info1' => t('Contact Information / Notes'),
314                         '$infedit' => t('Edit contact notes'),
315                         '$common_text' => $common_text,
316                         '$common_link' => $a->get_baseurl() . '/common/' . $contact['id'],
317                         '$all_friends' => $all_friends,
318                         '$relation_text' => $relation_text,
319                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$contact['name'],$contact['url']),
320                         '$blockunblock' => t('Block/Unblock contact'),
321                         '$ignorecont' => t('Ignore contact'),
322                         '$lblcrepair' => t("Repair URL settings"),
323                         '$lblrecent' => t('View conversations'),
324                         '$lblsuggest' => $lblsuggest,
325                         '$delete' => t('Delete contact'),
326                         '$nettype' => $nettype,
327                         '$poll_interval' => contact_poll_interval($contact['priority'],(! $poll_enabled)),
328                         '$poll_enabled' => $poll_enabled,
329                         '$lastupdtext' => t('Last update:'),
330                         '$updpub' => t('Update public posts'),
331                         '$last_update' => $last_update,
332                         '$udnow' => t('Update now'),
333                         '$profile_select' => contact_profile_assign($contact['profile-id'],(($contact['network'] !== NETWORK_DFRN) ? true : false)),
334                         '$contact_id' => $contact['id'],
335                         '$block_text' => (($contact['blocked']) ? t('Unblock') : t('Block') ),
336                         '$ignore_text' => (($contact['readonly']) ? t('Unignore') : t('Ignore') ),
337                         '$insecure' => (($contact['network'] !== NETWORK_DFRN && $contact['network'] !== NETWORK_MAIL && $contact['network'] !== NETWORK_FACEBOOK && $contact['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
338                         '$info' => $contact['info'],
339                         '$blocked' => (($contact['blocked']) ? t('Currently blocked') : ''),
340                         '$ignored' => (($contact['readonly']) ? t('Currently ignored') : ''),
341                         '$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')),
342                         '$photo' => $contact['photo'],
343                         '$name' => $contact['name'],
344                         '$dir_icon' => $dir_icon,
345                         '$alt_text' => $alt_text,
346                         '$sparkle' => $sparkle,
347                         '$url' => $url
348
349                 ));
350
351                 $arr = array('contact' => $contact,'output' => $o);
352
353                 call_hooks('contact_edit', $arr);
354
355                 return $arr['output'];
356
357         }
358
359         $_SESSION['return_url'] = $a->query_string;
360
361         if(($a->argc == 2) && ($a->argv[1] === 'all'))
362                 $sql_extra = '';
363         else
364                 $sql_extra = " AND `blocked` = 0 ";
365
366         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
367
368         $tpl = get_markup_template("contacts-top.tpl");
369         $o .= replace_macros($tpl,array(
370                 '$header' => t('Contacts'),
371                 '$hide_url' => ((strlen($sql_extra)) ? 'contacts/all' : 'contacts' ),
372                 '$hide_text' => ((strlen($sql_extra)) ? t('Show Blocked Connections') : t('Hide Blocked Connections')),
373                 '$search' => $search,
374                 '$desc' => t('Search your contacts'),
375                 '$finding' => (strlen($search) ? '<h4>' . t('Finding: ') . "'" . $search . "'" . '</h4>' : ""),
376                 '$submit' => t('Find'),
377                 '$cmd' => $a->cmd
378
379
380         )); 
381
382         if($search)
383                 $search = dbesc($search.'*');
384         $sql_extra .= ((strlen($search)) ? " AND MATCH `name` AGAINST ('$search' IN BOOLEAN MODE) " : "");
385
386         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : ''); 
387
388         
389         $r = q("SELECT COUNT(*) AS `total` FROM `contact` 
390                 WHERE `uid` = %d AND `pending` = 0 $sql_extra $sql_extra2 ",
391                 intval($_SESSION['uid']));
392         if(count($r))
393                 $a->set_pager_total($r[0]['total']);
394
395         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `pending` = 0 $sql_extra $sql_extra2 ORDER BY `name` ASC LIMIT %d , %d ",
396                 intval($_SESSION['uid']),
397                 intval($a->pager['start']),
398                 intval($a->pager['itemspage'])
399         );
400
401         if(count($r)) {
402
403                 $tpl = get_markup_template("contact_template.tpl");
404
405                 foreach($r as $rr) {
406                         if($rr['self'])
407                                 continue;
408
409                         switch($rr['rel']) {
410                                 case CONTACT_IS_FRIEND:
411                                         $dir_icon = 'images/lrarrow.gif';
412                                         $alt_text = t('Mutual Friendship');
413                                         break;
414                                 case  CONTACT_IS_FOLLOWER;
415                                         $dir_icon = 'images/larrow.gif';
416                                         $alt_text = t('is a fan of yours');
417                                         break;
418                                 case CONTACT_IS_SHARING;
419                                         $dir_icon = 'images/rarrow.gif';
420                                         $alt_text = t('you are a fan of');
421                                         break;
422                                 default:
423                                         break;
424                         }
425                         if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
426                                 $url = "redir/{$rr['id']}";
427                                 $sparkle = ' class="sparkle" ';
428                         }
429                         else { 
430                                 $url = $rr['url'];
431                                 $sparkle = '';
432                         }
433
434
435                         $o .= replace_macros($tpl, array(
436                                 '$img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
437                                 '$edit_hover' => t('Edit contact'),
438                                 '$contact_photo_menu' => contact_photo_menu($rr),
439                                 '$id' => $rr['id'],
440                                 '$alt_text' => $alt_text,
441                                 '$dir_icon' => $dir_icon,
442                                 '$thumb' => $rr['thumb'], 
443                                 '$name' => $rr['name'],
444                                 '$username' => $rr['name'],
445                                 '$sparkle' => $sparkle,
446                                 '$url' => $url
447                         ));
448                 }
449
450                 $o .= '<div id="contact-edit-end"></div>';
451
452         }
453         $o .= paginate($a);
454         return $o;
455 }