server/php/basic/resources/library/utilsFunctions.php
changeset 442 adb907bba956
equal deleted inserted replaced
441:4732f078d0fe 442:adb907bba956
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * Generates version 4 UUIDs
       
     5  * from https://github.com/ramsey/uuid/blob/2.8/src/Uuid.php
       
     6  * and http://stackoverflow.com/a/2040279
       
     7  *
       
     8  * @return string
       
     9  */
       
    10 function genUuid4() {
       
    11 
       
    12     if (function_exists('openssl_random_pseudo_bytes')) {
       
    13         $bytes = openssl_random_pseudo_bytes(16);
       
    14     }
       
    15     else {
       
    16         $bytes = '';
       
    17         mt_srand(crc32(serialize([microtime(true), 'USER_IP', 'ETC'])));
       
    18         for ($i = 1; $i <= $length; $i++) {
       
    19             $bytes = chr(mt_rand(0, 255)) . $bytes;
       
    20         }
       
    21     }
       
    22 
       
    23 
       
    24     $bytes[6] = chr(ord($bytes[6]) & 0x0f | 0x40); // set version to 0100
       
    25     $bytes[8] = chr(ord($bytes[8]) & 0x3f | 0x80); // set bits 6-7 to 10
       
    26 
       
    27     return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($bytes), 4));
       
    28 }
       
    29 
       
    30 
       
    31 /**
       
    32  * get page self url
       
    33  * from http://stackoverflow.com/a/2236887
       
    34  */
       
    35 function strleft($s1, $s2) { return substr($s1, 0, strpos($s1, $s2)); }
       
    36 function selfURL()
       
    37 {
       
    38     $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
       
    39     $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
       
    40     $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
       
    41     return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
       
    42 }
       
    43 
       
    44 function selfBaseURL()
       
    45 {
       
    46     $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
       
    47     $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
       
    48     $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
       
    49     return $protocol."://".$_SERVER['SERVER_NAME'].$port.dirname($_SERVER['REQUEST_URI']);
       
    50 }