forked from OpenMage/magento-lts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.php
204 lines (167 loc) · 5.29 KB
/
get.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
<?php
/**
* OpenMage
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 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:
* https://opensource.org/licenses/osl-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.
*
* @category Mage
* @package Mage
* @copyright Copyright (c) 2006-2020 Magento, Inc. (https://www.magento.com)
* @copyright Copyright (c) 2016-2022 The OpenMage Contributors (https://www.openmage.org)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
$start = microtime(true);
/**
* Error reporting
*/
ini_set('display_errors', 0);
$ds = DIRECTORY_SEPARATOR;
$ps = PATH_SEPARATOR;
$bp = __DIR__;
require $bp . '/app/bootstrap.php';
/**
* Set include path
*/
$paths[] = $bp . $ds . 'app' . $ds . 'code' . $ds . 'local';
$paths[] = $bp . $ds . 'app' . $ds . 'code' . $ds . 'community';
$paths[] = $bp . $ds . 'app' . $ds . 'code' . $ds . 'core';
$paths[] = $bp . $ds . 'lib';
$appPath = implode($ps, $paths);
set_include_path($appPath . $ps . get_include_path());
include_once 'Mage/Core/functions.php';
include_once 'Varien/Autoload.php';
Varien_Autoload::register();
$varDirectory = $bp . $ds . Mage_Core_Model_Config_Options::VAR_DIRECTORY;
$configCacheFile = $varDirectory . $ds . 'resource_config.json';
$mediaDirectory = null;
$allowedResources = [];
if (file_exists($configCacheFile) && is_readable($configCacheFile)) {
$config = json_decode(file_get_contents($configCacheFile), true);
//checking update time
if (filemtime($configCacheFile) + $config['update_time'] > time()) {
$mediaDirectory = trim(str_replace($bp . $ds, '', $config['media_directory']), $ds);
$allowedResources = array_merge($allowedResources, $config['allowed_resources']);
}
}
$request = new Zend_Controller_Request_Http();
$pathInfo = str_replace('..', '', ltrim($request->getPathInfo(), '/'));
$filePath = str_replace('/', $ds, rtrim($bp, $ds) . $ds . $pathInfo);
if ($mediaDirectory) {
if (0 !== stripos($pathInfo, $mediaDirectory . '/') || is_dir($filePath)) {
sendNotFoundPage();
}
$relativeFilename = str_replace($mediaDirectory . '/', '', $pathInfo);
checkResource($relativeFilename, $allowedResources);
sendFile($filePath);
}
$mageFilename = 'app/Mage.php';
if (!file_exists($mageFilename)) {
echo $mageFilename . ' was not found';
}
require_once $mageFilename;
umask(0);
/* Store or website code */
$mageRunCode = $_SERVER['MAGE_RUN_CODE'] ?? '';
/* Run store or run website */
$mageRunType = $_SERVER['MAGE_RUN_TYPE'] ?? 'store';
if (empty($mediaDirectory)) {
Mage::init($mageRunCode, $mageRunType);
} else {
Mage::init(
$mageRunCode,
$mageRunType,
['cache' => ['disallow_save' => true]],
$config['loaded_modules'] ?? ['Mage_Core']
);
}
if (!$mediaDirectory) {
$config = Mage_Core_Model_File_Storage::getScriptConfig();
$mediaDirectory = str_replace($bp . $ds, '', $config['media_directory']);
$allowedResources = array_merge($allowedResources, $config['allowed_resources']);
$relativeFilename = str_replace($mediaDirectory . '/', '', $pathInfo);
$fp = fopen($configCacheFile, 'w');
if (flock($fp, LOCK_EX | LOCK_NB)) {
ftruncate($fp, 0);
fwrite($fp, json_encode($config));
}
flock($fp, LOCK_UN);
fclose($fp);
checkResource($relativeFilename, $allowedResources);
}
if (0 !== stripos($pathInfo, $mediaDirectory . '/')) {
sendNotFoundPage();
}
if (substr_count($relativeFilename, '/') > 10) {
sendNotFoundPage();
}
// Nothing to do if DB storage is disabled
if (!Mage::helper('core/file_storage_database')->checkDbUsage()) {
sendNotFoundPage();
}
$localStorage = Mage::getModel('core/file_storage_file');
$remoteStorage = Mage::getModel('core/file_storage_database');
try {
if ($localStorage->lockCreateFile($relativeFilename)) {
try {
$remoteStorage->loadByFilename($relativeFilename);
} catch (Exception $e) {
Mage::logException($e);
}
if ($remoteStorage->getId()) {
$localStorage->saveFile($remoteStorage, false);
} else {
$localStorage->removeLockedFile($relativeFilename);
}
}
sendFile($filePath);
} catch (Exception $e) {
Mage::logException($e);
}
sendNotFoundPage();
/**
* Send 404
*/
function sendNotFoundPage()
{
header('HTTP/1.0 404 Not Found');
exit;
}
/**
* Check resource by whitelist
*
* @param string $resource
* @param array $allowedResources
*/
function checkResource($resource, array $allowedResources)
{
$isResourceAllowed = false;
foreach ($allowedResources as $allowedResource) {
if (0 === stripos($resource, $allowedResource)) {
$isResourceAllowed = true;
}
}
if (!$isResourceAllowed) {
sendNotFoundPage();
}
}
/**
* Send file to browser
*
* @param string $file
*/
function sendFile($file)
{
if (is_readable($file) && filesize($file) > 0) {
$transfer = new Varien_File_Transfer_Adapter_Http();
$transfer->send($file);
exit;
}
}