PageRenderTime 28ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/core/filesystem_functions.php

http://viet-group.googlecode.com/
PHP | 859 lines | 768 code | 34 blank | 57 comment | 44 complexity | 30b203ecbbf117ca6cc5a2cf88f8b480 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * @Project NUKEVIET 3.0
  4. * @Author VINADES.,JSC (contact@vinades.vn)
  5. * @Copyright (C) 2010 VINADES.,JSC. All rights reserved
  6. * @Createdate 2/9/2010, 2:33
  7. */
  8. if ( ! defined( 'NV_MAINFILE' ) ) die( 'Stop!!!' );
  9. /**
  10. * nv_parse_ini_file()
  11. *
  12. * @param mixed $filename
  13. * @param bool $process_sections
  14. * @return
  15. */
  16. function nv_parse_ini_file ( $filename, $process_sections = false )
  17. {
  18. $process_sections = ( bool )$process_sections;
  19. if ( ! file_exists( $filename ) || ! is_readable( $filename ) ) return false;
  20. $data = file( $filename );
  21. $ini = array();
  22. $section = '';
  23. foreach ( $data as $line )
  24. {
  25. $line = trim( $line );
  26. if ( empty( $line ) || preg_match( "/^;/", $line ) ) continue;
  27. unset( $match );
  28. if ( preg_match( "/^\[(.*?)\]$/", $line, $match ) )
  29. {
  30. $section = $match[1];
  31. continue;
  32. }
  33. if ( ! strpos( $line, "=" ) ) continue;
  34. list( $key, $value ) = explode( "=", $line );
  35. $key = trim( $key );
  36. $value = trim( $value );
  37. $value = str_replace( array( '"', "'" ), array( "", "" ), $value );
  38. if ( $process_sections && ! empty( $section ) )
  39. {
  40. unset( $match );
  41. if ( preg_match( "/^(.*?)\[\]$/", $key, $match ) )
  42. {
  43. $ini[$section][$match[1]][] = $value;
  44. }
  45. else
  46. {
  47. $ini[$section][$key] = $value;
  48. }
  49. }
  50. else
  51. {
  52. unset( $match );
  53. if ( preg_match( "/^(.*?)\[(.*?)\]$/", $key, $match ) )
  54. {
  55. $ini[$match[1]][] = $value;
  56. }
  57. else
  58. {
  59. $ini[$key] = $value;
  60. }
  61. }
  62. }
  63. return $ini;
  64. }
  65. /**
  66. * nv_scandir()
  67. *
  68. * @param mixed $directory
  69. * @param mixed $pattern
  70. * @param integer $sorting_order
  71. * @return
  72. */
  73. function nv_scandir ( $directory, $pattern, $sorting_order = 0 )
  74. {
  75. $return = array();
  76. if ( is_dir( $directory ) )
  77. {
  78. if ( $dh = opendir( $directory ) )
  79. {
  80. while ( ( $file = readdir( $dh ) ) !== false )
  81. {
  82. if ( preg_match( "/^\.(.*)$/", $file ) or $file == "index.html" ) continue;
  83. if ( ! is_array( $pattern ) )
  84. {
  85. if ( preg_match( $pattern, $file ) ) $return[] = $file;
  86. }
  87. else
  88. {
  89. foreach ( $pattern as $p )
  90. {
  91. if ( preg_match( $p, $file ) )
  92. {
  93. $return[] = $file;
  94. break;
  95. }
  96. }
  97. }
  98. }
  99. closedir( $dh );
  100. }
  101. }
  102. if ( ! empty( $return ) and $sorting_order ) rsort( $return );
  103. return $return;
  104. }
  105. /**
  106. * nv_get_mime_type()
  107. *
  108. * @param mixed $filename
  109. * @return
  110. */
  111. function nv_get_mime_type ( $filename, $magic_path = '' )
  112. {
  113. global $sys_info;
  114. if ( empty( $filename ) ) return false;
  115. $ext = strtolower( array_pop( explode( '.', $filename ) ) );
  116. if ( empty( $ext ) ) return false;
  117. $mime = 'application/octet-stream';
  118. if ( nv_function_exists( "finfo_open" ) )
  119. {
  120. if ( empty( $magic_path ) )
  121. {
  122. $finfo = finfo_open( FILEINFO_MIME );
  123. }
  124. elseif ( $magic_path != "auto" )
  125. {
  126. $finfo = finfo_open( FILEINFO_MIME, $magic_path );
  127. }
  128. else
  129. {
  130. if ( ( $magic = getenv( 'MAGIC' ) ) !== false )
  131. {
  132. $finfo = finfo_open( FILEINFO_MIME, $magic );
  133. }
  134. else
  135. {
  136. if ( substr( $sys_info['os'], 0, 3 ) == 'WIN' )
  137. {
  138. $path = realpath( ini_get( 'extension_dir' ) . '/../' ) . 'extras/magic';
  139. $finfo = finfo_open( FILEINFO_MIME, $path );
  140. }
  141. else
  142. {
  143. $finfo = finfo_open( FILEINFO_MIME, '/usr/share/file/magic' );
  144. }
  145. }
  146. }
  147. if ( is_resource( $finfo ) )
  148. {
  149. $mime = finfo_file( $finfo, realpath( $filename ) );
  150. finfo_close( $finfo );
  151. $mime = preg_replace( "/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', trim( $mime ) );
  152. }
  153. }
  154. if ( empty( $mime ) or $mime == "application/octet-stream" )
  155. {
  156. if ( nv_class_exists( "finfo" ) )
  157. {
  158. $finfo = new finfo( FILEINFO_MIME );
  159. if ( $finfo )
  160. {
  161. $mime = $finfo->file( realpath( $filename ) );
  162. $mime = preg_replace( "/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', trim( $mime ) );
  163. }
  164. }
  165. }
  166. if ( empty( $mime ) or $mime == "application/octet-stream" )
  167. {
  168. if ( substr( $sys_info['os'], 0, 3 ) != 'WIN' )
  169. {
  170. if ( nv_function_exists( 'system' ) )
  171. {
  172. ob_start();
  173. system( "file -i -b " . escapeshellarg( $filename ) );
  174. $m = ob_get_clean();
  175. $m = trim( $m );
  176. if ( ! empty( $m ) )
  177. {
  178. $mime = preg_replace( "/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $m );
  179. }
  180. }
  181. elseif ( nv_function_exists( 'exec' ) )
  182. {
  183. $m = @exec( "file -bi " . escapeshellarg( $filename ) );
  184. $m = trim( $m );
  185. if ( ! empty( $m ) )
  186. {
  187. $mime = preg_replace( "/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $m );
  188. }
  189. }
  190. }
  191. }
  192. if ( empty( $mime ) or $mime == "application/octet-stream" )
  193. {
  194. if ( nv_function_exists( 'mime_content_type' ) )
  195. {
  196. $mime = mime_content_type( $filename );
  197. $mime = preg_replace( "/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', trim( $mime ) );
  198. }
  199. }
  200. if ( empty( $mime ) or $mime == "application/octet-stream" )
  201. {
  202. $img_exts = array( 'png', 'gif', 'jpg', 'bmp', 'tiff', 'swf', 'psd' );
  203. if ( in_array( $ext, $img_exts ) )
  204. {
  205. if ( ( $img_info = @getimagesize( $filename ) ) !== false )
  206. {
  207. if ( isset( $img_info['mime'] ) and ! empty( $img_info['mime'] ) )
  208. {
  209. $mime = trim( $img_info['mime'] );
  210. $mime = preg_replace( "/^([\.-\w]+)\/([\.-\w]+)(.*)$/i", '$1/$2', $mime );
  211. }
  212. if ( empty( $mime ) and isset( $img_info[2] ) )
  213. {
  214. $mime = image_type_to_mime_type( $img_info[2] );
  215. }
  216. }
  217. }
  218. }
  219. if ( empty( $mime ) or $mime == "application/octet-stream" )
  220. {
  221. $mime_types = nv_parse_ini_file( NV_ROOTDIR . '/includes/ini/mime.ini' );
  222. if ( array_key_exists( $ext, $mime_types ) )
  223. {
  224. if ( is_string( $mime_types[$ext] ) ) return $mime_types[$ext];
  225. return $mime_types[$ext][0];
  226. }
  227. }
  228. if ( preg_match( "/^application\/(?:x-)?zip(?:-compressed)?$/is", $mime ) )
  229. {
  230. if ( $this->file_extension == "docx" ) $mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
  231. elseif ( $this->file_extension == "dotx" ) $mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.template";
  232. elseif ( $this->file_extension == "potx" ) $mime = "application/vnd.openxmlformats-officedocument.presentationml.template";
  233. elseif ( $this->file_extension == "ppsx" ) $mime = "application/vnd.openxmlformats-officedocument.presentationml.slideshow";
  234. elseif ( $this->file_extension == "pptx" ) $mime = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
  235. elseif ( $this->file_extension == "xlsx" ) $mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
  236. elseif ( $this->file_extension == "xltx" ) $mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.template";
  237. elseif ( $this->file_extension == "docm" ) $mime = "application/vnd.ms-word.document.macroEnabled.12";
  238. elseif ( $this->file_extension == "dotm" ) $mime = "application/vnd.ms-word.template.macroEnabled.12";
  239. elseif ( $this->file_extension == "potm" ) $mime = "application/vnd.ms-powerpoint.template.macroEnabled.12";
  240. elseif ( $this->file_extension == "ppam" ) $mime = "application/vnd.ms-powerpoint.addin.macroEnabled.12";
  241. elseif ( $this->file_extension == "ppsm" ) $mime = "application/vnd.ms-powerpoint.slideshow.macroEnabled.12";
  242. elseif ( $this->file_extension == "pptm" ) $mime = "application/vnd.ms-powerpoint.presentation.macroEnabled.12";
  243. elseif ( $this->file_extension == "xlam" ) $mime = "application/vnd.ms-excel.addin.macroEnabled.12";
  244. elseif ( $this->file_extension == "xlsb" ) $mime = "application/vnd.ms-excel.sheet.binary.macroEnabled.12";
  245. elseif ( $this->file_extension == "xlsm" ) $mime = "application/vnd.ms-excel.sheet.macroEnabled.12";
  246. elseif ( $this->file_extension == "xltm" ) $mime = "application/vnd.ms-excel.template.macroEnabled.12";
  247. }
  248. return $mime;
  249. }
  250. /**
  251. * nv_getextension()
  252. *
  253. * @param mixed $filename
  254. * @return
  255. */
  256. function nv_getextension ( $filename )
  257. {
  258. if ( strpos( $filename, '.' ) === false ) return '';
  259. $filename = basename( strtolower( $filename ) );
  260. $filename = explode( '.', $filename );
  261. return array_pop( $filename );
  262. }
  263. /**
  264. * nv_get_allowed_ext()
  265. *
  266. * @param mixed $allowed_filetypes
  267. * @param mixed $forbid_extensions
  268. * @param mixed $forbid_mimes
  269. * @return
  270. */
  271. function nv_get_allowed_ext ( $allowed_filetypes, $forbid_extensions, $forbid_mimes )
  272. {
  273. if ( $allowed_filetypes == "any" or ( ! empty( $allowed_filetypes ) and is_array( $allowed_filetypes ) and in_array( "any", $allowed_filetypes ) ) ) return "*";
  274. $ini = nv_parse_ini_file( NV_ROOTDIR . '/includes/ini/mime.ini', true );
  275. $allowmimes = array();
  276. if ( ! is_array( $allowed_filetypes ) ) $allowed_filetypes = array( $allowed_filetypes );
  277. if ( ! empty( $allowed_filetypes ) )
  278. {
  279. foreach ( $allowed_filetypes as $type )
  280. {
  281. if ( isset( $ini[$type] ) )
  282. {
  283. foreach ( $ini[$type] as $ext => $mimes )
  284. {
  285. if ( ! empty( $ext ) and ! in_array( $ext, $forbid_extensions ) )
  286. {
  287. $a = true;
  288. if ( ! is_array( $mimes ) )
  289. {
  290. if ( in_array( $mimes, $forbid_mimes ) ) $a = false;
  291. }
  292. else
  293. {
  294. foreach ( $mimes as $m )
  295. {
  296. if ( in_array( $m, $forbid_mimes ) )
  297. {
  298. $a = false;
  299. break;
  300. }
  301. }
  302. }
  303. if ( $a ) $allowmimes[$ext] = $mimes;
  304. }
  305. }
  306. }
  307. }
  308. }
  309. return $allowmimes;
  310. }
  311. /**
  312. * nv_string_to_filename()
  313. *
  314. * @param mixed $word
  315. * @return
  316. */
  317. function nv_string_to_filename ( $word )
  318. {
  319. $word = nv_EncString( $word );
  320. $word = preg_replace( '/[^a-z0-9\.\-\_ ]/i', '', $word );
  321. $word = preg_replace( '/^\W+|\W+$/', '', $word );
  322. $word = preg_replace( '/\s+/', '-', $word );
  323. return strtolower( preg_replace( '/\W-/', '', $word ) );
  324. }
  325. /**
  326. * nv_pathinfo_filename()
  327. *
  328. * @param mixed $file
  329. * @return
  330. */
  331. function nv_pathinfo_filename ( $file )
  332. {
  333. if ( defined( 'PATHINFO_FILENAME' ) ) return pathinfo( $file, PATHINFO_FILENAME );
  334. if ( strstr( $file, '.' ) ) return substr( $file, 0, strrpos( $file, '.' ) );
  335. }
  336. /**
  337. * nv_mkdir()
  338. *
  339. * @param mixed $path
  340. * @param mixed $dir_name
  341. * @return
  342. */
  343. function nv_mkdir ( $path, $dir_name )
  344. {
  345. global $lang_global, $global_config, $sys_info;
  346. $dir_name = nv_string_to_filename( trim( basename( $dir_name ) ) );
  347. if ( ! preg_match( "/^[a-zA-Z0-9-_.]+$/", $dir_name ) ) return array( 0, sprintf( $lang_global['error_create_directories_name_invalid'], $dir_name ) );
  348. $path = @realpath( $path );
  349. if ( ! preg_match( '/\/$/', $path ) ) $path = $path . "/";
  350. if ( file_exists( $path . $dir_name ) ) return array( 2, sprintf( $lang_global['error_create_directories_name_used'], $dir_name ), $path . $dir_name );
  351. if ( ! is_dir( $path ) ) return array( 0, sprintf( $lang_global['error_directory_does_not_exist'], $path ) );
  352. $ftp_check_login = 0;
  353. if ( $sys_info['ftp_support'] and intval( $global_config['ftp_check_login'] ) == 1 )
  354. {
  355. $ftp_server = nv_unhtmlspecialchars( $global_config['ftp_server'] );
  356. $ftp_port = intval( $global_config['ftp_port'] );
  357. $ftp_user_name = nv_unhtmlspecialchars( $global_config['ftp_user_name'] );
  358. $ftp_user_pass = nv_unhtmlspecialchars( $global_config['ftp_user_pass'] );
  359. $ftp_path = nv_unhtmlspecialchars( $global_config['ftp_path'] );
  360. // set up basic connection
  361. $conn_id = ftp_connect( $ftp_server, $ftp_port );
  362. // login with username and password
  363. $login_result = ftp_login( $conn_id, $ftp_user_name, $ftp_user_pass );
  364. if ( ( ! $conn_id ) || ( ! $login_result ) )
  365. {
  366. $ftp_check_login = 3;
  367. }
  368. elseif ( ftp_chdir( $conn_id, $ftp_path ) )
  369. {
  370. $ftp_check_login = 1;
  371. }
  372. else
  373. {
  374. $ftp_check_login = 2;
  375. }
  376. }
  377. if ( $ftp_check_login == 1 )
  378. {
  379. $dir = str_replace( NV_ROOTDIR . "/", "", str_replace( '\\', '/', $path . $dir_name ) );
  380. $res = ftp_mkdir( $conn_id, $dir );
  381. if ( substr( $sys_info['os'], 0, 3 ) != 'WIN' ) ftp_chmod( $conn_id, 0777, $dir );
  382. ftp_close( $conn_id );
  383. }
  384. if ( ! is_dir( $path . $dir_name ) )
  385. {
  386. if ( ! is_writable( $path ) )
  387. {
  388. @chmod( $path, 0777 );
  389. }
  390. if ( ! is_writable( $path ) ) return array( 0, sprintf( $lang_global['error_directory_can_not_write'], $path ) );
  391. $oldumask = umask( 0 );
  392. $res = @mkdir( $path . $dir_name );
  393. umask( $oldumask );
  394. }
  395. if ( ! $res ) return array( 0, sprintf( $lang_global['error_create_directories_failed'], $dir_name ) );
  396. file_put_contents( $path . $dir_name . '/index.html', '' );
  397. return array( 1, sprintf( $lang_global['directory_was_created'], $dir_name ), $path . $dir_name );
  398. }
  399. /**
  400. * nv_deletefile()
  401. *
  402. * @param mixed $file
  403. * @param bool $delsub
  404. * @return
  405. */
  406. function nv_deletefile ( $file, $delsub = false )
  407. {
  408. global $lang_global, $sys_info, $global_config;
  409. $realpath = realpath( $file );
  410. if ( empty( $realpath ) ) return array( 0, sprintf( $lang_global['error_non_existent_file'], $file ) );
  411. $realpath = str_replace( '\\', '/', $realpath );
  412. $realpath = rtrim( $realpath, "\\/" );
  413. $preg_match = preg_match( "/^(" . nv_preg_quote( NV_ROOTDIR ) . ")(\/[\S]+)/", $realpath, $path );
  414. if ( empty( $preg_match ) ) return array( 0, sprintf( $lang_global['error_delete_forbidden'], $file ) );
  415. $ftp_check_login = 0;
  416. if ( $sys_info['ftp_support'] and intval( $global_config['ftp_check_login'] ) == 1 )
  417. {
  418. $ftp_server = nv_unhtmlspecialchars( $global_config['ftp_server'] );
  419. $ftp_port = intval( $global_config['ftp_port'] );
  420. $ftp_user_name = nv_unhtmlspecialchars( $global_config['ftp_user_name'] );
  421. $ftp_user_pass = nv_unhtmlspecialchars( $global_config['ftp_user_pass'] );
  422. $ftp_path = nv_unhtmlspecialchars( $global_config['ftp_path'] );
  423. // set up basic connection
  424. $conn_id = ftp_connect( $ftp_server, $ftp_port );
  425. // login with username and password
  426. $login_result = ftp_login( $conn_id, $ftp_user_name, $ftp_user_pass );
  427. if ( ( ! $conn_id ) || ( ! $login_result ) )
  428. {
  429. $ftp_check_login = 3;
  430. }
  431. elseif ( ftp_chdir( $conn_id, $ftp_path ) )
  432. {
  433. $ftp_check_login = 1;
  434. }
  435. else
  436. {
  437. $ftp_check_login = 2;
  438. }
  439. }
  440. $filename = str_replace( NV_ROOTDIR . "/", "", str_replace( '\\', '/', $realpath ) );
  441. if ( $ftp_check_login == 1 )
  442. {
  443. if ( is_dir( $realpath ) )
  444. {
  445. nv_ftp_del_dir( $conn_id, $filename );
  446. }
  447. elseif ( ! ftp_delete( $conn_id, $filename ) )
  448. {
  449. @unlink( $realpath );
  450. }
  451. ftp_close( $conn_id );
  452. }
  453. elseif ( is_dir( $realpath ) )
  454. {
  455. $files = scandir( $realpath );
  456. $files2 = array_diff( $files, array( ".", "..", ".htaccess", "index.html" ) );
  457. if ( count( $files2 ) and ! $delsub )
  458. {
  459. return array( 0, sprintf( $lang_global['error_delete_subdirectories_not_empty'], $path[2] ) );
  460. }
  461. else
  462. {
  463. $files = array_diff( $files, array( ".", ".." ) );
  464. if ( count( $files ) )
  465. {
  466. foreach ( $files as $f )
  467. {
  468. $unlink = nv_deletefile( $realpath . '/' . $f, true );
  469. if ( empty( $unlink[0] ) )
  470. {
  471. $filename = str_replace( NV_ROOTDIR, "", str_replace( '\\', '/', $realpath . '/' . $f ) );
  472. return array( 0, sprintf( $lang_global['error_delete_failed'], $filename ) );
  473. }
  474. }
  475. }
  476. if ( ! @rmdir( $realpath ) ) return array( 0, sprintf( $lang_global['error_delete_subdirectories_failed'], $path[2] ) );
  477. else return array( 1, sprintf( $lang_global['directory_deleted'], $path[2] ) );
  478. }
  479. }
  480. else
  481. {
  482. @unlink( $realpath );
  483. }
  484. if ( file_exists( $realpath ) )
  485. {
  486. return array( 0, sprintf( $lang_global['error_delete_failed'], $filename ) );
  487. }
  488. else
  489. {
  490. return array( 1, sprintf( $lang_global['file_deleted'], $filename ) );
  491. }
  492. }
  493. /**
  494. * nv_ftp_del_dir()
  495. *
  496. * @param mixed $conn_id
  497. * @param mixed $dst_dir
  498. * @return
  499. */
  500. function nv_ftp_del_dir ( $conn_id, $dst_dir )
  501. {
  502. $dst_dir = preg_replace( "/\\/\$/", "", $dst_dir ); // remove trailing slash
  503. $ar_files = ftp_nlist( $conn_id, $dst_dir );
  504. if ( is_array( $ar_files ) )
  505. { // makes sure there are files
  506. for ( $i = 0; $i < sizeof( $ar_files ); $i ++ )
  507. { // for each file
  508. $st_file = basename( $ar_files[$i] );
  509. if ( $st_file == '.' || $st_file == '..' ) continue;
  510. if ( ftp_size( $conn_id, $dst_dir . '/' . $st_file ) == - 1 )
  511. { // check if it is a directory
  512. nv_ftp_del_dir( $conn_id, $dst_dir . '/' . $st_file );
  513. }
  514. else
  515. {
  516. ftp_delete( $conn_id, $dst_dir . '/' . $st_file );
  517. }
  518. }
  519. }
  520. return ftp_rmdir( $conn_id, $dst_dir ); // delete empty directories
  521. }
  522. /**
  523. * nv_copyfile()
  524. *
  525. * @param mixed $file
  526. * @param mixed $newfile
  527. * @return
  528. */
  529. function nv_copyfile ( $file, $newfile )
  530. {
  531. if ( ! copy( $file, $newfile ) )
  532. {
  533. $content = @file_get_contents( $file );
  534. $openedfile = fopen( $newfile, "w" );
  535. fwrite( $openedfile, $content );
  536. fclose( $openedfile );
  537. if ( $content === false ) return false;
  538. }
  539. if ( file_exists( $newfile ) )
  540. {
  541. return true;
  542. }
  543. return false;
  544. }
  545. /**
  546. * nv_renamefile()
  547. *
  548. * @param mixed $file
  549. * @param mixed $newname
  550. * @return
  551. */
  552. function nv_renamefile ( $file, $newname )
  553. {
  554. global $lang_global;
  555. $realpath = realpath( $file );
  556. if ( empty( $realpath ) ) return array( 0, sprintf( $lang_global['error_non_existent_file'], $file ) );
  557. $realpath = str_replace( '\\', '/', $realpath );
  558. $realpath = rtrim( $realpath, "\\/" );
  559. $preg_match = preg_match( "/^(" . nv_preg_quote( NV_ROOTDIR ) . ")(\/[\S]+)/", $realpath, $path );
  560. if ( empty( $preg_match ) ) return array( 0, sprintf( $lang_global['error_rename_forbidden'], $file ) );
  561. $newname = basename( trim( $newname ) );
  562. $pathinfo = pathinfo( $realpath );
  563. if ( file_exists( $pathinfo['dirname'] . '/' . $newname ) ) return array( 0, sprintf( $lang_global['error_rename_file_exists'], $newname ) );
  564. if ( is_dir( $realpath ) and ! preg_match( '/^[a-zA-Z0-9-_]+$/', $newname ) ) return array( 0, sprintf( $lang_global['error_rename_directories_invalid'], $newname ) );
  565. if ( ! is_dir( $realpath ) and ! preg_match( '/^[a-zA-Z0-9-_.]+$/', $newname ) ) return array( 0, sprintf( $lang_global['error_rename_file_invalid'], $newname ) );
  566. if ( ! is_dir( $realpath ) and $pathinfo['extension'] != nv_getextension( $newname ) ) return array( 0, sprintf( $lang_global['error_rename_extension_changed'], $newname, $pathinfo['basename'] ) );
  567. if ( ! @rename( $realpath, $pathinfo['dirname'] . '/' . $newname ) )
  568. {
  569. if ( ! @nv_copyfile( $realpath, $pathinfo['dirname'] . '/' . $newname ) )
  570. {
  571. return array( 0, sprintf( $lang_global['error_rename_failed'], $pathinfo['basename'], $newname ) );
  572. }
  573. else
  574. {
  575. @nv_deletefile( $realpath );
  576. }
  577. }
  578. return array( 1, sprintf( $lang_global['file_has_been_renamed'], $pathinfo['basename'], $newname ) );
  579. }
  580. /**
  581. * nv_chmod_dir()
  582. *
  583. * @param mixed $conn_id
  584. * @param mixed $dir
  585. * @param bool $subdir
  586. * @return
  587. */
  588. function nv_chmod_dir ( $conn_id, $dir, $subdir = false )
  589. {
  590. global $sys_info, $array_cmd_dir;
  591. $no_file = array( '.', '..', '.htaccess', 'index.html' );
  592. if ( substr( $sys_info['os'], 0, 3 ) != 'WIN' and ftp_chmod( $conn_id, 0777, $dir ) !== false )
  593. {
  594. $array_cmd_dir[] = $dir;
  595. if ( $subdir and is_dir( NV_ROOTDIR . '/' . $dir ) )
  596. {
  597. $list_files = ftp_nlist( $conn_id, $dir );
  598. foreach ( $list_files as $file_i )
  599. {
  600. $file_i = basename( $file_i );
  601. if ( ! in_array( $file_i, $no_file ) )
  602. {
  603. if ( is_dir( NV_ROOTDIR . '/' . $dir . '/' . $file_i ) )
  604. {
  605. nv_chmod_dir( $conn_id, $dir . '/' . $file_i, $subdir );
  606. }
  607. else
  608. {
  609. ftp_chmod( $conn_id, 0777, $dir . '/' . $file_i );
  610. }
  611. }
  612. }
  613. }
  614. }
  615. else
  616. {
  617. $array_cmd_dir[] = '<b>' . $dir . ' --> no chmod 777 </b>';
  618. }
  619. }
  620. /**
  621. * nv_gz_get_contents()
  622. *
  623. * @param mixed $filename
  624. * @return
  625. */
  626. function nv_gz_get_contents ( $filename )
  627. {
  628. global $sys_info;
  629. $content = file_get_contents( $filename );
  630. if ( isset( $sys_info['str_compress'] ) and ! empty( $sys_info['str_compress'] ) )
  631. {
  632. $content = call_user_func( $sys_info['str_compress'][1], $content );
  633. }
  634. return $content;
  635. }
  636. /**
  637. * nv_gz_put_contents()
  638. *
  639. * @param mixed $filename
  640. * @param mixed $content
  641. * @return
  642. */
  643. function nv_gz_put_contents ( $filename, $content )
  644. {
  645. global $sys_info;
  646. if ( isset( $sys_info['str_compress'] ) and ! empty( $sys_info['str_compress'] ) )
  647. {
  648. $content = call_user_func( $sys_info['str_compress'][0], $content, 9 );
  649. }
  650. return file_put_contents( $filename, $content, LOCK_EX );
  651. }
  652. /**
  653. * nv_is_image()
  654. *
  655. * @param mixed $img
  656. * @return
  657. */
  658. function nv_is_image ( $img )
  659. {
  660. $typeflag = array();
  661. $typeflag[1] = array( 'type' => IMAGETYPE_GIF, 'ext' => 'gif' );
  662. $typeflag[2] = array( 'type' => IMAGETYPE_JPEG, 'ext' => 'jpg' );
  663. $typeflag[3] = array( 'type' => IMAGETYPE_PNG, 'ext' => 'png' );
  664. $typeflag[4] = array( 'type' => IMAGETYPE_SWF, 'ext' => 'swf' );
  665. $typeflag[5] = array( 'type' => IMAGETYPE_PSD, 'ext' => 'psd' );
  666. $typeflag[6] = array( 'type' => IMAGETYPE_BMP, 'ext' => 'bmp' );
  667. $typeflag[7] = array( 'type' => IMAGETYPE_TIFF_II, 'ext' => 'tiff' );
  668. $typeflag[8] = array( 'type' => IMAGETYPE_TIFF_MM, 'ext' => 'tiff' );
  669. $typeflag[9] = array( 'type' => IMAGETYPE_JPC, 'ext' => 'jpc' );
  670. $typeflag[10] = array( 'type' => IMAGETYPE_JP2, 'ext' => 'jp2' );
  671. $typeflag[11] = array( 'type' => IMAGETYPE_JPX, 'ext' => 'jpf' );
  672. $typeflag[12] = array( 'type' => IMAGETYPE_JB2, 'ext' => 'jb2' );
  673. $typeflag[13] = array( 'type' => IMAGETYPE_SWC, 'ext' => 'swc' );
  674. $typeflag[14] = array( 'type' => IMAGETYPE_IFF, 'ext' => 'aiff' );
  675. $typeflag[15] = array( 'type' => IMAGETYPE_WBMP, 'ext' => 'wbmp' );
  676. $typeflag[16] = array( 'type' => IMAGETYPE_XBM, 'ext' => 'xbm' );
  677. $imageinfo = array();
  678. $file = @getimagesize( $img );
  679. if ( $file )
  680. {
  681. $imageinfo['src'] = $img;
  682. $imageinfo['width'] = $file[0];
  683. $imageinfo['height'] = $file[1];
  684. $imageinfo['mime'] = $file['mime'];
  685. $imageinfo['type'] = $typeflag[$file[2]]['type'];
  686. $imageinfo['ext'] = $typeflag[$file[2]]['ext'];
  687. $imageinfo['bits'] = $file['bits'];
  688. $imageinfo['channels'] = isset( $file['channels'] ) ? intval( $file['channels'] ) : 0;
  689. }
  690. return $imageinfo;
  691. }
  692. /**
  693. * nv_ImageInfo()
  694. * Function xuat ra cac thong tin ve IMAGE de dua vao HTML (src, width, height).
  695. *
  696. * @param mixed $original_name - duong dan tuyet doi den file goc (bat buoc)
  697. * @param integer $width - chieu rong xuat ra HTML (neu bang 0 se xuat ra kich thuoc thuc)
  698. * @param bool $is_create_thumb - Neu chieu rong cua hinh lon hon $width, co the tao thumbnail
  699. * @param string $thumb_path - neu tao thumbnail thi chi ra thu muc chua file thumbnail nay.
  700. * @return array('src','width','height')
  701. */
  702. function nv_ImageInfo ( $original_name, $width = 0, $is_create_thumb = false, $thumb_path = '' )
  703. {
  704. if ( empty( $original_name ) ) return false;
  705. $original_name = realpath( $original_name );
  706. if ( empty( $original_name ) ) return false;
  707. $original_name = str_replace( '\\', '/', $original_name );
  708. $original_name = rtrim( $original_name, "\\/" );
  709. unset( $matches );
  710. if ( ! preg_match( "/^" . nv_preg_quote( NV_ROOTDIR ) . "\/(([a-z0-9\-\_\/]+\/)*([a-z0-9\-\_\.]+)(\.(gif|jpg|jpeg|png)))$/i", $original_name, $matches ) ) return false;
  711. $imageinfo = array();
  712. $size = @getimagesize( $original_name );
  713. if ( ! $size or ! isset( $size[0] ) or ! isset( $size[1] ) or ! $size[0] or ! $size[1] ) return false;
  714. $imageinfo['orig_src'] = $imageinfo['src'] = NV_BASE_SITEURL . $matches[1];
  715. $imageinfo['orig_width'] = $imageinfo['width'] = $size[0];
  716. $imageinfo['orig_height'] = $imageinfo['height'] = $size[1];
  717. if ( $width )
  718. {
  719. $imageinfo['width'] = $width;
  720. $imageinfo['height'] = ceil( $width * $imageinfo['orig_height'] / $imageinfo['orig_width'] );
  721. }
  722. if ( $is_create_thumb and $width and $imageinfo['orig_width'] > $width )
  723. {
  724. if ( empty( $thumb_path ) or ! is_dir( $thumb_path ) or ! is_writeable( $thumb_path ) )
  725. {
  726. $thumb_path = $matches[2];
  727. }
  728. else
  729. {
  730. $thumb_path = realpath( $thumb_path );
  731. if ( empty( $thumb_path ) )
  732. {
  733. $thumb_path = $matches[2];
  734. }
  735. else
  736. {
  737. $thumb_path = str_replace( '\\', '/', $thumb_path );
  738. unset( $matches2 );
  739. if ( preg_match( "/^" . nv_preg_quote( NV_ROOTDIR ) . "([a-z0-9\-\_\/]+)*$/i", $thumb_path, $matches2 ) )
  740. {
  741. $thumb_path = ltrim( $matches2[1], "\\/" );
  742. }
  743. else
  744. {
  745. $thumb_path = $matches[2];
  746. }
  747. }
  748. }
  749. if ( ! empty( $thumb_path ) and ! preg_match( "/\/$/", $thumb_path ) ) $thumb_path = $thumb_path . '/';
  750. $new_src = $thumb_path . $matches[3] . '_' . md5( $original_name . $width ) . $matches[4];
  751. $is_create = true;
  752. if ( file_exists( NV_ROOTDIR . '/' . $new_src ) )
  753. {
  754. $size = @getimagesize( NV_ROOTDIR . '/' . $new_src );
  755. if ( $size and isset( $size[0] ) and isset( $size[1] ) and $size[0] and $size[1] )
  756. {
  757. $imageinfo['src'] = NV_BASE_SITEURL . $new_src;
  758. $imageinfo['width'] = $size[0];
  759. $imageinfo['height'] = $size[1];
  760. $is_create = false;
  761. }
  762. }
  763. if ( $is_create )
  764. {
  765. include ( NV_ROOTDIR . "/includes/class/image.class.php" );
  766. $image = new image( $original_name, NV_MAX_WIDTH, NV_MAX_HEIGHT );
  767. $image->resizeXY( $width );
  768. $image->save( NV_ROOTDIR . '/' . $thumb_path, $matches[3] . '_' . md5( $original_name . $width ) . $matches[4] );
  769. $image_info = $image->create_Image_info;
  770. if ( file_exists( NV_ROOTDIR . '/' . $new_src ) )
  771. {
  772. $imageinfo['src'] = NV_BASE_SITEURL . $new_src;
  773. $imageinfo['width'] = $image_info['width'];
  774. $imageinfo['height'] = $image_info['height'];
  775. }
  776. }
  777. }
  778. return $imageinfo;
  779. }
  780. ?>