forked from swordapp/swordappv2-php-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.php
76 lines (63 loc) · 1.96 KB
/
utils.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
<?php
function sac_clean($string) {
// Tidy a string
$string = str_replace("\n", "", $string);
$string = str_replace("\r", "", $string);
$string = str_replace("\t", "", $string);
$string = preg_replace('/\t/', '', $string);
$string = preg_replace('/\s\s+/', ' ', $string);
$string = trim($string);
return $string;
}
function base64chunk($in, $out) {
// Base64 encode, then chunk, a file
// By 'MitMacher' from http://www.php.net/manual/en/function.base64-encode.php#92762
$fh_in = fopen($in, 'rb');
$fh_out = fopen($out, 'ab');
$cache = '';
$eof = false;
while (true) {
if (!$eof) {
if (!feof($fh_in)) {
$row = fgets($fh_in, 4096);
} else {
$row = '';
$eof = true;
}
}
if ($cache !== '') {
$row = $cache . $row;
}
elseif ($eof) {
break;
}
$b64 = base64_encode($row);
$put = '';
if (strlen($b64) < 76) {
if ($eof) {
$put = $b64 . "\r\n";
$cache = '';
} else {
$cache = $row;
}
} elseif (strlen($b64) > 76) {
do {
$put .= substr($b64, 0, 76) . "\r\n";
$b64 = substr($b64, 76);
} while (strlen($b64) > 76);
$cache = base64_decode($b64);
} else {
if (!$eof && $b64{75} == '=') {
$cache = $row;
} else {
$put = $b64."\r\n";
$cache = '';
}
}
if ($put !== '') {
fputs($fh_out, $put);
}
}
fclose($fh_in);
}
?>