Esempio n. 1
0
 /**
  * Empty the folder.
  * Instead of deleting the folder itself as delete() does,
  * this method only removes its content recursivly.
  *
  * Note: It skips hidden folders (starting with a . dot).
  *
  * @return bool Success or null on invalid folder
  */
 public function clear($path = null)
 {
     if (!$path) {
         $path = $this->pwd();
     }
     if (!$path) {
         return null;
     }
     $path = Folder::slashTerm($path);
     if (!is_dir($path)) {
         return null;
     }
     $normalFiles = glob($path . '*');
     $hiddenFiles = glob($path . '\\.?*');
     $normalFiles = $normalFiles ? $normalFiles : array();
     $hiddenFiles = $hiddenFiles ? $hiddenFiles : array();
     $files = array_merge($normalFiles, $hiddenFiles);
     foreach ($files as $file) {
         if (preg_match('/(\\.|\\.\\.)$/', $file)) {
             continue;
         }
         if (is_file($file)) {
             if (@unlink($file)) {
                 $this->_messages[] = __('%s removed', $file);
             } else {
                 $this->_errors[] = __('%s NOT removed', $file);
             }
         } elseif (is_dir($file) && $this->delete($file) === false) {
             return false;
         }
     }
     return true;
 }
Esempio n. 2
0
 /**
  * Returns the full path of the File.
  *
  * @return string Full path to file
  * @access public
  */
 function pwd()
 {
     if (is_null($this->path)) {
         $this->path = $this->Folder->slashTerm($this->Folder->pwd()) . $this->name;
     }
     return $this->path;
 }
Esempio n. 3
0
 /**
  * Checks that given project path does not already exist, and
  * finds the app directory in it. Then it calls bake() with that information.
  *
  * @param string $project Project path
  * @access public
  */
 function execute($project = null)
 {
     if ($project === null) {
         if (isset($this->args[0])) {
             $project = $this->args[0];
             $this->Dispatch->shiftArgs();
         }
     }
     if ($project) {
         $this->Dispatch->parseParams(array('-app', $project));
         $project = $this->params['working'];
     }
     if (empty($this->params['skel'])) {
         $this->params['skel'] = '';
         if (is_dir(CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'console' . DS . 'libs' . DS . 'templates' . DS . 'skel') === true) {
             $this->params['skel'] = CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'console' . DS . 'libs' . DS . 'templates' . DS . 'skel';
         }
     }
     while (!$project) {
         $project = $this->in("What is the full path for this app including the app directory name?\nExample: " . $this->params['working'] . DS . "myapp", null, $this->params['working'] . DS . 'myapp');
     }
     if ($project) {
         $response = false;
         while ($response == false && is_dir($project) === true && file_exists($project . 'config' . 'core.php')) {
             $response = $this->in('A project already exists in this location: ' . $project . ' Overwrite?', array('y', 'n'), 'n');
             if (strtolower($response) === 'n') {
                 $response = $project = false;
             }
         }
     }
     if ($this->bake($project)) {
         $path = Folder::slashTerm($project);
         if ($this->createHome($path)) {
             $this->out(__('Welcome page created', true));
         } else {
             $this->out(__('The Welcome page was NOT created', true));
         }
         if ($this->securitySalt($path) === true) {
             $this->out(__('Random hash key created for \'Security.salt\'', true));
         } else {
             $this->err(sprintf(__('Unable to generate random hash for \'Security.salt\', you should change it in %s', true), CONFIGS . 'core.php'));
         }
         $corePath = $this->corePath($path);
         if ($corePath === true) {
             $this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', true), CAKE_CORE_INCLUDE_PATH));
             $this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', true), CAKE_CORE_INCLUDE_PATH));
             $this->out(__('Remember to check these value after moving to production server', true));
         } elseif ($corePath === false) {
             $this->err(sprintf(__('Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', true), $path . 'webroot' . DS . 'index.php'));
         }
         $Folder = new Folder($path);
         if (!$Folder->chmod($path . 'tmp', 0777)) {
             $this->err(sprintf(__('Could not set permissions on %s', true), $path . DS . 'tmp'));
             $this->out(sprintf(__('chmod -R 0777 %s', true), $path . DS . 'tmp'));
         }
         $this->params['working'] = $path;
         $this->params['app'] = basename($path);
         return true;
     }
 }
Esempio n. 4
0
 /**
  * Returns the full path of the file.
  *
  * @return string Full path to the file
  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::pwd
  */
 public function pwd()
 {
     if ($this->path === null) {
         $this->path = $this->Folder->slashTerm($this->Folder->pwd()) . $this->name;
     }
     return $this->path;
 }
Esempio n. 5
0
 /**
  * Returns the full path of the file.
  *
  * @return string Full path to the file
  */
 public function pwd()
 {
     if ($this->path === null) {
         $dir = $this->Folder->pwd();
         if (is_dir($dir)) {
             $this->path = $this->Folder->slashTerm($dir) . $this->name;
         }
     }
     return $this->path;
 }
 /**
  * beforeFilter callback
  *
  * @return void
  **/
 public function beforeFilter()
 {
     parent::beforeFilter();
     $this->ApiConfig = ClassRegistry::init('ApiGenerator.ApiConfig');
     $this->ApiConfig->read();
     $path = $this->ApiConfig->getPath();
     if (empty($path)) {
         $path = APP;
         $this->ApiConfig->data['paths'][$path] = true;
     }
     $this->path = Folder::slashTerm(realpath($path));
 }
