]> git.mxchange.org Git - friendica.git/blob - mod/contacts.php
7236a200d5a94b090fd76c67cb9bc7098eb29927
[friendica.git] / mod / contacts.php
1 <?php
2
3 require_once('include/Contact.php');
4
5 function contacts_init(&$a) {
6         require_once('include/group.php');
7         if(! x($a->page,'aside'))
8                 $a->page['aside'] = '';
9         $a->page['aside'] .= group_side();
10
11         if($a->config['register_policy'] != REGISTER_CLOSED)
12                 $a->page['aside'] .= '<div class="side-invite-link-wrapper" id="side-invite-link-wrapper" ><a href="invite" class="side-invite-link" id="side-invite-link">' . t("Invite Friends") . '</a></div>';
13
14         $tpl = load_view_file('view/follow.tpl');
15         $a->page['aside'] .= replace_macros($tpl,array(
16                 '$label' => t('Connect/Follow [profile address]'),
17                 '$hint' => t('Example: bob@example.com, http://example.com/barbara'),
18                 '$follow' => t('Follow')
19         ));
20
21 }
22
23 function contacts_post(&$a) {
24         
25         if(! local_user())
26                 return;
27
28         $contact_id = intval($a->argv[1]);
29         if(! $contact_id)
30                 return;
31
32         $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
33                 intval($contact_id),
34                 intval(local_user())
35         );
36
37         if(! count($orig_record)) {
38                 notice( t('Could not access contact record.') . EOL);
39                 goaway($a->get_baseurl() . '/contacts');
40                 return; // NOTREACHED
41         }
42
43         call_hooks('contact_edit_post', $_POST);
44
45         $profile_id = intval($_POST['profile-assign']);
46         if($profile_id) {
47                 $r = q("SELECT `id` FROM `profile` WHERE `id` = %d AND `uid` = %d LIMIT 1",
48                         intval($profile_id),
49                         intval(local_user())
50                 );
51                 if(! count($r)) {
52                         notice( t('Could not locate selected profile.') . EOL);
53                         return;
54                 }
55         }
56
57
58         $priority = intval($_POST['priority']);
59         if($priority == (-1))
60                 
61         if($priority > 5 || $priority < 0)
62                 $priority = 0;
63
64         $rating = intval($_POST['reputation']);
65         if($rating > 5 || $rating < 0)
66                 $rating = 0;
67
68         $reason = notags(trim($_POST['reason']));
69
70         $info = escape_tags(trim($_POST['info']));
71
72         $r = q("UPDATE `contact` SET `profile-id` = %d, `priority` = %d , `rating` = %d, `reason` = '%s', `info` = '%s'
73                 WHERE `id` = %d AND `uid` = %d LIMIT 1",
74                 intval($profile_id),
75                 intval($priority),
76                 intval($rating),
77                 dbesc($reason),
78                 dbesc($info),
79                 intval($contact_id),
80                 intval(local_user())
81         );
82         if($r)
83                 notice( t('Contact updated.') . EOL);
84         else
85                 notice( t('Failed to update contact record.') . EOL);
86         return;
87
88 }
89
90
91
92 function contacts_content(&$a) {
93
94         $sort_type = 0;
95         $o = '';
96         $o .= '<script> $(document).ready(function() { $(\'#nav-contacts-link\').addClass(\'nav-selected\'); });</script>';
97
98         $_SESSION['return_url'] = $a->get_baseurl() . '/' . $a->cmd;
99
100         if(! local_user()) {
101                 notice( t('Permission denied.') . EOL);
102                 return;
103         }
104
105         if($a->argc == 3) {
106
107                 $contact_id = intval($a->argv[1]);
108                 if(! $contact_id)
109                         return;
110
111                 $cmd = $a->argv[2];
112
113                 $orig_record = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
114                         intval($contact_id),
115                         intval(local_user())
116                 );
117
118                 if(! count($orig_record)) {
119                         notice( t('Could not access contact record.') . EOL);
120                         goaway($a->get_baseurl() . '/contacts');
121                         return; // NOTREACHED
122                 }
123
124
125                 if($cmd === 'block') {
126                         $blocked = (($orig_record[0]['blocked']) ? 0 : 1);
127                         $r = q("UPDATE `contact` SET `blocked` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
128                                         intval($blocked),
129                                         intval($contact_id),
130                                         intval(local_user())
131                         );
132                         if($r) {
133                                 notice( t('Contact has been ') . (($blocked) ? t('blocked') : t('unblocked')) . EOL );
134                         }
135                         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
136                         return; // NOTREACHED
137                 }
138
139                 if($cmd === 'ignore') {
140                         $readonly = (($orig_record[0]['readonly']) ? 0 : 1);
141                         $r = q("UPDATE `contact` SET `readonly` = %d WHERE `id` = %d AND `uid` = %d LIMIT 1",
142                                         intval($readonly),
143                                         intval($contact_id),
144                                         intval(local_user())
145                         );
146                         if($r) {
147                                 notice( t('Contact has been ') . (($readonly) ? t('ignored') : t('unignored')) . EOL );
148                         }
149                         goaway($a->get_baseurl() . '/contacts/' . $contact_id);
150                         return; // NOTREACHED
151                 }
152
153                 if($cmd === 'drop') {
154
155                         // create an unfollow slap
156
157                         if($orig_record[0]['network'] === 'stat') {
158                                 $tpl = load_view_file('view/follow_slap.tpl');
159                                 $slap = replace_macros($tpl, array(
160                                         '$name' => $a->user['username'],
161                                         '$profile_page' => $a->get_baseurl() . '/profile/' . $a->user['nickname'],
162                                         '$photo' => $a->contact['photo'],
163                                         '$thumb' => $a->contact['thumb'],
164                                         '$published' => datetime_convert('UTC','UTC', 'now', ATOM_TIME),
165                                         '$item_id' => 'urn:X-dfrn:' . $a->get_hostname() . ':unfollow:' . random_string(),
166                                         '$title' => '',
167                                         '$type' => 'text',
168                                         '$content' => t('stopped following'),
169                                         '$nick' => $a->user['nickname'],
170                                         '$verb' => ACTIVITY_UNFOLLOW,
171                                         '$ostat_follow' => '<as:verb>http://ostatus.org/schema/1.0/unfollow</as:verb>' . "\r\n"
172                                 ));
173
174                                 if((x($orig_record[0],'notify')) && (strlen($orig_record[0]['notify']))) {
175                                         require_once('include/salmon.php');
176                                         slapper($a->user,$orig_record[0]['notify'],$slap);
177                                 }
178                         }
179
180                         contact_remove($contact_id);
181                         notice( t('Contact has been removed.') . EOL );
182                         goaway($a->get_baseurl() . '/contacts');
183                         return; // NOTREACHED
184                 }
185         }
186
187         if(($a->argc == 2) && intval($a->argv[1])) {
188
189                 $contact_id = intval($a->argv[1]);
190                 $r = q("SELECT * FROM `contact` WHERE `uid` = %d and `id` = %d LIMIT 1",
191                         intval(local_user()),
192                         intval($contact_id)
193                 );
194                 if(! count($r)) {
195                         notice( t('Contact not found.') . EOL);
196                         return;
197                 }
198
199                 $tpl = load_view_file('view/contact_head.tpl');
200                 $a->page['htmlhead'] .= replace_macros($tpl, array('$baseurl' => $a->get_baseurl()));
201
202                 require_once('include/contact_selectors.php');
203
204                 $tpl = load_view_file("view/contact_edit.tpl");
205
206                 switch($r[0]['rel']) {
207                         case REL_BUD:
208                                 $dir_icon = 'images/lrarrow.gif';
209                                 $alt_text = t('Mutual Friendship');
210                                 break;
211                         case REL_VIP;
212                                 $dir_icon = 'images/larrow.gif';
213                                 $alt_text = t('is a fan of yours');
214                                 break;
215         
216                         case REL_FAN;
217                                 $dir_icon = 'images/rarrow.gif';
218                                 $alt_text = t('you are a fan of');
219                                 break;
220                         default:
221                                 break;
222                 }
223
224                 if(($r[0]['network'] === 'dfrn') && ($r[0]['rel'])) {
225                         $url = "redir/{$r[0]['id']}";
226                         $sparkle = ' class="sparkle" ';
227                 }
228                 else { 
229                         $url = $r[0]['url'];
230                         $sparkle = '';
231                 }
232
233                 $o .= replace_macros($tpl,array(
234                         '$header' => t('Contact Editor'),
235                         '$visit' => t('Visit $name\'s profile'),
236                         '$blockunblock' => t('Block/Unblock contact'),
237                         '$ignorecont' => t('Ignore contact'),
238                         '$delete' => t('Delete contact'),
239                         '$poll_interval' => contact_poll_interval($r[0]['priority']),
240                         '$lastupdtext' => t('Last updated: '),
241                         '$updpub' => t('Update public posts: '),
242                         '$last_update' => (($r[0]['last-update'] == '0000-00-00 00:00:00') 
243                                 ? t('Never') 
244                                 : datetime_convert('UTC',date_default_timezone_get(),$r[0]['last-update'],'D, j M Y, g:i A')),
245                         '$profile_select' => contact_profile_assign($r[0]['profile-id'],(($r[0]['network'] !== 'dfrn') ? true : false)),
246                         '$contact_id' => $r[0]['id'],
247                         '$block_text' => (($r[0]['blocked']) ? t('Unblock this contact') : t('Block this contact') ),
248                         '$ignore_text' => (($r[0]['readonly']) ? t('Unignore this contact') : t('Ignore this contact') ),
249                         '$insecure' => (($r[0]['network'] === 'dfrn') ? '' : load_view_file('view/insecure_net.tpl')),
250                         '$info' => $r[0]['info'],
251                         '$blocked' => (($r[0]['blocked']) ? '<div id="block-message">' . t('Currently blocked') . '</div>' : ''),
252                         '$ignored' => (($r[0]['readonly']) ? '<div id="ignore-message">' . t('Currently ignored') . '</div>' : ''),
253                         '$rating' => contact_reputation($r[0]['rating']),
254                         '$reason' => $r[0]['reason'],
255                         '$groups' => '', // group_selector(),
256                         '$photo' => $r[0]['photo'],
257                         '$name' => $r[0]['name'],
258                         '$dir_icon' => $dir_icon,
259                         '$alt_text' => $alt_text,
260                         '$sparkle' => $sparkle,
261                         '$url' => $url
262
263                 ));
264
265                 $arr = array('contact' => $r[0],'output' => $o);
266
267                 call_hooks('contact_edit', $arr);
268
269                 return $arr['output'];
270
271         }
272
273
274         if(($a->argc == 2) && ($a->argv[1] === 'all'))
275                 $sql_extra = '';
276         else
277                 $sql_extra = " AND `blocked` = 0 ";
278
279         $search = ((x($_GET,'search')) ? notags(trim($_GET['search'])) : '');
280
281         $tpl = load_view_file("view/contacts-top.tpl");
282         $o .= replace_macros($tpl,array(
283                 '$header' => t('Contacts'),
284                 '$hide_url' => ((strlen($sql_extra)) ? 'contacts/all' : 'contacts' ),
285                 '$hide_text' => ((strlen($sql_extra)) ? t('Show Blocked Connections') : t('Hide Blocked Connections')),
286                 '$search' => $search,
287                 '$finding' => (strlen($search) ? '<h4>' . t('Finding: ') . "'" . $search . "'" . '</h4>' : ""),
288                 '$submit' => t('Find'),
289                 '$cmd' => $a->cmd
290
291
292         )); 
293
294         if($search)
295                 $search = dbesc($search.'*');
296         $sql_extra .= ((strlen($search)) ? " AND MATCH `name` AGAINST ('$search' IN BOOLEAN MODE) " : "");
297
298         $sql_extra2 = ((($sort_type > 0) && ($sort_type <= REL_BUD)) ? sprintf(" AND `rel` = %d ",intval($sort_type)) : ''); 
299
300         
301         $r = q("SELECT COUNT(*) AS `total` FROM `contact` 
302                 WHERE `uid` = %d AND `pending` = 0 $sql_extra $sql_extra2 ",
303                 intval($_SESSION['uid']));
304         if(count($r))
305                 $a->set_pager_total($r[0]['total']);
306
307         $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `pending` = 0 $sql_extra $sql_extra2 ORDER BY `name` ASC LIMIT %d , %d ",
308                 intval($_SESSION['uid']),
309                 intval($a->pager['start']),
310                 intval($a->pager['itemspage'])
311         );
312
313         if(count($r)) {
314
315                 $tpl = load_view_file("view/contact_template.tpl");
316
317                 foreach($r as $rr) {
318                         if($rr['self'])
319                                 continue;
320
321                         switch($rr['rel']) {
322                                 case REL_BUD:
323                                         $dir_icon = 'images/lrarrow.gif';
324                                         $alt_text = t('Mutual Friendship');
325                                         break;
326                                 case  REL_VIP;
327                                         $dir_icon = 'images/larrow.gif';
328                                         $alt_text = t('is a fan of yours');
329                                         break;
330                                 case REL_FAN;
331                                         $dir_icon = 'images/rarrow.gif';
332                                         $alt_text = t('you are a fan of');
333                                         break;
334                                 default:
335                                         break;
336                         }
337                         if(($rr['network'] === 'dfrn') && ($rr['rel'])) {
338                                 $url = "redir/{$rr['id']}";
339                                 $sparkle = ' class="sparkle" ';
340                         }
341                         else { 
342                                 $url = $rr['url'];
343                                 $sparkle = '';
344                         }
345
346
347                         $o .= replace_macros($tpl, array(
348                                 '$img_hover' => t('Visit ') . $rr['name'] . t('\'s profile'),
349                                 '$edit_hover' => t('Edit contact'),
350                                 '$id' => $rr['id'],
351                                 '$alt_text' => $alt_text,
352                                 '$dir_icon' => $dir_icon,
353                                 '$thumb' => $rr['thumb'], 
354                                 '$name' => $rr['name'],
355                                 '$sparkle' => $sparkle,
356                                 '$url' => $url
357                         ));
358                 }
359
360                 $o .= '<div id="contact-edit-end"></div>';
361
362         }
363         $o .= paginate($a);
364         return $o;
365 }