/** * Удаление файла * * @param $clusterFileSrc * @return bool * @throws Exception */ public function delete($clusterFileUniqSrc) { $file = new File($this->getRootSrc($clusterFileUniqSrc)); if ($file->isExist()) { $file->remove(); } return true; }
/** * Lists all StorageFile models. * @return mixed */ public function actionProcess() { ini_set("memory_limit", "512M"); $imaging = \Yii::$app->imaging; if (!$imaging) { //TODO: можно добавить проверку YII ensure... throw new \yii\base\Exception("Component Imaging not found"); } $newFileSrc = \Yii::$app->request->getPathInfo(); $extension = Imaging::getExtension($newFileSrc); if (!$extension) { throw new \yii\base\Exception("Extension not found: " . $newFileSrc); } if (!$imaging->isAllowExtension($extension)) { throw new \yii\base\Exception("Extension '{$extension}' not supported in Imaging component"); } $newFile = File::object($newFileSrc); $strposFilter = strpos($newFileSrc, "/" . Imaging::THUMBNAIL_PREFIX); if (!$strposFilter) { throw new \ErrorException("This is not a filter thumbnail: " . $newFileSrc); } $originalFileSrc = substr($newFileSrc, 0, $strposFilter) . "." . $newFile->getExtension(); $webRoot = \Yii::getAlias('@webroot'); $originalFileRoot = $webRoot . DIRECTORY_SEPARATOR . $originalFileSrc; $newFileRoot = $webRoot . DIRECTORY_SEPARATOR . $newFileSrc; $newFileRootDefault = $webRoot . DIRECTORY_SEPARATOR . str_replace($newFile->getBaseName(), Imaging::DEFAULT_THUMBNAIL_FILENAME . "." . $extension, $newFileSrc); $originalFile = new File($originalFileRoot); if (!$originalFile->isExist()) { throw new \ErrorException("The original file is not found: " . $newFileSrc); } //Проверено наличие оригинального файла, есть пути к оригиналу, и результирующему файлу. //Отслось собрать фильтр, и проверить наличие параметров. А так же проверить разрешены ли эти параметры, для этого в строке есть захэшированный ключь $filterSting = substr($newFileSrc, $strposFilter + strlen(DIRECTORY_SEPARATOR . Imaging::THUMBNAIL_PREFIX), strlen($newFileSrc)); $filterCode = explode("/", $filterSting); $filterCode = $filterCode[0]; //Код фильтра //Если указаны парамтры, то ноужно проверить контрольную строчку, и если они не соответствуют ей, то ничего делать не будем if ($params = \Yii::$app->request->get()) { $pramsCheckArray = explode(DIRECTORY_SEPARATOR, $filterSting); if (count($pramsCheckArray) < 3) { throw new \yii\base\Exception("the control line not found: " . $newFileSrc); } $string = $imaging->getParamsCheckString($params); if ($pramsCheckArray[1] != $string) { throw new \yii\base\Exception("Parameters invalid: " . $newFileSrc); } } $filterClass = str_replace("-", "\\", $filterCode); if (!class_exists($filterClass)) { throw new \ErrorException("Filter class is not created: " . $newFileSrc); } /** * @var Filter $filter */ $filter = new $filterClass((array) $params); if (!is_subclass_of($filter, Filter::className())) { throw new \ErrorException("No child filter class: " . $newFileSrc); } try { //Проверяем а создан ли уже файл, и если да то просто делаем на него ссылку. $filter->setOriginalRootFilePath($originalFileRoot)->setNewRootFilePath($newFileRootDefault)->save(); if (PHP_OS === 'Windows') { if ($newFileRoot != $newFileRootDefault) { //Не тестировалось copy($newFileRootDefault, $newFileRoot); } } else { if ($newFileRoot != $newFileRootDefault) { symlink($newFileRootDefault, $newFileRoot); } } $url = \Yii::$app->request->getUrl() . ($params ? "" : '?sx-refresh'); /*Header("HTTP/1.0 200 OK"); Image::getImagine()->open($newFileRootDefault)->show('png'); return '';*/ return \Yii::$app->response->redirect($url, 302); } catch (\Exception $e) { return $e->getMessage(); } }
/** * * Взять все установленные расширения, и вернуть пути к конфигам * * @param string|array $fileName * @return array */ public function findConfigFiles($fileName = '/config/main.php') { $config = []; $fileNames = []; if (is_string($fileName)) { $fileNames[] = $fileName; } else { if (is_array($fileName)) { $fileNames = $fileName; } } foreach ((array) \Yii::$app->extensions as $code => $data) { if (is_array($data['alias'])) { $configTmp = []; foreach ($data['alias'] as $code => $path) { foreach ($fileNames as $fileName) { $file = new \skeeks\sx\File($path . $fileName); if ($file->isExist()) { $config[] = $file->getPath(); } } } } } return $config; }
/** * Пройтись по всем расширениям уставноленным в проект, и сгенерировать конфиг файл. * @return bool */ public function generateModulesConfigFile() { $configs = $this->findConfigFiles(['/config/main.php']); $configsConsole = $this->findConfigFiles(['/config/main-console.php']); if ($configs || $configsConsole) { $date = date("dd.mm.YY", time()); $fileContent = <<<PHP <?php /** * Автоматически сгенерированные конфиг, можно просто удалить этот файл. * * @author Semenov Alexander <*****@*****.**> * @link http://skeeks.com/ * @copyright 2010-2014 SkeekS (Sx) * @date {$date} * @since 1.0.0 */ return [ PHP; $fileContent .= "'web' => [\n"; foreach ($configs as $filePach) { $fileContent .= "\"" . $filePach . "\", \n"; } $fileContent .= "],\n"; $fileContent .= "'console' => [\n"; foreach ($configsConsole as $filePach) { $fileContent .= "\"" . $filePach . "\", \n"; } $fileContent .= "]\n"; $fileContent .= '];'; $file = new File(AUTO_GENERATED_MODULES_FILE); $file->write($fileContent); } $file = new File(AUTO_GENERATED_MODULES_FILE); return $file->isExist(); }
/** * Logs in a user using the provided username and password. * * @return boolean whether the user is logged in successfully */ public function write() { if ($this->validate() && $this->hasConnect()) { $fileContent = <<<PHP <?php /** * @author Semenov Alexander <*****@*****.**> * @link http://skeeks.com/ * @copyright 2010 SkeekS (СкикС) * @date 17.09.2015 */ return [ 'class' => 'yii\\db\\Connection', 'dsn' => 'mysql:host={$this->host};dbname={$this->dbname}', 'username' => '{$this->username}', 'password' => '{$this->password}', 'charset' => '{$this->charset}', 'enableSchemaCache' => true, 'schemaCacheDuration' => 3600, ]; PHP; $file = new File(Yii::getAlias('@common/config/db.php')); $file->write($fileContent); if ($file->isExist()) { return true; } return false; } else { return false; } }