PageRenderTime 23ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/wrapper/3rdParty/assets/fckeditor/editor/filemanager/upload/php/upload.php

https://bitbucket.org/khanh-yuranga/hwh_staging
PHP | 130 lines | 68 code | 24 blank | 38 comment | 19 complexity | cd6787c764ab343129efbdd172c55270 MD5 | raw file
  1. <?php
  2. /*
  3. * FCKeditor - The text editor for Internet - http://www.fckeditor.net
  4. * Copyright (C) 2003-2007 Frederico Caldeira Knabben
  5. *
  6. * == BEGIN LICENSE ==
  7. *
  8. * Licensed under the terms of any of the following licenses at your
  9. * choice:
  10. *
  11. * - GNU General Public License Version 2 or later (the "GPL")
  12. * http://www.gnu.org/licenses/gpl.html
  13. *
  14. * - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
  15. * http://www.gnu.org/licenses/lgpl.html
  16. *
  17. * - Mozilla Public License Version 1.1 or later (the "MPL")
  18. * http://www.mozilla.org/MPL/MPL-1.1.html
  19. *
  20. * == END LICENSE ==
  21. *
  22. * This is the "File Uploader" for PHP.
  23. */
  24. require('config.php') ;
  25. require('util.php') ;
  26. // This is the function that sends the results of the uploading process.
  27. function SendResults( $errorNumber, $fileUrl = '', $fileName = '', $customMsg = '' )
  28. {
  29. echo '<script type="text/javascript">' ;
  30. echo 'window.parent.OnUploadCompleted(' . $errorNumber . ',"' . str_replace( '"', '\\"', $fileUrl ) . '","' . str_replace( '"', '\\"', $fileName ) . '", "' . str_replace( '"', '\\"', $customMsg ) . '") ;' ;
  31. echo '</script>' ;
  32. exit ;
  33. }
  34. // Check if this uploader has been enabled.
  35. if ( !$Config['Enabled'] )
  36. SendResults( '1', '', '', 'This file uploader is disabled. Please check the "editor/filemanager/upload/php/config.php" file' ) ;
  37. // Check if the file has been correctly uploaded.
  38. if ( !isset( $_FILES['NewFile'] ) || is_null( $_FILES['NewFile']['tmp_name'] ) || $_FILES['NewFile']['name'] == '' )
  39. SendResults( '202' ) ;
  40. // Get the posted file.
  41. $oFile = $_FILES['NewFile'] ;
  42. // Get the uploaded file name extension.
  43. $sFileName = $oFile['name'] ;
  44. // Replace dots in the name with underscores (only one dot can be there... security issue).
  45. if ( $Config['ForceSingleExtension'] )
  46. $sFileName = preg_replace( '/\\.(?![^.]*$)/', '_', $sFileName ) ;
  47. $sOriginalFileName = $sFileName ;
  48. // Get the extension.
  49. $sExtension = substr( $sFileName, ( strrpos($sFileName, '.') + 1 ) ) ;
  50. $sExtension = strtolower( $sExtension ) ;
  51. // The the file type (from the QueryString, by default 'File').
  52. $sType = isset( $_GET['Type'] ) ? $_GET['Type'] : 'File' ;
  53. // Check if it is an allowed type.
  54. if ( !in_array( $sType, array('File','Image','Flash','Media') ) )
  55. SendResults( 1, '', '', 'Invalid type specified' ) ;
  56. // Get the allowed and denied extensions arrays.
  57. $arAllowed = $Config['AllowedExtensions'][$sType] ;
  58. $arDenied = $Config['DeniedExtensions'][$sType] ;
  59. // Check if it is an allowed extension.
  60. if ( ( count($arAllowed) > 0 && !in_array( $sExtension, $arAllowed ) ) || ( count($arDenied) > 0 && in_array( $sExtension, $arDenied ) ) )
  61. SendResults( '202' ) ;
  62. $sErrorNumber = '0' ;
  63. $sFileUrl = '' ;
  64. // Initializes the counter used to rename the file, if another one with the same name already exists.
  65. $iCounter = 0 ;
  66. // Get the target directory.
  67. if ( isset( $Config['UserFilesAbsolutePath'] ) && strlen( $Config['UserFilesAbsolutePath'] ) > 0 )
  68. $sServerDir = $Config['UserFilesAbsolutePath'] ;
  69. else
  70. $sServerDir = GetRootPath() . $Config["UserFilesPath"] ;
  71. if ( $Config['UseFileType'] )
  72. $sServerDir .= strtolower($sType) . '/' ;
  73. //check for the directory before uploading the file
  74. if(!is_dir($sServerDir))
  75. {
  76. mkdir($sServerDir, 0777);
  77. }
  78. while ( true )
  79. {
  80. // Compose the file path.
  81. $sFilePath = $sServerDir . $sFileName ;
  82. // If a file with that name already exists.
  83. if ( is_file( $sFilePath ) )
  84. {
  85. $iCounter++ ;
  86. $sFileName = RemoveExtension( $sOriginalFileName ) . '(' . $iCounter . ').' . $sExtension ;
  87. $sErrorNumber = '201' ;
  88. }
  89. else
  90. {
  91. move_uploaded_file( $oFile['tmp_name'], $sFilePath ) ;
  92. if ( is_file( $sFilePath ) )
  93. {
  94. $oldumask = umask(0) ;
  95. chmod( $sFilePath, 0777 ) ;
  96. umask( $oldumask ) ;
  97. }
  98. if ( $Config['UseFileType'] )
  99. $sFileUrl = $Config["UserFilesPath"] . strtolower($sType) . '/' . $sFileName ;
  100. else
  101. $sFileUrl = $Config["UserFilesPath"] . $sFileName ;
  102. break ;
  103. }
  104. }
  105. SendResults( $sErrorNumber, $sFileUrl, $sFileName ) ;
  106. ?>