length() static public method

An UTF-8 safe version of strlen()
static public length ( string $str ) : string
$str string
return string
Example #1
0
 public function validate()
 {
     // check for a valid library object
     if (!is_a($this->library, 'Library')) {
         throw new Exception('The library object is invalid');
     }
     // check for all required fields
     foreach (static::$required as $field) {
         if (empty($this->data[$field])) {
             throw new Exception('Missing required field: ' . $field);
         }
     }
     // id validation
     if (!is_string($this->data['id']) or !v::alphanum($this->data['id']) or str::length($this->data['id']) !== 32) {
         throw new Exception('Invalid id');
     }
     // type validation
     if (!is_string($this->data['type']) or !v::between($this->data['type'], 2, 32)) {
         throw new Exception('Invalid type');
     }
     // status validation
     if (!in_array($this->data['status'], static::$statuses)) {
         throw new Exception('Invalid status: ' . $this->data['status']);
     }
     // check for invalid updated timestamp
     if (!is_int($this->data['updated']) or !v::between(date('Y', $this->data['updated']), 1980, 2500)) {
         throw new Exception('Invalid updated timestamp');
     }
     // check for invalid created timestamp
     if (!is_int($this->data['created']) or !v::between(date('Y', $this->data['created']), 1980, 2500) or $this->data['created'] > time()) {
         throw new Exception('Invalid created timestamp');
     }
 }
Example #2
0
 public function update($username)
 {
     $user = $this->user($username);
     if (!$user) {
         return response::error(l('users.edit.error.missing'));
     } else {
         if (!site()->user()->isAdmin() and !$user->isCurrent()) {
             return response::error('You are not allowed to edit this user');
         }
         $form = $this->form($user);
         $data = $form->toArray();
         if (str::length($data['password']) > 0) {
             if ($data['password'] !== $data['passwordconfirmation']) {
                 return response::error(l('users.form.error.password.confirm'));
             }
         } else {
             unset($data['password']);
         }
         unset($data['passwordconfirmation']);
         if ($user->update($data)) {
             return response::success('success');
         } else {
             return response::error(l('users.form.error.update'));
         }
     }
 }
Example #3
0
 public function update($data = array())
 {
     if (!panel()->user()->isAdmin() and !$this->isCurrent()) {
         throw new Exception(l('users.form.error.update.rights'));
     }
     // users which are not an admin cannot change their role
     if (!panel()->user()->isAdmin()) {
         unset($data['role']);
     }
     if (str::length(a::get($data, 'password')) > 0) {
         if (a::get($data, 'password') !== a::get($data, 'passwordconfirmation')) {
             throw new Exception(l('users.form.error.password.confirm'));
         }
     } else {
         unset($data['password']);
     }
     unset($data['passwordconfirmation']);
     if ($this->isLastAdmin() and a::get($data, 'role') !== 'admin') {
         // check the number of left admins to not convert the last one
         throw new Exception(l('user.error.lastadmin'));
     }
     parent::update($data);
     // flush the cache in case if the user data is
     // used somewhere on the site (i.e. for profiles)
     kirby()->cache()->flush();
     kirby()->trigger('panel.user.update', $this);
     return $this;
 }
Example #4
0
 /**
  * Return the plural of the input string if quantity is larger than one.
  *
  * NOTE: This function will not handle any special cases.
  *
  * @see     http://www.oxforddictionaries.com/words/plurals-of-nouns
  *
  * @param   string   $singular  Singular noun
  * @param   integer  $quantity  Quantity
  * @param   string   $plural    Plural form
  *
  * @return  string
  */
 public static function plural($singular, $quantity = 2, $plural = null)
 {
     if ($quantity <= 1 || empty($singular)) {
         return $singular;
     }
     if (!is_null($plural)) {
         return $plural;
     }
     $last = str::lower($singular[str::length($singular) - 1]);
     $lastTwo = str::lower(substr($singular, 0, -2));
     if ('y' === $last) {
         return substr($singular, 0, -1) . 'ies';
     } else {
         if ('f' === $last || 'fe' === $lastTwo) {
             return $singular . 'ves';
         } else {
             if (in_array($last, array('s', 'x', 'z'))) {
                 return substr($singular, 0, -1) . 'es';
             } else {
                 if (in_array($lastTwo, array('ch', 'sh'))) {
                     return substr($singular, 0, -2) . 'es';
                 } else {
                     return $singular . 's';
                 }
             }
         }
     }
 }
