/** * Resize a resource image * * @param string $file * @param string $path * @param integer $width * @param integer $height * @param string $newPath * @param boolean $saveProportions * @return void */ public static function resizeResourceImage($file, $path, $width, $height, $newPath = null, $saveProportions = true) { $filePath = ApplicationService::getResourcesDir() . $path . $file; // resize the avatar $imagine = new Imagine\Gd\Imagine(); $size = new Imagine\Image\Box($width, $height); $mode = $saveProportions ? Imagine\Image\ImageInterface::THUMBNAIL_INSET : Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND; $imagine->open($filePath)->thumbnail($size, $mode)->save($newPath ? ApplicationService::getResourcesDir() . $newPath . $file : $filePath); }
/** * Upload a resource file * * @param integer|string $objectId * @param array $file * string name * string type * string tmp_name * integer error * integer size * @param string $path * @param boolean $addSalt * @param integer $saltLength * @return string|boolean */ public static function uploadResourceFile($objectId, $file, $path, $addSalt = true, $saltLength = 5) { $fileInfo = pathinfo($file['name']); $salt = $addSalt ? '_' . SlugUtility::generateRandomSlug($saltLength) : null; $fileName = $objectId . $salt . (!empty($fileInfo['extension']) ? '.' . strtolower($fileInfo['extension']) : null); $basePath = ApplicationService::getResourcesDir() . $path; if (is_writable($basePath)) { if (true === ($result = move_uploaded_file($file['tmp_name'], $basePath . $fileName))) { @chmod($basePath . $fileName, self::DEFAULT_FILE_PERMISSIONS); return $fileName; } } return false; }
/** * Upload module updates * * @param array $formData * string login required * string password required * array module required * @param string $host * @return array|string */ public function uploadModuleUpdates(array $formData, $host) { $uploadResult = true; try { // create a tmp dir $tmpDirName = $this->generateTmpDir(); ApplicationFileSystemUtility::createDir($tmpDirName); // unzip a module updates into the tmp dir $this->unzipFiles($formData['module']['tmp_name'], $tmpDirName); // check the module's config if (!file_exists($tmpDirName . '/update_config.php')) { throw new ApplicationException('Cannot define the module\'s config file'); } // get the module's config $updateModuleConfig = (include $tmpDirName . '/update_config.php'); $moduleCompatable = !empty($updateModuleConfig['compatable']) ? $updateModuleConfig['compatable'] : null; $moduleName = !empty($updateModuleConfig['module']) ? $updateModuleConfig['module'] : null; $moduleVersion = !empty($updateModuleConfig['version']) ? $updateModuleConfig['version'] : null; $moduleVendor = !empty($updateModuleConfig['vendor']) ? $updateModuleConfig['vendor'] : null; $moduleVendorEmail = !empty($updateModuleConfig['vendor_email']) ? $updateModuleConfig['vendor_email'] : null; // check the module existing if (!$moduleName) { throw new ApplicationException('Module not found'); } $moduleInstalled = true; // get module info from db if (null == ($moduleInfo = $this->getModuleInfo($moduleName))) { // get info from config if (false === ($moduleInfo = $this->getCustomModuleInstallConfig($moduleName))) { // nothing to update throw new ApplicationException('Module not found'); } $moduleInstalled = false; } // compare the modules options if (!$moduleVendor || !$moduleVendorEmail || empty($moduleInfo['vendor']) || empty($moduleInfo['vendor_email']) || strcasecmp($moduleVendor, $moduleInfo['vendor']) != 0 || strcasecmp($moduleVendorEmail, $moduleInfo['vendor_email']) != 0) { throw new ApplicationException('Module not found'); } if (!$moduleCompatable || true !== ($result = version_compare(SettingService::getSetting('application_generator_version'), $moduleCompatable, '>='))) { throw new ApplicationException('These updates are not compatible with current CMS version'); } // compare the module versions if (!$moduleVersion || empty($moduleInfo['version']) || version_compare($moduleVersion, $moduleInfo['version']) <= 0) { throw new ApplicationException('This module updates are not necessary or not defined'); } // get the module's path $modulePath = !empty($updateModuleConfig['module_path']) ? $tmpDirName . '/' . $updateModuleConfig['module_path'] : null; // check the module existing if ($modulePath && (!file_exists($modulePath) || !is_dir($modulePath))) { throw new ApplicationException('Cannot define the module\'s path into the config file'); } // check the module layout existing $moduleLayoutPath = !empty($updateModuleConfig['layout_path']) && $moduleInfo['layout_path'] ? $tmpDirName . '/' . $updateModuleConfig['layout_path'] : null; if ($moduleLayoutPath && (!file_exists($moduleLayoutPath) || !is_dir($moduleLayoutPath))) { throw new ApplicationException('Cannot define the module\'s layout path into the config file'); } // check system requirements $requirements = $this->getNotValidatedModuleSystemRequirements($updateModuleConfig); if (count($requirements)) { throw new ApplicationException('You have to install all system requirements before apply these module updates. Check the readme file'); } // check the module depends if (false === ($result = $this->checkModuleDepends($updateModuleConfig))) { throw new ApplicationException('You have to install all dependent modules before apply these module updates. Check the readme file'); } // install updates if ($moduleInstalled) { // clear caches $this->clearCaches(!empty($updateModuleConfig['clear_caches']) ? $updateModuleConfig['clear_caches'] : []); // create resources dirs if (!empty($updateModuleConfig['create_resources'])) { $this->createResourceDirs($updateModuleConfig['create_resources']); } // execute an update sql file if (!empty($updateModuleConfig['update_sql'])) { $this->executeSqlFile($tmpDirName . '/' . $updateModuleConfig['update_sql']); } // delete unnecessary resources dirs if (!empty($updateModuleConfig['delete_resources'])) { foreach ($updateModuleConfig['delete_resources'] as $dir) { if (!empty($dir['dir_name'])) { if (file_exists(ApplicationService::getResourcesDir() . $dir['dir_name'])) { ApplicationFileSystemUtility::deleteFiles(ApplicationService::getResourcesDir() . $dir['dir_name'], [], false, true); } } } } } // update files via FTP if ($modulePath || $moduleLayoutPath && $moduleInfo['layout_path']) { $ftp = new ApplicationFtpUtility($host, $formData['login'], $formData['password']); if ($modulePath) { $ftp->copyDirectory($modulePath, ApplicationService::getModulePath(false) . '/' . $moduleName); } if ($moduleLayoutPath && $moduleInfo['layout_path']) { $ftp->copyDirectory($moduleLayoutPath, basename(APPLICATION_PUBLIC) . '/' . ApplicationService::getBaseLayoutPath(false) . '/' . $moduleInfo['layout_path']); } } if ($moduleInstalled) { // update version $update = $this->update()->table('application_module')->set(['version' => $moduleVersion])->where(['name' => $moduleName]); $statement = $this->prepareStatementForSqlObject($update); $statement->execute(); } } catch (Exception $e) { ApplicationErrorLogger::log($e); $uploadResult = $e->getMessage(); } // remove the tmp dir if (file_exists($tmpDirName)) { ApplicationFileSystemUtility::deleteFiles($tmpDirName, [], false, true); } // fire the upload module updates event if (true === $uploadResult) { ApplicationEvent::fireUploadModuleUpdatesEvent($moduleName); return $updateModuleConfig; } // return an error description return $uploadResult; }
/** * Get user's base files dir * * @param integer $userId * @return string */ public static function getUserBaseFilesDir($userId = null) { return ApplicationService::getResourcesDir() . self::getUserFilesDir($userId); }