예제 #1
0
 public function beforeValidationOnCreate()
 {
     $this->createdAt = time();
     if (!$this->slug) {
         $this->slug = \Phalcon\Text::random(\Phalcon\Text::RANDOM_ALNUM, 8);
     }
 }
예제 #2
0
 public function indexAction()
 {
     // No view needed since this is all backend stuff.
     $this->view->disable();
     // Generate random ids until we find one not in use.
     // This will cause one additional SQL query at minimum when creating a paste.
     do {
         $id = Text::random(Text::RANDOM_ALNUM, rand(5, 13));
     } while (Paste::findFirstByid($id));
     $paste = new Paste();
     $paste->id = $id;
     $paste->content = rtrim($this->request->getPost("content"));
     $paste->lang = $this->request->getPost("lang") == null ? "auto" : $this->request->getPost("lang");
     // No sanitisation needed if we accept anything at all to mean true and nothing to mean false.
     // Also addresses http://stackoverflow.com/a/14067312
     $paste->private = $this->request->getPost("private") == null ? 0 : 1;
     $paste->owner_addr = $this->request->getClientAddress();
     $paste->size_bytes = strlen($paste->content);
     if (!$paste->save()) {
         foreach ($paste->getMessages() as $message) {
             $this->flash->error($message->getMessage());
         }
         return $this->response->redirect();
     }
     return $this->response->redirect($this->url->get("v/{$id}"));
 }
예제 #3
0
 public function registerAction()
 {
     return $this->handleRequest(function () {
         $req = new Request();
         if ($req->isPost()) {
             $post = json_decode($req->getRawBody());
             $a = $this->getUserDocument();
             $user = new $a();
             $user->salt = Text::random(Text::RANDOM_ALNUM);
             $user->password = $this->hash($post->password, $user->salt);
             unset($post->password);
             $post = (array) $post;
             foreach ($post as $key => $value) {
                 $user->{$key} = $value;
             }
             $user->save();
             $this->session->set('user', $user);
         } else {
             if ($req->isOptions()) {
                 return '';
             }
         }
         return $this->jsonOutput($user);
     });
 }
예제 #4
0
 public function beforeValidationOnCreate()
 {
     $this->createdAt = $this->createdAt ? $this->createdAt : time();
     if (!$this->slug) {
         $this->slug = \Phalcon\Text::random(\Phalcon\Text::RANDOM_ALNUM, 8);
     }
     $this->title = \Eva\EvaEngine\Text\Substring::substrCn(strip_tags($this->getContentHtml()), 100);
 }
예제 #5
0
파일: Post.php 프로젝트: skybird/phalcon
 public function beforeValidationOnCreate()
 {
     $this->createdAt = $this->createdAt ? $this->createdAt : time();
     if (!$this->slug) {
         $this->slug = \Phalcon\Text::random(\Phalcon\Text::RANDOM_ALNUM, 8);
     }
     $this->validate(new Uniqueness(array('field' => 'slug')));
 }
예제 #6
0
 /**
  * @param int $length length of the string
  * @param bool|true $strict whether or not the string should contain a symbol
  * @return string
  */
 public static function generateRandomString($length, $strict = true)
 {
     $password = Text::random(Text::RANDOM_ALNUM, $length);
     if (!$strict) {
         return $password;
     }
     //todo may add more symbols later
     $shuffledSymbols = str_shuffle("@#\$%^&*!+-_~");
     return substr($password, 0, strlen($password) - 1) . $shuffledSymbols[0];
 }
예제 #7
0
 /**
  * @operationName("Change API key")
  * @operationDescription("Change API key")
  */
 public function apikeyAction()
 {
     if (!$this->request->isPut()) {
         return $this->showErrorMessageAsJson(405, 'ERR_REQUEST_METHOD_NOT_ALLOW');
     }
     $id = $this->dispatcher->getParam('id');
     try {
         $apikey = Entities\Apikeys::findFirst($id);
         if ($apikey) {
             $apikey->apikey = \Phalcon\Text::random(\Phalcon\Text::RANDOM_ALNUM, 8);
             $apikey->save();
         }
     } catch (\Exception $e) {
         return $this->showExceptionAsJson($e, $apikey->getMessages());
     }
     return $this->response->setJsonContent($apikey);
 }
예제 #8
0
 public function getId()
 {
     if ($this->tokenId) {
         return $this->tokenId;
     }
     $request = $this->getDI()->getRequest();
     $token = TokenStorage::dicoverToken($this->getDI()->getRequest());
     //$token = $request->getHeader('Authorization');
     if ($token) {
         return $this->tokenId = $token;
     } else {
         if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
             $ip = $_SERVER['HTTP_CLIENT_IP'];
         } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
             $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
         } else {
             $ip = $_SERVER['REMOTE_ADDR'];
         }
         //Generate random hash for even same IP
         return $this->tokenId = 'ip' . ip2long($ip) . Text::random(Text::RANDOM_ALNUM, 6);
     }
 }
예제 #9
0
 public function setRequestId()
 {
     $this->requestId = Text::random();
 }
예제 #10
0
파일: Text.php 프로젝트: lisong/cphalcon
 public static function random($type = 0, $length = 8)
 {
     return parent::random($type, $length);
 }
