/**
  * Checks that given project path does not already exist, and
  * finds the app directory in it. Then it calls bake() with that information.
  *
  * @return mixed
  */
 public function main()
 {
     $project = null;
     if (isset($this->args[0])) {
         $project = $this->args[0];
     } else {
         $appContents = array_diff(scandir(APP), ['.', '..']);
         if (empty($appContents)) {
             $suggestedPath = rtrim(APP, DS);
         } else {
             $suggestedPath = APP . 'MyApp';
         }
     }
     while (!$project) {
         $prompt = 'What is the path to the project you want to bake?';
         $project = $this->in($prompt, null, $suggestedPath);
     }
     $namespace = basename($project);
     if (!preg_match('/^\\w[\\w\\d_]+$/', $namespace)) {
         $err = 'Project Name/Namespace must start with a letter' . ' and can only contain letters, digits and underscore';
         $this->err($err);
         $this->args = [];
         return $this->main();
     }
     if ($project && !Folder::isAbsolute($project) && isset($_SERVER['PWD'])) {
         $project = $_SERVER['PWD'] . DS . $project;
     }
     $response = false;
     $bootstrapPath = '..' . DS . 'config' . DS . 'boostrap.php';
     while (!$response && is_dir($project) === true && file_exists($project . $boostrapPath)) {
         $prompt = sprintf('<warning>A project already exists in this location:</warning> %s Overwrite?', $project);
         $response = $this->in($prompt, ['y', 'n'], 'n');
         if (strtolower($response) === 'n') {
             $response = $project = false;
         }
     }
     if ($project === false) {
         $this->out('Aborting project creation.');
         return;
     }
     if ($this->bake($project)) {
         $this->out('<success>Project baked successfully!</success>');
         return $project;
     }
 }
 /**
  * Construct.
  * It sets the origin file.
  *
  * If the origin is relative, it will be relative to  `APP/webroot/img`.
  * @param string $origin Origin file
  * @return \Thumbs\Utility\ThumbCreator
  * @throws InternalErrorException
  * @uses $height
  * @uses $origin
  * @uses $width
  * @uses _downloadTemporary()
  */
 public function __construct($origin)
 {
     //If the origin is a remote file, downloads as temporary file
     if (isUrl($origin)) {
         $origin = $this->_downloadTemporary($origin);
         //If it's a local file, can be relative to `APP/webroot/img/`
     } elseif (!Folder::isAbsolute($origin)) {
         $origin = WWW_ROOT . 'img' . DS . $origin;
     }
     //Checks if is readable
     if (!is_readable($origin)) {
         throw new NotFoundException(__d('thumbs', 'File or directory {0} not readable', $origin));
     }
     //Checks if has a valid extension
     if (!in_array(extension($origin), ['gif', 'jpg', 'jpeg', 'png'])) {
         throw new InternalErrorException(__d('thumbs', 'The file {0} is not an image', $origin));
     }
     //Sets path, width and height of the origin file
     $this->origin = $origin;
     $this->width = getimagesize($origin)[0];
     $this->height = getimagesize($origin)[1];
     return $this;
 }
 /**
  * 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.');
 }
Exemple #4
0
 /**
  * Get the real path (taking ".." and such into account)
  *
  * @param string $path Path to resolve
  * @return string The resolved path
  */
 public function realpath($path)
 {
     if (strpos($path, '..') === false) {
         if (!Folder::isAbsolute($path)) {
             $path = Folder::addPathElement($this->path, $path);
         }
         return $path;
     }
     $path = str_replace('/', DIRECTORY_SEPARATOR, trim($path));
     $parts = explode(DIRECTORY_SEPARATOR, $path);
     $newparts = [];
     $newpath = '';
     if ($path[0] === DIRECTORY_SEPARATOR) {
         $newpath = DIRECTORY_SEPARATOR;
     }
     while (($part = array_shift($parts)) !== null) {
         if ($part === '.' || $part === '') {
             continue;
         }
         if ($part === '..') {
             if (!empty($newparts)) {
                 array_pop($newparts);
                 continue;
             }
             return false;
         }
         $newparts[] = $part;
     }
     $newpath .= implode(DIRECTORY_SEPARATOR, $newparts);
     return Folder::slashTerm($newpath);
 }
 /**
  * Sets the filename where to export the database
  * @param string $filename Filename path
  * @return string Filename path
  * @throws InternalErrorException
  * @uses compression()
  * @uses $filename
  */
 public function filename($filename)
 {
     if (!\Cake\Filesystem\Folder::isAbsolute($filename)) {
         $filename = BACKUPS . DS . $filename;
     }
     if (!is_readable($filename)) {
         throw new InternalErrorException(__d('database_backup', 'File or directory {0} not readable', $filename));
     }
     //Checks if the file has an extension
     if (!preg_match('/\\.(.+)$/', pathinfo($filename, PATHINFO_BASENAME), $matches)) {
         throw new InternalErrorException(__d('database_backup', 'Invalid file extension'));
     }
     $this->compression(getCompression($matches[1]));
     return $this->filename = $filename;
 }
Exemple #6
0
 /**
  * testIsAbsolute method
  *
  * @return void
  */
 public function testIsAbsolute()
 {
     $this->assertFalse(Folder::isAbsolute('path/to/file'));
     $this->assertFalse(Folder::isAbsolute('cake/'));
     $this->assertFalse(Folder::isAbsolute('path\\to\\file'));
     $this->assertFalse(Folder::isAbsolute('0:\\path\\to\\file'));
     $this->assertFalse(Folder::isAbsolute('\\path/to/file'));
     $this->assertFalse(Folder::isAbsolute('\\path\\to\\file'));
     $this->assertFalse(Folder::isAbsolute('notRegisteredStreamWrapper://example'));
     $this->assertFalse(Folder::isAbsolute('://example'));
     $this->assertTrue(Folder::isAbsolute('/usr/local'));
     $this->assertTrue(Folder::isAbsolute('//path/to/file'));
     $this->assertTrue(Folder::isAbsolute('C:\\cake'));
     $this->assertTrue(Folder::isAbsolute('C:\\path\\to\\file'));
     $this->assertTrue(Folder::isAbsolute('d:\\path\\to\\file'));
     $this->assertTrue(Folder::isAbsolute('\\\\vmware-host\\Shared Folders\\file'));
     $this->assertTrue(Folder::isAbsolute('http://www.example.com'));
 }
 /**
  * Imports a database backup
  * @param string $filename Filename
  * @return void
  * @uses DatabaseBackup\Utility\BackupImport::filename()
  * @uses DatabaseBackup\Utility\BackupImport::import()
  */
 public function import($filename)
 {
     //The filename can be relative to the APP root
     if (!Folder::isAbsolute($filename)) {
         $filename = ROOT . DS . $filename;
     }
     try {
         $backup = new \DatabaseBackup\Utility\BackupImport();
         $backup->filename($filename);
         //Imports the backup file
         $file = $backup->import();
         $this->success(__d('database_backup', 'The backup {0} has been imported', $file));
     } catch (InternalErrorException $e) {
         $this->abort($e->getMessage());
     }
 }