Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1810 lines
58 KiB

4 years ago
3 years ago
3 years ago
  1. <?php
  2. /**
  3. * @file src/Model/Contact.php
  4. */
  5. namespace Friendica\Model;
  6. use Friendica\BaseObject;
  7. use Friendica\Core\Addon;
  8. use Friendica\Core\Config;
  9. use Friendica\Core\L10n;
  10. use Friendica\Core\PConfig;
  11. use Friendica\Core\System;
  12. use Friendica\Core\Worker;
  13. use Friendica\Database\DBA;
  14. use Friendica\Model\Profile;
  15. use Friendica\Network\Probe;
  16. use Friendica\Object\Image;
  17. use Friendica\Protocol\Diaspora;
  18. use Friendica\Protocol\OStatus;
  19. use Friendica\Protocol\PortableContact;
  20. use Friendica\Protocol\Salmon;
  21. use Friendica\Util\DateTimeFormat;
  22. use Friendica\Util\Network;
  23. require_once 'boot.php';
  24. require_once 'include/dba.php';
  25. require_once 'include/text.php';
  26. /**
  27. * @brief functions for interacting with a contact
  28. */
  29. class Contact extends BaseObject
  30. {
  31. /**
  32. * @name page/profile types
  33. *
  34. * PAGE_NORMAL is a typical personal profile account
  35. * PAGE_SOAPBOX automatically approves all friend requests as Contact::SHARING, (readonly)
  36. * PAGE_COMMUNITY automatically approves all friend requests as Contact::SHARING, but with
  37. * write access to wall and comments (no email and not included in page owner's ACL lists)
  38. * PAGE_FREELOVE automatically approves all friend requests as full friends (Contact::FRIEND).
  39. *
  40. * @{
  41. */
  42. const PAGE_NORMAL = 0;
  43. const PAGE_SOAPBOX = 1;
  44. const PAGE_COMMUNITY = 2;
  45. const PAGE_FREELOVE = 3;
  46. const PAGE_BLOG = 4;
  47. const PAGE_PRVGROUP = 5;
  48. /**
  49. * @}
  50. */
  51. /**
  52. * @name account types
  53. *
  54. * ACCOUNT_TYPE_PERSON - the account belongs to a person
  55. * Associated page types: PAGE_NORMAL, PAGE_SOAPBOX, PAGE_FREELOVE
  56. *
  57. * ACCOUNT_TYPE_ORGANISATION - the account belongs to an organisation
  58. * Associated page type: PAGE_SOAPBOX
  59. *
  60. * ACCOUNT_TYPE_NEWS - the account is a news reflector
  61. * Associated page type: PAGE_SOAPBOX
  62. *
  63. * ACCOUNT_TYPE_COMMUNITY - the account is community forum
  64. * Associated page types: PAGE_COMMUNITY, PAGE_PRVGROUP
  65. *
  66. * ACCOUNT_TYPE_RELAY - the account is a relay
  67. * This will only be assigned to contacts, not to user accounts
  68. * @{
  69. */
  70. const ACCOUNT_TYPE_PERSON = 0;
  71. const ACCOUNT_TYPE_ORGANISATION = 1;
  72. const ACCOUNT_TYPE_NEWS = 2;
  73. const ACCOUNT_TYPE_COMMUNITY = 3;
  74. const ACCOUNT_TYPE_RELAY = 4;
  75. /**
  76. * @}
  77. */
  78. /**
  79. * @name Contact_is
  80. *
  81. * Relationship types
  82. * @{
  83. */
  84. const FOLLOWER = 1;
  85. const SHARING = 2;
  86. const FRIEND = 3;
  87. /**
  88. * @}
  89. */
  90. /**
  91. * @brief Returns a list of contacts belonging in a group
  92. *
  93. * @param int $gid
  94. * @return array
  95. */
  96. public static function getByGroupId($gid)
  97. {
  98. $return = [];
  99. if (intval($gid)) {
  100. $stmt = DBA::p('SELECT `group_member`.`contact-id`, `contact`.*
  101. FROM `contact`
  102. INNER JOIN `group_member`
  103. ON `contact`.`id` = `group_member`.`contact-id`
  104. WHERE `gid` = ?
  105. AND `contact`.`uid` = ?
  106. AND NOT `contact`.`self`
  107. AND NOT `contact`.`blocked`
  108. AND NOT `contact`.`pending`
  109. ORDER BY `contact`.`name` ASC',
  110. $gid,
  111. local_user()
  112. );
  113. if (DBA::isResult($stmt)) {
  114. $return = DBA::toArray($stmt);
  115. }
  116. }
  117. return $return;
  118. }
  119. /**
  120. * @brief Returns the count of OStatus contacts in a group
  121. *
  122. * @param int $gid
  123. * @return int
  124. */
  125. public static function getOStatusCountByGroupId($gid)
  126. {
  127. $return = 0;
  128. if (intval($gid)) {
  129. $contacts = DBA::fetchFirst('SELECT COUNT(*) AS `count`
  130. FROM `contact`
  131. INNER JOIN `group_member`
  132. ON `contact`.`id` = `group_member`.`contact-id`
  133. WHERE `gid` = ?
  134. AND `contact`.`uid` = ?
  135. AND `contact`.`network` = ?
  136. AND `contact`.`notify` != ""',
  137. $gid,
  138. local_user(),
  139. NETWORK_OSTATUS
  140. );
  141. $return = $contacts['count'];
  142. }
  143. return $return;
  144. }
  145. /**
  146. * Creates the self-contact for the provided user id
  147. *
  148. * @param int $uid
  149. * @return bool Operation success
  150. */
  151. public static function createSelfFromUserId($uid)
  152. {
  153. // Only create the entry if it doesn't exist yet
  154. if (DBA::exists('contact', ['uid' => $uid, 'self' => true])) {
  155. return true;
  156. }
  157. $user = DBA::selectFirst('user', ['uid', 'username', 'nickname'], ['uid' => $uid]);
  158. if (!DBA::isResult($user)) {
  159. return false;
  160. }
  161. $return = DBA::insert('contact', [
  162. 'uid' => $user['uid'],
  163. 'created' => DateTimeFormat::utcNow(),
  164. 'self' => 1,
  165. 'name' => $user['username'],
  166. 'nick' => $user['nickname'],
  167. 'photo' => System::baseUrl() . '/photo/profile/' . $user['uid'] . '.jpg',
  168. 'thumb' => System::baseUrl() . '/photo/avatar/' . $user['uid'] . '.jpg',
  169. 'micro' => System::baseUrl() . '/photo/micro/' . $user['uid'] . '.jpg',
  170. 'blocked' => 0,
  171. 'pending' => 0,
  172. 'url' => System::baseUrl() . '/profile/' . $user['nickname'],
  173. 'nurl' => normalise_link(System::baseUrl() . '/profile/' . $user['nickname']),
  174. 'addr' => $user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3),
  175. 'request' => System::baseUrl() . '/dfrn_request/' . $user['nickname'],
  176. 'notify' => System::baseUrl() . '/dfrn_notify/' . $user['nickname'],
  177. 'poll' => System::baseUrl() . '/dfrn_poll/' . $user['nickname'],
  178. 'confirm' => System::baseUrl() . '/dfrn_confirm/' . $user['nickname'],
  179. 'poco' => System::baseUrl() . '/poco/' . $user['nickname'],
  180. 'name-date' => DateTimeFormat::utcNow(),
  181. 'uri-date' => DateTimeFormat::utcNow(),
  182. 'avatar-date' => DateTimeFormat::utcNow(),
  183. 'closeness' => 0
  184. ]);
  185. return $return;
  186. }
  187. /**
  188. * Updates the self-contact for the provided user id
  189. *
  190. * @param int $uid
  191. * @param boolean $update_avatar Force the avatar update
  192. */
  193. public static function updateSelfFromUserID($uid, $update_avatar = false)
  194. {
  195. $fields = ['id', 'name', 'nick', 'location', 'about', 'keywords', 'gender', 'avatar',
  196. 'xmpp', 'contact-type', 'forum', 'prv', 'avatar-date', 'nurl'];
  197. $self = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
  198. if (!DBA::isResult($self)) {
  199. return;
  200. }
  201. $fields = ['nickname', 'page-flags', 'account-type'];
  202. $user = DBA::selectFirst('user', $fields, ['uid' => $uid]);
  203. if (!DBA::isResult($user)) {
  204. return;
  205. }
  206. $fields = ['name', 'photo', 'thumb', 'about', 'address', 'locality', 'region',
  207. 'country-name', 'gender', 'pub_keywords', 'xmpp'];
  208. $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid, 'is-default' => true]);
  209. if (!DBA::isResult($profile)) {
  210. return;
  211. }
  212. $fields = ['name' => $profile['name'], 'nick' => $user['nickname'],
  213. 'avatar-date' => $self['avatar-date'], 'location' => Profile::formatLocation($profile),
  214. 'about' => $profile['about'], 'keywords' => $profile['pub_keywords'],
  215. 'gender' => $profile['gender'], 'avatar' => $profile['photo'],
  216. 'contact-type' => $user['account-type'], 'xmpp' => $profile['xmpp']];
  217. $avatar = DBA::selectFirst('photo', ['resource-id', 'type'], ['uid' => $uid, 'profile' => true]);
  218. if (DBA::isResult($avatar)) {
  219. if ($update_avatar) {
  220. $fields['avatar-date'] = DateTimeFormat::utcNow();
  221. }
  222. // Creating the path to the avatar, beginning with the file suffix
  223. $types = Image::supportedTypes();
  224. if (isset($types[$avatar['type']])) {
  225. $file_suffix = $types[$avatar['type']];
  226. } else {
  227. $file_suffix = 'jpg';
  228. }
  229. // We are adding a timestamp value so that other systems won't use cached content
  230. $timestamp = strtotime($fields['avatar-date']);
  231. $prefix = System::baseUrl() . '/photo/' .$avatar['resource-id'] . '-';
  232. $suffix = '.' . $file_suffix . '?ts=' . $timestamp;
  233. $fields['photo'] = $prefix . '4' . $suffix;
  234. $fields['thumb'] = $prefix . '5' . $suffix;
  235. $fields['micro'] = $prefix . '6' . $suffix;
  236. } else {
  237. // We hadn't found a photo entry, so we use the default avatar
  238. $fields['photo'] = System::baseUrl() . '/images/person-175.jpg';
  239. $fields['thumb'] = System::baseUrl() . '/images/person-80.jpg';
  240. $fields['micro'] = System::baseUrl() . '/images/person-48.jpg';
  241. }
  242. $fields['forum'] = $user['page-flags'] == self::PAGE_COMMUNITY;
  243. $fields['prv'] = $user['page-flags'] == self::PAGE_PRVGROUP;
  244. // it seems as if ported accounts can have wrong values, so we make sure that now everything is fine.
  245. $fields['url'] = System::baseUrl() . '/profile/' . $user['nickname'];
  246. $fields['nurl'] = normalise_link($fields['url']);
  247. $fields['addr'] = $user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
  248. $fields['request'] = System::baseUrl() . '/dfrn_request/' . $user['nickname'];
  249. $fields['notify'] = System::baseUrl() . '/dfrn_notify/' . $user['nickname'];
  250. $fields['poll'] = System::baseUrl() . '/dfrn_poll/' . $user['nickname'];
  251. $fields['confirm'] = System::baseUrl() . '/dfrn_confirm/' . $user['nickname'];
  252. $fields['poco'] = System::baseUrl() . '/poco/' . $user['nickname'];
  253. $update = false;
  254. foreach ($fields as $field => $content) {
  255. if (isset($self[$field]) && $self[$field] != $content) {
  256. $update = true;
  257. }
  258. }
  259. if ($update) {
  260. $fields['name-date'] = DateTimeFormat::utcNow();
  261. DBA::update('contact', $fields, ['id' => $self['id']]);
  262. // Update the public contact as well
  263. DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $self['nurl']]);
  264. // Update the profile
  265. $fields = ['photo' => System::baseUrl() . '/photo/profile/' .$uid . '.jpg',
  266. 'thumb' => System::baseUrl() . '/photo/avatar/' . $uid .'.jpg'];
  267. DBA::update('profile', $fields, ['uid' => $uid, 'is-default' => true]);
  268. }
  269. }
  270. /**
  271. * @brief Marks a contact for removal
  272. *
  273. * @param int $id contact id
  274. * @return null
  275. */
  276. public static function remove($id)
  277. {
  278. // We want just to make sure that we don't delete our "self" contact
  279. $contact = DBA::selectFirst('contact', ['uid'], ['id' => $id, 'self' => false]);
  280. if (!DBA::isResult($contact) || !intval($contact['uid'])) {
  281. return;
  282. }
  283. $archive = PConfig::get($contact['uid'], 'system', 'archive_removed_contacts');
  284. if ($archive) {
  285. DBA::update('contact', ['archive' => true, 'network' => 'none', 'writable' => false], ['id' => $id]);
  286. return;
  287. }
  288. DBA::delete('contact', ['id' => $id]);
  289. // Delete the rest in the background
  290. Worker::add(PRIORITY_LOW, 'RemoveContact', $id);
  291. }
  292. /**
  293. * @brief Sends an unfriend message. Does not remove the contact
  294. *
  295. * @param array $user User unfriending
  296. * @param array $contact Contact unfriended
  297. * @return void
  298. */
  299. public static function terminateFriendship(array $user, array $contact)
  300. {
  301. if (in_array($contact['network'], [NETWORK_OSTATUS, NETWORK_DFRN])) {
  302. // create an unfollow slap
  303. $item = [];
  304. $item['verb'] = NAMESPACE_OSTATUS . "/unfollow";
  305. $item['follow'] = $contact["url"];
  306. $slap = OStatus::salmon($item, $user);
  307. if (!empty($contact['notify'])) {
  308. Salmon::slapper($user, $contact['notify'], $slap);
  309. }
  310. } elseif ($contact['network'] == NETWORK_DIASPORA) {
  311. Diaspora::sendUnshare($user, $contact);
  312. }
  313. }
  314. /**
  315. * @brief Marks a contact for archival after a communication issue delay
  316. *
  317. * Contact has refused to recognise us as a friend. We will start a countdown.
  318. * If they still don't recognise us in 32 days, the relationship is over,
  319. * and we won't waste any more time trying to communicate with them.
  320. * This provides for the possibility that their database is temporarily messed
  321. * up or some other transient event and that there's a possibility we could recover from it.
  322. *
  323. * @param array $contact contact to mark for archival
  324. * @return null
  325. */
  326. public static function markForArchival(array $contact)
  327. {
  328. // Contact already archived or "self" contact? => nothing to do
  329. if ($contact['archive'] || $contact['self']) {
  330. return;
  331. }
  332. if ($contact['term-date'] <= NULL_DATE) {
  333. DBA::update('contact', ['term-date' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
  334. if ($contact['url'] != '') {
  335. DBA::update('contact', ['term-date' => DateTimeFormat::utcNow()], ['`nurl` = ? AND `term-date` <= ? AND NOT `self`', normalise_link($contact['url']), NULL_DATE]);
  336. }
  337. } else {
  338. /* @todo
  339. * We really should send a notification to the owner after 2-3 weeks
  340. * so they won't be surprised when the contact vanishes and can take
  341. * remedial action if this was a serious mistake or glitch
  342. */
  343. /// @todo Check for contact vitality via probing
  344. $archival_days = Config::get('system', 'archival_days', 32);
  345. $expiry = $contact['term-date'] . ' + ' . $archival_days . ' days ';
  346. if (DateTimeFormat::utcNow() > DateTimeFormat::utc($expiry)) {
  347. /* Relationship is really truly dead. archive them rather than
  348. * delete, though if the owner tries to unarchive them we'll start
  349. * the whole process over again.
  350. */
  351. DBA::update('contact', ['archive' => 1], ['id' => $contact['id']]);
  352. if ($contact['url'] != '') {
  353. DBA::update('contact', ['archive' => 1], ['nurl' => normalise_link($contact['url']), 'self' => false]);
  354. }
  355. }
  356. }
  357. }
  358. /**
  359. * @brief Cancels the archival countdown
  360. *
  361. * @see Contact::markForArchival()
  362. *
  363. * @param array $contact contact to be unmarked for archival
  364. * @return null
  365. */
  366. public static function unmarkForArchival(array $contact)
  367. {
  368. $condition = ['`id` = ? AND (`term-date` > ? OR `archive`)', $contact['id'], NULL_DATE];
  369. $exists = DBA::exists('contact', $condition);
  370. // We don't need to update, we never marked this contact for archival
  371. if (!$exists) {
  372. return;
  373. }
  374. // It's a miracle. Our dead contact has inexplicably come back to life.
  375. $fields = ['term-date' => NULL_DATE, 'archive' => false];
  376. DBA::update('contact', $fields, ['id' => $contact['id']]);
  377. if (!empty($contact['url'])) {
  378. DBA::update('contact', $fields, ['nurl' => normalise_link($contact['url'])]);
  379. }
  380. if (!empty($contact['batch'])) {
  381. $condition = ['batch' => $contact['batch'], 'contact-type' => self::ACCOUNT_TYPE_RELAY];
  382. DBA::update('contact', $fields, $condition);
  383. }
  384. }
  385. /**
  386. * @brief Get contact data for a given profile link
  387. *
  388. * The function looks at several places (contact table and gcontact table) for the contact
  389. * It caches its result for the same script execution to prevent duplicate calls
  390. *
  391. * @param string $url The profile link
  392. * @param int $uid User id
  393. * @param array $default If not data was found take this data as default value
  394. *
  395. * @return array Contact data
  396. */
  397. public static function getDetailsByURL($url, $uid = -1, array $default = [])
  398. {
  399. static $cache = [];
  400. if ($url == '') {
  401. return $default;
  402. }
  403. if ($uid == -1) {
  404. $uid = local_user();
  405. }
  406. if (isset($cache[$url][$uid])) {
  407. return $cache[$url][$uid];
  408. }
  409. $ssl_url = str_replace('http://', 'https://', $url);
  410. // Fetch contact data from the contact table for the given user
  411. $s = DBA::p("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
  412. `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
  413. FROM `contact` WHERE `nurl` = ? AND `uid` = ?", normalise_link($url), $uid);
  414. $r = DBA::toArray($s);
  415. // Fetch contact data from the contact table for the given user, checking with the alias
  416. if (!DBA::isResult($r)) {
  417. $s = DBA::p("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
  418. `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
  419. FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = ?", normalise_link($url), $url, $ssl_url, $uid);
  420. $r = DBA::toArray($s);
  421. }
  422. // Fetch the data from the contact table with "uid=0" (which is filled automatically)
  423. if (!DBA::isResult($r)) {
  424. $s = DBA::p("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
  425. `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
  426. FROM `contact` WHERE `nurl` = ? AND `uid` = 0", normalise_link($url));
  427. $r = DBA::toArray($s);
  428. }
  429. // Fetch the data from the contact table with "uid=0" (which is filled automatically) - checked with the alias
  430. if (!DBA::isResult($r)) {
  431. $s = DBA::p("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
  432. `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
  433. FROM `contact` WHERE `alias` IN (?, ?, ?) AND `uid` = 0", normalise_link($url), $url, $ssl_url);
  434. $r = DBA::toArray($s);
  435. }
  436. // Fetch the data from the gcontact table
  437. if (!DBA::isResult($r)) {
  438. $s = DBA::p("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`,
  439. `keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, 0 AS `forum`, 0 AS `prv`, `community`, `contact-type`, `birthday`, 0 AS `self`
  440. FROM `gcontact` WHERE `nurl` = ?", normalise_link($url));
  441. $r = DBA::toArray($s);
  442. }
  443. if (DBA::isResult($r)) {
  444. // If there is more than one entry we filter out the connector networks
  445. if (count($r) > 1) {
  446. foreach ($r as $id => $result) {
  447. if ($result["network"] == NETWORK_STATUSNET) {
  448. unset($r[$id]);
  449. }
  450. }
  451. }
  452. $profile = array_shift($r);
  453. // "bd" always contains the upcoming birthday of a contact.
  454. // "birthday" might contain the birthday including the year of birth.
  455. if ($profile["birthday"] > '0001-01-01') {
  456. $bd_timestamp = strtotime($profile["birthday"]);
  457. $month = date("m", $bd_timestamp);
  458. $day = date("d", $bd_timestamp);
  459. $current_timestamp = time();
  460. $current_year = date("Y", $current_timestamp);
  461. $current_month = date("m", $current_timestamp);
  462. $current_day = date("d", $current_timestamp);
  463. $profile["bd"] = $current_year . "-" . $month . "-" . $day;
  464. $current = $current_year . "-" . $current_month . "-" . $current_day;
  465. if ($profile["bd"] < $current) {
  466. $profile["bd"] = ( ++$current_year) . "-" . $month . "-" . $day;
  467. }
  468. } else {
  469. $profile["bd"] = '0001-01-01';
  470. }
  471. } else {
  472. $profile = $default;
  473. }
  474. if (empty($profile["photo"]) && isset($default["photo"])) {
  475. $profile["photo"] = $default["photo"];
  476. }
  477. if (empty($profile["name"]) && isset($default["name"])) {
  478. $profile["name"] = $default["name"];
  479. }
  480. if (empty($profile["network"]) && isset($default["network"])) {
  481. $profile["network"] = $default["network"];
  482. }
  483. if (empty($profile["thumb"]) && isset($profile["photo"])) {
  484. $profile["thumb"] = $profile["photo"];
  485. }
  486. if (empty($profile["micro"]) && isset($profile["thumb"])) {
  487. $profile["micro"] = $profile["thumb"];
  488. }
  489. if ((empty($profile["addr"]) || empty($profile["name"])) && (defaults($profile, "gid", 0) != 0)
  490. && in_array($profile["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS])
  491. ) {
  492. Worker::add(PRIORITY_LOW, "UpdateGContact", $profile["gid"]);
  493. }
  494. // Show contact details of Diaspora contacts only if connected
  495. if ((defaults($profile, "cid", 0) == 0) && (defaults($profile, "network", "") == NETWORK_DIASPORA)) {
  496. $profile["location"] = "";
  497. $profile["about"] = "";
  498. $profile["gender"] = "";
  499. $profile["birthday"] = '0001-01-01';
  500. }
  501. $cache[$url][$uid] = $profile;
  502. return $profile;
  503. }
  504. /**
  505. * @brief Get contact data for a given address
  506. *
  507. * The function looks at several places (contact table and gcontact table) for the contact
  508. *
  509. * @param string $addr The profile link
  510. * @param int $uid User id
  511. *
  512. * @return array Contact data
  513. */
  514. public static function getDetailsByAddr($addr, $uid = -1)
  515. {
  516. static $cache = [];
  517. if ($addr == '') {
  518. return [];
  519. }
  520. if ($uid == -1) {
  521. $uid = local_user();
  522. }
  523. // Fetch contact data from the contact table for the given user
  524. $r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
  525. `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`
  526. FROM `contact` WHERE `addr` = '%s' AND `uid` = %d",
  527. DBA::escape($addr),
  528. intval($uid)
  529. );
  530. // Fetch the data from the contact table with "uid=0" (which is filled automatically)
  531. if (!DBA::isResult($r)) {
  532. $r = q("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`,
  533. `keywords`, `gender`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`
  534. FROM `contact` WHERE `addr` = '%s' AND `uid` = 0",
  535. DBA::escape($addr)
  536. );
  537. }
  538. // Fetch the data from the gcontact table
  539. if (!DBA::isResult($r)) {
  540. $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`,
  541. `keywords`, `gender`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `contact-type`, `birthday`, 0 AS `self`
  542. FROM `gcontact` WHERE `addr` = '%s'",
  543. DBA::escape($addr)
  544. );
  545. }
  546. if (!DBA::isResult($r)) {
  547. $data = Probe::uri($addr);
  548. $profile = self::getDetailsByURL($data['url'], $uid);
  549. } else {
  550. $profile = $r[0];
  551. }
  552. return $profile;
  553. }
  554. /**
  555. * @brief Returns the data array for the photo menu of a given contact
  556. *
  557. * @param array $contact contact
  558. * @param int $uid optional, default 0
  559. * @return array
  560. */
  561. public static function photoMenu(array $contact, $uid = 0)
  562. {
  563. // @todo Unused, to be removed
  564. $a = get_app();
  565. $contact_url = '';
  566. $pm_url = '';
  567. $status_link = '';
  568. $photos_link = '';
  569. $posts_link = '';
  570. $contact_drop_link = '';
  571. $poke_link = '';
  572. if ($uid == 0) {
  573. $uid = local_user();
  574. }
  575. if (empty($contact['uid']) || ($contact['uid'] != $uid)) {
  576. if ($uid == 0) {
  577. $profile_link = self::magicLink($contact['url']);
  578. $menu = ['profile' => [L10n::t('View Profile'), $profile_link, true]];
  579. return $menu;
  580. }
  581. // Look for our own contact if the uid doesn't match and isn't public
  582. $contact_own = DBA::selectFirst('contact', [], ['nurl' => $contact['nurl'], 'network' => $contact['network'], 'uid' => $uid]);
  583. if (DBA::isResult($contact_own)) {
  584. return self::photoMenu($contact_own, $uid);
  585. } else {
  586. $profile_link = self::magicLink($contact['url']);
  587. $connlnk = 'follow/?url=' . $contact['url'];
  588. $menu = [
  589. 'profile' => [L10n::t('View Profile'), $profile_link, true],
  590. 'follow' => [L10n::t('Connect/Follow'), $connlnk, true]
  591. ];
  592. return $menu;
  593. }
  594. }
  595. $sparkle = false;
  596. if (($contact['network'] === NETWORK_DFRN) && !$contact['self']) {
  597. $sparkle = true;
  598. $profile_link = System::baseUrl() . '/redir/' . $contact['id'];
  599. } else {
  600. $profile_link = $contact['url'];
  601. }
  602. if ($profile_link === 'mailbox') {
  603. $profile_link = '';
  604. }
  605. if ($sparkle) {
  606. $status_link = $profile_link . '?url=status';
  607. $photos_link = $profile_link . '?url=photos';
  608. $profile_link = $profile_link . '?url=profile';
  609. }
  610. if (in_array($contact['network'], [NETWORK_DFRN, NETWORK_DIASPORA]) && !$contact['self']) {
  611. $pm_url = System::baseUrl() . '/message/new/' . $contact['id'];
  612. }
  613. if (($contact['network'] == NETWORK_DFRN) && !$contact['self']) {
  614. $poke_link = System::baseUrl() . '/poke/?f=&c=' . $contact['id'];
  615. }
  616. $contact_url = System::baseUrl() . '/contacts/' . $contact['id'];
  617. $posts_link = System::baseUrl() . '/contacts/' . $contact['id'] . '/posts';
  618. if (!$contact['self']) {
  619. $contact_drop_link = System::baseUrl() . '/contacts/' . $contact['id'] . '/drop?confirm=1';
  620. }
  621. /**
  622. * Menu array:
  623. * "name" => [ "Label", "link", (bool)Should the link opened in a new tab? ]
  624. */
  625. $menu = [
  626. 'status' => [L10n::t("View Status") , $status_link , true],
  627. 'profile' => [L10n::t("View Profile") , $profile_link , true],
  628. 'photos' => [L10n::t("View Photos") , $photos_link , true],
  629. 'network' => [L10n::t("Network Posts"), $posts_link , false],
  630. 'edit' => [L10n::t("View Contact") , $contact_url , false],
  631. 'drop' => [L10n::t("Drop Contact") , $contact_drop_link, false],
  632. 'pm' => [L10n::t("Send PM") , $pm_url , false],
  633. 'poke' => [L10n::t("Poke") , $poke_link , false],
  634. ];
  635. $args = ['contact' => $contact, 'menu' => &$menu];
  636. Addon::callHooks('contact_photo_menu', $args);
  637. $menucondensed = [];
  638. foreach ($menu as $menuname => $menuitem) {
  639. if ($menuitem[1] != '') {
  640. $menucondensed[$menuname] = $menuitem;
  641. }
  642. }
  643. return $menucondensed;
  644. }
  645. /**
  646. * @brief Returns ungrouped contact count or list for user
  647. *
  648. * Returns either the total number of ungrouped contacts for the given user
  649. * id or a paginated list of ungrouped contacts.
  650. *
  651. * @param int $uid uid
  652. * @param int $start optional, default 0
  653. * @param int $count optional, default 0
  654. *
  655. * @return array
  656. */
  657. public static function getUngroupedList($uid)
  658. {
  659. return q("SELECT *
  660. FROM `contact`
  661. WHERE `uid` = %d
  662. AND NOT `self`
  663. AND NOT `blocked`
  664. AND NOT `pending`
  665. AND `id` NOT IN (
  666. SELECT DISTINCT(`contact-id`)
  667. FROM `group_member`
  668. INNER JOIN `group` ON `group`.`id` = `group_member`.`gid`
  669. WHERE `group`.`uid` = %d
  670. )", intval($uid), intval($uid));
  671. }
  672. /**
  673. * @brief Fetch the contact id for a given URL and user
  674. *
  675. * First lookup in the contact table to find a record matching either `url`, `nurl`,
  676. * `addr` or `alias`.
  677. *
  678. * If there's no record and we aren't looking for a public contact, we quit.
  679. * If there's one, we check that it isn't time to update the picture else we
  680. * directly return the found contact id.
  681. *
  682. * Second, we probe the provided $url whether it's http://server.tld/profile or
  683. * nick@server.tld. We quit if we can't get any info back.
  684. *
  685. * Third, we create the contact record if it doesn't exist
  686. *
  687. * Fourth, we update the existing record with the new data (avatar, alias, nick)
  688. * if there's any updates
  689. *
  690. * @param string $url Contact URL
  691. * @param integer $uid The user id for the contact (0 = public contact)
  692. * @param boolean $no_update Don't update the contact
  693. * @param array $default Default value for creating the contact when every else fails
  694. *
  695. * @return integer Contact ID
  696. */
  697. public static function getIdForURL($url, $uid = 0, $no_update = false, $default = [])
  698. {
  699. logger("Get contact data for url " . $url . " and user " . $uid . " - " . System::callstack(), LOGGER_DEBUG);
  700. $contact_id = 0;
  701. if ($url == '') {
  702. return 0;
  703. }
  704. /// @todo Verify if we can't use Contact::getDetailsByUrl instead of the following
  705. // We first try the nurl (http://server.tld/nick), most common case
  706. $contact = DBA::selectFirst('contact', ['id', 'avatar', 'avatar-date'], ['nurl' => normalise_link($url), 'uid' => $uid]);
  707. // Then the addr (nick@server.tld)
  708. if (!DBA::isResult($contact)) {
  709. $contact = DBA::selectFirst('contact', ['id', 'avatar', 'avatar-date'], ['addr' => $url, 'uid' => $uid]);
  710. }
  711. // Then the alias (which could be anything)
  712. if (!DBA::isResult($contact)) {
  713. // The link could be provided as http although we stored it as https
  714. $ssl_url = str_replace('http://', 'https://', $url);
  715. $condition = ['`alias` IN (?, ?, ?) AND `uid` = ?', $url, normalise_link($url), $ssl_url, $uid];
  716. $contact = DBA::selectFirst('contact', ['id', 'avatar', 'avatar-date'], $condition);
  717. }
  718. if (DBA::isResult($contact)) {
  719. $contact_id = $contact["id"];
  720. // Update the contact every 7 days
  721. $update_contact = ($contact['avatar-date'] < DateTimeFormat::utc('now -7 days'));
  722. // We force the update if the avatar is empty
  723. if (!x($contact, 'avatar')) {
  724. $update_contact = true;
  725. }
  726. if (!$update_contact || $no_update) {
  727. return $contact_id;
  728. }
  729. } elseif ($uid != 0) {
  730. // Non-existing user-specific contact, exiting
  731. return 0;
  732. }
  733. $data = Probe::uri($url, "", $uid);
  734. // Last try in gcontact for unsupported networks
  735. if (!in_array($data["network"], [NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_PUMPIO, NETWORK_MAIL, NETWORK_FEED])) {
  736. if ($uid != 0) {
  737. return 0;
  738. }
  739. // Get data from the gcontact table
  740. $fields = ['name', 'nick', 'url', 'photo', 'addr', 'alias', 'network'];
  741. $contact = DBA::selectFirst('gcontact', $fields, ['nurl' => normalise_link($url)]);
  742. if (!DBA::isResult($contact)) {
  743. $contact = DBA::selectFirst('contact', $fields, ['nurl' => normalise_link($url)]);
  744. }
  745. if (!DBA::isResult($contact)) {
  746. $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
  747. 'photo', 'keywords', 'location', 'about', 'network',
  748. 'priority', 'batch', 'request', 'confirm', 'poco'];
  749. $contact = DBA::selectFirst('contact', $fields, ['addr' => $url]);
  750. }
  751. if (!DBA::isResult($contact)) {
  752. // The link could be provided as http although we stored it as https
  753. $ssl_url = str_replace('http://', 'https://', $url);
  754. $condition = ['alias' => [$url, normalise_link($url), $ssl_url]];
  755. $contact = DBA::selectFirst('contact', $fields, $condition);
  756. }
  757. if (!DBA::isResult($contact)) {
  758. $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
  759. 'photo', 'network', 'priority', 'batch', 'request', 'confirm'];
  760. $condition = ['url' => [$url, normalise_link($url), $ssl_url]];
  761. $contact = DBA::selectFirst('fcontact', $fields, $condition);
  762. }
  763. if (!empty($default)) {
  764. $contact = $default;
  765. }
  766. if (!DBA::isResult($contact)) {
  767. return 0;
  768. } else {
  769. $data = array_merge($data, $contact);
  770. }
  771. }
  772. if (!$contact_id && ($data["alias"] != '') && ($data["alias"] != $url)) {
  773. $contact_id = self::getIdForURL($data["alias"], $uid, true);
  774. }
  775. $url = $data["url"];
  776. if (!$contact_id) {
  777. DBA::insert('contact', [
  778. 'uid' => $uid,
  779. 'created' => DateTimeFormat::utcNow(),
  780. 'url' => $data["url"],
  781. 'nurl' => normalise_link($data["url"]),
  782. 'addr' => $data["addr"],
  783. 'alias' => $data["alias"],
  784. 'notify' => $data["notify"],
  785. 'poll' => $data["poll"],
  786. 'name' => $data["name"],
  787. 'nick' => $data["nick"],
  788. 'photo' => $data["photo"],
  789. 'keywords' => $data["keywords"],
  790. 'location' => $data["location"],
  791. 'about' => $data["about"],
  792. 'network' => $data["network"],
  793. 'pubkey' => $data["pubkey"],
  794. 'rel' => self::SHARING,
  795. 'priority' => $data["priority"],
  796. 'batch' => $data["batch"],
  797. 'request' => $data["request"],
  798. 'confirm' => $data["confirm"],
  799. 'poco' => $data["poco"],
  800. 'name-date' => DateTimeFormat::utcNow(),
  801. 'uri-date' => DateTimeFormat::utcNow(),
  802. 'avatar-date' => DateTimeFormat::utcNow(),
  803. 'writable' => 1,
  804. 'blocked' => 0,
  805. 'readonly' => 0,
  806. 'pending' => 0]
  807. );
  808. $s = DBA::select('contact', ['id'], ['nurl' => normalise_link($data["url"]), 'uid' => $uid], ['order' => ['id'], 'limit' => 2]);
  809. $contacts = DBA::toArray($s);
  810. if (!DBA::isResult($contacts)) {
  811. return 0;
  812. }
  813. $contact_id = $contacts[0]["id"];
  814. // Update the newly created contact from data in the gcontact table
  815. $gcontact = DBA::selectFirst('gcontact', ['location', 'about', 'keywords', 'gender'], ['nurl' => normalise_link($data["url"])]);
  816. if (DBA::isResult($gcontact)) {
  817. // Only use the information when the probing hadn't fetched these values
  818. if ($data['keywords'] != '') {
  819. unset($gcontact['keywords']);
  820. }
  821. if ($data['location'] != '') {
  822. unset($gcontact['location']);
  823. }
  824. if ($data['about'] != '') {
  825. unset($gcontact['about']);
  826. }
  827. DBA::update('contact', $gcontact, ['id' => $contact_id]);
  828. }
  829. if (count($contacts) > 1 && $uid == 0 && $contact_id != 0 && $data["url"] != "") {
  830. DBA::delete('contact', ["`nurl` = ? AND `uid` = 0 AND `id` != ? AND NOT `self`",
  831. normalise_link($data["url"]), $contact_id]);
  832. }
  833. }
  834. self::updateAvatar($data["photo"], $uid, $contact_id);
  835. $fields = ['url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date', 'pubkey'];
  836. $contact = DBA::selectFirst('contact', $fields, ['id' => $contact_id]);
  837. // This condition should always be true
  838. if (!DBA::isResult($contact)) {
  839. return $contact_id;
  840. }
  841. $updated = ['addr' => $data['addr'],
  842. 'alias' => $data['alias'],
  843. 'url' => $data['url'],
  844. 'nurl' => normalise_link($data['url']),
  845. 'name' => $data['name'],
  846. 'nick' => $data['nick']];
  847. if ($data['keywords'] != '') {
  848. $updated['keywords'] = $data['keywords'];
  849. }
  850. if ($data['location'] != '') {
  851. $updated['location'] = $data['location'];
  852. }
  853. // Update the technical stuff as well - if filled
  854. if ($data['notify'] != '') {
  855. $updated['notify'] = $data['notify'];
  856. }
  857. if ($data['poll'] != '') {
  858. $updated['poll'] = $data['poll'];
  859. }
  860. if ($data['batch'] != '') {
  861. $updated['batch'] = $data['batch'];
  862. }
  863. if ($data['request'] != '') {
  864. $updated['request'] = $data['request'];
  865. }
  866. if ($data['confirm'] != '') {
  867. $updated['confirm'] = $data['confirm'];
  868. }
  869. if ($data['poco'] != '') {
  870. $updated['poco'] = $data['poco'];
  871. }
  872. // Only fill the pubkey if it had been empty before. We have to prevent identity theft.
  873. if (empty($contact['pubkey'])) {
  874. $updated['pubkey'] = $data['pubkey'];
  875. }
  876. if (($data["addr"] != $contact["addr"]) || ($data["alias"] != $contact["alias"])) {
  877. $updated['uri-date'] = DateTimeFormat::utcNow();
  878. }
  879. if (($data["name"] != $contact["name"]) || ($data["nick"] != $contact["nick"])) {
  880. $updated['name-date'] = DateTimeFormat::utcNow();
  881. }
  882. $updated['avatar-date'] = DateTimeFormat::utcNow();
  883. DBA::update('contact', $updated, ['id' => $contact_id], $contact);
  884. return $contact_id;
  885. }
  886. /**
  887. * @brief Checks if the contact is blocked
  888. *
  889. * @param int $cid contact id
  890. *
  891. * @return boolean Is the contact blocked?
  892. */
  893. public static function isBlocked($cid)
  894. {
  895. if ($cid == 0) {
  896. return false;
  897. }
  898. $blocked = DBA::selectFirst('contact', ['blocked'], ['id' => $cid]);
  899. if (!DBA::isResult($blocked)) {
  900. return false;
  901. }
  902. return (bool) $blocked['blocked'];
  903. }
  904. /**
  905. * @brief Checks if the contact is hidden
  906. *
  907. * @param int $cid contact id
  908. *
  909. * @return boolean Is the contact hidden?
  910. */
  911. public static function isHidden($cid)
  912. {
  913. if ($cid == 0) {
  914. return false;
  915. }
  916. $hidden = DBA::selectFirst('contact', ['hidden'], ['id' => $cid]);
  917. if (!DBA::isResult($hidden)) {
  918. return false;
  919. }
  920. return (bool) $hidden['hidden'];
  921. }
  922. /**
  923. * @brief Returns posts from a given contact url
  924. *
  925. * @param string $contact_url Contact URL
  926. *
  927. * @return string posts in HTML
  928. */
  929. public static function getPostsFromUrl($contact_url)
  930. {
  931. $a = self::getApp();
  932. require_once 'include/conversation.php';
  933. // There are no posts with "uid = 0" with connector networks
  934. // This speeds up the query a lot
  935. $r = q("SELECT `network`, `id` AS `author-id`, `contact-type` FROM `contact`
  936. WHERE `contact`.`nurl` = '%s' AND `contact`.`uid` = 0",
  937. DBA::escape(normalise_link($contact_url))
  938. );
  939. if (!DBA::isResult($r)) {
  940. return '';
  941. }
  942. if (in_array($r[0]["network"], [NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""])) {
  943. $sql = "(`item`.`uid` = 0 OR (`item`.`uid` = ? AND NOT `item`.`global`))";
  944. } else {
  945. $sql = "`item`.`uid` = ?";
  946. }
  947. $author_id = intval($r[0]["author-id"]);
  948. $contact = ($r[0]["contact-type"] == self::ACCOUNT_TYPE_COMMUNITY ? 'owner-id' : 'author-id');
  949. $condition = ["`$contact` = ? AND `gravity` IN (?, ?) AND " . $sql,
  950. $author_id, GRAVITY_PARENT, GRAVITY_COMMENT, local_user()];
  951. $params = ['order' => ['created' => true],
  952. 'limit' => [$a->pager['start'], $a->pager['itemspage']]];
  953. $r = Item::selectForUser(local_user(), [], $condition, $params);
  954. $items = Item::inArray($r);
  955. $o = conversation($a, $items, 'contact-posts', false);
  956. $o .= alt_pager($a, count($items));
  957. return $o;
  958. }
  959. /**
  960. * @brief Returns the account type name
  961. *
  962. * The function can be called with either the user or the contact array
  963. *
  964. * @param array $contact contact or user array
  965. * @return string
  966. */
  967. public static function getAccountType(array $contact)
  968. {
  969. // There are several fields that indicate that the contact or user is a forum
  970. // "page-flags" is a field in the user table,
  971. // "forum" and "prv" are used in the contact table. They stand for self::PAGE_COMMUNITY and self::PAGE_PRVGROUP.
  972. // "community" is used in the gcontact table and is true if the contact is self::PAGE_COMMUNITY or self::PAGE_PRVGROUP.
  973. if ((isset($contact['page-flags']) && (intval($contact['page-flags']) == self::PAGE_COMMUNITY))
  974. || (isset($contact['page-flags']) && (intval($contact['page-flags']) == self::PAGE_PRVGROUP))
  975. || (isset($contact['forum']) && intval($contact['forum']))
  976. || (isset($contact['prv']) && intval($contact['prv']))
  977. || (isset($contact['community']) && intval($contact['community']))
  978. ) {
  979. $type = self::ACCOUNT_TYPE_COMMUNITY;
  980. } else {
  981. $type = self::ACCOUNT_TYPE_PERSON;
  982. }
  983. // The "contact-type" (contact table) and "account-type" (user table) are more general then the chaos from above.
  984. if (isset($contact["contact-type"])) {
  985. $type = $contact["contact-type"];
  986. }
  987. if (isset($contact["account-type"])) {
  988. $type = $contact["account-type"];
  989. }
  990. switch ($type) {
  991. case self::ACCOUNT_TYPE_ORGANISATION:
  992. $account_type = L10n::t("Organisation");
  993. break;
  994. case self::ACCOUNT_TYPE_NEWS:
  995. $account_type = L10n::t('News');
  996. break;
  997. case self::ACCOUNT_TYPE_COMMUNITY:
  998. $account_type = L10n::t("Forum");
  999. break;
  1000. default:
  1001. $account_type = "";
  1002. break;
  1003. }
  1004. return $account_type;
  1005. }
  1006. /**
  1007. * @brief Blocks a contact
  1008. *
  1009. * @param int $uid
  1010. * @return bool
  1011. */
  1012. public static function block($uid)
  1013. {
  1014. $return = DBA::update('contact', ['blocked' => true], ['id' => $uid]);
  1015. return $return;
  1016. }
  1017. /**
  1018. * @brief Unblocks a contact
  1019. *
  1020. * @param int $uid
  1021. * @return bool
  1022. */
  1023. public static function unblock($uid)
  1024. {
  1025. $return = DBA::update('contact', ['blocked' => false], ['id' => $uid]);
  1026. return $return;
  1027. }
  1028. /**
  1029. * @brief Updates the avatar links in a contact only if needed
  1030. *
  1031. * @param string $avatar Link to avatar picture
  1032. * @param int $uid User id of contact owner
  1033. * @param int $cid Contact id
  1034. * @param bool $force force picture update
  1035. *
  1036. * @return array Returns array of the different avatar sizes
  1037. */
  1038. public static function updateAvatar($avatar, $uid, $cid, $force = false)
  1039. {
  1040. $contact = DBA::selectFirst('contact', ['avatar', 'photo', 'thumb', 'micro', 'nurl'], ['id' => $cid]);
  1041. if (!DBA::isResult($contact)) {
  1042. return false;
  1043. } else {
  1044. $data = [$contact["photo"], $contact["thumb"], $contact["micro"]];
  1045. }
  1046. if (($contact["avatar"] != $avatar) || $force) {
  1047. $photos = Photo::importProfilePhoto($avatar, $uid, $cid, true);
  1048. if ($photos) {
  1049. DBA::update(
  1050. 'contact',
  1051. ['avatar' => $avatar, 'photo' => $photos[0], 'thumb' => $photos[1], 'micro' => $photos[2], 'avatar-date' => DateTimeFormat::utcNow()],
  1052. ['id' => $cid]
  1053. );
  1054. // Update the public contact (contact id = 0)
  1055. if ($uid != 0) {
  1056. $pcontact = DBA::selectFirst('contact', ['id'], ['nurl' => $contact['nurl'], 'uid' => 0]);
  1057. if (DBA::isResult($pcontact)) {
  1058. self::updateAvatar($avatar, 0, $pcontact['id'], $force);
  1059. }
  1060. }
  1061. return $photos;
  1062. }
  1063. }
  1064. return $data;
  1065. }
  1066. /**
  1067. * @param integer $id contact id
  1068. * @return boolean
  1069. */
  1070. public static function updateFromProbe($id)
  1071. {
  1072. /*
  1073. Warning: Never ever fetch the public key via Probe::uri and write it into the contacts.
  1074. This will reliably kill your communication with Friendica contacts.
  1075. */
  1076. $fields = ['url', 'nurl', 'addr', 'alias', 'batch', 'notify', 'poll', 'poco', 'network'];
  1077. $contact = DBA::selectFirst('contact', $fields, ['id' => $id]);
  1078. if (!DBA::isResult($contact)) {
  1079. return false;
  1080. }
  1081. $ret = Probe::uri($contact["url"]);
  1082. // If Probe::uri fails the network code will be different
  1083. if ($ret["network"] != $contact["network"]) {
  1084. return false;
  1085. }
  1086. $update = false;
  1087. // make sure to not overwrite existing values with blank entries
  1088. foreach ($ret as $key => $val) {
  1089. if (isset($contact[$key]) && ($contact[$key] != "") && ($val == "")) {
  1090. $ret[$key] = $contact[$key];
  1091. }
  1092. if (isset($contact[$key]) && ($ret[$key] != $contact[$key])) {
  1093. $update = true;
  1094. }
  1095. }
  1096. if (!$update) {
  1097. return true;
  1098. }
  1099. DBA::update(
  1100. 'contact', [
  1101. 'url' => $ret['url'],
  1102. 'nurl' => normalise_link($ret['url']),
  1103. 'addr' => $ret['addr'],
  1104. 'alias' => $ret['alias'],
  1105. 'batch' => $ret['batch'],
  1106. 'notify' => $ret['notify'],
  1107. 'poll' => $ret['poll'],
  1108. 'poco' => $ret['poco']
  1109. ],
  1110. ['id' => $id]
  1111. );
  1112. // Update the corresponding gcontact entry
  1113. PortableContact::lastUpdated($ret["url"]);
  1114. return true;
  1115. }
  1116. /**
  1117. * Takes a $uid and a url/handle and adds a new contact
  1118. * Currently if the contact is DFRN, interactive needs to be true, to redirect to the
  1119. * dfrn_request page.
  1120. *
  1121. * Otherwise this can be used to bulk add StatusNet contacts, Twitter contacts, etc.
  1122. *
  1123. * Returns an array
  1124. * $return['success'] boolean true if successful
  1125. * $return['message'] error text if success is false.
  1126. *
  1127. * @brief Takes a $uid and a url/handle and adds a new contact
  1128. * @param int $uid
  1129. * @param string $url
  1130. * @param bool $interactive
  1131. * @param string $network
  1132. * @return boolean|string
  1133. */
  1134. public static function createFromProbe($uid, $url, $interactive = false, $network = '')
  1135. {
  1136. $result = ['cid' => -1, 'success' => false, 'message' => ''];
  1137. $a = get_app();
  1138. // remove ajax junk, e.g. Twitter
  1139. $url = str_replace('/#!/', '/', $url);
  1140. if (!Network::isUrlAllowed($url)) {
  1141. $result['message'] = L10n::t('Disallowed profile URL.');
  1142. return $result;
  1143. }
  1144. if (Network::isUrlBlocked($url)) {
  1145. $result['message'] = L10n::t('Blocked domain');
  1146. return $result;
  1147. }
  1148. if (!$url) {
  1149. $result['message'] = L10n::t('Connect URL missing.');
  1150. return $result;
  1151. }
  1152. $arr = ['url' => $url, 'contact' => []];
  1153. Addon::callHooks('follow', $arr);
  1154. if (empty($arr)) {
  1155. $result['message'] = L10n::t('The contact could not be added. Please check the relevant network credentials in your Settings -> Social Networks page.');
  1156. return $result;
  1157. }
  1158. if (x($arr['contact'], 'name')) {
  1159. $ret = $arr['contact'];
  1160. } else {
  1161. $ret = Probe::uri($url, $network, $uid, false);
  1162. }
  1163. if (($network != '') && ($ret['network'] != $network)) {
  1164. logger('Expected network ' . $network . ' does not match actual network ' . $ret['network']);
  1165. return $result;
  1166. }
  1167. // check if we already have a contact
  1168. // the poll url is more reliable than the profile url, as we may have
  1169. // indirect links or webfinger links
  1170. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `poll` IN ('%s', '%s') AND `network` = '%s' AND NOT `pending` LIMIT 1",
  1171. intval($uid),
  1172. DBA::escape($ret['poll']),
  1173. DBA::escape(normalise_link($ret['poll'])),
  1174. DBA::escape($ret['network'])
  1175. );
  1176. if (!DBA::isResult($r)) {
  1177. $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' AND `network` = '%s' AND NOT `pending` LIMIT 1",
  1178. intval($uid),
  1179. DBA::escape(normalise_link($url)),
  1180. DBA::escape($ret['network'])
  1181. );
  1182. }
  1183. if (($ret['network'] === NETWORK_DFRN) && !DBA::isResult($r)) {
  1184. if ($interactive) {
  1185. if (strlen($a->urlpath)) {
  1186. $myaddr = bin2hex(System::baseUrl() . '/profile/' . $a->user['nickname']);
  1187. } else {
  1188. $myaddr = bin2hex($a->user['nickname'] . '@' . $a->get_hostname());
  1189. }
  1190. goaway($ret['request'] . "&addr=$myaddr");
  1191. // NOTREACHED
  1192. }
  1193. } elseif (Config::get('system', 'dfrn_only') && ($ret['network'] != NETWORK_DFRN)) {
  1194. $result['message'] = L10n::t('This site is not configured to allow communications with other networks.') . EOL;
  1195. $result['message'] != L10n::t('No compatible communication protocols or feeds were discovered.') . EOL;
  1196. return $result;
  1197. }
  1198. // This extra param just confuses things, remove it
  1199. if ($ret['network'] === NETWORK_DIASPORA) {
  1200. $ret['url'] = str_replace('?absolute=true', '', $ret['url']);
  1201. }
  1202. // do we have enough information?
  1203. if (!((x($ret, 'name')) && (x($ret, 'poll')) && ((x($ret, 'url')) || (x($ret, 'addr'))))) {
  1204. $result['message'] .= L10n::t('The profile address specified does not provide adequate information.') . EOL;
  1205. if (!x($ret, 'poll')) {
  1206. $result['message'] .= L10n::t('No compatible communication protocols or feeds were discovered.') . EOL;
  1207. }
  1208. if (!x($ret, 'name')) {
  1209. $result['message'] .= L10n::t('An author or name was not found.') . EOL;
  1210. }
  1211. if (!x($ret, 'url')) {
  1212. $result['message'] .= L10n::t('No browser URL could be matched to this address.') . EOL;
  1213. }
  1214. if (strpos($url, '@') !== false) {
  1215. $result['message'] .= L10n::t('Unable to match @-style Identity Address with a known protocol or email contact.') . EOL;
  1216. $result['message'] .= L10n::t('Use mailto: in front of address to force email check.') . EOL;
  1217. }
  1218. return $result;
  1219. }
  1220. if ($ret['network'] === NETWORK_OSTATUS && Config::get('system', 'ostatus_disabled')) {
  1221. $result['message'] .= L10n::t('The profile address specified belongs to a network which has been disabled on this site.') . EOL;
  1222. $ret['notify'] = '';
  1223. }
  1224. if (!$ret['notify']) {
  1225. $result['message'] .= L10n::t('Limited profile. This person will be unable to receive direct/personal notifications from you.') . EOL;
  1226. }
  1227. $writeable = ((($ret['network'] === NETWORK_OSTATUS) && ($ret['notify'])) ? 1 : 0);
  1228. $subhub = (($ret['network'] === NETWORK_OSTATUS) ? true : false);
  1229. $hidden = (($ret['network'] === NETWORK_MAIL) ? 1 : 0);
  1230. if (in_array($ret['network'], [NETWORK_MAIL, NETWORK_DIASPORA])) {
  1231. $writeable = 1;
  1232. }
  1233. if (DBA::isResult($r)) {
  1234. // update contact
  1235. $new_relation = (($r[0]['rel'] == self::FOLLOWER) ? self::FRIEND : self::SHARING);
  1236. $fields = ['rel' => $new_relation, 'subhub' => $subhub, 'readonly' => false];
  1237. DBA::update('contact', $fields, ['id' => $r[0]['id']]);
  1238. } else {
  1239. $new_relation = ((in_array($ret['network'], [NETWORK_MAIL])) ? self::FRIEND : self::SHARING);
  1240. // create contact record
  1241. DBA::insert('contact', [
  1242. 'uid' => $uid,
  1243. 'created' => DateTimeFormat::utcNow(),
  1244. 'url' => $ret['url'],
  1245. 'nurl' => normalise_link($ret['url']),
  1246. 'addr' => $ret['addr'],
  1247. 'alias' => $ret['alias'],
  1248. 'batch' => $ret['batch'],
  1249. 'notify' => $ret['notify'],
  1250. 'poll' => $ret['poll'],
  1251. 'poco' => $ret['poco'],
  1252. 'name' => $ret['name'],
  1253. 'nick' => $ret['nick'],
  1254. 'network' => $ret['network'],
  1255. 'pubkey' => $ret['pubkey'],
  1256. 'rel' => $new_relation,
  1257. 'priority'=> $ret['priority'],
  1258. 'writable'=> $writeable,
  1259. 'hidden' => $hidden,
  1260. 'blocked' => 0,
  1261. 'readonly'=> 0,
  1262. 'pending' => 0,
  1263. 'subhub' => $subhub
  1264. ]);
  1265. }
  1266. $contact = DBA::selectFirst('contact', [], ['url' => $ret['url'], 'network' => $ret['network'], 'uid' => $uid]);
  1267. if (!DBA::isResult($contact)) {
  1268. $result['message'] .= L10n::t('Unable to retrieve contact information.') . EOL;
  1269. return $result;
  1270. }
  1271. $contact_id = $contact['id'];
  1272. $result['cid'] = $contact_id;
  1273. Group::addMember(User::getDefaultGroup($uid, $contact["network"]), $contact_id);
  1274. // Update the avatar
  1275. self::updateAvatar($ret['photo'], $uid, $contact_id);
  1276. // pull feed and consume it, which should subscribe to the hub.
  1277. Worker::add(PRIORITY_HIGH, "OnePoll", $contact_id, "force");
  1278. $r = q("SELECT `contact`.*, `user`.* FROM `contact` INNER JOIN `user` ON `contact`.`uid` = `user`.`uid`
  1279. WHERE `user`.`uid` = %d AND `contact`.`self` LIMIT 1",
  1280. intval($uid)
  1281. );
  1282. if (DBA::isResult($r)) {
  1283. if (in_array($contact['network'], [NETWORK_OSTATUS, NETWORK_DFRN])) {
  1284. // create a follow slap
  1285. $item = [];
  1286. $item['verb'] = ACTIVITY_FOLLOW;
  1287. $item['follow'] = $contact["url"];
  1288. $slap = OStatus::salmon($item, $r[0]);
  1289. if (!empty($contact['notify'])) {
  1290. Salmon::slapper($r[0], $contact['notify'], $slap);
  1291. }
  1292. } elseif ($contact['network'] == NETWORK_DIASPORA) {
  1293. $ret = Diaspora::sendShare($a->user, $contact);
  1294. logger('share returns: ' . $ret);
  1295. }
  1296. }
  1297. $result['success'] = true;
  1298. return $result;
  1299. }
  1300. public static function updateSslPolicy($contact, $new_policy)
  1301. {
  1302. $ssl_changed = false;
  1303. if ((intval($new_policy) == SSL_POLICY_SELFSIGN || $new_policy === 'self') && strstr($contact['url'], 'https:')) {
  1304. $ssl_changed = true;
  1305. $contact['url'] = str_replace('https:', 'http:', $contact['url']);
  1306. $contact['request'] = str_replace('https:', 'http:', $contact['request']);
  1307. $contact['notify'] = str_replace('https:', 'http:', $contact['notify']);
  1308. $contact['poll'] = str_replace('https:', 'http:', $contact['poll']);
  1309. $contact['confirm'] = str_replace('https:', 'http:', $contact['confirm']);
  1310. $contact['poco'] = str_replace('https:', 'http:', $contact['poco']);
  1311. }
  1312. if ((intval($new_policy) == SSL_POLICY_FULL || $new_policy === 'full') && strstr($contact['url'], 'http:')) {
  1313. $ssl_changed = true;
  1314. $contact['url'] = str_replace('http:', 'https:', $contact['url']);
  1315. $contact['request'] = str_replace('http:', 'https:', $contact['request']);
  1316. $contact['notify'] = str_replace('http:', 'https:', $contact['notify']);
  1317. $contact['poll'] = str_replace('http:', 'https:', $contact['poll']);
  1318. $contact['confirm'] = str_replace('http:', 'https:', $contact['confirm']);
  1319. $contact['poco'] = str_replace('http:', 'https:', $contact['poco']);
  1320. }
  1321. if ($ssl_changed) {
  1322. $fields = ['url' => $contact['url'], 'request' => $contact['request'],
  1323. 'notify' => $contact['notify'], 'poll' => $contact['poll'],
  1324. 'confirm' => $contact['confirm'], 'poco' => $contact['poco']];
  1325. DBA::update('contact', $fields, ['id' => $contact['id']]);
  1326. }
  1327. return $contact;
  1328. }
  1329. public static function addRelationship($importer, $contact, $datarray, $item, $sharing = false) {
  1330. $url = notags(trim($datarray['author-link']));
  1331. $name = notags(trim($datarray['author-name']));
  1332. $photo = notags(trim($datarray['author-avatar']));
  1333. $nick = '';
  1334. if (is_object($item)) {
  1335. $rawtag = $item->get_item_tags(NAMESPACE_ACTIVITY,'actor');
  1336. if ($rawtag && $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data']) {
  1337. $nick = $rawtag[0]['child'][NAMESPACE_POCO]['preferredUsername'][0]['data'];
  1338. }
  1339. } else {
  1340. $nick = $item;
  1341. }
  1342. if (is_array($contact)) {
  1343. if (($contact['rel'] == self::SHARING)
  1344. || ($sharing && $contact['rel'] == self::FOLLOWER)) {
  1345. DBA::update('contact', ['rel' => self::FRIEND, 'writable' => true],
  1346. ['id' => $contact['id'], 'uid' => $importer['uid']]);
  1347. }
  1348. // send email notification to owner?
  1349. } else {
  1350. if (DBA::exists('contact', ['nurl' => normalise_link($url), 'uid' => $importer['uid'], 'pending' => true])) {
  1351. logger('ignoring duplicated connection request from pending contact ' . $url);
  1352. return;
  1353. }
  1354. // create contact record
  1355. q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `name`, `nick`, `photo`, `network`, `rel`,
  1356. `blocked`, `readonly`, `pending`, `writable`)
  1357. VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, 0, 0, 1, 1)",
  1358. intval($importer['uid']),
  1359. DBA::escape(DateTimeFormat::utcNow()),
  1360. DBA::escape($url),
  1361. DBA::escape(normalise_link($url)),
  1362. DBA::escape($name),
  1363. DBA::escape($nick),
  1364. DBA::escape($photo),
  1365. DBA::escape(NETWORK_OSTATUS),
  1366. intval(self::FOLLOWER)
  1367. );
  1368. $contact_record = [
  1369. 'id' => DBA::lastInsertId(),
  1370. 'network' => NETWORK_OSTATUS,
  1371. 'name' => $name,
  1372. 'url' => $url,
  1373. 'photo' => $photo
  1374. ];
  1375. Contact::updateAvatar($photo, $importer["uid"], $contact_record["id"], true);
  1376. /// @TODO Encapsulate this into a function/method
  1377. $fields = ['uid', 'username', 'email', 'page-flags', 'notify-flags', 'language'];
  1378. $user = DBA::selectFirst('user', $fields, ['uid' => $importer['uid']]);
  1379. if (DBA::isResult($user) && !in_array($user['page-flags'], [self::PAGE_SOAPBOX, self::PAGE_FREELOVE, self::PAGE_COMMUNITY])) {
  1380. // create notification
  1381. $hash = random_string();
  1382. if (is_array($contact_record)) {
  1383. DBA::insert('intro', ['uid' => $importer['uid'], 'contact-id' => $contact_record['id'],
  1384. 'blocked' => false, 'knowyou' => false,
  1385. 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow()]);
  1386. }
  1387. Group::addMember(User::getDefaultGroup($importer['uid'], $contact_record["network"]), $contact_record['id']);
  1388. if (($user['notify-flags'] & NOTIFY_INTRO) &&
  1389. in_array($user['page-flags'], [self::PAGE_NORMAL])) {
  1390. notification([
  1391. 'type' => NOTIFY_INTRO,
  1392. 'notify_flags' => $user['notify-flags'],
  1393. 'language' => $user['language'],
  1394. 'to_name' => $user['username'],
  1395. 'to_email' => $user['email'],
  1396. 'uid' => $user['uid'],
  1397. 'link' => System::baseUrl() . '/notifications/intro',
  1398. 'source_name' => ((strlen(stripslashes($contact_record['name']))) ? stripslashes($contact_record['name']) : L10n::t('[Name Withheld]')),
  1399. 'source_link' => $contact_record['url'],
  1400. 'source_photo' => $contact_record['photo'],
  1401. 'verb' => ($sharing ? ACTIVITY_FRIEND : ACTIVITY_FOLLOW),
  1402. 'otype' => 'intro'
  1403. ]);
  1404. }
  1405. } elseif (DBA::isResult($user) && in_array($user['page-flags'], [self::PAGE_SOAPBOX, self::PAGE_FREELOVE, self::PAGE_COMMUNITY])) {
  1406. q("UPDATE `contact` SET `pending` = 0 WHERE `uid` = %d AND `url` = '%s' AND `pending` LIMIT 1",
  1407. intval($importer['uid']),
  1408. DBA::escape($url)
  1409. );
  1410. }
  1411. }
  1412. }
  1413. public static function removeFollower($importer, $contact, array $datarray = [], $item = "")
  1414. {
  1415. if (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::SHARING)) {
  1416. DBA::update('contact', ['rel' => self::SHARING], ['id' => $contact['id']]);
  1417. } else {
  1418. Contact::remove($contact['id']);
  1419. }
  1420. }
  1421. public static function removeSharer($importer, $contact, array $datarray = [], $item = "")
  1422. {
  1423. if (($contact['rel'] == self::FRIEND) || ($contact['rel'] == self::FOLLOWER)) {
  1424. DBA::update('contact', ['rel' => self::FOLLOWER], ['id' => $contact['id']]);
  1425. } else {
  1426. Contact::remove($contact['id']);
  1427. }
  1428. }
  1429. /**
  1430. * @brief Create a birthday event.
  1431. *
  1432. * Update the year <