-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.inc.php
318 lines (314 loc) · 9.17 KB
/
functions.inc.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
require_once('config.php');
require_once('tables.inc.php');
require_once('SitePlugin.inc.php');
/*
0 to name (simplify)
1 from fn
2 to fn (regex)
3 to fn
4 to fn (glob)
*/
function name_simplify($name, $type=0) {
//'-'
static $ar1=array('_', ':', ',', '/', '\u00a0', '\u00A0', "\u00a0", "\u00A0", "\xC2\xA0", ' ', ' ', ' ', ' ', '&zwsp;', '‌'); // space
//'+'
static $ar2=array(',', '\'', '*', '—', '^', '=', '\u2014', "\u2014", "\xE2\x80\x93", ''', '\u2019', "\u2019", '’', '´', "\xE2\x80\x99", '’', '‘', '?', '!', '(', ')', '[', ']', 'Retranslated Version', 'Retranslated_Version', 'retranslated version', '.', '⁠', '&nobreak;', '\u2060', "\u2060", "\xE2\x81\xA0"); // del
$name=mb_convert_encoding($name, 'HTML-ENTITIES', 'UTF-8');
$name=html_entity_decode($name);
$name=str_replace(array('\uff1f', "\uff1F", '\uFF1F', "\uFF1sF", "\xEF\xBC\x9F", '?', '?'), '?', $name);
$name=str_replace($ar2, '', $name);
$name=str_replace($ar1, ' ', $name);
//$name=mb_convert_encoding($name, 'HTML-ENTITIES', 'UTF-8');
//$name=html_entity_decode($name);
//$name=str_replace('& ', ' ', $name);
$name=str_replace('&', '', $name);
$name=str_replace(array('é', '\u00e9'), 'e', $name);
$name=trim($name);
if($type==1) {
$name=str_replace(array('+'), '', $name);
}
else if($type==2) {
$name=str_replace(array('+'), '', $name);
$name=str_replace(array(' ', '_'), '.*', $name);
}
else if($type==3) {
$name=str_replace(array('+'), '', $name);
$name=str_replace(array(' ', '_'), '_', $name);
}
else if($type==4) {
$name=str_replace(array('+'), '', $name);
$name=str_replace(array(' ', '_'), '*', $name);
}
//$name=strtolower($name);
$name=str_replace(' ', ' ', $name);
return $name;
}
function name_compare($name1, $name2, $type=0) {
$name1=name_simplify($name1, $type);
$name2=name_simplify($name2, $type);
//var_dump($name1,$name2);
//if($name1=='lord of the mysteries') var_dump($name1, $name2, strcasecmp($name1, $name2));
return (strcasecmp($name1, $name2)==0);
}
function case_count($name) {
$name=trim($name);
$len=strlen($name);
$res=array( 'low'=>0, 'up'=>0, 'dig'=>0, 'symb'=>0, 'esp'=>0 );
for($i=0;$i<$len;++$i)
{
if($name[$i]>='a' && $name[$i]<='z') $res['low']++;
else if($name[$i]>='A' && $name[$i]<='Z') $res['up' ]++;
else if($name[$i]>='0' && $name[$i]<='9') $res['dig']++;
else if($name[$i]==' ' || $name[$i]=='_') $res['esp']++;
else $res['symb']++;
}
return $res;
}
function startswith($haystack, $needle) {
return (strncasecmp($haystack, $needle, strlen($needle))==0);
}
function endswith($haystack, $needle) {
return (strncasecmp(substr($haystack, -strlen($needle)), $needle, strlen($needle))==0);
}
$translit1=Transliterator::create('Hex-Any');
$translit2=Transliterator::create("[:^ASCII:] Any-Hex");
function normalize($str) {
global $translit1, $translit2;
$str=trim($str);
$str=html_entity_decode($str, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$str=htmlspecialchars_decode($str, ENT_COMPAT);
$str=trim($str);
$str=$translit1->transliterate($str);
$str=str_replace('\\u0026', '&', $str);
$str=str_replace(array('\\u00a0', ' '), ' ', $str);
$str=str_replace('\\uff1f', '?', $str);
$str=str_replace('\\u0110', 'D', $str);
$str=str_replace('\\u0111', 'd', $str);
$str=html_entity_decode($str, ENT_QUOTES | ENT_HTML5, 'UTF-8');
$str=$translit2->transliterate($str);
return $str;
}
function trim2($str) {
$str=trim($str);
$str=str_replace(array(' ', ' ', '&zwsp;', '‌'), ' ', $str);
$str=trim($str);
return $str;
}
//$translit4=Transliterator::create("NFD; [:Nonspacing Mark:] Remove; NFC");
$translit4=Transliterator::create("NFKD; [:Nonspacing Mark:] Remove; NFKC");
$translit5=Transliterator::create("Hex-Any; NFKD; [:Nonspacing Mark:] Remove; NFKC; Any-Latin; NFKD; [:Nonspacing Mark:] Remove; NFKC; Any-Latin; Latin-ASCII");
$translit6=Transliterator::create("Latin-ASCII");
function normalize2($str) {
global $translit4, $translit5, $translit6;
//$str=$translit4->transliterate($str);
$str=$translit5->transliterate($str);
//$str=$translit6->transliterate($str);
return $str;
}
$assoc=array(
'RoyalRoad'=>array(
'royalroad',
'rr',
),
'WebNovel'=>array(
'qi',
'qidian',
'webnovel',
'wn',
),
'WLNUpdate'=>array(
'wln',
'wlnupdate',
),
);
function enabled($site) {
global $assoc,$disabled;
$res=true;
foreach($disabled as $s) {
foreach($assoc as $k=>$a) {
if(in_array(strtolower($site),$a)||strtolower($site)==strtolower($k)) {
if(in_array(strtolower($s),$a)||strtolower($s)==strtolower($k)) {
//var_dump($s,$a,strtolower($site));
$res=false;
break(2);
}
}
}
}
//var_dump(__FUNCTION__,$site,$res);
return $res;
}
function disabled($site) {
global $assoc,$disabled;
$res=false;
foreach($disabled as $s) {
foreach($assoc as $k=>$a) {
if(in_array(strtolower($site),$a)||strtolower($site)==strtolower($k)) {
if(in_array(strtolower($s),$a)||strtolower($s)==strtolower($k)) {
//var_dump($s,$a,strtolower($site));
$res=true;
break(2);
}
}
}
}
//var_dump(__FUNCTION__,$site,$res);
var_dump(__FUNCTION__,$site,$res);
return $res;
}
if ( !function_exists( 'is_iterable' ) ) {
function is_iterable( $obj ) {
return is_array( $obj ) || ( is_object( $obj ) && ( $obj instanceof \Traversable ) );
}
}
function millitime() {
list($usec, $sec) = explode(' ', microtime());
return $sec.substr($usec, 2, 3);
}
function simplexml_load_html($html) {
libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->strictErrorChecking = FALSE;
$doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
libxml_use_internal_errors(false);
$xml = simplexml_import_dom($doc);
return $xml;
}
function parse_publishTimeFormat($str) {
$str=str_replace('yr', ' year', $str);
$str=str_replace('mth', ' month', $str);
$str=str_replace('d', ' day', $str);
$str=str_replace('h', ' hour', $str);
$str.=' ago';
return $str;
}
function &get($obj, $attr) {
if(is_array($obj)) {
if(array_key_exists($attr, $obj)) {
return ($obj[$attr]);
}
else throw new OutOfBoundsException();
}
else if(is_object($obj)) {
if(property_exists($obj, $attr)) {
return ($obj->{$attr});
}
else throw new OutOfBoundsException();
}
else throw new UnexpectedValueException();
}
function &set($obj, $attr, $value) {
$var=&get($obj, $attr);
$var=$value;
return $var;
}
function exists($obj, $attr) {
if(is_array($obj)) {
return array_key_exists($attr, $obj);
}
else if(is_object($obj)) {
return property_exists($obj, $attr);
}
else throw new UnexpectedValueException();
}
function &get2($obj, $attr) {
$res=NULL;
if(is_array($obj)) {
if(array_key_exists($attr, $obj)) {
return ($obj[$attr]);
}
else return $res;
}
else if(is_object($obj)) {
if(property_exists($obj, $attr)) {
return ($obj->{$attr});
}
else return $res;
}
else if(is_null($obj)) return $res;
else throw new UnexpectedValueException();
}
function exists2($obj, $attr) {
$res=NULL;
if(is_array($obj)) {
return array_key_exists($attr, $obj);
}
else if(is_object($obj)) {
return property_exists($obj, $attr);
}
else if(is_null($obj)) return $res;
else throw new UnexpectedValueException();
}
function direct2() {
return direct();
}
function direct() {
$bt=debug_backtrace();
$bt=array_filter($bt, fn($e) => !in_array($e['function'], ['include', 'include_once', 'require', 'require_once']));
if(count($bt)>0) {
$idx=count($bt)-1;
$btf=$bt[$idx];
} else {
$btf=array('file'=>__FILE__);
}
$direct= ( realpath($_SERVER['SCRIPT_FILENAME']) === realpath($btf['file']) );
//var_dump($bt,$direct);
return $direct;
}
/**
* Format a timestamp to display its age (5 days ago, in 3 days, etc.).
*
* @param int $timestamp
* @param int $now
* @return string
*/
function timetostr($timestamp, $now = null) {
$age = ($now ?: time()) - $timestamp;
$future = ($age < 0);
$age = abs($age);
$age = (int)($age / 60); // minutes ago
if ($age == 0) return $future ? "momentarily" : "just now";
$scales = [
["minute", "minutes", 60],
["hour", "hours", 24],
["day", "days", 7],
["week", "weeks", 4.348214286], // average with leap year every 4 years
["month", "months", 12],
["year", "years", 10],
["decade", "decades", 10],
["century", "centuries", 1000],
["millenium", "millenia", PHP_INT_MAX]
];
foreach ($scales as list($singular, $plural, $factor)) {
if ($age == 0)
return $future
? "in less than 1 $singular"
: "less than 1 $singular ago";
if ($age == 1)
return $future
? "in 1 $singular"
: "1 $singular ago";
if ($age < $factor)
return $future
? "in $age $plural"
: "$age $plural ago";
$age = (int)($age / $factor);
}
}
function iglob($pattern) {
if( ($handle=opendir('.'))!==false) {
$ar=array();
while( ($entry=readdir($handle))!==false ) {
if(fnmatch($pattern, $entry, FNM_CASEFOLD)) {
$ar[]=$entry;
}
}
natcasesort($ar);
closedir($handle);
return $ar;
}
else {
return array();
}
}