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

/components/com_jce/editor/libraries/classes/utility.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 255 lines | 152 code | 38 blank | 65 comment | 22 complexity | e527b9290181d545011d357a0a290f99 MD5 | raw file
  1. <?php
  2. /**
  3. * @package JCE
  4. * @copyright Copyright (c) 2009-2013 Ryan Demmer. All rights reserved.
  5. * @license GNU/GPL 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  6. * JCE is free software. This version may have been modified pursuant
  7. * to the GNU General Public License, and as distributed it includes or
  8. * is derivative of works licensed under the GNU General Public License or
  9. * other free or open source software licenses.
  10. */
  11. defined('_JEXEC') or die('RESTRICTED');
  12. abstract class WFUtility {
  13. public static function getExtension($path) {
  14. $dot = strrpos($path, '.') + 1;
  15. return substr($path, $dot);
  16. }
  17. public static function stripExtension($path) {
  18. $dot = strrpos($path, '.');
  19. return substr($path, 0, $dot);
  20. }
  21. public static function cleanPath($path, $ds = DIRECTORY_SEPARATOR, $prefix = '') {
  22. $path = trim(urldecode($path));
  23. // check for UNC path on IIS and set prefix
  24. if ($ds == '\\' && $path[0] == '\\' && $path[1] == '\\') {
  25. $prefix = "\\";
  26. }
  27. // clean path, removing double slashes, replacing back/forward slashes with DIRECTORY_SEPARATOR
  28. $path = preg_replace('#[/\\\\]+#', $ds, $path);
  29. // return path with prefix if any
  30. return $prefix . $path;
  31. }
  32. /**
  33. * Append a DIRECTORY_SEPARATOR to the path if required.
  34. * @param string $path the path
  35. * @param string $ds optional directory seperator
  36. * @return string path with trailing DIRECTORY_SEPARATOR
  37. */
  38. public static function fixPath($path, $ds = DIRECTORY_SEPARATOR) {
  39. return self::cleanPath($path . $ds);
  40. }
  41. private static function checkCharValue($string) {
  42. if (preg_match('#([^\w\.\-~\/\\\\\s ])#i', $string, $matches)) {
  43. foreach ($matches as $match) {
  44. // not a safe UTF-8 character
  45. if (ord($match) < 127) {
  46. return false;
  47. }
  48. }
  49. }
  50. return true;
  51. }
  52. public static function checkPath($path) {
  53. $path = urldecode($path);
  54. if (self::checkCharValue($path) === false || strpos($path, '..') !== false) {
  55. JError::raiseError(403, 'INVALID PATH'); // don't translate
  56. exit();
  57. }
  58. }
  59. /**
  60. * Concat two paths together. Basically $a + $b
  61. * @param string $a path one
  62. * @param string $b path two
  63. * @param string $ds optional directory seperator
  64. * @return string $a DIRECTORY_SEPARATOR $b
  65. */
  66. public static function makePath($a, $b, $ds = DIRECTORY_SEPARATOR) {
  67. return self::cleanPath($a . $ds . $b, $ds);
  68. }
  69. private static function utf8_latin_to_ascii($subject) {
  70. static $CHARS = NULL;
  71. if (is_null($CHARS)) {
  72. $CHARS = array(
  73. 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE',
  74. 'Ç' => 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I',
  75. 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ø' => 'O',
  76. 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ý' => 'Y', 'ß' => 's',
  77. 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae',
  78. 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
  79. 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u',
  80. 'ý' => 'y', 'ÿ' => 'y', 'Ā' => 'A', 'ā' => 'a', 'Ă' => 'A', 'ă' => 'a', 'Ą' => 'A', 'ą' => 'a',
  81. 'Ć' => 'C', 'ć' => 'c', 'Ĉ' => 'C', 'ĉ' => 'c', 'Ċ' => 'C', 'ċ' => 'c', 'Č' => 'C', 'č' => 'c', 'Ď' => 'D', 'ď' => 'd', 'Đ' => 'D', 'đ' => 'd',
  82. 'Ē' => 'E', 'ē' => 'e', 'Ĕ' => 'E', 'ĕ' => 'e', 'Ė' => 'E', 'ė' => 'e', 'Ę' => 'E', 'ę' => 'e', 'Ě' => 'E', 'ě' => 'e',
  83. 'Ĝ' => 'G', 'ĝ' => 'g', 'Ğ' => 'G', 'ğ' => 'g', 'Ġ' => 'G', 'ġ' => 'g', 'Ģ' => 'G', 'ģ' => 'g', 'Ĥ' => 'H', 'ĥ' => 'h', 'Ħ' => 'H', 'ħ' => 'h',
  84. 'Ĩ' => 'I', 'ĩ' => 'i', 'Ī' => 'I', 'ī' => 'i', 'Ĭ' => 'I', 'ĭ' => 'i', 'Į' => 'I', 'į' => 'i', 'İ' => 'I', 'ı' => 'i',
  85. 'IJ' => 'IJ', 'ij' => 'ij', 'Ĵ' => 'J', 'ĵ' => 'j', 'Ķ' => 'K', 'ķ' => 'k', 'Ĺ' => 'L', 'ĺ' => 'l', 'Ļ' => 'L', 'ļ' => 'l', 'Ľ' => 'L', 'ľ' => 'l', 'Ŀ' => 'L', 'ŀ' => 'l', 'Ł' => 'l', 'ł' => 'l',
  86. 'Ń' => 'N', 'ń' => 'n', 'Ņ' => 'N', 'ņ' => 'n', 'Ň' => 'N', 'ň' => 'n', 'ʼn' => 'n', 'Ō' => 'O', 'ō' => 'o', 'Ŏ' => 'O', 'ŏ' => 'o', 'Ő' => 'O', 'ő' => 'o', 'Œ' => 'OE', 'œ' => 'oe',
  87. 'Ŕ' => 'R', 'ŕ' => 'r', 'Ŗ' => 'R', 'ŗ' => 'r', 'Ř' => 'R', 'ř' => 'r', 'Ś' => 'S', 'ś' => 's', 'Ŝ' => 'S', 'ŝ' => 's', 'Ş' => 'S', 'ş' => 's', 'Š' => 'S', 'š' => 's',
  88. 'Ţ' => 'T', 'ţ' => 't', 'Ť' => 'T', 'ť' => 't', 'Ŧ' => 'T', 'ŧ' => 't', 'Ũ' => 'U', 'ũ' => 'u', 'Ū' => 'U', 'ū' => 'u', 'Ŭ' => 'U', 'ŭ' => 'u', 'Ů' => 'U', 'ů' => 'u', 'Ű' => 'U', 'ű' => 'u', 'Ų' => 'U', 'ų' => 'u',
  89. 'Ŵ' => 'W', 'ŵ' => 'w', 'Ŷ' => 'Y', 'ŷ' => 'y', 'Ÿ' => 'Y', 'Ź' => 'Z', 'ź' => 'z', 'Ż' => 'Z', 'ż' => 'z', 'Ž' => 'Z', 'ž' => 'z', 'ſ' => 's', 'ƒ' => 'f', 'Ơ' => 'O', 'ơ' => 'o', 'Ư' => 'U', 'ư' => 'u',
  90. 'Ǎ' => 'A', 'ǎ' => 'a', 'Ǐ' => 'I', 'ǐ' => 'i', 'Ǒ' => 'O', 'ǒ' => 'o', 'Ǔ' => 'U', 'ǔ' => 'u', 'Ǖ' => 'U', 'ǖ' => 'u', 'Ǘ' => 'U', 'ǘ' => 'u', 'Ǚ' => 'U', 'ǚ' => 'u', 'Ǜ' => 'U', 'ǜ' => 'u',
  91. 'Ǻ' => 'A', 'ǻ' => 'a', 'Ǽ' => 'AE', 'ǽ' => 'ae', 'Ǿ' => 'O', 'ǿ' => 'o'
  92. );
  93. }
  94. return str_replace(array_keys($CHARS), array_values($CHARS), $subject);
  95. }
  96. /**
  97. * Makes file name safe to use
  98. * @param mixed The name of the file (not full path)
  99. * @return mixed The sanitised string or array
  100. */
  101. public static function makeSafe($subject, $mode = 'utf-8', $allowspaces = false) {
  102. $search = array();
  103. // replace spaces with underscore
  104. if (!$allowspaces) {
  105. $subject = preg_replace('#[\s ]#', '_', $subject);
  106. }
  107. switch ($mode) {
  108. default:
  109. case 'utf-8':
  110. $search[] = '#[^a-zA-Z0-9_\.\-~\p{L}\p{N}\s ]#u';
  111. $mode = 'utf-8';
  112. break;
  113. case 'ascii':
  114. $subject = self::utf8_latin_to_ascii($subject);
  115. $search[] = '#[^a-zA-Z0-9_\.\-~\s ]#';
  116. break;
  117. }
  118. // remove multiple . characters
  119. $search[] = '#(\.){2,}#';
  120. // strip leading period
  121. $search[] = '#^\.#';
  122. // strip trailing period
  123. $search[] = '#\.$#';
  124. // strip whitespace
  125. $search[] = '#^\s*|\s*$#';
  126. // only for utf-8 to avoid PCRE errors - PCRE must be at least version 5
  127. if ($mode == 'utf-8') {
  128. try {
  129. $result = preg_replace($search, '', $subject);
  130. } catch (Exception $e) {
  131. // try ascii
  132. return self::makeSafe($subject, 'ascii');
  133. }
  134. // try ascii
  135. if (is_null($result) || $result === false) {
  136. return self::makeSafe($subject, 'ascii');
  137. }
  138. return $result;
  139. }
  140. return preg_replace($search, '', $subject);
  141. }
  142. /**
  143. * Format the file size, limits to Mb.
  144. * @param int $size the raw filesize
  145. * @return string formated file size.
  146. */
  147. public static function formatSize($size) {
  148. if ($size < 1024) {
  149. return $size . ' ' . WFText::_('WF_LABEL_BYTES');
  150. } else if ($size >= 1024 && $size < 1024 * 1024) {
  151. return sprintf('%01.2f', $size / 1024.0) . ' ' . WFText::_('WF_LABEL_KB');
  152. } else {
  153. return sprintf('%01.2f', $size / (1024.0 * 1024)) . ' ' . WFText::_('WF_LABEL_MB');
  154. }
  155. }
  156. /**
  157. * Format the date.
  158. * @param int $date the unix datestamp
  159. * @return string formated date.
  160. */
  161. public static function formatDate($date, $format = "%d/%m/%Y, %H:%M") {
  162. return strftime($format, $date);
  163. }
  164. /**
  165. * Get the modified date of a file
  166. *
  167. * @return Formatted modified date
  168. * @param string $file Absolute path to file
  169. */
  170. public static function getDate($file) {
  171. return self::formatDate(@filemtime($file));
  172. }
  173. /**
  174. * Get the size of a file
  175. *
  176. * @return Formatted filesize value
  177. * @param string $file Absolute path to file
  178. */
  179. public static function getSize($file) {
  180. return self::formatSize(@filesize($file));
  181. }
  182. public static function isUtf8($string) {
  183. if (!function_exists('mb_detect_encoding')) {
  184. // From http://w3.org/International/questions/qa-forms-utf-8.html
  185. return preg_match('%^(?:
  186. [\x09\x0A\x0D\x20-\x7E] # ASCII
  187. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  188. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  189. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  190. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  191. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  192. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  193. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  194. )*$%xs', $string);
  195. }
  196. return mb_detect_encoding($string, 'UTF-8', true);
  197. }
  198. /**
  199. * Convert size value to bytes
  200. */
  201. public static function convertSize($value) {
  202. // Convert to bytes
  203. switch (strtolower($value{strlen($value) - 1})) {
  204. case 'g':
  205. $value = intval($value) * 1073741824;
  206. break;
  207. case 'm':
  208. $value = intval($value) * 1048576;
  209. break;
  210. case 'k':
  211. $value = intval($value) * 1024;
  212. break;
  213. }
  214. return $value;
  215. }
  216. }
  217. ?>