Exemplo n.º 1
0
 protected function _process()
 {
     $return = array();
     try {
         $fileInfo = reset($_FILES);
         if (empty($fileInfo)) {
             throw new CM_Exception('Invalid file upload');
         }
         if (isset($fileInfo['error']) && $fileInfo['error'] !== UPLOAD_ERR_OK) {
             throw new CM_Exception('File upload error: ' . self::$_uploadErrors[$fileInfo['error']]);
         }
         $fileTmp = new CM_File($fileInfo['tmp_name']);
         if ($fileTmp->getSize() > self::MAX_FILE_SIZE) {
             throw new CM_Exception_FormFieldValidation(new CM_I18n_Phrase('File too big'));
         }
         $file = CM_File_UserContent_Temp::create($fileInfo['name'], $fileTmp->read());
         $fileTmp->delete();
         $query = $this->_request->getQuery();
         $preview = null;
         if (isset($query['field'])) {
             $field = CM_FormField_File::factory($query['field'], ['name' => 'file']);
             $field->validateFile($file);
             $preview = $this->getRender()->fetchViewTemplate($field, 'preview', array('file' => $file));
         }
         $return['success'] = array('id' => $file->getUniqid(), 'preview' => $preview);
     } catch (CM_Exception_FormFieldValidation $ex) {
         $return['error'] = array('type' => get_class($ex), 'msg' => $ex->getMessagePublic($this->getRender()));
     }
     $this->_setContent(json_encode($return, JSON_HEX_TAG));
     // JSON decoding in IE-iframe needs JSON_HEX_TAG
 }
Exemplo n.º 2
0
 /**
  * @param CM_Frontend_Render $render
  * @throws CM_Exception
  */
 public function __construct(CM_Frontend_Render $render)
 {
     parent::__construct($render);
     $this->addVariables();
     $file = new CM_File(DIR_PUBLIC . 'static/css/library/icon.less');
     if ($file->exists()) {
         $this->add($file->read());
     }
     foreach (array_reverse($render->getSite()->getModules()) as $moduleName) {
         foreach (array_reverse($render->getSite()->getThemes()) as $theme) {
             foreach (CM_Util::rglob('*.less', $render->getThemeDir(true, $theme, $moduleName) . 'css/') as $path) {
                 $file = new CM_File($path);
                 $this->add($file->read());
             }
         }
     }
     $viewClasses = CM_View_Abstract::getClassChildren(true);
     foreach ($viewClasses as $viewClassName) {
         $validModule = in_array(CM_Util::getNamespace($viewClassName), $render->getSite()->getModules());
         $validViewClass = $this->_isValidViewClass($viewClassName);
         if ($validModule && $validViewClass) {
             $asset = new CM_Asset_Css_View($this->_render, $viewClassName);
             $this->add($asset->_getContent());
         }
     }
 }
Exemplo n.º 3
0
 public function addVariables()
 {
     foreach (array_reverse($this->_render->getSite()->getModules()) as $moduleName) {
         foreach (array_reverse($this->_render->getSite()->getThemes()) as $theme) {
             $file = new CM_File($this->_render->getThemeDir(true, $theme, $moduleName) . 'variables.less');
             if ($file->exists()) {
                 $this->add($file->read());
             }
         }
     }
 }
Exemplo n.º 4
0
 public function __construct(CM_Frontend_Render $render)
 {
     parent::__construct($render);
     $extensions = array('css', 'less');
     foreach (array_reverse($render->getSite()->getModules()) as $moduleName) {
         $libraryPath = DIR_ROOT . CM_Bootloader::getInstance()->getModulePath($moduleName) . 'client-vendor/';
         foreach ($extensions as $extension) {
             foreach (CM_Util::rglob('*.' . $extension, $libraryPath) as $path) {
                 $file = new CM_File($path);
                 $this->add($file->read());
             }
         }
     }
 }
