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