|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * 2019 (c) VueFront |
| 4 | + * |
| 5 | + * MODULE VueFront |
| 6 | + * |
| 7 | + * @author VueFront |
| 8 | + * @copyright Copyright (c) permanent, VueFront |
| 9 | + * @license MIT |
| 10 | + * @version 0.1.0 |
| 11 | + */ |
| 12 | + |
| 13 | +class AdminVuefrontAjaxController extends ModuleAdminController |
| 14 | +{ |
| 15 | + |
| 16 | + public function ajaxProcessVfTurnOff() |
| 17 | + { |
| 18 | + if (strpos($_SERVER["SERVER_SOFTWARE"], "Apache") !== false) { |
| 19 | + if (file_exists(_PS_ROOT_DIR_ . '/modules/vuefront/.htaccess.txt')) { |
| 20 | + $content = file_get_contents(_PS_ROOT_DIR_ . '/modules/vuefront/.htaccess.txt'); |
| 21 | + file_put_contents(_PS_ROOT_DIR_ . '/.htaccess', $content); |
| 22 | + unlink(_PS_ROOT_DIR_ . '/modules/vuefront/.htaccess.txt'); |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + $this->ajaxProcessVfInformation(); |
| 27 | + } |
| 28 | + |
| 29 | + public function ajaxProcessVfTurnOn() { |
| 30 | + $catalog = Tools::getHttpHost(true). __PS_BASE_URI__; |
| 31 | + try { |
| 32 | + $catalog_url_info = parse_url($catalog); |
| 33 | + |
| 34 | + $catalog_path = $catalog_url_info['path']; |
| 35 | + |
| 36 | + if (strpos($_SERVER["SERVER_SOFTWARE"], "Apache") !== false) { |
| 37 | + |
| 38 | + if(!file_exists(_PS_ROOT_DIR_ . '/.htaccess')) { |
| 39 | + file_put_contents(_PS_ROOT_DIR_.'/.htaccess', "Options +FollowSymlinks |
| 40 | +Options -Indexes |
| 41 | +<FilesMatch \"(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))\"> |
| 42 | +Require all denied |
| 43 | +</FilesMatch> |
| 44 | +RewriteEngine On |
| 45 | +RewriteBase ".$catalog_path." |
| 46 | +RewriteRule ^sitemap.xml$ index.php?route=extension/feed/google_sitemap [L] |
| 47 | +RewriteRule ^googlebase.xml$ index.php?route=extension/feed/google_base [L] |
| 48 | +RewriteRule ^system/download/(.*) index.php?route=error/not_found [L] |
| 49 | +RewriteCond %{REQUEST_FILENAME} !-f |
| 50 | +RewriteCond %{REQUEST_FILENAME} !-d |
| 51 | +RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css) |
| 52 | +RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]"); |
| 53 | + } |
| 54 | + |
| 55 | + if (file_exists(_PS_ROOT_DIR_ . '/.htaccess')) { |
| 56 | + $inserting = "# VueFront scripts, styles and images |
| 57 | +RewriteCond %{REQUEST_URI} .*(_nuxt) |
| 58 | +RewriteCond %{REQUEST_URI} !.*/vuefront/_nuxt |
| 59 | +RewriteRule ^([^?]*) vuefront/$1 |
| 60 | +
|
| 61 | +# VueFront pages |
| 62 | +
|
| 63 | +# VueFront home page |
| 64 | +RewriteCond %{REQUEST_URI} !.*(images|index.php|.html|admin|.js|.css|.png|.jpeg|.ico|wp-json|wp-admin|checkout) |
| 65 | +RewriteCond %{QUERY_STRING} !.*(rest_route) |
| 66 | +RewriteCond %{DOCUMENT_ROOT}".$catalog_path."vuefront/index.html -f |
| 67 | +RewriteRule ^$ vuefront/index.html [L] |
| 68 | +
|
| 69 | +RewriteCond %{REQUEST_URI} !.*(images|index.php|.html|admin|.js|.css|.png|.jpeg|.ico|wp-json|wp-admin|checkout) |
| 70 | +RewriteCond %{QUERY_STRING} !.*(rest_route) |
| 71 | +RewriteCond %{DOCUMENT_ROOT}".$catalog_path."vuefront/index.html !-f |
| 72 | +RewriteRule ^$ vuefront/200.html [L] |
| 73 | +
|
| 74 | +# VueFront page if exists html file |
| 75 | +RewriteCond %{REQUEST_FILENAME} !-f |
| 76 | +RewriteCond %{REQUEST_FILENAME} !-d |
| 77 | +RewriteCond %{REQUEST_URI} !.*(images|index.php|.html|admin|.js|.css|.png|.jpeg|.ico|wp-json|wp-admin|checkout) |
| 78 | +RewriteCond %{QUERY_STRING} !.*(rest_route) |
| 79 | +RewriteCond %{DOCUMENT_ROOT}".$catalog_path."vuefront/$1.html -f |
| 80 | +RewriteRule ^([^?]*) vuefront/$1.html [L,QSA] |
| 81 | +
|
| 82 | +# VueFront page if not exists html file |
| 83 | +RewriteCond %{REQUEST_FILENAME} !-f |
| 84 | +RewriteCond %{REQUEST_FILENAME} !-d |
| 85 | +RewriteCond %{REQUEST_URI} !.*(images|index.php|.html|admin|.js|.css|.png|.jpeg|.ico|wp-json|wp-admin|checkout) |
| 86 | +RewriteCond %{QUERY_STRING} !.*(rest_route) |
| 87 | +RewriteCond %{DOCUMENT_ROOT}".$catalog_path."vuefront/$1.html !-f |
| 88 | +RewriteRule ^([^?]*) vuefront/200.html [L,QSA]"; |
| 89 | + |
| 90 | + $content = file_get_contents(_PS_ROOT_DIR_ . '/.htaccess'); |
| 91 | + |
| 92 | + file_put_contents(_PS_ROOT_DIR_ . '/modules/vuefront/.htaccess.txt', $content); |
| 93 | + |
| 94 | + preg_match('/# VueFront pages/m', $content, $matches); |
| 95 | + |
| 96 | + if (count($matches) == 0) { |
| 97 | + $content = preg_replace_callback('/#Domain:\s.*$/m', function ($matches) use ($inserting) { |
| 98 | + return $matches[0] . PHP_EOL . $inserting . PHP_EOL; |
| 99 | + }, $content); |
| 100 | + |
| 101 | + file_put_contents(_PS_ROOT_DIR_ . '/.htaccess', $content); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } catch (\Exception $e) { |
| 106 | + echo $e->getMessage(); |
| 107 | + } |
| 108 | + |
| 109 | + $this->ajaxProcessVfInformation(); |
| 110 | + } |
| 111 | + |
| 112 | + public function ajaxProcessVfUpdate() { |
| 113 | + try { |
| 114 | + $tmpFile = tempnam(sys_get_temp_dir(), 'TMP_'); |
| 115 | + rename($tmpFile, $tmpFile .= '.tar'); |
| 116 | + file_put_contents($tmpFile, file_get_contents($_POST['url'])); |
| 117 | + $this->removeDir(_PS_ROOT_DIR_ . '/vuefront'); |
| 118 | + $phar = new PharData($tmpFile); |
| 119 | + $phar->extractTo(_PS_ROOT_DIR_ . '/vuefront'); |
| 120 | + } catch (\Exception $e) { |
| 121 | + echo $e->getMessage(); |
| 122 | + } |
| 123 | + |
| 124 | + $this->ajaxProcessVfInformation(); |
| 125 | + } |
| 126 | + |
| 127 | + private function removeDir($dir) |
| 128 | + { |
| 129 | + if (is_dir($dir)) { |
| 130 | + $objects = scandir($dir); |
| 131 | + foreach ($objects as $object) { |
| 132 | + if ($object != "." && $object != "..") { |
| 133 | + if (is_dir($dir . "/" . $object) && !is_link($dir . "/" . $object)) { |
| 134 | + $this->removeDir($dir . "/" . $object); |
| 135 | + } else { |
| 136 | + unlink($dir . "/" . $object); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + rmdir($dir); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public function ajaxProcessVfInformation() { |
| 145 | + $extensions = []; |
| 146 | + |
| 147 | + $moduleInstance = Module::getInstanceByName('vuefront'); |
| 148 | + |
| 149 | + if (Module::isInstalled('prestablog')) { |
| 150 | + $blogInstance = Module::getInstanceByName('prestablog'); |
| 151 | + $extensions[] = [ |
| 152 | + 'name' => Module::getModuleName('prestablog'), |
| 153 | + 'version' => $blogInstance->version, |
| 154 | + 'status' => Module::isInstalled('prestablog') |
| 155 | + ]; |
| 156 | + } else { |
| 157 | + $extensions[] = [ |
| 158 | + 'name' => Module::getModuleName('prestablog'), |
| 159 | + 'version' => '', |
| 160 | + 'status' => Module::isInstalled('prestablog') |
| 161 | + ]; |
| 162 | + } |
| 163 | + |
| 164 | + $is_apache = strpos($_SERVER["SERVER_SOFTWARE"], "Apache") !== false; |
| 165 | + $status = false; |
| 166 | + if(file_exists(_PS_ROOT_DIR_.'/modules/vuefront/.htaccess.txt')) { |
| 167 | + $status = true; |
| 168 | + } |
| 169 | + die( |
| 170 | + Tools::jsonEncode( |
| 171 | + [ |
| 172 | + 'apache' => $is_apache, |
| 173 | + 'backup' => 'modules/vuefront/.htaccess.txt', |
| 174 | + 'htaccess' => file_exists(_PS_ROOT_DIR_ . '/.htaccess'), |
| 175 | + 'status' => $status, |
| 176 | + 'phpversion' => phpversion(), |
| 177 | + 'plugin_version' => $moduleInstance->version, |
| 178 | + 'extensions' => $extensions, |
| 179 | + 'cmsConnect' => Tools::getHttpHost(true). |
| 180 | + __PS_BASE_URI__.'index.php?controller=graphql&module=vuefront&fc=module', |
| 181 | + 'server' => $_SERVER["SERVER_SOFTWARE"] |
| 182 | + ] |
| 183 | + ) |
| 184 | + ); |
| 185 | + } |
| 186 | + |
| 187 | + public function ajaxProcessProxy() |
| 188 | + { |
| 189 | + $body = Tools::file_get_contents('php://input');; |
| 190 | + if (!function_exists('getallheaders')) { |
| 191 | + function getallheaders() |
| 192 | + { |
| 193 | + $headers = []; |
| 194 | + foreach ($_SERVER as $name => $value) { |
| 195 | + if (substr($name, 0, 5) == 'HTTP_') { |
| 196 | + $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; |
| 197 | + } |
| 198 | + } |
| 199 | + return $headers; |
| 200 | + } |
| 201 | + } |
| 202 | + $headers = getallheaders(); |
| 203 | + |
| 204 | + $cHeaders = array('Content-Type: application/json'); |
| 205 | + |
| 206 | + if(!empty($headers['token'])) { |
| 207 | + $cHeaders[] = 'token: '.$headers['token']; |
| 208 | + } |
| 209 | + if(!empty($headers['Token'])) { |
| 210 | + $cHeaders[] = 'token: '.$headers['Token']; |
| 211 | + } |
| 212 | + $ch = curl_init(); |
| 213 | + curl_setopt($ch, CURLOPT_URL, 'https://api.vuefront.com/graphql'); |
| 214 | + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); |
| 215 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 216 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
| 217 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
| 218 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $cHeaders); |
| 219 | + $result = curl_exec($ch); |
| 220 | + curl_close($ch); |
| 221 | + die($result); |
| 222 | + } |
| 223 | +} |
0 commit comments