|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Vuefront\Vuefront\Model; |
| 4 | + |
| 5 | +use Magento\Framework\App\Action\Context; |
| 6 | +use Vuefront\Vuefront\Api\InformationInterface; |
| 7 | + |
| 8 | +class InformationModel implements InformationInterface |
| 9 | +{ |
| 10 | + private $scopeConfig; |
| 11 | + private $context; |
| 12 | + private $response; |
| 13 | + private $redirect; |
| 14 | + private $url; |
| 15 | + private $driver; |
| 16 | + private $storeManager; |
| 17 | + private $moduleManager; |
| 18 | + private $moduleList; |
| 19 | + private $driverFile; |
| 20 | + private $dir; |
| 21 | + private $file; |
| 22 | + private $moduleReader; |
| 23 | + private $zendFileExists; |
| 24 | + private $zendHttp; |
| 25 | + private $arhiveTar; |
| 26 | + protected $request; |
| 27 | + protected $startup; |
| 28 | + protected $collectionFactory; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + Context $context, |
| 32 | + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, |
| 33 | + \Magento\Framework\UrlInterface $url, |
| 34 | + \Magento\Framework\Webapi\Rest\Response $response, |
| 35 | + \Magento\Framework\Webapi\Rest\Request $request, |
| 36 | + \Magento\Framework\Filesystem\Driver\File $driver, |
| 37 | + \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productFactory, |
| 38 | + \Vuefront\Vuefront\Model\Api\System\Startup $startup, |
| 39 | + \Magento\Store\Model\StoreManagerInterface $storeManager, |
| 40 | + \Magento\Framework\Module\Manager $moduleManager, |
| 41 | + \Magento\Framework\Module\ModuleListInterface $moduleList, |
| 42 | + \Magento\Framework\Filesystem\DirectoryList $dir, |
| 43 | + \Magento\Framework\Filesystem\Driver\File $driverFile, |
| 44 | + \Magento\Framework\Module\Dir\Reader $moduleReader, |
| 45 | + \Zend\Uri\Http $zendHttp, |
| 46 | + \Zend\Validator\File\Exists $zendFileExists, |
| 47 | + \Magento\Framework\Filesystem\Io\File $file, |
| 48 | + \Magento\Framework\Archive\Tar $arhiveTar |
| 49 | + ) { |
| 50 | + $this->scopeConfig = $scopeConfig; |
| 51 | + $this->context = $context; |
| 52 | + $this->response = $response; |
| 53 | + $this->request = $request; |
| 54 | + $this->redirect = $context->getRedirect(); |
| 55 | + $this->url = $url; |
| 56 | + $this->startup = $startup; |
| 57 | + $this->driver = $driver; |
| 58 | + $this->collectionFactory = $productFactory; |
| 59 | + $this->moduleManager = $moduleManager; |
| 60 | + $this->storeManager = $storeManager; |
| 61 | + $this->moduleList = $moduleList; |
| 62 | + $this->dir = $dir; |
| 63 | + $this->moduleReader = $moduleReader; |
| 64 | + $this->driverFile = $driverFile; |
| 65 | + $this->zendFileExists = $zendFileExists; |
| 66 | + $this->file = $file; |
| 67 | + $this->zendHttp = $zendHttp; |
| 68 | + $this->arhiveTar = $arhiveTar; |
| 69 | + } |
| 70 | + |
| 71 | + public function checkCors() |
| 72 | + { |
| 73 | + if (!empty($this->request->get('cors'))) { |
| 74 | + if (!empty($this->request->getServer('HTTP_ORIGIN'))) { |
| 75 | + $this->response->setHeader('Access-Control-Allow-Origin', $this->request->getServer('HTTP_ORIGIN')); |
| 76 | + } else { |
| 77 | + $this->response->setHeader('Access-Control-Allow-Origin', '*'); |
| 78 | + } |
| 79 | + $this->response->setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS, GET'); |
| 80 | + $this->response->setHeader('Access-Control-Allow-Credentials', 'true'); |
| 81 | + $this->response->setHeader( |
| 82 | + 'Access-Control-Allow-Headers', |
| 83 | + 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,' . |
| 84 | + 'Content-Type,Range,Token,token,Cookie,cookie,content-type,Authorization,authorization' |
| 85 | + ); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public function cors() |
| 90 | + { |
| 91 | + $this->checkCors(); |
| 92 | + return ''; |
| 93 | + } |
| 94 | + |
| 95 | + public function vfInformation() |
| 96 | + { |
| 97 | + |
| 98 | + $extensions = []; |
| 99 | + |
| 100 | + if ($this->moduleManager->isOutputEnabled('Magefan_Blog')) { |
| 101 | + $extensions[] = [ |
| 102 | + 'name' => 'Magefan Blog', |
| 103 | + 'version' => $this->moduleList->getOne('Magefan_Blog')['setup_version'], |
| 104 | + 'status' => $this->moduleManager->isEnabled('Magefan_Blog') |
| 105 | + ]; |
| 106 | + } else { |
| 107 | + $extensions[] = [ |
| 108 | + 'name' => 'Magefan Blog', |
| 109 | + 'version' => '', |
| 110 | + "status" => false |
| 111 | + ]; |
| 112 | + } |
| 113 | + |
| 114 | + $moduleDir = $this->moduleReader->getModuleDir( |
| 115 | + \Magento\Framework\Module\Dir::MODULE_VIEW_DIR, |
| 116 | + 'Vuefront_Vuefront' |
| 117 | + ); |
| 118 | + |
| 119 | + $catalog = $this->storeManager->getStore()->getBaseUrl() . "rest/V1/vuefront/graphql"; |
| 120 | + |
| 121 | + $is_apache = strpos($this->request->getServerValue("SERVER_SOFTWARE"), "Apache") !== false; |
| 122 | + |
| 123 | + return [ |
| 124 | + 'apache' => $is_apache, |
| 125 | + 'backup' => $moduleDir.'/adminhtml/web/js/backup/.htaccess.txt', |
| 126 | + 'htaccess' => true, |
| 127 | + 'status' => $this->zendFileExists->isValid($moduleDir . '/adminhtml/web/js/backup/.htaccess.txt'), |
| 128 | + 'phpversion' => phpversion(), |
| 129 | + 'plugin_version' => $this->moduleList->getOne('Vuefront_Vuefront')['setup_version'], |
| 130 | + 'extensions' => $extensions, |
| 131 | + 'cmsConnect' => $catalog, |
| 132 | + 'server' => $this->request->getServerValue("SERVER_SOFTWARE") |
| 133 | + ]; |
| 134 | + } |
| 135 | + |
| 136 | + public function vfTurnOn() |
| 137 | + { |
| 138 | + $catalog = $this->storeManager->getStore()->getBaseUrl(); |
| 139 | + |
| 140 | + try { |
| 141 | + $rootFolder = $this->driverFile->getRealPath($this->dir->getRoot()); |
| 142 | + |
| 143 | + $catalog_url_info = $this->zendHttp->parse($catalog); |
| 144 | + |
| 145 | + $catalog_path = $catalog_url_info->getPath(); |
| 146 | + $document_path = $catalog_path; |
| 147 | + if (!empty($this->request->getServerValue('DOCUMENT_ROOT'))) { |
| 148 | + $document_path = str_replace( |
| 149 | + $this->driverFile->getRealPath($this->request->getServerValue('DOCUMENT_ROOT')), |
| 150 | + '', |
| 151 | + $rootFolder |
| 152 | + ) . '/'; |
| 153 | + } |
| 154 | + |
| 155 | + if (strpos($this->request->getServerValue("SERVER_SOFTWARE"), "Apache") !== false) { |
| 156 | + |
| 157 | + if (!$this->zendFileExists->isValid($rootFolder . '/.htaccess')) { |
| 158 | + $this->driverFile->filePutContents($rootFolder.'/.htaccess', "Options +FollowSymlinks |
| 159 | +Options -Indexes |
| 160 | +<FilesMatch \"(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))\"> |
| 161 | +Require all denied |
| 162 | +</FilesMatch> |
| 163 | +RewriteEngine On |
| 164 | +RewriteBase ".$catalog_path." |
| 165 | +RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L] |
| 166 | +RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L] |
| 167 | +RewriteRule ^system/download/(.*) index.php?route=error/not_found [L] |
| 168 | +RewriteCond %{REQUEST_FILENAME} !-f |
| 169 | +RewriteCond %{REQUEST_FILENAME} !-d |
| 170 | +RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css) |
| 171 | +RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]"); |
| 172 | + } |
| 173 | + |
| 174 | + if (!$this->driverFile->isWritable($rootFolder . '/.htaccess')) { |
| 175 | + return [ |
| 176 | + 'error' => 'not_writable_htaccess' |
| 177 | + ]; |
| 178 | + } |
| 179 | + |
| 180 | + if ($this->zendFileExists->isValid($rootFolder . '/.htaccess')) { |
| 181 | + |
| 182 | + $paths = [ |
| 183 | + 'image', |
| 184 | + '.php', |
| 185 | + 'admin', |
| 186 | + 'catalog', |
| 187 | + '\/img\/.*\/', |
| 188 | + 'wp-json', |
| 189 | + 'wp-admin', |
| 190 | + 'wp-content', |
| 191 | + 'checkout', |
| 192 | + 'rest', |
| 193 | + 'static', |
| 194 | + 'order', |
| 195 | + 'themes\/', |
| 196 | + 'modules\/', |
| 197 | + 'js\/', |
| 198 | + '\/vuefront\/' |
| 199 | + ]; |
| 200 | + $inserting = "# VueFront scripts, styles and images |
| 201 | +RewriteCond %{REQUEST_URI} .*(_nuxt) |
| 202 | +RewriteCond %{REQUEST_URI} !.*/vuefront/_nuxt |
| 203 | +RewriteRule ^([^?]*) vuefront/$1 |
| 204 | +
|
| 205 | +# VueFront sw.js |
| 206 | +RewriteCond %{REQUEST_URI} .*(sw.js) |
| 207 | +RewriteCond %{REQUEST_URI} !.*/vuefront/sw.js |
| 208 | +RewriteRule ^([^?]*) vuefront/$1 |
| 209 | +
|
| 210 | +# VueFront favicon.ico |
| 211 | +RewriteCond %{REQUEST_URI} .*(favicon.ico) |
| 212 | +RewriteCond %{REQUEST_URI} !.*/vuefront/favicon.ico |
| 213 | +RewriteRule ^([^?]*) vuefront/$1 |
| 214 | +
|
| 215 | +# VueFront pages |
| 216 | +
|
| 217 | +# VueFront home page |
| 218 | +RewriteCond %{REQUEST_URI} !.*(".implode('|', $paths).") |
| 219 | +RewriteCond %{QUERY_STRING} !.*(rest_route) |
| 220 | +RewriteCond %{DOCUMENT_ROOT}".$document_path."vuefront/index.html -f |
| 221 | +RewriteRule ^$ vuefront/index.html [L] |
| 222 | +
|
| 223 | +RewriteCond %{REQUEST_URI} !.*(".implode('|', $paths).") |
| 224 | +RewriteCond %{QUERY_STRING} !.*(rest_route) |
| 225 | +RewriteCond %{DOCUMENT_ROOT}".$document_path."vuefront/index.html !-f |
| 226 | +RewriteRule ^$ vuefront/200.html [L] |
| 227 | +
|
| 228 | +# VueFront page if exists html file |
| 229 | +RewriteCond %{REQUEST_FILENAME} !-f |
| 230 | +RewriteCond %{REQUEST_FILENAME} !-d |
| 231 | +RewriteCond %{REQUEST_URI} !.*(".implode('|', $paths).") |
| 232 | +RewriteCond %{QUERY_STRING} !.*(rest_route) |
| 233 | +RewriteCond %{DOCUMENT_ROOT}".$document_path."vuefront/$1.html -f |
| 234 | +RewriteRule ^([^?]*) vuefront/$1.html [L,QSA] |
| 235 | +
|
| 236 | +# VueFront page if not exists html file |
| 237 | +RewriteCond %{REQUEST_FILENAME} !-f |
| 238 | +RewriteCond %{REQUEST_FILENAME} !-d |
| 239 | +RewriteCond %{REQUEST_URI} !.*(".implode('|', $paths).") |
| 240 | +RewriteCond %{QUERY_STRING} !.*(rest_route) |
| 241 | +RewriteCond %{DOCUMENT_ROOT}".$document_path."vuefront/$1.html !-f |
| 242 | +RewriteRule ^([^?]*) vuefront/200.html [L,QSA]"; |
| 243 | + |
| 244 | + $content = $this->driverFile->fileGetContents($rootFolder . '/.htaccess'); |
| 245 | + |
| 246 | + $moduleDir = $this->moduleReader->getModuleDir( |
| 247 | + \Magento\Framework\Module\Dir::MODULE_VIEW_DIR, |
| 248 | + 'Vuefront_Vuefront' |
| 249 | + ); |
| 250 | + |
| 251 | + if (!$this->driverFile->isDirectory($moduleDir . '/adminhtml/web/js/backup')) { |
| 252 | + $this->driverFile->createDirectory($moduleDir . '/adminhtml/web/js/backup'); |
| 253 | + } |
| 254 | + |
| 255 | + $this->driverFile->filePutContents( |
| 256 | + $moduleDir . '/adminhtml/web/js/backup/.htaccess.txt', |
| 257 | + $content |
| 258 | + ); |
| 259 | + |
| 260 | + preg_match('/# VueFront pages/m', $content, $matches); |
| 261 | + |
| 262 | + if (count($matches) == 0) { |
| 263 | + $content = preg_replace_callback('/RewriteBase\s.*$/m', function ($matches) use ($inserting) { |
| 264 | + return $matches[0] . PHP_EOL . $inserting . PHP_EOL; |
| 265 | + }, $content); |
| 266 | + |
| 267 | + $this->driverFile->filePutContents($rootFolder . '/.htaccess', $content); |
| 268 | + } |
| 269 | + } |
| 270 | + } |
| 271 | + } catch (\Exception $e) { |
| 272 | + return $e->getMessage(); |
| 273 | + } |
| 274 | + |
| 275 | + return $this->vfInformation(); |
| 276 | + } |
| 277 | + |
| 278 | + public function vfTurnOff() |
| 279 | + { |
| 280 | + $rootFolder = $this->driverFile->getRealPath($this->dir->getRoot()); |
| 281 | + |
| 282 | + $moduleDir = $this->moduleReader->getModuleDir( |
| 283 | + \Magento\Framework\Module\Dir::MODULE_VIEW_DIR, |
| 284 | + 'Vuefront_Vuefront' |
| 285 | + ); |
| 286 | + |
| 287 | + if (strpos($this->request->getServerValue("SERVER_SOFTWARE"), "Apache") !== false) { |
| 288 | + if ($this->zendFileExists->isValid($moduleDir . '/adminhtml/web/js/backup/.htaccess.txt')) { |
| 289 | + if (!$this->driverFile->isWritable($rootFolder . '/.htaccess') || |
| 290 | + !$this->driverFile->isWritable($moduleDir . '/adminhtml/web/js/backup/.htaccess.txt') |
| 291 | + ) { |
| 292 | + return [ |
| 293 | + 'error' => 'not_writable_htaccess' |
| 294 | + ]; |
| 295 | + } |
| 296 | + $content = $this->driverFile->fileGetContents($moduleDir . '/adminhtml/web/js/backup/.htaccess.txt'); |
| 297 | + $this->driverFile->filePutContents($rootFolder . '/.htaccess', $content); |
| 298 | + $this->file->rm($moduleDir . '/adminhtml/web/js/backup/.htaccess.txt'); |
| 299 | + } |
| 300 | + } |
| 301 | + |
| 302 | + return $this->vfInformation(); |
| 303 | + } |
| 304 | + |
| 305 | + public function vfUpdate() |
| 306 | + { |
| 307 | + try { |
| 308 | + $rootFolder = $this->driverFile->getRealPath($this->dir->getRoot()); |
| 309 | + $moduleDir = $this->moduleReader->getModuleDir( |
| 310 | + \Magento\Framework\Module\Dir::MODULE_VIEW_DIR, |
| 311 | + 'Vuefront_Vuefront' |
| 312 | + ); |
| 313 | + |
| 314 | + $this->driverFile->filePutContents( |
| 315 | + $moduleDir . '/adminhtml/web/js/download.tar', |
| 316 | + $this->driverFile->fileGetContents($this->request->getBodyParams()['url']) |
| 317 | + ); |
| 318 | + $this->file->rmdirRecursive($rootFolder . '/vuefront'); |
| 319 | + |
| 320 | + $this->arhiveTar->unpack($moduleDir . '/adminhtml/web/js/download.tar', $rootFolder . '/vuefront//'); |
| 321 | + |
| 322 | + $this->file->rm($moduleDir . '/adminhtml/web/js/download.tar'); |
| 323 | + } catch (\Exception $e) { |
| 324 | + return $e->getMessage(); |
| 325 | + } |
| 326 | + |
| 327 | + return $this->vfInformation(); |
| 328 | + } |
| 329 | + |
| 330 | + public function info() |
| 331 | + { |
| 332 | + $result = []; |
| 333 | + $this->checkCors(); |
| 334 | + |
| 335 | + switch ($this->request->getParam('id')) { |
| 336 | + case 'vf_information': |
| 337 | + $result = $this->vfInformation(); |
| 338 | + break; |
| 339 | + case 'vf_turn_on': |
| 340 | + $result = $this->vfTurnOn(); |
| 341 | + break; |
| 342 | + case 'vf_turn_off': |
| 343 | + $result = $this->vfTurnOff(); |
| 344 | + break; |
| 345 | + case 'vf_update': |
| 346 | + $result = $this->vfUpdate(); |
| 347 | + break; |
| 348 | + default: |
| 349 | + $result = []; |
| 350 | + break; |
| 351 | + } |
| 352 | + |
| 353 | + return $result; |
| 354 | + } |
| 355 | +} |
0 commit comments