PageRenderTime 21ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lanoversolutions/afterschool
PHP | 388 lines | 245 code | 37 blank | 106 comment | 67 complexity | 984211f3edabf0be8234aac8a7f34393 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. * 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. 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. 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. 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. // There are no memory limits, nothing to do
  126. if ($memoryLimit == -1) {
  127. return true;
  128. }
  129. if (!$memoryLimit) {
  130. $memoryLimit = 8;
  131. }
  132. $memoryLimitMB = $memoryLimit * $MB;
  133. if (function_exists('memory_get_usage')) {
  134. if (memory_get_usage() + $memoryNeeded > $memoryLimitMB) {
  135. $newLimit = $memoryLimit + ceil( ( memory_get_usage()
  136. + $memoryNeeded
  137. - $memoryLimitMB
  138. ) / $MB
  139. );
  140. if (@ini_set( 'memory_limit', $newLimit . 'M' ) === false) {
  141. return false;
  142. }
  143. }
  144. } else {
  145. if ($memoryNeeded + 3*$MB > $memoryLimitMB) {
  146. $newLimit = $memoryLimit + ceil(( 3*$MB
  147. + $memoryNeeded
  148. - $memoryLimitMB
  149. ) / $MB
  150. );
  151. if (false === @ini_set( 'memory_limit', $newLimit . 'M' )) {
  152. return false;
  153. }
  154. }
  155. }
  156. return true;
  157. }
  158. /**
  159. * convert shorthand php.ini notation into bytes, much like how the PHP source does it
  160. * @link http://pl.php.net/manual/en/function.ini-get.php
  161. *
  162. * @static
  163. * @access public
  164. * @param string $val
  165. * @return int
  166. */
  167. function returnBytes($val)
  168. {
  169. $val = trim($val);
  170. if (!$val) {
  171. return 0;
  172. }
  173. $last = strtolower($val[strlen($val)-1]);
  174. switch($last) {
  175. // The 'G' modifier is available since PHP 5.1.0
  176. case 'g':
  177. $val *= 1024;
  178. case 'm':
  179. $val *= 1024;
  180. case 'k':
  181. $val *= 1024;
  182. }
  183. return $val;
  184. }
  185. /**
  186. * Checks if a value exists in an array (case insensitive)
  187. *
  188. * @static
  189. * @access public
  190. * @param string $needle
  191. * @param array $haystack
  192. * @return boolean
  193. */
  194. function inArrayCaseInsensitive($needle, $haystack)
  195. {
  196. if (!$haystack || !is_array($haystack)) {
  197. return false;
  198. }
  199. $lcase = array();
  200. foreach ($haystack as $key => $val) {
  201. $lcase[$key] = strtolower($val);
  202. }
  203. return in_array($needle, $lcase);
  204. }
  205. /**
  206. * UTF-8 compatible version of basename()
  207. *
  208. * @static
  209. * @access public
  210. * @param string $file
  211. * @return string
  212. */
  213. function mbBasename($file)
  214. {
  215. $explode = explode('/', str_replace("\\", "/", $file));
  216. return end($explode);
  217. }
  218. /**
  219. * Checks whether the string is valid UTF8
  220. * @static
  221. * @access public
  222. * @param string $string
  223. * @return boolean
  224. */
  225. function isValidUTF8($string)
  226. {
  227. if (strlen($string) == 0) {
  228. return true;
  229. }
  230. return (preg_match('/^./us', $string) == 1);
  231. }
  232. /**
  233. * Source: http://pl.php.net/imagecreate
  234. * (optimized for speed and memory usage, but yet not very efficient)
  235. *
  236. * @static
  237. * @access public
  238. * @param string $filename
  239. * @return resource
  240. */
  241. function imageCreateFromBmp($filename)
  242. {
  243. //20 seconds seems to be a reasonable value to not kill a server and process images up to 1680x1050
  244. @set_time_limit(20);
  245. if (false === ($f1 = fopen($filename, "rb"))) {
  246. return false;
  247. }
  248. $FILE = unpack("vfile_type/Vfile_size/Vreserved/Vbitmap_offset", fread($f1, 14));
  249. if ($FILE['file_type'] != 19778) {
  250. return false;
  251. }
  252. $BMP = unpack('Vheader_size/Vwidth/Vheight/vplanes/vbits_per_pixel'.
  253. '/Vcompression/Vsize_bitmap/Vhoriz_resolution'.
  254. '/Vvert_resolution/Vcolors_used/Vcolors_important', fread($f1, 40));
  255. $BMP['colors'] = pow(2,$BMP['bits_per_pixel']);
  256. if ($BMP['size_bitmap'] == 0) {
  257. $BMP['size_bitmap'] = $FILE['file_size'] - $FILE['bitmap_offset'];
  258. }
  259. $BMP['bytes_per_pixel'] = $BMP['bits_per_pixel']/8;
  260. $BMP['bytes_per_pixel2'] = ceil($BMP['bytes_per_pixel']);
  261. $BMP['decal'] = ($BMP['width']*$BMP['bytes_per_pixel']/4);
  262. $BMP['decal'] -= floor($BMP['width']*$BMP['bytes_per_pixel']/4);
  263. $BMP['decal'] = 4-(4*$BMP['decal']);
  264. if ($BMP['decal'] == 4) {
  265. $BMP['decal'] = 0;
  266. }
  267. $PALETTE = array();
  268. if ($BMP['colors'] < 16777216) {
  269. $PALETTE = unpack('V'.$BMP['colors'], fread($f1, $BMP['colors']*4));
  270. }
  271. //2048x1536px@24bit don't even try to process larger files as it will probably fail
  272. if ($BMP['size_bitmap'] > 3 * 2048 * 1536) {
  273. return false;
  274. }
  275. $IMG = fread($f1, $BMP['size_bitmap']);
  276. fclose($f1);
  277. $VIDE = chr(0);
  278. $res = imagecreatetruecolor($BMP['width'],$BMP['height']);
  279. $P = 0;
  280. $Y = $BMP['height']-1;
  281. $line_length = $BMP['bytes_per_pixel']*$BMP['width'];
  282. if ($BMP['bits_per_pixel'] == 24) {
  283. while ($Y >= 0)
  284. {
  285. $X=0;
  286. $temp = unpack( "C*", substr($IMG, $P, $line_length));
  287. while ($X < $BMP['width'])
  288. {
  289. $offset = $X*3;
  290. imagesetpixel($res, $X++, $Y, ($temp[$offset+3] << 16) + ($temp[$offset+2] << 8) + $temp[$offset+1]);
  291. }
  292. $Y--;
  293. $P += $line_length + $BMP['decal'];
  294. }
  295. }
  296. elseif ($BMP['bits_per_pixel'] == 8)
  297. {
  298. while ($Y >= 0)
  299. {
  300. $X=0;
  301. $temp = unpack( "C*", substr($IMG, $P, $line_length));
  302. while ($X < $BMP['width'])
  303. {
  304. imagesetpixel($res, $X++, $Y, $PALETTE[$temp[$X] +1]);
  305. }
  306. $Y--;
  307. $P += $line_length + $BMP['decal'];
  308. }
  309. }
  310. elseif ($BMP['bits_per_pixel'] == 4)
  311. {
  312. while ($Y >= 0)
  313. {
  314. $X=0;
  315. $i = 1;
  316. $low = true;
  317. $temp = unpack( "C*", substr($IMG, $P, $line_length));
  318. while ($X < $BMP['width'])
  319. {
  320. if ($low) {
  321. $index = $temp[$i] >> 4;
  322. }
  323. else {
  324. $index = $temp[$i++] & 0x0F;
  325. }
  326. $low = !$low;
  327. imagesetpixel($res, $X++, $Y, $PALETTE[$index +1]);
  328. }
  329. $Y--;
  330. $P += $line_length + $BMP['decal'];
  331. }
  332. }
  333. elseif ($BMP['bits_per_pixel'] == 1)
  334. {
  335. $COLOR = unpack("n",$VIDE.substr($IMG,floor($P),1));
  336. if (($P*8)%8 == 0) $COLOR[1] = $COLOR[1] >>7;
  337. elseif (($P*8)%8 == 1) $COLOR[1] = ($COLOR[1] & 0x40)>>6;
  338. elseif (($P*8)%8 == 2) $COLOR[1] = ($COLOR[1] & 0x20)>>5;
  339. elseif (($P*8)%8 == 3) $COLOR[1] = ($COLOR[1] & 0x10)>>4;
  340. elseif (($P*8)%8 == 4) $COLOR[1] = ($COLOR[1] & 0x8)>>3;
  341. elseif (($P*8)%8 == 5) $COLOR[1] = ($COLOR[1] & 0x4)>>2;
  342. elseif (($P*8)%8 == 6) $COLOR[1] = ($COLOR[1] & 0x2)>>1;
  343. elseif (($P*8)%8 == 7) $COLOR[1] = ($COLOR[1] & 0x1);
  344. $COLOR[1] = $PALETTE[$COLOR[1]+1];
  345. }
  346. else {
  347. return false;
  348. }
  349. return $res;
  350. }
  351. }