3 namespace Rapsys\UserBundle\Utils
;
9 //The offset reduced from secret
12 //Retrieve secret and set offset from reduction
13 public function __construct(\Symfony\Component\DependencyInjection\ContainerInterface
$container) {
15 $this->secret
= $container->getParameter('secret');
18 $rev = array_flip(array_merge(range('0', '9'), range('a', 'z'), range('A', 'Z'), range('!', '~')));
21 $this->offset
= array_reduce(str_split($this->secret
), function ($res, $a) use ($rev) { return $res +
= $rev
[$a
]; }, count($this->secret
)) %
count($rev);
25 public function short($string) {
30 $alpha = array_merge(range('0', '9'), range('a', 'z'), range('A', 'Z'), range('!', '~'));
33 $rev = array_flip($alpha);
36 $count = count($alpha);
38 //Iterate on each character
39 foreach(str_split($string) as $c) {
40 if (isset($rev[$c]) && isset($alpha[($rev[$c]+
$this->offset
)%
$count])) {
41 $ret .= $alpha[($rev[$c]+
$this->offset
)%
$count];
46 return str_replace(array('+','/'), array('-','_'), base64_encode($ret));
50 public function unshort($string) {
55 $alpha = array_merge(range('0', '9'), range('a', 'z'), range('A', 'Z'), range('!', '~'));
58 $rev = array_flip($alpha);
61 $count = count($alpha);
63 //Iterate on each character
64 foreach(str_split(base64_decode(str_replace(array('-','_'), array('+','/'), $string))) as $c) {
65 if (isset($rev[$c]) && isset($alpha[($rev[$c]-$this->offset+
$count)%
$count])) {
66 $ret .= $alpha[($rev[$c]-$this->offset+
$count)%
$count];
74 //Crypt and base64uri encode string
75 public function hash($string) {
76 return str_replace(array('+','/'), array('-','_'), base64_encode(crypt($string, $this->secret
)));
79 //Convert string to safe slug
80 function slug($string) {
81 return preg_replace('/[\/_|+ -]+/', '-', strtolower(trim(preg_replace('/[^a-zA-Z0-9\/_|+ -]/', '', str_replace(array('\'', '"'), ' ', iconv('UTF-8', 'ASCII//TRANSLIT', $string))), '-')));