PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/backend/core/js/ckfinder/core/connector/php/php5/Utils/Misc.php

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