protected function addFiles() { $controllers = $this->addChild('Controllers')->ulClass('level1')->liClass('type'); $templates = $this->addChild('Templates')->ulClass('level1')->liClass('type'); $moduleDirs = sfFinder::type('dir')->maxDepth(0)->in(sfConfig::get('sf_app_module_dir')); natcasesort($moduleDirs); foreach ($moduleDirs as $moduleDir) { if (count($found = sfFinder::type('file')->in($moduleDir . '/actions'))) { $moduleControllers = $controllers->addChild(basename($moduleDir))->ulClass('level2'); natcasesort($found); foreach ($found as $path) { $moduleControllers->addChild(basename($path), '#' . dmProject::unRootify($path)); } } if (count($found = sfFinder::type('file')->in($moduleDir . '/templates'))) { $moduleTemplates = $templates->addChild(basename($moduleDir))->ulClass('level2'); natcasesort($found); foreach ($found as $path) { $moduleTemplates->addChild(str_replace($moduleDir . '/templates/', '', $path), '#' . dmProject::unRootify($path)); } } } $stylesheets = $this->addChild('Stylesheets')->ulClass('level1')->liClass('type')->addChild($this->user->getTheme()->getName())->ulClass('level2'); $cssDir = $this->user->getTheme()->getFullPath('css'); $cssFiles = sfFinder::type('file')->name('*.css')->in($cssDir); natcasesort($cssFiles); foreach ($cssFiles as $cssFile) { if (dmProject::isInProject($cssFile)) { $stylesheets->addChild(str_replace($cssDir . '/', '', $cssFile), '#' . dmProject::unRootify($cssFile)); } } return $this; }
public function create() { foreach ($this->getFullPaths() as $fullPath) { @$this->filesystem->mkdir($fullPath); if (!is_dir($fullPath)) { throw new dmException(sprintf('%s can not be created. Please check %s permissions', dmProject::unRootify($fullPath), dmProject::unRootify(dirname($fullPath)))); } } $this->filesystem->touch($this->getFullPath('css/markdown.css')); $this->dispatcher->notify(new sfEvent($this, 'dm.theme.created', array())); }
public function __construct($options = array()) { if (isset($options['cache_dir'])) { $name = substr($options['cache_dir'], strlen(sfConfig::get('sf_cache_dir')) + 1); } elseif (isset($options['prefix'])) { $name = dmProject::unRootify($options['prefix']); } else { throw new dmException('You must provide a cache_dir or a prefix'); } $this->initialize(array('prefix' => dmProject::getNormalizedRootDir() . '/' . $name)); }
public function executeFile(dmWebRequest $request) { $this->forward404Unless(file_exists($this->file = dmOs::join(sfConfig::get('sf_root_dir'), $request->getParameter('file'))), $request->getParameter('file') . ' does not exists'); $this->code = file_get_contents($this->file); $this->path = dmProject::unRootify($this->file); $this->isWritable = is_writable($this->file); $this->message = $this->isWritable ? '' : $this->getI18n()->__('This file is not writable'); $this->textareaOptions = array('spellcheck' => 'false'); if (!$this->isWritable) { $this->textareaOptions['readonly'] = 'true'; } }
/** * @see sfTask */ protected function execute($arguments = array(), $options = array()) { $filesystem = $this->get('filesystem'); $uploadDir = dmOs::join(sfConfig::get('sf_web_dir'), 'uploads'); if (!$filesystem->mkdir($uploadDir)) { throw new dmException(dmProject::unRootify($uploadDir) . ' is not writable'); } $cacheDir = dmOs::join(sfConfig::get('sf_cache_dir'), 'open_package', dmProject::getKey()); if (!$filesystem->mkdir($cacheDir)) { throw new dmException(dmProject::unRootify($cacheDir) . ' is not writable'); } $filesystem->deleteDirContent($cacheDir); if (file_exists(dmOs::join($uploadDir, dmProject::getKey() . '.tgz'))) { $filesystem->unlink(dmOs::join($uploadDir, dmProject::getKey() . '.tgz')); } $this->log('Building the package, please wait...'); $finder = sfFinder::type('all')->prune(array('.thumbs'))->discard(array('.thumbs')); foreach (array('apps', 'config', 'lib', 'plugins', 'public_html', 'test') as $dir) { $filesystem->mirror(dmOs::join(sfConfig::get('sf_root_dir'), $dir), dmOs::join($cacheDir, $dir), $finder); } $filesystem->copy(dmOs::join(sfConfig::get('sf_root_dir'), 'symfony'), dmOs::join($cacheDir, 'symfony')); foreach (array('cache', 'data') as $dir) { $filesystem->mkdir(dmOs::join($cacheDir, $dir)); } if (file_exists(dmOs::join(sfConfig::get('sf_data_dir'), 'mysql'))) { $filesystem->mirror(dmOs::join(sfConfig::get('sf_data_dir'), 'mysql'), dmOs::join($cacheDir, 'data/mysql'), sfFinder::type('all')); } $databasesFile = dmOs::join($cacheDir, 'config/databases.yml'); $databasesConfig = sfYaml::load($databasesFile); foreach ($databasesConfig as $namespace => $nsConfig) { foreach ($nsConfig as $dbName => $config) { $dsn = $databasesConfig[$namespace][$dbName]['param']['dsn']; $databasesConfig[$namespace][$dbName]['param']['dsn'] = preg_replace('#//(.+)\\:(.*)@#', '//#DB_USER#:#DB_PASSWORD#@', $dsn); foreach (array('username', 'password') as $key) { if (isset($databasesConfig[$namespace][$dbName]['param'][$key])) { unset($databasesConfig[$namespace][$dbName]['param'][$key]); } } } file_put_contents($databasesFile, sfYaml::dump($databasesConfig, 5)); } $filesystem->unlink(dmOs::join($cacheDir, 'test/functional')); $command = 'cd ' . dirname($cacheDir) . '; tar -czf ' . dmProject::getKey() . '.tgz ' . dmProject::getKey(); $this->logSection('Compression', $command); if (!$filesystem->exec($command)) { throw new dmException($filesystem->getLastExec('output')); } rename(dmOs::join(dirname($cacheDir), dmProject::getKey() . '.tgz'), dmOs::join($uploadDir, dmProject::getKey() . '.tgz')); $filesystem->unlink($cacheDir); $this->logBlock('Done !', 'INFO'); }
public function checkFolder($validator, $values) { if (!empty($values['file'])) { if (!($folder = dmDb::table('DmMediaFolder')->find($values['dm_media_folder_id']))) { throw new dmException('media has no folder'); } if (!is_writable($folder->fullPath)) { $error = new sfValidatorError($validator, dmProject::unRootify($folder->fullPath) . ' is not writable'); // throw an error bound to the file field throw new sfValidatorErrorSchema($validator, array('file' => $error)); } } return $values; }
protected function checkCliFile() { $file = $this->getCliFileFullPath(); if (!file_exists($file) || file_get_contents($file) != $this->getCliFileContent()) { $this->filesystem->mkdir(dirname($file)); file_put_contents($file, $this->getCliFileContent()); } if (!is_executable($file)) { chmod($file, 0777); } if (!is_executable($file) && '/' === DIRECTORY_SEPARATOR) { throw new dmException('Can not make ' . dmProject::unRootify($file) . ' executable'); } }
public function executeIndex(sfWebRequest $request) { $this->commands = implode(' ', $this->getCommands()); $this->uname = php_uname(); $filesystem = $this->context->getFilesystem(); if ($filesystem->exec('whoami')) { $this->whoami = $filesystem->getLastExec('output'); } else { $this->whoami = 'unknown_user'; } $this->pwd = getcwd(); $this->prompt = $this->whoami . '@' . php_uname('n') . ':' . '~/' . dmProject::unRootify($this->pwd) . '$ '; $this->getUser()->setAttribute('dm_console_prompt', $this->prompt); $this->form = $this->getCommandForm(); }
/** * Backup a file * return boolean success */ public function save($file) { if (!$this->isEnabled()) { return true; } if (!dmProject::isInProject($file)) { $file = dmOs::join(dmProject::getRootDir(), $file); } if (!is_readable($file)) { throw new dmException('Can no read ' . $file); } $relFile = dmProject::unRootify($file); $backupPath = dmOs::join($this->getDir(), dirname($relFile)); if (!$this->filesystem->mkdir($backupPath)) { throw new dmException('Can not create backup dir ' . $backupPath); } $backupFile = dmOs::join($backupPath, basename($relFile) . '.' . date('Y-m-d_H-i-s')); if (!$this->filesystem->touch($backupFile, 0777)) { throw new dmException('Can not copy ' . $file . ' to ' . $backupFile); } file_put_contents($backupFile, file_get_contents($file)); $this->filesystem->chmod($file, 0777); return true; }
/** * Move into another folder * * @param DmMediaFolder $folder */ public function move(DmMediaFolder $folder) { if ($folder->id == $this->nodeParentId) { return $this; } if ($folder->getNode()->isDescendantOfOrEqualTo($this)) { throw new dmException('Can not move to a descendant'); } if ($this->getNode()->isRoot()) { throw new dmException('The root folder cannot be moved'); } if (!$this->isWritable()) { throw new dmException(sprintf('The folder %s is not writable.', dmProject::unRootify($this->fullPath))); } if (!$folder->isWritable()) { throw new dmException(sprintf('The folder %s is not writable.', dmProject::unRootify($folder->fullPath))); } if ($folder->hasSubFolder($this->name)) { throw new dmException(sprintf('The selected folder already contains a folder named "%s".', $this->name)); } $oldRelPath = $this->get('rel_path'); $newRelPath = $folder->get('rel_path') . '/' . $this->name; $fs = $this->getService('filesystem'); $oldFullPath = $this->getFullPath(); $newFullPath = dmOs::join($folder->getFullPath(), $this->name); if (!rename($oldFullPath, $newFullPath)) { throw new dmException('Can not move %s to %s', dmProject::unRootify($oldFullPath), dmProject::unRootify($newFullPath)); } $this->set('rel_path', $newRelPath); $this->clearCache(); $this->getNode()->moveAsFirstChildOf($folder); //update descendants if ($descendants = $this->getNode()->getDescendants()) { foreach ($descendants as $folder) { $folder->set('rel_path', dmString::str_replace_once($oldRelPath, $newRelPath, $folder->get('rel_path'))); $folder->save(); } } return $this; }
protected function validatePath($path, $restrictedPaths, $verb = 'use') { if (!dmProject::isInProject($path)) { throw new dmCodeEditorException(sprintf('Can not %s %s because it is outside of the project', $verb, $path)); } $path = dmProject::unRootify($path); foreach ($restrictedPaths as $restrictedPath) { if (preg_match('#^' . $restrictedPath . '$#', $path)) { throw new dmCodeEditorException(sprintf('You are not allowed to %s %s', $verb, $path)); } } }
protected function write($filePath, $xml) { $file = dmOs::join($this->getOption('dir'), $filePath); if (!file_put_contents($file, $xml)) { throw new dmException('Can not save xml sitemap to ' . dmProject::unRootify($file)); } @$this->filesystem->chmod($file, 0666); }
protected function getResizedMediaFullPath(array $attributes) { $media = $this->resource->getSource(); if (!$media instanceof DmMedia) { throw new dmException('Can be used only if the source is a DmMedia instance'); } if (empty($attributes['width'])) { $attributes['width'] = $media->getWidth(); } elseif (empty($attributes['height'])) { $attributes['height'] = (int) ($media->getHeight() * ($attributes['width'] / $media->getWidth())); } $filter = dmArray::get($attributes, 'filter'); $overlay = dmArray::get($attributes, 'overlay', array()); /* * Nothing to change, return the original image */ if ($attributes['width'] == $media->getWidth() && $attributes['height'] == $media->getHeight() && !$filter && !$overlay) { return $media->getFullPath(); } if ($attributes['resize_method'] == 'fit') { $attributes['background'] = trim($attributes['background'], '#'); } if (!in_array($attributes['resize_method'], $this->getAvailableMethods())) { throw new dmException(sprintf('%s is not a valid resize method. These are : %s', $attributes['resize_method'], implode(', ', $this->getAvailableMethods()))); } if (!($thumbDir = dmArray::get(self::$verifiedThumbDirs, $media->get('dm_media_folder_id')))) { $thumbDir = dmOs::join($media->get('Folder')->getFullPath(), '.thumbs'); if (!@$this->context->getFilesystem()->mkdir($thumbDir)) { throw new dmException('Thumbnails can not be created in ' . $media->get('Folder')->getFullPath()); } self::$verifiedThumbDirs[$media->get('dm_media_folder_id')] = $thumbDir; } $pathInfo = pathinfo($media->get('file')); $thumbRelPath = $pathInfo['filename'] . '_' . substr(md5(implode('-', array($attributes['width'], $attributes['height'], $attributes['resize_method'] === 'fit' ? 'fit' . $attributes['background'] : $attributes['resize_method'], $filter, implode(' ', $overlay), isset($attributes['resize_quality']) ? $attributes['resize_quality'] : '', $media->getTimeHash()))), -6) . '.' . $pathInfo['extension']; $thumbPath = $thumbDir . '/' . $thumbRelPath; if (!file_exists($thumbPath)) { $image = $media->getImage(); $image->setQuality($attributes['resize_quality']); $image->thumbnail($attributes['width'], $attributes['height'], $attributes['resize_method'], !empty($attributes['background']) ? '#' . $attributes['background'] : null); if ($filter) { $image->{$filter}(); } if (!empty($overlay)) { $overlayPath = $overlay['image']->getServerFullPath(); $type = $this->context->get('mime_type_resolver')->getByFilename($overlayPath); if ($type != 'image/png') { throw new dmException('Only png images can be used as overlay.'); } $image->overlay(new sfImage($overlayPath, $type), $overlay['position']); } $image->saveAs($thumbPath, $media->get('mime')); if (!file_exists($thumbPath)) { throw new dmException(dmProject::unRootify($thumbPath) . ' cannot be created'); } else { $this->context->getFilesystem()->chmod($thumbPath, 0666); } } return $thumbPath; }
protected function renderEdit(dmWidgetBaseForm $form, dmWidgetType $widgetType, $withCopyActions = true) { $helper = $this->getHelper(); $devActions = ''; if ($this->getUser()->can('code_editor') && $form instanceof dmWidgetProjectForm) { $templateDir = dmOs::join(sfConfig::get('sf_app_module_dir'), $form->getDmModule()->getKey(), 'templates', '_' . $form->getDmComponent()->getKey() . '.php'); if (file_exists($templateDir)) { $devActions .= '<a href="#' . dmProject::unRootify($templateDir) . '" class="code_editor s16 s16_code_editor block">' . $this->getI18n()->__('Edit template code') . '</a>'; } $componentDir = dmOs::join(sfConfig::get('sf_app_module_dir'), $form->getDmModule()->getKey(), 'actions/components.class.php'); if (file_exists($componentDir)) { $devActions .= '<a href="#' . dmProject::unRootify($componentDir) . '" class="code_editor s16 s16_code_editor block">' . $this->getI18n()->__('Edit component code') . '</a>'; } } if ($devActions) { $devActions = '<div class="code_editor_links">' . $devActions . '</div>'; } $copyActions = ''; if ($withCopyActions && $this->getUser()->can('widget_add')) { $copyActions = $helper->tag('div.dm_cut_copy_actions.none', $helper->link('+/dmWidget/cut')->param('widget_id', $form->getDmWidget()->get('id'))->text('')->title($this->getI18n()->__('Cut'))->set('.s16block.s16_cut.dm_widget_cut') . $helper->link('+/dmWidget/copy')->param('widget_id', $form->getDmWidget()->get('id'))->text('')->title($this->getI18n()->__('Copy'))->set('.s16block.s16_copy.dm_widget_copy')); } return $helper->tag('div.dm.dm_widget_edit.' . dmString::underscore($widgetType->getFullKey()) . '_form', array('json' => array('form_class' => $widgetType->getFullKey() . 'Form', 'form_name' => $form->getName())), $form->render('.dm_form.list.little') . $devActions . $copyActions); }
$t->ok(!$folderTable->findOneByRelPath($folder->relPath), 'folder ' . $folder . ' can not be found anymore in db'); $t->diag('Sync root'); $root->sync(); $helper->checkTreeIntegrity($t); $helper->testFolderCorrelations($t); $t->diag('Copy heavy folders structure into folder'); $sourcePath = dmOs::join(dm::getDir(), 'dmCorePlugin/lib/model/doctrine'); $destRelPath = 'test_lib_model_' . dmString::random(8); $destFullPath = dmOs::join($root->fullPath, $destRelPath); try { $helper->get('filesystem')->mirror($sourcePath, $destFullPath, sfFinder::type('all')); $t->pass('Copy completed'); } catch (Exception $e) { $t->fail('Copy failed : ' . $e->getMessage()); } $t->ok(file_exists(dmOs::join($destFullPath, 'PluginDmMediaTable.class.php')), dmProject::unRootify(dmOs::join($destFullPath, 'PluginDmMediaTable.class.php')) . ' exists'); $t->diag('Sync root'); $root->sync(); $helper->checkTreeIntegrity($t); $helper->testFolderCorrelations($t); $folder = $folderTable->findOneByRelPath($destRelPath); $folderFullPath = $folder->fullPath; $t->is($folderFullPath, dmOs::join($root->fullPath, $destRelPath)); $mediaFolder = $folderTable->findOneByRelPath($destRelPath); $t->isa_ok($mediaFolder, 'DmMediaFolder', $destRelPath . '/media is a DmMediaFolder'); try { $deepMedia = $mediaTable->findOneByFileAndDmMediaFolderId('PluginDmMediaTable.class.php', $mediaFolder->id); $t->isa_ok($deepMedia, 'DmMedia', 'deep media found in db'); } catch (Exception $e) { $t->fail($e->getMessage()); }
public function move(DmMediaFolder $folder) { if ($folder->id == $this->dm_media_folder_id) { return $this; } if (!$this->isWritable()) { throw new dmException(sprintf('The file %s is not writable.', dmProject::unRootify($this->fullPath))); } if (!$folder->isWritable()) { throw new dmException(sprintf('The folder %s is not writable.', dmProject::unRootify($folder->fullPath))); } if ($folder->hasSubFolder($this->file)) { throw new dmException(sprintf('The selected folder already contains a folder named "%s".', $this->name)); } if ($folder->hasFile($this->file)) { throw new dmException(sprintf('The selected folder already contains a file named "%s".', $this->name)); } rename($this->fullPath, $folder->fullPath . '/' . $this->file); $this->dm_media_folder_id = $folder->id; $this->Folder = $folder; $this->save(); return $this; }