]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
ping returns notices and infos also to unlogged users
[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         $contact_id = 0;
10         if(($a->argc == 2) && intval($a->argv[1])) {
11                 $contact_id = intval($a->argv[1]);
12                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
13                         intval(local_user()),
14                         intval($contact_id)
15                 );
16                 if(! count($r)) {
17                         $contact_id = 0;
18                 }
19         }
20
21         require_once('include/group.php');
22         if(! x($a->page,'aside'))
23                 $a->page['aside'] = '';
24         $a->page['aside'] .= group_side('contacts','group',false,0,$contact_id);
25
26         if(get_config('system','invitation_only')) {
27                 $x = get_pconfig(local_user(),'system','invites_remaining');
28                 if($x || is_site_admin()) {
29                         $a->page['aside'] .= '<div class="side-link" id="side-invite-remain">' 
30                         . sprintf( tt('%d invitation available','%d invitations available',$x), $x) 
31                         . '</div>' . $inv;
32                 }
33         }
34
35         $tpl = get_markup_template('follow.tpl');
36         
37         $findSimilarLink = '<div class="side-link" id="side-match-link"><a href="match" >' 
38                 . t('Similar Interests') . '</a></div>';
39         
40         $inv = '';
41         if($a->config['register_policy'] != REGISTER_CLOSED) {
42                 $inv = '<div class="side-link" id="side-invite-link" ><a href="invite" >' . t("Invite Friends") . '</a></div>';
43         }
44                 
45         $a->page['aside'] .= replace_macros($tpl,array(
46                 '$label' => t('Connect/Follow'),
47                 '$hint' => t('Example: bob@example.com, http://example.com/barbara'),
48                 '$follow' => t('Follow'),
49                 '$findSimilar' => $findSimilarLink,
50                 '$inviteFriends' => $inv
51         ));
52
53         
54
55 }
56
57 function contacts_post(&$a) {
58         
59         if(! local_user())
60                 return;
61
62         $contact_id = intval($a->argv[1]);
63         if(! $contact_id)
64                 return;
65
66         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
67                 intval($contact_id),
68                 intval(local_user())
69         );
70
71         if(! count($orig_record)) {
72                 notice( t('Could not access contact record.') . EOL);
73                 goaway($a->get_baseurl() . '/contacts');
74                 return; // NOTREACHED
75         }
76
77         call_hooks('contact_edit_post', $_POST);
78
79         $profile_id = intval($_POST['profile-assign']);
80         if($profile_id) {
81                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
82                         intval($profile_id),
83                         intval(local_user())
84                 );
85                 if(! count($r)) {
86                         notice( t('Could not locate selected profile.') . EOL);
87                         return;
88                 }
89         }
90
91
92         $priority = intval($_POST['poll']);
93         if($priority > 5 || $priority < 0)
94                 $priority = 0;
95
96         $info = escape_tags(trim($_POST['info']));
97
98         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `info` = '%s'
99                 WHERE `id` = %d AND `uid` = %d LIMIT 1",
100                 intval($profile_id),
101                 intval($priority),
102                 dbesc($info),
103                 intval($contact_id),
104                 intval(local_user())
105         );
106         if($r)
107                 info( t('Contact updated.') . EOL);
108         else
109                 notice( t('Failed to update contact record.') . EOL);
110         return;
111
112 }
113
114
115
116 function contacts_content(&$a) {
117
118         $sort_type = 0;
119         $o = '';
120         nav_set_selected('contacts');
121
122         $_SESSION['return_url'] = $a->get_baseurl() . '/' . $a->cmd;
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                         goaway($a->get_baseurl() . '/contacts');
223                         return; // NOTREACHED
224                 }
225         }
226
227         if(($a->argc == 2) && intval($a->argv[1])) {
228
229                 $contact_id = intval($a->argv[1]);
230                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
231                         intval(local_user()),
232                         intval($contact_id)
233                 );
234                 if(! count($r)) {
235                         notice( t('Contact not found.') . EOL);
236                         return;
237                 }
238
239                 $tpl = get_markup_template('contact_head.tpl');
240                 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
241
242                 require_once('include/contact_selectors.php');
243
244                 $tpl = get_markup_template("contact_edit.tpl");
245
246                 switch($r[0]['rel']) {
247                         case CONTACT_IS_FRIEND:
248                                 $dir_icon = 'images/lrarrow.gif';
249                                 $alt_text = t('Mutual Friendship');
250                                 break;
251                         case CONTACT_IS_FOLLOWER;
252                                 $dir_icon = 'images/larrow.gif';
253                                 $alt_text = t('is a fan of yours');
254                                 break;
255         
256                         case CONTACT_IS_SHARING;
257                                 $dir_icon = 'images/rarrow.gif';
258                                 $alt_text = t('you are a fan of');
259                                 break;
260                         default:
261                                 break;
262                 }
263
264                 if(($r[0]['network'] === 'dfrn') && ($r[0]['rel'])) {
265                         $url = "redir/{$r[0]['id']}";
266                         $sparkle = ' class="sparkle" ';
267                 }
268                 else { 
269                         $url = $r[0]['url'];
270                         $sparkle = '';
271                 }
272
273                 $insecure = '<div id="profile-edit-insecure"><p><img src="images/unlock_icon.gif" alt="' . t('Privacy Unavailable') . '" />&nbsp;'
274                         . t('Private communications are not available for this contact.') . '</p></div>';
275
276                 $last_update = (($r[0]['last-update'] == '0000-00-00 00:00:00') 
277                                 ? t('Never') 
278                                 : datetime_convert('UTC',date_default_timezone_get(),$r[0]['last-update'],'D, j M Y, g:i A'));
279
280                 if($r[0]['last-update'] !== '0000-00-00 00:00:00')
281                         $last_update .= ' ' . (($r[0]['last-update'] == $r[0]['success_update']) ? t("\x28Update was successful\x29") : t("\x28Update was not successful\x29"));
282
283                 $lblsuggest = (($r[0]['network'] === NETWORK_DFRN) 
284                         ? '<div id="contact-suggest-wrapper"><a href="fsuggest/' . $r[0]['id'] . '" id="contact-suggest">' . t('Suggest friends') . '</a></div>' : '');
285
286                 $poll_enabled = (($r[0]['network'] !== NETWORK_DIASPORA) ? true : false);
287
288                 $nettype = '<div id="contact-edit-nettype">' . sprintf( t('Network type: %s'),network_to_name($r[0]['network'])) . '</div>';
289
290                 $o .= replace_macros($tpl,array(
291                         '$header' => t('Contact Editor'),
292                         '$submit' => t('Submit'),
293                         '$lbl_vis1' => t('Profile Visibility'),
294                         '$lbl_vis2' => sprintf( t('Please choose the profile you would like to display to %s when viewing your profile securely.'), $r[0]['name']),
295                         '$lbl_info1' => t('Contact Information / Notes'),
296                         '$lbl_rep1' => t('Online Reputation'),
297                         '$lbl_rep2' => t('Occasionally your friends may wish to inquire about this person\'s online legitimacy.'),
298                         '$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.'),
299                         '$lbl_rep4' => t('Please take a moment to elaborate on this selection if you feel it could be helpful to others.'),
300                         '$visit' => sprintf( t('Visit %s\'s profile [%s]'),$r[0]['name'],$r[0]['url']),
301                         '$blockunblock' => t('Block/Unblock contact'),
302                         '$ignorecont' => t('Ignore contact'),
303                         '$altcrepair' => t('Repair contact URL settings'),
304                         '$lblcrepair' => t("Repair contact URL settings \x28WARNING: Advanced\x29"),
305                         '$lblrecent' => t('View conversations'),
306                         '$lblsuggest' => $lblsuggest,
307                         '$delete' => t('Delete contact'),
308                         '$nettype' => $nettype,
309                         '$poll_interval' => contact_poll_interval($r[0]['priority'],(! $poll_enabled)),
310                         '$poll_enabled' => $poll_enabled,
311                         '$lastupdtext' => t('Last updated: '),
312                         '$updpub' => t('Update public posts: '),
313                         '$last_update' => $last_update,
314                         '$udnow' => t('Update now'),
315                         '$profile_select' => contact_profile_assign($r[0]['profile-id'],(($r[0]['network'] !== 'dfrn') ? true : false)),
316                         '$contact_id' => $r[0]['id'],
317                         '$block_text' => (($r[0]['blocked']) ? t('Unblock this contact') : t('Block this contact') ),
318                         '$ignore_text' => (($r[0]['readonly']) ? t('Unignore this contact') : t('Ignore this contact') ),
319                         '$insecure' => (($r[0]['network'] !== NETWORK_DFRN && $r[0]['network'] !== NETWORK_MAIL && $r[0]['network'] !== NETWORK_FACEBOOK && $r[0]['network'] !== NETWORK_DIASPORA) ? $insecure : ''),
320                         '$info' => $r[0]['info'],
321                         '$blocked' => (($r[0]['blocked']) ? '<div id="block-message">' . t('Currently blocked') . '</div>' : ''),
322                         '$ignored' => (($r[0]['readonly']) ? '<div id="ignore-message">' . t('Currently ignored') . '</div>' : ''),
323                         '$photo' => $r[0]['photo'],
324                         '$name' => $r[0]['name'],
325                         '$dir_icon' => $dir_icon,
326                         '$alt_text' => $alt_text,
327                         '$sparkle' => $sparkle,
328                         '$url' => $url
329
330                 ));
331
332                 $arr = array('contact' => $r[0],'output' => $o);
333
334                 call_hooks('contact_edit', $arr);
335
336                 return $arr['output'];
337
338         }
339
340
341         if(($a->argc == 2) && ($a->argv[1] === 'all'))
342                 $sql_extra = '';
343         else
344                 $sql_extra = " AND `blocked` = 0 ";
345
346         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
347
348         $tpl = get_markup_template("contacts-top.tpl");
349         $o .= replace_macros($tpl,array(
350                 '$header' => t('Contacts'),
351                 '$hide_url' => ((strlen($sql_extra)) ? 'contacts/all' : 'contacts' ),
352                 '$hide_text' => ((strlen($sql_extra)) ? t('Show Blocked Connections') : t('Hide Blocked Connections')),
353                 '$search' => $search,
354                 '$finding' => (strlen($search) ? '<h4>' . t('Finding: ') . "'" . $search . "'" . '</h4>' : ""),
355                 '$submit' => t('Find'),
356                 '$cmd' => $a->cmd
357
358
359         )); 
360
361         if($search)
362                 $search = dbesc($search.'*');
363         $sql_extra .= ((strlen($search)) ? " AND MATCH `name` AGAINST ('$search' IN BOOLEAN MODE) " : "");
364
365         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= CONTACT_IS_FRIEND)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : ''); 
366
367         
368         $r = q("SELECT COUNT(*) AS `total` FROM `contact` 
369                 WHERE `uid` = %d AND `pending` = 0 $sql_extra $sql_extra2 ",
370                 intval($_SESSION['uid']));
371         if(count($r))
372                 $a->set_pager_total($r[0]['total']);
373
374         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `pending` = 0 $sql_extra $sql_extra2 ORDER BY `name` ASC LIMIT %d , %d ",
375                 intval($_SESSION['uid']),
376                 intval($a->pager['start']),
377                 intval($a->pager['itemspage'])
378         );
379
380         if(count($r)) {
381
382                 $tpl = get_markup_template("contact_template.tpl");
383
384                 foreach($r as $rr) {
385                         if($rr['self'])
386                                 continue;
387
388                         switch($rr['rel']) {
389                                 case CONTACT_IS_FRIEND:
390                                         $dir_icon = 'images/lrarrow.gif';
391                                         $alt_text = t('Mutual Friendship');
392                                         break;
393                                 case  CONTACT_IS_FOLLOWER;
394                                         $dir_icon = 'images/larrow.gif';
395                                         $alt_text = t('is a fan of yours');
396                                         break;
397                                 case CONTACT_IS_SHARING;
398                                         $dir_icon = 'images/rarrow.gif';
399                                         $alt_text = t('you are a fan of');
400                                         break;
401                                 default:
402                                         break;
403                         }
404                         if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
405                                 $url = "redir/{$rr['id']}";
406                                 $sparkle = ' class="sparkle" ';
407                         }
408                         else { 
409                                 $url = $rr['url'];
410                                 $sparkle = '';
411                         }
412
413
414                         $o .= replace_macros($tpl, array(
415                                 '$img_hover' => sprintf( t('Visit %s\'s profile [%s]'),$rr['name'],$rr['url']),
416                                 '$edit_hover' => t('Edit contact'),
417                                 '$contact_photo_menu' => contact_photo_menu($rr),
418                                 '$id' => $rr['id'],
419                                 '$alt_text' => $alt_text,
420                                 '$dir_icon' => $dir_icon,
421                                 '$thumb' => $rr['thumb'], 
422                                 '$name' => $rr['name'],
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 }