PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/joomla/administrator/components/com_media/helpers/media.php

https://github.com/reechalee/joomla1.6
PHP | 183 lines | 127 code | 20 blank | 36 comment | 42 complexity | e4c0b8f3c61a5c6bc3e1cfe7058b09eb MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, JSON
  1. <?php
  2. /**
  3. * @version $Id: media.php 20196 2011-01-09 02:40:25Z ian $
  4. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  5. * @license GNU General Public License version 2 or later; see LICENSE.txt
  6. */
  7. /**
  8. * @package Joomla.Administrator
  9. * @subpackage com_media
  10. */
  11. abstract class MediaHelper
  12. {
  13. /**
  14. * Checks if the file is an image
  15. * @param string The filename
  16. * @return boolean
  17. */
  18. public static function isImage($fileName)
  19. {
  20. static $imageTypes = 'xcf|odg|gif|jpg|png|bmp';
  21. return preg_match("/\.(?:$imageTypes)$/i",$fileName);
  22. }
  23. /**
  24. * Checks if the file is an image
  25. * @param string The filename
  26. * @return boolean
  27. */
  28. public static function getTypeIcon($fileName)
  29. {
  30. // Get file extension
  31. return strtolower(substr($fileName, strrpos($fileName, '.') + 1));
  32. }
  33. /**
  34. * Checks if the file can be uploaded
  35. *
  36. * @param array File information
  37. * @param string An error message to be returned
  38. * @return boolean
  39. */
  40. public static function canUpload($file, &$err)
  41. {
  42. $params = JComponentHelper::getParams('com_media');
  43. if (empty($file['name'])) {
  44. $err = 'COM_MEDIA_ERROR_UPLOAD_INPUT';
  45. return false;
  46. }
  47. jimport('joomla.filesystem.file');
  48. if ($file['name'] !== JFile::makesafe($file['name'])) {
  49. $err = 'COM_MEDIA_ERROR_WARNFILENAME';
  50. return false;
  51. }
  52. $format = strtolower(JFile::getExt($file['name']));
  53. $allowable = explode(',', $params->get('upload_extensions'));
  54. $ignored = explode(',', $params->get('ignore_extensions'));
  55. if (!in_array($format, $allowable) && !in_array($format,$ignored))
  56. {
  57. $err = 'COM_MEDIA_ERROR_WARNFILETYPE';
  58. return false;
  59. }
  60. $maxSize = (int) ($params->get('upload_maxsize', 0) * 1024 * 1024);
  61. if ($maxSize > 0 && (int) $file['size'] > $maxSize)
  62. {
  63. $err = 'COM_MEDIA_ERROR_WARNFILETOOLARGE';
  64. return false;
  65. }
  66. $user = JFactory::getUser();
  67. $imginfo = null;
  68. if ($params->get('restrict_uploads',1)) {
  69. $images = explode(',', $params->get('image_extensions'));
  70. if (in_array($format, $images)) { // if its an image run it through getimagesize
  71. // if tmp_name is empty, then the file was bigger than the PHP limit
  72. if (!empty($file['tmp_name'])) {
  73. if (($imginfo = getimagesize($file['tmp_name'])) === FALSE) {
  74. $err = 'COM_MEDIA_ERROR_WARNINVALID_IMG';
  75. return false;
  76. }
  77. } else {
  78. $err = 'COM_MEDIA_ERROR_WARNFILETOOLARGE';
  79. return false;
  80. }
  81. } else if (!in_array($format, $ignored)) {
  82. // if its not an image...and we're not ignoring it
  83. $allowed_mime = explode(',', $params->get('upload_mime'));
  84. $illegal_mime = explode(',', $params->get('upload_mime_illegal'));
  85. if (function_exists('finfo_open') && $params->get('check_mime',1)) {
  86. // We have fileinfo
  87. $finfo = finfo_open(FILEINFO_MIME);
  88. $type = finfo_file($finfo, $file['tmp_name']);
  89. if (strlen($type) && !in_array($type, $allowed_mime) && in_array($type, $illegal_mime)) {
  90. $err = 'COM_MEDIA_ERROR_WARNINVALID_MIME';
  91. return false;
  92. }
  93. finfo_close($finfo);
  94. } else if (function_exists('mime_content_type') && $params->get('check_mime',1)) {
  95. // we have mime magic
  96. $type = mime_content_type($file['tmp_name']);
  97. if (strlen($type) && !in_array($type, $allowed_mime) && in_array($type, $illegal_mime)) {
  98. $err = 'COM_MEDIA_ERROR_WARNINVALID_MIME';
  99. return false;
  100. }
  101. } else if (!$user->authorise('core.manage')) {
  102. $err = 'COM_MEDIA_ERROR_WARNNOTADMIN';
  103. return false;
  104. }
  105. }
  106. }
  107. $xss_check = JFile::read($file['tmp_name'],false,256);
  108. $html_tags = array('abbr','acronym','address','applet','area','audioscope','base','basefont','bdo','bgsound','big','blackface','blink','blockquote','body','bq','br','button','caption','center','cite','code','col','colgroup','comment','custom','dd','del','dfn','dir','div','dl','dt','em','embed','fieldset','fn','font','form','frame','frameset','h1','h2','h3','h4','h5','h6','head','hr','html','iframe','ilayer','img','input','ins','isindex','keygen','kbd','label','layer','legend','li','limittext','link','listing','map','marquee','menu','meta','multicol','nobr','noembed','noframes','noscript','nosmartquotes','object','ol','optgroup','option','param','plaintext','pre','rt','ruby','s','samp','script','select','server','shadow','sidebar','small','spacer','span','strike','strong','style','sub','sup','table','tbody','td','textarea','tfoot','th','thead','title','tr','tt','ul','var','wbr','xml','xmp','!DOCTYPE', '!--');
  109. foreach($html_tags as $tag) {
  110. // A tag is '<tagname ', so we need to add < and a space or '<tagname>'
  111. if (stristr($xss_check, '<'.$tag.' ') || stristr($xss_check, '<'.$tag.'>')) {
  112. $err = 'COM_MEDIA_ERROR_WARNIEXSS';
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. public static function parseSize($size)
  119. {
  120. if ($size < 1024) {
  121. return JText::sprintf('COM_MEDIA_FILESIZE_BYTES', $size);
  122. }
  123. elseif ($size < 1024 * 1024) {
  124. return JText::sprintf('COM_MEDIA_FILESIZE_KILOBYTES', sprintf('%01.2f', $size / 1024.0));
  125. }
  126. else {
  127. return JText::sprintf('COM_MEDIA_FILESIZE_MEGABYTES', sprintf('%01.2f', $size / (1024.0 * 1024)));
  128. }
  129. }
  130. public static function imageResize($width, $height, $target)
  131. {
  132. //takes the larger size of the width and height and applies the
  133. //formula accordingly...this is so this script will work
  134. //dynamically with any size image
  135. if ($width > $height) {
  136. $percentage = ($target / $width);
  137. } else {
  138. $percentage = ($target / $height);
  139. }
  140. //gets the new value and applies the percentage, then rounds the value
  141. $width = round($width * $percentage);
  142. $height = round($height * $percentage);
  143. return array($width, $height);
  144. }
  145. public static function countFiles($dir)
  146. {
  147. $total_file = 0;
  148. $total_dir = 0;
  149. if (is_dir($dir)) {
  150. $d = dir($dir);
  151. while (false !== ($entry = $d->read())) {
  152. if (substr($entry, 0, 1) != '.' && is_file($dir . DIRECTORY_SEPARATOR . $entry) && strpos($entry, '.html') === false && strpos($entry, '.php') === false) {
  153. $total_file++;
  154. }
  155. if (substr($entry, 0, 1) != '.' && is_dir($dir . DIRECTORY_SEPARATOR . $entry)) {
  156. $total_dir++;
  157. }
  158. }
  159. $d->close();
  160. }
  161. return array ($total_file, $total_dir);
  162. }
  163. }