Exemplo n.º 5
0
function smarty_function_load(array $params, Smarty_Internal_Template $template)
{
    /** @var CM_Frontend_Render $render */
    $render = $template->smarty->getTemplateVars('render');
    $namespace = isset($params['namespace']) ? $params['namespace'] : null;
    $parse = isset($params['parse']) ? (bool) $params['parse'] : true;
    if ($parse) {
        $tplPath = $render->getLayoutPath($params['file'], $namespace);
        $params = array_merge($template->getTemplateVars(), $params);
        return $render->fetchTemplate($tplPath, $params);
    } else {
        $tplPath = $render->getLayoutPath($params['file'], $namespace, null, true);
        $file = new CM_File($tplPath);
        return $file->read();
    }
}
Exemplo n.º 6
0
 /**
  * @return array
  */
 private function _getColorStyles()
 {
     $site = $this->getParams()->getSite('site');
     $style = '';
     foreach (array_reverse($site->getModules()) as $moduleName) {
         $file = new CM_File(CM_Util::getModulePath($moduleName) . 'layout/default/variables.less');
         if ($file->exists()) {
             $style .= $file->read() . PHP_EOL;
         }
     }
     preg_match_all('#@(color\\w+)#', $style, $matches);
     $colors = array_unique($matches[1]);
     foreach ($colors as $variableName) {
         $style .= '.' . $variableName . ' { background-color: @' . $variableName . '; }' . PHP_EOL;
     }
     $lessCompiler = new lessc();
     $style = $lessCompiler->compile($style);
     preg_match_all('#.(color\\w+)\\s+\\{([^}]+)\\}#', $style, $matches);
     return array_combine($matches[1], $matches[2]);
 }
Exemplo n.º 7
0
 protected function _process()
 {
     $path = $this->getRequest()->getPath();
     $content = null;
     $mimeType = null;
     if ($pathRaw = $this->getRender()->getLayoutPath('resource/' . $path, null, null, true, false)) {
         $file = new CM_File($pathRaw);
         if (in_array($file->getExtension(), $this->_getFiletypesForbidden())) {
             throw new CM_Exception_Nonexistent('Forbidden filetype', CM_Exception::WARN, ['path' => $path]);
         }
         $content = $file->read();
         $mimeType = $file->getMimeType();
     } elseif ($pathTpl = $this->getRender()->getLayoutPath('resource/' . $path . '.smarty', null, null, true, false)) {
         $content = $this->getRender()->fetchTemplate($pathTpl);
         $mimeType = CM_File::getMimeTypeByContent($content);
     } else {
         throw new CM_Exception_Nonexistent('Invalid filename', CM_Exception::WARN, ['path' => $path]);
     }
     $this->setHeader('Content-Type', $mimeType);
     $this->_setContent($content);
 }
Exemplo n.º 8
0
 /**
  * @param           $manifestPath
  * @param string    $devicePath
  * @param bool|null $skipFormat
  * @param bool|null $dryRun
  */
 public function createJob($manifestPath, $devicePath, $skipFormat = null, $dryRun = null)
 {
     $manifestPath = (string) $manifestPath;
     if (!preg_match('/^\\//', $manifestPath)) {
         $manifestPath = getcwd() . '/' . $manifestPath;
     }
     $devicePath = (string) $devicePath;
     $skipFormat = (bool) $skipFormat;
     $dryRun = (bool) $dryRun;
     $awsBackupManager = $this->_getBackupManager();
     $this->_getStreamOutput()->writeln('Preparing backup device');
     $device = new S3Export_Device($devicePath);
     if (!$skipFormat) {
         $device->format();
     }
     $device->mount();
     $this->_getStreamOutput()->writeln('Creating AWS backup job');
     $manifestFile = new CM_File($manifestPath);
     $job = $awsBackupManager->createJob($manifestFile->read(), $dryRun);
     $this->_getStreamOutput()->writeln("Job created, id: `{$job->getId()}`");
     $this->_getStreamOutput()->writeln('Storing AWS Signature on backup device');
     $awsBackupManager->storeJobSignatureOnDevice($job, $device);
     $device->unmount();
 }
Exemplo n.º 9
0
 /**
  * @return string
  */
 public function read()
 {
     return $this->_file->read();
 }