Example #5
0
 /**
  * Constructor
  * 
  * @param array $data
  */
 public function __construct($data = array())
 {
     foreach ($data as $key => $val) {
         if (!is_string($key) || str::length($key) === 0) {
             continue;
         }
         $this->{$key} = $val;
     }
 }
Example #6
0
 public function counter()
 {
     if (!$this->minLength() && !$this->maxLength() || $this->readonly()) {
         return null;
     }
     $counter = new Brick('div');
     $counter->addClass('field-counter marginalia text');
     $length = str::length(trim($this->value()));
     if ($this->outsideRange($length)) {
         $counter->addClass('outside-range');
     }
     $counter->data('field', 'counter');
     $counter->html($length . ($this->maxLength() ? '/' . $this->maxLength() : ''));
     return $counter;
 }
Example #7
0
 public function run()
 {
     if (empty($this->query) or str::length($this->query) <= 1) {
         return false;
     }
     $data = $this->data();
     foreach ($data['pages'] as $page) {
         if (str::contains($page['title'], $this->query) or str::contains($page['uri'], $this->query)) {
             $this->pages->append($page['uri'], $page);
         }
     }
     foreach ($data['users'] as $user) {
         if (str::contains($user['username'], $this->query) or str::contains($user['email'], $this->query)) {
             $this->users->append($user['username'], $user);
         }
     }
     $this->pages = $this->pages->limit(5);
     $this->users = $this->users->limit(5);
 }
 public function update($data = array())
 {
     if (!panel()->user()->isAdmin() and !$this->isCurrent()) {
         throw new Exception(l('users.form.error.update.rights'));
     }
     if (str::length(a::get($data, 'password')) > 0) {
         if (a::get($data, 'password') !== a::get($data, 'passwordconfirmation')) {
             throw new Exception(l('users.form.error.password.confirm'));
         }
     } else {
         unset($data['password']);
     }
     unset($data['passwordconfirmation']);
     if ($this->isLastAdmin() and a::get($data, 'role') !== 'admin') {
         // check the number of left admins to not convert the last one
         throw new Exception(l('user.error.lastadmin'));
     }
     parent::update($data);
     kirby()->trigger('panel.user.update', $this);
     return $this;
 }
Example #9
0
 public function update($username)
 {
     $user = $this->user($username);
     if (!$user) {
         return response::error(l('users.edit.error.missing'));
     } else {
         $form = $this->form($user);
         $data = $form->toArray();
         if (str::length($data['password']) > 0) {
             if ($data['password'] !== $data['passwordConfirmation']) {
                 return response::error(l('users.form.error.password.confirm'));
             }
         } else {
             unset($data['password']);
         }
         unset($data['passwordConfirmation']);
         if ($user->update($data)) {
             return response::success('success');
         } else {
             return response::error(l('users.form.error.update'));
         }
     }
 }
Example #10
0
 public function send()
 {
     if (c::get('email.disabled')) {
         return array('status' => 'error', 'msg' => l::get('email.disabled', 'Email has been disabled'));
     }
     if (!v::email($this->extractAddress($this->options['from']))) {
         return array('status' => 'error', 'msg' => l::get('email.error.invalid.sender', 'Invalid sender'));
     }
     if (!v::email($this->extractAddress($this->options['to']))) {
         return array('status' => 'error', 'msg' => l::get('email.error.invalid.recipient', 'Invalid recipient'));
     }
     if (!v::email($this->extractAddress($this->options['replyto']))) {
         return array('status' => 'error', 'msg' => l::get('email.error.invalid.replyto', 'Invalid Reply-To Address'));
     }
     if (str::length($this->options['subject']) == 0) {
         return array('status' => 'error', 'msg' => l::get('email.error.invalid.subject', 'The subject is missing'));
     }
     $method = 'sendWith' . str::ucfirst($this->options['use']);
     if (!method_exists(__CLASS__, $method)) {
         return array('status' => 'error', 'msg' => l::get('email.error.invalid.mailer', 'This email service is not supported'));
     }
     return $this->{$method}();
 }
Example #11
0
 * Shortens the field value by the given length
 * @param Field $field The calling Kirby Field instance
 * @param integer $length The desired string length
 * @param string $rep The attached ellipsis character if the string is longer
 * @return string
 */
