/** * Upload an image * * @param integer $membershipId * @param array $image * string name * string type * string tmp_name * integer error * integer size * @param string $oldImage * @param boolean $deleteImage * @throws \Membership\Exception\MembershipException * @return void */ protected function uploadImage($membershipId, array $image, $oldImage = null, $deleteImage = false) { // upload the membership's image if (!empty($image['name'])) { // delete old image if ($oldImage) { if (true !== ($result = $this->deleteImage($oldImage))) { throw new MembershipException('Image deleting failed'); } } // upload a new one if (false === ($imageName = FileSystemUtility::uploadResourceFile($membershipId, $image, self::$imagesDir))) { throw new MembershipException('Avatar uploading failed'); } // resize the image ImageUtility::resizeResourceImage($imageName, self::$imagesDir, (int) ApplicationSettingService::getSetting('membership_image_width'), (int) ApplicationSettingService::getSetting('membership_image_height')); $update = $this->update()->table('membership_level')->set(['image' => $imageName])->where(['id' => $membershipId]); $statement = $this->prepareStatementForSqlObject($update); $statement->execute(); } elseif ($deleteImage && $oldImage) { // just delete the membership's image if (true !== ($result = $this->deleteImage($oldImage))) { throw new MembershipException('Image deleting failed'); } $update = $this->update()->table('membership_level')->set(['image' => null])->where(['id' => $membershipId]); $statement = $this->prepareStatementForSqlObject($update); $statement->execute(); } }
/** * Generate a file url * * @param string $fileName * @param array $options * string path * array filters * @return string */ public function __invoke($fileName, array $options) { $currentPath = $options['path'] . '/' . $fileName; // generate a directory navigation link if (is_dir(FileManagerBaseModel::getUserBaseFilesDir() . '/' . $options['path'] . '/' . $fileName)) { // get the directory url $directoryUrl = $this->getView()->url('application/page', ['controller' => $this->getView()->applicationRoute()->getParam('controller'), 'action' => $this->getView()->applicationRoute()->getParam('action')], ['force_canonical' => true, 'query' => ['path' => $currentPath] + $options['filters']]); return $this->getView()->partial('file-manager/patrial/directory-url', ['name' => $fileName, 'url' => $directoryUrl]); } // generate a file link return $this->getView()->partial('file-manager/patrial/file-url', ['file_extension' => FileSystemUtility::getFileExtension($fileName), 'name' => $fileName, 'url' => $this->view->serverUrl() . $this->view->basePath() . '/' . FileManagerBaseModel::getUserBaseFilesUrl() . '/' . $currentPath]); }
/** * Get form instance * * @return object */ public function getForm() { // get form builder if (!$this->form) { // add descriptions params $this->formElements['file']['description_params'] = [strtolower(SettingService::getSetting('file_manager_allowed_extensions')), FileSystemUtility::convertBytes((int) SettingService::getSetting('file_manager_allowed_size'))]; // add extra validators $this->formElements['file']['validators'] = [['name' => 'fileextension', 'options' => ['extension' => explode(',', strtolower(SettingService::getSetting('file_manager_allowed_extensions')))]], ['name' => 'filesize', 'options' => ['max' => (int) SettingService::getSetting('file_manager_allowed_size')]]]; $this->form = new ApplicationCustomFormBuilder($this->formName, $this->formElements, $this->translator, $this->ignoredElements, $this->notValidatedElements, $this->method); } return $this->form; }
/** * Test delete user home directory */ public function testDeleteUserHomeDirectory() { // test user data $data = ['nick_name' => Rand::getString(32), 'email' => Rand::getString(32), 'api_key' => Rand::getString(32), 'role' => AclModelBase::DEFAULT_ROLE_MEMBER, 'language' => null]; // create a test user $query = $this->userModel->insert()->into('user_list')->values($data); $statement = $this->userModel->prepareStatementForSqlObject($query); $statement->execute(); $testUserId = $this->userModel->getAdapter()->getDriver()->getLastGeneratedValue(); // create a test user's home directory $homeUserDirectory = FileManagerBaseModel::getUserBaseFilesDir($testUserId) . '/' . FileManagerBaseModel::getHomeDirectoryName(); FileSystemUtility::createDir($homeUserDirectory); // fire the delete user event UserEvent::fireUserDeleteEvent($testUserId, $data); // delete the created user $query = $this->userModel->delete()->from('user_list')->where(['user_id' => $testUserId]); $statement = $this->userModel->prepareStatementForSqlObject($query); $statement->execute(); // home directory must be deleted $this->assertFalse(file_exists($homeUserDirectory)); }
/** * Clear css cache * * @return boolean */ public static function clearCssCache() { try { return ApplicationFileSystem::deleteFiles(LayoutService::getLayoutCachePath()); } catch (Exception $e) { ApplicationErrorLogger::log($e); } return false; }
/** * Upload custom module * * @param array $formData * string login required * string password required * array module required * @param string $host * @return boolean|string */ public function uploadCustomModule(array $formData, $host) { $uploadResult = true; try { // create a tmp dir $tmpDirName = $this->generateTmpDir(); ApplicationFileSystemUtility::createDir($tmpDirName); // unzip a custom module into the tmp dir $this->unzipFiles($formData['module']['tmp_name'], $tmpDirName); // check the module's config if (!file_exists($tmpDirName . '/config.php')) { throw new ApplicationException('Cannot define the module\'s config file'); } // get the module's config $moduleConfig = (include $tmpDirName . '/config.php'); // get the module's path $modulePath = !empty($moduleConfig['module_path']) ? $tmpDirName . '/' . $moduleConfig['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'); } if (!file_exists($modulePath . '/' . $this->moduleInstallConfig) || !file_exists($modulePath . '/' . $this->moduleConfig)) { throw new ApplicationException('Module not found'); } // check the module layout existing $moduleLayoutPath = null; if (!empty($moduleConfig['layout_path'])) { $moduleLayoutPath = $tmpDirName . '/' . $moduleConfig['layout_path']; if (!file_exists($moduleLayoutPath) || !is_dir($moduleLayoutPath)) { throw new ApplicationException('Cannot define the module\'s layout path into the config file'); } } // check the module existing into modules and layouts dirs $moduleName = basename($modulePath); $globalModulePath = ApplicationService::getModulePath(false) . '/' . $moduleName; $moduleLayoutName = $moduleLayoutPath ? basename($moduleLayoutPath) : null; $localModulePath = APPLICATION_ROOT . '/' . $globalModulePath; $localModuleLayout = $moduleLayoutName ? ApplicationService::getBaseLayoutPath() . '/' . $moduleLayoutName : null; if (file_exists($localModulePath) || $localModuleLayout && file_exists($localModuleLayout)) { throw new ApplicationException('Module already uploaded'); } // upload the module via FTP $ftp = new ApplicationFtpUtility($host, $formData['login'], $formData['password']); $ftp->createDirectory($globalModulePath); $ftp->copyDirectory($modulePath, $globalModulePath); // upload the module's layout if ($moduleLayoutPath) { $globalModuleLayoutPath = basename(APPLICATION_PUBLIC) . '/' . ApplicationService::getBaseLayoutPath(false) . '/' . $moduleLayoutName; $ftp->createDirectory($globalModuleLayoutPath); $ftp->copyDirectory($moduleLayoutPath, $globalModuleLayoutPath); } } 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 custom module event if (true === $uploadResult) { ApplicationEvent::fireUploadCustomModuleEvent($moduleName); } return $uploadResult; }
/** * Delete an image * * @param string $imageName * @return boolean */ protected function deleteMiniPhotoGalleryImage($imageName) { $imageTypes = [self::$thumbnailsDir, self::$imagesDir]; // delete images foreach ($imageTypes as $path) { if (true !== ($result = FileSystemUtility::deleteResourceFile($imageName, $path))) { return $result; } } return true; }
/** * File size * * @param integer $bytes * @return string */ public function __invoke($bytes) { return FileSystemUtility::convertBytes($bytes); }
/** * Delete an membership's image * * @param string $imageName * @return boolean */ protected function deleteImage($imageName) { return FileSystemUtility::deleteResourceFile($imageName, self::$imagesDir); }
/** * Validate existing file * * @param $value * @param array $context * @return boolean */ public function validateExistingFile($value, array $context = []) { $newFilePath = $this->fullUserPath . $value . '.' . FileSystemUtility::getFileExtension($this->fileName); $oldFilePath = $this->fullFilePath . $this->fileName; if ($newFilePath != $oldFilePath) { if (file_exists($newFilePath)) { return is_dir($newFilePath); } } return true; }
/** * Delete an user's avatar * * @param string $avatarName * @return boolean */ protected function deleteUserAvatar($avatarName) { $avatarTypes = [self::$thumbnailsDir, self::$avatarsDir]; // delete avatar foreach ($avatarTypes as $path) { if (true !== ($result = FileSystemUtility::deleteResourceFile($avatarName, $path))) { return $result; } } return true; }
/** * Upload an image * * @param integer $imageId * @param array $image * string name * string type * string tmp_name * integer error * integer size * @param string $oldImage * @throws \MiniPhotoGallery\Exception\MiniPhotoGalleryException * @return void */ protected function uploadImage($imageId, array $image, $oldImage = null) { if (!empty($image['name'])) { // delete an old image if ($oldImage) { if (true !== ($result = $this->deleteMiniPhotoGalleryImage($oldImage))) { throw new MiniPhotoGalleryException('Image deleting failed'); } } // upload the image if (false === ($imageName = FileSystemUtility::uploadResourceFile($imageId, $image, self::$imagesDir))) { throw new MiniPhotoGalleryException('Image uploading failed'); } // resize the image ImageUtility::resizeResourceImage($imageName, self::$imagesDir, (int) SettingService::getSetting('miniphotogallery_thumbnail_width'), (int) SettingService::getSetting('miniphotogallery_thumbnail_height'), self::$thumbnailsDir); $update = $this->update()->table('miniphotogallery_image')->set(['image' => $imageName])->where(['id' => $imageId]); $statement = $this->prepareStatementForSqlObject($update); $statement->execute(); } }
/** * Upload image * * @param integer $imageId * @param array $image * string name * string type * string tmp_name * integer error * integer size * @param string $oldImage * @throws \Slideshow\Exception\SlideshowException * @return void */ protected function uploadImage($imageId, array $image, $oldImage = null) { if (!empty($image['name'])) { // delete an old image if ($oldImage) { if (true !== ($result = $this->deleteSlideShowImage($oldImage))) { throw new SlideshowException('Image deleting failed'); } } // upload the image if (false === ($imageName = FileSystemUtility::uploadResourceFile($imageId, $image, self::$imagesDir))) { throw new SlideshowException('Image uploading failed'); } $update = $this->update()->table('slideshow_image')->set(['image' => $imageName])->where(['id' => $imageId]); $statement = $this->prepareStatementForSqlObject($update); $statement->execute(); } }
/** * Upload an news image * * @param integer $newsId * @param array $image * string name * string type * string tmp_name * integer error * integer size * @param string $oldImage * @param boolean $deleteImage * @throws \News\Exception\NewsException * @return void */ protected function uploadImage($newsId, array $image, $oldImage = null, $deleteImage = false) { // upload the news's image if (!empty($image['name'])) { // delete an old image if ($oldImage) { if (true !== ($result = $this->deleteNewsImage($oldImage))) { throw new NewsException('Image deleting failed'); } } // upload the image if (false === ($imageName = FileSystemUtility::uploadResourceFile($newsId, $image, self::$imagesDir))) { throw new NewsException('Image uploading failed'); } // resize the image ImageUtility::resizeResourceImage($imageName, self::$imagesDir, (int) SettingService::getSetting('news_thumbnail_width'), (int) SettingService::getSetting('news_thumbnail_height'), self::$thumbnailsDir); ImageUtility::resizeResourceImage($imageName, self::$imagesDir, (int) SettingService::getSetting('news_image_width'), (int) SettingService::getSetting('news_image_height')); $update = $this->update()->table('news_list')->set(['image' => $imageName])->where(['id' => $newsId]); $statement = $this->prepareStatementForSqlObject($update); $statement->execute(); } elseif ($deleteImage && $oldImage) { // just delete the news's image if (true !== ($result = $this->deleteNewsImage($oldImage))) { throw new NewsException('Image deleting failed'); } $update = $this->update()->table('news_list')->set(['image' => null])->where(['id' => $newsId]); $statement = $this->prepareStatementForSqlObject($update); $statement->execute(); } }
/** * Upload layout updates * * @param array $formData * string login required * string password required * array layout required * @param string $host * @return array|string */ public function uploadLayoutUpdates(array $formData, $host) { $uploadResult = true; try { // create a tmp dir $tmpDirName = $this->generateTmpDir(); ApplicationFileSystemUtility::createDir($tmpDirName); // unzip a layout updates into the tmp dir $this->unzipFiles($formData['layout']['tmp_name'], $tmpDirName); // check the layout's config if (!file_exists($tmpDirName . '/update_layout_config.php')) { throw new LayoutException('Cannot define the layout\'s config file'); } // get the layout's config $updateLayoutConfig = (include $tmpDirName . '/update_layout_config.php'); // get updates params $layoutCompatable = !empty($updateLayoutConfig['compatable']) ? $updateLayoutConfig['compatable'] : null; $layoutName = !empty($updateLayoutConfig['layout_name']) ? $updateLayoutConfig['layout_name'] : null; $layoutVersion = !empty($updateLayoutConfig['version']) ? $updateLayoutConfig['version'] : null; $layoutVendor = !empty($updateLayoutConfig['vendor']) ? $updateLayoutConfig['vendor'] : null; $layoutVendorEmail = !empty($updateLayoutConfig['vendor_email']) ? $updateLayoutConfig['vendor_email'] : null; // check the layout existing if (!$layoutName) { throw new LayoutException('Layout not found'); } $layoutInstalled = true; // get layout info from db if (null == ($layoutInfo = $this->getLayoutInfo($layoutName))) { // get info from config if (false === ($layoutInfo = $this->getCustomLayoutInstallConfig($layoutName))) { // nothing to update throw new LayoutException('Layout not found'); } $layoutInstalled = false; } // compare the layout options if (!$layoutVendor || !$layoutVendorEmail || empty($layoutInfo['vendor']) || empty($layoutInfo['vendor_email']) || strcasecmp($layoutVendor, $layoutInfo['vendor']) != 0 || strcasecmp($layoutVendorEmail, $layoutInfo['vendor_email']) != 0) { throw new LayoutException('Layout not found'); } if (!$layoutCompatable || true !== ($result = version_compare(SettingService::getSetting('application_generator_version'), $layoutCompatable, '>='))) { throw new LayoutException('These updates are not compatible with current CMS version'); } // compare the layout versions if (!$layoutVersion || empty($layoutInfo['version']) || version_compare($layoutVersion, $layoutInfo['version']) <= 0) { throw new LayoutException('This layout updates are not necessary or not defined'); } // clear caches $this->clearLayoutCaches(); // upload layout's updates $this->uploadLayoutFiles($layoutName, $updateLayoutConfig, $tmpDirName, $host, $formData, false); // update version if ($layoutInstalled) { $update = $this->update()->table('layout_list')->set(['version' => $layoutVersion])->where(['name' => $layoutName]); $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 layout updates event if (true === $uploadResult) { LayoutEvent::fireUploadLayoutUpdatesEvent($layoutName); } // return an error description return $uploadResult; }
/** * Delete an image * * @param string $imageName * @return boolean */ protected function deleteSlideShowImage($imageName) { if (true !== ($result = FileSystemUtility::deleteResourceFile($imageName, self::$imagesDir))) { return $result; } return true; }
/** * Get the user's list of directories * * @return array|boolean */ public function getUserDirectories() { $baseDir = self::getUserBaseFilesDir(); $homeDir = $baseDir . '/' . self::$homeDirectoryName; // check the home directory existing if (!file_exists($homeDir)) { // create a new home directory try { FileSystemUtility::createDir($homeDir); } catch (Exception $e) { ApplicationErrorLogger::log($e); return false; } } $iterator = new RecursiveDirectoryIterator($baseDir, RecursiveDirectoryIterator::SKIP_DOTS); $files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST); $directories = []; // get list of user's directories foreach ($files as $file) { if (!$file->isDir()) { continue; } // PHP_EOL - fix for array_merge_recursive (values should be as strings) $path = [$file->getFilename() . PHP_EOL => []]; for ($depth = $files->getDepth() - 1; $depth >= 0; $depth--) { $path = [$files->getSubIterator($depth)->current()->getFilename() . PHP_EOL => $path]; } $directories = array_merge_recursive($directories, $path); } return $directories; }