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.
 
 
 
 
 
 

1934 lines
59 KiB

  1. <?php
  2. /**
  3. * @file include/socgraph.php
  4. *
  5. * @todo Move GNU Social URL schemata (http://server.tld/user/number) to http://server.tld/username
  6. * @todo Fetch profile data from profile page for Redmatrix users
  7. * @todo Detect if it is a forum
  8. */
  9. require_once('include/datetime.php');
  10. require_once("include/Scrape.php");
  11. require_once("include/network.php");
  12. require_once("include/html2bbcode.php");
  13. require_once("include/Contact.php");
  14. require_once("include/Photo.php");
  15. /*
  16. * poco_load
  17. *
  18. * Given a contact-id (minimum), load the PortableContacts friend list for that contact,
  19. * and add the entries to the gcontact (Global Contact) table, or update existing entries
  20. * if anything (name or photo) has changed.
  21. * We use normalised urls for comparison which ignore http vs https and www.domain vs domain
  22. *
  23. * Once the global contact is stored add (if necessary) the contact linkage which associates
  24. * the given uid, cid to the global contact entry. There can be many uid/cid combinations
  25. * pointing to the same global contact id.
  26. *
  27. */
  28. function poco_load($cid,$uid = 0,$zcid = 0,$url = null) {
  29. $a = get_app();
  30. if($cid) {
  31. if((! $url) || (! $uid)) {
  32. $r = q("select `poco`, `uid` from `contact` where `id` = %d limit 1",
  33. intval($cid)
  34. );
  35. if (dbm::is_result($r)) {
  36. $url = $r[0]['poco'];
  37. $uid = $r[0]['uid'];
  38. }
  39. }
  40. if(! $uid)
  41. return;
  42. }
  43. if(! $url)
  44. return;
  45. $url = $url . (($uid) ? '/@me/@all?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation' : '?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation') ;
  46. logger('poco_load: ' . $url, LOGGER_DEBUG);
  47. $s = fetch_url($url);
  48. logger('poco_load: returns ' . $s, LOGGER_DATA);
  49. logger('poco_load: return code: ' . $a->get_curl_code(), LOGGER_DEBUG);
  50. if(($a->get_curl_code() > 299) || (! $s))
  51. return;
  52. $j = json_decode($s);
  53. logger('poco_load: json: ' . print_r($j,true),LOGGER_DATA);
  54. if(! isset($j->entry))
  55. return;
  56. $total = 0;
  57. foreach($j->entry as $entry) {
  58. $total ++;
  59. $profile_url = '';
  60. $profile_photo = '';
  61. $connect_url = '';
  62. $name = '';
  63. $network = '';
  64. $updated = '0000-00-00 00:00:00';
  65. $location = '';
  66. $about = '';
  67. $keywords = '';
  68. $gender = '';
  69. $contact_type = -1;
  70. $generation = 0;
  71. $name = $entry->displayName;
  72. if (isset($entry->urls)) {
  73. foreach ($entry->urls as $url) {
  74. if ($url->type == 'profile') {
  75. $profile_url = $url->value;
  76. continue;
  77. }
  78. if ($url->type == 'webfinger') {
  79. $connect_url = str_replace('acct:' , '', $url->value);
  80. continue;
  81. }
  82. }
  83. }
  84. if (isset($entry->photos)) {
  85. foreach ($entry->photos as $photo) {
  86. if ($photo->type == 'profile') {
  87. $profile_photo = $photo->value;
  88. continue;
  89. }
  90. }
  91. }
  92. if (isset($entry->updated)) {
  93. $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
  94. }
  95. if (isset($entry->network)) {
  96. $network = $entry->network;
  97. }
  98. if (isset($entry->currentLocation)) {
  99. $location = $entry->currentLocation;
  100. }
  101. if (isset($entry->aboutMe)) {
  102. $about = html2bbcode($entry->aboutMe);
  103. }
  104. if (isset($entry->gender)) {
  105. $gender = $entry->gender;
  106. }
  107. if (isset($entry->generation) AND ($entry->generation > 0)) {
  108. $generation = ++$entry->generation;
  109. }
  110. if (isset($entry->tags)) {
  111. foreach($entry->tags as $tag) {
  112. $keywords = implode(", ", $tag);
  113. }
  114. }
  115. if (isset($entry->contactType) AND ($entry->contactType >= 0))
  116. $contact_type = $entry->contactType;
  117. // If you query a Friendica server for its profiles, the network has to be Friendica
  118. /// TODO It could also be a Redmatrix server
  119. //if ($uid == 0)
  120. // $network = NETWORK_DFRN;
  121. poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid);
  122. $gcontact = array("url" => $profile_url, "contact-type" => $contact_type, "generation" => $generation);
  123. update_gcontact($gcontact);
  124. // Update the Friendica contacts. Diaspora is doing it via a message. (See include/diaspora.php)
  125. // Deactivated because we now update Friendica contacts in dfrn.php
  126. //if (($location != "") OR ($about != "") OR ($keywords != "") OR ($gender != ""))
  127. // q("UPDATE `contact` SET `location` = '%s', `about` = '%s', `keywords` = '%s', `gender` = '%s'
  128. // WHERE `nurl` = '%s' AND NOT `self` AND `network` = '%s'",
  129. // dbesc($location),
  130. // dbesc($about),
  131. // dbesc($keywords),
  132. // dbesc($gender),
  133. // dbesc(normalise_link($profile_url)),
  134. // dbesc(NETWORK_DFRN));
  135. }
  136. logger("poco_load: loaded $total entries",LOGGER_DEBUG);
  137. q("DELETE FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `zcid` = %d AND `updated` < UTC_TIMESTAMP - INTERVAL 2 DAY",
  138. intval($cid),
  139. intval($uid),
  140. intval($zcid)
  141. );
  142. }
  143. function poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid = 0, $uid = 0, $zcid = 0) {
  144. // Generation:
  145. // 0: No definition
  146. // 1: Profiles on this server
  147. // 2: Contacts of profiles on this server
  148. // 3: Contacts of contacts of profiles on this server
  149. // 4: ...
  150. $gcid = "";
  151. if ($profile_url == "")
  152. return $gcid;
  153. $urlparts = parse_url($profile_url);
  154. if (!isset($urlparts["scheme"]))
  155. return $gcid;
  156. if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
  157. "identi.ca", "alpha.app.net")))
  158. return $gcid;
  159. // Don't store the statusnet connector as network
  160. // We can't simply set this to NETWORK_OSTATUS since the connector could have fetched posts from friendica as well
  161. if ($network == NETWORK_STATUSNET)
  162. $network = "";
  163. // Assure that there are no parameter fragments in the profile url
  164. if (in_array($network, array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, "")))
  165. $profile_url = clean_contact_url($profile_url);
  166. $alternate = poco_alternate_ostatus_url($profile_url);
  167. $orig_updated = $updated;
  168. // The global contacts should contain the original picture, not the cached one
  169. if (($generation != 1) AND stristr(normalise_link($profile_photo), normalise_link(App::get_baseurl()."/photo/"))) {
  170. $profile_photo = "";
  171. }
  172. $r = q("SELECT `network` FROM `contact` WHERE `nurl` = '%s' AND `network` != '' AND `network` != '%s' LIMIT 1",
  173. dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
  174. );
  175. if (dbm::is_result($r)) {
  176. $network = $r[0]["network"];
  177. }
  178. if (($network == "") OR ($network == NETWORK_OSTATUS)) {
  179. $r = q("SELECT `network`, `url` FROM `contact` WHERE `alias` IN ('%s', '%s') AND `network` != '' AND `network` != '%s' LIMIT 1",
  180. dbesc($profile_url), dbesc(normalise_link($profile_url)), dbesc(NETWORK_STATUSNET)
  181. );
  182. if (dbm::is_result($r)) {
  183. $network = $r[0]["network"];
  184. //$profile_url = $r[0]["url"];
  185. }
  186. }
  187. $x = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s' LIMIT 1",
  188. dbesc(normalise_link($profile_url))
  189. );
  190. if (count($x)) {
  191. if (($network == "") AND ($x[0]["network"] != NETWORK_STATUSNET))
  192. $network = $x[0]["network"];
  193. if ($updated == "0000-00-00 00:00:00")
  194. $updated = $x[0]["updated"];
  195. $created = $x[0]["created"];
  196. $server_url = $x[0]["server_url"];
  197. $nick = $x[0]["nick"];
  198. $addr = $x[0]["addr"];
  199. $alias = $x[0]["alias"];
  200. $notify = $x[0]["notify"];
  201. } else {
  202. $created = "0000-00-00 00:00:00";
  203. $server_url = "";
  204. $urlparts = parse_url($profile_url);
  205. $nick = end(explode("/", $urlparts["path"]));
  206. $addr = "";
  207. $alias = "";
  208. $notify = "";
  209. }
  210. if ((($network == "") OR ($name == "") OR ($addr == "") OR ($profile_photo == "") OR ($server_url == "") OR $alternate)
  211. AND poco_reachable($profile_url, $server_url, $network, false)) {
  212. $data = probe_url($profile_url);
  213. $orig_profile = $profile_url;
  214. $network = $data["network"];
  215. $name = $data["name"];
  216. $nick = $data["nick"];
  217. $addr = $data["addr"];
  218. $alias = $data["alias"];
  219. $notify = $data["notify"];
  220. $profile_url = $data["url"];
  221. $profile_photo = $data["photo"];
  222. $server_url = $data["baseurl"];
  223. if ($alternate AND ($network == NETWORK_OSTATUS)) {
  224. // Delete the old entry - if it exists
  225. $r = q("SELECT `id` FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile)));
  226. if ($r) {
  227. q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($orig_profile)));
  228. q("DELETE FROM `glink` WHERE `gcid` = %d", intval($r[0]["id"]));
  229. }
  230. // possibly create a new entry
  231. poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, $cid, $uid, $zcid);
  232. }
  233. }
  234. if ($alternate AND ($network == NETWORK_OSTATUS))
  235. return $gcid;
  236. if (count($x) AND ($x[0]["network"] == "") AND ($network != "")) {
  237. q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
  238. dbesc($network),
  239. dbesc(normalise_link($profile_url))
  240. );
  241. }
  242. if (($name == "") OR ($profile_photo == ""))
  243. return $gcid;
  244. if (!in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
  245. return $gcid;
  246. logger("profile-check generation: ".$generation." Network: ".$network." URL: ".$profile_url." name: ".$name." avatar: ".$profile_photo, LOGGER_DEBUG);
  247. poco_check_server($server_url, $network);
  248. $gcontact = array("url" => $profile_url,
  249. "addr" => $addr,
  250. "alias" => $alias,
  251. "name" => $name,
  252. "network" => $network,
  253. "photo" => $profile_photo,
  254. "about" => $about,
  255. "location" => $location,
  256. "gender" => $gender,
  257. "keywords" => $keywords,
  258. "server_url" => $server_url,
  259. "connect" => $connect_url,
  260. "notify" => $notify,
  261. "updated" => $updated,
  262. "generation" => $generation);
  263. $gcid = update_gcontact($gcontact);
  264. if(!$gcid)
  265. return $gcid;
  266. $r = q("SELECT * FROM `glink` WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d LIMIT 1",
  267. intval($cid),
  268. intval($uid),
  269. intval($gcid),
  270. intval($zcid)
  271. );
  272. if (! dbm::is_result($r)) {
  273. q("INSERT INTO `glink` (`cid`,`uid`,`gcid`,`zcid`, `updated`) VALUES (%d,%d,%d,%d, '%s') ",
  274. intval($cid),
  275. intval($uid),
  276. intval($gcid),
  277. intval($zcid),
  278. dbesc(datetime_convert())
  279. );
  280. } else {
  281. q("UPDATE `glink` SET `updated` = '%s' WHERE `cid` = %d AND `uid` = %d AND `gcid` = %d AND `zcid` = %d",
  282. dbesc(datetime_convert()),
  283. intval($cid),
  284. intval($uid),
  285. intval($gcid),
  286. intval($zcid)
  287. );
  288. }
  289. return $gcid;
  290. }
  291. function poco_reachable($profile, $server = "", $network = "", $force = false) {
  292. if ($server == "")
  293. $server = poco_detect_server($profile);
  294. if ($server == "")
  295. return true;
  296. return poco_check_server($server, $network, $force);
  297. }
  298. function poco_detect_server($profile) {
  299. // Try to detect the server path based upon some known standard paths
  300. $server_url = "";
  301. if ($server_url == "") {
  302. $friendica = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $profile);
  303. if ($friendica != $profile) {
  304. $server_url = $friendica;
  305. $network = NETWORK_DFRN;
  306. }
  307. }
  308. if ($server_url == "") {
  309. $diaspora = preg_replace("=(https?://)(.*)/u/(.*)=ism", "$1$2", $profile);
  310. if ($diaspora != $profile) {
  311. $server_url = $diaspora;
  312. $network = NETWORK_DIASPORA;
  313. }
  314. }
  315. if ($server_url == "") {
  316. $red = preg_replace("=(https?://)(.*)/channel/(.*)=ism", "$1$2", $profile);
  317. if ($red != $profile) {
  318. $server_url = $red;
  319. $network = NETWORK_DIASPORA;
  320. }
  321. }
  322. // Mastodon
  323. if ($server_url == "") {
  324. $red = preg_replace("=(https?://)(.*)/users/(.*)=ism", "$1$2", $profile);
  325. if ($red != $profile) {
  326. $server_url = $red;
  327. $network = NETWORK_OSTATUS;
  328. }
  329. }
  330. return $server_url;
  331. }
  332. function poco_alternate_ostatus_url($url) {
  333. return(preg_match("=https?://.+/user/\d+=ism", $url, $matches));
  334. }
  335. function poco_last_updated($profile, $force = false) {
  336. $gcontacts = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'",
  337. dbesc(normalise_link($profile)));
  338. if ($gcontacts[0]["created"] == "0000-00-00 00:00:00")
  339. q("UPDATE `gcontact` SET `created` = '%s' WHERE `nurl` = '%s'",
  340. dbesc(datetime_convert()), dbesc(normalise_link($profile)));
  341. if ($gcontacts[0]["server_url"] != "")
  342. $server_url = $gcontacts[0]["server_url"];
  343. else
  344. $server_url = poco_detect_server($profile);
  345. if (!in_array($gcontacts[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_FEED, NETWORK_OSTATUS, ""))) {
  346. logger("Profile ".$profile.": Network type ".$gcontacts[0]["network"]." can't be checked", LOGGER_DEBUG);
  347. return false;
  348. }
  349. if ($server_url != "") {
  350. if (!poco_check_server($server_url, $gcontacts[0]["network"], $force)) {
  351. if ($force)
  352. q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
  353. dbesc(datetime_convert()), dbesc(normalise_link($profile)));
  354. logger("Profile ".$profile.": Server ".$server_url." wasn't reachable.", LOGGER_DEBUG);
  355. return false;
  356. }
  357. q("UPDATE `gcontact` SET `server_url` = '%s' WHERE `nurl` = '%s'",
  358. dbesc($server_url), dbesc(normalise_link($profile)));
  359. }
  360. if (in_array($gcontacts[0]["network"], array("", NETWORK_FEED))) {
  361. $server = q("SELECT `network` FROM `gserver` WHERE `nurl` = '%s' AND `network` != ''",
  362. dbesc(normalise_link($server_url)));
  363. if ($server)
  364. q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
  365. dbesc($server[0]["network"]), dbesc(normalise_link($profile)));
  366. else
  367. return false;
  368. }
  369. // noscrape is really fast so we don't cache the call.
  370. if (($gcontacts[0]["server_url"] != "") AND ($gcontacts[0]["nick"] != "")) {
  371. // Use noscrape if possible
  372. $server = q("SELECT `noscrape`, `network` FROM `gserver` WHERE `nurl` = '%s' AND `noscrape` != ''", dbesc(normalise_link($gcontacts[0]["server_url"])));
  373. if ($server) {
  374. $noscraperet = z_fetch_url($server[0]["noscrape"]."/".$gcontacts[0]["nick"]);
  375. if ($noscraperet["success"] AND ($noscraperet["body"] != "")) {
  376. $noscrape = json_decode($noscraperet["body"], true);
  377. if (is_array($noscrape)) {
  378. $contact = array("url" => $profile,
  379. "network" => $server[0]["network"],
  380. "generation" => $gcontacts[0]["generation"]);
  381. if (isset($noscrape["fn"]))
  382. $contact["name"] = $noscrape["fn"];
  383. if (isset($noscrape["comm"]))
  384. $contact["community"] = $noscrape["comm"];
  385. if (isset($noscrape["tags"])) {
  386. $keywords = implode(" ", $noscrape["tags"]);
  387. if ($keywords != "")
  388. $contact["keywords"] = $keywords;
  389. }
  390. $location = formatted_location($noscrape);
  391. if ($location)
  392. $contact["location"] = $location;
  393. if (isset($noscrape["dfrn-notify"]))
  394. $contact["notify"] = $noscrape["dfrn-notify"];
  395. // Remove all fields that are not present in the gcontact table
  396. unset($noscrape["fn"]);
  397. unset($noscrape["key"]);
  398. unset($noscrape["homepage"]);
  399. unset($noscrape["comm"]);
  400. unset($noscrape["tags"]);
  401. unset($noscrape["locality"]);
  402. unset($noscrape["region"]);
  403. unset($noscrape["country-name"]);
  404. unset($noscrape["contacts"]);
  405. unset($noscrape["dfrn-request"]);
  406. unset($noscrape["dfrn-confirm"]);
  407. unset($noscrape["dfrn-notify"]);
  408. unset($noscrape["dfrn-poll"]);
  409. // Set the date of the last contact
  410. /// @todo By now the function "update_gcontact" doesn't work with this field
  411. //$contact["last_contact"] = datetime_convert();
  412. $contact = array_merge($contact, $noscrape);
  413. update_gcontact($contact);
  414. if (trim($noscrape["updated"]) != "") {
  415. q("UPDATE `gcontact` SET `last_contact` = '%s' WHERE `nurl` = '%s'",
  416. dbesc(datetime_convert()), dbesc(normalise_link($profile)));
  417. logger("Profile ".$profile." was last updated at ".$noscrape["updated"]." (noscrape)", LOGGER_DEBUG);
  418. return $noscrape["updated"];
  419. }
  420. }
  421. }
  422. }
  423. }
  424. // If we only can poll the feed, then we only do this once a while
  425. if (!$force AND !poco_do_update($gcontacts[0]["created"], $gcontacts[0]["updated"], $gcontacts[0]["last_failure"], $gcontacts[0]["last_contact"])) {
  426. logger("Profile ".$profile." was last updated at ".$gcontacts[0]["updated"]." (cached)", LOGGER_DEBUG);
  427. return $gcontacts[0]["updated"];
  428. }
  429. $data = probe_url($profile);
  430. // Is the profile link the alternate OStatus link notation? (http://domain.tld/user/4711)
  431. // Then check the other link and delete this one
  432. if (($data["network"] == NETWORK_OSTATUS) AND poco_alternate_ostatus_url($profile) AND
  433. (normalise_link($profile) == normalise_link($data["alias"])) AND
  434. (normalise_link($profile) != normalise_link($data["url"]))) {
  435. // Delete the old entry
  436. q("DELETE FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($profile)));
  437. q("DELETE FROM `glink` WHERE `gcid` = %d", intval($gcontacts[0]["id"]));
  438. poco_check($data["url"], $data["name"], $data["network"], $data["photo"], $gcontacts[0]["about"], $gcontacts[0]["location"],
  439. $gcontacts[0]["gender"], $gcontacts[0]["keywords"], $data["addr"], $gcontacts[0]["updated"], $gcontacts[0]["generation"]);
  440. poco_last_updated($data["url"], $force);
  441. logger("Profile ".$profile." was deleted", LOGGER_DEBUG);
  442. return false;
  443. }
  444. if (($data["poll"] == "") OR (in_array($data["network"], array(NETWORK_FEED, NETWORK_PHANTOM)))) {
  445. q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
  446. dbesc(datetime_convert()), dbesc(normalise_link($profile)));
  447. logger("Profile ".$profile." wasn't reachable (profile)", LOGGER_DEBUG);
  448. return false;
  449. }
  450. $contact = array("generation" => $gcontacts[0]["generation"]);
  451. $contact = array_merge($contact, $data);
  452. $contact["server_url"] = $data["baseurl"];
  453. unset($contact["batch"]);
  454. unset($contact["poll"]);
  455. unset($contact["request"]);
  456. unset($contact["confirm"]);
  457. unset($contact["poco"]);
  458. unset($contact["priority"]);
  459. unset($contact["pubkey"]);
  460. unset($contact["baseurl"]);
  461. update_gcontact($contact);
  462. $feedret = z_fetch_url($data["poll"]);
  463. if (!$feedret["success"]) {
  464. q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
  465. dbesc(datetime_convert()), dbesc(normalise_link($profile)));
  466. logger("Profile ".$profile." wasn't reachable (no feed)", LOGGER_DEBUG);
  467. return false;
  468. }
  469. $doc = new DOMDocument();
  470. @$doc->loadXML($feedret["body"]);
  471. $xpath = new DomXPath($doc);
  472. $xpath->registerNamespace('atom', "http://www.w3.org/2005/Atom");
  473. $entries = $xpath->query('/atom:feed/atom:entry');
  474. $last_updated = "";
  475. foreach ($entries AS $entry) {
  476. $published = $xpath->query('atom:published/text()', $entry)->item(0)->nodeValue;
  477. $updated = $xpath->query('atom:updated/text()', $entry)->item(0)->nodeValue;
  478. if ($last_updated < $published)
  479. $last_updated = $published;
  480. if ($last_updated < $updated)
  481. $last_updated = $updated;
  482. }
  483. // Maybe there aren't any entries. Then check if it is a valid feed
  484. if ($last_updated == "")
  485. if ($xpath->query('/atom:feed')->length > 0)
  486. $last_updated = "0000-00-00 00:00:00";
  487. q("UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'",
  488. dbesc(dbm::date($last_updated)), dbesc(dbm::date()), dbesc(normalise_link($profile)));
  489. if (($gcontacts[0]["generation"] == 0))
  490. q("UPDATE `gcontact` SET `generation` = 9 WHERE `nurl` = '%s'",
  491. dbesc(normalise_link($profile)));
  492. logger("Profile ".$profile." was last updated at ".$last_updated, LOGGER_DEBUG);
  493. return($last_updated);
  494. }
  495. function poco_do_update($created, $updated, $last_failure, $last_contact) {
  496. $now = strtotime(datetime_convert());
  497. if ($updated > $last_contact)
  498. $contact_time = strtotime($updated);
  499. else
  500. $contact_time = strtotime($last_contact);
  501. $failure_time = strtotime($last_failure);
  502. $created_time = strtotime($created);
  503. // If there is no "created" time then use the current time
  504. if ($created_time <= 0)
  505. $created_time = $now;
  506. // If the last contact was less than 24 hours then don't update
  507. if (($now - $contact_time) < (60 * 60 * 24))
  508. return false;
  509. // If the last failure was less than 24 hours then don't update
  510. if (($now - $failure_time) < (60 * 60 * 24))
  511. return false;
  512. // If the last contact was less than a week ago and the last failure is older than a week then don't update
  513. //if ((($now - $contact_time) < (60 * 60 * 24 * 7)) AND ($contact_time > $failure_time))
  514. // return false;
  515. // If the last contact time was more than a week ago and the contact was created more than a week ago, then only try once a week
  516. if ((($now - $contact_time) > (60 * 60 * 24 * 7)) AND (($now - $created_time) > (60 * 60 * 24 * 7)) AND (($now - $failure_time) < (60 * 60 * 24 * 7)))
  517. return false;
  518. // If the last contact time was more than a month ago and the contact was created more than a month ago, then only try once a month
  519. if ((($now - $contact_time) > (60 * 60 * 24 * 30)) AND (($now - $created_time) > (60 * 60 * 24 * 30)) AND (($now - $failure_time) < (60 * 60 * 24 * 30)))
  520. return false;
  521. return true;
  522. }
  523. function poco_to_boolean($val) {
  524. if (($val == "true") OR ($val == 1))
  525. return(true);
  526. if (($val == "false") OR ($val == 0))
  527. return(false);
  528. return ($val);
  529. }
  530. function poco_detect_friendica_server($body) {
  531. $server = false;
  532. $doc = new \DOMDocument();
  533. @$doc->loadHTML($body);
  534. $xpath = new \DomXPath($doc);
  535. $list = $xpath->query("//meta[@name]");
  536. foreach ($list as $node) {
  537. $attr = array();
  538. if ($node->attributes->length) {
  539. foreach ($node->attributes as $attribute) {
  540. $attr[$attribute->name] = $attribute->value;
  541. }
  542. }
  543. if ($attr['name'] == 'generator') {
  544. $version_part = explode(" ", $attr['content']);
  545. if (count($version_part) == 2) {
  546. if (in_array($version_part[0], array("Friendika", "Friendica"))) {
  547. $server = array();
  548. $server["platform"] = $version_part[0];
  549. $server["version"] = $version_part[1];
  550. $server["network"] = NETWORK_DFRN;
  551. }
  552. }
  553. }
  554. }
  555. if (!$server) {
  556. return false;
  557. }
  558. $server["site_name"] = $xpath->evaluate($element."//head/title/text()", $context)->item(0)->nodeValue;
  559. return $server;
  560. }
  561. function poco_check_server($server_url, $network = "", $force = false) {
  562. // Unify the server address
  563. $server_url = trim($server_url, "/");
  564. $server_url = str_replace("/index.php", "", $server_url);
  565. if ($server_url == "")
  566. return false;
  567. $servers = q("SELECT * FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
  568. if ($servers) {
  569. if ($servers[0]["created"] == "0000-00-00 00:00:00")
  570. q("UPDATE `gserver` SET `created` = '%s' WHERE `nurl` = '%s'",
  571. dbesc(datetime_convert()), dbesc(normalise_link($server_url)));
  572. $poco = $servers[0]["poco"];
  573. $noscrape = $servers[0]["noscrape"];
  574. if ($network == "")
  575. $network = $servers[0]["network"];
  576. $last_contact = $servers[0]["last_contact"];
  577. $last_failure = $servers[0]["last_failure"];
  578. $version = $servers[0]["version"];
  579. $platform = $servers[0]["platform"];
  580. $site_name = $servers[0]["site_name"];
  581. $info = $servers[0]["info"];
  582. $register_policy = $servers[0]["register_policy"];
  583. if (!$force AND !poco_do_update($servers[0]["created"], "", $last_failure, $last_contact)) {
  584. logger("Use cached data for server ".$server_url, LOGGER_DEBUG);
  585. return ($last_contact >= $last_failure);
  586. }
  587. } else {
  588. $poco = "";
  589. $noscrape = "";
  590. $version = "";
  591. $platform = "";
  592. $site_name = "";
  593. $info = "";
  594. $register_policy = -1;
  595. $last_contact = "0000-00-00 00:00:00";
  596. $last_failure = "0000-00-00 00:00:00";
  597. }
  598. logger("Server ".$server_url." is outdated or unknown. Start discovery. Force: ".$force." Created: ".$servers[0]["created"]." Failure: ".$last_failure." Contact: ".$last_contact, LOGGER_DEBUG);
  599. $failure = false;
  600. $possible_failure = false;
  601. $orig_last_failure = $last_failure;
  602. $orig_last_contact = $last_contact;
  603. // Check if the page is accessible via SSL.
  604. $server_url = str_replace("http://", "https://", $server_url);
  605. $serverret = z_fetch_url($server_url."/.well-known/host-meta");
  606. // Maybe the page is unencrypted only?
  607. $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
  608. if (!$serverret["success"] OR ($serverret["body"] == "") OR (@sizeof($xmlobj) == 0) OR !is_object($xmlobj)) {
  609. $server_url = str_replace("https://", "http://", $server_url);
  610. $serverret = z_fetch_url($server_url."/.well-known/host-meta");
  611. $xmlobj = @simplexml_load_string($serverret["body"],'SimpleXMLElement',0, "http://docs.oasis-open.org/ns/xri/xrd-1.0");
  612. }
  613. if (!$serverret["success"] OR ($serverret["body"] == "") OR (sizeof($xmlobj) == 0) OR !is_object($xmlobj)) {
  614. // Workaround for bad configured servers (known nginx problem)
  615. if ($serverret["debug"]["http_code"] != "403") {
  616. $last_failure = datetime_convert();
  617. $failure = true;
  618. }
  619. $possible_failure = true;
  620. } elseif ($network == NETWORK_DIASPORA)
  621. $last_contact = datetime_convert();
  622. if (!$failure) {
  623. // Test for Diaspora
  624. $serverret = z_fetch_url($server_url);
  625. if (!$serverret["success"] OR ($serverret["body"] == "")) {
  626. $last_failure = datetime_convert();
  627. $failure = true;
  628. } else {
  629. $lines = explode("\n",$serverret["header"]);
  630. if(count($lines)) {
  631. foreach($lines as $line) {
  632. $line = trim($line);
  633. if(stristr($line,'X-Diaspora-Version:')) {
  634. $platform = "Diaspora";
  635. $version = trim(str_replace("X-Diaspora-Version:", "", $line));
  636. $version = trim(str_replace("x-diaspora-version:", "", $version));
  637. $network = NETWORK_DIASPORA;
  638. $versionparts = explode("-", $version);
  639. $version = $versionparts[0];
  640. $last_contact = datetime_convert();
  641. }
  642. if(stristr($line,'Server: Mastodon')) {
  643. $platform = "Mastodon";
  644. $network = NETWORK_OSTATUS;
  645. // Mastodon doesn't reveal version numbers
  646. $version = "";
  647. $last_contact = datetime_convert();
  648. }
  649. }
  650. }
  651. $friendica_server = poco_detect_friendica_server($serverret["body"]);
  652. if ($friendica_server) {
  653. $platform = $friendica_server['platform'];
  654. $network = $friendica_server['network'];
  655. $version = $friendica_server['version'];
  656. $site_name = $friendica_server['site_name'];
  657. $last_contact = datetime_convert();
  658. }
  659. }
  660. }
  661. if (!$failure) {
  662. // Test for Statusnet
  663. // Will also return data for Friendica and GNU Social - but it will be overwritten later
  664. // The "not implemented" is a special treatment for really, really old Friendica versions
  665. $serverret = z_fetch_url($server_url."/api/statusnet/version.json");
  666. if ($serverret["success"] AND ($serverret["body"] != '{"error":"not implemented"}') AND
  667. ($serverret["body"] != '') AND (strlen($serverret["body"]) < 30)) {
  668. $platform = "StatusNet";
  669. $version = trim($serverret["body"], '"');
  670. $network = NETWORK_OSTATUS;
  671. $last_contact = datetime_convert();
  672. }
  673. // Test for GNU Social
  674. $serverret = z_fetch_url($server_url."/api/gnusocial/version.json");
  675. if ($serverret["success"] AND ($serverret["body"] != '{"error":"not implemented"}') AND
  676. ($serverret["body"] != '') AND (strlen($serverret["body"]) < 30)) {
  677. $platform = "GNU Social";
  678. $version = trim($serverret["body"], '"');
  679. $network = NETWORK_OSTATUS;
  680. $last_contact = datetime_convert();
  681. }
  682. $serverret = z_fetch_url($server_url."/api/statusnet/config.json");
  683. if ($serverret["success"]) {
  684. $data = json_decode($serverret["body"]);
  685. if (isset($data->site->server)) {
  686. $last_contact = datetime_convert();
  687. if (isset($data->site->hubzilla)) {
  688. $platform = $data->site->hubzilla->PLATFORM_NAME;
  689. $version = $data->site->hubzilla->RED_VERSION;
  690. $network = NETWORK_DIASPORA;
  691. }
  692. if (isset($data->site->redmatrix)) {
  693. if (isset($data->site->redmatrix->PLATFORM_NAME))
  694. $platform = $data->site->redmatrix->PLATFORM_NAME;
  695. elseif (isset($data->site->redmatrix->RED_PLATFORM))
  696. $platform = $data->site->redmatrix->RED_PLATFORM;
  697. $version = $data->site->redmatrix->RED_VERSION;
  698. $network = NETWORK_DIASPORA;
  699. }
  700. if (isset($data->site->friendica)) {
  701. $platform = $data->site->friendica->FRIENDICA_PLATFORM;
  702. $version = $data->site->friendica->FRIENDICA_VERSION;
  703. $network = NETWORK_DFRN;
  704. }
  705. $site_name = $data->site->name;
  706. $data->site->closed = poco_to_boolean($data->site->closed);
  707. $data->site->private = poco_to_boolean($data->site->private);
  708. $data->site->inviteonly = poco_to_boolean($data->site->inviteonly);
  709. if (!$data->site->closed AND !$data->site->private and $data->site->inviteonly)
  710. $register_policy = REGISTER_APPROVE;
  711. elseif (!$data->site->closed AND !$data->site->private)
  712. $register_policy = REGISTER_OPEN;
  713. else
  714. $register_policy = REGISTER_CLOSED;
  715. }
  716. }
  717. }
  718. // Query statistics.json. Optional package for Diaspora, Friendica and Redmatrix
  719. if (!$failure) {
  720. $serverret = z_fetch_url($server_url."/statistics.json");
  721. if ($serverret["success"]) {
  722. $data = json_decode($serverret["body"]);
  723. if ($version == "")
  724. $version = $data->version;
  725. $site_name = $data->name;
  726. if (isset($data->network) AND ($platform == ""))
  727. $platform = $data->network;
  728. if ($platform == "Diaspora")
  729. $network = NETWORK_DIASPORA;
  730. if ($data->registrations_open)
  731. $register_policy = REGISTER_OPEN;
  732. else
  733. $register_policy = REGISTER_CLOSED;
  734. if (isset($data->version))
  735. $last_contact = datetime_convert();
  736. }
  737. }
  738. // Check for noscrape
  739. // Friendica servers could be detected as OStatus servers
  740. if (!$failure AND in_array($network, array(NETWORK_DFRN, NETWORK_OSTATUS))) {
  741. $serverret = z_fetch_url($server_url."/friendica/json");
  742. if (!$serverret["success"])
  743. $serverret = z_fetch_url($server_url."/friendika/json");
  744. if ($serverret["success"]) {
  745. $data = json_decode($serverret["body"]);
  746. if (isset($data->version)) {
  747. $last_contact = datetime_convert();
  748. $network = NETWORK_DFRN;
  749. $noscrape = $data->no_scrape_url;
  750. $version = $data->version;
  751. $site_name = $data->site_name;
  752. $info = $data->info;
  753. $register_policy_str = $data->register_policy;
  754. $platform = $data->platform;
  755. switch ($register_policy_str) {
  756. case "REGISTER_CLOSED":
  757. $register_policy = REGISTER_CLOSED;
  758. break;
  759. case "REGISTER_APPROVE":
  760. $register_policy = REGISTER_APPROVE;
  761. break;
  762. case "REGISTER_OPEN":
  763. $register_policy = REGISTER_OPEN;
  764. break;
  765. }
  766. }
  767. }
  768. }
  769. // Look for poco
  770. if (!$failure) {
  771. $serverret = z_fetch_url($server_url."/poco");
  772. if ($serverret["success"]) {
  773. $data = json_decode($serverret["body"]);
  774. if (isset($data->totalResults)) {
  775. $poco = $server_url."/poco";
  776. $last_contact = datetime_convert();
  777. }
  778. }
  779. }
  780. if ($possible_failure AND !$failure) {
  781. $last_failure = datetime_convert();
  782. $failure = true;
  783. }
  784. if ($failure) {
  785. $last_contact = $orig_last_contact;
  786. } else {
  787. $last_failure = $orig_last_failure;
  788. }
  789. if (($last_contact <= $last_failure) AND !$failure) {
  790. logger("Server ".$server_url." seems to be alive, but last contact wasn't set - could be a bug", LOGGER_DEBUG);
  791. } else if (($last_contact >= $last_failure) AND $failure) {
  792. logger("Server ".$server_url." seems to be dead, but last failure wasn't set - could be a bug", LOGGER_DEBUG);
  793. }
  794. // Check again if the server exists
  795. $servers = q("SELECT `nurl` FROM `gserver` WHERE `nurl` = '%s'", dbesc(normalise_link($server_url)));
  796. $version = strip_tags($version);
  797. $site_name = strip_tags($site_name);
  798. $info = strip_tags($info);
  799. $platform = strip_tags($platform);
  800. if ($servers)
  801. q("UPDATE `gserver` SET `url` = '%s', `version` = '%s', `site_name` = '%s', `info` = '%s', `register_policy` = %d, `poco` = '%s', `noscrape` = '%s',
  802. `network` = '%s', `platform` = '%s', `last_contact` = '%s', `last_failure` = '%s' WHERE `nurl` = '%s'",
  803. dbesc($server_url),
  804. dbesc($version),
  805. dbesc($site_name),
  806. dbesc($info),
  807. intval($register_policy),
  808. dbesc($poco),
  809. dbesc($noscrape),
  810. dbesc($network),
  811. dbesc($platform),
  812. dbesc($last_contact),
  813. dbesc($last_failure),
  814. dbesc(normalise_link($server_url))
  815. );
  816. else
  817. q("INSERT INTO `gserver` (`url`, `nurl`, `version`, `site_name`, `info`, `register_policy`, `poco`, `noscrape`, `network`, `platform`, `created`, `last_contact`, `last_failure`)
  818. VALUES ('%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s')",
  819. dbesc($server_url),
  820. dbesc(normalise_link($server_url)),
  821. dbesc($version),
  822. dbesc($site_name),
  823. dbesc($info),
  824. intval($register_policy),
  825. dbesc($poco),
  826. dbesc($noscrape),
  827. dbesc($network),
  828. dbesc($platform),
  829. dbesc(datetime_convert()),
  830. dbesc($last_contact),
  831. dbesc($last_failure),
  832. dbesc(datetime_convert())
  833. );
  834. logger("End discovery for server ".$server_url, LOGGER_DEBUG);
  835. return !$failure;
  836. }
  837. function count_common_friends($uid,$cid) {
  838. $r = q("SELECT count(*) as `total`
  839. FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  840. WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
  841. ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
  842. AND `gcontact`.`nurl` IN (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 and id != %d ) ",
  843. intval($cid),
  844. intval($uid),
  845. intval($uid),
  846. intval($cid)
  847. );
  848. // logger("count_common_friends: $uid $cid {$r[0]['total']}");
  849. if (dbm::is_result($r))
  850. return $r[0]['total'];
  851. return 0;
  852. }
  853. function common_friends($uid,$cid,$start = 0,$limit=9999,$shuffle = false) {
  854. if($shuffle)
  855. $sql_extra = " order by rand() ";
  856. else
  857. $sql_extra = " order by `gcontact`.`name` asc ";
  858. $r = q("SELECT `gcontact`.*, `contact`.`id` AS `cid`
  859. FROM `glink`
  860. INNER JOIN `gcontact` ON `glink`.`gcid` = `gcontact`.`id`
  861. INNER JOIN `contact` ON `gcontact`.`nurl` = `contact`.`nurl`
  862. WHERE `glink`.`cid` = %d and `glink`.`uid` = %d
  863. AND `contact`.`uid` = %d AND `contact`.`self` = 0 AND `contact`.`blocked` = 0
  864. AND `contact`.`hidden` = 0 AND `contact`.`id` != %d
  865. AND ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
  866. $sql_extra LIMIT %d, %d",
  867. intval($cid),
  868. intval($uid),
  869. intval($uid),
  870. intval($cid),
  871. intval($start),
  872. intval($limit)
  873. );
  874. return $r;
  875. }
  876. function count_common_friends_zcid($uid,$zcid) {
  877. $r = q("SELECT count(*) as `total`
  878. FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  879. where `glink`.`zcid` = %d
  880. and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 ) ",
  881. intval($zcid),
  882. intval($uid)
  883. );
  884. if (dbm::is_result($r))
  885. return $r[0]['total'];
  886. return 0;
  887. }
  888. function common_friends_zcid($uid,$zcid,$start = 0, $limit = 9999,$shuffle = false) {
  889. if($shuffle)
  890. $sql_extra = " order by rand() ";
  891. else
  892. $sql_extra = " order by `gcontact`.`name` asc ";
  893. $r = q("SELECT `gcontact`.*
  894. FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  895. where `glink`.`zcid` = %d
  896. and `gcontact`.`nurl` in (select nurl from contact where uid = %d and self = 0 and blocked = 0 and hidden = 0 )
  897. $sql_extra limit %d, %d",
  898. intval($zcid),
  899. intval($uid),
  900. intval($start),
  901. intval($limit)
  902. );
  903. return $r;
  904. }
  905. function count_all_friends($uid,$cid) {
  906. $r = q("SELECT count(*) as `total`
  907. FROM `glink` INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  908. where `glink`.`cid` = %d and `glink`.`uid` = %d AND
  909. ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))",
  910. intval($cid),
  911. intval($uid)
  912. );
  913. if (dbm::is_result($r))
  914. return $r[0]['total'];
  915. return 0;
  916. }
  917. function all_friends($uid,$cid,$start = 0, $limit = 80) {
  918. $r = q("SELECT `gcontact`.*, `contact`.`id` AS `cid`
  919. FROM `glink`
  920. INNER JOIN `gcontact` on `glink`.`gcid` = `gcontact`.`id`
  921. LEFT JOIN `contact` ON `contact`.`nurl` = `gcontact`.`nurl` AND `contact`.`uid` = %d
  922. WHERE `glink`.`cid` = %d AND `glink`.`uid` = %d AND
  923. ((`gcontact`.`last_contact` >= `gcontact`.`last_failure`) OR (`gcontact`.`updated` >= `gcontact`.`last_failure`))
  924. ORDER BY `gcontact`.`name` ASC LIMIT %d, %d ",
  925. intval($uid),
  926. intval($cid),
  927. intval($uid),
  928. intval($start),
  929. intval($limit)
  930. );
  931. return $r;
  932. }
  933. function suggestion_query($uid, $start = 0, $limit = 80) {
  934. if (!$uid) {
  935. return array();
  936. }
  937. // Uncommented because the result of the queries are to big to store it in the cache.
  938. // We need to decide if we want to change the db column type or if we want to delete it.
  939. // $list = Cache::get("suggestion_query:".$uid.":".$start.":".$limit);
  940. // if (!is_null($list)) {
  941. // return $list;
  942. // }
  943. $network = array(NETWORK_DFRN);
  944. if (get_config('system','diaspora_enabled'))
  945. $network[] = NETWORK_DIASPORA;
  946. if (!get_config('system','ostatus_disabled'))
  947. $network[] = NETWORK_OSTATUS;
  948. $sql_network = implode("', '", $network);
  949. $sql_network = "'".$sql_network."'";
  950. /// @todo This query is really slow
  951. // By now we cache the data for five minutes
  952. $r = q("SELECT count(glink.gcid) as `total`, gcontact.* from gcontact
  953. INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
  954. where uid = %d and not gcontact.nurl in ( select nurl from contact where uid = %d )
  955. AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
  956. AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
  957. AND `gcontact`.`updated` != '0000-00-00 00:00:00'
  958. AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
  959. AND `gcontact`.`network` IN (%s)
  960. GROUP BY `glink`.`gcid` ORDER BY `gcontact`.`updated` DESC,`total` DESC LIMIT %d, %d",
  961. intval($uid),
  962. intval($uid),
  963. intval($uid),
  964. intval($uid),
  965. $sql_network,
  966. intval($start),
  967. intval($limit)
  968. );
  969. if (dbm::is_result($r) && count($r) >= ($limit -1)) {
  970. // Uncommented because the result of the queries are to big to store it in the cache.
  971. // We need to decide if we want to change the db column type or if we want to delete it.
  972. // Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $r, CACHE_FIVE_MINUTES);
  973. return $r;
  974. }
  975. $r2 = q("SELECT gcontact.* FROM gcontact
  976. INNER JOIN `glink` ON `glink`.`gcid` = `gcontact`.`id`
  977. WHERE `glink`.`uid` = 0 AND `glink`.`cid` = 0 AND `glink`.`zcid` = 0 AND NOT `gcontact`.`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = %d)
  978. AND NOT `gcontact`.`name` IN (SELECT `name` FROM `contact` WHERE `uid` = %d)
  979. AND NOT `gcontact`.`id` IN (SELECT `gcid` FROM `gcign` WHERE `uid` = %d)
  980. AND `gcontact`.`updated` != '0000-00-00 00:00:00'
  981. AND `gcontact`.`last_contact` >= `gcontact`.`last_failure`
  982. AND `gcontact`.`network` IN (%s)
  983. ORDER BY rand() LIMIT %d, %d",
  984. intval($uid),
  985. intval($uid),
  986. intval($uid),
  987. $sql_network,
  988. intval($start),
  989. intval($limit)
  990. );
  991. $list = array();
  992. foreach ($r2 AS $suggestion)
  993. $list[$suggestion["nurl"]] = $suggestion;
  994. foreach ($r AS $suggestion)
  995. $list[$suggestion["nurl"]] = $suggestion;
  996. while (sizeof($list) > ($limit))
  997. array_pop($list);
  998. // Uncommented because the result of the queries are to big to store it in the cache.
  999. // We need to decide if we want to change the db column type or if we want to delete it.
  1000. // Cache::set("suggestion_query:".$uid.":".$start.":".$limit, $list, CACHE_FIVE_MINUTES);
  1001. return $list;
  1002. }
  1003. function update_suggestions() {
  1004. $a = get_app();
  1005. $done = array();
  1006. /// @TODO Check if it is really neccessary to poll the own server
  1007. poco_load(0,0,0,App::get_baseurl() . '/poco');
  1008. $done[] = App::get_baseurl() . '/poco';
  1009. if (strlen(get_config('system','directory'))) {
  1010. $x = fetch_url(get_server()."/pubsites");
  1011. if ($x) {
  1012. $j = json_decode($x);
  1013. if ($j->entries) {
  1014. foreach ($j->entries as $entry) {
  1015. poco_check_server($entry->url);
  1016. $url = $entry->url . '/poco';
  1017. if (! in_array($url,$done)) {
  1018. poco_load(0,0,0,$entry->url . '/poco');
  1019. }
  1020. }
  1021. }
  1022. }
  1023. }
  1024. // Query your contacts from Friendica and Redmatrix/Hubzilla for their contacts
  1025. $r = q("SELECT DISTINCT(`poco`) AS `poco` FROM `contact` WHERE `network` IN ('%s', '%s')",
  1026. dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA)
  1027. );
  1028. if (dbm::is_result($r)) {
  1029. foreach ($r as $rr) {
  1030. $base = substr($rr['poco'],0,strrpos($rr['poco'],'/'));
  1031. if(! in_array($base,$done))
  1032. poco_load(0,0,0,$base);
  1033. }
  1034. }
  1035. }
  1036. function poco_discover_federation() {
  1037. $last = get_config('poco','last_federation_discovery');
  1038. if ($last) {
  1039. $next = $last + (24 * 60 * 60);
  1040. if($next > time())
  1041. return;
  1042. }
  1043. // Discover Friendica, Hubzilla and Diaspora servers
  1044. $serverdata = fetch_url("http://the-federation.info/pods.json");
  1045. if ($serverdata) {
  1046. $servers = json_decode($serverdata);
  1047. foreach($servers->pods AS $server)
  1048. poco_check_server("https://".$server->host);
  1049. }
  1050. // Discover GNU Social Servers
  1051. if (!get_config('system','ostatus_disabled')) {
  1052. $serverdata = "http://gstools.org/api/get_open_instances/";
  1053. $result = z_fetch_url($serverdata);
  1054. if ($result["success"]) {
  1055. $servers = json_decode($result["body"]);
  1056. foreach($servers->data AS $server)
  1057. poco_check_server($server->instance_address);
  1058. }
  1059. }
  1060. set_config('poco','last_federation_discovery', time());
  1061. }
  1062. function poco_discover($complete = false) {
  1063. // Update the server list
  1064. poco_discover_federation();
  1065. $no_of_queries = 5;
  1066. $requery_days = intval(get_config("system", "poco_requery_days"));
  1067. if ($requery_days == 0)
  1068. $requery_days = 7;
  1069. $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
  1070. $r = q("SELECT `poco`, `nurl`, `url`, `network` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `poco` != '' AND `last_poco_query` < '%s' ORDER BY RAND()", dbesc($last_update));
  1071. if ($r)
  1072. foreach ($r AS $server) {
  1073. if (!poco_check_server($server["url"], $server["network"])) {
  1074. // The server is not reachable? Okay, then we will try it later
  1075. q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
  1076. continue;
  1077. }
  1078. // Fetch all users from the other server
  1079. $url = $server["poco"]."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
  1080. logger("Fetch all users from the server ".$server["nurl"], LOGGER_DEBUG);
  1081. $retdata = z_fetch_url($url);
  1082. if ($retdata["success"]) {
  1083. $data = json_decode($retdata["body"]);
  1084. poco_discover_server($data, 2);
  1085. if (get_config('system','poco_discovery') > 1) {
  1086. $timeframe = get_config('system','poco_discovery_since');
  1087. if ($timeframe == 0)
  1088. $timeframe = 30;
  1089. $updatedSince = date("Y-m-d H:i:s", time() - $timeframe * 86400);
  1090. // Fetch all global contacts from the other server (Not working with Redmatrix and Friendica versions before 3.3)
  1091. $url = $server["poco"]."/@global?updatedSince=".$updatedSince."&fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
  1092. $success = false;
  1093. $retdata = z_fetch_url($url);
  1094. if ($retdata["success"]) {
  1095. logger("Fetch all global contacts from the server ".$server["nurl"], LOGGER_DEBUG);
  1096. $success = poco_discover_server(json_decode($retdata["body"]));
  1097. }
  1098. if (!$success AND (get_config('system','poco_discovery') > 2)) {
  1099. logger("Fetch contacts from users of the server ".$server["nurl"], LOGGER_DEBUG);
  1100. poco_discover_server_users($data, $server);
  1101. }
  1102. }
  1103. q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
  1104. if (!$complete AND (--$no_of_queries == 0))
  1105. break;
  1106. } else {
  1107. // If the server hadn't replied correctly, then force a sanity check
  1108. poco_check_server($server["url"], $server["network"], true);
  1109. // If we couldn't reach the server, we will try it some time later
  1110. q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
  1111. }
  1112. }
  1113. }
  1114. function poco_discover_server_users($data, $server) {
  1115. if (!isset($data->entry))
  1116. return;
  1117. foreach ($data->entry AS $entry) {
  1118. $username = "";
  1119. if (isset($entry->urls)) {
  1120. foreach($entry->urls as $url)
  1121. if ($url->type == 'profile') {
  1122. $profile_url = $url->value;
  1123. $urlparts = parse_url($profile_url);
  1124. $username = end(explode("/", $urlparts["path"]));
  1125. }
  1126. }
  1127. if ($username != "") {
  1128. logger("Fetch contacts for the user ".$username." from the server ".$server["nurl"], LOGGER_DEBUG);
  1129. // Fetch all contacts from a given user from the other server
  1130. $url = $server["poco"]."/".$username."/?fields=displayName,urls,photos,updated,network,aboutMe,currentLocation,tags,gender,contactType,generation";
  1131. $retdata = z_fetch_url($url);
  1132. if ($retdata["success"])
  1133. poco_discover_server(json_decode($retdata["body"]), 3);
  1134. }
  1135. }
  1136. }
  1137. function poco_discover_server($data, $default_generation = 0) {
  1138. if (!isset($data->entry) OR !count($data->entry))
  1139. return false;
  1140. $success = false;
  1141. foreach ($data->entry AS $entry) {
  1142. $profile_url = '';
  1143. $profile_photo = '';
  1144. $connect_url = '';
  1145. $name = '';
  1146. $network = '';
  1147. $updated = '0000-00-00 00:00:00';
  1148. $location = '';
  1149. $about = '';
  1150. $keywords = '';
  1151. $gender = '';
  1152. $contact_type = -1;
  1153. $generation = $default_generation;
  1154. $name = $entry->displayName;
  1155. if (isset($entry->urls)) {
  1156. foreach($entry->urls as $url) {
  1157. if ($url->type == 'profile') {
  1158. $profile_url = $url->value;
  1159. continue;
  1160. }
  1161. if ($url->type == 'webfinger') {
  1162. $connect_url = str_replace('acct:' , '', $url->value);
  1163. continue;
  1164. }
  1165. }
  1166. }
  1167. if (isset($entry->photos)) {
  1168. foreach ($entry->photos as $photo) {
  1169. if ($photo->type == 'profile') {
  1170. $profile_photo = $photo->value;
  1171. continue;
  1172. }
  1173. }
  1174. }
  1175. if (isset($entry->updated)) {
  1176. $updated = date("Y-m-d H:i:s", strtotime($entry->updated));
  1177. }
  1178. if(isset($entry->network)) {
  1179. $network = $entry->network;
  1180. }
  1181. if(isset($entry->currentLocation)) {
  1182. $location = $entry->currentLocation;
  1183. }
  1184. if(isset($entry->aboutMe)) {
  1185. $about = html2bbcode($entry->aboutMe);
  1186. }
  1187. if(isset($entry->gender)) {
  1188. $gender = $entry->gender;
  1189. }
  1190. if(isset($entry->generation) AND ($entry->generation > 0)) {
  1191. $generation = ++$entry->generation;
  1192. }
  1193. if(isset($entry->contactType) AND ($entry->contactType >= 0)) {
  1194. $contact_type = $entry->contactType;
  1195. }
  1196. if(isset($entry->tags)) {
  1197. foreach ($entry->tags as $tag) {
  1198. $keywords = implode(", ", $tag);
  1199. }
  1200. }
  1201. if ($generation > 0) {
  1202. $success = true;
  1203. logger("Store profile ".$profile_url, LOGGER_DEBUG);
  1204. poco_check($profile_url, $name, $network, $profile_photo, $about, $location, $gender, $keywords, $connect_url, $updated, $generation, 0, 0, 0);
  1205. $gcontact = array("url" => $profile_url, "contact-type" => $contact_type, "generation" => $generation);
  1206. update_gcontact($gcontact);
  1207. logger("Done for profile ".$profile_url, LOGGER_DEBUG);
  1208. }
  1209. }
  1210. return $success;
  1211. }
  1212. /**
  1213. * @brief Removes unwanted parts from a contact url
  1214. *
  1215. * @param string $url Contact url
  1216. * @return string Contact url with the wanted parts
  1217. */
  1218. function clean_contact_url($url) {
  1219. $parts = parse_url($url);
  1220. if (!isset($parts["scheme"]) OR !isset($parts["host"]))
  1221. return $url;
  1222. $new_url = $parts["scheme"]."://".$parts["host"];
  1223. if (isset($parts["port"]))
  1224. $new_url .= ":".$parts["port"];
  1225. if (isset($parts["path"]))
  1226. $new_url .= $parts["path"];
  1227. if ($new_url != $url)
  1228. logger("Cleaned contact url ".$url." to ".$new_url." - Called by: ".App::callstack(), LOGGER_DEBUG);
  1229. return $new_url;
  1230. }
  1231. /**
  1232. * @brief Replace alternate OStatus user format with the primary one
  1233. *
  1234. * @param arr $contact contact array (called by reference)
  1235. */
  1236. function fix_alternate_contact_address(&$contact) {
  1237. if (($contact["network"] == NETWORK_OSTATUS) AND poco_alternate_ostatus_url($contact["url"])) {
  1238. $data = probe_url($contact["url"]);
  1239. if ($contact["network"] == NETWORK_OSTATUS) {
  1240. logger("Fix primary url from ".$contact["url"]." to ".$data["url"]." - Called by: ".App::callstack(), LOGGER_DEBUG);
  1241. $contact["url"] = $data["url"];
  1242. $contact["addr"] = $data["addr"];
  1243. $contact["alias"] = $data["alias"];
  1244. $contact["server_url"] = $data["baseurl"];
  1245. }
  1246. }
  1247. }
  1248. /**
  1249. * @brief Fetch the gcontact id, add an entry if not existed
  1250. *
  1251. * @param arr $contact contact array
  1252. * @return bool|int Returns false if not found, integer if contact was found
  1253. */
  1254. function get_gcontact_id($contact) {
  1255. $gcontact_id = 0;
  1256. $doprobing = false;
  1257. if (in_array($contact["network"], array(NETWORK_PHANTOM))) {
  1258. logger("Invalid network for contact url ".$contact["url"]." - Called by: ".App::callstack(), LOGGER_DEBUG);
  1259. return false;
  1260. }
  1261. if ($contact["network"] == NETWORK_STATUSNET)
  1262. $contact["network"] = NETWORK_OSTATUS;
  1263. // All new contacts are hidden by default
  1264. if (!isset($contact["hide"]))
  1265. $contact["hide"] = true;
  1266. // Replace alternate OStatus user format with the primary one
  1267. fix_alternate_contact_address($contact);
  1268. // Remove unwanted parts from the contact url (e.g. "?zrl=...")
  1269. if (in_array($contact["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS)))
  1270. $contact["url"] = clean_contact_url($contact["url"]);
  1271. $r = q("SELECT `id`, `last_contact`, `last_failure`, `network` FROM `gcontact` WHERE `nurl` = '%s' LIMIT 2",
  1272. dbesc(normalise_link($contact["url"])));
  1273. if ($r) {
  1274. $gcontact_id = $r[0]["id"];
  1275. // Update every 90 days
  1276. if (in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""))) {
  1277. $last_failure_str = $r[0]["last_failure"];
  1278. $last_failure = strtotime($r[0]["last_failure"]);
  1279. $last_contact_str = $r[0]["last_contact"];
  1280. $last_contact = strtotime($r[0]["last_contact"]);
  1281. $doprobing = (((time() - $last_contact) > (90 * 86400)) AND ((time() - $last_failure) > (90 * 86400)));
  1282. }
  1283. } else {
  1284. q("INSERT INTO `gcontact` (`name`, `nick`, `addr` , `network`, `url`, `nurl`, `photo`, `created`, `updated`, `location`, `about`, `hide`, `generation`)
  1285. VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d)",
  1286. dbesc($contact["name"]),
  1287. dbesc($contact["nick"]),
  1288. dbesc($contact["addr"]),
  1289. dbesc($contact["network"]),
  1290. dbesc($contact["url"]),
  1291. dbesc(normalise_link($contact["url"])),
  1292. dbesc($contact["photo"]),
  1293. dbesc(datetime_convert()),
  1294. dbesc(datetime_convert()),
  1295. dbesc($contact["location"]),
  1296. dbesc($contact["about"]),
  1297. intval($contact["hide"]),
  1298. intval($contact["generation"])
  1299. );
  1300. $r = q("SELECT `id`, `network` FROM `gcontact` WHERE `nurl` = '%s' ORDER BY `id` LIMIT 2",
  1301. dbesc(normalise_link($contact["url"])));
  1302. if ($r) {
  1303. $gcontact_id = $r[0]["id"];
  1304. $doprobing = in_array($r[0]["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS, ""));
  1305. }
  1306. }
  1307. if ($doprobing) {
  1308. logger("Last Contact: ". $last_contact_str." - Last Failure: ".$last_failure_str." - Checking: ".$contact["url"], LOGGER_DEBUG);
  1309. proc_run(PRIORITY_LOW, 'include/gprobe.php', bin2hex($contact["url"]));
  1310. }
  1311. if ((dbm::is_result($r)) AND (count($r) > 1) AND ($gcontact_id > 0) AND ($contact["url"] != ""))
  1312. q("DELETE FROM `gcontact` WHERE `nurl` = '%s' AND `id` != %d",
  1313. dbesc(normalise_link($contact["url"])),
  1314. intval($gcontact_id));
  1315. return $gcontact_id;
  1316. }
  1317. /**
  1318. * @brief Updates the gcontact table from a given array
  1319. *
  1320. * @param arr $contact contact array
  1321. * @return bool|int Returns false if not found, integer if contact was found
  1322. */
  1323. function update_gcontact($contact) {
  1324. // Check for invalid "contact-type" value
  1325. if (isset($contact['contact-type']) AND (intval($contact['contact-type']) < 0)) {
  1326. $contact['contact-type'] = 0;
  1327. }
  1328. /// @todo update contact table as well
  1329. $gcontact_id = get_gcontact_id($contact);
  1330. if (!$gcontact_id)
  1331. return false;
  1332. $r = q("SELECT `name`, `nick`, `photo`, `location`, `about`, `addr`, `generation`, `birthday`, `gender`, `keywords`,
  1333. `contact-type`, `hide`, `nsfw`, `network`, `alias`, `notify`, `server_url`, `connect`, `updated`, `url`
  1334. FROM `gcontact` WHERE `id` = %d LIMIT 1",
  1335. intval($gcontact_id));
  1336. // Get all field names
  1337. $fields = array();
  1338. foreach ($r[0] AS $field => $data)
  1339. $fields[$field] = $data;
  1340. unset($fields["url"]);
  1341. unset($fields["updated"]);
  1342. unset($fields["hide"]);
  1343. // Bugfix: We had an error in the storing of keywords which lead to the "0"
  1344. // This value is still transmitted via poco.
  1345. if ($contact["keywords"] == "0")
  1346. unset($contact["keywords"]);
  1347. if ($r[0]["keywords"] == "0")
  1348. $r[0]["keywords"] = "";
  1349. // assign all unassigned fields from the database entry
  1350. foreach ($fields AS $field => $data)
  1351. if (!isset($contact[$field]) OR ($contact[$field] == ""))
  1352. $contact[$field] = $r[0][$field];
  1353. if (!isset($contact["hide"]))
  1354. $contact["hide"] = $r[0]["hide"];
  1355. $fields["hide"] = $r[0]["hide"];
  1356. if ($contact["network"] == NETWORK_STATUSNET)
  1357. $contact["network"] = NETWORK_OSTATUS;
  1358. // Replace alternate OStatus user format with the primary one
  1359. fix_alternate_contact_address($contact);
  1360. if (!isset($contact["updated"]))
  1361. $contact["updated"] = datetime_convert();
  1362. if ($contact["server_url"] == "") {
  1363. $server_url = $contact["url"];
  1364. $server_url = matching_url($server_url, $contact["alias"]);
  1365. if ($server_url != "")
  1366. $contact["server_url"] = $server_url;
  1367. $server_url = matching_url($server_url, $contact["photo"]);
  1368. if ($server_url != "")
  1369. $contact["server_url"] = $server_url;
  1370. $server_url = matching_url($server_url, $contact["notify"]);
  1371. if ($server_url != "")
  1372. $contact["server_url"] = $server_url;
  1373. } else
  1374. $contact["server_url"] = normalise_link($contact["server_url"]);
  1375. if (($contact["addr"] == "") AND ($contact["server_url"] != "") AND ($contact["nick"] != "")) {
  1376. $hostname = str_replace("http://", "", $contact["server_url"]);
  1377. $contact["addr"] = $contact["nick"]."@".$hostname;
  1378. }
  1379. // Check if any field changed
  1380. $update = false;
  1381. unset($fields["generation"]);
  1382. if ((($contact["generation"] > 0) AND ($contact["generation"] <= $r[0]["generation"])) OR ($r[0]["generation"] == 0)) {
  1383. foreach ($fields AS $field => $data)
  1384. if ($contact[$field] != $r[0][$field]) {
  1385. logger("Difference for contact ".$contact["url"]." in field '".$field."'. New value: '".$contact[$field]."', old value '".$r[0][$field]."'", LOGGER_DEBUG);
  1386. $update = true;
  1387. }
  1388. if ($contact["generation"] < $r[0]["generation"]) {
  1389. logger("Difference for contact ".$contact["url"]." in field 'generation'. new value: '".$contact["generation"]."', old value '".$r[0]["generation"]."'", LOGGER_DEBUG);
  1390. $update = true;
  1391. }
  1392. }
  1393. if ($update) {
  1394. logger("Update gcontact for ".$contact["url"], LOGGER_DEBUG);
  1395. q("UPDATE `gcontact` SET `photo` = '%s', `name` = '%s', `nick` = '%s', `addr` = '%s', `network` = '%s',
  1396. `birthday` = '%s', `gender` = '%s', `keywords` = '%s', `hide` = %d, `nsfw` = %d,
  1397. `contact-type` = %d, `alias` = '%s', `notify` = '%s', `url` = '%s',
  1398. `location` = '%s', `about` = '%s', `generation` = %d, `updated` = '%s',
  1399. `server_url` = '%s', `connect` = '%s'
  1400. WHERE `nurl` = '%s' AND (`generation` = 0 OR `generation` >= %d)",
  1401. dbesc($contact["photo"]), dbesc($contact["name"]), dbesc($contact["nick"]),
  1402. dbesc($contact["addr"]), dbesc($contact["network"]), dbesc($contact["birthday"]),
  1403. dbesc($contact["gender"]), dbesc($contact["keywords"]), intval($contact["hide"]),
  1404. intval($contact["nsfw"]), intval($contact["contact-type"]), dbesc($contact["alias"]),
  1405. dbesc($contact["notify"]), dbesc($contact["url"]), dbesc($contact["location"]),
  1406. dbesc($contact["about"]), intval($contact["generation"]), dbesc($contact["updated"]),
  1407. dbesc($contact["server_url"]), dbesc($contact["connect"]),
  1408. dbesc(normalise_link($contact["url"])), intval($contact["generation"]));
  1409. // Now update the contact entry with the user id "0" as well.
  1410. // This is used for the shadow copies of public items.
  1411. $r = q("SELECT `id` FROM `contact` WHERE `nurl` = '%s' AND `uid` = 0 ORDER BY `id` LIMIT 1",
  1412. dbesc(normalise_link($contact["url"])));
  1413. if ($r) {
  1414. logger("Update shadow contact ".$r[0]["id"], LOGGER_DEBUG);
  1415. update_contact_avatar($contact["photo"], 0, $r[0]["id"]);
  1416. q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `addr` = '%s',
  1417. `network` = '%s', `bd` = '%s', `gender` = '%s',
  1418. `keywords` = '%s', `alias` = '%s', `contact-type` = %d,
  1419. `url` = '%s', `location` = '%s', `about` = '%s'
  1420. WHERE `id` = %d",
  1421. dbesc($contact["name"]), dbesc($contact["nick"]), dbesc($contact["addr"]),
  1422. dbesc($contact["network"]), dbesc($contact["birthday"]), dbesc($contact["gender"]),
  1423. dbesc($contact["keywords"]), dbesc($contact["alias"]), intval($contact["contact-type"]),
  1424. dbesc($contact["url"]), dbesc($contact["location"]), dbesc($contact["about"]),
  1425. intval($r[0]["id"]));
  1426. }
  1427. }
  1428. return $gcontact_id;
  1429. }
  1430. /**
  1431. * @brief Updates the gcontact entry from probe
  1432. *
  1433. * @param str $url profile link
  1434. */
  1435. function update_gcontact_from_probe($url) {
  1436. $data = probe_url($url);
  1437. if (in_array($data["network"], array(NETWORK_PHANTOM))) {
  1438. logger("Invalid network for contact url ".$data["url"]." - Called by: ".App::callstack(), LOGGER_DEBUG);
  1439. return;
  1440. }
  1441. update_gcontact($data);
  1442. }
  1443. /**
  1444. * @brief Update the gcontact entry for a given user id
  1445. *
  1446. * @param int $uid User ID
  1447. */
  1448. function update_gcontact_for_user($uid) {
  1449. $r = q("SELECT `profile`.`locality`, `profile`.`region`, `profile`.`country-name`,
  1450. `profile`.`name`, `profile`.`about`, `profile`.`gender`,
  1451. `profile`.`pub_keywords`, `profile`.`dob`, `profile`.`photo`,
  1452. `profile`.`net-publish`, `user`.`nickname`, `user`.`hidewall`,
  1453. `contact`.`notify`, `contact`.`url`, `contact`.`addr`
  1454. FROM `profile`
  1455. INNER JOIN `user` ON `user`.`uid` = `profile`.`uid`
  1456. INNER JOIN `contact` ON `contact`.`uid` = `profile`.`uid`
  1457. WHERE `profile`.`uid` = %d AND `profile`.`is-default` AND `contact`.`self`",
  1458. intval($uid));
  1459. $location = formatted_location(array("locality" => $r[0]["locality"], "region" => $r[0]["region"],
  1460. "country-name" => $r[0]["country-name"]));
  1461. // The "addr" field was added in 3.4.3 so it can be empty for older users
  1462. if ($r[0]["addr"] != "")
  1463. $addr = $r[0]["nickname"].'@'.str_replace(array("http://", "https://"), "", App::get_baseurl());
  1464. else
  1465. $addr = $r[0]["addr"];
  1466. $gcontact = array("name" => $r[0]["name"], "location" => $location, "about" => $r[0]["about"],
  1467. "gender" => $r[0]["gender"], "keywords" => $r[0]["pub_keywords"],
  1468. "birthday" => $r[0]["dob"], "photo" => $r[0]["photo"],
  1469. "notify" => $r[0]["notify"], "url" => $r[0]["url"],
  1470. "hide" => ($r[0]["hidewall"] OR !$r[0]["net-publish"]),
  1471. "nick" => $r[0]["nickname"], "addr" => $addr,
  1472. "connect" => $addr, "server_url" => App::get_baseurl(),
  1473. "generation" => 1, "network" => NETWORK_DFRN);
  1474. update_gcontact($gcontact);
  1475. }
  1476. /**
  1477. * @brief Fetches users of given GNU Social server
  1478. *
  1479. * If the "Statistics" plugin is enabled (See http://gstools.org/ for details) we query user data with this.
  1480. *
  1481. * @param str $server Server address
  1482. */
  1483. function gs_fetch_users($server) {
  1484. logger("Fetching users from GNU Social server ".$server, LOGGER_DEBUG);
  1485. $url = $server."/main/statistics";
  1486. $result = z_fetch_url($url);
  1487. if (!$result["success"])
  1488. return false;
  1489. $statistics = json_decode($result["body"]);
  1490. if (is_object($statistics->config)) {
  1491. if ($statistics->config->instance_with_ssl)
  1492. $server = "https://";
  1493. else
  1494. $server = "http://";
  1495. $server .= $statistics->config->instance_address;
  1496. $hostname = $statistics->config->instance_address;
  1497. } else {
  1498. if ($statistics->instance_with_ssl)
  1499. $server = "https://";
  1500. else
  1501. $server = "http://";
  1502. $server .= $statistics->instance_address;
  1503. $hostname = $statistics->instance_address;
  1504. }
  1505. if (is_object($statistics->users))
  1506. foreach ($statistics->users AS $nick => $user) {
  1507. $profile_url = $server."/".$user->nickname;
  1508. $contact = array("url" => $profile_url,
  1509. "name" => $user->fullname,
  1510. "addr" => $user->nickname."@".$hostname,
  1511. "nick" => $user->nickname,
  1512. "about" => $user->bio,
  1513. "network" => NETWORK_OSTATUS,
  1514. "photo" => App::get_baseurl()."/images/person-175.jpg");
  1515. get_gcontact_id($contact);
  1516. }
  1517. }
  1518. /**
  1519. * @brief Asking GNU Social server on a regular base for their user data
  1520. *
  1521. */
  1522. function gs_discover() {
  1523. $requery_days = intval(get_config("system", "poco_requery_days"));
  1524. $last_update = date("c", time() - (60 * 60 * 24 * $requery_days));
  1525. $r = q("SELECT `nurl`, `url` FROM `gserver` WHERE `last_contact` >= `last_failure` AND `network` = '%s' AND `last_poco_query` < '%s' ORDER BY RAND() LIMIT 5",
  1526. dbesc(NETWORK_OSTATUS), dbesc($last_update));
  1527. if (!$r)
  1528. return;
  1529. foreach ($r AS $server) {
  1530. gs_fetch_users($server["url"]);
  1531. q("UPDATE `gserver` SET `last_poco_query` = '%s' WHERE `nurl` = '%s'", dbesc(datetime_convert()), dbesc($server["nurl"]));
  1532. }
  1533. }
  1534. ?>