Exemplo n.º 10
0
 /**
  * @param string $content
  * @param bool   $compress
  * @return string
  */
 private function _compileLess($content, $compress)
 {
     $render = $this->_render;
     $lessCompiler = new lessc();
     $lessCompiler->registerFunction('image', function ($arg) use($render) {
         /** @var CM_Frontend_Render $render */
         list($type, $delimiter, $values) = $arg;
         return array('function', 'url', array('string', $delimiter, array($render->getUrlResource('layout', 'img/' . $values[0]))));
     });
     $lessCompiler->registerFunction('image-inline', function ($arg) use($render) {
         /** @var CM_Frontend_Render $render */
         list($type, $delimiter, $values) = $arg;
         if (2 == sizeof($values) && is_array($values[0]) && is_array($values[1])) {
             $delimiter = (string) $values[0][1];
             $path = (string) $values[0][2][0];
             $size = (int) $values[1][1];
         } else {
             $path = $values[0];
             $size = 0;
         }
         $imagePath = $render->getLayoutPath('resource/img/' . $path, null, null, true, true);
         $cache = CM_Cache_Persistent::getInstance();
         $imageBase64 = $cache->get($cache->key(__METHOD__, md5($imagePath), 'size:' . $size), function () use($imagePath, $size) {
             $file = new CM_File($imagePath);
             $img = new CM_Image_Image($file->read());
             if ($size > 0) {
                 $img->resize($size, $size);
             }
             $img->setFormat(CM_Image_Image::FORMAT_GIF);
             return base64_encode($img->getBlob());
         });
         $url = 'data:image/gif;base64,' . $imageBase64;
         return array('function', 'url', array('string', $delimiter, array($url)));
     });
     $lessCompiler->registerFunction('urlFont', function ($arg) use($render) {
         /** @var CM_Frontend_Render $render */
         list($type, $delimiter, $values) = $arg;
         return array($type, $delimiter, array($render->getUrlStatic('/font/' . $values[0])));
     });
     if ($compress) {
         $lessCompiler->setFormatter('compressed');
     }
     return $lessCompiler->compile($content);
 }
Exemplo n.º 11
0
 /**
  * @return string
  */
 protected function _getMachineId()
 {
     // Global machine-id from systemd https://www.freedesktop.org/software/systemd/man/machine-id.html
     $file = new CM_File('/etc/machine-id');
     if (!$file->exists()) {
         // Local machine-id as a backup
         $serviceManager = CM_Service_Manager::getInstance();
         $file = new CM_File('machine-id', $serviceManager->getFilesystems()->getData());
         if (!$file->exists()) {
             $uuid = Ramsey\Uuid\Uuid::uuid4()->toString();
             $file->write($uuid);
         }
     }
     return trim($file->read());
 }
Exemplo n.º 12
0
 public function testResizeSpecificKeepExif()
 {
     $imageFileOriginal = new CM_File(DIR_TEST_DATA . 'img/test-rotated.jpg');
     $image = new CM_Image_Image($imageFileOriginal->read());
     $image->resize($image->getWidth(), $image->getHeight());
     $imageFile = CM_File::createTmp(null, $image->getBlob());
     $newImage = new CM_Image_Image($imageFile->read());
     $this->assertSame(6, $this->_getImagickObject($newImage)->getImageOrientation());
 }
Exemplo n.º 13
0
 /**
  * @param string       $className
  * @param boolean|null $includeAbstracts
  * @return string[]
  */
 public static function getClassChildren($className, $includeAbstracts = null)
 {
     $key = CM_CacheConst::ClassChildren . '_className:' . $className . '_abstracts:' . (int) $includeAbstracts;
     $cache = CM_Cache_Local::getInstance();
     if (false === ($classNames = $cache->get($key))) {
         $pathsFiltered = array();
         $paths = array();
         foreach (CM_Bootloader::getInstance()->getModules() as $modulePath) {
             $namespacePaths = CM_Util::rglob('*.php', CM_Util::getModulePath($modulePath) . 'library/');
             $paths = array_merge($paths, $namespacePaths);
         }
         $regexp = '#\\bclass\\s+(?<name>[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)\\s+#';
         foreach ($paths as $path) {
             $file = new CM_File($path);
             $fileContents = $file->read();
             if (preg_match($regexp, $fileContents, $matches)) {
                 if (class_exists($matches['name'], true)) {
                     $reflectionClass = new ReflectionClass($matches['name']);
                     if (($reflectionClass->isSubclassOf($className) || interface_exists($className) && $reflectionClass->implementsInterface($className)) && (!$reflectionClass->isAbstract() || $includeAbstracts)) {
                         $pathsFiltered[] = $path;
                     }
                 }
             }
         }
         $classNames = self::getClasses($pathsFiltered);
         $cache->set($key, $classNames);
     }
     return $classNames;
 }
Exemplo n.º 14
0
 public function testIsAnimatedConvertingToNonAnimated()
 {
     $file = new CM_File(DIR_TEST_DATA . 'img/animated.gif');
     $image = CM_File_Image::createTmp('gif', $file->read());
     $this->assertTrue($image->isAnimated());
     $image->convert(CM_File_Image::FORMAT_GIF);
     $this->assertTrue($image->isAnimated());
     $image->convert(CM_File_Image::FORMAT_JPEG);
     $this->assertFalse($image->isAnimated());
 }