-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.func.php
More file actions
224 lines (173 loc) · 6.01 KB
/
tools.func.php
File metadata and controls
224 lines (173 loc) · 6.01 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/****************************************************************
* tools.func.php v1.0.0
* by Fumi.Iseki (c) 2010 5/13
*
* http://www.nsl.tuis.ac.jp/
*
****************************************************************/
/****************************************************************
* Function List
function isNumeric($str, $nullok=false)
function isAlphabetNumeric($str, $nullok=false)
function isAlphabetNumericSpecial($str, $nullok=false)
function isGUID($uuid, $nullok=false)
function split_key_value($str)
function make_random_hash()
function make_random_guid()
function j2k_to_tga($file, $iscopy=true)
function get_j2k_to_tga_command() // need j2k_to_image (OpenJpeg)
function get_image_size_convert_command($xsize, $ysize)
function find_command_path($command)
****************************************************************/
function isNumeric($str, $nullok=false)
{
if ($str!='0' and $str==null) return $nullok;
if (!preg_match('/^[0-9\.]+$/', $str)) return false;
return true;
}
function isAlphabetNumeric($str, $nullok=false)
{
if ($str!='0' and $str==null) return $nullok;
if (!preg_match('/^\w+$/', $str)) return false;
return true;
}
function isAlphabetNumericSpecial($str, $nullok=false)
{
if ($str!='0' and $str==null) return $nullok;
//if (!preg_match('/^[_a-zA-Z0-9 &@%#\-\.]+$/', $str)) return false;
if (!preg_match('/^[_a-zA-Z0-9 !&@%#\-\.\$]+$/', $str)) return false;
return true;
}
function isGUID($uuid, $nullok=false)
{
if ($uuid==null) return $nullok;
if (!preg_match('/^[0-9A-Fa-f]{8,8}-[0-9A-Fa-f]{4,4}-[0-9A-Fa-f]{4,4}-[0-9A-Fa-f]{4,4}-[0-9A-Fa-f]{12,12}$/', $uuid)) return false;
return true;
}
function make_random_hash()
{
$ret = sprintf('%04x%04x%04x%04x%04x%04x%04x%04x',mt_rand(0,0xffff),mt_rand(0,0xffff),mt_rand(0,0xffff),mt_rand(0,0xffff),
mt_rand(0,0xffff),mt_rand(0,0xffff),mt_rand(0,0xffff),mt_rand(0,0xffff));
return $ret;
}
function make_random_guid()
{
$uuid = sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) );
return $uuid;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// String Tools
//
// parse {"key1":"value1","key2":{"key3":"value3"}}
//
// --> [key1] => value1
// [key2] => Array
// (
// [key3] => value3
// )
//
function split_key_value($str)
{
$info = array();
$str = trim($str);
if (substr($str, 0, 1)=='{' and substr($str, -1)=='}') {
$str = substr($str, 1, -1);
$inbrkt = 0;
$inquot = false;
$inkkko = false;
$isakey = true;
$key = "";
$val = "";
for ($i=0; $i<strlen($str); $i++) {
$cc = substr($str, $i, 1);
if ($inbrkt==0 and !$inquot and ($cc=='"' or $cc=='\'')) {
$inquot = true;
}
else if ($inbrkt==0 and $inquot and ($cc=='"' or $cc=='\'')) {
$inquot = false;
}
else if ($inbrkt==0 and $isakey and !$inquot and !$inkkko and $cc==':') {
$isakey = false;
}
else if ($inbrkt==0 and !$isakey and !$inquot and !$inkkko and $cc==',') {
if (substr($val, 0, 1)=='{' and substr($val, -1)=='}') {
$info[$key] = split_key_value($val);
}
else $info[$key] = $val;
$isakey = true;
$key = "";
$val = "";
}
else {
if ($cc=='{') $inbrkt++;
else if ($cc=='}') $inbrkt--;
else {
if ($inbrkt==0 and !$inkkko and $cc=='[') $inkkko = true;
else if ($inbrkt==0 and $inkkko and $cc==']') $inkkko = false;
}
if ($isakey) $key .= $cc;
else $val .= $cc;
}
}
//
if ($key!="") {
if (substr($val, 0, 1)=='{' and substr($val, -1)=='}') {
$info[$key] = split_key_value($val);
}
else $info[$key] = $val;
}
}
return $info;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//
// Image
//
//
// Convert Image from JPEG2000 to TGA
// file -> file.tga
//
function j2k_to_tga($file, $iscopy=true)
{
if (!file_exists($file)) return false;
$com_totga = get_j2k_to_tga_command();
if ($com_totga=='') return false;
if ($iscopy) $ret = copy ($file, $file.'.j2k');
else $ret = rename($file, $file.'.j2k');
if (!$ret) return false;
exec("$com_totga -i $file.j2k -o $file.tga 1>/dev/null 2>&1");
unlink($file.'.j2k');
return true;
}
function get_j2k_to_tga_command()
{
$command = find_command_path('j2k_to_image');
return $command;
}
//
// Image Size Convert Command String
//
function get_image_size_convert_command($xsize, $ysize)
{
if (!isNumeric($xsize) or !isNumeric($ysize)) return '';
$command = find_command_path('convert');
if ($command=='') return '';
$prog = $command.' - -geometry '.$xsize.'x'.$ysize.'! -';
return $prog;
}
function find_command_path($command)
{
$path = '';
if (file_exists('/usr/local/bin/'.$command)) $path = '/usr/local/bin/';
else if (file_exists('/usr/bin/'.$command)) $path = '/usr/bin/';
else if (file_exists('/usr/X11R6/bin/'.$command)) $path = '/usr/X11R6/bin/';
else if (file_exists('/bin/'.$command)) $path = '/bin/';
else return '';
return $path.$command;
}