field::$methods['short'] = function ($field, $length, $rep = '…') {
    return str::short($field->value, $length, $rep);
};
/**
 * Returns the string length of the field value
 * @param Field $field The calling Kirby Field instance
 * @return integer
 */
field::$methods['length'] = function ($field) {
    return str::length($field->value);
};
/**
 * Returns the word count for the field value
 * @param Field $field The calling Kirby Field instance
 * @return integer
 */
field::$methods['words'] = function ($field) {
    return str_word_count(strip_tags($field->value));
};
/**
 * Splits the field value by the given separator
 * @param Field $field The calling Kirby Field instance
 * @param string $separator The string to split the field value by
 * @return array
 */
Example #12
0
 public function testLength()
 {
     $this->assertEquals(20, str::length($this->sample));
 }
Example #13
0
 static function editFile()
 {
     global $panel, $page;
     $filename = get('filename');
     $newname = str::urlify(get('newname'));
     $file = $page->files()->find($filename);
     if (!$file) {
         return array('status' => 'error', 'msg' => l::get('files.edit.errors.notfound'));
     }
     if (str::length($newname) < 1) {
         return array('status' => 'error', 'msg' => l::get('files.edit.errors.filename'));
     }
     $newfilename = $newname . '.' . $file->extension();
     if ($newfilename != $file->filename()) {
         $newroot = dirname($file->root()) . '/' . $newfilename;
         if (file_exists($newroot)) {
             return array('status' => 'error', 'msg' => l::get('files.edit.errors.exists'));
         }
         if (!f::move($file->root(), $newroot)) {
             return array('status' => 'error', 'msg' => l::get('files.edit.errors.permissions'));
         }
         // delete the old meta file
         $oldmeta = dirname($file->root()) . '/' . $file->filename() . '.txt';
         f::remove($oldmeta);
     }
     $destination = dirname($file->root()) . '/' . $newfilename . '.txt';
     $updateInfo = self::updateFileinfo($file, $destination);
     if (error($updateInfo)) {
         return $updateInfo;
     }
     self::killCache();
     return array('status' => 'success', 'msg' => l::get('files.edit.success'));
 }
Example #14
0
 public function license()
 {
     $key = c::get('license');
     $type = 'trial';
     /**
      * Hey stranger, 
      * 
      * So this is the mysterious place where the panel checks for 
      * valid licenses. As you can see, this is not reporting
      * back to any server and the license keys are rather simple to 
      * hack. If you really feel like removing the warning in the panel
      * or tricking Kirby into believing you bought a valid license even 
      * if you didn't, go for it! But remember that literally thousands of 
      * hours of work have gone into Kirby in order to make your 
      * life as a developer, designer, publisher, etc. easier. If this 
      * doesn't mean anything to you, you are probably a lost case anyway. 
      * 
      * Have a great day! 
      * 
      * Bastian
      */
     if (str::startsWith($key, 'K2-PRO') and str::length($key) == 39) {
         $type = 'Kirby 2 Professional';
     } else {
         if (str::startsWith($key, 'K2-PERSONAL') and str::length($key) == 44) {
             $type = 'Kirby 2 Personal';
         } else {
             if (str::startsWith($key, 'MD-') and str::length($key) == 35) {
                 $type = 'Kirby 1';
             } else {
                 if (str::startsWith($key, 'BETA') and str::length($key) == 9) {
                     $type = 'Kirby 1';
                 } else {
                     if (str::length($key) == 32) {
                         $type = 'Kirby 1';
                     } else {
                         $key = null;
                     }
                 }
             }
         }
     }
     $localhosts = array('::1', '127.0.01', '0.0.0.0');
     return new Obj(array('key' => $key, 'local' => in_array(server::get('SERVER_ADDR'), $localhosts) or server::get('SERVER_NAME') == 'localhost', 'type' => $type));
 }
Example #15
0
 /**
  * Load plugin options.
  *
  * @return array
  */
 public function load()
 {
     // Retrieve all plugin options from the configuration starting with a
     // prefix matching the plugin name
     $prefix = $this->namespace . '.';
     $keys = array_keys(c::$data);
     $keys = array_filter($keys, function ($key) use($prefix) {
         return str::startsWith($key, $prefix);
     });
     // Remove prefix and collect data
     $options = array();
     foreach ($keys as $key) {
         $option = str::substr($key, str::length($prefix));
         $options[$option] = c::$data[$key];
     }
     // Merge plugin settings with defaults
     $defaults = $this->defaults();
     if (is_array($defaults) && !empty($defaults)) {
         $options = array_merge($defaults, $options);
     }
     return $options;
 }
