Skip to content

Commit 3ce51d3

Browse files
author
Tobias Seipke
committed
added html js boilerplate for js testing and comparision of php results
1 parent 0c46e12 commit 3ce51d3

File tree

8 files changed

+302
-17
lines changed

8 files changed

+302
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Created by .ignore support plugin (hsz.mobi)
22
.idea/
33
/vendor
4+
boilerplate/bower_components

boilerplate/bower.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "lz-string-php-boilerplate",
3+
"homepage": "https://github.com/nullpunkt/lz-string-php",
4+
"authors": [
5+
"Tobias Seiple <tobias.seipke@gmail.com>"
6+
],
7+
"description": "",
8+
"main": "",
9+
"moduleType": [],
10+
"license": "MIT",
11+
"ignore": [
12+
"**/.*",
13+
"node_modules",
14+
"bower_components",
15+
"test",
16+
"tests"
17+
],
18+
"dependencies": {
19+
"lz-string": "~1.4.4"
20+
}
21+
}

boilerplate/index.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!doctype html>
2+
<html class="no-js" lang="" data-ng-app="lzapp" ng-strict-di>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
6+
<title>lz-string-php boilerplate</title>
7+
<meta name="description" content="">
8+
<meta name="viewport" content="width=device-width, initial-scale=1">
9+
10+
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
11+
<style>
12+
body {
13+
padding-top: 50px;
14+
padding-bottom: 20px;
15+
}
16+
</style>
17+
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap-theme.min.css">
18+
19+
</head>
20+
<body>
21+
22+
<div class="container" data-ng-controller="LZStringCtrl as vm">
23+
<div class="row">
24+
<div class="col-md-12">
25+
<form class="form-inline">
26+
<div class="form-group">
27+
<div class="input-group">
28+
<input type="text" class="form-control" data-ng-model="vm.source">
29+
</div>
30+
</div>
31+
<button type="submit" class="btn btn-primary" data-ng-click="vm.encode()">Encode!</button>
32+
</form>
33+
</div>
34+
<br>
35+
<br>
36+
<hr>
37+
<br>
38+
<div class="col-md-12">
39+
40+
<table class="table table-condensed">
41+
<thead>
42+
<tr>
43+
<th>Source</th>
44+
<th>Compressed Base64 (JS)<br>Compressed Base64 (PHP)</th>
45+
<th>Decompressed Base64 (JS)<br>Decompressed Base64 (PHP)</th>
46+
</tr>
47+
</thead>
48+
<tbody>
49+
<tr data-ng-repeat="row in vm.results | orderBy:'$index':true">
50+
<td data-ng-bind="row.input"></td>
51+
<td ng-class="{warning: row.compressed64!=row.compressed64php}">
52+
<div data-ng-bind="row.compressed64"></div>
53+
<div data-ng-bind="row.compressed64php"></div>
54+
</td>
55+
<td ng-class="{warning: row.decompressed64!=row.decompressed64php}">
56+
<div data-ng-bind="row.decompressed64"></div>
57+
<div data-ng-bind="row.decompressed64php"></div>
58+
</td>
59+
</tr>
60+
</tbody>
61+
</table>
62+
63+
</div>
64+
</div>
65+
</div>
66+
67+
<script src="bower_components/angular/angular.min.js"></script>
68+
<script src="bower_components/lz-string/libs/lz-string.min.js"></script>
69+
<script src="main.js"></script>
70+
</body>
71+
</html>
72+

boilerplate/main.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
angular.module('lzapp', []);
2+
3+
angular.module('lzapp').controller('LZStringCtrl', LZStringCtrl);
4+
5+
6+
LZStringCtrl.$inject = ['$http'];
7+
8+
function LZStringCtrl($http) {
9+
var vm = this;
10+
11+
vm.source = '';
12+
vm.results = [];
13+
14+
vm.encode = encode;
15+
16+
function encode() {
17+
vm.results.push(generate(vm.source));
18+
vm.source = '';
19+
}
20+
21+
function generate(str) {
22+
var com = LZString.compress(str), com64 = LZString.compressToBase64(str);
23+
var result = {
24+
input: str,
25+
compressed: com,
26+
compressed64: com64,
27+
decompressed: LZString.decompress(com),
28+
decompressed64: LZString.decompressFromBase64(com64)
29+
};
30+
31+
$http.post('service.php', {str: str, com64: com64}).then(function(res) {
32+
result.compressed64php = res.data.compressed64php;
33+
result.decompressed64php = res.data.decompressed64php;
34+
});
35+
36+
return result;
37+
38+
}
39+
}

