PageRenderTime 23ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/184.168.182.1/admin/old/ckfinder/core/connector/php/php4/Utils/Misc.php

https://gitlab.com/endomorphosis/falkenstein
PHP | 401 lines | 359 code | 14 blank | 28 comment | 32 complexity | 15dfea143e40b75d81402368df00977f MD5 | raw file
  1. <?php
  2. /*
  3. * CKFinder
  4. * ========
  5. * http://ckfinder.com
  6. * Copyright (C) 2007-2012, CKSource - Frederico Knabben. All rights reserved.
  7. *
  8. * The software, this file and its contents are subject to the CKFinder
  9. * License. Please read the license.txt file before using, installing, copying,
  10. * modifying or distribute this file or part of its contents. The contents of
  11. * this file is part of the Source Code of CKFinder.
  12. */
  13. if (!defined('IN_CKFINDER')) exit;
  14. /**
  15. * @package CKFinder
  16. * @subpackage Utils
  17. * @copyright CKSource - Frederico Knabben
  18. */
  19. /**
  20. * @package CKFinder
  21. * @subpackage Utils
  22. * @copyright CKSource - Frederico Knabben
  23. */
  24. class CKFinder_Connector_Utils_Misc
  25. {
  26. function getErrorMessage($number, $arg = "") {
  27. $langCode = 'en';
  28. if (!empty($_GET['langCode']) && preg_match("/^[a-z\-]+$/", $_GET['langCode'])) {
  29. if (file_exists(CKFINDER_CONNECTOR_LANG_PATH . "/" . $_GET['langCode'] . ".php"))
  30. $langCode = $_GET['langCode'];
  31. }
  32. include CKFINDER_CONNECTOR_LANG_PATH . "/" . $langCode . ".php";
  33. if ($number) {
  34. if (!empty ($GLOBALS['CKFLang']['Errors'][$number])) {
  35. $errorMessage = str_replace("%1", $arg, $GLOBALS['CKFLang']['Errors'][$number]);
  36. } else {
  37. $errorMessage = str_replace("%1", $number, $GLOBALS['CKFLang']['ErrorUnknown']);
  38. }
  39. } else {
  40. $errorMessage = "";
  41. }
  42. return $errorMessage;
  43. }
  44. /**
  45. * Simulate the encodeURIComponent() function available in JavaScript
  46. * @static
  47. * @access public
  48. * @param string $str
  49. * @return string
  50. */
  51. function encodeURIComponent($str)
  52. {
  53. $revert = array('%21'=>'!', '%2A'=>'*', '%27'=>"'", '%28'=>'(', '%29'=>')');
  54. return strtr(rawurlencode($str), $revert);
  55. }
  56. /**
  57. * Convert any value to boolean, strings like "false", "FalSE" and "off" are also considered as false
  58. *
  59. * @static
  60. * @access public
  61. * @param mixed $value
  62. * @return boolean
  63. */
  64. function booleanValue($value)
  65. {
  66. if (strcasecmp("false", $value) == 0 || strcasecmp("off", $value) == 0 || !$value) {
  67. return false;
  68. } else {
  69. return true;
  70. }
  71. }
  72. /**
  73. * @link http://pl.php.net/manual/en/function.imagecopyresampled.php
  74. * replacement to imagecopyresampled that will deliver results that are almost identical except MUCH faster (very typically 30 times faster)
  75. *
  76. * @static
  77. * @access public
  78. * @param string $dst_image
  79. * @param string $src_image
  80. * @param int $dst_x
  81. * @param int $dst_y
  82. * @param int $src_x
  83. * @param int $src_y
  84. * @param int $dst_w
  85. * @param int $dst_h
  86. * @param int $src_w
  87. * @param int $src_h
  88. * @param int $quality
  89. * @return boolean
  90. */
  91. function fastImageCopyResampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 3)
  92. {
  93. if (empty($src_image) || empty($dst_image)) {
  94. return false;
  95. }
  96. if ($quality <= 1) {
  97. $temp = imagecreatetruecolor ($dst_w + 1, $dst_h + 1);
  98. imagecopyresized ($temp, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w + 1, $dst_h + 1, $src_w, $src_h);
  99. imagecopyresized ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $dst_w, $dst_h);
  100. imagedestroy ($temp);
  101. } elseif ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
  102. $tmp_w = $dst_w * $quality;
  103. $tmp_h = $dst_h * $quality;
  104. $temp = imagecreatetruecolor ($tmp_w + 1, $tmp_h + 1);
  105. imagecopyresized ($temp, $src_image, 0, 0, $src_x, $src_y, $tmp_w + 1, $tmp_h + 1, $src_w, $src_h);
  106. imagecopyresampled ($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h);
  107. imagedestroy ($temp);
  108. } else {
  109. imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
  110. }
  111. return true;
  112. }
  113. /**
  114. * @link http://pl.php.net/manual/pl/function.imagecreatefromjpeg.php
  115. * function posted by e dot a dot schultz at gmail dot com
  116. *
  117. * @static
  118. * @access public
  119. * @param string $filename
  120. * @return boolean
  121. */
  122. function setMemoryForImage($imageWidth, $imageHeight, $imageBits, $imageChannels)
  123. {
  124. $MB = 1048576; // number of bytes in 1M
  125. $K64 = 65536; // number of bytes in 64K
  126. $TWEAKFACTOR = 2.4; // Or whatever works for you
  127. $memoryNeeded = round( ( $imageWidth * $imageHeight
  128. * $imageBits
  129. * $imageChannels / 8
  130. + $K64
  131. ) * $TWEAKFACTOR
  132. ) + 3*$MB;
  133. //ini_get('memory_limit') only works if compiled with "--enable-memory-limit" also
  134. //Default memory limit is 8MB so well stick with that.
  135. //To find out what yours is, view your php.ini file.
  136. $memoryLimit = CKFinder_Connector_Utils_Misc::returnBytes(@ini_get('memory_limit'))/$MB;
  137. // There are no memory limits, nothing to do
  138. if ($memoryLimit == -1) {
  139. return true;
  140. }
  141. if (!$memoryLimit) {
  142. $memoryLimit = 8;
  143. }
  144. $memoryLimitMB = $memoryLimit * $MB;
  145. if (function_exists('memory_get_usage')) {
  146. if (memory_get_usage() + $memoryNeeded > $memoryLimitMB) {
  147. $newLimit = $memoryLimit + ceil( ( memory_get_usage()
  148. + $memoryNeeded
  149. - $memoryLimitMB
  150. ) / $MB
  151. );
  152. if (@ini_set( 'memory_limit', $newLimit . 'M' ) === false) {
  153. return false;
  154. }
  155. }
  156. } else {
  157. if ($memoryNeeded + 3*$MB > $memoryLimitMB) {
  158. $newLimit = $memoryLimit + ceil(( 3*$MB
  159. + $memoryNeeded
  160. - $memoryLimitMB
  161. ) / $MB
  162. );
  163. if (false === @ini_set( 'memory_limit', $newLimit . 'M' )) {
  164. return false;
  165. }
  166. }
  167. }
  168. return true;
  169. }
  170. /**
  171. * convert shorthand php.ini notation into bytes, much like how the PHP source does it
  172. * @link http://pl.php.net/manual/en/function.ini-get.php
  173. *
  174. * @static
  175. * @access public
  176. * @param string $val
  177. * @return int
  178. */
  179. function returnBytes($val)
  180. {
  181. $val = trim($val);
  182. if (!$val) {
  183. return 0;
  184. }
  185. $last = strtolower($val[strlen($val)-1]);
  186. switch($last) {
  187. // The 'G' modifier is available since PHP 5.1.0
  188. case 'g':
  189. $val *= 1024;
  190. case 'm':
  191. $val *= 1024;
  192. case 'k':
  193. $val *= 1024;
  194. }
  195. return $val;
  196. }
  197. /**
  198. * Checks if a value exists in an array (case insensitive)
  199. *
  200. * @static
  201. * @access public
  202. * @param string $needle
  203. * @param array $haystack
  204. * @return boolean
  205. */
  206. function inArrayCaseInsensitive($needle, $haystack)
  207. {
  208. if (!$haystack || !is_array($haystack)) {
  209. return false;
  210. }
  211. $lcase = array();
  212. foreach ($haystack as $key => $val) {
  213. $lcase[$key] = strtolower($val);
  214. }
  215. return in_array($needle, $lcase);
  216. }
  217. /**
  218. * UTF-8 compatible version of basename()
  219. *
  220. * @static
  221. * @access public
  222. * @param string $file
  223. * @return string
  224. */
  225. function mbBasename($file)
  226. {
  227. $explode = explode('/', str_replace("\\", "/", $file));
  228. return end($explode);
  229. }
  230. /**
  231. * Checks whether the string is valid UTF8
  232. * @static
  233. * @access public
  234. * @param string $string
  235. * @return boolean
  236. */
  237. function isValidUTF8($string)
  238. {
  239. if (strlen($string) == 0) {
  240. return true;
  241. }
  242. return (preg_match('/^./us', $string) == 1);
  243. }
  244. /**
  245. * Source: http://pl.php.net/imagecreate
  246. * (optimized for speed and memory usage, but yet not very efficient)
  247. *
  248. * @static
  249. * @access public
  250. * @param string $filename
  251. * @return resource
  252. */
  253. function imageCreateFromBmp($filename)
  254. {
  255. //20 seconds seems to be a reasonable value to not kill a server and process images up to 1680x1050
  256. @set_time_limit(20);
  257. if (false === ($f1 = fopen($filename, "rb"))) {
  258. return false;
  259. }
  260. $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1, 14));
  261. if ($FILE['file_type'] != 19778) {
  262. return false;
  263. }
  264. $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
  265. '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
  266. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1, 40));
  267. $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
  268. if ($BMP['size_bitmap'] == 0) {
  269. $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
  270. }
  271. $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
  272. $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
  273. $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
  274. $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
  275. $BMP['decal'] = 4-(4*$BMP['decal']);
  276. if ($BMP['decal'] == 4) {
  277. $BMP['decal'] = 0;
  278. }
  279. $PALETTE = array();
  280. if ($BMP['colors'] < 16777216) {
  281. $PALETTE = unpack('V'.$BMP['colors'], fread($f1, $BMP['colors']*4));
  282. }
  283. //2048x1536px@24bit don't even try to process larger files as it will probably fail
  284. if ($BMP['size_bitmap'] > 3 * 2048 * 1536) {
  285. return false;
  286. }
  287. $IMG = fread($f1, $BMP['size_bitmap']);
  288. fclose($f1);
  289. $VIDE = chr(0);
  290. $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
  291. $P = 0;
  292. $Y = $BMP['height']-1;
  293. $line_length = $BMP['bytes_per_pixel']*$BMP['width'];
  294. if ($BMP['bits_per_pixel'] == 24) {
  295. while ($Y >= 0)
  296. {
  297. $X=0;
  298. $temp = unpack( "C*", substr($IMG, $P, $line_length));
  299. while ($X < $BMP['width'])
  300. {
  301. $offset = $X*3;
  302. imagesetpixel($res, $X++, $Y, ($temp[$offset+3] << 16) + ($temp[$offset+2] << 8) + $temp[$offset+1]);
  303. }
  304. $Y--;
  305. $P += $line_length + $BMP['decal'];
  306. }
  307. }
  308. elseif ($BMP['bits_per_pixel'] == 8)
  309. {
  310. while ($Y >= 0)
  311. {
  312. $X=0;
  313. $temp = unpack( "C*", substr($IMG, $P, $line_length));
  314. while ($X < $BMP['width'])
  315. {
  316. imagesetpixel($res, $X++, $Y, $PALETTE[$temp[$X] +1]);
  317. }
  318. $Y--;
  319. $P += $line_length + $BMP['decal'];
  320. }
  321. }
  322. elseif ($BMP['bits_per_pixel'] == 4)
  323. {
  324. while ($Y >= 0)
  325. {
  326. $X=0;
  327. $i = 1;
  328. $low = true;
  329. $temp = unpack( "C*", substr($IMG, $P, $line_length));
  330. while ($X < $BMP['width'])
  331. {
  332. if ($low) {
  333. $index = $temp[$i] >> 4;
  334. }
  335. else {
  336. $index = $temp[$i++] & 0x0F;
  337. }
  338. $low = !$low;
  339. imagesetpixel($res, $X++, $Y, $PALETTE[$index +1]);
  340. }
  341. $Y--;
  342. $P += $line_length + $BMP['decal'];
  343. }
  344. }
  345. elseif ($BMP['bits_per_pixel'] == 1)
  346. {
  347. $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
  348. if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
  349. elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
  350. elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
  351. elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
  352. elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
  353. elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
  354. elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
  355. elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
  356. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  357. }
  358. else {
  359. return false;
  360. }
  361. return $res;
  362. }
  363. }