Example #16
0
 public function license()
 {
     $key = c::get('license');
     $type = 'trial';
     if (str::startsWith($key, 'K2-PRO') and str::length($key) == 39) {
         $type = 'Kirby 2 Professional';
     } else {
         if (str::startsWith($key, 'K2-PERSONAL') and str::length($key) == 44) {
             $type = 'Kirby 2 Personal';
         } else {
             if (str::length($key) == 32) {
                 $type = 'Kirby 1';
             } else {
                 $key = null;
             }
         }
     }
     $localhosts = array('::1', '127.0.01', '0.0.0.0');
     return new Obj(array('key' => $key, 'local' => in_array(server::get('SERVER_ADDR'), $localhosts), 'type' => $type));
 }
Example #17
0
 static function editFile()
 {
     global $panel, $page;
     $filename = get('filename');
     $newname = str::urlify(get('newname'));
     $file = $page->files()->find($filename);
     if (!$file) {
         return array('status' => 'error', 'msg' => l::get('files.edit.errors.notfound'));
     }
     if (str::length($newname) < 1) {
         return array('status' => 'error', 'msg' => l::get('files.edit.errors.filename'));
     }
     $newfilename = $newname . '.' . $file->extension();
     if ($newfilename != $file->filename()) {
         $newroot = dirname($file->root()) . '/' . $newfilename;
         if (file_exists($newroot)) {
             return array('status' => 'error', 'msg' => l::get('files.edit.errors.exists'));
         }
         if (!f::move($file->root(), $newroot)) {
             return array('status' => 'error', 'msg' => l::get('files.edit.errors.permissions'));
         }
         // delete the old meta file
         if (c::get('lang.support')) {
             // make sure to remove the meta file without language extension
             $invalidfile = dirname($file->root()) . '/' . $file->filename() . '.' . c::get('content.file.extension', 'txt');
             f::remove($invalidfile);
             // remove the translated meta file
             $oldmeta = dirname($file->root()) . '/' . $file->filename() . '.' . c::get('lang.current') . '.' . c::get('content.file.extension', 'txt');
         } else {
             $oldmeta = dirname($file->root()) . '/' . $file->filename() . '.' . c::get('content.file.extension', 'txt');
         }
         f::remove($oldmeta);
     }
     if (c::get('lang.support')) {
         // delete the untranslated file
         $delete = dirname($file->root()) . '/' . $newfilename . '.' . c::get('content.file.extension', 'txt');
         f::remove($delete);
         // set the translated file
         $destination = dirname($file->root()) . '/' . $newfilename . '.' . c::get('lang.current') . '.' . c::get('content.file.extension', 'txt');
     } else {
         $destination = dirname($file->root()) . '/' . $newfilename . '.' . c::get('content.file.extension', 'txt');
     }
     $updateInfo = self::updateFileinfo($file, $destination);
     if (error($updateInfo)) {
         return $updateInfo;
     }
     self::killCache();
     return array('status' => 'success', 'msg' => l::get('files.edit.success'));
 }
Example #18
0
 static function sort($flip = true)
 {
     $visible = get('visible');
     $invisible = get('invisible');
     $errors = array();
     if (empty($visible) && empty($invisible)) {
         return array('status' => 'error', 'msg' => l::get('pages.errors.nosort'));
     }
     if (!empty($visible)) {
         $cnt = count($visible);
         $len = str::length($cnt);
         $num = 0;
         if ($len <= 1) {
             $len = 2;
         }
         if ($flip) {
             $visible = array_reverse($visible);
         }
         foreach ($visible as $uid) {
             $num++;
             $sort = self::changeNum($uid, pad($num, $len));
             if (error($sort)) {
                 $errors[] = $sort;
             }
         }
     }
     if (!empty($invisible)) {
         foreach ($invisible as $uid) {
             $sort = self::removeNum($uid);
             if (error($sort)) {
                 $errors[] = $sort;
             }
         }
     }
     if (!empty($errors)) {
         return array('status' => 'error', 'msg' => l::get('pages.errors.sort'), 'error' => $errors);
     }
     self::killCache();
     return array('status' => 'success', 'msg' => l::get('pages.sorted'));
 }
