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