function crawl() { $path = url::strip_query($this->raw); $path = (array) str::split($path, '/'); if (a::first($path) == 'index.php') { array_shift($path); } // parse params foreach ($path as $p) { if (str::contains($p, ':')) { $parts = explode(':', $p); if (count($parts) < 2) { continue; } $this->params->{$parts}[0] = $parts[1]; } else { $this->path->_[] = $p; } } // get the extension from the last part of the path $this->extension = f::extension($this->path->last()); if ($this->extension != false) { // remove the last part of the path $last = array_pop($this->path->_); $this->path->_[] = f::name($last); } return $this->path; }
public static function write($file, $data, $type = null) { // type autodetection if (is_null($type)) { $type = f::extension($file); } return f::write($file, data::encode($data, $type)); }
public function to() { $source = $this->source(); $name = f::name($source['name']); $extension = f::extension($source['name']); $safeName = f::safeName($name); $safeExtension = str_replace('jpeg', 'jpg', str::lower($extension)); return str::template($this->options['to'], array('name' => $name, 'filename' => $source['name'], 'safeName' => $safeName, 'safeFilename' => $safeName . '.' . $safeExtension, 'extension' => $extension, 'safeExtension' => $safeExtension)); }
public function language() { if (!is_null($this->language)) { return $this->language; } $codes = $this->page->site()->languages()->codes(); $code = f::extension(f::name($this->root)); return $this->language = in_array($code, $codes) ? $this->page->site()->languages()->find($code) : false; }
function sublime($params) { global $site; $page = $this->obj; $id = @$params['sublime']; $class = @$params['class']; $videos = array(); $poster = false; // gather all video files which match the given id/name foreach ($page->videos() as $v) { if (preg_match('!^' . preg_quote($id) . '!i', $v->name())) { $extension = f::extension($v->name()); $mobile = $extension == 'mobile' ? $v->mobile = true : ($v->mobile = false); $hd = $extension == 'hd' ? $v->hd = true : ($v->hd = false); $videos[] = $v; } } if (empty($videos)) { return false; } // find the poster for this video foreach ($page->images() as $i) { if (preg_match('!^' . preg_quote($id) . '!i', $i->name())) { $poster = $i; break; } } $defaults = array('uid' => $id, 'name' => $id); $options = array_merge($defaults, $params); $width = html($options['width']); $height = html($options['height']); $uid = html($options['uid']); $name = html($options['name']); if (!$width) { $width = c::get('kirbytext.video.width'); } if (!$height) { $height = c::get('kirbytext.video.height'); } // create an additional css class if specified if (!empty($class)) { $class = ' ' . html($class); } // check for a poster $poster = $poster ? ' poster="' . $poster->url() . '"' : false; $html = '<video class="sublime' . $class . '"' . $poster . ' width="' . $width . '" height="' . $height . '" data-uid="' . $uid . '" data-name="' . $name . '" preload="none">'; foreach ($videos as $video) { // check for hd quality $hd = $video->hd() ? ' data-quality="hd"' : ''; // generate the source tag for each video $html .= '<source src="' . $video->url() . '"' . $hd . ' />'; } $html .= '</video>'; return $html; }
public function __construct() { $root = kirby::instance()->roots()->accounts(); foreach (dir::read($root) as $file) { // skip invalid account files if (f::extension($file) != 'php') { continue; } $user = new User(f::name($file)); $this->append($user->username(), $user); } }
static function plugins() { $root = c::get('root.plugins'); $files = dir::read($root); if (!is_array($files)) { return false; } foreach ($files as $file) { if (f::extension($file) != 'php') { continue; } self::file($root . '/' . $file); } }
public function to() { if (!is_null($this->to)) { return $this->to; } $source = $this->source(); $name = f::name($source['name']); $extension = f::extension($source['name']); $safeName = f::safeName($name); $safeExtension = str_replace('jpeg', 'jpg', str::lower($extension)); if (empty($safeExtension)) { $safeExtension = f::mimeToExtension(f::mime($source['tmp_name'])); } return $this->to = str::template($this->options['to'], array('name' => $name, 'filename' => $source['name'], 'safeName' => $safeName, 'safeFilename' => $safeName . r(!empty($safeExtension), '.' . $safeExtension), 'extension' => $extension, 'safeExtension' => $safeExtension)); }
function file($field, $destination, $params = array()) { $allowed = a::get($params, 'allowed', c::get('upload.allowed', array('image/jpeg', 'image/png', 'image/gif'))); $maxsize = a::get($params, 'maxsize', c::get('upload.maxsize', self::max_size())); $overwrite = a::get($params, 'overwrite', c::get('upload.overwrite', true)); $sanitize = a::get($params, 'sanitize', true); $file = a::get($_FILES, $field); if (empty($file)) { return array('status' => 'error', 'msg' => l::get('upload.errors.missing-file', 'The file has not been found')); } $name = a::get($file, 'name'); $type = a::get($file, 'type'); $tmp_name = a::get($file, 'tmp_name'); $error = a::get($file, 'error'); $size = a::get($file, 'size'); $msg = false; $extension = self::mime_to_extension($type, f::extension($name)); // convert the filename to a save name $fname = $sanitize ? f::safe_name(f::name($name)) : f::name($name); // setup the destination $destination = str_replace('{name}', $fname, $destination); $destination = str_replace('{extension}', $extension, $destination); if (file_exists($destination) && $overwrite == false) { return array('status' => 'error', 'msg' => l::get('upload.errors.file-exists', 'The file exists and cannot be overwritten')); } if (empty($tmp_name)) { return array('status' => 'error', 'msg' => l::get('upload.errors.missing-file', 'The file has not been found')); } if ($error != 0) { return array('status' => 'error', 'msg' => l::get('upload.errors.invalid-upload', 'The upload failed')); } if ($size > $maxsize) { return array('status' => 'error', 'msg' => l::get('upload.errors.too-big', 'The file is too big')); } if (!in_array($type, $allowed)) { return array('status' => 'error', 'msg' => l::get('upload.errors.invalid-file', 'The file type is not allowed') . ': ' . $type); } // try to change the permissions for the destination @chmod(dirname($destination), 0777); if (!@copy($tmp_name, $destination)) { return array('status' => 'error', 'msg' => l::get('upload.errors.move-error', 'The file could not be moved to the server')); } // try to change the permissions for the final file @chmod($destination, 0777); return array('status' => 'success', 'msg' => l::get('upload.success', 'The file has been uploaded'), 'type' => $type, 'extension' => $extension, 'file' => $destination, 'size' => $size, 'name' => f::filename($destination)); }
static function plugins($folder = false) { $root = c::get('root.plugins'); $folder = $folder ? $folder : $root; $files = dir::read($folder); if (!is_array($files)) { return false; } foreach ($files as $file) { if (is_dir($folder . '/' . $file) && $folder == $root) { self::plugins($folder . '/' . $file); continue; } if (f::extension($file) != 'php') { continue; } self::file($folder . '/' . $file); } }
public function photo() { if (!is_null($this->photo)) { return $this->photo; } $extension = f::extension($this->data['photo']); $filename = rtrim(sha1($this->url) . '.' . $extension, '.'); $path = c::get('webmentions.images', 'assets/images/mentions'); $root = kirby()->roots()->index() . DS . str_replace('/', DS, $path) . DS . $filename; $url = kirby()->urls()->index() . '/' . $path . '/' . $filename; $photo = new Media($root, $url); if (!$photo->exists()) { $image = remote::get($this->data['photo']); $allowed = array('image/jpeg', 'image/png', 'image/gif'); f::write($root, $image->content()); if (!in_array($photo->mime(), $allowed) or $photo->size() == 0) { $photo->delete(); } } if (!$photo->exists() or !$photo->type() == 'image') { $photo = new Obj(array('url' => $this->data['photo'], 'exists' => false)); } return $this->photo = $photo; }
/** * Upload image * * Requirement: Kirby * * @param string $name Name POSTNAME * @return array * @version 1.0 - 2011-01-12 */ function secure_upload($options) { /* $options['field'] // (required) source string $options['path'] // (required) source string */ $options['image'] = isset($options['image']) ? $options['image'] : true; // default true $options['max_size'] = isset($options['max_size']) ? min($options['max_size'], server_maxupload()) : server_maxupload(); // default server max upload in bytes if (empty($options['field']) || empty($options['path'])) { return array('error' => 'Option field and path is required'); } if (!isset($_FILES[$options['field']])) { return array('error' => 'No file was selected'); } // validate path $upload_path = $options['path']; $upload_path = rtrim($upload_path, '/') . '/'; if (@realpath($upload_path) !== false) { $upload_path = str_replace("\\", "/", realpath($upload_path)); } if (!file_exists($upload_path)) { if (!@mkdir($upload_path, 0777)) { return array('error' => 'Directory isnt writable'); } chmod($upload_path, 0777); } if (!@is_dir($upload_path) || !is_writable($upload_path)) { return array('error' => 'Directory isnt writable'); } $upload_path = preg_replace("/(.+?)\\/*\$/", "\\1/", $upload_path); // ? // Remapping for loop if (!is_array($_FILES[$options['field']]['tmp_name'])) { $_FILES[$options['field']] = array_map(function ($item) { return array($item); }, $_FILES[$options['field']]); } $success = array(); foreach ($_FILES[$options['field']]['tmp_name'] as $key => $value) { // Get upload info $error = $_FILES[$options['field']]['error'][$key]; $name = $_FILES[$options['field']]['name'][$key]; $tmp_name = $_FILES[$options['field']]['tmp_name'][$key]; $size = $_FILES[$options['field']]['size'][$key]; $type = $_FILES[$options['field']]['type'][$key]; if (!is_uploaded_file($tmp_name) || $error != UPLOAD_ERR_OK) { continue; } $type = preg_replace("/^(.+?);.*\$/", "\\1", $type); // ? $type = strtolower(trim(stripslashes($type), '"')); $ext = f::extension($name); $name = f::safe_name(f::name($name)); $name = substr($name, 0, 100); // Check allowed file type $image_types = array('gif', 'jpg', 'jpeg', 'png', 'jpe'); if ($options['image']) { if (!in_array($ext, $image_types) || !is_image($type) || getimagesize($tmp_name) === false) { continue; } } // Check file size if ($options['max_size'] < $size) { continue; } // Unique filename if (file_exists($upload_path . $name . "." . $ext)) { $number = 1; while (file_exists($upload_path . $name . $number . "." . $ext)) { $number++; } $name = $name . $number; } // save if (!@move_uploaded_file($tmp_name, $upload_path . $name . "." . $ext)) { continue; } // TODO xss clean $success[] = array('extension' => $ext, 'filename' => $name . "." . $ext, 'original_filename' => $_FILES[$options['field']]['name'][$key], 'name' => $name, 'size' => $size, 'nice_size' => f::nice_size($size), 'md5' => md5(file_get_contents($upload_path . $name . "." . $ext))); } return array('failed' => count($_FILES[$options['field']]['tmp_name']) - count($success), 'success' => $success); }
foreach ($page->images() as $i) { if (preg_match('!^' . preg_quote($id) . '!i', $i->name())) { $poster = $i; break; } } $width = $tag->attr('width', c::get('kirbytext.video.width')); $height = $tag->attr('height', c::get('kirbytext.video.height')); $uid = $tag->attr('uid', $id); $name = $tag->attr('name', $id); $html = new Brick('video'); $html->attr('width', $width); $html->attr('height', $height); $html->attr('data-uid', $uid); $html->attr('data-name', $name); $html->attr('preload', 'none'); $html->attr('class', trim('video sublime' . $class)); if ($poster) { $html->attr('poster', $poster->url()); } foreach ($videos as $video) { $source = new Brick('source'); $source->attr('src', $video->url()); $source->attr('type', $video->mime()); if (f::extension($video->name()) == 'hd') { $source->attr('data-quality', 'hd'); } $html->append($source); } return $html; });
protected function templates() { $templates = ['all']; foreach (dir::read($this->dir() . '/site/blueprints') as $file) { if (!in_array(f::extension($file), ['php', 'yml', 'yaml'])) { continue; } $name = f::name($file); if (!in_array($name, ['error', 'site', 'home'])) { $templates[] = $name; } } return $templates; }
function init($page) { foreach ($page->rawfiles as $key => $file) { // skip invisible files if (preg_match('!^\\.!', $file)) { continue; } $info = array('name' => f::name($file), 'filename' => $file, 'extension' => f::extension($file), 'root' => $page->root . '/' . $file, 'uri' => $page->diruri . '/' . $file, 'parent' => $this, 'modified' => @filectime($page->root . '/' . $file)); switch ($info['extension']) { case 'jpg': case 'jpeg': case 'gif': case 'png': $info['type'] = 'image'; $class = 'image'; break; case 'pdf': case 'doc': case 'xls': case 'ppt': $info['type'] = 'document'; $class = 'file'; break; case 'mov': case 'avi': case 'ogg': case 'ogv': case 'webm': case 'flv': case 'swf': case 'mp4': $info['type'] = 'video'; $class = 'video'; break; case 'mp3': $info['type'] = 'sound'; $class = 'file'; break; case c::get('content.file.extension', 'txt'): $info['type'] = 'content'; $class = 'variables'; break; default: $info['type'] = 'other'; $class = 'file'; } $this->{$file} = new $class($info); } $this->dispatchImages(); $this->dispatchContent(); }
/** * Loads all available plugins for the site * * @return array */ public function plugins() { // check for a cached plugins array if (!is_null($this->plugins)) { return $this->plugins; } // get the plugins root $root = $this->roots->plugins(); // start the plugin registry $this->plugins = array(); // check for an existing plugins dir if (!is_dir($root)) { return $this->plugins; } foreach (array_diff(scandir($root), array('.', '..')) as $file) { if (is_dir($root . DS . $file)) { $this->plugin($file, 'dir'); } else { if (f::extension($file) == 'php') { $this->plugin(f::name($file), 'file'); } } } return $this->plugins; }
/** * Modified inventory fetcher * * @return array */ public function inventory() { $inventory = parent::inventory(); $defaultLang = $this->site->defaultLanguage->code; $expression = '!(.*?)(\\.(' . implode('|', $this->site->languages->codes()) . ')|)\\.' . $this->kirby->options['content.file.extension'] . '$!i'; foreach ($inventory['meta'] as $key => $meta) { $inventory['meta'][$key] = array($defaultLang => $meta); } foreach ($inventory['content'] as $key => $content) { preg_match($expression, $content, $match); $file = $match[1]; $lang = isset($match[3]) ? $match[3] : null; if (in_array($file, $inventory['files'])) { $inventory['meta'][$file][$lang] = $content; } else { if (is_null($lang)) { $lang = f::extension($file); if (empty($lang)) { $lang = $defaultLang; } } $inventory['content'][$lang] = $content; } unset($inventory['content'][$key]); } // try to fill the default language with something else if (!isset($inventory['content'][$defaultLang])) { $inventory['content'][$defaultLang] = a::first($inventory['content']); } return $inventory; }
/** * Generates a new filename for a given name * and makes sure to handle badly given extensions correctly * * @param string $name * @return string */ public function createNewFilename($name, $safeName = true) { $name = basename($safeName ? f::safeName($name) : $name); $ext = f::extension($name); // remove possible extensions if (in_array($ext, f::extensions())) { $name = f::name($name); } return trim($name . '.' . $this->extension(), '.'); }
static function stillHasDefaultAccount() { $file = c::get('root.site') . '/' . c::get('panel.folder') . '/accounts/admin.php'; if (file_exists($file)) { return true; } $dir = c::get('root.site') . '/' . c::get('panel.folder') . '/accounts'; $files = dir::read($dir); $default = array('username' => 'admin', 'password' => 'adminpassword', 'language' => 'en'); foreach ($files as $file) { if (f::extension($file) != 'php') { continue; } $username = f::name($file); $user = user::load($username); $diff = array_diff($user, $default); if (empty($diff)) { return true; } } return false; }
/** * Loads all available plugins for the site * * @return array */ protected static function plugins() { // check for a cached plugins array if (!is_null(static::$plugins)) { return static::$plugins; } // check for an existing plugins dir if (!is_dir(c::$data['root.plugins'])) { return static::$plugins = array(); } foreach (array_diff(scandir(c::$data['root.plugins']), array('.', '..')) as $file) { if (is_dir(c::$data['root.plugins'] . DS . $file)) { static::plugin($file, 'dir'); } else { if (f::extension($file) == 'php') { static::plugin(f::name($file), 'file'); } } } return static::$plugins; }
/** * Returns the mime type of a file * * @param string $file * @return mixed */ public static function mime($file) { // stop for invalid files if (!file_exists($file)) { return null; } // Fileinfo is prefered if available if (function_exists('finfo_file')) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $file); finfo_close($finfo); return $mime; } // for older versions with mime_content_type go for that. if (function_exists('mime_content_type') && ($mime = @mime_content_type($file) !== false)) { return $mime; } // shell check try { $mime = system::execute('file', [$file, '-z', '-b', '--mime'], 'output'); $mime = trim(str::split($mime, ';')[0]); if (f::mimeToExtension($mime)) { return $mime; } } catch (Exception $e) { // no mime type detectable with shell $mime = null; } // Mime Sniffing $reader = new MimeReader($file); $mime = $reader->get_type(); if (!empty($mime) && f::mimeToExtension($mime)) { return $mime; } // guess the matching mime type by extension return f::extensionToMime(f::extension($file)); }
/** * Autoloads all page models */ public function models() { if (!is_dir($this->roots()->models())) { return false; } $root = $this->roots()->models(); $files = dir::read($root); $load = array(); foreach ($files as $file) { if (f::extension($file) != 'php') { continue; } $name = f::name($file); $classname = str_replace(array('.', '-', '_'), '', $name . 'page'); $load[$classname] = $root . DS . $file; // register the model page::$models[$name] = $classname; } // start the autoloader if (!empty($load)) { load($load); } }
public function languages() { $languages = new Collection(); $root = $this->roots()->languages(); foreach (dir::read($root) as $file) { // skip invalid language files if (f::extension($file) != 'php') { continue; } // fetch all strings from the language file $strings = (require $root . DS . $file); // skip invalid language files if (!is_array($strings)) { continue; } // create the language object $language = new Obj($strings); $language->code = str_replace('.php', '', $file); $languages->set($language->code, $language); } return $languages; }
/** * Generate the required wizard assets. * * @param string $file Requested asset file. * @return boolean */ public function assets($file) { $root = $this->hub()->finder()->assets(); $path = $root . DS . $file; if ('css/styles.css' === $file) { $this->styles(); } else { if ('js/scripts.css' === $file) { $this->scripts(); } else { if (file_exists($path) && 'woff' === f::extension($path)) { header('Content-type: application/font-woff'); echo f::read($path); exit; } } } return false; }
public function testExtension() { $this->assertEquals('php', f::extension($this->contentFile)); $this->assertEquals('content.txt', f::extension($this->contentFile, 'txt')); }