-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.php
224 lines (197 loc) · 5.53 KB
/
functions.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
<?php
function reversePos($string, $search, $till, $offset = 0) {
$pos = strlen($string) - $till - $offset;
if($pos >= 0)
return strrpos($string, $search, -$pos);
}
function cutToSay($string, $maxLen = 1000) {
$say = '';
while ($string) {
if(strlen($string) <= $maxLen) {
$say .= BING . trim($string);
break;
}
$pos = reversePos($string, '.', $maxLen) ?: reversePos($string, ' ', $maxLen);
$say .= BING . trim(substr($string, 0, $pos + 1));
$string = trim(substr($string, $pos + 1));
}
return $say;
}
function prepareTTS($string)
{
$string = pregReplace($string, "[^a-zא-תA-Z0-9\s()'\".,:?!-]", false, 'u');
$string = pregReplace($string, "\s+", ' ', 'u');
$string = trim($string);
if(!$string) return false;
$string = cutToSay($string);
return $string;
}
function pregExtract($string, $pattern)
{
return preg_match('#' . str_replace('~', '(.*)', $pattern) . '#is', $string, $match) ? $match[1] : '';
}
function pregReplace($string, $pattern, $subject = '', $options = 'U')
{
if (is_array($pattern))
$pattern = implode('|', $pattern);
$pattern = '#' . $pattern . '#is' . $options;
return preg_replace($pattern, $subject, $string);
}
function seconds2hours($init)
{
$hours = floor($init / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;
$hours = str_pad($hours, 2, '0', STR_PAD_LEFT);
$minutes = str_pad($minutes, 2, '0', STR_PAD_LEFT);
$seconds = str_pad($seconds, 2, '0', STR_PAD_LEFT);
$duration = $hours == '00' ? "$minutes:$seconds" : "$hours:$minutes:$seconds";
return $duration;
}
function cutFrom($from, $string, $end = false)
{
$pos = strpos($from, $string);
if ($end)
$return = $pos ? substr($from, $pos + strlen($string)) : $from;
else
$return = $pos ? substr($from, 0, $pos) : $from;
return $return;
}
if (!function_exists('redirect')) {
function redirect($url = './')
{
if ($url == 'reffer')
$url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : url();
header('location: ' . $url);
exit;
}
}
function pre($aray, $exit = false)
{
echo '<pre>';
print_r($aray);
echo '</pre>';
if ($exit) exit;
}
function GetPost(...$vars)
{ //interdit d'avoir un post et get du meme nom !!
$return = array();
foreach ($vars AS $var) {
$return[$var] = '';
if (isset($_GET[$var])) $return[$var] = $_GET[$var];
if (isset($_POST[$var])) $return[$var] = $_POST[$var];
}
return $return;
}
function GetP(...$vars)
{ //interdit d'avoir un post et get du meme nom !!
$return = array();
foreach ($vars AS $var) {
$return[$var] = '';
if (isset($_GET[$var])) $return[$var] = $_GET[$var];
if (isset($_POST[$var])) $return[$var] = $_POST[$var];
}
if (count($return) == 1)
return $return[$vars[0]];
else
return $return;
}
function preg_extract($string, $pattern)
{
$pattern = str_replace('~', '(.*)', $pattern);
if (preg_match("#$pattern#isU", $string, $match))
return $match[1];
}
function curlY($url, $post = false, $headers = false, $cookie = false, $proxy = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ($post) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if ($cookie) {
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
}
if ($proxy)
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
$return = curl_exec($ch);
return $return;
}
function Ycurl($url, $post = false, $cookie = false, $proxy = false, $useragent = false)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($useragent)
curl_setopt($ch, CURLOPT_HTTPHEADER, $useragent);
if ($post) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
if ($cookie) {
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
}
if ($proxy)
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
$return = curl_exec($ch);
return $return;
}
function clean_say($string, $mode = 't', $adress = false)
{
$max = 600;
if ($adress) {
preg_match('#([0-9]*)@[email36\.com|gmail26\.com|frum\.cf|mivtsar\.com]#isU', $string, $a);
if ($a AND $numero = $a[1])
return "d-$numero";
}
Yremplace($string, array("'", '\#', '\+', '@', '\-', '<', '>', '/', '&'), ' ');
Yremplace($string, array("\n", "\r", "\."), '.t-');
Yremplace($string, " *t\- *\.", '');
Yremplace($string, " *\.t\- *", '.t-');
Yremplace($string, "₪", 'שח');
Yremplace($string, "[^a-zA-Zא-ת0-9 ,:\'\?!\.\-]", '');
$string = trim($string);
if ($mode == 't') {
$return = '';
$arr = explode('t-', $string);
foreach ($arr AS $ar) {
$ar = trim($ar, '.');
$ar = trim($ar);
if (strlen($ar) > $max) {
$sol = '';
$spac = explode(' ', $ar);
foreach ($spac AS $spa) {
if (strlen(substr($sol, strrpos($sol, 't-'))) > $max)
$sol = trim($sol) . '.t-';
$sol .= $spa . " ";
}
$ar = trim($sol);
}
if ($ar) $return .= "t-$ar.";
}
$return = substr($return, 0, 3000);
return trim($return, '.');
} else
return "$mode-$string";
}
function Yremplace(&$string, $pattern, $subject)
{
$patt = '';
if (is_array($pattern)) {
foreach ($pattern AS $p)
$patt .= "$p|";
$patt = trim($patt, '|');
} else
$patt = $pattern;
$string = preg_replace("#$patt#isU", $subject, $string);
}