Esempio n. 7
0
 /**
  * Create new project
  */
 public function create()
 {
     $project = '.';
     $root = ROOT;
     $app = APP_DIR;
     $working = APP;
     $core = "{$root}/{$app}/Vendor/cakephp/cakephp/lib";
     $skel = "{$core}/Cake/Console/Templates/skel";
     $this->out(__d('cake_console', '<info>Create project `%s` in `%s`</info>', $app, $working));
     if (!empty($project) && !Folder::isAbsolute($project) && isset($_SERVER['PWD'])) {
         $project = $_SERVER['PWD'] . DS . $project;
     }
     if ($this->_bake($project, $skel)) {
         $path = Folder::slashTerm($project);
         $this->_fixConfigureFiles($path);
         $this->_fixCakeCoreIncludePath($path);
         $this->_fixDebugKitPlugin($path);
         $this->_fixAutoloader($path);
         $this->_fixPermissionForDirs($path);
         $this->_fixGitIgnore($path);
     }
 }
 public function uploadFullPath()
 {
     return Folder::slashTerm($this->uploadPath() . $this->uploadDir());
 }
Esempio n. 9
0
 /**
  * Parses instruction sets and invokes `makeVersion()` for each version on a file.
  * Also creates the destination directory if enabled by settings.
  *
  * If the `makeVersion()` method is implemented in the current model it'll be used
  * for generating a specifc version of the file (i.e. `s`, `m` or `l`) otherwise
  * the method within this behavior is going to be used.
  *
  * If you already have generated versions of files and change the filter
  * configuration afterwards you may want to recreate those files with the new
  * settings.
  *
  * You can achieve that by removing already generated files first (optional), than
  * invoking the task from the shell:
  * $ cake media make
  *
  * For more information on options and arguments for the task call:
  * $ cake media help
  *
  * @param Model $Model
  * @param string $file Path to a file relative to `baseDirectory`  or an absolute path to a file
  * @return boolean
  */
 function make(&$Model, $file)
 {
     extract($this->settings[$Model->alias]);
     list($file, $relativeFile) = $this->_file($Model, $file);
     $relativeDirectory = DS . rtrim(dirname($relativeFile), '.');
     $filter = Configure::read('Media.filter.' . Mime_Type::guessName($file));
     $result = true;
     foreach ($filter as $version => $instructions) {
         $directory = Folder::slashTerm($filterDirectory . $version . $relativeDirectory);
         $Folder = new Folder($directory, $createDirectory, $createDirectoryMode);
         if (!$Folder->pwd()) {
             $message = "GeneratorBehavior::make - Directory `{$directory}` ";
             $message .= "could not be created or is not writable. ";
             $message .= "Please check the permissions.";
             trigger_error($message, E_USER_WARNING);
             $result = false;
             continue;
         }
         try {
             $result = $Model->makeVersion($file, compact('version', 'directory', 'instructions'));
         } catch (Exception $E) {
             $message = "GeneratorBehavior::make - While making version `{$version}` ";
             $message .= "of file `{$file}` an exception was thrown, the message provided ";
             $message .= 'was `' . $E->getMessage() . '`. Skipping version.';
             trigger_error($message, E_USER_WARNING);
             $result = false;
         }
         if (!$result) {
             $message = "GeneratorBehavior::make - The method responsible for making version ";
             $message .= "`{$version}` of file `{$file}` returned `false`. Skipping version.";
             trigger_error($message, E_USER_WARNING);
             $result = false;
         }
     }
     return $result;
 }
Esempio n. 10
0
 /**
  * Returns a path based on settings configuration
  *
  * @param Model $model Model instance
  * @param string $field Name of field being modified
  * @param array $options Options to use when building a path
  * @return string
  **/
 protected function _path(Model $model, $field, $options = array())
 {
     $defaults = array('isThumbnail' => true, 'path' => '{ROOT}webroot{DS}files{DS}{model}{DS}{field}{DS}', 'rootDir' => $this->defaults['rootDir']);
     $options = array_merge($defaults, $options);
     foreach ($options as $key => $value) {
         if ($value === null) {
             $options[$key] = $defaults[$key];
         }
     }
     if (!$options['isThumbnail']) {
         $options['path'] = str_replace(array('{size}', '{geometry}'), '', $options['path']);
     }
     $replacements = array('{ROOT}' => $options['rootDir'], '{primaryKey}' => $model->id, '{model}' => Inflector::underscore($model->alias), '{field}' => $field, '{time}' => time(), '{microtime}' => microtime(), '{DS}' => DIRECTORY_SEPARATOR, '//' => DIRECTORY_SEPARATOR, '/' => DIRECTORY_SEPARATOR, '\\' => DIRECTORY_SEPARATOR);
     $newPath = Folder::slashTerm(str_replace(array_keys($replacements), array_values($replacements), $options['path']));
     if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
         if (!preg_match('/^([a-zA-Z]:\\\\|\\\\)/', $newPath)) {
             $newPath = $options['rootDir'] . $newPath;
         }
     } elseif ($newPath[0] !== DIRECTORY_SEPARATOR) {
         $newPath = $options['rootDir'] . $newPath;
     }
     $pastPath = $newPath;
     while (true) {
         $pastPath = $newPath;
         $newPath = str_replace(array('//', '\\', DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR, $newPath);
         if ($pastPath == $newPath) {
             break;
         }
     }
     return $newPath;
 }
Esempio n. 11
0
 /**
  * __realPath
  *
  * @param string $path path
  * @return string Real path
  */
 private function __realPath($path)
 {
     $replacements = array('{ROOT}' => $this->rootDir, '{DS}' => DS);
     $path = Folder::slashTerm(str_replace(array_keys($replacements), array_values($replacements), $path));
     return $path;
 }
