PageRenderTime 63ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/usr/editors/ckfinder/core/connector/php/php5/Utils/Misc.php

https://github.com/Doap/xoops
PHP | 369 lines | 234 code | 37 blank | 98 comment | 62 complexity | be3c7842fb75955f8da8bcc64aa6efd8 MD5 | raw file
  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. * Source: http://pl.php.net/imagecreate
  215. * (optimized for speed and memory usage, but yet not very efficient)
  216. *
  217. * @static
  218. * @access public
  219. * @param string $filename
  220. * @return resource
  221. */
  222. public static function imageCreateFromBmp($filename)
  223. {
  224. //20 seconds seems to be a reasonable value to not kill a server and process images up to 1680x1050
  225. @set_time_limit(20);
  226. if (false === ($f1 = fopen($filename, "rb"))) {
  227. return false;
  228. }
  229. $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1, 14));
  230. if ($FILE['file_type'] != 19778) {
  231. return false;
  232. }
  233. $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
  234. '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
  235. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1, 40));
  236. $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
  237. if ($BMP['size_bitmap'] == 0) {
  238. $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
  239. }
  240. $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
  241. $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
  242. $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
  243. $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
  244. $BMP['decal'] = 4-(4*$BMP['decal']);
  245. if ($BMP['decal'] == 4) {
  246. $BMP['decal'] = 0;
  247. }
  248. $PALETTE = array();
  249. if ($BMP['colors'] < 16777216) {
  250. $PALETTE = unpack('V'.$BMP['colors'], fread($f1, $BMP['colors']*4));
  251. }
  252. //2048x1536px@24bit don't even try to process larger files as it will probably fail
  253. if ($BMP['size_bitmap'] > 3 * 2048 * 1536) {
  254. return false;
  255. }
  256. $IMG = fread($f1, $BMP['size_bitmap']);
  257. fclose($f1);
  258. $VIDE = chr(0);
  259. $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
  260. $P = 0;
  261. $Y = $BMP['height']-1;
  262. $line_length = $BMP['bytes_per_pixel']*$BMP['width'];
  263. if ($BMP['bits_per_pixel'] == 24) {
  264. while ($Y >= 0)
  265. {
  266. $X=0;
  267. $temp = unpack( "C*", substr($IMG, $P, $line_length));
  268. while ($X < $BMP['width'])
  269. {
  270. $offset = $X*3;
  271. imagesetpixel($res, $X++, $Y, ($temp[$offset+3] << 16) + ($temp[$offset+2] << 8) + $temp[$offset+1]);
  272. }
  273. $Y--;
  274. $P += $line_length + $BMP['decal'];
  275. }
  276. }
  277. elseif ($BMP['bits_per_pixel'] == 8)
  278. {
  279. while ($Y >= 0)
  280. {
  281. $X=0;
  282. $temp = unpack( "C*", substr($IMG, $P, $line_length));
  283. while ($X < $BMP['width'])
  284. {
  285. imagesetpixel($res, $X++, $Y, $PALETTE[$temp[$X] +1]);
  286. }
  287. $Y--;
  288. $P += $line_length + $BMP['decal'];
  289. }
  290. }
  291. elseif ($BMP['bits_per_pixel'] == 4)
  292. {
  293. while ($Y >= 0)
  294. {
  295. $X=0;
  296. $i = 1;
  297. $low = true;
  298. $temp = unpack( "C*", substr($IMG, $P, $line_length));
  299. while ($X < $BMP['width'])
  300. {
  301. if ($low) {
  302. $index = $temp[$i] >> 4;
  303. }
  304. else {
  305. $index = $temp[$i++] & 0x0F;
  306. }
  307. $low = !$low;
  308. imagesetpixel($res, $X++, $Y, $PALETTE[$index +1]);
  309. }
  310. $Y--;
  311. $P += $line_length + $BMP['decal'];
  312. }
  313. }
  314. elseif ($BMP['bits_per_pixel'] == 1)
  315. {
  316. $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
  317. if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
  318. elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
  319. elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
  320. elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
  321. elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
  322. elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
  323. elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
  324. elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
  325. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  326. }
  327. else {
  328. return false;
  329. }
  330. return $res;
  331. }
  332. }