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.

192 lines
5.7 KiB

6 years ago
6 years ago
  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. require_once('include/pidfile.php');
  18. load_config('config');
  19. load_config('system');
  20. $maxsysload = intval(get_config('system','maxloadavg'));
  21. if($maxsysload < 1)
  22. $maxsysload = 50;
  23. if(function_exists('sys_getloadavg')) {
  24. $load = sys_getloadavg();
  25. if(intval($load[0]) > $maxsysload) {
  26. logger('system: load ' . $load[0] . ' too high. discover_poco deferred to next scheduled run.');
  27. return;
  28. }
  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 == 1) {
  36. $search = "";
  37. $mode = 0;
  38. } else
  39. die("Unknown or missing parameter ".$argv[1]."\n");
  40. $lockpath = get_lockpath();
  41. if ($lockpath != '') {
  42. $pidfile = new pidfile($lockpath, 'discover_poco'.$mode.urlencode($search));
  43. if($pidfile->is_already_running()) {
  44. logger("discover_poco: Already running");
  45. if ($pidfile->running_time() > 19*60) {
  46. $pidfile->kill();
  47. logger("discover_poco: killed stale process");
  48. // Calling a new instance
  49. if ($mode == 0)
  50. proc_run('php','include/discover_poco.php');
  51. }
  52. exit;
  53. }
  54. }
  55. $a->set_baseurl(get_config('system','url'));
  56. load_hooks();
  57. logger('start '.$search);
  58. if (($mode == 2) AND get_config('system','poco_completion'))
  59. discover_users();
  60. elseif (($mode == 1) AND ($search != "") and get_config('system','poco_local_search'))
  61. discover_directory($search);
  62. elseif (($mode == 0) AND ($search == "") and (get_config('system','poco_discovery') > 0))
  63. poco_discover();
  64. logger('end '.$search);
  65. return;
  66. }
  67. function discover_users() {
  68. logger("Discover users", LOGGER_DEBUG);
  69. $users = q("SELECT `url`, `created`, `updated`, `last_failure`, `last_contact`, `server_url` FROM `gcontact`
  70. WHERE `last_contact` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
  71. `last_failure` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
  72. `network` IN ('%s', '%s', '%s', '%s', '') ORDER BY rand()",
  73. dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA),
  74. dbesc(NETWORK_OSTATUS), dbesc(NETWORK_FEED));
  75. if (!$users)
  76. return;
  77. $checked = 0;
  78. foreach ($users AS $user) {
  79. $urlparts = parse_url($user["url"]);
  80. if (!isset($urlparts["scheme"])) {
  81. q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
  82. dbesc(NETWORK_PHANTOM), dbesc(normalise_link($user["url"])));
  83. continue;
  84. }
  85. if (in_array($urlparts["host"], array("www.facebook.com", "facebook.com", "twitter.com",
  86. "identi.ca", "alpha.app.net"))) {
  87. $networks = array("www.facebook.com" => NETWORK_FACEBOOK,
  88. "facebook.com" => NETWORK_FACEBOOK,
  89. "twitter.com" => NETWORK_TWITTER,
  90. "identi.ca" => NETWORK_PUMPIO,
  91. "alpha.app.net" => NETWORK_APPNET);
  92. q("UPDATE `gcontact` SET `network` = '%s' WHERE `nurl` = '%s'",
  93. dbesc($networks[$urlparts["host"]]), dbesc(normalise_link($user["url"])));
  94. continue;
  95. }
  96. if ($user["server_url"] != "")
  97. $server_url = $user["server_url"];
  98. else
  99. $server_url = poco_detect_server($user["url"]);
  100. if (poco_check_server($server_url, $gcontacts[0]["network"])) {
  101. logger('Check user '.$user["url"]);
  102. poco_last_updated($user["url"], true);
  103. if (++$checked > 100)
  104. return;
  105. } else
  106. q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
  107. dbesc(datetime_convert()), dbesc(normalise_link($user["url"])));
  108. }
  109. }
  110. function discover_directory($search) {
  111. $data = Cache::get("dirsearch:".$search);
  112. if (!is_null($data)){
  113. // Only search for the same item every 24 hours
  114. if (time() < $data + (60 * 60 * 24)) {
  115. logger("Already searched for ".$search." in the last 24 hours", LOGGER_DEBUG);
  116. return;
  117. }
  118. }
  119. $x = fetch_url("http://dir.friendica.com/lsearch?p=1&n=500&search=".urlencode($search));
  120. $j = json_decode($x);
  121. if(count($j->results))
  122. foreach($j->results as $jj) {
  123. // Check if the contact already exists
  124. $exists = q("SELECT `id`, `last_contact`, `last_failure`, `updated` FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($jj->url));
  125. if ($exists) {
  126. logger("Profile ".$jj->url." already exists (".$search.")", LOGGER_DEBUG);
  127. if (($exists[0]["last_contact"] < $exists[0]["last_failure"]) AND
  128. ($exists[0]["updated"] < $exists[0]["last_failure"]))
  129. continue;
  130. // Update the contact
  131. poco_last_updated($jj->url);
  132. continue;
  133. }
  134. // Harcoded paths aren't so good. But in this case it is okay.
  135. // First: We only will get Friendica contacts (which always are using this url schema)
  136. // Second: There will be no further problems if we are doing a mistake
  137. $server_url = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $jj->url);
  138. if ($server_url != $jj->url)
  139. if (!poco_check_server($server_url)) {
  140. logger("Friendica server ".$server_url." doesn't answer.", LOGGER_DEBUG);
  141. continue;
  142. }
  143. logger("Friendica server ".$server_url." seems to be okay.", LOGGER_DEBUG);
  144. logger("Check if profile ".$jj->url." is reachable (".$search.")", LOGGER_DEBUG);
  145. $data = probe_url($jj->url);
  146. if ($data["network"] == NETWORK_DFRN) {
  147. logger("Add profile ".$jj->url." to local directory (".$search.")", LOGGER_DEBUG);
  148. poco_check($data["url"], $data["name"], $data["network"], $data["photo"], "", "", "", $jj->tags, $data["addr"], "", 0);
  149. }
  150. }
  151. Cache::set("dirsearch:".$search, time());
  152. }
  153. if (array_search(__file__,get_included_files())===0){
  154. discover_poco_run($_SERVER["argv"],$_SERVER["argc"]);
  155. killme();
  156. }