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

/01.Source/01.CORE/includes/core/filesystem_functions.php

http://creative-portal.googlecode.com/
PHP | 691 lines | 634 code | 21 blank | 36 comment | 29 complexity | 35e822b826f9e32f3ce9d7cc5c01d68f 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. $files = @scandir( $directory, $sorting_order );
  79. if ( ! empty( $files ) )
  80. {
  81. foreach ( $files as $file )
  82. {
  83. if ( $file != "." and $file != ".." and $file != ".htaccess" and $file != "index.html" )
  84. {
  85. if ( ! is_array( $pattern ) )
  86. {
  87. if ( preg_match( $pattern, $file ) ) $return[] = $file;
  88. }
  89. else
  90. {
  91. foreach ( $pattern as $p )
  92. {
  93. if ( preg_match( $p, $file ) )
  94. {
  95. $return[] = $file;
  96. break;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. return $return;
  105. }
  106. if ( ! function_exists( 'mime_content_type' ) )
  107. {
  108. /**
  109. * mime_content_type()
  110. *
  111. * @param mixed $filename
  112. * @return
  113. */
  114. function mime_content_type( $filename )
  115. {
  116. if ( empty( $filename ) ) return false;
  117. $ext = strtolower( array_pop( explode( '.', $filename ) ) );
  118. if ( empty( $ext ) ) return false;
  119. if ( function_exists( 'finfo_open' ) )
  120. {
  121. $finfo = finfo_open( FILEINFO_MIME );
  122. $mimetype = finfo_file( $finfo, $filename );
  123. finfo_close( $finfo );
  124. return $mimetype;
  125. }
  126. else
  127. {
  128. $mime_types = nv_parse_ini_file( NV_ROOTDIR . '/includes/ini/mime.ini' );
  129. if ( array_key_exists( $ext, $mime_types ) )
  130. {
  131. if ( is_string( $mime_types[$ext] ) ) return $mime_types[$ext];
  132. else return $mime_types[$ext][0];
  133. }
  134. else
  135. {
  136. return 'application/octet-stream';
  137. }
  138. }
  139. }
  140. }
  141. /**
  142. * nv_getextension()
  143. *
  144. * @param mixed $filename
  145. * @return
  146. */
  147. function nv_getextension( $filename )
  148. {
  149. if ( strpos( $filename, '.' ) === false ) return '';
  150. $filename = basename( strtolower( $filename ) );
  151. $filename = explode( '.', $filename );
  152. return array_pop( $filename );
  153. }
  154. /**
  155. * nv_get_allowed_ext()
  156. *
  157. * @param mixed $allowed_filetypes
  158. * @param mixed $forbid_extensions
  159. * @param mixed $forbid_mimes
  160. * @return
  161. */
  162. function nv_get_allowed_ext( $allowed_filetypes, $forbid_extensions, $forbid_mimes )
  163. {
  164. if ( $allowed_filetypes == "any" or ( ! empty( $allowed_filetypes ) and is_array( $allowed_filetypes ) and in_array( "any", $allowed_filetypes ) ) ) return "*";
  165. $ini = nv_parse_ini_file( NV_ROOTDIR . '/includes/ini/mime.ini', true );
  166. $allowmimes = array();
  167. if ( ! is_array( $allowed_filetypes ) ) $allowed_filetypes = array( $allowed_filetypes );
  168. if ( ! empty( $allowed_filetypes ) )
  169. {
  170. foreach ( $allowed_filetypes as $type )
  171. {
  172. if ( isset( $ini[$type] ) )
  173. {
  174. foreach ( $ini[$type] as $ext => $mimes )
  175. {
  176. if ( ! empty( $ext ) and ! in_array( $ext, $forbid_extensions ) )
  177. {
  178. $a = true;
  179. if ( ! is_array( $mimes ) )
  180. {
  181. if ( in_array( $mimes, $forbid_mimes ) ) $a = false;
  182. }
  183. else
  184. {
  185. foreach ( $mimes as $m )
  186. {
  187. if ( in_array( $m, $forbid_mimes ) )
  188. {
  189. $a = false;
  190. break;
  191. }
  192. }
  193. }
  194. if ( $a ) $allowmimes[$ext] = $mimes;
  195. }
  196. }
  197. }
  198. }
  199. }
  200. return $allowmimes;
  201. }
  202. /**
  203. * nv_string_to_filename()
  204. *
  205. * @param mixed $word
  206. * @return
  207. */
  208. function nv_string_to_filename( $word )
  209. {
  210. $word = nv_EncString( $word );
  211. $word = preg_replace( '/[^a-z0-9\.\-\_ ]/i', '', $word );
  212. $word = preg_replace( '/\s+/', '_', $word );
  213. return preg_replace( '/\W-/', '', $word );
  214. }
  215. /**
  216. * nv_pathinfo_filename()
  217. *
  218. * @param mixed $file
  219. * @return
  220. */
  221. function nv_pathinfo_filename( $file )
  222. {
  223. if ( defined( 'PATHINFO_FILENAME' ) ) return pathinfo( $file, PATHINFO_FILENAME );
  224. if ( strstr( $file, '.' ) ) return substr( $file, 0, strrpos( $file, '.' ) );
  225. }
  226. /**
  227. * nv_mkdir()
  228. *
  229. * @param mixed $path
  230. * @param mixed $dir_name
  231. * @return
  232. */
  233. function nv_mkdir( $path, $dir_name )
  234. {
  235. global $lang_global, $global_config, $sys_info;
  236. $dir_name = nv_string_to_filename( trim( basename( $dir_name ) ) );
  237. if ( ! preg_match( "/^[a-zA-Z0-9-_.]+$/", $dir_name ) ) return array( 0, sprintf( $lang_global['error_create_directories_name_invalid'], $dir_name ) );
  238. $path = @realpath( $path );
  239. if ( ! preg_match( '/\/$/', $path ) ) $path = $path . "/";
  240. if ( file_exists( $path . $dir_name ) ) return array( 2, sprintf( $lang_global['error_create_directories_name_used'], $dir_name ), $path . $dir_name );
  241. if ( ! is_dir( $path ) ) return array( 0, sprintf( $lang_global['error_directory_does_not_exist'], $path ) );
  242. $ftp_check_login = 0;
  243. if ( $sys_info['ftp_support'] and intval( $global_config['ftp_check_login'] ) == 1 )
  244. {
  245. $ftp_server = nv_unhtmlspecialchars( $global_config['ftp_server'] );
  246. $ftp_port = intval( $global_config['ftp_port'] );
  247. $ftp_user_name = nv_unhtmlspecialchars( $global_config['ftp_user_name'] );
  248. $ftp_user_pass = nv_unhtmlspecialchars( $global_config['ftp_user_pass'] );
  249. $ftp_path = nv_unhtmlspecialchars( $global_config['ftp_path'] );
  250. // set up basic connection
  251. $conn_id = ftp_connect( $ftp_server, $ftp_port );
  252. // login with username and password
  253. $login_result = ftp_login( $conn_id, $ftp_user_name, $ftp_user_pass );
  254. if ( ( ! $conn_id ) || ( ! $login_result ) )
  255. {
  256. $ftp_check_login = 3;
  257. } elseif ( ftp_chdir( $conn_id, $ftp_path ) )
  258. {
  259. $ftp_check_login = 1;
  260. }
  261. else
  262. {
  263. $ftp_check_login = 2;
  264. }
  265. }
  266. if ( $ftp_check_login == 1 )
  267. {
  268. $dir = str_replace( NV_ROOTDIR . "/", "", str_replace( '\\', '/', $path . $dir_name ) );
  269. $res = ftp_mkdir( $conn_id, $dir );
  270. ftp_chmod( $conn_id, 0777, $dir );
  271. ftp_close( $conn_id );
  272. }
  273. if ( ! is_dir( $path . $dir_name ) )
  274. {
  275. if ( ! is_writable( $path ) )
  276. {
  277. @chmod( $path, 0777 );
  278. }
  279. if ( ! is_writable( $path ) ) return array( 0, sprintf( $lang_global['error_directory_can_not_write'], $path ) );
  280. $oldumask = umask( 0 );
  281. $res = @mkdir( $path . $dir_name );
  282. umask( $oldumask );
  283. }
  284. if ( ! $res ) return array( 0, sprintf( $lang_global['error_create_directories_failed'], $dir_name ) );
  285. file_put_contents( $path . $dir_name . '/index.html', '' );
  286. return array( 1, sprintf( $lang_global['directory_was_created'], $dir_name ), $path . $dir_name );
  287. }
  288. /**
  289. * nv_deletefile()
  290. *
  291. * @param mixed $file
  292. * @param bool $delsub
  293. * @return
  294. */
  295. function nv_deletefile( $file, $delsub = false )
  296. {
  297. global $lang_global, $sys_info, $global_config;
  298. $realpath = realpath( $file );
  299. if ( empty( $realpath ) ) return array( 0, sprintf( $lang_global['error_non_existent_file'], $file ) );
  300. $realpath = str_replace( '\\', '/', $realpath );
  301. $realpath = rtrim( $realpath, "\\/" );
  302. $preg_match = preg_match( "/^(" . nv_preg_quote( NV_ROOTDIR ) . ")(\/[\S]+)/", $realpath, $path );
  303. if ( empty( $preg_match ) ) return array( 0, sprintf( $lang_global['error_delete_forbidden'], $file ) );
  304. if ( is_dir( $realpath ) )
  305. {
  306. $files = scandir( $realpath );
  307. $files2 = array_diff( $files, array( ".", "..", ".htaccess", "index.html" ) );
  308. if ( count( $files2 ) and ! $delsub )
  309. {
  310. return array( 0, sprintf( $lang_global['error_delete_subdirectories_not_empty'], $path[2] ) );
  311. }
  312. else
  313. {
  314. $files = array_diff( $files, array( ".", ".." ) );
  315. if ( count( $files ) )
  316. {
  317. foreach ( $files as $f )
  318. {
  319. $unlink = nv_deletefile( $realpath . '/' . $f, true );
  320. if ( empty( $unlink[0] ) ) return $unlink[1];
  321. }
  322. }
  323. if ( ! @rmdir( $realpath ) ) return array( 0, sprintf( $lang_global['error_delete_subdirectories_failed'], $path[2] ) );
  324. else return array( 1, sprintf( $lang_global['directory_deleted'], $path[2] ) );
  325. }
  326. }
  327. else
  328. {
  329. $filename = str_replace( NV_ROOTDIR . "/", "", str_replace( '\\', '/', $realpath ) );
  330. $ftp_check_login = 0;
  331. if ( $sys_info['ftp_support'] and intval( $global_config['ftp_check_login'] ) == 1 )
  332. {
  333. $ftp_server = nv_unhtmlspecialchars( $global_config['ftp_server'] );
  334. $ftp_port = intval( $global_config['ftp_port'] );
  335. $ftp_user_name = nv_unhtmlspecialchars( $global_config['ftp_user_name'] );
  336. $ftp_user_pass = nv_unhtmlspecialchars( $global_config['ftp_user_pass'] );
  337. $ftp_path = nv_unhtmlspecialchars( $global_config['ftp_path'] );
  338. // set up basic connection
  339. $conn_id = ftp_connect( $ftp_server, $ftp_port );
  340. // login with username and password
  341. $login_result = ftp_login( $conn_id, $ftp_user_name, $ftp_user_pass );
  342. if ( ( ! $conn_id ) || ( ! $login_result ) )
  343. {
  344. $ftp_check_login = 3;
  345. } elseif ( ftp_chdir( $conn_id, $ftp_path ) )
  346. {
  347. $ftp_check_login = 1;
  348. }
  349. else
  350. {
  351. $ftp_check_login = 2;
  352. }
  353. }
  354. if ( $ftp_check_login == 1 )
  355. {
  356. ftp_delete($conn_id, $filename);
  357. ftp_close( $conn_id );
  358. }
  359. else{
  360. @unlink( $realpath );
  361. }
  362. if (file_exists($realpath)){
  363. return array( 0, sprintf( $lang_global['error_delete_failed'], $filename ) );
  364. }
  365. else{
  366. return array( 1, sprintf( $lang_global['file_deleted'], $filename ) );
  367. }
  368. }
  369. }
  370. /**
  371. * nv_copyfile()
  372. *
  373. * @param mixed $file
  374. * @param mixed $newfile
  375. * @return
  376. */
  377. function nv_copyfile( $file, $newfile )
  378. {
  379. if ( ! copy( $file, $newfile ) )
  380. {
  381. $content = @file_get_contents( $file );
  382. $openedfile = fopen( $newfile, "w" );
  383. fwrite( $openedfile, $content );
  384. fclose( $openedfile );
  385. if ( $content === false ) return false;
  386. }
  387. if ( file_exists( $newfile ) )
  388. {
  389. return true;
  390. }
  391. return false;
  392. }
  393. /**
  394. * nv_renamefile()
  395. *
  396. * @param mixed $file
  397. * @param mixed $newname
  398. * @return
  399. */
  400. function nv_renamefile( $file, $newname )
  401. {
  402. global $lang_global;
  403. $realpath = realpath( $file );
  404. if ( empty( $realpath ) ) return array( 0, sprintf( $lang_global['error_non_existent_file'], $file ) );
  405. $realpath = str_replace( '\\', '/', $realpath );
  406. $realpath = rtrim( $realpath, "\\/" );
  407. $preg_match = preg_match( "/^(" . nv_preg_quote( NV_ROOTDIR ) . ")(\/[\S]+)/", $realpath, $path );
  408. if ( empty( $preg_match ) ) return array( 0, sprintf( $lang_global['error_rename_forbidden'], $file ) );
  409. $newname = basename( trim( $newname ) );
  410. $pathinfo = pathinfo( $realpath );
  411. if ( file_exists( $pathinfo['dirname'] . '/' . $newname ) ) return array( 0, sprintf( $lang_global['error_rename_file_exists'], $newname ) );
  412. if ( is_dir( $realpath ) and ! preg_match( '/^[a-zA-Z0-9-_]+$/', $newname ) ) return array( 0, sprintf( $lang_global['error_rename_directories_invalid'], $newname ) );
  413. if ( ! is_dir( $realpath ) and ! preg_match( '/^[a-zA-Z0-9-_.]+$/', $newname ) ) return array( 0, sprintf( $lang_global['error_rename_file_invalid'], $newname ) );
  414. if ( ! is_dir( $realpath ) and $pathinfo['extension'] != nv_getextension( $newname ) ) return array( 0, sprintf( $lang_global['error_rename_extension_changed'], $newname, $pathinfo['basename'] ) );
  415. if ( ! @rename( $realpath, $pathinfo['dirname'] . '/' . $newname ) )
  416. {
  417. if ( ! @nv_copyfile( $realpath, $pathinfo['dirname'] . '/' . $newname ) )
  418. {
  419. return array( 0, sprintf( $lang_global['error_rename_failed'], $pathinfo['basename'], $newname ) );
  420. }
  421. else
  422. {
  423. @nv_deletefile( $realpath );
  424. }
  425. }
  426. return array( 1, sprintf( $lang_global['file_has_been_renamed'], $pathinfo['basename'], $newname ) );
  427. }
  428. /**
  429. * nv_chmod_dir()
  430. *
  431. * @param mixed $conn_id
  432. * @param mixed $dir
  433. * @param bool $subdir
  434. * @return
  435. */
  436. function nv_chmod_dir( $conn_id, $dir, $subdir = false )
  437. {
  438. global $array_cmd_dir;
  439. $no_file = array( '.', '..', '.htaccess', 'index.html' );
  440. if ( ftp_chmod( $conn_id, 0777, $dir ) !== false )
  441. {
  442. $array_cmd_dir[] = $dir;
  443. if ( $subdir and is_dir( NV_ROOTDIR . '/' . $dir ) )
  444. {
  445. $list_files = ftp_nlist( $conn_id, $dir );
  446. foreach ( $list_files as $file_i )
  447. {
  448. if ( ! in_array( $file_i, $no_file ) )
  449. {
  450. if ( is_dir( NV_ROOTDIR . '/' . $dir . '/' . $file_i ) )
  451. {
  452. nv_chmod_dir( $conn_id, $dir . '/' . $file_i, $subdir );
  453. }
  454. else
  455. {
  456. ftp_chmod( $conn_id, 0777, $dir . '/' . $file_i );
  457. }
  458. }
  459. }
  460. }
  461. }
  462. else
  463. {
  464. $array_cmd_dir[] = '<b>' . $dir . ' --> no chmod 777 </b>';
  465. }
  466. }
  467. /**
  468. * nv_gz_get_contents()
  469. *
  470. * @param mixed $filename
  471. * @return
  472. */
  473. function nv_gz_get_contents( $filename )
  474. {
  475. global $sys_info;
  476. $content = file_get_contents( $filename );
  477. if ( isset( $sys_info['str_compress'] ) and ! empty( $sys_info['str_compress'] ) )
  478. {
  479. $content = call_user_func( $sys_info['str_compress'][1], $content );
  480. }
  481. return $content;
  482. }
  483. /**
  484. * nv_gz_put_contents()
  485. *
  486. * @param mixed $filename
  487. * @param mixed $content
  488. * @return
  489. */
  490. function nv_gz_put_contents( $filename, $content )
  491. {
  492. global $sys_info;
  493. if ( isset( $sys_info['str_compress'] ) and ! empty( $sys_info['str_compress'] ) )
  494. {
  495. $content = call_user_func( $sys_info['str_compress'][0], $content, 9 );
  496. }
  497. return file_put_contents( $filename, $content, LOCK_EX );
  498. }
  499. /**
  500. * nv_is_image()
  501. *
  502. * @param mixed $img
  503. * @return
  504. */
  505. function nv_is_image( $img )
  506. {
  507. $typeflag = array();
  508. $typeflag[1] = array( 'type' => 'IMAGETYPE_GIF', 'ext' => 'gif' );
  509. $typeflag[2] = array( 'type' => 'IMAGETYPE_JPEG', 'ext' => 'jpg' );
  510. $typeflag[3] = array( 'type' => 'IMAGETYPE_PNG', 'ext' => 'png' );
  511. $typeflag[4] = array( 'type' => 'IMAGETYPE_SWF', 'ext' => 'swf' );
  512. $typeflag[5] = array( 'type' => 'IMAGETYPE_PSD', 'ext' => 'psd' );
  513. $typeflag[6] = array( 'type' => 'IMAGETYPE_BMP', 'ext' => 'bmp' );
  514. $typeflag[7] = array( 'type' => 'IMAGETYPE_TIFF_II', 'ext' => 'tiff' );
  515. $typeflag[8] = array( 'type' => 'IMAGETYPE_TIFF_MM', 'ext' => 'tiff' );
  516. $typeflag[9] = array( 'type' => 'IMAGETYPE_JPC', 'ext' => 'jpc' );
  517. $typeflag[10] = array( 'type' => 'IMAGETYPE_JP2', 'ext' => 'jp2' );
  518. $typeflag[11] = array( 'type' => 'IMAGETYPE_JPX', 'ext' => 'jpf' );
  519. $typeflag[12] = array( 'type' => 'IMAGETYPE_JB2', 'ext' => 'jb2' );
  520. $typeflag[13] = array( 'type' => 'IMAGETYPE_SWC', 'ext' => 'swc' );
  521. $typeflag[14] = array( 'type' => 'IMAGETYPE_IFF', 'ext' => 'aiff' );
  522. $typeflag[15] = array( 'type' => 'IMAGETYPE_WBMP', 'ext' => 'wbmp' );
  523. $typeflag[16] = array( 'type' => 'IMAGETYPE_XBM', 'ext' => 'xbm' );
  524. $imageinfo = array();
  525. $file = @getimagesize( $img );
  526. if ( $file )
  527. {
  528. $channels = isset( $file['channels'] ) ? intval( $file['channels'] ) : 0;
  529. $imageinfo['src'] = $img;
  530. $imageinfo['width'] = $file[0];
  531. $imageinfo['height'] = $file[1];
  532. $imageinfo['mime'] = $file['mime'];
  533. $imageinfo['type'] = $typeflag[$file[2]]['type'];
  534. $imageinfo['ext'] = $typeflag[$file[2]]['ext'];
  535. $imageinfo['bits'] = $file['bits'];
  536. $imageinfo['channels'] = isset( $file['channels'] ) ? intval( $file['channels'] ) : 0;
  537. }
  538. return $imageinfo;
  539. }
  540. /**
  541. * nv_ImageInfo()
  542. * Function xuat ra cac thong tin ve IMAGE de dua vao HTML (src, width, height).
  543. *
  544. * @param mixed $original_name - duong dan tuyet doi den file goc (bat buoc)
  545. * @param integer $width - chieu rong xuat ra HTML (neu bang 0 se xuat ra kich thuoc thuc)
  546. * @param bool $is_create_thumb - Neu chieu rong cua hinh lon hon $width, co the tao thumbnail
  547. * @param string $thumb_path - neu tao thumbnail thi chi ra thu muc chua file thumbnail nay.
  548. * @return array('src','width','height')
  549. */
  550. function nv_ImageInfo( $original_name, $width = 0, $is_create_thumb = false, $thumb_path = '' )
  551. {
  552. if ( empty( $original_name ) ) return false;
  553. $original_name = realpath( $original_name );
  554. if ( empty( $original_name ) ) return false;
  555. $original_name = str_replace( '\\', '/', $original_name );
  556. $original_name = rtrim( $original_name, "\\/" );
  557. unset( $matches );
  558. if ( ! preg_match( "/^" . nv_preg_quote( NV_ROOTDIR ) . "\/(([a-z0-9\-\_\/]+\/)*([a-z0-9\-\_\.]+)(\.(gif|jpg|jpeg|png)))$/i", $original_name, $matches ) ) return false;
  559. $imageinfo = array();
  560. $size = @getimagesize( $original_name );
  561. if ( ! $size or ! isset( $size[0] ) or ! isset( $size[1] ) or ! $size[0] or ! $size[1] ) return false;
  562. $imageinfo['orig_src'] = $imageinfo['src'] = NV_BASE_SITEURL . $matches[1];
  563. $imageinfo['orig_width'] = $imageinfo['width'] = $size[0];
  564. $imageinfo['orig_height'] = $imageinfo['height'] = $size[1];
  565. if ( $width )
  566. {
  567. $imageinfo['width'] = $width;
  568. $imageinfo['height'] = ceil( $width * $imageinfo['orig_height'] / $imageinfo['orig_width'] );
  569. }
  570. if ( $is_create_thumb and $width and $imageinfo['orig_width'] > $width )
  571. {
  572. if ( empty( $thumb_path ) or ! is_dir( $thumb_path ) or ! is_writeable( $thumb_path ) )
  573. {
  574. $thumb_path = $matches[2];
  575. }
  576. else
  577. {
  578. $thumb_path = realpath( $thumb_path );
  579. if ( empty( $thumb_path ) )
  580. {
  581. $thumb_path = $matches[2];
  582. }
  583. else
  584. {
  585. $thumb_path = str_replace( '\\', '/', $thumb_path );
  586. unset( $matches2 );
  587. if ( preg_match( "/^" . nv_preg_quote( NV_ROOTDIR ) . "([a-z0-9\-\_\/]+)*$/i", $thumb_path, $matches2 ) )
  588. {
  589. $thumb_path = ltrim( $matches2[1], "\\/" );
  590. }
  591. else
  592. {
  593. $thumb_path = $matches[2];
  594. }
  595. }
  596. }
  597. if ( ! empty( $thumb_path ) and ! preg_match( "/\/$/", $thumb_path ) ) $thumb_path = $thumb_path . '/';
  598. $new_src = $thumb_path . $matches[3] . '_' . $width . $matches[4];
  599. $is_create = true;
  600. if ( file_exists( NV_ROOTDIR . '/' . $new_src ) )
  601. {
  602. $size = @getimagesize( NV_ROOTDIR . '/' . $new_src );
  603. if ( $size and isset( $size[0] ) and isset( $size[1] ) and $size[0] and $size[1] )
  604. {
  605. $imageinfo['src'] = NV_BASE_SITEURL . $new_src;
  606. $imageinfo['width'] = $size[0];
  607. $imageinfo['height'] = $size[1];
  608. $is_create = false;
  609. }
  610. }
  611. if ( $is_create )
  612. {
  613. include ( NV_ROOTDIR . "/includes/class/image.class.php" );
  614. $image = new image( $original_name, NV_MAX_WIDTH, NV_MAX_HEIGHT );
  615. $image->resizeXY( $width );
  616. $image->save( NV_ROOTDIR . '/' . $thumb_path, $matches[3] . '_' . $width . $matches[4] );
  617. $image_info = $image->create_Image_info;
  618. if ( file_exists( NV_ROOTDIR . '/' . $new_src ) )
  619. {
  620. $imageinfo['src'] = NV_BASE_SITEURL . $new_src;
  621. $imageinfo['width'] = $image_info['width'];
  622. $imageinfo['height'] = $image_info['height'];
  623. }
  624. }
  625. }
  626. return $imageinfo;
  627. }
  628. ?>