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.

247 lines
7.3 KiB

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