boilerplate/service.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
require_once '../vendor/autoload.php';
3+
4+
$log = new \Monolog\Logger('name');
5+
$log->pushHandler(new \Monolog\Handler\StreamHandler(getcwd().'/log/service.log'));
6+
$request = json_decode(file_get_contents('php://input'), true);
7+
8+
9+
10+
$log->debug('Request: '.json_encode($request));
11+
12+
$compressed64 = \LZCompressor\LZString::compressToBase64($request['str']);
13+
$decompressed64 = \LZCompressor\LZString::decompressFromBase64($request['com64']);
14+
15+
//$decompressed64 = '';
16+
17+
$result = [
18+
'compressed64php' => $compressed64,
19+
'decompressed64php' => $decompressed64
20+
];
21+
22+
$log->debug('Result: '.json_encode($result));
23+
24+
header('Content-type: text/application');
25+
echo json_encode($result);

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
],
1515
"description": "PHP Class implementation of LZ-String javascript.",
1616
"require": {
17-
"php": ">=5.3.0"
17+
"php": ">=5.3.0",
18+
"monolog/monolog": "^1.17"
1819
},
1920
"require-dev": {
2021
"phpunit/phpunit": "~5.1"

composer.lock

Lines changed: 119 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/LZCompressor/LZString.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,13 @@ private static function produceW(LZContext $context)
150150
public static function compressToBase64($input)
151151
{
152152
$output = '';
153-
$chr1 = 'NaN';
154-
$chr2 = 'NaN';
155-
$chr3 = 'NaN';
156-
$enc1 = 'NaN';
157-
$enc2 = 'NaN';
158-
$enc3 = 'NaN';
159-
$enc4 = 'NaN';
153+
$chr1 = NAN;
154+
$chr2 = NAN;
155+
$chr3 = NAN;
156+
$enc1 = NAN;
157+
$enc2 = NAN;
158+
$enc3 = NAN;
159+
$enc4 = NAN;
160160
$input = self::compress($input);
161161
$i = 0;
162162
$strlen = mb_strlen($input, 'UTF-8');
@@ -167,16 +167,16 @@ public static function compressToBase64($input)
167167
if (($i / 2) + 1 < $strlen) {
168168
$chr3 = self::charCodeAt($input, ($i / 2) + 1) >> 8;
169169
} else {
170-
$chr3 = 'NaN';
170+
$chr3 = NAN;
171171
}
172172
} else {
173173
$chr1 = self::charCodeAt($input, ($i - 1) / 2) & 255;
174174
if (($i + 1) / 2 < $strlen) {
175175
$chr2 = self::charCodeAt($input, ($i + 1) / 2) >> 8;
176176
$chr3 = self::charCodeAt($input, ($i + 1) / 2) & 255;
177177
} else {
178-
$chr2 = 'NaN';
179-
$chr3 = 'NaN';
178+
$chr2 = NAN;
179+
$chr3 = NAN;
180180
}
181181
}
182182
$i += 3;
@@ -186,16 +186,27 @@ public static function compressToBase64($input)
186186
$enc3 = (($chr2 & 15) << 2) | ($chr3 >> 6);
187187
$enc4 = $chr3 & 63;
188188

189-
if ($chr2 === 'NaN') {
189+
if ($chr2 === NAN) {
190190
$enc3 = 64;
191191
$enc4 = 64;
192-
} else if ($chr3 === 'NaN') {
192+
} else if ($chr3 === NAN) {
193193
$enc4 = 64;
194194
}
195195

196196
$output = $output . self::$keyStr{$enc1} . self::$keyStr{$enc2} . self::$keyStr{$enc3} . self::$keyStr{$enc4};
197197
}
198198

199+
switch (strlen($output) % 4) {
200+
case 0 :
201+
break;
202+
case 1 :
203+
$output .= "==="; break;
204+
case 2 :
205+
$output .= "=="; break;
206+
case 3 :
207+
$output .= "="; break;
208+
}
209+
199210
return $output;
200211
}
201212

@@ -232,7 +243,6 @@ public static function compress($uncompressed)
232243

233244
self::writeBits($context->numBits, 2, $context->data);
234245

235-
$safe = 0;
236246
while (true) {
237247
$context->data->val = $context->data->val << 1;
238248
if ($context->data->position == 15) {

0 commit comments

Comments
 (0)