コード例 #1
0
ファイル: UtilsTest.php プロジェクト: pdyn/base
 /**
  * Test genRandomStr function.
  */
 public function test_genRandomStr()
 {
     $lengths = [2, 4, 8, 16, 32, 64, 128];
     foreach ($lengths as $len) {
         $str = \pdyn\base\Utils::genRandomStr($len);
         $this->assertInternalType('string', $str);
         $this->assertEquals($len, mb_strlen($str));
     }
 }
コード例 #2
0
ファイル: Utils.php プロジェクト: pdyn/base
 /**
  * Generate a guaranteed unique value of a specified length.
  * @param int|bool $length A desired length, or false to use uniqid length.
  * @return string The unique string.
  */
 public static function uniqid($length = false)
 {
     if ($length <= 13) {
         return uniqid();
     }
     $uniq = strrev(str_replace('.', '', uniqid('', true)));
     $uniqlen = mb_strlen($uniq);
     if ($length === false || $length === $uniqlen) {
         return $uniq;
     } elseif ($length < $uniqlen) {
         return mb_substr($uniq, 0, $length);
     } else {
         return \pdyn\base\Utils::genRandomStr($length - mb_strlen($uniq)) . $uniq;
     }
 }
コード例 #3
0
ファイル: Client.php プロジェクト: pdyn/pubsubhubbub
 /**
  * This sends a request to the hub.
  *
  * @param string $mode The request mode. 'subscribe' or 'unsubscribe'
  * @param string $url The URL to send the request to - the hub URL.
  * @param string $topic The topic you are referring to.
  * @param string $callback The local callback URL.
  * @param \pdyn\httpclient\HttpClientInterface $httpclient An HttpClientInterface object that will handle the transfer.
  * @return bool|array True if successful, array of 'status_code', and 'msg', specifying received http code and body text.
  */
 protected function send_request($mode, $url, $topic, $callback, \pdyn\httpclient\HttpClientInterface $httpclient)
 {
     if (!\pdyn\datatype\Url::validate($callback)) {
         throw new Exception('Bad $callback received', Exception::ERR_BAD_REQUEST);
     }
     if (!\pdyn\datatype\Url::validate($url)) {
         throw new Exception('Bad $url received', Exception::ERR_BAD_REQUEST);
     }
     $request = $this->storage->get_request($mode, $topic, $url, $callback);
     $verifytoken = \pdyn\base\Utils::genRandomStr(64);
     if (empty($request)) {
         $request = $this->storage->create_request($mode, $topic, $url, $callback, $verifytoken);
     }
     $postdata = ['hub.mode' => $mode, 'hub.callback' => $callback, 'hub.topic' => $topic, 'hub.verify' => 'async', 'hub.verify_token' => $verifytoken, 'hub.lease_seconds' => 604800];
     $response = $httpclient->post($url, $postdata);
     if ($response->status_code() === 204 || $response->status_code() === 202) {
         return true;
     } else {
         $this->storage->delete_request($request->id);
         return ['status_code' => $response->status_code(), 'msg' => $response->body()];
     }
 }