-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
load.php
352 lines (325 loc) · 10.5 KB
/
load.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* List Venues Loader
*
* Carrega venues a partir de um endereço web, arquivo de texto ou IDs
*
* @category Foursquare
* @package Foursquare-Mass-Editor-Tools
* @author Elio Gavlinski <gavlinski@gmail.com>
* @copyright Copyleft (c) 2011-2018
* @version 2.3.1
* @link https://github.com/gavlinski/Foursquare-Mass-Editor-Tools/blob/master/load.php
* @since File available since Release 1.1
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
/**
* Variáveis de Sessão:
*
* $_SESSION["file"]) = Array com as URLs completas, caso informadas
* $_SESSION["venuesIds"] = Array com os IDs parseados das venues
* $_SESSION["venues"] = String com os IDs das venues (v1,v2,...)
* $_SESSION["campos"] = Array com os campos editáveis
*/
if (!isset($_SESSION))
session_start();
if (isset($_GET["venues"]))
$_SESSION["venues"] = $_GET["venues"];
if (!isset($_SESSION["oauth_token"])) {
header('Location: index.php');
}
?>
<!doctype html>
<html lang="pt-BR">
<head>
<title>Carregando...</title>
<meta charset="utf-8">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="pragma" content="no-cache">
<?php
define("VERSION", "Venues Loader 2.3.1");
define("LINKS", '<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="js/dijit/themes/tundra/tundra.css">
<link rel="stylesheet" type="text/css" href="estilo.css">
');
define("HBODY", '</head>
<body class="tundra">
');
define("CARREGANDO", LINKS . HBODY . '<div id="carregando">Carregando venues…</div>
');
define("TEMPLATE1", '<script src="js/dojo/dojo.js" djConfig="parseOnLoad: true"></script>
<script>dojo.require("dijit.form.Button");</script>
');
define("TEMPLATE2", '<p><button dojoType="dijit.form.Button" type="button" onclick="history.go(-1)" style="margin-left: 0px;">Voltar</button></p>
</body>
</html>');
define("ERRO01", TEMPLATE1 . '<p>O limite da API é de 500 requisições por hora por conjunto de endpoints por OAuth.</p>
<p>Reduza a quantidade de linhas e tente novamente.</p>
' . TEMPLATE2);
//define("ERRO02", TEMPLATE1 . '<p>Erro na leitura do ID ou URL de uma das venues.</p>
//<p>Verifique o arquivo ou a lista e tente novamente.</p>
//' . TEMPLATE2);
define("ERRO03", TEMPLATE1 . '<p>Nenhuma venue encontrada no endereço informado.</p>
<p>Verifique a página e tente novamente.</p>
' . TEMPLATE2);
define("ERRO99", '<meta http-equiv="refresh" content="5; url=index.php">
' . LINKS . '</head>
<body>
<p>Erro na leitura dos dados.</p>
</body>
</html>');
define("EDIT", '<script>
window.location = "edit.php"
</script>;');
/*** Importação de dados de um arquivo texto ***/
if (isset($_FILES['txt']['tmp_name'])) {
$arquivo = $_FILES['txt']['tmp_name'];
if (is_uploaded_file($arquivo)) {
$_SESSION["file"] = validarVenues(file($arquivo));
$_SESSION["campos"] = $_POST["campos1"];
setLocalCache("txt", implode('%0A,', $_SESSION["file"]), "venues");
echo EDIT;
}
/*** Importação de lista de uma página web ***/
} else if (isset($_POST["pagina"])) {
$pagina = $_POST["pagina"];
if ($pagina != "") {
$_SESSION["file"] = parseVenues(trim($pagina));
if ($_SESSION["file"] == false) {
echo ERRO03;
exit;
} else {
$_SESSION["campos"] = $_POST["campos2"];
setLocalCache("txt", implode('%0A,', $_SESSION["file"]), "venues");
echo EDIT;
}
}
/*** IDs ou URLs informados manualmente ***/
} else if ((isset($_POST["textarea"])) && ($_POST["textarea"] != "")) {
$lista = explode("\n", $_POST["textarea"]);
$_SESSION["file"] = validarVenues($lista);
if ($_SESSION["file"] == false) {
$p->hide();
echo ERRO03;
exit;
} else {
if (isset($_POST["campos3"]))
$_SESSION["campos"] = $_POST["campos3"];
else
$_SESSION["campos"] = null;
setLocalCache("txt", implode('%0A,', $_SESSION["file"]), "venues");
echo EDIT;
}
} else if (isset($_SESSION["venues"])) {
$lista = explode(",", $_SESSION["venues"]);
$_SESSION["file"] = validarVenues($lista);
if ($_SESSION["file"] == false) {
$p->hide();
echo ERRO03;
exit;
} else {
$_SESSION["campos"] = array("nome", "endereco", "ruatransversal", "bairro",
"cidade", "estado", "codigopostal", "dentro", "telefone", "sitedaweb",
"twitter", "facebook", "instagram", "latlng", "descricao", "menu", "horas");
setLocalCache("txt", implode('%0A,', $_SESSION["file"]), "venues");
echo EDIT;
}
} else {
echo ERRO99;
}
function filtrarArray($array) {
foreach ($array as $i => &$value) {
$value = trim($value);
if (((strlen($value) < 24) || (strlen(basename($value)) != 24) || (!ctype_xdigit(basename($value)))))
unset($array[$i]);
}
unset($value);
return array_values(array_unique($array));
}
function filtrarArrayPorId($array) {
$new_array = array();
foreach ($array as $i => &$value) {
$vid = basename($value);
if ((strlen($vid) == 24) && ((!in_array($vid, $new_array)) && (ctype_xdigit($vid))))
$new_array[] = $vid;
else
unset($array[$i]);
}
unset($value);
return array_values($array);
}
function validarVenues($lines) {
$ret = array();
global $venuesIds;
$venuesIds = array();
$i = 0;
require_once 'ProgressBar.Class.php';
echo CARREGANDO;
$p = new ProgressBar();
echo '<div style="width: 400px;">' . "\r\n";
$p->render();
echo '</div>' . "\r\n";
foreach ($lines as $line_num => $line) {
/*** Places ***/
//$pos = stripos($line, ' "id": "');
//if ($pos !== false) {
//echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . " at position <b>" . $pos . "</b><br />\n";
//$ret[] = substr($line, $pos + 8);
/*** Tidysquare e 4sqmap - Foursquare Maps and Statistics ***/
if (stripos($line, 'foursquare.com/venue/') !== false) {
//echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . " at position <b>" . stripos($line, 'https://foursquare.com/venue/') . "</b><br />\n";
$ret = array_merge($ret, array_slice(explode('://foursquare.com/venue/', $line), 1));
}
}
if (count($ret) > 0) {
$size = count($ret);
foreach ($ret as &$r) {
$vid = substr($r, 0, 24);
if ((stripos($vid, ' ') === false) && (!in_array($vid, $venuesIds))) {
$venuesIds[] = $vid;
$r = "https://foursquare.com/v/" . $vid;
} else {
$r = "";
}
$i++;
$p->setProgressBarProgress($i*100/$size);
usleep(50000*0.1);
}
/*** break the reference with the last element ***/
unset($r);
$_SESSION["venuesIds"] = $venuesIds;
return filtrarArray($ret);
} else if (count($lines) > 500) {
$p->hide();
echo ERRO01;
exit;
} else {
$size = count($lines);
foreach ($lines as &$line) {
$line = trim($line);
$length = strlen($line);
//if ((stripos($line, "foursquare.com/v") === false) && ($length > 25)) {
//$p->hide();
//echo ERRO02;
//exit;
//}
if ($length > 24) {
$l = $length - 2;
if ($line[$l] === "/")
$line = substr($line, 0, $l);
$line = str_replace("/edit_history", "", $line);
$line = str_replace("/edit", "", $line);
$line = str_replace("/history", "", $line);
//$venuesIds[$i] = substr($line, strrpos($line, "/") + 1, 24);
$venuesIds[$i] = basename($line);
} else if ($length == 24) {
$venuesIds[$i] = $line;
$line = "https://foursquare.com/v/" . $line;
//$line = "" + $venuesIds[$i];
}
$i++;
$p->setProgressBarProgress($i*100/$size);
usleep(50000*0.1);
}
/*** break the reference with the last element ***/
unset($line);
$lines = filtrarArray($lines);
if (count($lines) == 0) {
$p->hide();
echo ERRO03;
exit;
}
$_SESSION["venuesIds"] = filtrarArray($venuesIds);
if (count($_SESSION["venuesIds"]) != count($lines))
$lines = filtrarArrayPorId($lines);
return $lines;
}
}
function parseVenues($html) {
$lines = @file($html);
$ret = array();
global $venuesIds;
$venuesIds = array();
$i = 0;
if ($lines) {
require_once 'ProgressBar.Class.php';
echo CARREGANDO;
$p = new ProgressBar();
echo '<div style="width: 400px;">' . "\r\n";
$p->render();
echo '</div>' . "\r\n";
foreach ($lines as $line_num => $line) {
/*** Listas do usuario do foursquare ***/
//if (stripos($line, 'ITEMS_JSON') !== false) {
if (stripos($line, 'listJson') !== false) {
//$ret = array_slice(explode('\"venue\":{\"id\":\"', $line), 1);
$ret = array_slice(explode('"venue":{"id":"', $line), 1);
break;
/*** Resultados da pesquisa ***/
} else if (stripos($line, 'fourSq.tiplists.setupSearchPageListControls([{"id":"v') !== false) {
$ret = array_slice(explode('"id":"v', $line), 1);
break;
}
}
/*** Paginas normais com a tag <a href="https://foursquare.com/venue/..."></a> ou <a href="https://foursquare.com/v/..."></a> ***/
if ($ret == null) {
/*** a new dom object ***/
$dom = new domDocument;
/*** get the HTML (suppress errors) ***/
@$dom->loadHTML(file_get_contents($html));
/*** remove silly white space ***/
$dom->preserveWhiteSpace = false;
/*** get the links from the HTML ***/
$links = $dom->getElementsByTagName('a');
/*** loop over the links ***/
$size = $links->length;
$j = 0;
foreach ($links as $tag) {
if ((stripos($tag->getAttribute('href'), "/venue/") !== false) || (stripos($tag->getAttribute('href'), "/v/") !== false)) {
if (stripos($tag->getAttribute('href'), "?referralId=a-") !== false)
$vid = substr($tag->getAttribute('href'), -62, 24);
else
$vid = substr($tag->getAttribute('href'), -24);
if (!in_array($vid, $venuesIds)) {
$venuesIds[$i] = $vid;
if (stripos($tag->getAttribute('href'), "foursquare.com") !== false)
$ret[$i] = $tag->getAttribute('href');
else
$ret[$i] = "https://foursquare.com" . $tag->getAttribute('href');
$i++;
}
}
$j++;
$p->setProgressBarProgress($j*100/$size);
usleep(50000*0.1);
}
} else {
$size = count($ret);
foreach ($ret as &$r) {
$venuesIds[$i] = substr($r, 0, 24);
$r = "https://foursquare.com/v/" . $venuesIds[$i];
$i++;
$p->setProgressBarProgress($i*100/$size);
usleep(50000*0.1);
}
/*** break the reference with the last element ***/
unset($r);
}
if ($ret == null)
$p->hide();
else if (count($ret) > 500)
$ret = array_slice($ret, 0, 500);
$_SESSION["venuesIds"] = $venuesIds;
return $ret;
} else {
echo LINKS . HBODY;
print('<script>document.title = "Erro";</script>'."\r");
return false;
}
}
function setLocalCache($key, $data, $key2) {
print('<script>'."\n\r".' localStorage.setItem(\''.$key.'\', \''.$data.'\');'."\n\r".' localStorage.removeItem(\''.$key2.'\');'."\n\r".'</script>');
print str_pad('', intval(ini_get('output_buffering'))) . "\n\r";
flush();
}
?>