]> git.mxchange.org Git - friendica.git/blob - include/Contact.php
added much more curly braces + space between "if" and brace
[friendica.git] / include / Contact.php
1 <?php
2
3
4 // Included here for completeness, but this is a very dangerous operation.
5 // It is the caller's responsibility to confirm the requestor's intent and
6 // authorisation to do this.
7
8 function user_remove($uid) {
9         if(! $uid)
10                 return;
11         logger('Removing user: ' . $uid);
12
13         $r = q("select * from user where uid = %d limit 1", intval($uid));
14
15         call_hooks('remove_user',$r[0]);
16
17         // save username (actually the nickname as it is guaranteed
18         // unique), so it cannot be re-registered in the future.
19
20         q("insert into userd ( username ) values ( '%s' )",
21                 $r[0]['nickname']
22         );
23
24         /// @todo Should be done in a background job since this likely will run into a time out
25         // don't delete yet, will be done later when contacts have deleted my stuff
26         // q("DELETE FROM `contact` WHERE `uid` = %d", intval($uid));
27         q("DELETE FROM `gcign` WHERE `uid` = %d", intval($uid));
28         q("DELETE FROM `group` WHERE `uid` = %d", intval($uid));
29         q("DELETE FROM `group_member` WHERE `uid` = %d", intval($uid));
30         q("DELETE FROM `intro` WHERE `uid` = %d", intval($uid));
31         q("DELETE FROM `event` WHERE `uid` = %d", intval($uid));
32         q("DELETE FROM `item` WHERE `uid` = %d", intval($uid));
33         q("DELETE FROM `item_id` WHERE `uid` = %d", intval($uid));
34         q("DELETE FROM `mail` WHERE `uid` = %d", intval($uid));
35         q("DELETE FROM `mailacct` WHERE `uid` = %d", intval($uid));
36         q("DELETE FROM `manage` WHERE `uid` = %d", intval($uid));
37         q("DELETE FROM `notify` WHERE `uid` = %d", intval($uid));
38         q("DELETE FROM `photo` WHERE `uid` = %d", intval($uid));
39         q("DELETE FROM `attach` WHERE `uid` = %d", intval($uid));
40         q("DELETE FROM `profile` WHERE `uid` = %d", intval($uid));
41         q("DELETE FROM `profile_check` WHERE `uid` = %d", intval($uid));
42         q("DELETE FROM `pconfig` WHERE `uid` = %d", intval($uid));
43         q("DELETE FROM `search` WHERE `uid` = %d", intval($uid));
44         q("DELETE FROM `spam` WHERE `uid` = %d", intval($uid));
45         // don't delete yet, will be done later when contacts have deleted my stuff
46         // q("DELETE FROM `user` WHERE `uid` = %d", intval($uid));
47         q("UPDATE `user` SET `account_removed` = 1, `account_expires_on` = UTC_TIMESTAMP() WHERE `uid` = %d", intval($uid));
48         proc_run(PRIORITY_HIGH, "include/notifier.php", "removeme", $uid);
49
50         // Send an update to the directory
51         proc_run(PRIORITY_LOW, "include/directory.php", $r[0]['url']);
52
53         if($uid == local_user()) {
54                 unset($_SESSION['authenticated']);
55                 unset($_SESSION['uid']);
56                 goaway(App::get_baseurl());
57         }
58 }
59
60
61 function contact_remove($id) {
62
63         $r = q("select uid from contact where id = %d limit 1",
64                 intval($id)
65         );
66         if((! dbm::is_result($r)) || (! intval($r[0]['uid'])))
67                 return;
68
69         $archive = get_pconfig($r[0]['uid'], 'system','archive_removed_contacts');
70         if($archive) {
71                 q("update contact set `archive` = 1, `network` = 'none', `writable` = 0 where id = %d",
72                         intval($id)
73                 );
74                 return;
75         }
76
77         q("DELETE FROM `contact` WHERE `id` = %d", intval($id));
78
79         // Delete the rest in the background
80         proc_run(PRIORITY_LOW, 'include/remove_contact.php', $id);
81 }
82
83
84 // sends an unfriend message. Does not remove the contact
85
86 function terminate_friendship($user,$self,$contact) {
87
88         $a = get_app();
89
90         require_once('include/datetime.php');
91
92         if($contact['network'] === NETWORK_OSTATUS) {
93
94                 require_once('include/ostatus.php');
95
96                 // create an unfollow slap
97                 $item = array();
98                 $item['verb'] = NAMESPACE_OSTATUS."/unfollow";
99                 $item['follow'] = $contact["url"];
100                 $slap = ostatus::salmon($item, $user);
101
102                 if((x($contact,'notify')) && (strlen($contact['notify']))) {
103                         require_once('include/salmon.php');
104                         slapper($user,$contact['notify'],$slap);
105                 }
106         }
107         elseif($contact['network'] === NETWORK_DIASPORA) {
108                 require_once('include/diaspora.php');
109                 diaspora::send_unshare($user,$contact);
110         }
111         elseif($contact['network'] === NETWORK_DFRN) {
112                 require_once('include/dfrn.php');
113                 dfrn::deliver($user,$contact,'placeholder', 1);
114         }
115
116 }
117
118
119 // Contact has refused to recognise us as a friend. We will start a countdown.
120 // If they still don't recognise us in 32 days, the relationship is over,
121 // and we won't waste any more time trying to communicate with them.
122 // This provides for the possibility that their database is temporarily messed
123 // up or some other transient event and that there's a possibility we could recover from it.
124
125 function mark_for_death($contact) {
126
127         if($contact['archive'])
128                 return;
129
130         if($contact['term-date'] == '0000-00-00 00:00:00') {
131                 q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
132                                 dbesc(datetime_convert()),
133                                 intval($contact['id'])
134                 );
135
136                 if ($contact['url'] != '') {
137                         q("UPDATE `contact` SET `term-date` = '%s'
138                                 WHERE `nurl` = '%s' AND `term-date` <= '1000-00-00'",
139                                         dbesc(datetime_convert()),
140                                         dbesc(normalise_link($contact['url']))
141                         );
142                 }
143         } else {
144
145                 /// @todo
146                 /// We really should send a notification to the owner after 2-3 weeks
147                 /// so they won't be surprised when the contact vanishes and can take
148                 /// remedial action if this was a serious mistake or glitch
149
150                 /// @todo
151                 /// Check for contact vitality via probing
152
153                 $expiry = $contact['term-date'] . ' + 32 days ';
154                 if(datetime_convert() > datetime_convert('UTC','UTC',$expiry)) {
155
156                         // relationship is really truly dead.
157                         // archive them rather than delete
158                         // though if the owner tries to unarchive them we'll start the whole process over again
159
160                         q("UPDATE `contact` SET `archive` = 1 WHERE `id` = %d",
161                                 intval($contact['id'])
162                         );
163
164                         if ($contact['url'] != '') {
165                                 q("UPDATE `contact` SET `archive` = 1 WHERE `nurl` = '%s'",
166                                         dbesc(normalise_link($contact['url']))
167                                 );
168                         }
169                 }
170         }
171
172 }
173
174 function unmark_for_death($contact) {
175
176         $r = q("SELECT `term-date` FROM `contact` WHERE `id` = %d AND `term-date` > '%s'",
177                 intval($contact['id']),
178                 dbesc('1000-00-00 00:00:00')
179         );
180
181         // We don't need to update, we never marked this contact as dead
182         if (!dbm::is_result($r)) {
183                 return;
184         }
185
186         // It's a miracle. Our dead contact has inexplicably come back to life.
187         q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
188                 dbesc('0000-00-00 00:00:00'),
189                 intval($contact['id'])
190         );
191
192         if ($contact['url'] != '') {
193                 q("UPDATE `contact` SET `term-date` = '%s' WHERE `nurl` = '%s'",
194                         dbesc('0000-00-00 00:00:00'),
195                         dbesc(normalise_link($contact['url']))
196                 );
197         }
198 }
199
200 /**
201  * @brief Get contact data for a given profile link
202  *
203  * The function looks at several places (contact table and gcontact table) for the contact
204  * It caches its result for the same script execution to prevent duplicate calls
205  *
206  * @param string $url The profile link
207  * @param int $uid User id
208  * @param array $default If not data was found take this data as default value
209  *
210  * @return array Contact data
211  */
212 function get_contact_details_by_url($url, $uid = -1, $default = array()) {
213         static $cache = array();
214
215         if ($uid == -1) {
216                 $uid = local_user();
217         }
218
219         if (isset($cache[$url][$uid])) {
220                 return $cache[$url][$uid];
221         }
222
223         // Fetch contact data from the contact table for the given user
224         $r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
225                         `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
226                 FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d",
227                         dbesc(normalise_link($url)), intval($uid));
228
229         // Fetch the data from the contact table with "uid=0" (which is filled automatically)
230         if (!dbm::is_result($r))
231                 $r = q("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
232                         `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
233                         FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0",
234                                 dbesc(normalise_link($url)));
235
236         // Fetch the data from the gcontact table
237         if (!dbm::is_result($r))
238                 $r = q("SELECT 0 AS `id`, 0 AS `cid`, `id` AS `gid`, 0 AS `zid`, 0 AS `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, '' AS `xmpp`,
239                         `keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `contact-type`, `birthday`, 0 AS `self`
240                         FROM `gcontact` WHERE `nurl` = '%s'",
241                                 dbesc(normalise_link($url)));
242
243         if (dbm::is_result($r)) {
244                 // If there is more than one entry we filter out the connector networks
245                 if (count($r) > 1) {
246                         foreach ($r AS $id => $result) {
247                                 if ($result["network"] == NETWORK_STATUSNET) {
248                                         unset($r[$id]);
249                                 }
250                         }
251                 }
252
253                 $profile = array_shift($r);
254
255                 // "bd" always contains the upcoming birthday of a contact.
256                 // "birthday" might contain the birthday including the year of birth.
257                 if ($profile["birthday"] != "0000-00-00") {
258                         $bd_timestamp = strtotime($profile["birthday"]);
259                         $month = date("m", $bd_timestamp);
260                         $day = date("d", $bd_timestamp);
261
262                         $current_timestamp = time();
263                         $current_year = date("Y", $current_timestamp);
264                         $current_month = date("m", $current_timestamp);
265                         $current_day = date("d", $current_timestamp);
266
267                         $profile["bd"] = $current_year."-".$month."-".$day;
268                         $current = $current_year."-".$current_month."-".$current_day;
269
270                         if ($profile["bd"] < $current) {
271                                 $profile["bd"] = (++$current_year)."-".$month."-".$day;
272                         }
273                 } else {
274                         $profile["bd"] = "0000-00-00";
275                 }
276         } else {
277                 $profile = $default;
278         }
279
280         if (($profile["photo"] == "") AND isset($default["photo"])) {
281                 $profile["photo"] = $default["photo"];
282         }
283
284         if (($profile["name"] == "") AND isset($default["name"])) {
285                 $profile["name"] = $default["name"];
286         }
287
288         if (($profile["network"] == "") AND isset($default["network"])) {
289                 $profile["network"] = $default["network"];
290         }
291
292         if (($profile["thumb"] == "") AND isset($profile["photo"])) {
293                 $profile["thumb"] = $profile["photo"];
294         }
295
296         if (($profile["micro"] == "") AND isset($profile["thumb"])) {
297                 $profile["micro"] = $profile["thumb"];
298         }
299
300         if ((($profile["addr"] == "") OR ($profile["name"] == "")) AND ($profile["gid"] != 0) AND
301                 in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) {
302                 proc_run(PRIORITY_LOW, "include/update_gcontact.php", $profile["gid"]);
303         }
304
305         // Show contact details of Diaspora contacts only if connected
306         if (($profile["cid"] == 0) AND ($profile["network"] == NETWORK_DIASPORA)) {
307                 $profile["location"] = "";
308                 $profile["about"] = "";
309                 $profile["gender"] = "";
310                 $profile["birthday"] = "0000-00-00";
311         }
312
313         $cache[$url][$uid] = $profile;
314
315         return $profile;
316 }
317
318 if (! function_exists('contact_photo_menu')) {
319 function contact_photo_menu($contact, $uid = 0)
320 {
321         $a = get_app();
322
323         $contact_url = '';
324         $pm_url = '';
325         $status_link = '';
326         $photos_link = '';
327         $posts_link = '';
328         $contact_drop_link = '';
329         $poke_link = '';
330
331         if ($uid == 0) {
332                 $uid = local_user();
333         }
334
335         if ($contact['uid'] != $uid) {
336                 if ($uid == 0) {
337                         $profile_link = zrl($contact['url']);
338                         $menu = Array('profile' => array(t('View Profile'), $profile_link, true));
339
340                         return $menu;
341                 }
342
343                 $r = q("SELECT * FROM `contact` WHERE `nurl` = '%s' AND `network` = '%s' AND `uid` = %d",
344                         dbesc($contact['nurl']), dbesc($contact['network']), intval($uid));
345                 if ($r) {
346                         return contact_photo_menu($r[0], $uid);
347                 } else {
348                         $profile_link = zrl($contact['url']);
349                         $connlnk = 'follow/?url='.$contact['url'];
350                         $menu = array(
351                                 'profile' => array(t('View Profile'), $profile_link, true),
352                                 'follow' => array(t('Connect/Follow'), $connlnk, true)
353                         );
354
355                         return $menu;
356                 }
357         }
358
359         $sparkle = false;
360         if ($contact['network'] === NETWORK_DFRN) {
361                 $sparkle = true;
362                 $profile_link = App::get_baseurl() . '/redir/' . $contact['id'];
363         } else {
364                 $profile_link = $contact['url'];
365         }
366
367         if ($profile_link === 'mailbox') {
368                 $profile_link = '';
369         }
370
371         if ($sparkle) {
372                 $status_link = $profile_link . '?url=status';
373                 $photos_link = $profile_link . '?url=photos';
374                 $profile_link = $profile_link . '?url=profile';
375         }
376
377         if (in_array($contact['network'], array(NETWORK_DFRN, NETWORK_DIASPORA))) {
378                 $pm_url = App::get_baseurl() . '/message/new/' . $contact['id'];
379         }
380
381         if ($contact['network'] == NETWORK_DFRN) {
382                 $poke_link = App::get_baseurl() . '/poke/?f=&c=' . $contact['id'];
383         }
384
385         $contact_url = App::get_baseurl() . '/contacts/' . $contact['id'];
386
387         $posts_link = App::get_baseurl() . '/contacts/' . $contact['id'] . '/posts';
388         $contact_drop_link = App::get_baseurl() . '/contacts/' . $contact['id'] . '/drop?confirm=1';
389
390         /**
391          * menu array:
392          * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
393          */
394         $menu = array(
395                 'status' => array(t("View Status"), $status_link, true),
396                 'profile' => array(t("View Profile"), $profile_link, true),
397                 'photos' => array(t("View Photos"), $photos_link, true),
398                 'network' => array(t("Network Posts"), $posts_link, false),
399                 'edit' => array(t("View Contact"), $contact_url, false),
400                 'drop' => array(t("Drop Contact"), $contact_drop_link, false),
401                 'pm' => array(t("Send PM"), $pm_url, false),
402                 'poke' => array(t("Poke"), $poke_link, false),
403         );
404
405
406         $args = array('contact' => $contact, 'menu' => &$menu);
407
408         call_hooks('contact_photo_menu', $args);
409
410         $menucondensed = array();
411
412         foreach ($menu AS $menuname => $menuitem) {
413                 if ($menuitem[1] != '') {
414                         $menucondensed[$menuname] = $menuitem;
415                 }
416         }
417
418         return $menucondensed;
419 }}
420
421
422 function random_profile() {
423         $r = q("SELECT `url` FROM `gcontact` WHERE `network` = '%s'
424                                 AND `last_contact` >= `last_failure`
425                                 AND `updated` > UTC_TIMESTAMP - INTERVAL 1 MONTH
426                         ORDER BY rand() LIMIT 1",
427                 dbesc(NETWORK_DFRN));
428
429         if (dbm::is_result($r))
430                 return dirname($r[0]['url']);
431         return '';
432 }
433
434
435 function contacts_not_grouped($uid,$start = 0,$count = 0) {
436
437         if(! $count) {
438                 $r = q("select count(*) as total from contact where uid = %d and self = 0 and id not in (select distinct(`contact-id`) from group_member where uid = %d) ",
439                         intval($uid),
440                         intval($uid)
441                 );
442
443                 return $r;
444
445
446         }
447
448         $r = q("select * from contact where uid = %d and self = 0 and id not in (select distinct(`contact-id`) from group_member where uid = %d) and blocked = 0 and pending = 0 limit %d, %d",
449                 intval($uid),
450                 intval($uid),
451                 intval($start),
452                 intval($count)
453         );
454
455         return $r;
456 }
457
458 /**
459  * @brief Fetch the contact id for a given url and user
460  *
461  * @param string $url Contact URL
462  * @param integer $uid The user id for the contact
463  * @param boolean $no_update Don't update the contact
464  *
465  * @return integer Contact ID
466  */
467 function get_contact($url, $uid = 0, $no_update = false) {
468         require_once("include/Scrape.php");
469
470         logger("Get contact data for url ".$url." and user ".$uid." - ".App::callstack(), LOGGER_DEBUG);;
471
472         $data = array();
473         $contactid = 0;
474
475         // is it an address in the format user@server.tld?
476         /// @todo use gcontact and/or the addr field for a lookup
477         if (!strstr($url, "http") OR strstr($url, "@")) {
478                 $data = probe_url($url);
479                 $url = $data["url"];
480                 if ($url == "")
481                         return 0;
482         }
483
484         $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2",
485                         dbesc(normalise_link($url)),
486                         intval($uid));
487
488         if (!$contact)
489                 $contact = q("SELECT `id`, `avatar-date` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `uid` = %d ORDER BY `id` LIMIT 1",
490                                 dbesc($url),
491                                 dbesc(normalise_link($url)),
492                                 intval($uid));
493
494         if ($contact) {
495                 $contactid = $contact[0]["id"];
496
497                 // Update the contact every 7 days
498                 $update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -7 days'));
499                 //$update_photo = ($contact[0]['avatar-date'] < datetime_convert('','','now -12 hours'));
500
501                 if (!$update_photo OR $no_update) {
502                         return($contactid);
503                 }
504         } elseif ($uid != 0)
505                 return 0;
506
507         if (!count($data))
508                 $data = probe_url($url);
509
510         // Does this address belongs to a valid network?
511         if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
512                 if ($uid != 0)
513                         return 0;
514
515                 // Get data from the gcontact table
516                 $r = q("SELECT `name`, `nick`, `url`, `photo`, `addr`, `alias`, `network` FROM `gcontact` WHERE `nurl` = '%s'",
517                          dbesc(normalise_link($url)));
518                 if (!$r)
519                         return 0;
520
521                 $data = $r[0];
522         }
523
524         $url = $data["url"];
525
526         if ($contactid == 0) {
527                 q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
528                                         `name`, `nick`, `photo`, `network`, `pubkey`, `rel`, `priority`,
529                                         `batch`, `request`, `confirm`, `poco`, `name-date`, `uri-date`,
530                                         `writable`, `blocked`, `readonly`, `pending`)
531                                         VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', '%s', '%s', '%s', '%s', 1, 0, 0, 0)",
532                         intval($uid),
533                         dbesc(datetime_convert()),
534                         dbesc($data["url"]),
535                         dbesc(normalise_link($data["url"])),
536                         dbesc($data["addr"]),
537                         dbesc($data["alias"]),
538                         dbesc($data["notify"]),
539                         dbesc($data["poll"]),
540                         dbesc($data["name"]),
541                         dbesc($data["nick"]),
542                         dbesc($data["photo"]),
543                         dbesc($data["network"]),
544                         dbesc($data["pubkey"]),
545                         intval(CONTACT_IS_SHARING),
546                         intval($data["priority"]),
547                         dbesc($data["batch"]),
548                         dbesc($data["request"]),
549                         dbesc($data["confirm"]),
550                         dbesc($data["poco"]),
551                         dbesc(datetime_convert()),
552                         dbesc(datetime_convert())
553                 );
554
555                 $contact = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = %d ORDER BY `id` LIMIT 2",
556                                 dbesc(normalise_link($data["url"])),
557                                 intval($uid));
558                 if (!$contact)
559                         return 0;
560
561                 $contactid = $contact[0]["id"];
562
563                 // Update the newly created contact from data in the gcontact table
564                 $r = q("SELECT `location`, `about`, `keywords`, `gender` FROM `gcontact` WHERE `nurl` = '%s'",
565                          dbesc(normalise_link($data["url"])));
566                 if ($r) {
567                         logger("Update contact ".$data["url"]);
568                         q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s' WHERE `id` = %d",
569                                 dbesc($r["location"]), dbesc($r["about"]), dbesc($r["keywords"]),
570                                 dbesc($r["gender"]), intval($contactid));
571                 }
572         }
573
574         if ((count($contact) > 1) AND ($uid == 0) AND ($contactid != 0) AND ($url != ""))
575                 q("DELETE FROM `contact` WHERE `nurl` = '%s' AND `id` != %d",
576                         dbesc(normalise_link($url)),
577                         intval($contactid));
578
579         require_once("Photo.php");
580
581         update_contact_avatar($data["photo"],$uid,$contactid);
582
583         $r = q("SELECT `addr`, `alias`, `name`, `nick` FROM `contact`  WHERE `id` = %d", intval($contactid));
584
585         // This condition should always be true
586         if (!dbm::is_result($r))
587                 return $contactid;
588
589         // Only update if there had something been changed
590         if (($data["addr"] != $r[0]["addr"]) OR
591                 ($data["alias"] != $r[0]["alias"]) OR
592                 ($data["name"] != $r[0]["name"]) OR
593                 ($data["nick"] != $r[0]["nick"]))
594                 q("UPDATE `contact` SET `addr` = '%s', `alias` = '%s', `name` = '%s', `nick` = '%s',
595                         `name-date` = '%s', `uri-date` = '%s' WHERE `id` = %d",
596                         dbesc($data["addr"]),
597                         dbesc($data["alias"]),
598                         dbesc($data["name"]),
599                         dbesc($data["nick"]),
600                         dbesc(datetime_convert()),
601                         dbesc(datetime_convert()),
602                         intval($contactid)
603                 );
604
605         return $contactid;
606 }
607
608 /**
609  * @brief Returns posts from a given gcontact
610  *
611  * @param App $a argv application class
612  * @param int $gcontact_id Global contact
613  *
614  * @return string posts in HTML
615  */
616 function posts_from_gcontact($a, $gcontact_id) {
617
618         require_once('include/conversation.php');
619
620         // There are no posts with "uid = 0" with connector networks
621         // This speeds up the query a lot
622         $r = q("SELECT `network` FROM `gcontact` WHERE `id` = %d", dbesc($gcontact_id));
623         if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")))
624                 $sql = "(`item`.`uid` = 0 OR  (`item`.`uid` = %d AND `item`.`private`))";
625         else
626                 $sql = "`item`.`uid` = %d";
627
628         if(get_config('system', 'old_pager')) {
629                 $r = q("SELECT COUNT(*) AS `total` FROM `item`
630                         WHERE `gcontact-id` = %d and $sql",
631                         intval($gcontact_id),
632                         intval(local_user()));
633
634                 $a->set_pager_total($r[0]['total']);
635         }
636
637         $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
638                         `author-name` AS `name`, `owner-avatar` AS `photo`,
639                         `owner-link` AS `url`, `owner-avatar` AS `thumb`
640                 FROM `item` FORCE INDEX (`gcontactid_uid_created`)
641                 WHERE `gcontact-id` = %d AND $sql AND
642                         NOT `deleted` AND NOT `moderated` AND `visible`
643                 ORDER BY `item`.`created` DESC LIMIT %d, %d",
644                 intval($gcontact_id),
645                 intval(local_user()),
646                 intval($a->pager['start']),
647                 intval($a->pager['itemspage'])
648         );
649
650         $o = conversation($a,$r,'community',false);
651
652         if(!get_config('system', 'old_pager')) {
653                 $o .= alt_pager($a,count($r));
654         } else {
655                 $o .= paginate($a);
656         }
657
658         return $o;
659 }
660 /**
661  * @brief Returns posts from a given contact url
662  *
663  * @param App $a argv application class
664  * @param string $contact_url Contact URL
665  *
666  * @return string posts in HTML
667  */
668 function posts_from_contact_url($a, $contact_url) {
669
670         require_once('include/conversation.php');
671
672         // There are no posts with "uid = 0" with connector networks
673         // This speeds up the query a lot
674         $r = q("SELECT `network`, `id` AS `author-id` FROM `contact`
675                 WHERE `contact`.`nurl` = '%s' AND `contact`.`uid` = 0",
676                 dbesc(normalise_link($contact_url)));
677         if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
678                 $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = %d AND `item`.`private`))";
679         } else {
680                 $sql = "`item`.`uid` = %d";
681         }
682
683         $author_id = intval($r[0]["author-id"]);
684
685         if (get_config('system', 'old_pager')) {
686                 $r = q("SELECT COUNT(*) AS `total` FROM `item`
687                         WHERE `author-id` = %d and $sql",
688                         intval($author_id),
689                         intval(local_user()));
690
691                 $a->set_pager_total($r[0]['total']);
692         }
693
694         $r = q(item_query()." AND `item`.`author-id` = %d AND ".$sql.
695                 " ORDER BY `item`.`created` DESC LIMIT %d, %d",
696                 intval($author_id),
697                 intval(local_user()),
698                 intval($a->pager['start']),
699                 intval($a->pager['itemspage'])
700         );
701
702         $o = conversation($a,$r,'community',false);
703
704         if (!get_config('system', 'old_pager')) {
705                 $o .= alt_pager($a,count($r));
706         } else {
707                 $o .= paginate($a);
708         }
709
710         return $o;
711 }
712
713 /**
714  * @brief Returns a formatted location string from the given profile array
715  *
716  * @param array $profile Profile array (Generated from the "profile" table)
717  *
718  * @return string Location string
719  */
720 function formatted_location($profile) {
721         $location = '';
722
723         if($profile['locality'])
724                 $location .= $profile['locality'];
725
726         if($profile['region'] AND ($profile['locality'] != $profile['region'])) {
727                 if($location)
728                         $location .= ', ';
729
730                 $location .= $profile['region'];
731         }
732
733         if($profile['country-name']) {
734                 if($location)
735                         $location .= ', ';
736
737                 $location .= $profile['country-name'];
738         }
739
740         return $location;
741 }
742
743 /**
744  * @brief Returns the account type name
745  *
746  * The function can be called with either the user or the contact array
747  *
748  * @param array $contact contact or user array
749  */
750 function account_type($contact) {
751
752         // There are several fields that indicate that the contact or user is a forum
753         // "page-flags" is a field in the user table,
754         // "forum" and "prv" are used in the contact table. They stand for PAGE_COMMUNITY and PAGE_PRVGROUP.
755         // "community" is used in the gcontact table and is true if the contact is PAGE_COMMUNITY or PAGE_PRVGROUP.
756         if((isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_COMMUNITY))
757                 || (isset($contact['page-flags']) && (intval($contact['page-flags']) == PAGE_PRVGROUP))
758                 || (isset($contact['forum']) && intval($contact['forum']))
759                 || (isset($contact['prv']) && intval($contact['prv']))
760                 || (isset($contact['community']) && intval($contact['community'])))
761                 $type = ACCOUNT_TYPE_COMMUNITY;
762         else
763                 $type = ACCOUNT_TYPE_PERSON;
764
765         // The "contact-type" (contact table) and "account-type" (user table) are more general then the chaos from above.
766         if (isset($contact["contact-type"]))
767                 $type = $contact["contact-type"];
768         if (isset($contact["account-type"]))
769                 $type = $contact["account-type"];
770
771         switch($type) {
772                 case ACCOUNT_TYPE_ORGANISATION:
773                         $account_type = t("Organisation");
774                         break;
775                 case ACCOUNT_TYPE_NEWS:
776                         $account_type = t('News');
777                         break;
778                 case ACCOUNT_TYPE_COMMUNITY:
779                         $account_type = t("Forum");
780                         break;
781                 default:
782                         $account_type = "";
783                         break;
784         }
785
786         return $account_type;
787 }
788 ?>