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