/** * Save upload editor * * @return void */ public function save() { $parameters = $this->getConfig(); $data = array(); if (!empty($_FILES[$this->getName()]['name'])) { $oldFiles = $_FILES; $file = $_FILES[$this->getName()]; //Ignore others data $_FILES = array(); $_FILES[$this->getName()] = $file; $fileClass = new File(); $fileClass->load($this->getProperty(), $this->getDatatype()->getDocument(), $this->getName()); $fileClass->upload(); $files = $fileClass->getFiles(); if (!empty($files)) { foreach ($files as $file) { $name = $file['filename']; $file = $fileClass->getPath() . '/' . $name; if (file_exists($file)) { $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME; $finfo = finfo_open($const); // return mimetype extension if (!in_array(finfo_file($finfo, $file), $parameters['mime_list'])) { unlink($file); } else { $fileInfo = @getimagesize($file); $data[] = array('value' => $name, 'width' => empty($fileInfo[0]) ? 0 : $fileInfo[0], 'height' => empty($fileInfo[1]) ? 0 : $fileInfo[1], 'html' => empty($fileInfo[2]) ? '' : $fileInfo[2], 'mime' => empty($fileInfo['mime']) ? '' : $fileInfo['mime']); } finfo_close($finfo); } } if (empty($parameters['is_multiple']) and !empty($data[0])) { $data = $data[0]; } $data = serialize($data); } //Restore file data $_FILES = $oldFiles; } else { $data = $this->getRequest()->getPost()->get($this->getName() . '-hidden'); } $this->setValue(empty($data) ? '' : $data); }
/** * Remove directories * * @return void */ protected function removeDirectories() { $dir = $this->object->getPath() . $this->object->getDirectory(); if (is_dir($dir)) { $data = glob($dir . '/*'); foreach ($data as $file) { unlink($file); } $tmpDir = $dir; while ($tmpDir != GC_MEDIA_PATH . '/files') { rmdir($tmpDir); $tmpDir = realpath(dirname($tmpDir)); } } }
/** * Save Image cropper editor * * @return void */ public function save() { $post = $this->getRequest()->getPost(); $parameters = $this->getConfig(); $data = array(); $imageModel = new Image(); $fileClass = new File(); $fileClass->load($this->getProperty(), $this->getDatatype()->getDocument(), $this->getName()); $backgroundColor = empty($parameters['background']) ? '#000000' : $parameters['background']; if (!empty($_FILES[$this->getName()]['name'])) { $oldFiles = $_FILES; $file = $_FILES[$this->getName()]; //Ignore others data $_FILES = array(); $_FILES[$this->getName()] = $file; $fileClass->upload(); $files = $fileClass->getFiles(); if (!empty($files) and is_array($files)) { foreach ($files as $file) { $name = $file['filename']; $file = $fileClass->getPath() . $name; if (file_exists($file)) { $const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME; $finfo = finfo_open($const); // return mimetype extension if (!in_array(finfo_file($finfo, $file), $parameters['mime_list'])) { unlink($file); } else { $fileInfo = @getimagesize($file); $data['original'] = array('value' => $name, 'width' => empty($fileInfo[0]) ? 0 : $fileInfo[0], 'height' => empty($fileInfo[1]) ? 0 : $fileInfo[1], 'html' => empty($fileInfo[2]) ? '' : $fileInfo[2], 'mime' => empty($fileInfo['mime']) ? '' : $fileInfo['mime']); $imageModel->open($file); foreach ($parameters['size'] as $size) { $imageModel->open($file); $imageModel->resize($size['width'], $size['height'], empty($parameters['resize_option']) ? 'auto' : $parameters['resize_option'], $backgroundColor); $sizeFilename = preg_replace('~\\.([a-zA-Z]+)$~', '-' . $size['name'] . '.$1', $name); $imageModel->save($fileClass->getPath() . $sizeFilename); $fileInfo = @getimagesize($fileClass->getPath() . $sizeFilename); $data[$size['name']] = array('value' => $sizeFilename, 'width' => empty($fileInfo[0]) ? 0 : $fileInfo[0], 'height' => empty($fileInfo[1]) ? 0 : $fileInfo[1], 'html' => empty($fileInfo[2]) ? '' : $fileInfo[2], 'mime' => empty($fileInfo['mime']) ? '' : $fileInfo['mime'], 'x' => 0, 'y' => 0); } } finfo_close($finfo); } } } //Restore file data $_FILES = $oldFiles; } else { $data = $post->get($this->getName() . '-hidden'); $data = unserialize($data); if (!empty($data)) { if (!empty($data['original']['value'])) { foreach ($parameters['size'] as $size) { $x = (int) $post->get($this->getName() . $size['name'] . '-x'); $y = (int) $post->get($this->getName() . $size['name'] . '-y'); $filename = !empty($data[$size['name']]['value']) ? $data[$size['name']]['value'] : preg_replace('~\\.([a-zA-Z]+)$~', '-' . $size['name'] . '.$1', $data['original']['value']); $imageModel->open($fileClass->getPath() . $data['original']['value']); $imageModel->resize($size['width'], $size['height'], empty($parameters['resize_option']) ? 'auto' : $parameters['resize_option'], $backgroundColor, $x, $y); $imageModel->save($fileClass->getPath() . $filename); if (!empty($data[$size['name']]['value'])) { $data[$size['name']]['x'] = $x; $data[$size['name']]['y'] = $y; } else { $fileInfo = @getimagesize($fileClass->getPath() . $filename); $data[$size['name']] = array('value' => $filename, 'width' => empty($fileInfo[0]) ? 0 : $fileInfo[0], 'height' => empty($fileInfo[1]) ? 0 : $fileInfo[1], 'html' => empty($fileInfo[2]) ? '' : $fileInfo[2], 'mime' => empty($fileInfo['mime']) ? '' : $fileInfo['mime'], 'x' => 0, 'y' => 0); } } foreach ($data as $name => $file) { if ($name == 'original') { continue; } $found = false; foreach ($parameters['size'] as $size) { if ($size['name'] == $name) { $found = true; $file['options'] = $size; break; } } if (empty($found)) { unset($data[$name]); } } } } } $data = serialize($data); $this->setValue(empty($data) ? '' : $data); }
/** * Load upload editor * * @return string */ public function load() { $parameters = $this->getConfig(); $options = empty($parameters['options']) ? array() : $parameters['options']; $this->initScript(); $fileList = array(); $files = unserialize($this->getValue()); if (!empty($files)) { $fileClass = new File(); $fileClass->load($this->getProperty(), $this->getDatatype()->getDocument()); foreach ($files as $fileData) { $fileObject = array(); $fileObject['name'] = $fileData['value']; $fileObject['filename'] = $fileData['value']; $fileObject['thumbnail_url'] = $fileData['value']; $router = Registry::get('Application')->getMvcEvent()->getRouter(); $fileObject['delete_url'] = $router->assemble(array('document_id' => $this->getDatatype()->getDocument()->getId(), 'property_id' => $this->getProperty()->getId(), 'file' => base64_encode($fileData['value'])), array('name' => 'content/media/remove')); $fileObject['delete_type'] = 'DELETE'; $fileList[] = $fileObject; } } return $this->addPath(__DIR__)->render('upload-editor.phtml', array('property' => $this->getProperty(), 'uploadUrl' => $this->getUploadUrl(), 'name' => $this->getName(), 'files' => json_encode($fileList), 'options' => $options)); }
/** * Upgrade * * @return boolean */ public function upgrade() { $backupFilename = $this->getTmpPath() . '/backup.zip'; //Remove old backup if (file_exists($backupFilename)) { unlink($backupFilename); } if (File::isWritable(GC_APPLICATION_PATH, array(GC_APPLICATION_PATH . '/data/cache', GC_APPLICATION_PATH . '/.git'))) { if ($this->createBackup($backupFilename)) { foreach (array('library', 'module', 'vendor', 'tests') as $directory) { $this->addMessage(sprintf('Remove %s directory', GC_APPLICATION_PATH . '/' . $directory)); File::removeDirectory(GC_APPLICATION_PATH . '/' . $directory); } $directory = $this->getTmpPath() . '/v' . $this->getLatestVersion(); $this->addMessage(sprintf('Copy %s to %s', $directory, GC_APPLICATION_PATH)); File::copyDirectory($directory, GC_APPLICATION_PATH); $this->addMessage('Done!'); return true; } } $this->addMessage('Some files are not writable!'); $this->addMessage(sprintf('Please execute: chmod -R ug+rw %s', GC_APPLICATION_PATH)); return false; }
/** * Remove directories * * @return mixed */ protected function removeDirectories() { $file = new File(); $file->load($this->property, $this->document); $dir = $file->getPath() . $file->getDirectory(); if (is_dir($dir)) { $data = glob($dir . '/*'); foreach ($data as $file) { unlink($file); } $tmpDir = $dir; while ($tmpDir != GC_MEDIA_PATH . '/files') { rmdir($tmpDir); $tmpDir = realpath(dirname($tmpDir)); } } }
/** * Insert data into database * * @param \Zend\Db\Adapter\Adapter $dbAdapter Database adapter * @param string $template Template name * @param string $sqlType Sql database type * * @return \Zend\View\Model\JsonModel|null */ protected function installTemplate($dbAdapter, $template, $sqlType) { $templatePath = GC_APPLICATION_PATH . sprintf('/data/install/design/%s', $template); $info = new Info(); $info->fromFile($templatePath . '/design.info'); $filePath = sprintf('%s/sql/%s.sql', $templatePath, $sqlType); if (!file_exists($filePath)) { return $this->returnJson(array('success' => false, 'message' => sprintf('Could not find data for this template and driver: Driver %s, path %s', $sqlType, $templatePath))); } $designInfos = $info->getInfos(); if (!empty($designInfos['modules'])) { $modules = $this->getServiceLocator()->get('CustomModules'); foreach ($designInfos['modules'] as $module) { ModuleModel::install($modules, $module); } } $sql = file_get_contents($filePath); $dbAdapter->getDriver()->getConnection()->getResource()->exec($sql); File::copyDirectory($templatePath . '/frontend', GC_PUBLIC_PATH . '/frontend'); if (file_exists($templatePath . '/files')) { File::copyDirectory($templatePath . '/files', GC_MEDIA_PATH . '/files'); } File::copyDirectory($templatePath . '/templates', GC_APPLICATION_PATH . '/templates'); }
/** * Delete file * * @return \Zend\View\Model\JsonModel */ public function removeAction() { $property = Property\Model::fromId($this->getRouteMatch()->getParam('property_id')); $document = Document\Model::fromId($this->getRouteMatch()->getParam('document_id')); if ($this->getRequest()->getMethod() != 'DELETE' or empty($document) or empty($property)) { return $this->returnJson(array('error' => true)); } $file = base64_decode($this->getRouteMatch()->getParam('file')); $fileClass = new File(); $fileClass->load($property, $document); return $this->returnJson(array($fileClass->remove($file))); }