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