PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Quản lý website trường trung học phổ thông PHP/lc1/admin/modules/upload/functions.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 413 lines | 310 code | 55 blank | 48 comment | 49 complexity | 7b5d16255977ad91e8a184937713350d MD5 | raw file
  1. <?php
  2. /**
  3. * @Project NUKEVIET 3.0
  4. * @Author VINADES.,JSC (contact@vinades.vn)
  5. * @copyright 2009
  6. * @createdate 12/31/2009 2:29
  7. */
  8. if ( ! defined( 'NV_ADMIN' ) or ! defined( 'NV_MAINFILE' ) or ! defined( 'NV_IS_MODADMIN' ) ) die( 'Stop!!!' );
  9. if ( $module_name != "upload" ) return;
  10. define( 'NV_IS_FILE_ADMIN', true );
  11. $allow_func = array( 'main', 'imglist', 'delimg', 'createimg', 'dlimg', 'renameimg', 'moveimg', 'folderlist', 'delfolder', 'renamefolder', 'createfolder', 'quickupload', 'upload' );
  12. #################################################################################
  13. /**
  14. * nv_check_allow_upload_dir()
  15. *
  16. * @param mixed $dir
  17. * @return
  18. */
  19. function nv_check_allow_upload_dir( $dir )
  20. {
  21. global $site_mods, $allow_upload_dir, $admin_info;
  22. $dir = trim( $dir );
  23. if ( empty( $dir ) ) return array();
  24. $dir = str_replace( "\\", "/", $dir );
  25. $dir = rtrim( $dir, "/" );
  26. $arr_dir = explode( "/", $dir );
  27. $level = array();
  28. if ( in_array( $arr_dir[0], $allow_upload_dir ) )
  29. {
  30. if ( defined( 'NV_IS_SPADMIN' ) )
  31. {
  32. $level['view_dir'] = true;
  33. if ( $admin_info['allow_create_subdirectories'] )
  34. {
  35. $level['create_dir'] = true;
  36. }
  37. if ( $admin_info['allow_modify_subdirectories'] and ! in_array( $dir, $allow_upload_dir ) )
  38. {
  39. $level['rename_dir'] = true;
  40. $level['delete_dir'] = true;
  41. if ( isset( $arr_dir[1] ) and ! empty( $arr_dir[1] ) and isset( $site_mods[$arr_dir[1]] ) and ! isset( $arr_dir[2] ) )
  42. {
  43. unset( $level['rename_dir'], $level['delete_dir'] );
  44. }
  45. }
  46. if ( ! empty( $admin_info['allow_files_type'] ) )
  47. {
  48. $level['upload_file'] = true;
  49. }
  50. if ( $admin_info['allow_modify_files'] )
  51. {
  52. $level['create_file'] = true;
  53. $level['rename_file'] = true;
  54. $level['delete_file'] = true;
  55. $level['move_file'] = true;
  56. }
  57. } elseif ( isset( $arr_dir[1] ) and ! empty( $arr_dir[1] ) and isset( $site_mods[$arr_dir[1]] ) )
  58. {
  59. $level['view_dir'] = true;
  60. if ( $admin_info['allow_create_subdirectories'] )
  61. {
  62. $level['create_dir'] = true;
  63. }
  64. if ( isset( $arr_dir[2] ) and ! empty( $arr_dir[2] ) and $admin_info['allow_modify_subdirectories'] )
  65. {
  66. $level['rename_dir'] = true;
  67. $level['delete_dir'] = true;
  68. }
  69. if ( ! empty( $admin_info['allow_files_type'] ) )
  70. {
  71. $level['upload_file'] = true;
  72. }
  73. if ( $admin_info['allow_modify_files'] )
  74. {
  75. $level['create_file'] = true;
  76. $level['rename_file'] = true;
  77. $level['delete_file'] = true;
  78. $level['move_file'] = true;
  79. }
  80. }
  81. if ( preg_match( "/^([\d]{4})\_([\d]{1,2})$/", $arr_dir[count( $arr_dir ) - 1] ) )
  82. {
  83. unset( $level['rename_dir'], $level['delete_dir'] );
  84. }
  85. }
  86. return $level;
  87. }
  88. /**
  89. * nv_check_path_upload()
  90. *
  91. * @param mixed $path
  92. * @return
  93. */
  94. function nv_check_path_upload( $path )
  95. {
  96. global $allow_upload_dir;
  97. $path = htmlspecialchars( trim( $path ), ENT_QUOTES );
  98. $path = rtrim( $path, "/" );
  99. if ( empty( $path ) ) return "";
  100. $path = NV_ROOTDIR . "/" . $path;
  101. if ( ( $path = realpath( $path ) ) === false ) return "";
  102. $path = str_replace( "\\", "/", $path );
  103. $path = str_replace( NV_ROOTDIR . "/", "", $path );
  104. $result = false;
  105. foreach ( $allow_upload_dir as $dir )
  106. {
  107. $dir = nv_preg_quote( $dir );
  108. if ( preg_match( "/^" . $dir . "/", $path ) )
  109. {
  110. $result = true;
  111. break;
  112. }
  113. }
  114. if ( $result === false ) return "";
  115. return $path;
  116. }
  117. /**
  118. * nv_delete_cache_upload()
  119. *
  120. * @param mixed $realpath
  121. * @return
  122. */
  123. function nv_delete_cache_upload( $path )
  124. {
  125. $tempFile = NV_ROOTDIR . "/" . NV_FILES_DIR . "/dcache/" . md5( $path );
  126. if ( file_exists( $tempFile ) )
  127. {
  128. @nv_deletefile( $tempFile );
  129. }
  130. $files = scandir( NV_ROOTDIR . '/' . $path );
  131. $files = array_diff( $files, array( ".", ".." ) );
  132. if ( count( $files ) )
  133. {
  134. foreach ( $files as $file )
  135. {
  136. if ( is_dir( NV_ROOTDIR . '/' . $path . '/' . $file ) )
  137. {
  138. nv_delete_cache_upload( $path . '/' . $file );
  139. }
  140. else
  141. {
  142. $md5_view_image = NV_ROOTDIR . "/" . NV_FILES_DIR . "/images/" . md5( $path . '/' . $file ) . "." . nv_getextension( $file );
  143. if ( file_exists( $md5_view_image ) )
  144. {
  145. @nv_deletefile( $md5_view_image );
  146. }
  147. }
  148. }
  149. }
  150. }
  151. /**
  152. * nv_get_viewImage()
  153. *
  154. * @param mixed $fileName
  155. * @param integer $w
  156. * @param integer $h
  157. * @return
  158. */
  159. function nv_get_viewImage( $fileName, $w = 80, $h = 80 )
  160. {
  161. $ext = nv_getextension( $fileName );
  162. $md5_view_image = md5( $fileName );
  163. $viewDir = NV_FILES_DIR . '/images';
  164. $viewFile = $viewDir . '/' . $md5_view_image . '.' . $ext;
  165. if ( file_exists( NV_ROOTDIR . '/' . $viewFile ) )
  166. {
  167. $size = @getimagesize( NV_ROOTDIR . '/' . $viewFile );
  168. return array( $viewFile, $size[0], $size[1] );
  169. }
  170. include_once ( NV_ROOTDIR . "/includes/class/image.class.php" );
  171. $image = new image( NV_ROOTDIR . '/' . $fileName, NV_MAX_WIDTH, NV_MAX_HEIGHT );
  172. $image->resizeXY( $w, $h );
  173. $image->save( NV_ROOTDIR . '/' . $viewDir, $md5_view_image, 75 );
  174. $create_Image_info = $image->create_Image_info;
  175. $error = $image->error;
  176. $image->close();
  177. if ( empty( $error ) )
  178. {
  179. return array( $viewFile, $create_Image_info['width'], $create_Image_info['height'] );
  180. }
  181. return false;
  182. }
  183. /**
  184. * nv_getFileInfo()
  185. *
  186. * @param mixed $pathimg
  187. * @param mixed $file
  188. * @return
  189. */
  190. function nv_getFileInfo( $pathimg, $file )
  191. {
  192. global $array_images, $array_flash, $array_archives, $array_documents;
  193. clearstatcache();
  194. unset( $matches );
  195. preg_match( "/([a-zA-Z0-9\.\-\_]+)\.([a-zA-Z0-9]+)$/", $file, $matches );
  196. $info = array();
  197. $info[0] = $file;
  198. $max = 16;
  199. if ( strlen( $file ) > $max )
  200. {
  201. $info[0] = substr( $matches[1], 0, ( $max - 3 - strlen( $matches[2] ) ) ) . "..." . $matches[2];
  202. }
  203. $info[1] = $matches[2];
  204. $info[2] = "file";
  205. $stat = @stat( NV_ROOTDIR . '/' . $pathimg . '/' . $file );
  206. $info[3] = $stat['size'];
  207. $info[4] = 'images/file.gif';
  208. $info[5] = 32;
  209. $info[6] = 32;
  210. $info[7] = "|";
  211. if ( in_array( $matches[2], $array_images ) )
  212. {
  213. $size = @getimagesize( NV_ROOTDIR . '/' . $pathimg . '/' . $file );
  214. $info[2] = "image";
  215. $info[4] = $pathimg . '/' . $file;
  216. $info[5] = $size[0];
  217. $info[6] = $size[1];
  218. $info[7] = $size[0] . "|" . $size[1];
  219. if ( $size[0] > 80 or $size[1] > 80 )
  220. {
  221. if ( ( $_src = nv_get_viewImage( $pathimg . '/' . $file, 80, 80 ) ) !== false )
  222. {
  223. $info[4] = $_src[0];
  224. $info[5] = $_src[1];
  225. $info[6] = $_src[2];
  226. }
  227. else
  228. {
  229. if ( $info[5] > 80 )
  230. {
  231. $info[6] = round( 80 / $info[5] * $info[6] );
  232. $info[5] = 80;
  233. }
  234. if ( $info[6] > 80 )
  235. {
  236. $info[5] = round( 80 / $info[6] * $info[5] );
  237. $info[6] = 80;
  238. }
  239. }
  240. }
  241. } elseif ( in_array( $matches[2], $array_flash ) )
  242. {
  243. $info[2] = "flash";
  244. $info[4] = 'images/flash.gif';
  245. if ( $matches[2] == "swf" )
  246. {
  247. $size = @getimagesize( NV_ROOTDIR . '/' . $pathimg . '/' . $file );
  248. if ( isset( $size, $size[0], $size[1] ) )
  249. {
  250. $info[7] = $size[0] . "|" . $size[1];
  251. }
  252. }
  253. } elseif ( in_array( $matches[2], $array_archives ) )
  254. {
  255. $info[4] = 'images/zip.gif';
  256. } elseif ( in_array( $matches[2], $array_documents ) )
  257. {
  258. $info[4] = 'images/doc.gif';
  259. }
  260. $info[8] = 0;
  261. $info[9] = $stat['mtime'];
  262. return $info;
  263. }
  264. /**
  265. * nv_filesList()
  266. *
  267. * @param mixed $pathimg
  268. * @param bool $refresh
  269. * @param string $newFile
  270. * @return
  271. */
  272. function nv_filesList( $pathimg, $refresh, $newFile = "", $delFile = "" )
  273. {
  274. global $array_hidefolders, $admin_info;
  275. $md5 = md5( $pathimg );
  276. $tempFile = NV_ROOTDIR . "/" . NV_FILES_DIR . "/dcache/" . $md5;
  277. $file_exists = file_exists( $tempFile );
  278. $results = array();
  279. if ( $file_exists )
  280. {
  281. $results = file_get_contents( $tempFile );
  282. $results = unserialize( $results );
  283. }
  284. else
  285. {
  286. $refresh = true;
  287. }
  288. if ( is_dir( NV_ROOTDIR . "/" . $pathimg ) )
  289. {
  290. if ( $refresh )
  291. {
  292. if ( $dh = opendir( NV_ROOTDIR . "/" . $pathimg ) )
  293. {
  294. $files = array();
  295. while ( ( $file = readdir( $dh ) ) !== false )
  296. {
  297. if ( in_array( $file, $array_hidefolders ) ) continue;
  298. if ( preg_match( "/([a-zA-Z0-9\.\-\_]+)\.([a-zA-Z0-9]+)$/", $file ) )
  299. {
  300. $files[] = $file;
  301. $info = nv_getFileInfo( $pathimg, $file );
  302. if ( ! empty( $newFile ) and $file == $newFile )
  303. {
  304. $info[8] = $admin_info['userid'];
  305. $info[9] = NV_CURRENTTIME;
  306. }
  307. else
  308. {
  309. if ( isset( $results[$file][8] ) ) $info[8] = $results[$file][8];
  310. if ( isset( $results[$file][9] ) ) $info[9] = $results[$file][9];
  311. }
  312. $results[$file] = $info;
  313. }
  314. }
  315. closedir( $dh );
  316. $files = array_flip( $files );
  317. $results = array_intersect_key( $results, $files );
  318. }
  319. ksort( $results );
  320. file_put_contents( $tempFile, serialize( $results ) );
  321. }
  322. else
  323. {
  324. if ( ! empty( $newFile ) )
  325. {
  326. $info = nv_getFileInfo( $pathimg, $newFile );
  327. $info[8] = $admin_info['userid'];
  328. $info[9] = NV_CURRENTTIME;
  329. $results[$newFile] = $info;
  330. ksort( $results );
  331. file_put_contents( $tempFile, serialize( $results ) );
  332. }
  333. if ( ! empty( $delFile ) )
  334. {
  335. unset( $results[$delFile] );
  336. file_put_contents( $tempFile, serialize( $results ) );
  337. }
  338. }
  339. }
  340. return $results;
  341. }
  342. $allow_upload_dir = array( 'images', NV_UPLOADS_DIR );
  343. $array_hidefolders = array( ".svn", "CVS", ".", "..", "index.html", ".htaccess", ".tmp" );
  344. $array_images = array( "gif", "jpg", "jpeg", "pjpeg", "png" );
  345. $array_flash = array( 'swf', 'swc', 'flv' );
  346. $array_archives = array( 'rar', 'zip', 'tar' );
  347. $array_documents = array( 'doc', 'xls', 'chm', 'pdf', 'docx', 'xlsx' );
  348. $dirlistCache = NV_ROOTDIR . "/" . NV_FILES_DIR . "/dcache/dirlist-" . md5( implode( $allow_upload_dir ) );
  349. if ( ! file_exists( $dirlistCache ) or ( $nv_Request->isset_request( 'dirListRefresh', 'get' ) and filemtime( $dirlistCache ) < ( NV_CURRENTTIME - 30 ) ) )
  350. {
  351. $dirlist = nv_loadUploadDirList();
  352. }
  353. else
  354. {
  355. $dirlist = file_get_contents( $dirlistCache );
  356. $dirlist = unserialize( $dirlist );
  357. }
  358. ?>