Esempio n. 12
0
 /**
  * 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 execute()
 {
     $project = null;
     if (isset($this->args[0])) {
         $project = $this->args[0];
     } else {
         $appContents = array_diff(scandir(APP), array('.', '..'));
         if (empty($appContents)) {
             $suggestedPath = rtrim(APP, DS);
         } else {
             $suggestedPath = APP . 'myapp';
         }
     }
     while (!$project) {
         $prompt = __d('cake_console', "What is the path to the project you want to bake?");
         $project = $this->in($prompt, null, $suggestedPath);
     }
     if ($project && !Folder::isAbsolute($project) && isset($_SERVER['PWD'])) {
         $project = $_SERVER['PWD'] . DS . $project;
     }
     $response = false;
     while (!$response && is_dir($project) === true && file_exists($project . 'Config' . 'core.php')) {
         $prompt = __d('cake_console', '<warning>A project already exists in this location:</warning> %s Overwrite?', $project);
         $response = $this->in($prompt, array('y', 'n'), 'n');
         if (strtolower($response) === 'n') {
             $response = $project = false;
         }
     }
     $success = true;
     if ($this->bake($project)) {
         $path = Folder::slashTerm($project);
         if ($this->securitySalt($path) === true) {
             $this->out(__d('cake_console', ' * Random hash key created for \'Security.salt\''));
         } else {
             $this->err(__d('cake_console', 'Unable to generate random hash for \'Security.salt\', you should change it in %s', APP . 'Config' . DS . 'core.php'));
             $success = false;
         }
         if ($this->securityCipherSeed($path) === true) {
             $this->out(__d('cake_console', ' * Random seed created for \'Security.cipherSeed\''));
         } else {
             $this->err(__d('cake_console', 'Unable to generate random seed for \'Security.cipherSeed\', you should change it in %s', APP . 'Config' . DS . 'core.php'));
             $success = false;
         }
         if ($this->cachePrefix($path)) {
             $this->out(__d('cake_console', ' * Cache prefix set'));
         } else {
             $this->err(__d('cake_console', 'The cache prefix was <error>NOT</error> set'));
             $success = false;
         }
         if ($this->consolePath($path) === true) {
             $this->out(__d('cake_console', ' * app/Console/cake.php path set.'));
         } else {
             $this->err(__d('cake_console', 'Unable to set console path for app/Console.'));
             $success = false;
         }
         $hardCode = false;
         if ($this->cakeOnIncludePath()) {
             $this->out(__d('cake_console', '<info>CakePHP is on your `include_path`. CAKE_CORE_INCLUDE_PATH will be set, but commented out.</info>'));
         } else {
             $this->out(__d('cake_console', '<warning>CakePHP is not on your `include_path`, CAKE_CORE_INCLUDE_PATH will be hard coded.</warning>'));
             $this->out(__d('cake_console', 'You can fix this by adding CakePHP to your `include_path`.'));
             $hardCode = true;
         }
         $success = $this->corePath($path, $hardCode) === true;
         if ($success) {
             $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in %s', CAKE_CORE_INCLUDE_PATH, 'webroot/index.php'));
             $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in %s', CAKE_CORE_INCLUDE_PATH, 'webroot/test.php'));
         } else {
             $this->err(__d('cake_console', 'Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', $path . 'webroot' . DS . 'index.php'));
             $success = false;
         }
         if ($success && $hardCode) {
             $this->out(__d('cake_console', '   * <warning>Remember to check these values after moving to production server</warning>'));
         }
         $Folder = new Folder($path);
         if (!$Folder->chmod($path . 'tmp', 0777)) {
             $this->err(__d('cake_console', 'Could not set permissions on %s', $path . DS . 'tmp'));
             $this->out('chmod -R 0777 ' . $path . DS . 'tmp');
             $success = false;
         }
         if ($success) {
             $this->out(__d('cake_console', '<success>Project baked successfully!</success>'));
         } else {
             $this->out(__d('cake_console', 'Project baked but with <warning>some issues.</warning>.'));
         }
         return $path;
     }
 }
Esempio n. 13
0
// Define the default/fallback datastore folder as application_folder/data
Configure::load('defaults');
// If a datastore.php exists, overwrite the datastore folder location
if (file_exists(APP . 'Config' . DS . 'datastore.php')) {
    $datastorePath = (include APP . 'Config' . DS . 'datastore.php');
}
// Alternatively read the '[APP_NAME]_DATASTORE' environment variable
if (empty($datastorePath)) {
    $datastorePath = env(APP_NAME . '_DATASTORE');
}
// If the datastore path was set by datastore.php OR the environment variable
if (!empty($datastorePath)) {
    Configure::write(APP_NAME . '.datastore', Folder::slashTerm($datastorePath));
}
// Build datastore paths
$datastorePath = Folder::slashTerm(Configure::read(APP_NAME . '.datastore'));
$datastoreConfig = $datastorePath . 'config' . DS;
$datastoreFiles = $datastorePath . 'files' . DS;
$datastoreLogs = $datastorePath . 'logs' . DS;
$datastoreTmp = $datastorePath . 'tmp' . DS;
$datastoreCache = $datastoreTmp . 'cache' . DS;
$datastoreSessions = $datastoreTmp . 'sessions' . DS;
// Reset Cache, logging and Session paths accordingly
Configure::write('Cache.default.path', $datastoreCache);
Configure::write('Cache.core.path', $datastoreCache . 'persistent' . DS);
Configure::write('Cache.model.path', $datastoreCache . 'models' . DS);
Configure::write('Session.ini', Hash::merge(Configure::read('Session.ini'), array('session.save_path' => $datastoreSessions)));
Configure::write(APP_NAME . '.logging.debug.path', $datastoreLogs);
Configure::write(APP_NAME . '.logging.error.path', $datastoreLogs);
// Ignore datastore when using the AppSetup shell
if (php_sapi_name() == "cli" && (in_array('orca_app_setup.app_setup', env('argv')) || in_array('orca_app_setup.AppSetup', env('argv')) || in_array('OrcaAppSetup.app_setup', env('argv')) || in_array('OrcaAppSetup.AppSetup', env('argv')))) {
Esempio n. 14
0
 /**
  * Returns the full path of the File.
  *
  * @return string
  */
 function getFullPath()
 {
     return $this->folder->slashTerm($this->folder->pwd()) . $this->getName();
 }
