-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathold-precache.php
More file actions
executable file
·229 lines (192 loc) · 6.93 KB
/
old-precache.php
File metadata and controls
executable file
·229 lines (192 loc) · 6.93 KB
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
<?php
/**
* 2007-2015 PrestaShop.
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2015 PrestaShop SA
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
die('deprecated, use new URL');
ini_set('max_execution_time', 0);
ob_start();
include dirname(__FILE__).'/../../config/config.inc.php';
include dirname(__FILE__).'/expresscache.php';
if (Tools::substr(Tools::encrypt('expresscache/index'), 0, 10) != Tools::getValue('token') || !Module::isInstalled('expresscache')) {
die('Bad token');
}
function is_cache_exist($url)
{
$page_id = md5($url);
$cache_row = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT id_express_cache, cache, hits, miss, last_updated
FROM ' ._DB_PREFIX_."express_cache
WHERE page_id = '" .pSQL($page_id)."'");
if ($cache_row) {
$last_updated = strtotime($cache_row[0]['last_updated']);
$now = strtotime(gmdate('Y-m-d H:i:s'));
if (round(abs($now - $last_updated) / 60, 2) > Configuration::get('EXPRESSCACHE_TIMEOUT')) {
$cache_row = false;
}
}
}
function get_web_page($url, $agent = "desktop")
{
// return;
if ($agent == "desktop") {
$user_agent = 'Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0 EXPRESSCACHE_BOT';
} else {
$user_agent = 'Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 EXPRESSCACHE_BOT';
}
$options = array(
CURLOPT_CUSTOMREQUEST => 'GET', //set request type post or get
CURLOPT_POST => false, //set to GET
CURLOPT_USERAGENT => $user_agent, //set user agent
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => '', // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 5, // stop after 10 redirects
);
$url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?');
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$header = curl_getinfo($ch);
curl_close($ch);
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
// var_dump($header);
return $header;
}
function display($txt)
{
if (php_sapi_name() === 'cli') {
echo $txt;
} else {
echo $txt.'<br>';
}
ob_flush();
}
//generating URLs
//TODO: if we can use the URLs from Express Cache table itself?
//if not try to generate by yourself.
$context = Context::getContext();
$id_shop = (int) $context->shop->id;
//Getting settings from the URL
$id_lang = (int) $context->language->id;
//fetch index page
$index_page = $context->shop->getBaseURL(true);
display($index_page);
get_web_page($index_page);
get_web_page($index_page, "mobile");
//fetch bestsales,pricesdrop,newproducts
$link = $context->link->getPageLink('bestsales');
display($link);
get_web_page($link);
get_web_page($link, "mobile");
$link = $context->link->getPageLink('pricesdrop');
display($link);
get_web_page($link);
get_web_page($link, "mobile");
$link = $context->link->getPageLink('newproducts');
display($link);
get_web_page($link);
get_web_page($link, "mobile");
// exit;
//fetch categories pages
$categories = Category::getSimpleCategories($id_lang, true);
foreach ($categories as $category) {
$cat = new Category($category['id_category']);
// var_dump($cat);
if ($cat->active == '1') {
$link = $cat->getLink();
display($link);
get_web_page($link, "desktop");
get_web_page($link, "mobile");
}
}
//fetch product pages
$products = Product::getSimpleProducts($id_lang, $context);
//fetch cached products
$cached_products = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT id_entity as id_product
FROM ' ._DB_PREFIX_."express_cache where cache !='NULL' and entity_type = 'product' order by last_updated desc");
//convert to one dimensional array for easier search
$cached_products = array_map('current', $cached_products);
//build the list of pages to be pre-cached by filtering thru these two lists
// Note - could have done this with simple SQL, but don't want to mess with PS tables
$product_to_cache = array();
foreach ($products as $product) {
$id_product = $product['id_product'];
// echo $id_product;
if (!in_array($id_product, $cached_products)) {
$product_to_cache[] = $id_product;
}
}
//merge the not cache products with cached ones (but in reverse direction)
// $product_to_cache = $product_to_cache + array_reverse($cached_products);
$product_to_cache = array_merge($product_to_cache, array_reverse($cached_products));
$limit = Configuration::get('EXPRESSCACHE_PRECACHE_LIMIT');
$i = 1;
foreach ($product_to_cache as $id_product) {
// display($product['id_product']);
$prod = new Product($id_product);
// var_dump($cat);
if ($prod->active == '1') {
$link = $prod->getLink($context);
display($link);
get_web_page($link);
get_web_page($link, "mobile");
}
if ($i >= (int) $limit) {
break;
}
++$i;
}
//fetch cms pages
$cmses = CMS::getLinks($id_lang);
foreach ($cmses as $cms) {
$link = $cms['link'];
display($link);
get_web_page($link);
get_web_page($link, "mobile");
// var_dump($cms);
}
//fetch manufacturer pages
$manufacturers = Manufacturer::getManufacturers();
// var_dump($manufacturers);
foreach ($manufacturers as $manufacturer) {
// display($product['id_product']);
$man = new Manufacturer($manufacturer['id_manufacturer']);
// var_dump($cat);
if ($man->active == '1') {
// echo "1";
$link = $context->link->getManufacturerLink($man, $man->link_rewrite, $id_lang);
display($link);
get_web_page($link);
get_web_page($link, "mobile");
}
}