name() static public method

Extracts the name from a file path or filename without extension
static public name ( $name, boolean $remove_path = false ) : string
$remove_path boolean remove the path from the name
return string
Example #1
0
 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;
 }
Example #2
0
 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;
 }
Example #3
0
 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));
 }
Example #4
0
 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);
     }
 }
Example #5
0
 public static function all()
 {
     $files = dir::read(static::$root);
     $result = array();
     $home = site()->homePage()->uid();
     $error = site()->errorPage()->uid();
     foreach ($files as $file) {
         $name = f::name($file);
         if ($name != 'site' and $name != $home and $name != $error) {
             $result[] = $name;
         }
     }
     return $result;
 }
 public static function all()
 {
     $files = dir::read(static::$root);
     $result = array();
     $home = kirby()->option('home', 'home');
     $error = kirby()->option('error', 'error');
     foreach ($files as $file) {
         $name = f::name($file);
         if ($name != 'site' and $name != $home and $name != $error) {
             $result[] = $name;
         }
     }
     return $result;
 }
Example #7
0
 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));
 }
Example #8
0
 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, 'jpg');
     // 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));
 }
Example #9
0
 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;
 }
Example #10
0
 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();
 }
Example #11
0
 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) {
         $username = f::name($file);
         $user = user::load($username);
         $diff = array_diff($user, $default);
         if (empty($diff)) {
             return true;
         }
     }
     return false;
 }
Example #12
0
 /**
  * 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;
 }
Example #13
0
 static function findTemplates()
 {
     global $settings, $pages, $page, $site;
     $templates = array();
     if (@$settings->pages['template'] && !$site->isHome) {
         $templates[] = $settings->pages['template'];
     } else {
         $files = dir::read(c::get('root.site') . '/templates');
         foreach ($files as $file) {
             $name = f::name($file);
             // check if it is valid or already there.
             if (empty($name) || in_array($name, $templates)) {
                 continue;
             }
             // add it to the list of templates
             $templates[] = $name;
         }
     }
     return $templates;
 }
Example #14
0
 /**
  * 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(), '.');
 }
/**
 * 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);
}
Example #16
0
 protected function selection()
 {
     return array_map(function ($filename) {
         return f::name($filename);
     }, dir::read($this->root()));
 }
$jsHandler = kirby()->option('js.handler');
$kirby->options['css.handler'] = function ($url, $media = false) use($cssHandler, $kirby) {
    if (is_array($url)) {
        $css = array();
        foreach ($url as $u) {
            $css[] = call($kirby->options['css.handler'], $u);
        }
        return implode(PHP_EOL, $css) . PHP_EOL;
    }
    $file = $kirby->roots()->index() . DS . $url;
    if (file_exists($file)) {
        $mod = f::modified($file);
        $url = dirname($url) . '/' . f::name($url) . '.' . $mod . '.css';
    }
    return call($cssHandler, array($url, $media));
};
$kirby->options['js.handler'] = function ($src, $async = false) use($jsHandler, $kirby) {
    if (is_array($src)) {
        $js = array();
        foreach ($src as $s) {
            $js[] = call($kirby->options['js.handler'], $s);
        }
        return implode(PHP_EOL, $js) . PHP_EOL;
    }
    $file = $kirby->roots()->index() . DS . $src;
    if (file_exists($file)) {
        $mod = f::modified($file);
        $src = dirname($src) . '/' . f::name($src) . '.' . $mod . '.js';
    }
    return call($jsHandler, array($src, $async));
};
Example #18
0
 public function testName()
 {
     $this->assertEquals('content', f::name($this->contentFile));
 }
Example #19
0
        static $name = 10;
        static function name()
        {
            return 11;
        }
    }
    // fully qualified names
    echo \foo\bar\qux\c::$property;
    echo \foo\bar\qux\c::CONSTANT;
    echo \foo\bar\qux\c::method();
    use foo\bar;
    // qualified names
    echo bar\qux\c::$property;
    echo bar\qux\c::CONSTANT;
    echo bar\qux\c::method();
    echo bar\baz\c::$property;
    echo bar\baz\c::CONSTANT;
    echo bar\baz\c::method();
    // aliases
    use foo\bar\qux\c;
    echo c::$property;
    echo c::CONSTANT;
    echo c::method();
    // not-qualified names
    echo d::$property;
    echo d::CONSTANT;
    echo d::method();
    // property vs method with the same name
    echo f::$name;
    echo f::name();
}
Example #20
0
File: file.php Project: smongey/ck
 /**
  * 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)
 {
     $name = f::safeName($name);
     $ext = $this->extension();
     // remove possible extensions
     if (preg_match('!\\.[a-z0-9]{2,4}$!i', $name)) {
         $name = f::name($name);
     }
     return trim($name . '.' . $ext, '.');
 }
Example #21
0
 /**
  * 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);
     }
 }
Example #22
0
 /**
  * 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;
 }