PluginProbe
Sessions / 2.3.1
Sessions v2.3.1
2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.2.0 2.3.0 2.3.1 2.4.0 2.4.1 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.1.0 3.1.1 All 39 releases
sessions / includes / system / class-uuid.php

class-uuid.php in Sessions 2.3.1, at includes/system/class-uuid.php

75 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UUIDs handling
4 *
5 * Handles all UUID operations and generation.
6 *
7 * @package System
8 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
9 * @since 1.0.0
10 */
11
12 namespace POSessions\System;
13
14 /**
15 * Define the UUID functionality.
16 *
17 * Handles all UUID operations and generation.
18 *
19 * @package System
20 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
21 * @since 1.0.0
22 */
23 class UUID {
24
25 /**
26 * Initializes the class and set its properties.
27 *
28 * @since 1.0.0
29 */
30 public function __construct() {
31 }
32
33 /**
34 * Generates a v4 UUID.
35 *
36 * @since 1.0.0
37 * @return string A v4 UUID.
38 */
39 public static function generate_v4() {
40 return sprintf(
41 '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
42 // phpcs:disable
43 mt_rand( 0, 0xffff ),
44 mt_rand( 0, 0xffff ),
45 mt_rand( 0, 0xffff ),
46 mt_rand( 0, 0x0fff ) | 0x4000,
47 mt_rand( 0, 0x3fff ) | 0x8000,
48 mt_rand( 0, 0xffff ),
49 mt_rand( 0, 0xffff ),
50 mt_rand( 0, 0xffff )
51 // phpcs:enabled
52 );
53 }
54
55 /**
56 * Generates a (pseudo) unique ID.
57 * This function does not generate cryptographically secure values, and should not be used for cryptographic purposes.
58 *
59 * @param integer $length The length of the ID.
60 * @return string The unique ID.
61 * @since 1.0.0
62 */
63 public static function generate_unique_id( $length = 10 ) {
64 $result = '';
65 do {
66 $s = self::generate_v4();
67 $s = str_replace( '-', date( 'his' ), $s );
68 $result .= $s;
69 $l = strlen( $result );
70 } while ( $l < $length );
71 return substr( str_shuffle( $result ), 0, $length );
72 }
73
74 }
75