Exemple #1
0
 /**
  * This function is used to uplodad file in /uploads/YEAR/MONTH directory 
  * and to create Content record contextually.
  * 
  * 
  * @param type $inputfile
  * @param string $destfile
  * @return boolean
  */
 public function upload($inputfile, $destfile = null)
 {
     if (strlen(trim($inputfile['name'])) === 0) {
         return FALSE;
     }
     $filename = strtolower($inputfile['name']);
     $sourcefile = $inputfile['tmp_name'];
     $file = new \Cake\Filesystem\File($sourcefile, true, 0775);
     $CONTENT_YEAR = date('Y');
     $CONTENT_MONTH = date('m');
     $UPLOAD_DIR = $this->path;
     $folder_dest = new Folder($UPLOAD_DIR);
     if (!$folder_dest->inCakePath($folder_dest->pwd() . DS . $CONTENT_YEAR)) {
         $folder_dest->create($folder_dest->pwd() . DS . $CONTENT_YEAR);
     }
     $folder_dest->cd($CONTENT_YEAR);
     if (!$folder_dest->inCakePath($folder_dest->pwd() . DS . $CONTENT_MONTH)) {
         $folder_dest->create($folder_dest->pwd() . DS . $CONTENT_MONTH);
     }
     $folder_dest->cd($CONTENT_MONTH);
     $path = DS . $CONTENT_YEAR . DS . $CONTENT_MONTH . DS;
     $permittedFilename = $this->getPermittedFilename($UPLOAD_DIR . $path, $filename);
     $destfile = $folder_dest->pwd() . DS . $permittedFilename;
     if ($file->copy($destfile, true)) {
         return $path . $permittedFilename;
     } else {
         return FALSE;
     }
 }
 /**
  * Includes all necessary JS code for admin editing of CmsBlocks.
  *
  * @return void
  */
 public function includeCmsAdminAssets()
 {
     $script = 'var Cms = {AdminWidgetControllers: {}};';
     $this->Html->scriptBlock($script, ['block' => true]);
     $this->Html->script('Cms.app/lib/AdminWidgetController.js', ['block' => true]);
     $availableWidgets = WidgetManager::getAvailableWidgets();
     $adminControllers = [];
     $folder = new Folder();
     foreach ($availableWidgets as $widgetIdentifier) {
         $widget = WidgetFactory::identifierFactory($widgetIdentifier);
         $webrootPath = $widget->getWebrootPath();
         if (!is_dir($webrootPath)) {
             continue;
         }
         $folder->cd($webrootPath . 'js/');
         $jsFiles = $folder->read();
         if (empty($jsFiles[1])) {
             continue;
         }
         foreach ($jsFiles[1] as $filename) {
             if (strpos($filename, 'AdminWidgetController.js') !== false) {
                 $adminControllers[] = '/cms/widget/' . $widgetIdentifier . '/js/' . $filename;
             }
         }
     }
     $this->Html->script($adminControllers, ['block' => true]);
 }
 private function checkIfSubfolderExistsAndCreateIfFalseAndReturnPath($folder, $subfolder)
 {
     $folderObject = new Folder($folder);
     $folder = $folderObject->read();
     if (!in_array($subfolder, $folder[0])) {
         $folderObject->create($subfolder);
     }
     $folderObject->cd($subfolder);
     return $folderObject->path;
 }
 /**
  * Faz uma instalação básica do plugin.
  * A instalação consiste em criar o arquivo de configurações do plugin no diretório apropriado.
  * Este método é chamado pelo método PluginStarter::load() quando necessário.
  * 
  * @param string $pluginName O nome do plugin a ser instalado.
  * @return void             
  */
 public function install($pluginName)
 {
     $settingsFileFolder = $this->pluginInstallationFolder . $pluginName . DS;
     if (Plugin::loaded($pluginName)) {
         $defaultFile = Plugin::path($pluginName) . 'config' . DS . 'default_settings.php';
         $folderHandler = new Folder();
         if (!$folderHandler->cd($settingsFileFolder)) {
             $folderHandler->create($settingsFileFolder);
         }
         $fileHandler = new File($defaultFile);
         $fileHandler->copy($settingsFileFolder . 'settings.php');
         $fileHandler->close();
     }
 }
 /**
  * main method
  *
  * @param  string $tempDir an other directory to clear of all folders and files, if desired
  * @return void
  */
 public function main($tempDir = null)
 {
     if (empty($tempDir)) {
         $tempDir = Configure::read('Attachments.tmpUploadsPath');
     }
     if (!Folder::isAbsolute($tempDir)) {
         $this->out('The path must be absolute, "' . $tempDir . '" given.');
         exit;
     }
     $Folder = new Folder();
     if ($Folder->cd($tempDir) === false) {
         $this->out('Path "' . $tempDir . '" doesn\'t seem to exist.');
         exit;
     }
     $dir = new Folder($tempDir);
     $folders = $dir->read();
     $files = $dir->findRecursive();
     $deletedFiles = 0;
     $deletedFolders = 0;
     $this->out('Found ' . count($folders[0]) . ' folders and ' . count($files) . ' files');
     foreach ($files as $filePath) {
         $file = new File($filePath);
         // only delete if last change is longer than 24 hours ago
         if ($file->lastChange() < time() - 24 * 60 * 60 && $file->delete()) {
             $deletedFiles++;
         }
         $file->close();
     }
     foreach ($folders[0] as $folderName) {
         $folder = new Folder($dir->pwd() . $folderName);
         // only delete if folder is empty
         if ($folder->dirsize() === 0 && $folder->delete()) {
             $deletedFolders++;
         }
     }
     $this->out('Deleted ' . $deletedFolders . ' folders and ' . $deletedFiles . ' files.');
 }
 /**
  * Include necessary cms assets. This has to be called after the page has been rendered.
  * Best place is in the layout.
  *
  * @param array $options Options for the Html->script() calls
  * @return void|string
  */
 public function includeCmsAssets(array $options = [])
 {
     if ($this->_cmsAssetsIncluded) {
         return;
     }
     $options = Hash::merge(['block' => true], $options);
     $out = '';
     $out .= $this->Html->scriptBlock('var Cms = {WidgetControllers: {}};', $options);
     $out .= $this->Html->script('Cms.app/lib/WidgetController.js', $options);
     $folder = new Folder();
     $controllers = [];
     $jsonData = [];
     foreach (WidgetManager::getRegistry() as $uniqueId => $widget) {
         $webrootPath = $widget->getWebrootPath();
         if (!is_dir($webrootPath)) {
             continue;
         }
         $jsonData[$uniqueId] = ['identifier' => $widget->getFullIdentifier(), 'blockId' => $widget->getCmsBlock()->id, 'jsonData' => $widget->getJsonData()];
         $folder->cd($webrootPath . 'js/');
         $jsFiles = $folder->read();
         if (empty($jsFiles[1])) {
             continue;
         }
         foreach ($jsFiles[1] as $filename) {
             if (strpos($filename, 'WidgetController.js') !== false && strpos($filename, 'AdminWidgetController.js') === false) {
                 $controllers[] = '/cms/widget/' . $widget->getIdentifier() . '/js/' . $filename;
             }
         }
     }
     $this->FrontendBridge->setFrontendData('Cms', ['widgets' => $jsonData]);
     $out .= $this->Html->script($controllers, $options);
     $this->_cmsAssetsIncluded = true;
     if (!$options['block']) {
         return $out;
     }
 }
