]> Raphaƫl G. Git Repositories - userbundle/blob - Utils/Slugger.php
e781fa5c95e3c55e7b9a4605a3ba386053b4765e
[userbundle] / Utils / Slugger.php
1 <?php
2
3 namespace Rapsys\UserBundle\Utils;
4
5 use Symfony\Component\DependencyInjection\ContainerInterface;
6
7 class Slugger {
8 //The secret parameter
9 private $secret;
10
11 //The alpha array
12 private $alpha;
13
14 //The rev array
15 private $rev;
16
17 //The alpha array key number
18 private $count;
19
20 //The offset reduced from secret
21 private $offset;
22
23 //Retrieve secret and set offset from reduction
24 public function __construct(ContainerInterface $container) {
25 //Set secret
26 $this->secret = $_SERVER['APP_SECRET'] ?? $container->getParameter('kernel.secret');
27
28 //Pseudo-random alphabet
29 //XXX: use array flip and keys to workaround php "smart" that cast range('0', '9') as int instead of string
30 //XXX: The key count mismatch, count(alpha)>count(rev), resulted in a data corruption due to duplicate numeric values
31 //TODO: set this as a parameter generated once in a command ?
32 $this->alpha = array_keys(array_flip(array_merge(
33 range('^', '[', -1),
34 range('V', 'Z'),
35 range('9', '7', -1),
36 range('L', 'O'),
37 range('f', 'a', -1),
38 range('_', '`'),
39 range('3', '0', -1),
40 range('E', 'H'),
41 range('v', 'r', -1),
42 range('+', '/'),
43 range('K', 'I', -1),
44 range('g', 'j'),
45 range('=', ':', -1),
46 range('>', '@'),
47 range('m', 'k', -1),
48 range('4', '6'),
49 range('*', '%', -1),
50 range('n', 'q'),
51 range('U', 'P', -1),
52 range(' ', '$'),
53 range('D', 'A', -1),
54 range('w', 'z'),
55 range('~', '!', -1)
56 )));
57
58 //Init rev array
59 $this->count = count($rev = $this->rev = array_flip($this->alpha));
60
61 //Init split
62 $split = str_split($this->secret);
63
64 //Set offset
65 $this->offset = array_reduce($split, function ($res, $a) use ($rev) { return $res += $rev[$a]; }, count($split)) % $this->count;
66 }
67
68 //Short the string
69 public function short(string $string): string {
70 //Return string
71 $ret = '';
72
73 //Iterate on each character
74 foreach(str_split($string) as $k => $c) {
75 if (isset($this->rev[$c]) && isset($this->alpha[($this->rev[$c]+$this->offset)%$this->count])) {
76 //XXX: Remap char to an other one
77 $ret .= chr(($this->rev[$c] - $this->offset + $this->count) % $this->count);
78 }
79 }
80
81 //Send result
82 return str_replace(['+','/'], ['-','_'], base64_encode($ret));
83 }
84
85 //Unshort the string
86 public function unshort(string $string): string {
87 //Return string
88 $ret = '';
89
90 //Iterate on each character
91 foreach(str_split(base64_decode(str_replace(['-','_'], ['+','/'], $string))) as $c) {
92 //XXX: Reverse map char to an other one
93 $ret .= $this->alpha[(ord($c) + $this->offset) % $this->count];
94 }
95
96 //Send result
97 return $ret;
98 }
99
100 //Crypt and base64uri encode string
101 public function hash(string $string): string {
102 return str_replace(['+','/'], ['-','_'], base64_encode(crypt($string, $this->secret)));
103 }
104
105 //Convert string to safe slug
106 function slug(string $string): string {
107 //Use Transliterator if available
108 if (class_exists('Transliterator')) {
109 $trans = Transliterator::create('Any-Latin; Latin-ASCII; Lower()');
110 return preg_replace(['/[^a-zA-Z0-9]+/', '/(^-+|-+$)/'], ['-', ''], $trans->transliterate($string));
111 }
112 return preg_replace('/[\/_|+ -]+/', '-', strtolower(trim(preg_replace('/[^a-zA-Z0-9\/_|+ -]/', '', str_replace(['\'', '"'], ' ', iconv('UTF-8', 'ASCII//TRANSLIT', $string))), '-')));
113 }
114 }