random() static public method

Generates a random string
static public random ( integer $length = false ) : string
$length integer The length of the random string
return string
Ejemplo n.º 1
0
 public function add($data)
 {
     $data['id'] = str::random(32);
     $this->data[$data['id']] = $data;
     $this->save();
     return $data['id'];
 }
Ejemplo n.º 2
0
 public function create($uid, $template, $content = array())
 {
     if (empty($template)) {
         throw new Exception(l('pages.add.error.template'));
     }
     $uid = empty($uid) ? str::random(32) : $uid;
     $blueprint = new Blueprint($template);
     $data = array();
     foreach ($blueprint->fields(null) as $key => $field) {
         $data[$key] = $field->default();
     }
     $data = array_merge($data, $content);
     // create the new page and convert it to a page model
     $page = new Page($this->page, parent::create($uid, $template, $data)->dirname());
     if (!$page) {
         throw new Exception(l('pages.add.error.create'));
     }
     kirby()->trigger('panel.page.create', $page);
     // subpage builder
     foreach ((array) $page->blueprint()->pages()->build() as $build) {
         $missing = a::missing($build, array('title', 'template', 'uid'));
         if (!empty($missing)) {
             continue;
         }
         $subpage = $page->children()->create($build['uid'], $build['template'], array('title' => $build['title']));
         if (isset($build['num'])) {
             $subpage->sort($build['num']);
         }
     }
     return $page;
 }
Ejemplo n.º 3
0
 public function id()
 {
     $id = str::random(32);
     while ($this->index()->where('id', '=', $id)->one()) {
         $id = str::random(32);
     }
     return $id;
 }
Ejemplo n.º 4
0
 public function __construct()
 {
     $this->data = new Collection();
     foreach (range(0, 99) as $key => $item) {
         $this->data->set($key, str::random());
     }
     $this->url = 'http://getkirby.com';
     $this->pages = $this->data->paginate(10, array('url' => $this->url));
     $this->pagination = $this->pages->pagination();
 }
Ejemplo n.º 5
0
 protected static function kill()
 {
     self::$user = null;
     // overwrite the token
     $token = str::random();
     // the cookie is valid for 24 hours
     cookie::set('authFrontend', $token, 60 * 60 * 24);
     // restart the session
     s::restart();
 }
Ejemplo n.º 6
0
 public function testSize()
 {
     dir::make($this->tmpDir);
     f::write($this->tmpDir . DS . 'testfile-1.txt', str::random(5));
     f::write($this->tmpDir . DS . 'testfile-2.txt', str::random(5));
     f::write($this->tmpDir . DS . 'testfile-3.txt', str::random(5));
     $this->assertEquals(15, dir::size($this->tmpDir));
     $this->assertEquals('15 b', dir::niceSize($this->tmpDir));
     dir::remove($this->tmpDir);
 }
Ejemplo n.º 7
0
/**
 * Checks / returns a csrf token
 *
 * @param string $check Pass a token here to compare it to the one in the session
 * @return mixed Either the token or a boolean check result
 */
function csrf($check = null)
{
    // make sure a session is started
    s::start();
    if (is_null($check)) {
        $token = str::random(64);
        s::set('csrf', $token);
        return $token;
    }
    return $check === s::get('csrf') ? true : false;
}
Ejemplo n.º 8
0
 /**
  * Finds a new unique token, using a loop to make sure that the token does
  * not already exist in the database. This could potentially become an
  * infinite loop, but the chances of that happening are very unlikely.
  *
  * @return  string
  */
 protected function create_token()
 {
     while (true) {
         // Create a random token
         $token = str::random('alnum', 32);
         // Make sure the token does not already exist
         self::db()->use_master(YES);
         if (self::db()->select('user_token_id')->where('user_token_token', $token)->get($this->table_name)->count() === 0) {
             // A unique token has been found
             return $token;
         }
     }
 }
Ejemplo n.º 9
0
 public function testRandom()
 {
     // choose a high length for a high probability of occurrence of a character of any type
     $length = 200;
     $this->assertRegexp("/^[[:alnum:]]+\$/", str::random());
     $this->assertInternalType('string', str::random());
     $this->assertEquals($length, strlen(str::random($length)));
     $this->assertRegexp("/^[[:alpha:]]+\$/", str::random($length, 'alpha'));
     $this->assertRegexp("/^[[:upper:]]+\$/", str::random($length, 'alphaUpper'));
     $this->assertRegexp("/^[[:lower:]]+\$/", str::random($length, 'alphaLower'));
     $this->assertRegexp("/^[[:digit:]]+\$/", str::random($length, 'num'));
     $this->assertFalse(str::random($length, 'something invalid'));
 }
Ejemplo n.º 10
0
 public function csrf()
 {
     if (!is_null($this->csrf)) {
         return $this->csrf;
     }
     // see if there's a token in the session
     $token = s::get('csrf');
     // create a new csrf token if not available yet
     if (str::length($token) !== 32) {
         $token = str::random(32);
     }
     // store the new token in the session
     s::set('csrf', $token);
     // create a new csrf token
     return $this->csrf = $token;
 }
Ejemplo n.º 11
0
 public function generateToken()
 {
     return sha1($this->username() . str::random(32) . time());
 }
Ejemplo n.º 12
0
 function logout()
 {
     // overwrite the token
     $token = str::random();
     // the cookie is valid for 24 hours
     cookie::set('auth', $token, 60 * 60 * 24);
     // restart the session
     s::restart();
     // go to the homepage
     go(url());
 }
Ejemplo n.º 13
0
 /**
  * Generates a salted hash for a plaintext password
  *
  * @param string $plaintext
  * @return string
  */
 public static function hash($plaintext)
 {
     $salt = substr(str_replace('+', '.', base64_encode(sha1(str::random(), true))), 0, 22);
     return crypt($plaintext, '$2a$10$' . $salt);
 }
Ejemplo n.º 14
0
 public function generateKey()
 {
     return str::random(64);
 }
Ejemplo n.º 15
0
 /**
  * Generates a new Captcha challenge.
  *
  * @return  string  the challenge answer
  */
 public function generate_challenge()
 {
     // Complexity setting is used as character count
     return str::random('distinct', max(1, ceil(Captcha::$config['complexity'] / 1.5)));
 }
Ejemplo n.º 16
0
 /**
  * Generates a new token for this form and session.
  */
 private function generateToken()
 {
     $this->token = str::random(SendForm::TOKEN_LENGTH);
     s::set($this->id, $this->token);
 }
Ejemplo n.º 17
0
 public static function generateSha1Hash($value)
 {
     return sha1($value . str::random(32) . time());
 }