| [ Index ] |
|
Code source de GeekLog 1.4.1 |
1 <?php 2 3 /* Reminder: always indent with 4 spaces (no tabs). */ 4 // +---------------------------------------------------------------------------+ 5 // | Geeklog 1.3 | 6 // +---------------------------------------------------------------------------+ 7 // | downloader.class.php | 8 // | | 9 // | Geeklog file download class library. | 10 // +---------------------------------------------------------------------------+ 11 // | Copyright (C) 2002-2005 by the following authors: | 12 // | | 13 // | Authors: Tony Bibbs - tony AT tonybibbs DOT com | 14 // +---------------------------------------------------------------------------+ 15 // | | 16 // | This program is free software; you can redistribute it and/or | 17 // | modify it under the terms of the GNU General Public License | 18 // | as published by the Free Software Foundation; either version 2 | 19 // | of the License, or (at your option) any later version. | 20 // | | 21 // | This program is distributed in the hope that it will be useful, | 22 // | but WITHOUT ANY WARRANTY; without even the implied warranty of | 23 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 24 // | GNU General Public License for more details. | 25 // | | 26 // | You should have received a copy of the GNU General Public License | 27 // | along with this program; if not, write to the Free Software Foundation, | 28 // | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 29 // | | 30 // +---------------------------------------------------------------------------+ 31 // 32 // $Id: downloader.class.php,v 1.12 2005/11/19 03:58:20 vinny Exp $ 33 34 /** 35 * This class allows you to download a file from outside the web tree. Many hooks 36 * around security and file types have been added for customization within any app 37 * 38 * @author Tony Bibbs 39 * 40 */ 41 class downloader 42 { 43 // Private Properties 44 /** 45 * @access private 46 */ 47 var $_errors; // Array 48 /** 49 * @access private 50 */ 51 var $_warnings; // Array 52 /** 53 * @access private 54 */ 55 var $_debugMessages; // Array 56 /** 57 * @access private 58 */ 59 var $_allowedExtensions; // Array 60 /** 61 * @access private 62 */ 63 var $_availableExtensions; // Array 64 /** 65 * @access private 66 */ 67 var $_allowedIPS; // Array 68 /** 69 * @access private 70 */ 71 var $_sourceDirectory; // String 72 /** 73 * @access private 74 */ 75 var $_logFile; // String 76 /** 77 * @access private 78 */ 79 var $_doLogging; // Boolean 80 /** 81 * @access private 82 */ 83 var $_debug; // Boolean 84 /** 85 * @access private 86 */ 87 var $_limitByIP; // Boolean 88 89 /** 90 * Constructor 91 * 92 */ 93 function downloader() 94 { 95 $this->_errors = array(); 96 $this->_warnings = array(); 97 $this->_debugMessages = array(); 98 $this->_allowedExtensions = array(); 99 $this->_availableExtensions = array(); 100 $this->_sourceDirectory = ''; 101 $this->_logFile = ''; 102 $this->_doLogging = false; 103 $this->_limitByIP = false; 104 105 $this->_setAvailableExtensions (); 106 107 } 108 109 // PRIVATE METHODS 110 111 /** 112 * Adds a warning that was encountered 113 * 114 * @param string $warningTextText of warning 115 * @access private 116 * 117 */ 118 function _addWarning($warningText) 119 { 120 $nwarnings = count($this->_warnings); 121 $nwarnings = $nwarnings + 1; 122 $this->_warnings[$nwarnings] = $warningText; 123 if ($this->loggingEnabled()) { 124 $this->_logItem('Warning',$warningText); 125 } 126 } 127 128 /** 129 * Adds an error that was encountered 130 * 131 * @param string $errorText Text of error 132 * @access private 133 * 134 */ 135 function _addError($errorText) 136 { 137 $nerrors = count($this->_errors); 138 $nerrors = $nerrors + 1; 139 $this->_errors[$nerrors] = $errorText; 140 if ($this->loggingEnabled()) { 141 $this->_logItem('Error',$errorText); 142 } 143 } 144 145 /** 146 * Adds a debug message 147 * 148 * @param string $debugText Text of debug message 149 * @access private 150 * 151 */ 152 function _addDebugMsg($debugText) 153 { 154 $nmsgs = count($this->_debugMessages); 155 $nmsgs = $nmsgs + 1; 156 $this->_debugMessages[$nmsgs] = $debugText; 157 if ($this->loggingEnabled()) { 158 $this->_logItem('Debug',$debugText); 159 } 160 } 161 162 /** 163 * Logs an item to the log file 164 * 165 * @param string $logtype can be 'warning' or 'error' 166 * @param string $text Text to log to log file 167 * @return boolean true on success otherwise false 168 * @access private 169 * 170 */ 171 function _logItem($logtype, $text) 172 { 173 $timestamp = strftime("%c"); 174 if (!$file = fopen($this->_logFile,a)) { 175 // couldn't open log file for writing so let's disable logging and add an error 176 $this->setLogging(false); 177 $this->_addError('Error writing to log file: ' . $this->_logFile . '. Logging has been disabled'); 178 return false; 179 } 180 fputs ($file, "$timestamp - $logtype: $text \n"); 181 fclose($file); 182 return true; 183 } 184 185 /** 186 * Defines superset of available Mime types. 187 * 188 * @param array $extensions string array of valid mime types this object will accept 189 * 190 */ 191 function _setAvailableExtensions($extensions = array()) 192 { 193 if (sizeof($extensions) == 0) { 194 $this->_availableMimeTypes = 195 array( 196 'tgz' => 'application/x-gzip-compressed', 197 'gz' => 'application/x-gzip-compressed', 198 'zip' => 'application/x-zip-compresseed', 199 'tar' => 'application/x-tar', 200 'php' => 'text/plain', 201 'phps' => 'text/plain', 202 'txt' => 'text/plain', 203 'html' => 'text/html', 204 'htm' => 'text/html', 205 'bmp' => 'image/bmp', 206 'ico' => 'image/bmp', 207 'gif' => 'image/gif', 208 'jpg' => 'image/jpeg', 209 'jpeg' => 'image/jpeg', 210 'png' => 'image/x-png', 211 'mp3' => 'audio/mpeg', 212 'wav' => 'audio/wav', 213 'pdf' => 'application/pdf', 214 'swf' => 'application/x-shockwave-flash', 215 'doc' => 'application/msword', 216 'xls' => 'application/vnd.ms-excel', 217 'exe' => 'application/octet-stream' 218 ); 219 } else { 220 $this->_availableMimeTypes = $extensions; 221 } 222 223 $this->_availableExtensions = array (); 224 foreach ($this->_availableMimeTypes as $ext => $mime) { 225 $this->_availableExtensions[] = $ext; 226 } 227 } 228 229 // Public Methods 230 231 /** 232 * Extra security option that forces all attempts to upload a file to be done 233 * so from a set of VERY specific IP's. This is only good for those who are 234 * paranoid 235 * 236 * @param $array $validIPS Array of valid IP addresses to allow file uploads from 237 * @return boolean returns true on success otherwise false 238 * 239 */ 240 function limitByIP($validIPS = array('127.0.0.1')) 241 { 242 if (is_array($validIPS)) { 243 $this->_limitByIP = true; 244 $this->_allowedIPS = $validIPS; 245 return true; 246 } else { 247 $this->_addError('Bad call to method limitByIP(), must pass array of valid IP addresses'); 248 return false; 249 } 250 } 251 252 /** 253 * Sets log file 254 * 255 * @param string $fileName fully qualified path to log files 256 * @return boolean true on success otherwise false 257 * 258 */ 259 function setLogFile($logFile = '') 260 { 261 if (empty($logFile) OR !file_exists($logFile)) { 262 // Log file doesn't exist, produce warning 263 $this->_addWarning('Log file, ' . $logFile . ' does not exists, setLogFile() method failed'); 264 $this->_doLogging = false; 265 return false; 266 } 267 $this->_logFile = $logFile; 268 return true; 269 } 270 271 /** 272 * Enables/disables logging of errors and warnings 273 * 274 * @param boolean $switch flag, true or false 275 * 276 */ 277 function setLogging($switch) 278 { 279 if ($switch AND !empty($this->_logFile)) { 280 $this->_doLogging = true; 281 } else { 282 if ($switch AND empty($this->_logFile)) { 283 $this->_addWarning('Unable to enable logging because no log file was set. Use setLogFile() method'); 284 } 285 $this->_doLogging = false; 286 } 287 } 288 289 /** 290 * Returns whether or not logging is enabled 291 * 292 * @return boolean true if logging is enabled otherwise false 293 * 294 */ 295 function loggingEnabled() 296 { 297 return $this->_doLogging; 298 } 299 300 /** 301 * Will force the debug messages in this class to be 302 * printed 303 * 304 * @param boolean $switch flag, true or false 305 * 306 */ 307 function setDebug($switch) 308 { 309 if ($switch) { 310 $this->_debug = true; 311 // setting debugs implies logging is on too 312 $this->setLogging(true); 313 } else { 314 $this->_debug = false; 315 } 316 } 317 318 /** 319 * This function will print any errors out. This is useful in debugging 320 * 321 * @param boolean $verbose will print errors to web browser if true 322 * @return boolean string of all errors 323 * 324 */ 325 function printErrors($verbose=true) 326 { 327 if (isset($this->_errors) AND is_array($this->_errors)) { 328 $retval = ''; 329 reset($this->_errors); 330 $nerrors = count($this->_errors); 331 for ($i = 1; $i <= $nerrors; $i++) { 332 if ($verbose) { 333 print current($this->_errors) . "<BR>\n"; 334 } else { 335 $retval .= current($this->_errors) . "<BR>\n"; 336 } 337 next($this->_errors); 338 } 339 return $retval; 340 } 341 } 342 343 /** 344 * This function will print any warnings out. This is useful in debugging 345 * 346 */ 347 function printWarnings() 348 { 349 if (isset($this->_warnings) AND is_array($this->_warnings)) { 350 reset($this->_warnings); 351 $nwarnings = count($this->_warnings); 352 for ($i = 1; $i <= $nwarnings; $i++) { 353 print current($this->_warnings) . "<BR>\n"; 354 next($this->_warnings); 355 } 356 } 357 } 358 359 /** 360 * This function will print any debmug messages out. 361 * 362 */ 363 function printDebugMsgs() 364 { 365 if (isset($this->_debugMessages) AND is_array($this->_debugMessages)) { 366 reset($this->_debugMessages); 367 $nmsgs = count($this->_debugMessages); 368 for ($i = 1; $i <= $nmsgs; $i++) { 369 print current($this->_debugMessages) . "<BR>\n"; 370 next($this->_debugMessages); 371 } 372 } 373 } 374 375 /** 376 * Returns if any errors have been encountered thus far 377 * 378 * @return boolean True if errors occurred otherwise false 379 * 380 */ 381 function areErrors() 382 { 383 if (count($this->_errors) > 0) { 384 return true; 385 } else { 386 return false; 387 } 388 } 389 390 /** 391 * Sets allowed mime types for this instance 392 * 393 * @param array $allowedMimeTypes Array of allowed mime types 394 * 395 */ 396 function setAllowedExtensions($validExtensions = array()) 397 { 398 // This is a subset of _availableMimetypes. Go ahead and make sure 399 // all the mime types passed to this function are in the 400 // available list 401 foreach ($validExtensions as $ext => $mime) { 402 if (!in_array ($mime, $this->_availableMimeTypes)) { 403 $this->_addError('extension, ' . $ext . ' is not in the list of available file types for download'); 404 return; 405 } 406 } 407 $this->_allowedExtensions = $validExtensions; 408 } 409 410 /** 411 * Gets allowed mime types for this instance 412 * 413 * @return array array of allowed mime types/file extensions 414 * 415 */ 416 function getAllowedExtensions() 417 { 418 return $this->_allowedExtensions; 419 } 420 421 /** 422 * Checks to see that mime type for current file is allowed for upload 423 * 424 * @param string $extension Verifies file extension is allowed for download 425 * @return boolean true if allowed otherwise false 426 * 427 */ 428 function checkExtension($extension) 429 { 430 if (!in_array($extension,array_keys($this->getAllowedExtensions()))) { 431 $this->_addError('File type, .' . $extension . ', not in list of allowed file types available for download'); 432 return false; 433 } else { 434 return true; 435 } 436 } 437 438 /** 439 * Sets file upload path 440 * 441 * @param string $uploadDir Directory on server to store uploaded files 442 * @return boolean true on success otherwise false 443 * 444 */ 445 function setPath($uploadDir) 446 { 447 if (!is_dir($uploadDir)) { 448 $this->_addError('Specified source directory, ' . $uploadDir . ' is not a valid directory'); 449 return false; 450 } 451 452 if (!is_readable($uploadDir)) { 453 $this->_addError('Specified source directory, ' . $uploadDir . ' exists but is not readable'); 454 return false; 455 } 456 457 $this->_sourceDirectory = $uploadDir; 458 459 return true; 460 } 461 462 /** 463 * Returns directory to upload to 464 * 465 * @return string returns directory where files for downloading reside 466 * 467 */ 468 function getPath() 469 { 470 return $this->_sourceDirectory; 471 } 472 473 /** 474 * Attempts to dowload a file 475 * 476 * @param $string $fileName file to download without path 477 * @return boolean true on success otherwise false 478 * 479 */ 480 function downloadFile($fileName) 481 { 482 if (strstr( PHP_OS, "WIN")) { // Added as test1 below was failing on Windows platforms 483 $strPathSeparator = '\\'; 484 $this->_sourceDirectory = str_replace('/','\\',$this->_sourceDirectory); 485 } else { 486 $strPathSeparator = '/'; 487 } 488 489 if(!is_file($this->_sourceDirectory . $fileName)) { 490 echo "<br>{$this->sourceDirectory}{$filename} does not exist"; 491 } 492 493 494 // Ensure file exists and is accessible 495 if(!is_file($this->_sourceDirectory . $fileName) OR 496 ($this->_sourceDirectory <> (dirname($this->_sourceDirectory . $strPathSeparator .$fileName) .$strPathSeparator)) ) { 497 $this->_addError('Specified file ' . $this->_sourceDirectory . $fileName . ' does not exist or is not accessible'); 498 return false; 499 } 500 501 // Make sure file is readable - test 2 502 clearstatcache(); 503 if (!is_readable($this->_sourceDirectory . $fileName)) { 504 $this->_addError('Specified file, ' . $this->_sourceDirectory . $fileName . ' exists but is not readable'); 505 return false; 506 } 507 508 // OK, file is valid, get file extension 509 $pos = strrpos($fileName,'.') + 1; 510 $fextension = substr($fileName, $pos); 511 512 // Send headers. 513 if ($this->checkExtension($fextension)) { 514 // Display file inside browser. 515 header('Content-Type: ' . $this->_availableMimeTypes[$fextension]); 516 header('Content-transfer-encoding: binary'); 517 header('Content-length: ' . filesize($this->_sourceDirectory . $fileName)); 518 header('Content-Disposition: attachment; filename="' . $fileName . '"'); 519 520 // Send file contents. 521 $fp = fopen($this->_sourceDirectory . $fileName, 'rb'); 522 523 fpassthru( $fp ); 524 } 525 526 return true; 527 } 528 529 } 530 531 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Wed Nov 21 12:27:40 2007 | par Balluche grâce à PHPXref 0.7 |
|