forked from kam66be/jSEND
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsend.class.php
executable file
·186 lines (181 loc) · 6.26 KB
/
jsend.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/* -----------------------------------------------------------------------------
* jSEND PHP class v1.0.0
* -----------------------------------------------------------------------------
* Date: Fri Sep 24 19:35:11 2010 +0100
*
* Summary: This class provides text-to-binary decoding & unpacking
* for jSEND
*
* Author: Michael Kortstiege, Copyright 2010
* Website: http://jsend.org/
*
* License: Dual licensed under the MIT or GPL Version 2 licenses.
* (http://jsend.org/license/)
*
* Credits: See http://jsend.org/about/
*
* -----------------------------------------------------------------------------
* USAGE
* -----------------------------------------------------------------------------
* include('jsend.class.php');
* $data = $_POST["data"];
* // Checks, Validation etc.
* $jSEND = new jSEND();
* $str = $jSEND->getData($data);
* -----------------------------------------------------------------------------
*/
class jSEND
{
public function getData($s)
{
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DOUBLE DECODE & DECOMPRESS STRING(S)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
list($s1,$s2) = explode('==',$s);
$sDataTmp1 = self::decompressLZW(self::decodeBinary(self::decode847($s1)));
if ($s2)
$sDataTmp2 = self::decompressLZW(self::decodeBinary(self::decode847($s2)));
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
REGENERATE DATA
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
$sData = '';
$aLookup = array(
128 => 8364, 130 => 8218, 131 => 402, 132 => 8222, 133 => 8230, 134 => 8224,
135 => 8225, 136 => 710, 137 => 8240, 138 => 352, 139 => 8249, 140 => 338,
142 => 381, 145 => 8216, 146 => 8217, 147 => 8220, 148 => 8221, 149 => 8226,
150 => 8211, 151 => 8212, 152 => 732, 153 => 8482, 154 => 353, 155 => 8250,
156 => 339, 158 => 382, 159 => 376
);
/* -------------------------------------------------------
Merge strings (only if UTF-8 chars were used)
------------------------------------------------------- */
if ($sDataTmp2)
{
// Cache file (in RAM) for better performance!
//$aUtf8 = file('jsend.class.utf8chars.php', FILE_IGNORE_NEW_LINES);
for($i = 0; $i < strlen($sDataTmp1); $i++)
{
$sTmp1 = substr($sDataTmp1,$i,1);
$sTmp2 = ord(substr($sDataTmp2,$i,1));
if ($sTmp2 != 224)
//$sData .= $aUtf8[(ord($sTmp1) + 256 * $sTmp2) - 256 + 1];
//$sData .= mb_convert_encoding('&#' . (ord($sTmp1) + 256 * $sTmp2) . ';', 'UTF-8', 'HTML-ENTITIES');
$sData .= self::unichr((ord($sTmp1) + 256 * $sTmp2));
else
if (ord($sTmp1) > 127)
$sData .= utf8_encode($sTmp1);
else
$sData .= $sTmp1;
}
}
else
$sData = utf8_encode($sDataTmp1);
/* -----------------------------
ANSI Chars 128-159 to UCS
----------------------------- */
foreach ($aLookup as $sKey => $iValue)
$sData = str_replace(chr(194).chr($sKey),self::unichr($iValue),$sData);
//$sData = str_replace(chr(194).chr($sKey),html_entity_decode('&#'.$iValue.';',ENT_NOQUOTES,'UTF-8'),$sData);
return substr($sData,1);
}
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PRIVATE FUNCTIONS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
/* -------------------
* 847enc Decoder
------------------- */
private static function decode847($sChars)
{
$iByte = 7;
$iMask = 0;
$aCharCodes = array();
for($i = 0; $i < strlen($sChars); $i++)
{
$iValue = ord(substr($sChars,$i,1));
if ($iValue == 61) {
$i++;
$iValue = ord(substr($sChars,$i,1)) - 16;
}
if ($iByte > 6)
{
$iMask = $iValue;
$iByte = 0;
}
else
{
$pt = pow(2,$iByte);
if (($iMask & $pt) === $pt)
$iValue += 128;
$aCharCodes[] = $iValue;
$iByte++;
}
}
return $aCharCodes;
}
/* ------------------
* Binary Decoder
------------------ */
private static function decodeBinary($aCharCodes)
{
$aCodes = array();
$iDictCount = 256;
$iBits = 8;
$iRest = 0;
$iRestLength = 0;
for($i = 0; $i < count($aCharCodes); $i++)
{
$iRest = ($iRest << 8) + $aCharCodes[$i];
$iRestLength += 8;
if ($iRestLength >= $iBits)
{
$iRestLength -= $iBits;
$aCodes[] = $iRest >> $iRestLength;
$iRest &= (1 << $iRestLength) - 1;
$iDictCount++;
if ($iDictCount >> $iBits)
$iBits++;
}
}
return $aCodes;
}
/* --------------------
* LZW Decompressor
-------------------- */
private static function decompressLZW($aCodes)
{
$sData = '';
$oDictionary = range("\x0000", "\xff");
foreach ($aCodes as $sKey => $iCode)
{
$sElement = $oDictionary[$iCode];
if (!isset($sElement))
$sElement = $sWord . $sWord[0];
$sData .= $sElement;
if ($sKey)
$oDictionary[] = $sWord . $sElement[0];
$sWord = $sElement;
}
return $sData;
}
/* --------------------
* Unicode chr
-------------------- */
private static function unichr($iCode) {
if ($iCode <= 0x7F) {
return chr($iCode);
} else if ($iCode <= 0x7FF) {
return chr(0xC0 | $iCode >> 6) . chr(0x80 | $iCode & 0x3F);
} else if ($iCode <= 0xFFFF) {
return chr(0xE0 | $iCode >> 12) . chr(0x80 | $iCode >> 6 & 0x3F)
. chr(0x80 | $iCode & 0x3F);
} else if ($iCode <= 0x10FFFF) {
return chr(0xF0 | $iCode >> 18) . chr(0x80 | $iCode >> 12 & 0x3F)
. chr(0x80 | $iCode >> 6 & 0x3F)
. chr(0x80 | $iCode & 0x3F);
} else {
return false;
}
}
}
?>