Esempio n. 15
0
 /**
  * testStatic method
  *
  * @access public
  * @return void
  */
 function testStatic()
 {
     $result = Folder::slashTerm('/path/to/file');
     $this->assertEqual($result, '/path/to/file/');
 }
Esempio n. 16
0
 /**
  * Parses instruction sets and invokes `Medium::make()` for a file
  *
  * @param Model $Model
  * @param string $file Path to a file relative to `baseDirectory`  or an absolute path to a file
  * @return boolean
  */
 function make(&$Model, $file, $overwrite = false)
 {
     extract($this->settings[$Model->alias]);
     list($file, $relativeFile) = $this->_file($Model, $file);
     $relativeDirectory = DS . rtrim(dirname($relativeFile), '.');
     $name = Medium::name($file);
     $filter = Configure::read('Media.filter.' . strtolower($name));
     $hasCallback = method_exists($Model, 'beforeMake');
     foreach ($filter as $version => $instructions) {
         $directory = Folder::slashTerm($filterDirectory . $version . $relativeDirectory);
         $Folder = new Folder($directory, $createDirectory);
         if (!$Folder->pwd()) {
             $message = "MediaBehavior::make - Directory `{$directory}` ";
             $message .= "could not be created or is not writable. ";
             $message .= "Please check the permissions.";
             trigger_error($message, E_USER_WARNING);
             continue;
         }
         if ($hasCallback) {
             $process = compact('overwrite', 'directory', 'name', 'version', 'instructions');
             if ($Model->beforeMake($file, $process)) {
                 continue;
             }
         }
         if (!($Medium = Medium::make($file, $instructions))) {
             $message = "MediaBehavior::make - Failed to make version `{$version}` ";
             $message .= "of file `{$file}`. ";
             trigger_error($message, E_USER_WARNING);
             continue;
         }
         $Medium->store($directory . basename($file), $overwrite);
     }
     return true;
 }
Esempio n. 17
0
 /**
  * Looks for a skeleton template of a Cake application,
  * and if not found asks the user for a path. When there is a path
  * this method will make a deep copy of the skeleton to the project directory.
  * A default home page will be added, and the tmp file storage will be chmod'ed to 0777.
  *
  * @param string $path Project path
  * @access private
  */
 function __buildDirLayout($path)
 {
     $skel = $this->params['skel'];
     while ($skel == '') {
         $skel = $this->in("What is the path to the app directory you wish to copy?\nExample: " . APP, null, ROOT . DS . 'myapp' . DS);
         if ($skel == '') {
             $this->out('The directory path you supplied was empty. Please try again.');
         } else {
             while (is_dir($skel) === false) {
                 $skel = $this->in('Directory path does not exist please choose another:');
             }
         }
     }
     $app = basename($path);
     $this->out('Bake Project');
     $this->out("Skel Directory: {$skel}");
     $this->out("Will be copied to: {$path}");
     $this->hr();
     $looksGood = $this->in('Look okay?', array('y', 'n', 'q'), 'y');
     if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
         $verboseOuptut = $this->in('Do you want verbose output?', array('y', 'n'), 'n');
         $verbose = false;
         if (low($verboseOuptut) == 'y' || low($verboseOuptut) == 'yes') {
             $verbose = true;
         }
         $Folder = new Folder($skel);
         if ($Folder->copy($path)) {
             $path = $Folder->slashTerm($path);
             $this->hr();
             $this->out(sprintf(__("Created: %s in %s", true), $app, $path));
             $this->hr();
             if ($this->createHome($path)) {
                 $this->out('Welcome page created');
             } else {
                 $this->out('The Welcome page was NOT created');
             }
             if ($this->securitySalt($path) === true) {
                 $this->out('Random hash key created for \'Security.salt\'');
             } else {
                 $this->err('Unable to generate random hash for \'Security.salt\', please change this yourself in ' . CONFIGS . 'core.php');
             }
             $corePath = $this->corePath($path);
             if ($corePath === true) {
                 $this->out('CAKE_CORE_INCLUDE_PATH set to ' . CAKE_CORE_INCLUDE_PATH);
             } elseif ($corePath === false) {
                 $this->err('Unable to to set CAKE_CORE_INCLUDE_PATH, please change this yourself in ' . $path . 'webroot' . DS . 'index.php');
             }
             if (!$Folder->chmod($path . 'tmp', 0777)) {
                 $this->err('Could not set permissions on ' . $path . DS . 'tmp');
                 $this->out('You must manually check that these directories can be wrote to by the server');
             }
         } else {
             $this->err(" '" . $app . "' could not be created properly");
         }
         if ($verbose) {
             foreach ($Folder->messages() as $message) {
                 $this->out($message);
             }
         }
         return;
     } elseif (low($looksGood) == 'q' || low($looksGood) == 'quit') {
         $this->out('Bake Aborted.');
     } else {
         $this->params['working'] = null;
         $this->params['app'] = null;
         $this->execute(false);
     }
 }
