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