-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprivate_functions.php
More file actions
280 lines (272 loc) · 10.7 KB
/
private_functions.php
File metadata and controls
280 lines (272 loc) · 10.7 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
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
<?php
function get_encoded_string ($str) {
setlocale(LC_ALL, 'fr_FR.UTF-8');
return iconv('UTF-8', 'ASCII//TRANSLIT', $str);
}
/**
* truncate : cuts a string to the length of $length and replaces the last
* characters with the ending if the text is longer than length.
* credits goes to CakePHP for this wonder
*
* @param string $text : string to truncate
* @param int $length : length of returned string, including ellipsis
* @param array $options : an array of html attributes and options :
* 'ending' will be used as Ending and appended to the trimmed string
* 'exact' if false, $text will not be cut mid-word
* 'html' if true, HTML tags would be handled correctly
* @access public
* @link http://book.cakephp.org/view/1469/Text#truncate-1625
* @return string : trimmed string
*/
function truncate ($text, $length = 100, $options = array()) {
$default = array(
'ending' => '...', 'exact' => true, 'html' => false
);
$options = array_merge($default, $options);
extract($options);
if ($html) {
if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
return $text;
}
$totalLength = mb_strlen(strip_tags($ending));
$openTags = array();
$truncate = '';
preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
foreach ($tags as $tag) {
if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) {
if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) {
array_unshift($openTags, $tag[2]);
} else if (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) {
$pos = array_search($closeTag[1], $openTags);
if ($pos !== false) {
array_splice($openTags, $pos, 1);
}
}
}
$truncate .= $tag[1];
$contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3]));
if ($contentLength + $totalLength > $length) {
$left = $length - $totalLength;
$entitiesLength = 0;
if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE)) {
foreach ($entities[0] as $entity) {
if ($entity[1] + 1 - $entitiesLength <= $left) {
$left--;
$entitiesLength += mb_strlen($entity[0]);
} else {
break;
}
}
}
$truncate .= mb_substr($tag[3], 0 , $left + $entitiesLength);
break;
} else {
$truncate .= $tag[3];
$totalLength += $contentLength;
}
if ($totalLength >= $length) {
break;
}
}
} else {
if (mb_strlen($text) <= $length) {
return $text;
} else {
$truncate = mb_substr($text, 0, $length - mb_strlen($ending));
}
}
if (!$exact) {
$spacepos = mb_strrpos($truncate, ' ');
if (isset($spacepos)) {
if ($html) {
$bits = mb_substr($truncate, $spacepos);
preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
if (!empty($droppedTags)) {
foreach ($droppedTags as $closingTag) {
if (!in_array($closingTag[1], $openTags)) {
array_unshift($openTags, $closingTag[1]);
}
}
}
}
$truncate = mb_substr($truncate, 0, $spacepos);
}
}
$truncate .= $ending;
if ($html) {
foreach ($openTags as $tag) {
$truncate .= '</'.$tag.'>';
}
}
return $truncate;
}
// -----------------------------------------
// Ajoute/Modifie un parametre à un URL.
// -----------------------------------------
function ajouterParametreGET ($url, $paramNom, $paramValeur){
$urlFinal = "";
if ($paramNom == "") {
$urlFinal = $url;
} else {
$t_url = explode("?", $url);
if (count($t_url) == 1) {
// pas de queryString
$urlFinal .= $url;
if(substr($url, strlen($url) - 1, strlen($url)) != "/"){
$t_url2 = explode("/", $url);
if(preg_match("/./", $t_url2[count($t_url2) - 1]) == false){
$urlFinal .= "/";
}
}
$urlFinal .= "?".$paramNom."=".$paramValeur;
} else if(count($t_url) == 2) {
// il y a une queryString
$paramAAjouterPresentDansQueryString = "non";
$t_queryString = explode("&", $t_url[1]);
foreach ($t_queryString as $cle => $coupleNomValeur) {
$t_param = explode("=", $coupleNomValeur);
if($t_param[0] == $paramNom){
$paramAAjouterPresentDansQueryString = "oui";
}
}
if ($paramAAjouterPresentDansQueryString == "non") {
// le parametre à ajouter n'existe pas encore dans la queryString
$urlFinal = $url . "&" . $paramNom . "=" . $paramValeur;
} else if($paramAAjouterPresentDansQueryString == "oui") {
// le parametre à ajouter existe déjà dans la queryString
// donc on va reconstruire l'URL
$urlFinal = $t_url[0]."?";
foreach ($t_queryString as $cle => $coupleNomValeur) {
if ($cle > 0) {
$urlFinal .= "&";
}
$t_coupleNomValeur = explode("=", $coupleNomValeur);
if ($t_coupleNomValeur[0] == $paramNom) {
$urlFinal .= $paramNom . "=" . $paramValeur;
} else {
$urlFinal .= $t_coupleNomValeur[0] . "=" . $t_coupleNomValeur[1];
}
}
}
}
}
return $urlFinal;
}
function print_r_net($array)
{
_xDumpVar($array);
}
/**
* Dump a var
*
* @access private
* @param mixed $data
* @return string
*/
function _xDumpVar($data)
{
$B_echo = true;
ob_start();
var_dump($data);
$c = ob_get_contents();
ob_end_clean();
$c = preg_replace("/\r\n|\r/", "\n", $c);
$c = str_replace("]=>\n", '] = ', $c);
$c = preg_replace('/= {2,}/', '= ', $c);
$c = preg_replace("/\[\"(.*?)\"\] = /i", "[$1] = ", $c);
$c = preg_replace('/ /', " ", $c);
$c = preg_replace("/\"\"(.*?)\"/i", "\"$1\"", $c);
$c = htmlspecialchars($c, ENT_NOQUOTES);
// Expand numbers (ie. int(2) 10 => int(1) 2 10, float(6) 128.64 => float(1) 6 128.64 etc.)
$c = preg_replace("/\(int|float\)\(([0-9\.]+)\)/ie", "'($1)('." . strlen('$2') . ".') <span class=\"number\">$2</span>'", $c);
// Syntax Highlighting of Strings. This seems cryptic, but it will also allow non-terminated strings to get parsed.
$c = preg_replace("/(\[[\w ]+\] = string\([0-9]+\) )\"(.*?)/sim", "$1<span class=\"string\">\"", $c);
$c = preg_replace("/(\"\n{1,})( {0,}\})/sim", "$1</span>$2", $c);
$c = preg_replace("/(\"\n{1,})( {0,}\[)/sim", "$1</span>$2", $c);
$c = preg_replace("/(string\([0-9]+\) )\"(.*?)\"\n/sim", "$1<span class=\"string\">\"$2\"</span>\n", $c);
$regex = array(// Numberrs
'numbers' => array('/(^|] = )(array|float|int|string|resource|object\(.*\)|\&object\(.*\))\(([0-9\.]+)\)/i', '$1$2(<span class="number">$3</span>)'),
// Keywords
'null' => array('/(^|] = )(null)/i', '$1<span class="keyword">$2</span>'),
'bool' => array('/(bool)\((true|false)\)/i', '$1(<span class="keyword">$2</span>)'),
// Types
'types' => array('/(of type )\((.*)\)/i', '$1(<span class="type">$2</span>)'),
// Objects
'object' => array('/(object|\&object)\(([\w]+)\)/i', '$1(<span class="object">$2</span>)'),
// Function
'function' => array('/(^|] = )(array|string|int|float|bool|resource|object|\&object)\(/i', '$1<span class="function">$2</span>('));
foreach ($regex as $x) {
$c = preg_replace($x[0], $x[1], $c);
}
$style = '
/* outside div - it will float and match the screen */
.dumpr {
margin: 2px;
padding: 2px;
background-color: #fbfbfb;
float: left;
clear: both;
}
/* font size and family */
.dumpr pre {
color: #000000;
text-align:left;
font-size: 9pt;
font-family: "Courier New",Courier,Monaco,monospace;
margin: 0px;
padding-top: 5px;
padding-bottom: 7px;
padding-left: 9px;
padding-right: 9px;
}
/* inside div */
.dumpr div {
background-color: #fcfcfc;
border: 1px solid #d9d9d9;
float: left;
clear: both;
}
/* syntax highlighting */
.dumpr span.string {color: #c40000;}
.dumpr span.number {color: #ff0000;}
.dumpr span.keyword {color: #007200;}
.dumpr span.function {color: #0000c4;}
.dumpr span.object {color: #ac00ac;}
.dumpr span.type {color: #0072c4;}
.legenddumpr {
background-color: #fcfcfc;
border: 1px solid #d9d9d9;
padding: 2px;
}
';
$style = preg_replace("/ {2,}/", "", $style);
$style = preg_replace("/\t|\r\n|\r|\n/", "", $style);
$style = preg_replace("/\/\*.*?\*\//i", '', $style);
$style = str_replace('}', '} ', $style);
$style = str_replace(' {', '{', $style);
$style = trim($style);
$c = trim($c);
$c = preg_replace("/\n<\/span>/", "</span>\n", $c);
$S_from = '';
// Nom du fichier appelant la fonction
$A_backTrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
if (is_array($A_backTrace) && array_key_exists(0, $A_backTrace)) {
$S_from = <<< BACKTRACE
{$A_backTrace[1]{'file'}}, ligne {$A_backTrace[1]{'line'}}
BACKTRACE;
} else {
$S_from = basename($_SERVER['SCRIPT_FILENAME']);
}
$S_out = '';
$S_out .= "<style type=\"text/css\">" . $style . "</style>\n";
$S_out .= '<fieldset class="dumpr">';
$S_out .= '<legend class="legenddumpr">' . $S_from . '</legend>';
$S_out .= '<pre>' . $c . '</pre>';
$S_out .= '</fieldset>';
$S_out .= "<div style=\"clear:both;\"> </div>";
if ($B_echo) {
echo $S_out;
} else {
return $S_out;
}
}