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