PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/MY_Upload.php

https://bitbucket.org/IceElect/slsocial
PHP | 364 lines | 221 code | 69 blank | 74 comment | 38 complexity | 036f5c2c894b6dbc9ecd5b6326234b37 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * File Uploading Class
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Uploads
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/libraries/file_uploading.html
  24. */
  25. class MY_Upload extends CI_Upload
  26. {
  27. public $field_title = '';
  28. public function set_field_title($title)
  29. {
  30. $this->field_title = $title;
  31. }
  32. /**
  33. * Set an error message
  34. *
  35. * @param string
  36. * @return void
  37. */
  38. public function set_uerror($msg)
  39. {
  40. $CI =& get_instance();
  41. $CI->lang->load('upload');
  42. if (is_array($msg)) {
  43. foreach ($msg as $val) {
  44. $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);
  45. $msg = $CI->translate->t('upload_' . $val, $msg);
  46. $msg = sprintf($msg, $this->field_title);
  47. $this->error_msg[] = $msg;
  48. log_message('error', $msg);
  49. }
  50. } else {
  51. $lang = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg);
  52. $msg = $CI->translate->t('upload_' . $msg, $lang);
  53. $msg = sprintf($msg, $this->field_title);
  54. $this->error_msg[] = $msg;
  55. log_message('error', $msg);
  56. }
  57. }
  58. public function set_filename($path, $filename)
  59. {
  60. if ($this->encrypt_name == TRUE)
  61. {
  62. mt_srand();
  63. $filename = md5(uniqid(mt_rand())).$this->file_ext;
  64. }
  65. if ( ! file_exists($path.$filename))
  66. {
  67. return $filename;
  68. }
  69. $filename = str_replace($this->file_ext, '', $filename);
  70. $new_filename = '';
  71. for ($i = 1; $i < 100; $i++)
  72. {
  73. $_filename = $filename . '_' . $i . $this->file_ext;
  74. if ( ! file_exists($path.$_filename))
  75. {
  76. $new_filename = $_filename;
  77. break;
  78. }
  79. }
  80. if ($new_filename == '')
  81. {
  82. $this->set_uerror('upload_bad_filename');
  83. return FALSE;
  84. }
  85. else
  86. {
  87. return $new_filename;
  88. }
  89. }
  90. // --------------------------------------------------------------------
  91. public function do_multi_upload( $field = 'userfile', $return_info = TRUE, $filenames = NULL ){
  92. // Is $_FILES[$field] set? If not, no reason to continue.
  93. if ( ! isset($_FILES[$field]))
  94. {
  95. $this->set_error('upload_no_file_selected');
  96. return FALSE;
  97. }
  98. //If not every file filled was used, clear the empties
  99. foreach( $_FILES[$field]['name'] as $k => $n )
  100. {
  101. if( empty( $n ) )
  102. {
  103. foreach( $_FILES[$field] as $kk => $f )
  104. {
  105. unset( $_FILES[$field][$kk][$k] );
  106. }
  107. }
  108. }
  109. // Is the upload path valid?
  110. if ( ! $this->validate_upload_path($field) )
  111. {
  112. // errors will already be set by validate_upload_path() so just return FALSE
  113. return FALSE;
  114. }
  115. //Multiple file upload
  116. if( is_array( $_FILES[$field] ) )
  117. {
  118. //$count = count($_FILES[$field]['name']); //Number of files to process
  119. foreach( $_FILES[$field]['name'] as $k => $file )
  120. {
  121. // Was the file able to be uploaded? If not, determine the reason why.
  122. if ( ! is_uploaded_file($_FILES[$field]['tmp_name'][$k] ) )
  123. {
  124. $error = ( ! isset($_FILES[$field]['error'][$k])) ? 4 : $_FILES[$field]['error'][$k];
  125. switch($error)
  126. {
  127. case 1: // UPLOAD_ERR_INI_SIZE
  128. $this->set_error('upload_file_exceeds_limit');
  129. break;
  130. case 2: // UPLOAD_ERR_FORM_SIZE
  131. $this->set_error('upload_file_exceeds_form_limit');
  132. break;
  133. case 3: // UPLOAD_ERR_PARTIAL
  134. $this->set_error('upload_file_partial');
  135. break;
  136. case 4: // UPLOAD_ERR_NO_FILE
  137. $this->set_error('upload_no_file_selected');
  138. break;
  139. case 6: // UPLOAD_ERR_NO_TMP_DIR
  140. $this->set_error('upload_no_temp_directory');
  141. break;
  142. case 7: // UPLOAD_ERR_CANT_WRITE
  143. $this->set_error('upload_unable_to_write_file');
  144. break;
  145. case 8: // UPLOAD_ERR_EXTENSION
  146. $this->set_error('upload_stopped_by_extension');
  147. break;
  148. default : $this->set_error('upload_no_file_selected');
  149. break;
  150. }
  151. return FALSE;
  152. }
  153. // Set the uploaded data as class variables
  154. $this->file_temp = $_FILES[$field]['tmp_name'][$k];
  155. $this->file_size = $_FILES[$field]['size'][$k];
  156. $this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type'][$k]);
  157. $this->file_type = strtolower(trim(stripslashes($this->file_type), '"'));
  158. if(empty($filenames))
  159. {
  160. $this->file_name = $this->_prep_filename($_FILES[$field]['name'][$k]);
  161. }
  162. else
  163. {
  164. $this->file_name = $this->_prep_filename($filenames[$k]);
  165. }
  166. $this->file_ext = $this->get_extension($this->file_name);
  167. $this->client_name = $this->file_name;
  168. // Is the file type allowed to be uploaded?
  169. if ( ! $this->is_allowed_filetype())
  170. {
  171. $this->set_error('upload_invalid_filetype');
  172. return FALSE;
  173. }
  174. // if we're overriding, let's now make sure the new name and type is allowed
  175. if ($this->_file_name_override != '')
  176. {
  177. $this->file_name = $this->_prep_filename($this->_file_name_override);
  178. // If no extension was provided in the file_name config item, use the uploaded one
  179. if (strpos($this->_file_name_override, '.') === FALSE)
  180. {
  181. $this->file_name .= $this->file_ext;
  182. }
  183. // An extension was provided, lets have it!
  184. else
  185. {
  186. $this->file_ext = $this->get_extension($this->_file_name_override);
  187. }
  188. if ( ! $this->is_allowed_filetype(TRUE))
  189. {
  190. $this->set_error('upload_invalid_filetype');
  191. return FALSE;
  192. }
  193. }
  194. // Convert the file size to kilobytes
  195. if ($this->file_size > 0)
  196. {
  197. $this->file_size = round($this->file_size/1024, 2);
  198. }
  199. // Is the file size within the allowed maximum?
  200. if ( ! $this->is_allowed_filesize())
  201. {
  202. $this->set_error('upload_invalid_filesize');
  203. return FALSE;
  204. }
  205. // Are the image dimensions within the allowed size?
  206. // Note: This can fail if the server has an open_basdir restriction.
  207. if ( ! $this->is_allowed_dimensions())
  208. {
  209. $this->set_error('upload_invalid_dimensions');
  210. return FALSE;
  211. }
  212. // Sanitize the file name for security
  213. $this->file_name = $this->clean_file_name($this->file_name);
  214. // Truncate the file name if it's too long
  215. if ($this->max_filename > 0)
  216. {
  217. $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
  218. }
  219. // Remove white spaces in the name
  220. if ($this->remove_spaces == TRUE)
  221. {
  222. $this->file_name = preg_replace("/\s+/", "_", $this->file_name);
  223. }
  224. /*
  225. * Validate the file name
  226. * This function appends an number onto the end of
  227. * the file if one with the same name already exists.
  228. * If it returns false there was a problem.
  229. */
  230. $this->orig_name = $this->file_name;
  231. if ($this->overwrite == FALSE)
  232. {
  233. $this->file_name = $this->set_filename($this->upload_path, $this->file_name);
  234. if ($this->file_name === FALSE)
  235. {
  236. return FALSE;
  237. }
  238. }
  239. /*
  240. * Run the file through the XSS hacking filter
  241. * This helps prevent malicious code from being
  242. * embedded within a file. Scripts can easily
  243. * be disguised as images or other file types.
  244. */
  245. if ($this->xss_clean)
  246. {
  247. if ($this->do_xss_clean() === FALSE)
  248. {
  249. $this->set_error('upload_unable_to_write_file');
  250. return FALSE;
  251. }
  252. }
  253. /*
  254. * Move the file to the final destination
  255. * To deal with different server configurations
  256. * we'll attempt to use copy() first. If that fails
  257. * we'll use move_uploaded_file(). One of the two should
  258. * reliably work in most environments
  259. */
  260. if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
  261. {
  262. if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
  263. {
  264. $this->set_error('upload_destination_error');
  265. return FALSE;
  266. }
  267. }
  268. /*
  269. * Set the finalized image dimensions
  270. * This sets the image width/height (assuming the
  271. * file was an image). We use this information
  272. * in the "data" function.
  273. */
  274. $this->set_image_properties($this->upload_path.$this->file_name);
  275. if( $return_info === TRUE )
  276. {
  277. $return_value[$k] = $this->data();
  278. }
  279. else
  280. {
  281. $return_value = TRUE;
  282. }
  283. }
  284. return $return_value;
  285. }
  286. else //Single file upload, rely on native CI upload class
  287. {
  288. $upload = self::do_upload();
  289. return $upload;
  290. }
  291. }
  292. function clean_file_name($file_name){
  293. return $file_name;
  294. }
  295. }