<?php
/*
This file is part of Hotseat Corner.
Hotseat Corner is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Hotseat Corner is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Hotseat Corner. If not, see <http://www.gnu.org/licenses/>.
*/
class HSUtils_ChID {
private static $mask = array('b'=>'0','c'=>'1','d'=>'2','f'=>'3','g'=>'4','h'=>'5','j'=>'6','k'=>'7','l'=>'8','m'=>'9','n'=>'a','p'=>'b','q'=>'c','r'=>'d','s'=>'e','t'=>'f','v'=>'g','w'=>'h','x'=>'i','y'=>'j');
private static $padchar = 'z';
private static $padlength = 4;
private static $translations = array();
public static function get ($key)
{
$uid = HSUtils_UID::get($key);
return self::encode($uid);
}
public static function encode($id)
{
if (!preg_match('/^\d+$/', $id)) {
throw new exception('not an int');
}
$idb20 = base_convert ($id, 10, 20);
$chars = preg_split('//', $idb20, -1, PREG_SPLIT_NO_EMPTY);
foreach ($chars as $char) {
$key .= array_search($char, self::$mask, true);
}
$key .= self::$padchar;
self::$translations[$key] = $id;
str_pad($key, self::$padlength, self::$padchar, STR_PAD_RIGHT);
return $key;
}
public static function decode($key)
{
if (self::$translations[$key]) {
return self::$translations[$key];
}
$chars = preg_split('//', $key, -1, PREG_SPLIT_NO_EMPTY);
foreach ($chars as $char) {
$idb20 .= self::$mask[$char];
}
$id = base_convert ($idb20, 20, 10);
self::$translations[$key] = $id;
return $id;
}
}
?>