Example #19
0
/**
 * Determines the size/length of numbers, strings, arrays and files
 *
 * @param mixed $value
 * @return int
 */
function size($value)
{
    if (is_numeric($value)) {
        return $value;
    }
    if (is_string($value)) {
        return str::length(trim($value));
    }
    if (is_array($value)) {
        return count($value);
    }
    if (f::exists($value)) {
        return f::size($value) / 1024;
    }
}
Example #20
0
File: v.php Project: nsteiner/kdoc
}, 'in' => function ($value, $in) {
    return in_array($value, $in, true);
}, 'integer' => function ($value) {
    return filter_var($value, FILTER_VALIDATE_INT) !== false;
}, 'ip' => function ($value) {
    return filter_var($value, FILTER_VALIDATE_IP) !== false;
}, 'match' => function ($value, $preg) {
    return preg_match($preg, $value) > 0;
}, 'max' => function ($value, $max) {
    return size($value) <= $max;
}, 'min' => function ($value, $min) {
    return size($value) >= $min;
}, 'maxLength' => function ($value, $max) {
    return str::length(trim($value)) <= $max;
}, 'minLength' => function ($value, $min) {
    return str::length(trim($value)) >= $min;
}, 'maxWords' => function ($value, $max) {
    return v::max(explode(' ', $value), $max);
}, 'minWords' => function ($value, $min) {
    return v::min(explode(' ', $value), $min);
}, 'notIn' => function ($value, $notIn) {
    return !v::in($value, $notIn);
}, 'num' => function ($value) {
    return is_numeric($value);
}, 'required' => function ($key, $array) {
    return !empty($array[$key]);
}, 'same' => function ($value, $other) {
    return $value === $other;
}, 'size' => function ($value, $size) {
    return size($value) == $size;
}, 'url' => function ($value) {
Example #21
0
 /** 
  * Core method to create a new validator
  * 
  * @param  string  $string
  * @param  array   $options
  * @return boolean
  */
 static function string($string, $options)
 {
     $format = null;
     $min_length = $max_length = 0;
     if (is_array($options)) {
         extract($options);
     }
     if ($format && !preg_match('/^[' . $format . ']*$/is', $string)) {
         return false;
     }
     if ($min_length && str::length($string) < $min_length) {
         return false;
     }
     if ($max_length && str::length($string) > $max_length) {
         return false;
     }
     return true;
 }
}
foreach ($response as $post) {
    if ($post['post_status'] != 'publish') {
        continue;
    }
    $posts[] = array('title' => $post['title'], 'text' => $post['description'], 'date' => $post['date_created_gmt']->getTimestamp(), 'slug' => $post['wp_slug'], 'tags' => $post['mt_keywords'], 'cats' => implode(',', $post['categories']));
}
if (empty($posts)) {
    puterror('No articles have been found');
}
function pad($number, $n)
{
    return str_pad((int) $number, $n, "0", STR_PAD_LEFT);
}
$cnt = count($posts);
$len = str::length($cnt);
if ($len <= 1) {
    $len = 2;
}
$n = 0;
$skipped = array();
$errors = array();
foreach (array_reverse($posts) as $post) {
    $n++;
    $output = array();
    if (empty($post['title']) || empty($post['slug'])) {
        $errors[] = $post;
        continue;
    }
    $output[] = 'title: ' . $post['title'];
    $output[] = 'date: ' . date($dateformat, $post['date']);
Example #23
0
 /**
  * Escape character for CSS
  * 
  * Callback function for preg_replace_callback() that applies CSS
  * escaping to all matches.
  * 
  * @param  array  $matches
  * @return string
  */
 protected static function escapeCSSChar($matches)
 {
     $char = $matches[0];
     if (str::length($char) == 1) {
         $ord = ord($char);
     } else {
         $char = static::convertEncoding($char);
         $ord = hexdec(bin2hex($char));
     }
     return sprintf('\\%X ', $ord);
 }
Example #24
0
 /**
  * Returns the beginning of a string until the given character
  * 
  * @param string $string
  * @param string $char
  * @return string
  */
 public static function until($string, $char)
 {
     $pos = strpos($string, $char);
     return static::substr($string, 0, $pos + str::length($char));
 }