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