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.
 
 
 
 
 
 

132 lines
3.8 KiB

  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. $searchmode = 1;
  33. } elseif ($argc == 1) {
  34. $search = "";
  35. $searchmode = 0;
  36. } else
  37. die("Unknown or missing parameter ".$argv[1]."\n");
  38. $lockpath = get_lockpath();
  39. if ($lockpath != '') {
  40. $pidfile = new pidfile($lockpath, 'discover_poco'.urlencode($search));
  41. if($pidfile->is_already_running()) {
  42. logger("discover_poco: Already running");
  43. if ($pidfile->running_time() > 19*60) {
  44. $pidfile->kill();
  45. logger("discover_poco: killed stale process");
  46. // Calling a new instance
  47. proc_run('php','include/discover_poco.php');
  48. }
  49. exit;
  50. }
  51. }
  52. $a->set_baseurl(get_config('system','url'));
  53. load_hooks();
  54. logger('start '.$search);
  55. if (($search != "") and get_config('system','poco_local_search'))
  56. discover_directory($search);
  57. elseif (($search == "") and get_config('system','poco_discovery') > 0)
  58. poco_discover();
  59. logger('end '.$search);
  60. return;
  61. }
  62. function discover_directory($search) {
  63. $data = Cache::get("dirsearch:".$search);
  64. if (!is_null($data)){
  65. // Only search for the same item every 24 hours
  66. if (time() < $data + (60 * 60 * 24)) {
  67. logger("Already searched for ".$search." in the last 24 hours", LOGGER_DEBUG);
  68. return;
  69. }
  70. }
  71. $x = fetch_url("http://dir.friendica.com/lsearch?p=1&n=500&search=".urlencode($search));
  72. $j = json_decode($x);
  73. if(count($j->results))
  74. foreach($j->results as $jj) {
  75. // Check if the contact already exists
  76. $exists = q("SELECT `id`, `last_contact`, `last_failure` FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($jj->url));
  77. if ($exists) {
  78. logger("Profile ".$jj->url." already exists (".$search.")", LOGGER_DEBUG);
  79. if ($exists[0]["last_contact"] < $exists[0]["last_failure"])
  80. continue;
  81. $last_updated = poco_last_updated($jj->url);
  82. $last_contact = datetime_convert();
  83. if ($last_updated) {
  84. logger("Mark profile ".$jj->url." as accessible (".$search.")", LOGGER_DEBUG);
  85. q("UPDATE `gcontact` SET `updated` = '%s', `last_contact` = '%s' WHERE `nurl` = '%s'",
  86. dbesc($last_updated), dbesc($last_contact), dbesc(normalise_link($jj->url)));
  87. } else {
  88. logger("Mark profile ".$jj->url." as unaccessible (".$search.")", LOGGER_DEBUG);
  89. q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
  90. dbesc($last_contact), dbesc(normalise_link($jj->url)));
  91. }
  92. continue;
  93. }
  94. logger("Check if profile ".$jj->url." is reachable (".$search.")", LOGGER_DEBUG);
  95. $data = probe_url($jj->url);
  96. if ($data["network"] == NETWORK_DFRN) {
  97. logger("Add profile ".$jj->url." to local directory (".$search.")", LOGGER_DEBUG);
  98. poco_check($data["url"], $data["name"], $data["network"], $data["photo"], "", "", "", $jj->tags, $data["addr"], "", 0);
  99. }
  100. }
  101. Cache::set("dirsearch:".$search, time());
  102. }
  103. if (array_search(__file__,get_included_files())===0){
  104. discover_poco_run($_SERVER["argv"],$_SERVER["argc"]);
  105. killme();
  106. }