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

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

https://github.com/ot2sen/Molajo
PHP | 185 lines | 128 code | 21 blank | 36 comment | 42 complexity | 748bc6b775db2076b1db5d6cc88c641b MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: media.php 21389 2011-05-26 17:28:26Z dextercowley $
  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. defined('_JEXEC') or die;
  8. /**
  9. * @package Joomla.Administrator
  10. * @subpackage com_media
  11. */
  12. abstract class MediaHelper
  13. {
  14. /**
  15. * Checks if the file is an image
  16. * @param string The filename
  17. * @return boolean
  18. */
  19. public static function isImage($fileName)
  20. {
  21. static $imageTypes = 'xcf|odg|gif|jpg|png|bmp';
  22. return preg_match("/\.(?:$imageTypes)$/i",$fileName);
  23. }
  24. /**
  25. * Checks if the file is an image
  26. * @param string The filename
  27. * @return boolean
  28. */
  29. public static function getTypeIcon($fileName)
  30. {
  31. // Get file extension
  32. return strtolower(substr($fileName, strrpos($fileName, '.') + 1));
  33. }
  34. /**
  35. * Checks if the file can be uploaded
  36. *
  37. * @param array File information
  38. * @param string An error message to be returned
  39. * @return boolean
  40. */
  41. public static function canUpload($file, &$err)
  42. {
  43. $params = JComponentHelper::getParams('com_media');
  44. if (empty($file['name'])) {
  45. $err = 'COM_MEDIA_ERROR_UPLOAD_INPUT';
  46. return false;
  47. }
  48. jimport('joomla.filesystem.file');
  49. if ($file['name'] !== JFile::makesafe($file['name'])) {
  50. $err = 'COM_MEDIA_ERROR_WARNFILENAME';
  51. return false;
  52. }
  53. $format = strtolower(JFile::getExt($file['name']));
  54. $allowable = explode(',', $params->get('upload_extensions'));
  55. $ignored = explode(',', $params->get('ignore_extensions'));
  56. if (!in_array($format, $allowable) && !in_array($format,$ignored))
  57. {
  58. $err = 'COM_MEDIA_ERROR_WARNFILETYPE';
  59. return false;
  60. }
  61. $maxSize = (int) ($params->get('upload_maxsize', 0) * 1024 * 1024);
  62. if ($maxSize > 0 && (int) $file['size'] > $maxSize)
  63. {
  64. $err = 'COM_MEDIA_ERROR_WARNFILETOOLARGE';
  65. return false;
  66. }
  67. $user = MolajoFactory::getUser();
  68. $imginfo = null;
  69. if ($params->get('restrict_uploads',1)) {
  70. $images = explode(',', $params->get('image_extensions'));
  71. if (in_array($format, $images)) { // if its an image run it through getimagesize
  72. // if tmp_name is empty, then the file was bigger than the PHP limit
  73. if (!empty($file['tmp_name'])) {
  74. if (($imginfo = getimagesize($file['tmp_name'])) === FALSE) {
  75. $err = 'COM_MEDIA_ERROR_WARNINVALID_IMG';
  76. return false;
  77. }
  78. } else {
  79. $err = 'COM_MEDIA_ERROR_WARNFILETOOLARGE';
  80. return false;
  81. }
  82. } else if (!in_array($format, $ignored)) {
  83. // if its not an image...and we're not ignoring it
  84. $allowed_mime = explode(',', $params->get('upload_mime'));
  85. $illegal_mime = explode(',', $params->get('upload_mime_illegal'));
  86. if (function_exists('finfo_open') && $params->get('check_mime',1)) {
  87. // We have fileinfo
  88. $finfo = finfo_open(FILEINFO_MIME);
  89. $type = finfo_file($finfo, $file['tmp_name']);
  90. if (strlen($type) && !in_array($type, $allowed_mime) && in_array($type, $illegal_mime)) {
  91. $err = 'COM_MEDIA_ERROR_WARNINVALID_MIME';
  92. return false;
  93. }
  94. finfo_close($finfo);
  95. } else if (function_exists('mime_content_type') && $params->get('check_mime',1)) {
  96. // we have mime magic
  97. $type = mime_content_type($file['tmp_name']);
  98. if (strlen($type) && !in_array($type, $allowed_mime) && in_array($type, $illegal_mime)) {
  99. $err = 'COM_MEDIA_ERROR_WARNINVALID_MIME';
  100. return false;
  101. }
  102. } else if (!$user->authorise('core.manage')) {
  103. $err = 'COM_MEDIA_ERROR_WARNNOTADMIN';
  104. return false;
  105. }
  106. }
  107. }
  108. $xss_check = JFile::read($file['tmp_name'],false,256);
  109. $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', '!--');
  110. foreach($html_tags as $tag) {
  111. // A tag is '<tagname ', so we need to add < and a space or '<tagname>'
  112. if (stristr($xss_check, '<'.$tag.' ') || stristr($xss_check, '<'.$tag.'>')) {
  113. $err = 'COM_MEDIA_ERROR_WARNIEXSS';
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. public static function parseSize($size)
  120. {
  121. if ($size < 1024) {
  122. return JText::sprintf('COM_MEDIA_FILESIZE_BYTES', $size);
  123. }
  124. elseif ($size < 1024 * 1024) {
  125. return JText::sprintf('COM_MEDIA_FILESIZE_KILOBYTES', sprintf('%01.2f', $size / 1024.0));
  126. }
  127. else {
  128. return JText::sprintf('COM_MEDIA_FILESIZE_MEGABYTES', sprintf('%01.2f', $size / (1024.0 * 1024)));
  129. }
  130. }
  131. public static function imageResize($width, $height, $target)
  132. {
  133. //takes the larger size of the width and height and applies the
  134. //formula accordingly...this is so this script will work
  135. //dynamically with any size image
  136. if ($width > $height) {
  137. $percentage = ($target / $width);
  138. } else {
  139. $percentage = ($target / $height);
  140. }
  141. //gets the new value and applies the percentage, then rounds the value
  142. $width = round($width * $percentage);
  143. $height = round($height * $percentage);
  144. return array($width, $height);
  145. }
  146. public static function countFiles($dir)
  147. {
  148. $total_file = 0;
  149. $total_dir = 0;
  150. if (is_dir($dir)) {
  151. $d = dir($dir);
  152. while (false !== ($entry = $d->read())) {
  153. if (substr($entry, 0, 1) != '.' && is_file($dir . DIRECTORY_SEPARATOR . $entry) && strpos($entry, '.html') === false && strpos($entry, '.php') === false) {
  154. $total_file++;
  155. }
  156. if (substr($entry, 0, 1) != '.' && is_dir($dir . DIRECTORY_SEPARATOR . $entry)) {
  157. $total_dir++;
  158. }
  159. }
  160. $d->close();
  161. }
  162. return array ($total_file, $total_dir);
  163. }
  164. }