Exemple #7
0
 /**
  * Scan plugin directories and returns plugin names and their paths within file
  * system. We consider "plugin name" as the name of the container directory.
  *
  * Example output:
  *
  * ```php
  * [
  *     'Users' => '/full/path/plugins/Users/',
  *     'ThemeManager' => '/full/path/plugins/ThemeManager/',
  *     ...
  *     'MySuperPlugin' => '/full/path/plugins/MySuperPlugin/',
  *     'DarkGreenTheme' => '/full/path/plugins/DarkGreenTheme/',
  * ]
  * ```
  *
  * If $ignoreThemes is set to true `DarkGreenTheme` will not be part of the
  * result.
  *
  * NOTE: All paths includes trailing slash.
  *
  * @param bool $ignoreThemes Whether include themes as well or not
  * @return array Associative array as `PluginName` => `/full/path/to/PluginName`
  */
 public static function scan($ignoreThemes = false)
 {
     $cacheKey = "scan({$ignoreThemes})";
     $cache = static::cache($cacheKey);
     if (!$cache) {
         $cache = [];
         $paths = App::path('Plugin');
         $Folder = new Folder();
         $Folder->sort = true;
         foreach ($paths as $path) {
             $Folder->cd($path);
             foreach ($Folder->read(true, true, true)[0] as $dir) {
                 $name = basename($dir);
                 $cache[$name] = normalizePath("{$dir}/");
             }
         }
         // look for Cake plugins installed using Composer
         if (file_exists(VENDOR_INCLUDE_PATH . 'cakephp-plugins.php')) {
             $cakePlugins = (array) (include VENDOR_INCLUDE_PATH . 'cakephp-plugins.php');
             if (!empty($cakePlugins['plugins'])) {
                 $cache = Hash::merge($cakePlugins['plugins'], $cache);
             }
         }
         // filter, remove hidden folders and others
         foreach ($cache as $name => $path) {
             if (strpos($name, '.') === 0) {
                 unset($cache[$name]);
             } elseif ($name == 'CMS') {
                 unset($cache[$name]);
             } elseif ($ignoreThemes && str_ends_with($name, 'Theme')) {
                 unset($cache[$name]);
             }
         }
         $cache = static::cache($cacheKey, $cache);
     }
     return $cache;
 }
 public function upload($system, $path)
 {
     // UPLOAD THE FILE TO THE RIGHT SYSTEM
     if ($system == 'production') {
         $other = 'development';
     } else {
         $other = 'production';
     }
     $path = str_replace('___', '/', $path);
     $file = new File('../../' . $other . $path);
     $file2 = new File('../../' . $system . $path);
     if (!$file2->exists()) {
         $dirs = explode('/', $path);
         $prod = new Folder('../../' . $system);
         for ($i = 0; $i < sizeof($dirs) - 1; $i++) {
             if (!$prod->cd($dirs[$i])) {
                 $prod->create($dirs[$i]);
                 $prod->cd($dirs[$i]);
             }
         }
     }
     if ($file->copy('../../' . $system . $path)) {
         if (touch('../../' . $system . $path, $file->lastChange())) {
             $this->Flash->success(__('File copied successfully.'));
         } else {
             $this->Flash->success(__('File copied successfully, but time not updated.'));
         }
     } else {
         $this->Flash->error(__('Unable copy file. '));
     }
     return $this->redirect($this->referer());
 }
