PageRenderTime 32ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_fabrik/helpers/uploader.php

https://github.com/chrisinammo/arthurmcneil
PHP | 200 lines | 112 code | 25 blank | 63 comment | 21 complexity | ec7af520e19cb9d61f054121215828a2 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * @package Joomla
  4. * @subpackage Fabrik
  5. * @copyright Copyright (C) 2005 Rob Clayburn. All rights reserved.
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  7. */
  8. // Check to ensure this file is included in Joomla!
  9. defined('_JEXEC') or die();
  10. class uploader extends JObject {
  11. var $_groups = null;
  12. var $_form = null;
  13. var $moveError = false;
  14. function uploader( &$oForm )
  15. {
  16. $this->_form = $oForm;
  17. $this->_groups = &$oForm->_groups;
  18. }
  19. /**
  20. * perform upload of files
  21. * @return bol true if error occured
  22. */
  23. function upload( )
  24. {
  25. foreach ($this->_form->_groups as $groupModel) {
  26. foreach ($groupModel->_aElements as $elementModel) {
  27. if ($elementModel->isUpload()) {
  28. $elementModel->processUpload( );
  29. }
  30. }
  31. }
  32. }
  33. /**
  34. * moves a file from one location to another
  35. * @param string file to move
  36. * @param string location to move file to
  37. * @param bol do we overwrite any existing files found at pathTo?
  38. */
  39. function move( $pathFrom , $pathTo, $overwrite = true )
  40. {
  41. if(file_exists($pathTo)) {
  42. if($overwrite){
  43. unlink($pathTo);
  44. $ok = rename($pathFrom, $pathTo);
  45. }else{
  46. $ok = false;
  47. }
  48. } else {
  49. $ok = rename($pathFrom, $pathTo);
  50. }
  51. return $ok;
  52. }
  53. /**
  54. * @access private
  55. * @param string path to folder - eg /images/stories
  56. */
  57. function _makeRecursiveFolders( $folderPath, $mode = 0755 )
  58. {
  59. if (!JFolder::create($folderPath, $mode)) {
  60. return JError::raiseError(21, "Could not make dir $folderPath ");
  61. }
  62. //jimport('joomla.filesystem.path');
  63. //return JPath::setPermissions( $uploadFolder );
  64. }
  65. /**
  66. * iterates through $_FILE data to see if any files have been uploaded
  67. * @return bol true if files uploaded
  68. */
  69. function check()
  70. {
  71. if (isset($_FILES) and !empty($_FILES)){
  72. foreach($_FILES as $f){
  73. if($f['name'] != ''){
  74. return true;
  75. }
  76. }
  77. }
  78. return false;
  79. }
  80. /**
  81. * Checks if the file can be uploaded
  82. *
  83. * @param array File information
  84. * @param string An error message to be returned
  85. * @return boolean
  86. */
  87. function canUpload( $file, &$err, &$params )
  88. {
  89. if (empty( $file['name'] )) {
  90. $err = 'Please input a file for upload';
  91. return false;
  92. }
  93. jimport('joomla.filesystem.file');
  94. $format = strtolower( JFile::getExt( $file['name'] ) );
  95. $allowable = explode( ',',$params->get( 'ul_file_types' ));
  96. $format = FabrikString::ltrimword( $format, '.' );
  97. $format2 = ".$format";
  98. if (!in_array( $format, $allowable ) && !in_array( $format2, $allowable))
  99. {
  100. $err = 'WARNFILETYPE';
  101. return false;
  102. }
  103. $maxSize = (int) $params->get( 'upload_maxsize', 0 );
  104. if ($maxSize > 0 && (int) $file['size'] > $maxSize)
  105. {
  106. $err = 'WARNFILETOOLARGE';
  107. return false;
  108. }
  109. $ignored = array();
  110. $user = JFactory::getUser();
  111. $imginfo = null;
  112. if($params->get('restrict_uploads',1) ) {
  113. $images = explode( ',', $params->get( 'image_extensions' ));
  114. if(in_array($format, $images)) { // if its an image run it through getimagesize
  115. if(($imginfo = getimagesize($file['tmp_name'])) === FALSE) {
  116. $err = 'WARNINVALIDIMG';
  117. return false;
  118. }
  119. } else if(!in_array($format, $ignored)) {
  120. // if its not an image...and we're not ignoring it
  121. /*$allowed_mime = explode( ',', $upload_mime );
  122. $illegal_mime = explode( ',', $upload_mime_illegal );
  123. if(function_exists('finfo_open') && $params->get('check_mime',1)) {
  124. // We have fileinfo
  125. $finfo = finfo_open(FILEINFO_MIME);
  126. $type = finfo_file($finfo, $file['tmp_name']);
  127. if(strlen($type) && !in_array($type, $allowed_mime) && in_array($type, $illegal_mime)) {
  128. $err = 'WARNINVALIDMIME';
  129. return false;
  130. }
  131. finfo_close($finfo);
  132. } else if(function_exists('mime_content_type') && $params->get('check_mime',1)) {
  133. // we have mime magic
  134. $type = mime_content_type($file['tmp_name']);
  135. if (strlen( $type ) && !in_array($type, $allowed_mime) && in_array($type, $illegal_mime)) {
  136. $err = 'WARNINVALIDMIME';
  137. return false;
  138. }
  139. }*/
  140. }
  141. }
  142. $xss_check = JFile::read($file['tmp_name'],false,256);
  143. $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', '!--');
  144. foreach ($html_tags as $tag) {
  145. // A tag is '<tagname ', so we need to add < and a space or '<tagname>'
  146. if (stristr( $xss_check, '<'.$tag.' ' ) || stristr( $xss_check, '<'.$tag.'>' )) {
  147. $err = 'WARNIEXSS';
  148. return false;
  149. }
  150. }
  151. return true;
  152. }
  153. /**
  154. * recursive file name incrementation untill no file with exsiting name
  155. * exists
  156. * @param string intial file name
  157. * @param string this recursions file name
  158. * @param int file version
  159. * @return string new file name
  160. */
  161. function incrementFileName( $origFileName, $newFileName, $version )
  162. {
  163. if ( JFile::exists( $newFileName ) ) {
  164. $bits = explode('.', $newFileName);
  165. $ext = array_pop($bits);
  166. $f = implode('.', $bits);
  167. $f = rtrim( $f, $version - 1);
  168. $newFileName = $f . $version . "." . $ext;
  169. $version ++;
  170. $newFileName = uploader::incrementFileName( $origFileName, $newFileName, $version );
  171. }
  172. return $newFileName;
  173. }
  174. }
  175. ?>