Esempio n. 18
0
 /**
  * Parses instruction sets and invokes `makeVersion()` for each version on a file.
  * Also creates the destination directory if enabled by settings.
  *
  * If the `makeVersion()` method is implemented in the current model it'll be used
  * for generating a specifc version of the file (i.e. `s`, `m` or `l`) otherwise
  * the method within this behavior is going to be used.
  *
  * @param Model $Model
  * @param string $file Path to a file relative to `baseDirectory`  or an absolute path to a file
  * @return boolean
  */
 function make(&$Model, $file)
 {
     extract($this->settings[$Model->alias]);
     list($file, $relativeFile) = $this->_file($Model, $file);
     $relativeDirectory = DS . rtrim(dirname($relativeFile), '.');
     $filter = Configure::read('Media.filter.' . Mime_Type::guessName($file));
     $result = true;
     foreach ($filter as $version => $instructions) {
         $directory = Folder::slashTerm($filterDirectory . $version . $relativeDirectory);
         $Folder = new Folder($directory, $createDirectory, $createDirectoryMode);
         if (!$Folder->pwd()) {
             $message = "GeneratorBehavior::generateVersion - Directory `{$directory}` ";
             $message .= "could not be created or is not writable. ";
             $message .= "Please check the permissions.";
             trigger_error($message, E_USER_WARNING);
             $result = false;
             continue;
         }
         if (!$Model->makeVersion($file, compact('version', 'directory', 'instructions'))) {
             $message = "GeneratorBehavior::make - Failed to make version `{$version}` ";
             $message .= "of file `{$file}`. ";
             trigger_error($message, E_USER_WARNING);
             $result = false;
         }
     }
     return $result;
 }
Esempio n. 19
0
 /**
  * Recursively Remove directories if the system allows.
  *
  * @param string $path Path of directory to delete
  *
  * @return boolean Success
  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::delete
  */
 public function delete($path = null)
 {
     if (!$path) {
         $path = $this->pwd();
     }
     if (!$path) {
         return null;
     }
     $path = Folder::slashTerm($path);
     if (is_dir($path)) {
         try {
             $directory = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::CURRENT_AS_SELF);
             $iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::CHILD_FIRST);
         } catch (Exception $e) {
             return false;
         }
         foreach ($iterator as $item) {
             $filePath = $item->getPathname();
             if ($item->isFile() || $item->isLink()) {
                 //@codingStandardsIgnoreStart
                 if (@unlink($filePath)) {
                     //@codingStandardsIgnoreEnd
                     $this->_messages[] = __d('cake_dev', '%s removed', $filePath);
                 } else {
                     $this->_errors[] = __d('cake_dev', '%s NOT removed', $filePath);
                 }
             } elseif ($item->isDir() && !$item->isDot()) {
                 //@codingStandardsIgnoreStart
                 if (@rmdir($filePath)) {
                     //@codingStandardsIgnoreEnd
                     $this->_messages[] = __d('cake_dev', '%s removed', $filePath);
                 } else {
                     $this->_errors[] = __d('cake_dev', '%s NOT removed', $filePath);
                     return false;
                 }
             }
         }
         $path = rtrim($path, DS);
         //@codingStandardsIgnoreStart
         if (@rmdir($path)) {
             //@codingStandardsIgnoreEnd
             $this->_messages[] = __d('cake_dev', '%s removed', $path);
         } else {
             $this->_errors[] = __d('cake_dev', '%s NOT removed', $path);
             return false;
         }
     }
     return true;
 }
Esempio n. 20
0
 /**
  * Returns a path based on settings configuration
  *
  * @return void
  **/
 function _path(&$model, $fieldName, $options = array())
 {
     $defaults = array('isThumbnail' => true, 'path' => '{ROOT}webroot{DS}files{DS}{model}{DS}{field}{DS}', 'rootDir' => $this->defaults['rootDir']);
     $options = array_merge($defaults, $options);
     foreach ($options as $key => $value) {
         if ($value === null) {
             $options[$key] = $defaults[$key];
         }
     }
     if (!$options['isThumbnail']) {
         $options['path'] = str_replace(array('{size}', '{geometry}'), '', $options['path']);
     }
     $replacements = array('{ROOT}' => $options['rootDir'], '{model}' => Inflector::underscore($model->alias), '{field}' => $fieldName, '{DS}' => DIRECTORY_SEPARATOR, '//' => DIRECTORY_SEPARATOR, '/' => DIRECTORY_SEPARATOR, '\\' => DIRECTORY_SEPARATOR);
     $newPath = Folder::slashTerm(str_replace(array_keys($replacements), array_values($replacements), $options['path']));
     if ($newPath[0] !== DIRECTORY_SEPARATOR) {
         $newPath = $options['rootDir'] . $newPath;
     }
     $pastPath = $newPath;
     while (true) {
         $pastPath = $newPath;
         $newPath = str_replace(array('//', '\\', DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR), DIRECTORY_SEPARATOR, $newPath);
         if ($pastPath == $newPath) {
             break;
         }
     }
     return $newPath;
 }
Esempio n. 21
0
 /**
  * "makes" a file
  *
  * @param string $file Absolute path to a file
  * @access protected
  * @return boolean
  */
 function _make($file)
 {
     $File = new File($file);
     $name = Medium::name($file);
     $subdir = array_pop(explode(DS, dirname($this->source)));
     if ($name === 'Icon' || strpos($file, 'ico' . DS) !== false) {
         return true;
     }
     if ($this->version) {
         $configString = 'Media.filter.' . strtolower($name) . '.' . $this->version;
         $filter = array(Configure::read($configString));
     } else {
         $configString = 'Media.filter.' . strtolower($name);
         $filter = Configure::read($configString);
     }
     foreach ($filter as $version => $instructions) {
         $directory = Folder::slashTerm(rtrim($this->destination . $version . DS . $subdir, '.'));
         $Folder = new Folder($directory, $this->_createDirectories);
         if (!$Folder->pwd()) {
             $this->err($directory . ' could not be created or is not writable.');
             $this->err('Please check your permissions.');
             return false;
         }
         $Medium = Medium::make($File->pwd(), $instructions);
         if (!$Medium) {
             $this->err('Failed to make version ' . $version . ' of medium.');
             return false;
         }
         $Medium->store($Folder->pwd() . $File->name, $this->overwrite);
     }
     return true;
 }
