The string generated matches [A-Za-z0-9_-]+ and is transparent to URL-encoding.
public generateRandomString ( integer $length = 32 ) : string | ||
$length | integer | the length of the key in characters |
Результат | string | the generated random key |
/** * @param int $length * @return string */ public static function generateSecret($length = 20) { $security = new Security(); $full = Base32::encode($security->generateRandomString($length)); return substr($full, 0, $length); }
public function actionMultiple() { $security = new Security(); $randomString = $security->generateRandomString(); $randomKey = $security->generateRandomKey(); return $this->render('multiple', ['randomString' => $randomString, 'randomKey' => $randomKey]); }
public function matchLength($attribute, $min = null, $max = null, $onScenario = Model::SCENARIO_DEFAULT) { $stringValidator = $this->getValidator('yii\\validators\\StringValidator', $attribute, $onScenario); $stringGenerator = new Security(); if (!empty($min)) { $string = $stringGenerator->generateRandomString($min - 1); $this->assertFalse($stringValidator->validate($string)); $string = $stringGenerator->generateRandomString($min); $this->assertTrue($stringValidator->validate($string)); } if (!empty($max)) { $string = $stringGenerator->generateRandomString($max + 1); $this->assertFalse($stringValidator->validate($string)); $string = $stringGenerator->generateRandomString($max); $this->assertTrue($stringValidator->validate($string)); } }
/** * @return mixed */ private static function getQ() { if (empty(self::$q)) { $s = new Security(); self::$q = '!#@' . $s->generateRandomString(5) . '@#!'; } return self::$q; }
/** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new User(); if ($model->load(Yii::$app->request->post())) { $security = new Security(); $model->password = $security->generatePasswordHash(md5($model->password)); $model->auth_key = $security->generateRandomString(64); $model->created_date = time(); if ($model->save()) { return $this->redirect(['view', 'id' => $model->id]); } } else { return $this->render('create', ['model' => $model]); } }
public function actionIndex() { $username = '******'; $db = Yii::$app->db; $command = $db->createCommand('SELECT COUNT(*) FROM {{%user}} WHERE username = :username'); $command->bindValue(':username', $username, PDO::PARAM_STR); $exist = $command->queryScalar(); if (!$exist) { $now = time(); $security = new Security(); $columns = ['type' => User::TYPE_BACKEND, 'username' => $username, 'nickname' => 'admin', 'auth_key' => $security->generateRandomString(), 'password_hash' => $security->generatePasswordHash('admin'), 'password_reset_token' => null, 'email' => '*****@*****.**', 'role' => 10, 'status' => User::STATUS_ACTIVE, 'register_ip' => '::1', 'login_count' => 0, 'last_login_ip' => null, 'last_login_time' => null, 'created_by' => 0, 'created_at' => $now, 'updated_by' => 0, 'updated_at' => $now, 'deleted_by' => null, 'deleted_at' => null]; $db->createCommand()->insert('{{%user}}', $columns)->execute(); } else { echo "'{$username}' is exists.\r\n"; } echo "Done"; }
/** * Generates "remember me" authentication key */ public function generateAuthKey() { if (php_sapi_name() == 'cli') { $security = new Security(); $this->auth_key = $security->generateRandomString(); } else { $this->auth_key = Yii::$app->security->generateRandomString(); } }
/** * */ public function generateActivationKey() { $activate = new Security(); $this->activation_key = strtr($activate->generateRandomString(6), '_-', 'bB'); }
public static function upload($file, $name = null, $dir = self::F_FILES, $slug = null, $data = null, $delete = true) { if (is_null($dir)) { $dir = self::F_FILES; } $dir = trim($dir); $filePath = ''; if (strpos($file, 'http') === 0) { $tmp = Yii::getAlias('@runtime') . DIRECTORY_SEPARATOR . uniqid("fu"); file_put_contents($tmp, file_get_contents($file)); $filePath = $tmp; $name = $name ? $name : basename($file); } elseif (is_string($file)) { $filePath = Yii::getAlias($file); } elseif ($file instanceof UploadedFile) { $filePath = $file->tempName; $name = $name ? $name : $file->name; } $name = $name ? $name : basename($filePath); $sec = new Security(); while (FileUpload::find()->where(["path" => $uniquePath = md5($sec->generateRandomString())])->one()) { } $dirSlug = $dir; if (!is_dir($dir) && !($dir = self::getFolder($dirSlug))) { if (!$dir) { throw new \Exception("Folder for param '{$dirSlug}' is not set"); } else { throw new \Exception("Folder '{$dir}' not found"); } } $fullPath = self::formPath($uniquePath, $dir); if (!FileHelper::createDirectory(dirname($fullPath))) { throw new \Exception("Can't create folder '{" . dirname($fullPath) . "}'"); } if (!file_exists($filePath)) { throw new \Exception('File not loaded or not exist'); } if (is_uploaded_file($filePath)) { if (!move_uploaded_file($filePath, $fullPath)) { throw new \Exception('Unknown upload error'); } } elseif ($delete ? !rename($filePath, $fullPath) : !copy($filePath, $fullPath)) { throw new \Exception('Failed to write file to disk'); } $info = pathinfo($name); $fileUpload = new self(); //Фиск для сессии, при аяксовом запросе if (isset(Yii::$app->session)) { Yii::$app->session->open(); $fileUpload->session = Yii::$app->session->getIsActive() ? Yii::$app->session->getId() : null; Yii::$app->session->close(); } $fileUpload->user_id = CurrentUser::getId(1); $fileUpload->data = !is_null($data) ? json_encode($data) : null; $fileUpload->mime_type = FileHelper::getMimeType($fullPath); $fileUpload->md5 = md5_file($fullPath); $fileUpload->folder = $dirSlug; $fileUpload->path = $uniquePath; $fileUpload->slug = $slug; $fileUpload->size = filesize($fullPath); if (!($extension = strtolower(ArrayHelper::getValue($info, "extension")))) { $extension = ArrayHelper::getValue(FileHelper::getExtensionsByMimeType($fileUpload->mime_type), 0); } $fileUpload->name = basename($name, '.' . $extension); $fileUpload->extension = $extension; if ($fileUpload->save()) { return $fileUpload; } else { $fileUpload->deleteFile(); return null; } }
/** * Generates token with given length * @return string */ public function generate() { $this->security = Instance::ensure($this->security, Security::className()); return $this->security->generateRandomString($this->length); }