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.

1752 lines
54 KiB

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