Esempio n. 22
0
 /**
  * undocumented function
  *
  * @param string $path
  * @return void
  */
 function read($path = null)
 {
     $data = null;
     if (!is_dir($this->Repo->working)) {
         return false;
     }
     if (is_file($this->Repo->working . DS . $path)) {
         $File = new File($this->Repo->working . DS . $path);
         return array('Content' => $File->read());
     }
     $isRoot = false;
     $wwwPath = $base = null;
     if ($path) {
         $wwwPath = $base = join('/', explode(DS, $path)) . '/';
     }
     $Folder = new Folder($this->Repo->working . '/' . $path);
     $path = Folder::slashTerm($Folder->pwd());
     if ($this->Repo->type == 'git') {
         if ($this->Repo->branch == null) {
             $isRoot = true;
         } elseif ($this->Repo->branch != 'master') {
             $wwwPath = 'branches/' . $this->Repo->branch . '/' . $base;
         }
     }
     list($dirs, $files) = $Folder->read(true, array('.git', '.svn'));
     $dir = $file = array();
     $count = count($dirs);
     for ($i = 0; $i < $count; $i++) {
         $dir[$i]['name'] = $dirs[$i];
         $lookup = $path . $dirs[$i];
         $here = $wwwPath . $dirs[$i];
         if ($dirs[$i] == 'master') {
             $isRoot = true;
         }
         if ($isRoot) {
             $this->Repo->working = $path . $dirs[$i];
             $here = $base . 'branches/' . $dirs[$i];
             if ($dirs[$i] == 'master') {
                 $here = $base;
             }
         }
         $dir[$i]['path'] = $here;
         $dir[$i]['info'] = $this->Repo->pathInfo($lookup . DS);
         //$dir[$i]['md5'] = null;
         //$dir[$i]['size'] = $this->__size($path . $dirs[$i]);
         //$dir[$i]['icon'] = '/icons/dir.gif';
     }
     $count = count($files);
     for ($i = 0; $i < $count; $i++) {
         $file[$i]['name'] = $files[$i];
         $file[$i]['path'] = $wwwPath . $files[$i];
         $file[$i]['info'] = $this->Repo->pathInfo($path . $files[$i]);
         //$file[$i]['md5'] = md5($Folder->pwd() . $files[$i]);
         //$file[$i]['size'] = $this->__size($path . $files[$i]);
         //$file[$i]['icon'] = $this->__icon($files[$i]);
     }
     return array('Folder' => $dir, 'File' => $file);
 }
Esempio n. 23
0
 /**
  * testStatic method
  *
  * @return void
  */
 public function testSlashTerm()
 {
     $result = Folder::slashTerm('/path/to/file');
     $this->assertEquals('/path/to/file/', $result);
 }
Esempio n. 24
0
 /**
  * __realPath
  *
  * @param string $path path
  * @return string Real path
  */
 private function __realPath($path)
 {
     CakeLog::debug('FindModel::__realPath()');
     $replacements = array('{ROOT}' => $this->rootDir, '{DS}' => DS);
     CakeLog::debug('FindModel::__realPath() array_keys($replacements)' . print_r(array_keys($replacements), true));
     CakeLog::debug('FindModel::__realPath() array_values($replacements)' . print_r(array_values($replacements), true));
     CakeLog::debug('FindModel::__realPath() $path' . print_r($path, true));
     $path = Folder::slashTerm(str_replace(array_keys($replacements), array_values($replacements), $path));
     return $path;
 }
Esempio n. 25
0
 /**
  * set the basePath
  *
  * @return void
  **/
 public function setBasePath($path)
 {
     $this->_basePath = Folder::slashTerm(realpath($path));
 }
Esempio n. 26
0
 /**
  * Checks that given project path does not already exist, and
  * finds the app directory in it. Then it calls bake() with that information.
  *
  * @param string $project Project path
  */
 public function execute()
 {
     $project = null;
     if (isset($this->args[0])) {
         $project = $this->args[0];
     }
     if ($project && isset($_SERVER['PWD'])) {
         $project = $_SERVER['PWD'] . DS . $project;
     }
     if (empty($this->params['skel'])) {
         $core = App::core('shells');
         $skelPath = dirname($core[0]) . DS . 'templates' . DS . 'skel';
         if (is_dir($skelPath) === true) {
             $this->params['skel'] = $skelPath;
         }
     }
     while (!$project) {
         $prompt = __("What is the full path for this app including the app directory name?\n Example:");
         $default = APP_PATH . 'myapp';
         $project = $this->in($prompt . $default, null, $default);
     }
     if ($project) {
         $response = false;
         while ($response == false && is_dir($project) === true && file_exists($project . 'config' . 'core.php')) {
             $prompt = __('<warning>A project already exists in this location:</warning> %s Overwrite?', $project);
             $response = $this->in($prompt, array('y', 'n'), 'n');
             if (strtolower($response) === 'n') {
                 $response = $project = false;
             }
         }
     }
     $success = true;
     if ($this->bake($project)) {
         $path = Folder::slashTerm($project);
         if ($this->createHome($path)) {
             $this->out(__(' * Welcome page created'));
         } else {
             $this->err(__('The Welcome page was <error>NOT</error> created'));
             $success = false;
         }
         if ($this->securitySalt($path) === true) {
             $this->out(__(' * Random hash key created for \'Security.salt\''));
         } else {
             $this->err(__('Unable to generate random hash for \'Security.salt\', you should change it in %s', CONFIGS . 'core.php'));
             $success = false;
         }
         if ($this->securityCipherSeed($path) === true) {
             $this->out(__(' * Random seed created for \'Security.cipherSeed\''));
         } else {
             $this->err(__('Unable to generate random seed for \'Security.cipherSeed\', you should change it in %s', CONFIGS . 'core.php'));
             $success = false;
         }
         if ($this->corePath($path) === true) {
             $this->out(__(' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', CAKE_CORE_INCLUDE_PATH));
             $this->out(__(' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', CAKE_CORE_INCLUDE_PATH));
             $this->out(__('   * <warning>Remember to check these value after moving to production server</warning>'));
         } else {
             $this->err(__('Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', $path . 'webroot' . DS . 'index.php'));
             $success = false;
         }
         if ($this->consolePath($path) === true) {
             $this->out(__(' * app/console/cake.php path set.'));
         } else {
             $this->err(__('Unable to set console path for app/console.'));
             $success = false;
         }
         $Folder = new Folder($path);
         if (!$Folder->chmod($path . 'tmp', 0777)) {
             $this->err(__('Could not set permissions on %s', $path . DS . 'tmp'));
             $this->out(__('chmod -R 0777 %s', $path . DS . 'tmp'));
             $success = false;
         }
         if ($success) {
             $this->out(__('<success>Project baked successfully!</success>'));
         } else {
             $this->out(__('Project baked but with <warning>some issues.</warning>.'));
         }
         return $path;
     }
 }
