Beispiel #1
0
 /**
  * Test uniqid function.
  */
 public function test_uniqid()
 {
     //test no parameters
     $one = \pdyn\base\Utils::uniqid();
     $two = \pdyn\base\Utils::uniqid();
     $this->assertInternalType('string', $one);
     $this->assertInternalType('string', $two);
     $this->assertNotEquals($one, $two);
     $prefixes = ['', ''];
     $min = 13;
     foreach ($prefixes as $i => $prefix) {
         $lengths = [2, 3, 4, 6, 10, 12, 13, 14, 16, 32, 64, 128];
         foreach ($lengths as $j => $len) {
             if (!empty($prefix)) {
                 $one = \pdyn\base\Utils::uniqid($len, $prefix);
                 $two = \pdyn\base\Utils::uniqid($len, $prefix);
             } else {
                 $one = \pdyn\base\Utils::uniqid($len);
                 $two = \pdyn\base\Utils::uniqid($len);
             }
             $this->assertInternalType('string', $one, 'Failed with ' . $prefix . '/' . $len);
             $this->assertInternalType('string', $two, 'Failed with ' . $prefix . '/' . $len);
             // Encountered false failures when using $this->assertNotEquals
             $this->assertTrue($one !== $two, 'Failed with ' . $prefix . '/' . $len);
             if ($len < $min) {
                 $len = $min;
             }
             $this->assertEquals($len, mb_strlen($one), 'Failed with ' . $prefix . '/' . $len);
             $this->assertEquals($len, mb_strlen($two), 'Failed with ' . $prefix . '/' . $len);
         }
     }
 }
Beispiel #2
0
 /**
  * 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;
     }
 }
Beispiel #3
0
 /**
  * Generate a unique session ID.
  *
  * @return string A unique 64-character string.
  */
 protected function generate_session_id()
 {
     return \pdyn\base\Utils::uniqid(64);
 }
Beispiel #4
0
 /**
  * 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()];
     }
 }
Beispiel #5
0
 /**
  * Format a backtrace for better display.
  *
  * @param array $backtrace A backtrace from debug_backtrace.
  * @return string Html that better displays the backtrace.
  */
 public static function format_backtrace($backtrace)
 {
     global $CFG;
     $html = '';
     $i = count($backtrace);
     foreach ($backtrace as $trace) {
         $file = isset($trace['file']) ? str_replace($CFG->base_absroot, '', $trace['file']) : 'unknown';
         $line = 'Line: ' . (isset($trace['line']) ? $trace['line'] : '-');
         $func = isset($trace['function']) ? $trace['function'] : '';
         ini_set('html_errors', 0);
         $argstr = [];
         if (!empty($trace['args']) && is_array($trace['args'])) {
             foreach ($trace['args'] as $arg) {
                 ob_start();
                 var_dump($arg);
                 $stringval = ob_get_contents();
                 ob_end_clean();
                 $argstr[] = $stringval;
             }
         }
         $args = implode(', ', $argstr);
         $func .= '(' . $args . ')';
         if (\pdyn\base\Utils::is_cli_env() === true) {
             $html .= $i . "\t" . $file . "\t" . $line . "\t" . $func . "\n";
         } else {
             $html .= '<tr>';
             $html .= '<td style="vertical-align:top">' . $i . '</td>';
             $html .= '<td style="vertical-align:top">' . $file . '</td>';
             $html .= '<td style="vertical-align:top">' . $line . '</td>';
             $html .= '<td><pre style="margin:0;">' . $func . '</pre></td>';
             $html .= '</tr>';
         }
         $i--;
     }
     return \pdyn\base\Utils::is_cli_env() === true ? "**********\n" . $html . "**********\n" : '<table cellspacing="5" style="color:inherit">' . $html . '</table>';
 }