]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
"profile locator/address" is now called "Identity Address" to reduce confusion
[friendica.git] / mod / contacts.php
1 <?php
2
3 require_once('include/Contact.php');
4
5 function contacts_init(&$a) {
6         require_once('include/group.php');
7         if(! x($a->page,'aside'))
8                 $a->page['aside'] = '';
9         $a->page['aside'] .= group_side();
10
11         if($a->config['register_policy'] != REGISTER_CLOSED)
12                 $a->page['aside'] .= '<div class="side-link" id="side-invite-link" ><a href="invite" >' . t("Invite Friends") . '</a></div>';
13
14         if(strlen(get_config('system','directory_submit_url')))
15                 $a->page['aside'] .= '<div class="side-link" id="side-match-link"><a href="match" >' . t('Find People With Shared Interests') . '</a></div>';
16
17         $tpl = load_view_file('view/follow.tpl');
18         $a->page['aside'] .= replace_macros($tpl,array(
19                 '$label' => t('Connect/Follow'),
20                 '$hint' => t('Example: bob@example.com, http://example.com/barbara'),
21                 '$follow' => t('Follow')
22         ));
23
24
25
26 }
27
28 function contacts_post(&$a) {
29         
30         if(! local_user())
31                 return;
32
33         $contact_id = intval($a->argv[1]);
34         if(! $contact_id)
35                 return;
36
37         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
38                 intval($contact_id),
39                 intval(local_user())
40         );
41
42         if(! count($orig_record)) {
43                 notice( t('Could not access contact record.') . EOL);
44                 goaway($a->get_baseurl() . '/contacts');
45                 return; // NOTREACHED
46         }
47
48         call_hooks('contact_edit_post', $_POST);
49
50         $profile_id = intval($_POST['profile-assign']);
51         if($profile_id) {
52                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
53                         intval($profile_id),
54                         intval(local_user())
55                 );
56                 if(! count($r)) {
57                         notice( t('Could not locate selected profile.') . EOL);
58                         return;
59                 }
60         }
61 logger('contact_edit ' . print_r($_POST,true));
62
63         $priority = intval($_POST['poll']);
64         if($priority == (-1))
65                 
66         if($priority > 5 || $priority < 0)
67                 $priority = 0;
68
69         $rating = intval($_POST['reputation']);
70         if($rating > 5 || $rating < 0)
71                 $rating = 0;
72
73         $reason = notags(trim($_POST['reason']));
74
75         $info = escape_tags(trim($_POST['info']));
76
77         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `rating` = %d, `reason` = '%s', `info` = '%s'
78                 WHERE `id` = %d AND `uid` = %d LIMIT 1",
79                 intval($profile_id),
80                 intval($priority),
81                 intval($rating),
82                 dbesc($reason),
83                 dbesc($info),
84                 intval($contact_id),
85                 intval(local_user())
86         );
87         if($r)
88                 notice( t('Contact updated.') . EOL);
89         else
90                 notice( t('Failed to update contact record.') . EOL);
91         return;
92
93 }
94
95
96
97 function contacts_content(&$a) {
98
99         $sort_type = 0;
100         $o = '';
101         $o .= '<script> $(document).ready(function() { $(\'#nav-contacts-link\').addClass(\'nav-selected\'); });</script>';
102
103         $_SESSION['return_url'] = $a->get_baseurl() . '/' . $a->cmd;
104
105         if(! local_user()) {
106                 notice( t('Permission denied.') . EOL);
107                 return;
108         }
109
110         if($a->argc == 3) {
111
112                 $contact_id = intval($a->argv[1]);
113                 if(! $contact_id)
114                         return;
115
116                 $cmd = $a->argv[2];
117
118                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
119                         intval($contact_id),
120                         intval(local_user())
121                 );
122
123                 if(! count($orig_record)) {
124                         notice( t('Could not access contact record.') . EOL);
125                         goaway($a->get_baseurl() . '/contacts');
126                         return; // NOTREACHED
127                 }
128
129                 if($cmd === 'update') {
130
131                         // pull feed and consume it, which should subscribe to the hub.
132                         proc_run('php',"include/poller.php","$contact_id");
133                         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
134                         // NOTREACHED
135                 }
136
137                 if($cmd === 'block') {
138                         $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
139                         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
140                                         intval($blocked),
141                                         intval($contact_id),
142                                         intval(local_user())
143                         );
144                         if($r) {
145                                 //notice( t('Contact has been ') . (($blocked) ? t('blocked') : t('unblocked')) . EOL );
146                                 notice( (($blocked) ? t('Contact has been blocked') : t('Contact has been unblocked')) . EOL );
147                         }
148                         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
149                         return; // NOTREACHED
150                 }
151
152                 if($cmd === 'ignore') {
153                         $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
154                         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
155                                         intval($readonly),
156                                         intval($contact_id),
157                                         intval(local_user())
158                         );
159                         if($r) {
160                                 notice( (($readonly) ? t('Contact has been ignored') : t('Contact has been unignored')) . EOL );
161                         }
162                         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
163                         return; // NOTREACHED
164                 }
165
166                 if($cmd === 'drop') {
167
168                         // create an unfollow slap
169
170                         if($orig_record[0]['network'] === 'stat') {
171                                 $tpl = load_view_file('view/follow_slap.tpl');
172                                 $slap = replace_macros($tpl, array(
173                                         '$name' => $a->user['username'],
174                                         '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
175                                         '$photo' => $a->contact['photo'],
176                                         '$thumb' => $a->contact['thumb'],
177                                         '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
178                                         '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':unfollow:' . random_string(),
179                                         '$title' => '',
180                                         '$type' => 'text',
181                                         '$content' => t('stopped following'),
182                                         '$nick' => $a->user['nickname'],
183                                         '$verb' => 'http://ostatus.org/schema/1.0/unfollow', // ACTIVITY_UNFOLLOW,
184                                         '$ostat_follow' => '' // '<as:verb>http://ostatus.org/schema/1.0/unfollow</as:verb>' . "\r\n"
185                                 ));
186
187                                 if((x($orig_record[0],'notify')) && (strlen($orig_record[0]['notify']))) {
188                                         require_once('include/salmon.php');
189                                         slapper($a->user,$orig_record[0]['notify'],$slap);
190                                 }
191                         }
192
193                         if($orig_record[0]['network'] === 'dfrn') {
194                                 require_once('include/items.php');
195                                 dfrn_deliver($a->user,$orig_record[0],'placeholder', 1);
196                         }
197
198
199                         contact_remove($contact_id);
200                         notice( t('Contact has been removed.') . EOL );
201                         goaway($a->get_baseurl() . '/contacts');
202                         return; // NOTREACHED
203                 }
204         }
205
206         if(($a->argc == 2) && intval($a->argv[1])) {
207
208                 $contact_id = intval($a->argv[1]);
209                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
210                         intval(local_user()),
211                         intval($contact_id)
212                 );
213                 if(! count($r)) {
214                         notice( t('Contact not found.') . EOL);
215                         return;
216                 }
217
218                 $tpl = load_view_file('view/contact_head.tpl');
219                 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
220
221                 require_once('include/contact_selectors.php');
222
223                 $tpl = load_view_file("view/contact_edit.tpl");
224
225                 switch($r[0]['rel']) {
226                         case REL_BUD:
227                                 $dir_icon = 'images/lrarrow.gif';
228                                 $alt_text = t('Mutual Friendship');
229                                 break;
230                         case REL_VIP;
231                                 $dir_icon = 'images/larrow.gif';
232                                 $alt_text = t('is a fan of yours');
233                                 break;
234         
235                         case REL_FAN;
236                                 $dir_icon = 'images/rarrow.gif';
237                                 $alt_text = t('you are a fan of');
238                                 break;
239                         default:
240                                 break;
241                 }
242
243                 if(($r[0]['network'] === 'dfrn') && ($r[0]['rel'])) {
244                         $url = "redir/{$r[0]['id']}";
245                         $sparkle = ' class="sparkle" ';
246                 }
247                 else { 
248                         $url = $r[0]['url'];
249                         $sparkle = '';
250                 }
251
252                 $insecure = '<div id="profile-edit-insecure"><p><img src="images/unlock_icon.gif" alt="' . t('Privacy Unavailable') . '" />&nbsp;'
253                         . t('Private communications are not available for this contact.') . '</p></div>';
254
255                 $last_update = (($r[0]['last-update'] == '0000-00-00 00:00:00') 
256                                 ? t('Never') 
257                                 : datetime_convert('UTC',date_default_timezone_get(),$r[0]['last-update'],'D, j M Y, g:i A'));
258
259                 if($r[0]['last-update'] !== '0000-00-00 00:00:00')
260                         $last_update .= ' ' . (($r[0]['last-update'] == $r[0]['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
261
262                 $o .= replace_macros($tpl,array(
263                         '$header' => t('Contact Editor'),
264                         '$submit' => t('Submit'),
265                         '$lbl_vis1' => t('Profile Visibility'),
266                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $r[0]['name']),
267                         '$lbl_info1' => t('Contact Information / Notes'),
268                         '$lbl_rep1' => t('Online Reputation'),
269                         '$lbl_rep2' => t('Occasionally your friends may wish to inquire about this person\'s online legitimacy.'),
270                         '$lbl_rep3' => t('You may help them choose whether or not to interact with this person by providing a <em>reputation</em> to guide them.'),
271                         '$lbl_rep4' => t('Please take a moment to elaborate on this selection if you feel it could be helpful to others.'),
272                         '$visit' => t('Visit $name\'s profile'),
273                         '$blockunblock' => t('Block/Unblock contact'),
274                         '$ignorecont' => t('Ignore contact'),
275                         '$altcrepair' => t('Repair contact URL settings'),
276                         '$lblcrepair' => t("Repair contact URL settings \x28WARNING: Advanced\x29"),
277                         '$delete' => t('Delete contact'),
278                         '$poll_interval' => contact_poll_interval($r[0]['priority']),
279                         '$lastupdtext' => t('Last updated: '),
280                         '$updpub' => t('Update public posts: '),
281                         '$last_update' => $last_update,
282                         '$udnow' => t('Update now'),
283                         '$profile_select' => contact_profile_assign($r[0]['profile-id'],(($r[0]['network'] !== 'dfrn') ? true : false)),
284                         '$contact_id' => $r[0]['id'],
285                         '$block_text' => (($r[0]['blocked']) ? t('Unblock this contact') : t('Block this contact') ),
286                         '$ignore_text' => (($r[0]['readonly']) ? t('Unignore this contact') : t('Ignore this contact') ),
287                         '$insecure' => (($r[0]['network'] !== 'dfrn') ? $insecure : ''),
288                         '$info' => $r[0]['info'],
289                         '$blocked' => (($r[0]['blocked']) ? '<div id="block-message">' . t('Currently blocked') . '</div>' : ''),
290                         '$ignored' => (($r[0]['readonly']) ? '<div id="ignore-message">' . t('Currently ignored') . '</div>' : ''),
291                         '$rating' => contact_reputation($r[0]['rating']),
292                         '$reason' => $r[0]['reason'],
293                         '$groups' => '', // group_selector(),
294                         '$photo' => $r[0]['photo'],
295                         '$name' => $r[0]['name'],
296                         '$dir_icon' => $dir_icon,
297                         '$alt_text' => $alt_text,
298                         '$sparkle' => $sparkle,
299                         '$url' => $url
300
301                 ));
302
303                 $arr = array('contact' => $r[0],'output' => $o);
304
305                 call_hooks('contact_edit', $arr);
306
307                 return $arr['output'];
308
309         }
310
311
312         if(($a->argc == 2) && ($a->argv[1] === 'all'))
313                 $sql_extra = '';
314         else
315                 $sql_extra = " AND `blocked` = 0 ";
316
317         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
318
319         $tpl = load_view_file("view/contacts-top.tpl");
320         $o .= replace_macros($tpl,array(
321                 '$header' => t('Contacts'),
322                 '$hide_url' => ((strlen($sql_extra)) ? 'contacts/all' : 'contacts' ),
323                 '$hide_text' => ((strlen($sql_extra)) ? t('Show Blocked Connections') : t('Hide Blocked Connections')),
324                 '$search' => $search,
325                 '$finding' => (strlen($search) ? '<h4>' . t('Finding: ') . "'" . $search . "'" . '</h4>' : ""),
326                 '$submit' => t('Find'),
327                 '$cmd' => $a->cmd
328
329
330         )); 
331
332         if($search)
333                 $search = dbesc($search.'*');
334         $sql_extra .= ((strlen($search)) ? " AND MATCH `name` AGAINST ('$search' IN BOOLEAN MODE) " : "");
335
336         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= REL_BUD)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : ''); 
337
338         
339         $r = q("SELECT COUNT(*) AS `total` FROM `contact` 
340                 WHERE `uid` = %d AND `pending` = 0 $sql_extra $sql_extra2 ",
341                 intval($_SESSION['uid']));
342         if(count($r))
343                 $a->set_pager_total($r[0]['total']);
344
345         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `pending` = 0 $sql_extra $sql_extra2 ORDER BY `name` ASC LIMIT %d , %d ",
346                 intval($_SESSION['uid']),
347                 intval($a->pager['start']),
348                 intval($a->pager['itemspage'])
349         );
350
351         if(count($r)) {
352
353                 $tpl = load_view_file("view/contact_template.tpl");
354
355                 foreach($r as $rr) {
356                         if($rr['self'])
357                                 continue;
358
359                         switch($rr['rel']) {
360                                 case REL_BUD:
361                                         $dir_icon = 'images/lrarrow.gif';
362                                         $alt_text = t('Mutual Friendship');
363                                         break;
364                                 case  REL_VIP;
365                                         $dir_icon = 'images/larrow.gif';
366                                         $alt_text = t('is a fan of yours');
367                                         break;
368                                 case REL_FAN;
369                                         $dir_icon = 'images/rarrow.gif';
370                                         $alt_text = t('you are a fan of');
371                                         break;
372                                 default:
373                                         break;
374                         }
375                         if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
376                                 $url = "redir/{$rr['id']}";
377                                 $sparkle = ' class="sparkle" ';
378                         }
379                         else { 
380                                 $url = $rr['url'];
381                                 $sparkle = '';
382                         }
383
384
385                         $o .= replace_macros($tpl, array(
386                                 '$img_hover' => t('Visit $username\'s profile'),
387                                 '$edit_hover' => t('Edit contact'),
388                                 '$id' => $rr['id'],
389                                 '$alt_text' => $alt_text,
390                                 '$dir_icon' => $dir_icon,
391                                 '$thumb' => $rr['thumb'], 
392                                 '$name' => substr($rr['name'],0,20),
393                                 '$username' => $rr['name'],
394                                 '$sparkle' => $sparkle,
395                                 '$url' => $url
396                         ));
397                 }
398
399                 $o .= '<div id="contact-edit-end"></div>';
400
401         }
402         $o .= paginate($a);
403         return $o;
404 }