Esempio n. 27
0
 /**
  * Get the real path (taking ".." and such into account)
  *
  * @param string $path Path to resolve
  * @return string The resolved path
  * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#Folder::realpath
  */
 public function realpath($path)
 {
     $path = str_replace('/', DS, trim($path));
     if (strpos($path, '..') === false) {
         if (!Folder::isAbsolute($path)) {
             $path = Folder::addPathElement($this->path, $path);
         }
         return $path;
     }
     $parts = explode(DS, $path);
     $newparts = array();
     $newpath = '';
     if ($path[0] === DS) {
         $newpath = DS;
     }
     while (($part = array_shift($parts)) !== NULL) {
         if ($part === '.' || $part === '') {
             continue;
         }
         if ($part === '..') {
             if (!empty($newparts)) {
                 array_pop($newparts);
                 continue;
             } else {
                 return false;
             }
         }
         $newparts[] = $part;
     }
     $newpath .= implode(DS, $newparts);
     return Folder::slashTerm($newpath);
 }
Esempio n. 28
0
 /**
  * Checks that given project path does not already exist, and
  * finds the app directory in it. Then it calls bake() with that information.
  *
  * @param string $project Project path
  * @access public
  */
 function execute($project = null)
 {
     if ($project === null) {
         if (isset($this->args[0])) {
             $project = $this->args[0];
             $this->Dispatch->shiftArgs();
         }
     }
     if ($project) {
         if ($project[0] == '/' || $project[0] == DS) {
             $this->Dispatch->parseParams(array('-working', $project));
         } else {
             $this->Dispatch->parseParams(array('-app', $project));
         }
     }
     $project = $this->params['working'];
     if (empty($this->params['skel'])) {
         $this->params['skel'] = '';
         if (is_dir(CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'console' . DS . 'libs' . DS . 'templates' . DS . 'skel') === true) {
             $this->params['skel'] = CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'console' . DS . 'libs' . DS . 'templates' . DS . 'skel';
         }
     }
     if ($project) {
         $response = false;
         while ($response == false && is_dir($project) === true && config('core') === true) {
             $response = $this->in('A project already exists in this location: ' . $project . ' Overwrite?', array('y', 'n'), 'n');
             if (low($response) === 'n') {
                 $response = false;
                 while (!$response) {
                     $response = $this->in("What is the full path for this app including the app directory name?\nExample: " . $this->params['root'] . DS . "myapp\n[Q]uit", null, 'Q');
                     if (strtoupper($response) === 'Q') {
                         $this->out('Bake Aborted');
                         exit;
                     }
                     $this->params['working'] = null;
                     $this->params['app'] = null;
                     $this->execute($response);
                     exit;
                 }
             }
         }
     }
     while (!$project) {
         $project = $this->in("What is the full path for this app including the app directory name?\nExample: " . $this->params['root'] . DS . "myapp", null, $this->params['root'] . DS . 'myapp');
         $this->execute($project);
         exit;
     }
     if (!is_dir($this->params['root'])) {
         $this->err(__('The directory path you supplied was not found. Please try again.', true));
     }
     if ($this->bake($project)) {
         $path = Folder::slashTerm($project);
         if ($this->createHome($path)) {
             $this->out(__('Welcome page created', true));
         } else {
             $this->out(__('The Welcome page was NOT created', true));
         }
         if ($this->securitySalt($path) === true) {
             $this->out(__('Random hash key created for \'Security.salt\'', true));
         } else {
             $this->err(sprintf(__('Unable to generate random hash for \'Security.salt\', you should change it in %s', true), CONFIGS . 'core.php'));
         }
         $corePath = $this->corePath($path);
         if ($corePath === true) {
             $this->out(sprintf(__('CAKE_CORE_INCLUDE_PATH set to %s'), true, CAKE_CORE_INCLUDE_PATH));
         } elseif ($corePath === false) {
             $this->err(sprintf(__('Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', true), $path . 'webroot' . DS . 'index.php'));
         }
         $Folder = new Folder($path);
         if (!$Folder->chmod($path . 'tmp', 0777)) {
             $this->err(sprintf(__('Could not set permissions on %s', true), $path . DS . 'tmp'));
             $this->out(sprintf(__('chmod -R 0777 %s', true), $path . DS . 'tmp'));
         }
     }
     exit;
 }
