PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/www/ckfinder/core/connector/php/php4/Utils/Misc.php

https://bitbucket.org/wayfarer/verse
PHP | 367 lines | 233 code | 36 blank | 98 comment | 61 complexity | 75412509d93553a5e4fdfd6e1efeba93 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /*
  3. * CKFinder
  4. * ========
  5. * http://www.ckfinder.com
  6. * Copyright (C) 2007-2008 Frederico Caldeira Knabben (FredCK.com)
  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. /**
  14. * @package CKFinder
  15. * @subpackage Utils
  16. * @copyright Frederico Caldeira Knabben
  17. */
  18. /**
  19. * @package CKFinder
  20. * @subpackage Utils
  21. * @copyright Frederico Caldeira Knabben
  22. */
  23. class CKFinder_Connector_Utils_Misc
  24. {
  25. function getErrorMessage($number, $arg = "") {
  26. $langCode = 'en';
  27. if (!empty($_GET['langCode']) && preg_match("/^[a-z\-]+$/", $_GET['langCode'])) {
  28. if (file_exists(CKFINDER_CONNECTOR_LANG_PATH . "/" . $_GET['langCode'] . ".php"))
  29. $langCode = $_GET['langCode'];
  30. }
  31. include CKFINDER_CONNECTOR_LANG_PATH . "/" . $langCode . ".php";
  32. if ($number) {
  33. if (!empty ($GLOBALS['CKFLang']['Errors'][$number])) {
  34. $errorMessage = str_replace("%1", $arg, $GLOBALS['CKFLang']['Errors'][$number]);
  35. } else {
  36. $errorMessage = str_replace("%1", $number, $GLOBALS['CKFLang']['ErrorUnknown']);
  37. }
  38. } else {
  39. $errorMessage = "";
  40. }
  41. return $errorMessage;
  42. }
  43. /**
  44. * Convert any value to boolean, strings like "false", "FalSE" and "off" are also considered as false
  45. *
  46. * @static
  47. * @access public
  48. * @param mixed $value
  49. * @return boolean
  50. */
  51. function booleanValue($value)
  52. {
  53. if (strcasecmp("false", $value) == 0 || strcasecmp("off", $value) == 0 || !$value) {
  54. return false;
  55. } else {
  56. return true;
  57. }
  58. }
  59. /**
  60. * @link http://pl.php.net/manual/en/function.imagecopyresampled.php
  61. * replacement to imagecopyresampled that will deliver results that are almost identical except MUCH faster (very typically 30 times faster)
  62. *
  63. * @static
  64. * @access public
  65. * @param string $dst_image
  66. * @param string $src_image
  67. * @param int $dst_x
  68. * @param int $dst_y
  69. * @param int $src_x
  70. * @param int $src_y
  71. * @param int $dst_w
  72. * @param int $dst_h
  73. * @param int $src_w
  74. * @param int $src_h
  75. * @param int $quality
  76. * @return boolean
  77. */
  78. function fastImageCopyResampled (&$dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $quality = 3)
  79. {
  80. if (empty($src_image) || empty($dst_image)) {
  81. return false;
  82. }
  83. if ($quality <= 1) {
  84. $temp = imagecreatetruecolor ($dst_w + 1, $dst_h + 1);
  85. imagecopyresized ($temp, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w + 1, $dst_h + 1, $src_w, $src_h);
  86. imagecopyresized ($dst_image, $temp, 0, 0, 0, 0, $dst_w, $dst_h, $dst_w, $dst_h);
  87. imagedestroy ($temp);
  88. } elseif ($quality < 5 && (($dst_w * $quality) < $src_w || ($dst_h * $quality) < $src_h)) {
  89. $tmp_w = $dst_w * $quality;
  90. $tmp_h = $dst_h * $quality;
  91. $temp = imagecreatetruecolor ($tmp_w + 1, $tmp_h + 1);
  92. imagecopyresized ($temp, $src_image, 0, 0, $src_x, $src_y, $tmp_w + 1, $tmp_h + 1, $src_w, $src_h);
  93. imagecopyresampled ($dst_image, $temp, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h, $tmp_w, $tmp_h);
  94. imagedestroy ($temp);
  95. } else {
  96. imagecopyresampled ($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
  97. }
  98. return true;
  99. }
  100. /**
  101. * @link http://pl.php.net/manual/pl/function.imagecreatefromjpeg.php
  102. * function posted by e dot a dot schultz at gmail dot com
  103. *
  104. * @static
  105. * @access public
  106. * @param string $filename
  107. * @return boolean
  108. */
  109. function setMemoryForImage($imageWidth, $imageHeight, $imageBits, $imageChannels)
  110. {
  111. $MB = 1048576; // number of bytes in 1M
  112. $K64 = 65536; // number of bytes in 64K
  113. $TWEAKFACTOR = 2.4; // Or whatever works for you
  114. $memoryNeeded = round( ( $imageWidth * $imageHeight
  115. * $imageBits
  116. * $imageChannels / 8
  117. + $K64
  118. ) * $TWEAKFACTOR
  119. ) + 3*$MB;
  120. //ini_get('memory_limit') only works if compiled with "--enable-memory-limit" also
  121. //Default memory limit is 8MB so well stick with that.
  122. //To find out what yours is, view your php.ini file.
  123. $memoryLimit = CKFinder_Connector_Utils_Misc::returnBytes(@ini_get('memory_limit'))/$MB;
  124. if (!$memoryLimit) {
  125. $memoryLimit = 8;
  126. }
  127. $memoryLimitMB = $memoryLimit * $MB;
  128. if (function_exists('memory_get_usage')) {
  129. if (memory_get_usage() + $memoryNeeded > $memoryLimitMB) {
  130. $newLimit = $memoryLimit + ceil( ( memory_get_usage()
  131. + $memoryNeeded
  132. - $memoryLimitMB
  133. ) / $MB
  134. );
  135. if (@ini_set( 'memory_limit', $newLimit . 'M' ) === false) {
  136. return false;
  137. }
  138. }
  139. } else {
  140. if ($memoryNeeded + 3*$MB > $memoryLimitMB) {
  141. $newLimit = $memoryLimit + ceil(( 3*$MB
  142. + $memoryNeeded
  143. - $memoryLimitMB
  144. ) / $MB
  145. );
  146. if (false === @ini_set( 'memory_limit', $newLimit . 'M' )) {
  147. return false;
  148. }
  149. }
  150. }
  151. return true;
  152. }
  153. /**
  154. * convert shorthand php.ini notation into bytes, much like how the PHP source does it
  155. * @link http://pl.php.net/manual/en/function.ini-get.php
  156. *
  157. * @static
  158. * @access public
  159. * @param string $val
  160. * @return int
  161. */
  162. function returnBytes($val)
  163. {
  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. 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. function mbBasename($file)
  209. {
  210. return end(explode('/', str_replace("\\", "/", $file)));
  211. }
  212. /**
  213. * Source: http://pl.php.net/imagecreate
  214. * (optimized for speed and memory usage, but yet not very efficient)
  215. *
  216. * @static
  217. * @access public
  218. * @param string $filename
  219. * @return resource
  220. */
  221. function imageCreateFromBmp($filename)
  222. {
  223. //20 seconds seems to be a reasonable value to not kill a server and process images up to 1680x1050
  224. @set_time_limit(20);
  225. if (false === ($f1 = fopen($filename, "rb"))) {
  226. return false;
  227. }
  228. $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1, 14));
  229. if ($FILE['file_type'] != 19778) {
  230. return false;
  231. }
  232. $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
  233. '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
  234. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1, 40));
  235. $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
  236. if ($BMP['size_bitmap'] == 0) {
  237. $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
  238. }
  239. $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
  240. $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
  241. $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
  242. $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
  243. $BMP['decal'] = 4-(4*$BMP['decal']);
  244. if ($BMP['decal'] == 4) {
  245. $BMP['decal'] = 0;
  246. }
  247. $PALETTE = array();
  248. if ($BMP['colors'] < 16777216) {
  249. $PALETTE = unpack('V'.$BMP['colors'], fread($f1, $BMP['colors']*4));
  250. }
  251. //2048x1536px@24bit don't even try to process larger files as it will probably fail
  252. if ($BMP['size_bitmap'] > 3 * 2048 * 1536) {
  253. return false;
  254. }
  255. $IMG = fread($f1, $BMP['size_bitmap']);
  256. fclose($f1);
  257. $VIDE = chr(0);
  258. $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
  259. $P = 0;
  260. $Y = $BMP['height']-1;
  261. $line_length = $BMP['bytes_per_pixel']*$BMP['width'];
  262. if ($BMP['bits_per_pixel'] == 24) {
  263. while ($Y >= 0)
  264. {
  265. $X=0;
  266. $temp = unpack( "C*", substr($IMG, $P, $line_length));
  267. while ($X < $BMP['width'])
  268. {
  269. $offset = $X*3;
  270. imagesetpixel($res, $X++, $Y, ($temp[$offset+3] << 16) + ($temp[$offset+2] << 8) + $temp[$offset+1]);
  271. }
  272. $Y--;
  273. $P += $line_length + $BMP['decal'];
  274. }
  275. }
  276. elseif ($BMP['bits_per_pixel'] == 8)
  277. {
  278. while ($Y >= 0)
  279. {
  280. $X=0;
  281. $temp = unpack( "C*", substr($IMG, $P, $line_length));
  282. while ($X < $BMP['width'])
  283. {
  284. imagesetpixel($res, $X++, $Y, $PALETTE[$temp[$X] +1]);
  285. }
  286. $Y--;
  287. $P += $line_length + $BMP['decal'];
  288. }
  289. }
  290. elseif ($BMP['bits_per_pixel'] == 4)
  291. {
  292. while ($Y >= 0)
  293. {
  294. $X=0;
  295. $i = 1;
  296. $low = true;
  297. $temp = unpack( "C*", substr($IMG, $P, $line_length));
  298. while ($X < $BMP['width'])
  299. {
  300. if ($low) {
  301. $index = $temp[$i] >> 4;
  302. }
  303. else {
  304. $index = $temp[$i++] & 0x0F;
  305. }
  306. $low = !$low;
  307. imagesetpixel($res, $X++, $Y, $PALETTE[$index +1]);
  308. }
  309. $Y--;
  310. $P += $line_length + $BMP['decal'];
  311. }
  312. }
  313. elseif ($BMP['bits_per_pixel'] == 1)
  314. {
  315. $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
  316. if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
  317. elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
  318. elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
  319. elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
  320. elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
  321. elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
  322. elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
  323. elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
  324. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  325. }
  326. else {
  327. return false;
  328. }
  329. return $res;
  330. }
  331. }