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