/** * Triggered by `beforeValidate`, `beforeSave` or upon user request * * Prepares runtime for being used by `perform()` * * @param Model $Model * @param string $file Optionally provide a valid transfer resource to be used as source * @return mixed true if transfer is ready to be performed, false on error, null if no data was found */ function prepare(&$Model, $file = null) { if (isset($Model->data[$Model->alias]['file'])) { $file = $Model->data[$Model->alias]['file']; } if (empty($file)) { return null; } if ($this->runtime[$Model->alias]['hasPerformed']) { $this->reset($Model); } if ($this->runtime[$Model->alias]['isReady']) { return true; } /* Extraction must happen after reset */ extract($this->settings[$Model->alias], EXTR_SKIP); extract($this->runtime[$Model->alias], EXTR_SKIP); if (TransferValidation::blank($file)) { /* Set explicitly null enabling allowEmpty in rules act upon emptiness */ return $Model->data[$Model->alias]['file'] = null; } if ($source = $this->_source($Model, $file)) { $this->runtime[$Model->alias]['source'] = $source; } else { return false; } /* Temporary is optional and can fail */ if ($source['type'] !== 'file-local') { $temporary = $this->runtime[$Model->alias]['temporary'] = $this->_temporary($Model, $file); } $this->_addMarker($Model, 'DS', DS); $this->_addMarker($Model, 'uuid', String::uuid()); $this->_addMarker($Model, 'unixTimestamp', time()); $this->_addMarker($Model, 'year', date('Y')); $this->_addMarker($Model, 'month', date('m')); $this->_addMarker($Model, 'day', date('d')); $filename = $this->_addMarker($Model, 'Source.filename', $source['filename'], true); $extension = $this->_addMarker($Model, 'Source.extension', $source['extension'], true); $this->_addMarker($Model, 'Source.basename', empty($extension) ? $filename : $filename . '.' . $extension); $this->_addMarker($Model, 'Source.mimeType', $source['mimeType'], true); $this->_addMarker($Model, 'Source.type', $source['type']); $this->_addMarker($Model, 'Medium.name', strtolower(Medium::name($source['file'], $source['mimeType']))); $this->_addMarker($Model, 'Medium.short', Medium::short($source['file'], $source['mimeType'])); /* Needed for tableless Models */ if (isset($Model->data[$Model->alias])) { $this->_addMarker($Model, $Model->alias . '.', $Model->data[$Model->alias], true); $this->_addMarker($Model, 'Model.', $Model->data[$Model->alias], true); } $this->_addMarker($Model, 'Model.name', $Model->name); $this->_addMarker($Model, 'Model.alias', $Model->alias); if (!($destinationFile = $this->_replaceMarker($Model, $destinationFile))) { return false; } if ($destination = $this->_destination($Model, $baseDirectory . $destinationFile)) { $this->runtime[$Model->alias]['destination'] = $destination; } else { return false; } if ($source == $destination || $temporary == $destination) { return false; } $Folder = new Folder($destination['dirname'], $createDirectory); if (!$Folder->pwd()) { $message = "TransferBehavior::prepare - Directory `{$destination['dirname']}` could "; $message .= "not be created or is not writable. Please check the permissions."; trigger_error($message, E_USER_WARNING); return false; } return $this->runtime[$Model->alias]['isReady'] = true; }
/** * Resolves partial path * * Examples: * css/cake.generic >>> MEDIA_STATIC/css/cake.generic.css * transfer/img/image.jpg >>> MEDIA_TRANSFER/img/image.jpg * s/img/image.jpg >>> MEDIA_FILTER/s/static/img/image.jpg * * @param string|array $path Either a string or an array with dirname and basename keys * @return string|boolean False on error or if path couldn't be resolbed otherwise * an absolute path to the file */ function file($path) { $path = array(); foreach (func_get_args() as $arg) { if (is_array($arg)) { if (isset($arg['dirname'])) { $path[] = rtrim($arg['dirname'], '/\\'); } if (isset($arg['basename'])) { $path[] = $arg['basename']; } } else { $path[] = rtrim($arg, '/\\'); } } $path = implode(DS, $path); $path = str_replace(array('/', '\\'), DS, $path); if (isset($this->__cached[$path])) { return $this->__cached[$path]; } if (Folder::isAbsolute($path)) { return file_exists($path) ? $path : false; } $parts = explode(DS, $path); if (in_array($parts[0], $this->_versions)) { array_unshift($parts, basename(key($this->_map['filter']))); } if (!in_array($parts[0], array_keys($this->_directories))) { array_unshift($parts, basename(key($this->_map['static']))); } if (in_array($parts[1], $this->_versions) && !in_array($parts[2], array_keys($this->_directories))) { array_splice($parts, 2, 0, basename(key($this->_map['static']))); } $path = implode(DS, $parts); if (isset($this->__cached[$path])) { return $this->__cached[$path]; } $file = $this->_directories[array_shift($parts)] . implode(DS, $parts); if (file_exists($file)) { return $this->__cached[$path] = $file; } $short = current(array_intersect(Medium::short(), $parts)); if (!$short) { $message = "MediumHelper::file - "; $message .= "You've provided a partial path without a medium directory (e.g. img) "; $message .= "which is required to resolve the path."; trigger_error($message, E_USER_NOTICE); return false; } $extension = null; extract(pathinfo($file), EXTR_OVERWRITE); if (!isset($filename)) { /* PHP < 5.2.0 */ $filename = substr($basename, 0, isset($extension) ? -(strlen($extension) + 1) : 0); } for ($i = 0; $i < 2; $i++) { $file = $i ? $dirname . DS . $filename : $dirname . DS . $basename; foreach ($this->_extensions[$short] as $extension) { $try = $file . '.' . $extension; if (file_exists($try)) { return $this->__cached[$path] = $try; } } } return false; }
/** * Initializes directory structure * * @access public * @return void */ function init() { $message = 'Do you want to create missing media directories now?'; if ($this->in($message, 'y,n', 'n') == 'n') { return false; } $dirs = array(MEDIA => array(), MEDIA_STATIC => Medium::short(), MEDIA_TRANSFER => Medium::short(), MEDIA_FILTER => array()); foreach ($dirs as $dir => $subDirs) { if (is_dir($dir)) { $result = 'SKIP'; } else { new Folder($dir, true); if (is_dir($dir)) { $result = 'OK'; } else { $result = 'FAIL'; } } $this->out(sprintf('%-50s [%-4s]', $this->shortPath($dir), $result)); foreach ($subDirs as $subDir) { if (is_dir($dir . $subDir)) { $result = 'SKIP'; } else { new Folder($dir . $subDir, true); if (is_dir($dir . $subDir)) { $result = 'OK'; } else { $result = 'FAIL'; } } $this->out(sprintf('%-50s [%-4s]', $this->shortPath($dir . $subDir), $result)); } } $this->out(); $this->protect(); $this->out('Remember to set the correct permissions on transfer and filter directory.'); }
/** * (Interactively) maps source files to destinations * * @param string $path Path to search for source files * @access protected * @return array */ function _map($path) { $include = '.*[\\/\\].*\\.[a-z0-9]{2,3}$'; $directories = array('.htaccess', '.DS_Store', 'media', '.git', '.svn', 'simpletest', 'empty'); $extensions = array('db', 'htm', 'html', 'txt', 'php', 'ctp'); $exclude = '.*[/\\\\](' . implode('|', $directories) . ').*$'; $exclude .= '|.*[/\\\\].*\\.(' . implode('|', $extensions) . ')$'; if (!empty($this->_exclude)) { $exclude = '|.*[/\\\\](' . implode('|', $this->_exclude) . ').*$'; } $Folder = new Folder($path); $files = $Folder->findRecursive($include); foreach ($files as $file) { if (preg_match('#' . $exclude . '#', $file)) { continue; } $search[] = '/' . preg_quote($Folder->pwd(), '/') . '/'; $search[] = '/(' . implode('|', Medium::short()) . ')' . preg_quote(DS, '/') . '/'; $fragment = preg_replace($search, null, $file); $mapped = array($file => MEDIA_STATIC . Medium::short($file) . DS . $fragment); while (in_array(current($mapped), $this->_map) && $mapped) { $mapped = $this->_handleCollision($mapped); } while (file_exists(current($mapped)) && $mapped) { $this->out($this->shortPath(current($mapped)) . ' already exists.'); $answer = $this->in('Would you like to [r]ename or [s]kip?', 'r,s', 's'); if ($answer == 's') { $mapped = array(); } else { $mapped = array(key($mapped) => $this->_rename(current($mapped))); } } if ($mapped) { $this->_map[key($mapped)] = current($mapped); } } }