added recv_nick and P10_User
[PHP-P10.git] / Uplink / Numerics.class.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
7  * PHP-P10 is free software; you can redistribute it and/or modify      *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or    *
10  * (at your option) any later version.                                  *
11  *                                                                      *
12  * This program is distributed in the hope that it will be useful,      *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
15  * GNU General Public License for more details.                         *
16  *                                                                      *
17  * You should have received a copy of the GNU General Public License    *
18  * along with PHP-P10; if not, write to the Free Software Foundation,   *
19  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  Uplink/Numerics.class.php
24  *
25  * P10 numeric functions
26  *
27  ************************************************************************
28  * accessable functions
29  *
30  * static String intToNum(int $int, int $length)
31  *     returns the numeric representing $int
32  *
33  * static int numToInt(String $numeric)
34  *     returns the integer value, the numeric represents
35  *
36  * static String parseIP(String $numeric)
37  *     parses an IP Address in numeric format
38  */
39
40 class Numerics {
41         private static $base64chars = array(
42           'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
43           'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
44           'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
45           'w','x','y','z','0','1','2','3','4','5','6','7','8','9','[',']'
46         );
47         private static $base64charsLength = 64;
48         
49         public static function intToNum($int, $length) {
50                 //fix a small "bug": normaly 0 = AAAAA but we need 1 = AAAAA
51                 $int = $int - 1;
52                 
53                 $numeric = "";
54                 for($pos = $length-1; $pos >= 0; $pos--) {
55                         //current position represents floor($int / ($base64charsLength ^ $pos))
56                         $base = 1;
57                         for($i = 0; $i < $pos; $i++) {
58                                 $base = $base * self::$base64charsLength;
59                         }
60                         $posValue = floor($int / $base);
61                         //get the char representing $posValue
62                         $posChar = self::$base64chars[$posValue];
63                         $numeric .= $posChar;
64                 }
65                 
66                 return $numeric;
67         }
68         
69         public static function numToInt($numeric) {
70                 $base = 1;
71                 $int = 0;
72                 for($pos = strlen($numeric)-1; $pos >= 0; $pos--) {
73                         $posValue = array_search($numeric[$pos], self::$base64chars);
74                         $int = ($posValue * $base);
75                         $base = $base * self::$base64charsLength;
76                 }
77                 
78                 //fix a small "bug": normaly 0 = AAAAA but we need 1 = AAAAA
79                 $int = $int + 1;
80                 
81                 return $int;
82         }
83         
84         public static function parseIP($numeric) {
85                 if(strlen($numeric) == 6) { //IPv4
86                         $value = self::numToInt($numeric);
87                         $ip = array();
88                         $ip[0] = ($value & 0xff000000) >> 24;
89                         $ip[1] = ($value & 0x00ff0000) >> 16;
90                         $ip[2] = ($value & 0x0000ff00) >> 8;
91                         $ip[3] = ($value & 0x000000ff);
92                         return implode(".", $ip);
93                 } else { //IPv6
94                         $ip = array();
95                         for($i = 0; $i < strlen($numeric);) {
96                                 if($numeric[$i] == "_") {
97                                         $rightBlocks = (strlen($numeric) - ($i + 1)) / 3;
98                                         $skipCount = 8 - count($ip) - $rightBlocks;
99                                         for($j = 0; $j < $skipBlocks; $j++) {
100                                                 $ip[] = "0";
101                                         }
102                                 } else {
103                                         $value = self::numToInt($numeric[$i].$numeric[$i+1].$numeric[$i+2]);
104                                         $ip[] = dechex($value);
105                                         $i += 3;
106                                 }
107                         }
108                         return implode(":", $ip);
109                 }
110         }
111         
112 }
113
114 ?>