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.

216 lines
6.3KB

  1. <?php
  2. require_once("boot.php");
  3. require_once("include/socgraph.php");
  4. function discover_poco_run(&$argv, &$argc){
  5. global $a, $db;
  6. if(is_null($a)) {
  7. $a = new App;
  8. }
  9. if(is_null($db)) {
  10. @include(".htconfig.php");
  11. require_once("include/dba.php");
  12. $db = new dba($db_host, $db_user, $db_pass, $db_data);
  13. unset($db_host, $db_user, $db_pass, $db_data);
  14. };
  15. require_once('include/session.php');
  16. require_once('include/datetime.php');
  17. load_config('config');
  18. load_config('system');
  19. // Don't check this stuff if the function is called by the poller
  20. if (App::callstack() != "poller_run")
  21. if (App::maxload_reached())
  22. return;
  23. if(($argc > 2) && ($argv[1] == "dirsearch")) {
  24. $search = urldecode($argv[2]);
  25. $mode = 1;
  26. } elseif(($argc == 2) && ($argv[1] == "checkcontact")) {
  27. $mode = 2;
  28. } elseif(($argc == 2) && ($argv[1] == "suggestions")) {
  29. $mode = 3;
  30. } elseif ($argc == 1) {
  31. $search = "";
  32. $mode = 0;
  33. } else
  34. die("Unknown or missing parameter ".$argv[1]."\n");
  35. // Don't check this stuff if the function is called by the poller
  36. if (App::callstack() != "poller_run")
  37. if (App::is_already_running('discover_poco'.$mode.urlencode($search), 'include/discover_poco.php', 1140))
  38. return;
  39. $a->set_baseurl(get_config('system','url'));
  40. load_hooks();
  41. logger('start '.$search);
  42. if ($mode==3)
  43. update_suggestions();
  44. elseif (($mode == 2) AND get_config('system','poco_completion'))
  45. discover_users();
  46. elseif (($mode == 1) AND ($search != "") and get_config('system','poco_local_search')) {
  47. discover_directory($search);
  48. gs_search_user($search);
  49. } elseif (($mode == 0) AND ($search == "") and (get_config('system','poco_discovery') > 0)) {
  50. // Query Friendica and Hubzilla servers for their users
  51. poco_discover();
  52. // Query GNU Social servers for their users ("statistics" addon has to be enabled on the GS server)
  53. if (!get_config('system','ostatus_disabled'))
  54. gs_discover();
  55. }
  56. logger('end '.$search);
  57. return;
  58. }
  59. function discover_users() {
  60. logger("Discover users", LOGGER_DEBUG);
  61. $users = q("SELECT `url`, `created`, `updated`, `last_failure`, `last_contact`, `server_url` FROM `gcontact`
  62. WHERE `last_contact` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
  63. `last_failure` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
  64. `network` IN ('%s', '%s', '%s', '%s', '') ORDER BY rand()",
  65. dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA),
  66. dbesc(NETWORK_OSTATUS), dbesc(NETWORK_FEED));
  67. if (!$users)
  68. return;
  69. $checked = 0;
  70. foreach ($users AS $user) {
  71. $urlparts = parse_url($user["url"]);
  72. if (!isset($urlparts["scheme"])) {
  73. q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
  74. dbesc(NETWORK_PHANTOM), dbesc(normalise_link($user["url"])));
  75. continue;
  76. }
  77. if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
  78. "identi.ca", "alpha.app.net"))) {
  79. $networks = array("www.facebook.com" => NETWORK_FACEBOOK,
  80. "facebook.com" => NETWORK_FACEBOOK,
  81. "twitter.com" => NETWORK_TWITTER,
  82. "identi.ca" => NETWORK_PUMPIO,
  83. "alpha.app.net" => NETWORK_APPNET);
  84. q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
  85. dbesc($networks[$urlparts["host"]]), dbesc(normalise_link($user["url"])));
  86. continue;
  87. }
  88. if ($user["server_url"] != "")
  89. $server_url = $user["server_url"];
  90. else
  91. $server_url = poco_detect_server($user["url"]);
  92. if (($server_url == "") OR poco_check_server($server_url, $gcontacts[0]["network"])) {
  93. logger('Check user '.$user["url"]);
  94. poco_last_updated($user["url"], true);
  95. if (++$checked > 100)
  96. return;
  97. } else
  98. q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
  99. dbesc(datetime_convert()), dbesc(normalise_link($user["url"])));
  100. }
  101. }
  102. function discover_directory($search) {
  103. $data = Cache::get("dirsearch:".$search);
  104. if (!is_null($data)){
  105. // Only search for the same item every 24 hours
  106. if (time() < $data + (60 * 60 * 24)) {
  107. logger("Already searched for ".$search." in the last 24 hours", LOGGER_DEBUG);
  108. return;
  109. }
  110. }
  111. $x = fetch_url(get_server()."/lsearch?p=1&n=500&search=".urlencode($search));
  112. $j = json_decode($x);
  113. if(count($j->results))
  114. foreach($j->results as $jj) {
  115. // Check if the contact already exists
  116. $exists = q("SELECT `id`, `last_contact`, `last_failure`, `updated` FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($jj->url));
  117. if ($exists) {
  118. logger("Profile ".$jj->url." already exists (".$search.")", LOGGER_DEBUG);
  119. if (($exists[0]["last_contact"] < $exists[0]["last_failure"]) AND
  120. ($exists[0]["updated"] < $exists[0]["last_failure"]))
  121. continue;
  122. // Update the contact
  123. poco_last_updated($jj->url);
  124. continue;
  125. }
  126. // Harcoded paths aren't so good. But in this case it is okay.
  127. // First: We only will get Friendica contacts (which always are using this url schema)
  128. // Second: There will be no further problems if we are doing a mistake
  129. $server_url = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $jj->url);
  130. if ($server_url != $jj->url)
  131. if (!poco_check_server($server_url)) {
  132. logger("Friendica server ".$server_url." doesn't answer.", LOGGER_DEBUG);
  133. continue;
  134. }
  135. logger("Friendica server ".$server_url." seems to be okay.", LOGGER_DEBUG);
  136. logger("Check if profile ".$jj->url." is reachable (".$search.")", LOGGER_DEBUG);
  137. $data = probe_url($jj->url);
  138. if ($data["network"] == NETWORK_DFRN) {
  139. logger("Add profile ".$jj->url." to local directory (".$search.")", LOGGER_DEBUG);
  140. poco_check($data["url"], $data["name"], $data["network"], $data["photo"], "", "", "", $jj->tags, $data["addr"], "", 0);
  141. }
  142. }
  143. Cache::set("dirsearch:".$search, time(), CACHE_DAY);
  144. }
  145. /**
  146. * @brief Search for GNU Social user with gstools.org
  147. *
  148. * @param str $search User name
  149. */
  150. function gs_search_user($search) {
  151. $a = get_app();
  152. $url = "http://gstools.org/api/users_search/".urlencode($search);
  153. $result = z_fetch_url($url);
  154. if (!$result["success"])
  155. return false;
  156. $contacts = json_decode($result["body"]);
  157. if ($contacts->status == 'ERROR')
  158. return false;
  159. foreach($contacts->data AS $user) {
  160. $contact = probe_url($user->site_address."/".$user->name);
  161. if ($contact["network"] != NETWORK_PHANTOM) {
  162. $contact["about"] = $user->description;
  163. update_gcontact($contact);
  164. }
  165. }
  166. }
  167. if (array_search(__file__,get_included_files())===0){
  168. discover_poco_run($_SERVER["argv"],$_SERVER["argc"]);
  169. killme();
  170. }