Esempio n. 29
0
 /**
  * Callback
  *
  * Requires `file` field to be present if a record is created.
  *
  * Handles deletion of a record and corresponding file if the `delete` field is
  * present and has not a value of either `null` or `'0'.`
  *
  * Prevents `dirname`, `basename`, `checksum` and `delete` fields to be written to
  * database.
  *
  * Parses contents of the `file` field if present and generates a normalized path
  * relative to the path set in the `baseDirectory` option.
  *
  * @param Model $Model Model using this behavior
  * @param array $options Options passed from Model::save().
  * @return mixed False if the operation should abort. Any other result will continue.
  */
 public function beforeSave(Model $Model, $options = array())
 {
     if (!$Model->exists()) {
         if (!isset($Model->data[$Model->alias]['file'])) {
             //unset($Model->data[$Model->alias]);
             return true;
         }
     } else {
         if (isset($Model->data[$Model->alias]['delete']) && $Model->data[$Model->alias]['delete'] !== '0') {
             $Model->delete();
             unset($Model->data[$Model->alias]);
             return true;
         }
     }
     $blacklist = array('dirname', 'basename', 'checksum', 'delete');
     $whitelist = array('id', 'file', 'model', 'foreign_key', 'created', 'modified', 'alternative');
     foreach ($Model->data[$Model->alias] as $key => $value) {
         if (in_array($key, $whitelist)) {
             continue;
         }
         if (in_array($key, $blacklist)) {
             unset($Model->data[$Model->alias][$key]);
         }
     }
     extract($this->settings[$Model->alias]);
     /* @var $baseDirectory string */
     if (isset($Model->data[$Model->alias]['file'])) {
         $File = new File($Model->data[$Model->alias]['file']);
         /* `baseDirectory` may equal the file's directory or use backslashes */
         $dirname = substr(str_replace(str_replace('\\', '/', $baseDirectory), null, str_replace('\\', '/', Folder::slashTerm($File->Folder->pwd()))), 0, -1);
         $result = array('dirname' => $dirname, 'basename' => $File->name);
         $Model->data[$Model->alias] = array_merge($Model->data[$Model->alias], $result);
     }
     return true;
 }
Esempio n. 30
0
 /**
  * Checks that given project path does not already exist, and
  * finds the app directory in it. Then it calls bake() with that information.
  *
  * @param string $project Project path
  */
 public function execute()
 {
     $project = null;
     if (isset($this->args[0])) {
         $project = $this->args[0];
     }
     while (!$project) {
         $prompt = __d('cake_console', "What is the path to the project you want to bake?");
         $project = $this->in($prompt, null, APP_PATH . 'myapp');
     }
     if ($project && !Folder::isAbsolute($project) && isset($_SERVER['PWD'])) {
         $project = $_SERVER['PWD'] . DS . $project;
     }
     $response = false;
     while ($response == false && is_dir($project) === true && file_exists($project . 'Config' . 'core.php')) {
         $prompt = __d('cake_console', '<warning>A project already exists in this location:</warning> %s Overwrite?', $project);
         $response = $this->in($prompt, array('y', 'n'), 'n');
         if (strtolower($response) === 'n') {
             $response = $project = false;
         }
     }
     $success = true;
     if ($this->bake($project)) {
         $path = Folder::slashTerm($project);
         if ($this->createHome($path)) {
             $this->out(__d('cake_console', ' * Welcome page created'));
         } else {
             $this->err(__d('cake_console', 'The Welcome page was <error>NOT</error> created'));
             $success = false;
         }
         if ($this->securitySalt($path) === true) {
             $this->out(__d('cake_console', ' * Random hash key created for \'Security.salt\''));
         } else {
             $this->err(__d('cake_console', 'Unable to generate random hash for \'Security.salt\', you should change it in %s', APP . 'Config' . DS . 'core.php'));
             $success = false;
         }
         if ($this->securityCipherSeed($path) === true) {
             $this->out(__d('cake_console', ' * Random seed created for \'Security.cipherSeed\''));
         } else {
             $this->err(__d('cake_console', 'Unable to generate random seed for \'Security.cipherSeed\', you should change it in %s', APP . 'Config' . DS . 'core.php'));
             $success = false;
         }
         if ($this->consolePath($path) === true) {
             $this->out(__d('cake_console', ' * app/Console/cake.php path set.'));
         } else {
             $this->err(__d('cake_console', 'Unable to set console path for app/Console.'));
             $success = false;
         }
         $this->out(__d('cake_console', 'The value for CAKE_CORE_INCLUDE_PATH can be hardcoded set to %s in webroot/index.php', CAKE_CORE_INCLUDE_PATH));
         $this->out(__d('cake_console', '<warning>If you hard code it, the project will possibly run only in your computer.</warning>'));
         $setConstants = $this->in(__d('cake_console', 'Do you want to set CAKE_CORE_INCLUDE_PATH in webroot/index.php?'), array('y', 'n'), 'n');
         if (strtolower($setConstants) === 'y') {
             if ($this->corePath($path) === true) {
                 $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php', CAKE_CORE_INCLUDE_PATH));
                 $this->out(__d('cake_console', ' * CAKE_CORE_INCLUDE_PATH set to %s in webroot/test.php', CAKE_CORE_INCLUDE_PATH));
                 $this->out(__d('cake_console', '   * <warning>Remember to check these values after moving to production server</warning>'));
             } else {
                 $this->err(__d('cake_console', 'Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s', $path . 'webroot' . DS . 'index.php'));
                 $success = false;
             }
         } else {
             $this->out(__d('cake_console', '<warning>Please make sure your cake core is accessible, if you have problems edit CAKE_CORE_INCLUDE_PATH in webroot/index.php</warning>'));
         }
         $Folder = new Folder($path);
         if (!$Folder->chmod($path . 'tmp', 0777)) {
             $this->err(__d('cake_console', 'Could not set permissions on %s', $path . DS . 'tmp'));
             $this->out(__d('cake_console', 'chmod -R 0777 %s', $path . DS . 'tmp'));
             $success = false;
         }
         if ($success) {
             $this->out(__d('cake_console', '<success>Project baked successfully!</success>'));
         } else {
             $this->out(__d('cake_console', 'Project baked but with <warning>some issues.</warning>.'));
         }
         return $path;
     }
 }