PageRenderTime 22ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/admin/lib/lib_upload.php

http://pixie-cms.googlecode.com/
PHP | 338 lines | 296 code | 1 blank | 41 comment | 24 complexity | 0c1434e00e443f1b2c4680cdaf2492b6 MD5 | raw file
  1. <?php
  2. if (!defined('DIRECT_ACCESS')) {
  3. header('Location: ../../');
  4. exit();
  5. }
  6. /**
  7. * Pixie: The Small, Simple, Site Maker.
  8. *
  9. * Licence: GNU General Public License v3
  10. * Copyright (C) 2010, Scott Evans
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see http://www.gnu.org/licenses/
  24. *
  25. * Title: lib_upload
  26. *
  27. * @package Pixie
  28. * @copyright 2008-2010 Scott Evans
  29. * @author Scott Evans
  30. * @author Sam Collett
  31. * @author Tony White
  32. * @author Isa Worcs
  33. * @link http://www.getpixie.co.uk
  34. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3
  35. *
  36. */
  37. //------------------------------------------------------------------
  38. // This new Pixie function is used by both uploaders to inform the user their max upload file size php setting
  39. // The failing of a too large file still needs to be logged. Someone please do it. Currently it just fails silently with no error message.
  40. /* This file needs language strings */
  41. /**
  42. * Convert a shorthand byte value from a PHP configuration directive to an integer value
  43. * @param string $value
  44. * @return int
  45. */
  46. function convertBytes($value) {
  47. if (is_numeric($value)) {
  48. return $value;
  49. } else {
  50. $value_length = strlen($value);
  51. $qty = substr($value, 0, $value_length - 1);
  52. $unit = strtolower(substr($value, $value_length - 1));
  53. switch ($unit) {
  54. case 'k':
  55. $qty *= 1024;
  56. break;
  57. case 'm':
  58. $qty *= 1048576;
  59. break;
  60. case 'g':
  61. $qty *= 1073741824;
  62. break;
  63. }
  64. return $qty;
  65. }
  66. }
  67. // End function convertBytes
  68. class file_upload {
  69. var $the_file;
  70. var $the_temp_file;
  71. var $upload_dir;
  72. var $replace;
  73. var $do_filename_check;
  74. var $max_length_filename = 100;
  75. var $extensions;
  76. var $ext_string;
  77. var $language;
  78. var $http_error;
  79. var $rename_file;
  80. var $file_copy;
  81. var $message = array();
  82. var $create_directory = TRUE;
  83. function file_upload() {
  84. $this->language = 'en';
  85. $this->rename_file = FALSE;
  86. $this->ext_string = "";
  87. }
  88. function show_error_string() {
  89. $msg_string = "";
  90. foreach ($this->message as $value) {
  91. $msg_string = $value . "";
  92. }
  93. return $msg_string;
  94. }
  95. function set_file_name($new_name = "") {
  96. if ($this->rename_file) {
  97. if ($this->the_file == "")
  98. return;
  99. $name = ($new_name == "") ? strtotime('now') : $new_name;
  100. $name = $name . $this->get_extension($this->the_file);
  101. } else {
  102. $name = $this->the_file;
  103. }
  104. return $name;
  105. }
  106. function upload($to_name = "") {
  107. $new_name = $this->set_file_name($to_name);
  108. if ($this->check_file_name($new_name)) {
  109. if ($this->validateExtension()) {
  110. if (is_uploaded_file($this->the_temp_file)) {
  111. $this->file_copy = $new_name;
  112. if ($this->move_upload($this->the_temp_file, $this->file_copy)) {
  113. $this->message[] = $this->error_text($this->http_error);
  114. if ($this->rename_file)
  115. $this->message[] = $this->error_text(16);
  116. return TRUE;
  117. }
  118. } else {
  119. $this->message[] = $this->error_text($this->http_error);
  120. return FALSE;
  121. }
  122. } else {
  123. $this->show_extensions();
  124. $this->message[] = $this->error_text(11);
  125. return FALSE;
  126. }
  127. } else {
  128. return FALSE;
  129. }
  130. }
  131. function check_file_name($the_name) {
  132. if ($the_name != "") {
  133. if (strlen($the_name) > $this->max_length_filename) {
  134. $this->message[] = $this->error_text(13);
  135. return FALSE;
  136. } else {
  137. if ($this->do_filename_check == 'y') {
  138. if (preg_match("/^[^<>:\"\/\\|\?\*]*$/i", $the_name)) {
  139. return TRUE;
  140. } else {
  141. $this->message[] = $this->error_text(12);
  142. return FALSE;
  143. }
  144. } else {
  145. return TRUE;
  146. }
  147. }
  148. } else {
  149. $this->message[] = $this->error_text(10);
  150. return FALSE;
  151. }
  152. }
  153. function get_extension($from_file) {
  154. $ext = strtolower(strrchr($from_file, '.'));
  155. return $ext;
  156. }
  157. function validateExtension() {
  158. $extension = $this->get_extension($this->the_file);
  159. $ext_array = $this->extensions;
  160. if (in_array($extension, $ext_array)) {
  161. return TRUE;
  162. } else {
  163. return FALSE;
  164. }
  165. }
  166. function show_extensions() {
  167. $this->ext_string = implode(" ", $this->extensions);
  168. }
  169. function move_upload($tmp_file, $new_file) {
  170. umask(0);
  171. if ($this->existing_file($new_file)) {
  172. $newfile = $this->upload_dir . $new_file;
  173. if ($this->check_dir($this->upload_dir)) {
  174. if (move_uploaded_file($tmp_file, $newfile)) {
  175. if ($this->replace == 'y') {
  176. //system("chmod 0777 $newfile");
  177. chmod($newfile, 0777);
  178. } else {
  179. // system("chmod 0755 $newfile");
  180. chmod($newfile, 0755);
  181. }
  182. return TRUE;
  183. } else {
  184. return FALSE;
  185. }
  186. } else {
  187. $this->message[] = $this->error_text(14);
  188. return FALSE;
  189. }
  190. } else {
  191. $this->message[] = $this->error_text(15);
  192. return FALSE;
  193. }
  194. }
  195. function check_dir($directory) {
  196. if (!is_dir($directory)) {
  197. if ($this->create_directory) {
  198. umask(0);
  199. mkdir($directory, 0777);
  200. return TRUE;
  201. } else {
  202. return FALSE;
  203. }
  204. } else {
  205. return TRUE;
  206. }
  207. }
  208. function existing_file($file_name) {
  209. if ($this->replace == 'y') {
  210. return TRUE;
  211. } else {
  212. if (file_exists($this->upload_dir . $file_name)) {
  213. return FALSE;
  214. } else {
  215. return TRUE;
  216. }
  217. }
  218. }
  219. function get_uploaded_file_info($name) {
  220. $str = 'File name: ' . basename($name) . "\n";
  221. $str .= "File size: " . filesize($name) . " bytes\n";
  222. if (function_exists('mime_content_type')) {
  223. $str .= 'Mime type: ' . mime_content_type($name) . "\n";
  224. }
  225. if ($img_dim = getimagesize($name)) {
  226. $str .= 'Image dimensions: x = ' . $img_dim[0] . 'px, y = ' . $img_dim[1] . "px\n";
  227. }
  228. return $str;
  229. }
  230. function del_temp_file($file) {
  231. $delete = @unlink($file);
  232. clearstatcache();
  233. if (@file_exists($file)) {
  234. $filesys = str_replace('/', '\\', $file);
  235. $delete = @system("del $filesys");
  236. clearstatcache();
  237. if (@file_exists($file)) {
  238. $delete = @chmod($file, 0775);
  239. $delete = @unlink($file);
  240. $delete = @system("del $filesys");
  241. }
  242. }
  243. }
  244. // some error (HTTP)reporting, change the messages or remove options if you like. need some better handling of this with language file
  245. function error_text($err_num) {
  246. switch ($this->language) {
  247. default:
  248. // start http errors
  249. $error[0] = "" . $this->the_file . ' was successfully uploaded.';
  250. $error[1] = 'The uploaded file exceeds the max. upload filesize directive in the server configuration.';
  251. $error[2] = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form.';
  252. $error[3] = 'The uploaded file was only partially uploaded. Try uploading the file again.';
  253. $error[4] = 'No file was uploaded.';
  254. // end http errors
  255. $error[10] = 'Please select a file for upload.';
  256. $error[11] = 'Only files with the following extensions are allowed: ' . $this->ext_string . "";
  257. $error[12] = 'The filename contains invalid characters. Use only alphanumerical chars and separate parts of the name (if needed) with an underscore. A valid filename ends with one dot followed by the extension.';
  258. $error[13] = 'The filename exceeds the maximum length of ' . $this->max_length_filename . ' characters.';
  259. $error[14] = 'The upload directory does not exist';
  260. $error[15] = 'A file with that name already exist.';
  261. $error[16] = 'The uploaded file was renamed to ' . $this->file_copy . '.';
  262. }
  263. return $error[$err_num];
  264. }
  265. }
  266. class muli_files extends file_upload {
  267. var $number_of_files = 0;
  268. var $names_array;
  269. var $tmp_names_array;
  270. var $error_array;
  271. var $wrong_extensions = 0;
  272. var $bad_filenames = 0;
  273. function extra_text($msg_num) {
  274. switch ($this->language) {
  275. default:
  276. $extra_msg[1] = 'Error for: ' . $this->the_file . "";
  277. $extra_msg[2] = 'You have tried to upload ' . $this->wrong_extensions . ' files with a bad extension, the following extensions are allowed: ' . $this->ext_string . "";
  278. $extra_msg[3] = 'Select a file for upload.';
  279. $extra_msg[4] = 'Select the file(s) for upload.';
  280. $extra_msg[5] = 'You have tried to upload ' . $this->bad_filenames . ' files with invalid characters inside the filename.';
  281. }
  282. return $extra_msg[$msg_num];
  283. }
  284. function count_files() {
  285. foreach ($this->names_array as $test) {
  286. if ($test != "") {
  287. $this->number_of_files++;
  288. }
  289. }
  290. if ($this->number_of_files > 0) {
  291. return TRUE;
  292. } else {
  293. return FALSE;
  294. }
  295. }
  296. function upload_multi_files() {
  297. $this->message = "";
  298. if ($this->count_files()) {
  299. foreach ($this->names_array as $key => $value) {
  300. if ($value != "") {
  301. $this->the_file = $value;
  302. $new_name = $this->set_file_name();
  303. if ($this->check_file_name($new_name)) {
  304. if ($this->validateExtension()) {
  305. $this->file_copy = $new_name;
  306. $this->the_temp_file = $this->tmp_names_array[$key];
  307. if (is_uploaded_file($this->the_temp_file)) {
  308. if ($this->move_upload($this->the_temp_file, $this->file_copy)) {
  309. $this->message[] = $this->error_text($this->error_array[$key]);
  310. if ($this->rename_file)
  311. $this->message[] = $this->error_text(16);
  312. sleep(1);
  313. }
  314. } else {
  315. $this->message[] = $this->extra_text(1);
  316. $this->message[] = $this->error_text($this->error_array[$key]);
  317. }
  318. } else {
  319. $this->wrong_extensions++;
  320. }
  321. } else {
  322. $this->bad_filenames++;
  323. }
  324. }
  325. }
  326. if ($this->bad_filenames > 0)
  327. $this->message[] = $this->extra_text(5);
  328. if ($this->wrong_extensions > 0) {
  329. $this->show_extensions();
  330. $this->message[] = $this->extra_text(2);
  331. }
  332. } else {
  333. $this->message[] = $this->extra_text(3);
  334. }
  335. }
  336. }
  337. ?>