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