예제 #11
0
파일: Upload.php 프로젝트: skybird/phalcon
 public function uploadByEncodedData($data, $originalName, $mimeType = null)
 {
     if (!($headPos = strpos($data, ','))) {
         throw new Exception\InvalidArgumentException('ERR_FILE_ENCODED_UPLOAD_FORMAT_INCORRECT');
     }
     $fileHead = substr($data, 0, $headPos + 1);
     $fileEncodedData = trim(substr($data, $headPos + 1));
     $data = base64_decode($fileEncodedData);
     $tmpName = Text::random(\Phalcon\Text::RANDOM_ALNUM, 6);
     $tmpPath = $this->getUploadTmpPath();
     $tmp = $tmpPath . '/' . $tmpName;
     $adapter = new \Gaufrette\Adapter\Local($tmpPath);
     $filesystem = new \Gaufrette\Filesystem($adapter);
     $filesystem->write($tmpName, $data);
     $fileSize = filesize($tmp);
     $type = $mimeType;
     $filenameArray = explode(".", $originalName);
     $fileExtension = strtolower(array_pop($filenameArray));
     $originalFileName = implode('.', $filenameArray);
     $fileName = Tag::friendlyTitle($originalFileName);
     if ($fileName == '-') {
         $fileName = Text::random(Text::RANDOM_ALNUM, 6);
     }
     //hash file less then 10M
     if ($fileSize < 1048576 * 10) {
         $fileHash = hash_file('CRC32', $tmp, false);
     }
     if (false === strpos($type, 'image')) {
         $isImage = 0;
     } else {
         $isImage = 1;
     }
     $fileinfo = array('title' => $originalFileName, 'status' => 'published', 'storageAdapter' => 'local', 'originalName' => $originalName, 'fileSize' => $fileSize, 'mimeType' => $type, 'fileExtension' => $fileExtension, 'fileHash' => $fileHash, 'isImage' => $isImage, 'fileName' => $fileName . '.' . $fileExtension, 'createdAt' => time());
     if ($isImage) {
         $image = getimagesize($tmp);
         $fileinfo['imageWidth'] = $image[0];
         $fileinfo['imageHeight'] = $image[1];
     }
     $filesystem = $this->getDI()->getFileSystem();
     $path = md5(time());
     $path = str_split($path, 2);
     $pathlevel = $this->getUploadPathLevel();
     $pathlevel = $pathlevel > 6 ? 6 : $pathlevel;
     $path = array_slice($path, 0, $pathlevel);
     $filePath = implode('/', $path);
     $path = $filePath . '/' . $fileName . '.' . $fileExtension;
     $fileinfo['filePath'] = $filePath;
     $this->assign($fileinfo);
     if ($this->save()) {
         if (!$filesystem->has($path)) {
             if ($filesystem->write($path, file_get_contents($tmp))) {
                 unlink($tmp);
             } else {
                 throw new Exception\IOException('ERR_FILE_MOVE_TO_STORAGE_FAILED');
             }
         } else {
             throw new Exception\ResourceConflictException('ERR_FILE_UPLOAD_BY_CONFLICT_NAME');
         }
     } else {
         throw new Exception\RuntimeException('ERR_FILE_SAVE_TO_DB_FAILED');
     }
     return $this;
 }
예제 #12
0
 public function testRandomNonZero()
 {
     for ($i = 1; $i < 10; $i++) {
         $text = PhText::random(PhText::RANDOM_NOZERO, $i);
         $this->assertEquals(preg_match('/[1-9]+/', $text, $matches), 1);
         $this->assertEquals($matches[0], $text);
         $this->assertEquals(strlen($text), $i);
     }
 }
예제 #13
0
 public function testRandomDefaultLength()
 {
     $generated = \Phalcon\Text::random(\Phalcon\Text::RANDOM_NOZERO);
     $this->assertEquals(strlen($generated), 8);
 }
 /**
  * issue a generic token
  * replace with your own logic
  *
  * @return multitype:boolean NULL
  */
 public function generateToken()
 {
     return \Phalcon\Text::random(\Phalcon\Text::RANDOM_ALNUM, 64);
 }
예제 #15
0
파일: Apikey.php 프로젝트: skybird/phalcon
 public function beforeValidationOnCreate()
 {
     $this->createdAt = time();
     $this->apikey = \Phalcon\Text::random(\Phalcon\Text::RANDOM_ALNUM, 8);
 }
 /**
  * issue a generic token
  * replace with your own logic
  *
  * @return string
  */
 public function generateToken()
 {
     return Text::random(Text::RANDOM_ALNUM, 64);
 }
예제 #17
0
 /**
  * Create auto login token.
  *
  * @return  string
  */
 protected function create_token()
 {
     do {
         $token = sha1(uniqid(\Phalcon\Text::random(\Phalcon\Text::RANDOM_ALNUM, 32), true));
     } while (Tokens::findFirst(array('token=:token:', 'bind' => array(':token' => $token))));
     return $token;
 }
예제 #18
0
 public function async_htmltoimage($type, $url, $fileName = '')
 {
     $this->setGroup("pdf_group");
     if ($fileName == '') {
         $fileName = time() . \Phalcon\Text::random(\Phalcon\Text::RANDOM_NUMERIC, 4);
     }
     $category = date("Y") . "/" . date("m");
     $result = self::$client->singleAPI("async_htmltoimage", array("url" => $url, "type" => $type, "category" => $category, "fileName" => $fileName), \DoraRPC\DoraConst::SW_MODE_NORESULT);
     $stat = $this->extract_async_status($result);
     if ($stat) {
         //发送成功,返回uri
         $uri = $type . "/image/" . $category . "/" . $fileName;
         return $uri;
     } else {
         return false;
     }
 }
예제 #19
0
 /**
  * 生成自动登录令牌
  *
  * @return string
  */
 private function createToken()
 {
     do {
         $token = sha1(uniqid(\Phalcon\Text::random(\Phalcon\Text::RANDOM_ALNUM, 32), true));
     } while (UserTokens::findFirstByToken($token));
     return $token;
 }
예제 #20
0
 protected static function coupon($id)
 {
     return \Stripe\Coupon::create(['id' => $id . '-' . \Phalcon\Text::random(5), 'duration' => 'forever', 'percent_off' => 25]);
 }