3 require_once("boot.php");
4 require_once("include/socgraph.php");
7 function discover_poco_run(&$argv, &$argc){
15 @include(".htconfig.php");
16 require_once("include/dba.php");
17 $db = new dba($db_host, $db_user, $db_pass, $db_data);
18 unset($db_host, $db_user, $db_pass, $db_data);
21 require_once('include/session.php');
22 require_once('include/datetime.php');
23 require_once('include/pidfile.php');
25 load_config('config');
26 load_config('system');
28 $maxsysload = intval(get_config('system','maxloadavg'));
31 if(function_exists('sys_getloadavg')) {
32 $load = sys_getloadavg();
33 if(intval($load[0]) > $maxsysload) {
34 logger('system: load ' . $load[0] . ' too high. discover_poco deferred to next scheduled run.');
39 if(($argc > 2) && ($argv[1] == "dirsearch")) {
40 $search = urldecode($argv[2]);
42 } elseif(($argc == 2) && ($argv[1] == "checkcontact")) {
44 } elseif ($argc == 1) {
48 die("Unknown or missing parameter ".$argv[1]."\n");
50 $lockpath = get_lockpath();
51 if ($lockpath != '') {
52 $pidfile = new pidfile($lockpath, 'discover_poco'.$mode.urlencode($search));
53 if($pidfile->is_already_running()) {
54 logger("discover_poco: Already running");
55 if ($pidfile->running_time() > 19*60) {
57 logger("discover_poco: killed stale process");
58 // Calling a new instance
60 proc_run('php','include/discover_poco.php');
66 $a->set_baseurl(get_config('system','url'));
70 logger('start '.$search);
72 if (($mode == 2) AND get_config('system','poco_completion'))
74 elseif (($mode == 1) AND ($search != "") and get_config('system','poco_local_search'))
75 discover_directory($search);
76 elseif (($mode == 0) AND ($search == "") and (get_config('system','poco_discovery') > 0))
79 logger('end '.$search);
84 function discover_users() {
85 logger("Discover users", LOGGER_DEBUG);
87 $users = q("SELECT `url`, `created`, `updated`, `last_failure`, `last_contact`, `server_url` FROM `gcontact`
88 WHERE `last_contact` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
89 `last_failure` < UTC_TIMESTAMP - INTERVAL 1 MONTH AND
90 `network` IN ('%s', '%s', '%s', '%s', '') ORDER BY rand()",
91 dbesc(NETWORK_DFRN), dbesc(NETWORK_DIASPORA),
92 dbesc(NETWORK_OSTATUS), dbesc(NETWORK_FEED));
99 foreach ($users AS $user) {
101 if ($user[0]["server_url"] != "")
102 $server_url = $user[0]["server_url"];
104 $server_url = poco_detect_server($user["url"]);
106 if (poco_check_server($server_url, $gcontacts[0]["network"])) {
107 logger('Check user '.$user["url"]);
108 poco_last_updated($user["url"], true);
110 if (++$checked > 100)
113 q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `nurl` = '%s'",
114 dbesc(datetime_convert()), dbesc(normalise_link($user["url"])));
118 function discover_directory($search) {
120 $data = Cache::get("dirsearch:".$search);
121 if (!is_null($data)){
122 // Only search for the same item every 24 hours
123 if (time() < $data + (60 * 60 * 24)) {
124 logger("Already searched for ".$search." in the last 24 hours", LOGGER_DEBUG);
129 $x = fetch_url("http://dir.friendica.com/lsearch?p=1&n=500&search=".urlencode($search));
130 $j = json_decode($x);
132 if(count($j->results))
133 foreach($j->results as $jj) {
134 // Check if the contact already exists
135 $exists = q("SELECT `id`, `last_contact`, `last_failure`, `updated` FROM `gcontact` WHERE `nurl` = '%s'", normalise_link($jj->url));
137 logger("Profile ".$jj->url." already exists (".$search.")", LOGGER_DEBUG);
139 if (($exists[0]["last_contact"] < $exists[0]["last_failure"]) AND
140 ($exists[0]["updated"] < $exists[0]["last_failure"]))
143 // Update the contact
144 poco_last_updated($jj->url);
148 // Harcoded paths aren't so good. But in this case it is okay.
149 // First: We only will get Friendica contacts (which always are using this url schema)
150 // Second: There will be no further problems if we are doing a mistake
151 $server_url = preg_replace("=(https?://)(.*)/profile/(.*)=ism", "$1$2", $jj->url);
152 if ($server_url != $jj->url)
153 if (!poco_check_server($server_url)) {
154 logger("Friendica server ".$server_url." doesn't answer.", LOGGER_DEBUG);
157 logger("Friendica server ".$server_url." seems to be okay.", LOGGER_DEBUG);
159 logger("Check if profile ".$jj->url." is reachable (".$search.")", LOGGER_DEBUG);
160 $data = probe_url($jj->url);
161 if ($data["network"] == NETWORK_DFRN) {
162 logger("Add profile ".$jj->url." to local directory (".$search.")", LOGGER_DEBUG);
163 poco_check($data["url"], $data["name"], $data["network"], $data["photo"], "", "", "", $jj->tags, $data["addr"], "", 0);
166 Cache::set("dirsearch:".$search, time());
169 if (array_search(__file__,get_included_files())===0){
170 discover_poco_run($_SERVER["argv"],$_SERVER["argc"]);