Exemple #9
0
 /**
  * testConstructWithNonExistentPath method
  *
  * @return void
  */
 public function testConstructWithNonExistentPath()
 {
     $path = TMP . 'tests' . DS;
     $Folder = new Folder($path . 'config_non_existent', true);
     $this->assertTrue(is_dir($path . 'config_non_existent'));
     $Folder->cd($path);
 }
Exemple #10
0
 /**
  * Retrieve all controller names + paths for the app src.
  *
  * @return array
  */
 public function getControllersForApp()
 {
     $controllers = [];
     $ctrlFolder = new Folder();
     /** @var Folder $ctrlFolder */
     $ctrlFolder->cd(ROOT . DS . 'src' . DS . 'Controller');
     if (!empty($ctrlFolder)) {
         $files = $ctrlFolder->find('.*Controller\\.php$');
         $subLength = strlen('Controller.php');
         foreach ($files as $f) {
             $filename = basename($f);
             if ($filename === 'AppController.php') {
                 continue;
             }
             $ctrlName = substr($filename, 0, strlen($filename) - $subLength);
             $controllers[] = ['name' => $ctrlName, 'path' => $ctrlFolder->path . DS . $f];
         }
         $subFolders = $ctrlFolder->read(true, false, true)[0];
         foreach ($subFolders as $subFolder) {
             $ctrlFolder->cd($subFolder);
             $files = $ctrlFolder->find('.*Controller\\.php$');
             $subLength = strlen('Controller.php');
             foreach ($files as $f) {
                 $filename = basename($f);
                 if ($filename === 'AppController.php') {
                     continue;
                 }
                 $ctrlName = substr($filename, 0, strlen($filename) - $subLength);
                 $controllers[] = ['name' => $ctrlName, 'path' => $ctrlFolder->path . DS . $f];
             }
         }
     }
     return $controllers;
 }