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