PageRenderTime 49ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/184.168.182.1/admin/ckfinder/core/connector/php/php5/Utils/Misc.php

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