import mtweb.0.4.1
[mtweb] / web / libs / tiny_mce / plugins / tinybrowser / fns_tinybrowser.php
1 <?php\r
2 // *************************CREATE FOLDER**********************************\r
3 function createfolder($dir,$perm) {\r
4 is_dir(dirname($dir)) || createfolder(dirname($dir), $perm);\r
5     return is_dir($dir) || @mkdir($dir, $perm);\r
6 }\r
7 \r
8 // *************************VALIDATE FILE EXTENSIONS**********************************\r
9 function validateExtension($extension, $types) {\r
10 if(in_array($extension,$types)) return false; else return true;\r
11 }\r
12 \r
13 //*************************************Display Alert Notifications*********************************\r
14 function alert(&$notify){\r
15 $alert_num = count($notify['type']);\r
16 for($i=0;$i<$alert_num;$i++)\r
17         {\r
18         ?><div class="alert<?php echo $notify['type'][$i]; ?>"><?php echo $notify['message'][$i]; ?></div><br /><?php\r
19         }\r
20 }\r
21 \r
22 // *************************SORT FILE ARRAY BY SELECTED ORDER**********************************\r
23 function sortfileorder(&$sortbynow,&$sortorder,&$file) {\r
24 \r
25 switch($sortbynow) \r
26         {\r
27         case 'name':\r
28                 array_multisort($file['sortname'], $sortorder, $file['name'], $sortorder, $file['type'], $sortorder, $file['modified'], $sortorder, $file['size'], $sortorder, $file['dimensions'], $sortorder, $file['width'], $sortorder, $file['height'], $sortorder);\r
29                 break;\r
30         case 'size':\r
31                 array_multisort($file['size'], $sortorder, $file['sortname'], SORT_ASC, $file['name'], SORT_ASC, $file['type'], $sortorder, $file['modified'], $sortorder, $file['dimensions'], $sortorder, $file['width'], $sortorder, $file['height'], $sortorder);\r
32                 break;\r
33         case 'type':\r
34                 array_multisort($file['type'], $sortorder, $file['sortname'], SORT_ASC, $file['name'], SORT_ASC, $file['size'], $sortorder, $file['modified'], $sortorder, $file['dimensions'], $sortorder, $file['width'], $sortorder, $file['height'], $sortorder);\r
35                 break;\r
36         case 'modified':\r
37                 array_multisort($file['modified'], $sortorder, $file['name'], $sortorder, $file['name'], $sortorder, $file['type'], $sortorder, $file['size'], $sortorder, $file['dimensions'], $sortorder, $file['width'], $sortorder, $file['height'], $sortorder);\r
38                 break;\r
39         case 'dimensions':\r
40                 array_multisort($file['dimensions'], $sortorder, $file['width'], $sortorder, $file['sortname'], SORT_ASC, $file['name'], SORT_ASC, $file['modified'], $sortorder, $file['type'], $sortorder, $file['size'], $sortorder, $file['height'], $sortorder);\r
41                 break;\r
42         default:\r
43                 // do nothing\r
44         }\r
45 }\r
46 \r
47 // **************************RESIZE IMAGE TO GIVEN SIZE*****************************************\r
48 function resizeimage($im,$maxwidth,$maxheight,$urlandname,$comp,$imagetype){\r
49 $width = imagesx($im);\r
50 $height = imagesy($im);\r
51 if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight))\r
52         {\r
53         if($maxwidth && $width > $maxwidth)\r
54                 {\r
55                 $widthratio = $maxwidth/$width;\r
56                 $resizewidth=true;\r
57                 } \r
58         else $resizewidth=false;\r
59 \r
60         if($maxheight && $height > $maxheight)\r
61                 {\r
62                 $heightratio = $maxheight/$height;\r
63                 $resizeheight=true;\r
64                 } \r
65         else $resizeheight=false;\r
66 \r
67         if($resizewidth && $resizeheight)\r
68                 {\r
69                 if($widthratio < $heightratio) $ratio = $widthratio;\r
70                 else $ratio = $heightratio;\r
71                 }\r
72         elseif($resizewidth)\r
73                 {\r
74                 $ratio = $widthratio;\r
75                 }\r
76         elseif($resizeheight)\r
77                 {\r
78                 $ratio = $heightratio;\r
79                 }\r
80         $newwidth = $width * $ratio;\r
81         $newheight = $height * $ratio;\r
82                 if(function_exists('imagecopyresampled') && $imagetype !='image/gif')\r
83                 {\r
84                 $newim = imagecreatetruecolor($newwidth, $newheight);\r
85                 }\r
86         else\r
87                 {\r
88                 $newim = imagecreate($newwidth, $newheight);\r
89                 }\r
90 \r
91         // additional processing for png / gif transparencies (credit to Dirk Bohl)\r
92         if($imagetype == 'image/x-png' || $imagetype == 'image/png')\r
93                 {\r
94                 imagealphablending($newim, false);\r
95                 imagesavealpha($newim, true);\r
96                 }\r
97         elseif($imagetype == 'image/gif')\r
98                 {\r
99                 $originaltransparentcolor = imagecolortransparent( $im );\r
100                 if($originaltransparentcolor >= 0 && $originaltransparentcolor < imagecolorstotal( $im ))\r
101                         {\r
102                         $transparentcolor = imagecolorsforindex( $im, $originaltransparentcolor );\r
103                         $newtransparentcolor = imagecolorallocate($newim,$transparentcolor['red'],$transparentcolor['green'],$transparentcolor['blue']);\r
104                         imagefill( $newim, 0, 0, $newtransparentcolor );\r
105                         imagecolortransparent( $newim, $newtransparentcolor );\r
106                         }\r
107                 }\r
108 \r
109    imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);\r
110    \r
111    if($imagetype == 'image/pjpeg' || $imagetype == 'image/jpeg')\r
112         {\r
113         imagejpeg ($newim,$urlandname,$comp);\r
114         }\r
115    elseif($imagetype == 'image/x-png' || $imagetype == 'image/png')\r
116         {\r
117         imagepng ($newim,$urlandname,substr($comp,0,1));\r
118         }\r
119    elseif($imagetype == 'image/gif')\r
120         {\r
121         imagegif ($newim,$urlandname);\r
122         }\r
123         imagedestroy ($newim);\r
124         }\r
125 else\r
126         {\r
127    if($imagetype == 'image/pjpeg' || $imagetype == 'image/jpeg')\r
128         {\r
129         imagejpeg ($im,$urlandname,$comp);\r
130         }\r
131    elseif($imagetype == 'image/x-png' || $imagetype == 'image/png')\r
132         {\r
133         imagepng ($im,$urlandname,substr($comp,0,1));\r
134         }\r
135    elseif($imagetype == 'image/gif')\r
136         {\r
137         imagegif ($im,$urlandname);\r
138         }\r
139         }\r
140 }\r
141 \r
142 // **************************CHECK IMAGE TYPE AND CONVERT TO TEMP TYPE*****************************\r
143 function convert_image($imagetemp,$imagetype){\r
144 \r
145 if($imagetype == 'image/pjpeg' || $imagetype == 'image/jpeg')\r
146         {\r
147         $cim1 = imagecreatefromjpeg($imagetemp);\r
148         }\r
149 elseif($imagetype == 'image/x-png' || $imagetype == 'image/png')\r
150         {\r
151         $cim1 = imagecreatefrompng($imagetemp);\r
152         imagealphablending($cim1, false);\r
153         imagesavealpha($cim1, true);\r
154         }\r
155 elseif($imagetype == 'image/gif')\r
156         {\r
157         $cim1 = imagecreatefromgif($imagetemp);\r
158         }\r
159 return $cim1;\r
160 }\r
161 \r
162 // **************************GENERATE FORM OPEN*****************************\r
163 function form_open($name,$class,$url,$parameters){\r
164 ?><form name="<?php echo $name; ?>" class="<?php echo $class; ?>" method="post" action="<?php echo $url.$parameters; ?>">\r
165 <?php\r
166 }\r
167 \r
168 // **************************GENERATE FORM SELECT ELEMENT*****************************\r
169 function form_select($options,$name,$label,$current,$auto){\r
170 if ($label) {?><label for="<?php echo $name; ?>"><?php echo $label; ?></label><?php } \r
171 ?><select name="<?php echo $name; ?>" <?php if ($auto) {?>onchange="this.form.submit();"<?php }?>>\r
172 <?php\r
173 $loopnum = count($options); \r
174 for($i=0;$i<$loopnum;$i++)\r
175         {\r
176         $selected = ($options[$i][0] == $current ? ' selected' : ''); \r
177         echo '<option value="'.$options[$i][0].'"'.$selected.'>'.$options[$i][1].'</option>';\r
178         }\r
179 ?></select><?php\r
180 }\r
181 \r
182 // **************************GENERATE FORM HIDDEN ELEMENT*****************************\r
183 function form_hidden_input($name,$value) {\r
184 ?><input type="hidden" name="<?php echo $name; ?>" value="<?php echo $value; ?>" />\r
185 <?php\r
186 }\r
187 \r
188 // **************************GENERATE FORM TEXT ELEMENT*****************************\r
189 function form_text_input($name,$label,$value,$size,$maxlength) {\r
190 if ($label) {?><label for="<?php echo $name; ?>"><?php echo $label; ?></label><?php } ?>\r
191 <input type="text" name="<?php echo $name; ?>" size="<?php echo $size; ?>" maxlength="<?php echo $maxlength; ?>" value="<?php echo $value; ?>" /><?php\r
192 }\r
193 \r
194 // **************************GENERATE FORM SUBMIT BUTTON*****************************\r
195 function form_submit_button($name,$label,$class) {\r
196 ?><button <?php if ($class) {?>class="<?php echo $class; ?>"<?php } ?>type="submit" name="<?php echo $name; ?>"><?php echo $label; ?></button>\r
197 </form>\r
198 <?php\r
199 }\r
200 \r
201 //********************************Returns True if Number is Odd**************************************\r
202 function IsOdd($num)\r
203 {\r
204 return (1 - ($num & 1));\r
205 }\r
206 \r
207 //********************************Truncate Text to Given Length If Required***************************\r
208 function truncate_text($textstring,$length){\r
209         if (strlen($textstring) > $length)\r
210                 {\r
211                 $textstring = substr($textstring,0,$length).'...';\r
212                 }\r
213         return $textstring;\r
214 }\r
215 \r
216 /**\r
217  * Present a size (in bytes) as a human-readable value\r
218  * \r
219  * @param int    $size        size (in bytes)\r
220  * @param int    $precision    number of digits after the decimal point\r
221  * @return string\r
222  */\r
223 function bytestostring($size, $precision = 0) {\r
224     $sizes = array('YB', 'ZB', 'EB', 'PB', 'TB', 'GB', 'MB', 'KB', 'B');\r
225     $total = count($sizes);\r
226 \r
227     while($total-- && $size > 1024) $size /= 1024;\r
228     return round($size, $precision).' '.$sizes[$total];\r
229 }\r
230 \r
231 //function to clean a filename string so it is a valid filename\r
232 function clean_filename($filename){\r
233     $filename = preg_replace('/^\W+|\W+$/', '', $filename); // remove all non-alphanumeric chars at begin & end of string\r
234     $filename = preg_replace('/\s+/', '_', $filename); // compress internal whitespace and replace with _\r
235     return strtolower(preg_replace('/\W-/', '', $filename)); // remove all non-alphanumeric chars except _ and -\r
236 \r
237 }\r
238 \r
239 //********************************Return File MIME Type***************************\r
240 function returnMIMEType($filename)\r
241     {\r
242         preg_match("|\.([a-z0-9]{2,4})$|i", $filename, $fileSuffix);\r
243 \r
244         switch(strtolower($fileSuffix[1]))\r
245         {\r
246             case 'js' :\r
247                 return 'application/x-javascript';\r
248 \r
249             case 'json' :\r
250                 return 'application/json';\r
251 \r
252             case 'jpg' :\r
253             case 'jpeg' :\r
254             case 'jpe' :\r
255                 return 'image/jpg';\r
256 \r
257             case 'png' :\r
258             case 'gif' :\r
259             case 'bmp' :\r
260             case 'tiff' :\r
261                 return 'image/'.strtolower($fileSuffix[1]);\r
262 \r
263             case 'css' :\r
264                 return 'text/css';\r
265 \r
266             case 'xml' :\r
267                 return 'application/xml';\r
268 \r
269             case 'doc' :\r
270             case 'docx' :\r
271                 return 'application/msword';\r
272 \r
273             case 'xls' :\r
274             case 'xlt' :\r
275             case 'xlm' :\r
276             case 'xld' :\r
277             case 'xla' :\r
278             case 'xlc' :\r
279             case 'xlw' :\r
280             case 'xll' :\r
281                 return 'application/vnd.ms-excel';\r
282 \r
283             case 'ppt' :\r
284             case 'pps' :\r
285                 return 'application/vnd.ms-powerpoint';\r
286 \r
287             case 'rtf' :\r
288                 return 'application/rtf';\r
289 \r
290             case 'pdf' :\r
291                 return 'application/pdf';\r
292 \r
293             case 'html' :\r
294             case 'htm' :\r
295             case 'php' :\r
296                 return 'text/html';\r
297 \r
298             case 'txt' :\r
299                 return 'text/plain';\r
300 \r
301             case 'mpeg' :\r
302             case 'mpg' :\r
303             case 'mpe' :\r
304                 return 'video/mpeg';\r
305 \r
306             case 'mp3' :\r
307                 return 'audio/mpeg3';\r
308 \r
309             case 'wav' :\r
310                 return 'audio/wav';\r
311 \r
312             case 'aiff' :\r
313             case 'aif' :\r
314                 return 'audio/aiff';\r
315 \r
316             case 'avi' :\r
317                 return 'video/msvideo';\r
318 \r
319             case 'wmv' :\r
320                 return 'video/x-ms-wmv';\r
321 \r
322             case 'mov' :\r
323                 return 'video/quicktime';\r
324 \r
325             case 'zip' :\r
326                 return 'application/zip';\r
327 \r
328             case 'tar' :\r
329                 return 'application/x-tar';\r
330 \r
331             case 'swf' :\r
332                 return 'application/x-shockwave-flash';\r
333 \r
334             default :\r
335             if(function_exists('mime_content_type'))\r
336             {\r
337                 $fileSuffix = mime_content_type($filename);\r
338             }\r
339 \r
340             return 'unknown/' . trim($fileSuffix[0], '.');\r
341         }\r
342     }\r
343 \r
344 //************************Return Array of Directory Structure***************************\r
345 function dirtree(&$alldirs,$types='*.*',$root='',$tree='',$branch='',$level=0) {\r
346 \r
347 // filter file types according to type\r
348 $filetypes = explode(',',preg_replace('{[ \t]+}', '',$types));\r
349 \r
350 if($level==0 && is_dir($root.$tree.$branch))\r
351         {\r
352         $filenum=0;\r
353         foreach($filetypes as $filetype)\r
354            {\r
355         $filenum = $filenum + count(glob($root.$tree.$branch.sql_regcase($filetype),GLOB_NOSORT));\r
356         }\r
357    $treeparts = explode('/',rtrim($tree,'/'));\r
358         $topname = end($treeparts);\r
359         $alldirs[] = array($branch,rtrim($topname,'/').' ('.$filenum.')',rtrim($topname,'/'),rtrim($topname,'/'),$filenum,filemtime($root.$tree.$branch));\r
360         }\r
361 $level++;\r
362 \r
363 $dh = opendir($root.$tree.$branch);\r
364 while (($dirname = readdir($dh)) !== false)\r
365         {\r
366         if($dirname != '.' && $dirname != '..' && is_dir($root.$tree.$branch.$dirname) && $dirname != '_thumbs')\r
367                 {\r
368                 $filenum=0;\r
369                 foreach($filetypes as $filetype)\r
370                    {\r
371                         $filenum = $filenum + count(glob($root.$tree.$branch.$dirname.'/'.sql_regcase($filetype),GLOB_NOSORT));\r
372                         }\r
373                 $indent = '';\r
374                 for($i=0;$i<$level;$i++) { $indent .= ' &nbsp; '; }\r
375       if(strlen($indent)>0) $indent .= '&rarr; ';\r
376                 $alldirs[] = array(urlencode($branch.$dirname.'/'),$indent.$dirname.' ('.$filenum.')',$indent.$dirname,$dirname,$filenum,filemtime($root.$tree.$branch.$dirname));\r
377                 dirtree($alldirs,$types,$root,$tree,$branch.$dirname.'/',$level);\r
378                 }\r
379         }\r
380 closedir($dh);\r
381 $level--;\r
382 }\r
383 \r
384 /* user defined error handling function. */\r
385 function userErrorHandler($errno, $errmsg, $filename, $linenum, $vars)\r
386 {\r
387     // timestamp for the error entry.\r
388     $dt = date('Y-m-d H:i:s (T)');\r
389 \r
390     // define an assoc array of error string\r
391     // in reality the only entries we should\r
392     // consider are E_WARNING, E_NOTICE, E_USER_ERROR,\r
393     // E_USER_WARNING and E_USER_NOTICE.\r
394     $errortype = array (\r
395                 E_ERROR => 'Error',\r
396                 E_WARNING => 'Warning',\r
397                 E_PARSE => 'Parsing Error',\r
398                 E_NOTICE => 'Notice',\r
399                 E_CORE_ERROR => 'Core Error',\r
400                 E_CORE_WARNING => 'Core Warning',\r
401                 E_COMPILE_ERROR => 'Compile Error',\r
402                 E_COMPILE_WARNING => 'Compile Warning',\r
403                 E_USER_ERROR => 'User Error',\r
404                 E_USER_WARNING => 'User Warning',\r
405                 E_USER_NOTICE => 'User Notice',\r
406                 E_STRICT => 'Runtime Notice'\r
407                 );\r
408     // set of errors for which a var trace will be saved.\r
409     $user_errors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE);\r
410 \r
411          if($errno != E_STRICT) // exclude Runtime Notices\r
412                 {\r
413                 $err  = $dt. "\t";\r
414         $err .= $errno.' '.$errortype[$errno]. "\t";\r
415         $err .= $errmsg. "\t";\r
416         $err .= 'File: '.basename($filename). "\t";\r
417         $err .= 'Line: '.$linenum. "\t";\r
418 \r
419         if (in_array($errno, $user_errors))\r
420                         {\r
421                 $err .= 'Trace: '.wddx_serialize_value($vars, 'Variables'). "\t";\r
422                 }\r
423         $err .= "\n";\r
424 \r
425            // save to the error log file, and e-mail me if there is a critical user error.\r
426            error_log($err, 3, 'error.log');\r
427            }\r
428 }\r
429 $old_error_handler = set_error_handler('userErrorHandler');\r
430 \r
431 ?>\r