/** * (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('|', Media::short()) . ')' . preg_quote(DS, '/') . '/'; $fragment = preg_replace($search, null, $file); $mapped = array($file => MEDIA_STATIC . Media::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); } } }
/** * Returns a relative path to the destination file * * @param array $source Information about the source * @return string */ function transferTo(&$Model, $via, $from) { extract($from); $path = Media::short($file, $mimeType) . DS; $path .= strtolower(Inflector::slug($filename)); $path .= !empty($extension) ? '.' . strtolower($extension) : null; return $path; }
/** * Resolves partial path (compat) * * Examples: * img/cern >>> MEDIA_STATIC/img/cern.png * 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 * @deprecated */ function __compatFile($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(Media::short(), $parts)); if (!$short) { $message = "MediaHelper::file - "; $message .= "You've provided a partial path without a media 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 => Media::short(), MEDIA_TRANSFER => Media::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.'); }