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