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