PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/01.Source/01.CORE/includes/class/geturl.class.php

http://creative-portal.googlecode.com/
PHP | 473 lines | 260 code | 66 blank | 147 comment | 58 complexity | 398abb7e186104c78607267baa45da6f 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 5-8-2010 1:13
  7. */
  8. /**
  9. * Get data from file as a URL
  10. *
  11. * @package NUKEVIET 3.0
  12. * @author VINADES., JSC
  13. * @copyright 2010
  14. * @version $Id$
  15. * @access public
  16. */
  17. class UrlGetContents
  18. {
  19. private $allow_methods = array();
  20. private $safe_mode;
  21. private $open_basedir;
  22. private $url_info = false;
  23. private $login = '';
  24. private $password = '';
  25. private $ref = '';
  26. private $user_agent = '';
  27. /**
  28. * UrlGetContents::__construct()
  29. *
  30. * @return
  31. */
  32. function __construct ( $global_config )
  33. {
  34. $this->user_agent = 'NUKEVIET CMS ' . $global_config['version'] . '. Developed by VINADES. Url: http://nukeviet.vn. Code: ' . md5( $global_config['sitekey'] );
  35. $disable_functions = ( ini_get( "disable_functions" ) != "" and ini_get( "disable_functions" ) != false ) ? array_map( 'trim', preg_split( "/[\s,]+/", ini_get( "disable_functions" ) ) ) : array();
  36. $safe_mode = ( ini_get( 'safe_mode' ) == '1' || strtolower( ini_get( 'safe_mode' ) ) == 'on' ) ? 1 : 0;
  37. if ( ! $safe_mode and function_exists( 'set_time_limit' ) and ! in_array( 'set_time_limit', $disable_functions ) )
  38. {
  39. set_time_limit( 0 );
  40. }
  41. if ( function_exists( 'ini_set' ) and ! in_array( 'ini_set', $disable_functions ) )
  42. {
  43. ini_set( 'allow_url_fopen', 1 );
  44. ini_set( 'default_socket_timeout', 120 );
  45. $memoryLimitMB = ( integer )ini_get( 'memory_limit' );
  46. if ( $memoryLimitMB < 64 )
  47. {
  48. ini_set( "memory_limit", "64M" );
  49. }
  50. ini_set( 'user_agent', $this->user_agent );
  51. }
  52. if ( extension_loaded( 'curl' ) )
  53. {
  54. $curl_functions = array(
  55. 'curl_init', 'curl_setopt', 'curl_exec', 'curl_errno', 'curl_close', 'curl_getinfo'
  56. );
  57. $is_enable = true;
  58. foreach ( $curl_functions as $function )
  59. {
  60. if ( ! function_exists( $function ) )
  61. {
  62. $is_enable = false;
  63. break;
  64. }
  65. if ( ! empty( $disable_functions ) and in_array( $function, $disable_functions ) )
  66. {
  67. $is_enable = false;
  68. break;
  69. }
  70. }
  71. if ( $is_enable )
  72. {
  73. $this->allow_methods[] = 'curl';
  74. }
  75. }
  76. if ( function_exists( "fsockopen" ) and ! in_array( 'fsockopen', $disable_functions ) )
  77. {
  78. $this->allow_methods[] = 'fsockopen';
  79. }
  80. if ( ini_get( 'allow_url_fopen' ) == '1' or strtolower( ini_get( 'allow_url_fopen' ) ) == 'on' )
  81. {
  82. if ( function_exists( "fopen" ) and ! in_array( 'fopen', $disable_functions ) )
  83. {
  84. $this->allow_methods[] = 'fopen';
  85. }
  86. if ( function_exists( "file_get_contents" ) and ! in_array( 'file_get_contents', $disable_functions ) )
  87. {
  88. $this->allow_methods[] = 'file_get_contents';
  89. }
  90. }
  91. if ( function_exists( "file" ) and ! in_array( 'file', $disable_functions ) )
  92. {
  93. $this->allow_methods[] = 'file';
  94. }
  95. if ( ini_get( 'safe_mode' ) == '1' || strtolower( ini_get( 'safe_mode' ) ) == 'on' )
  96. {
  97. $this->safe_mode = true;
  98. }
  99. else
  100. {
  101. $this->safe_mode = false;
  102. }
  103. if ( ini_get( 'open_basedir' ) == '1' || strtolower( ini_get( 'open_basedir' ) ) == 'on' )
  104. {
  105. $this->open_basedir = true;
  106. }
  107. else
  108. {
  109. $this->open_basedir = false;
  110. }
  111. }
  112. /**
  113. * UrlGetContents::curl_Get()
  114. *
  115. * @param mixed $url
  116. * @param string $login
  117. * @param string $password
  118. * @param string $ref
  119. * @return
  120. */
  121. private function curl_Get ( )
  122. {
  123. $curlHandle = curl_init();
  124. curl_setopt( $curlHandle, CURLOPT_ENCODING, '' );
  125. curl_setopt( $curlHandle, CURLOPT_URL, $this->url_info['uri'] );
  126. curl_setopt( $curlHandle, CURLOPT_HEADER, 0 );
  127. curl_setopt( $curlHandle, CURLOPT_RETURNTRANSFER, 1 );
  128. if ( ! empty( $this->login ) )
  129. {
  130. curl_setopt( $curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
  131. curl_setopt( CURLOPT_USERPWD, '[' . $this->login . ']:[' . $this->password . ']' );
  132. }
  133. curl_setopt( $curlHandle, CURLOPT_USERAGENT, $this->user_agent );
  134. if ( ! empty( $this->ref ) )
  135. {
  136. curl_setopt( $curlHandle, CURLOPT_REFERER, urlencode( $this->ref ) );
  137. }
  138. else
  139. {
  140. curl_setopt( $curlHandle, CURLOPT_REFERER, $this->url_info['uri'] );
  141. }
  142. if ( ! $this->safe_mode and $this->open_basedir )
  143. {
  144. curl_setopt( $curlHandle, CURLOPT_FOLLOWLOCATION, 1 );
  145. curl_setopt( $curlHandle, CURLOPT_MAXREDIRS, 10 );
  146. }
  147. curl_setopt( $curlHandle, CURLOPT_TIMEOUT, 30 );
  148. $result = curl_exec( $curlHandle );
  149. if ( curl_errno( $curlHandle ) == 23 || curl_errno( $curlHandle ) == 61 )
  150. {
  151. curl_setopt( $curlHandle, CURLOPT_ENCODING, 'none' );
  152. $result = curl_exec( $curlHandle );
  153. }
  154. if ( curl_errno( $curlHandle ) )
  155. {
  156. curl_close( $curlHandle );
  157. return false;
  158. }
  159. $response = curl_getinfo( $curlHandle );
  160. if ( ( $response['http_code'] < 200 ) || ( 300 <= $response['http_code'] ) )
  161. {
  162. curl_close( $curlHandle );
  163. return false;
  164. }
  165. curl_close( $curlHandle );
  166. return $result;
  167. }
  168. /**
  169. * UrlGetContents::fsockopen_Get()
  170. *
  171. * @param mixed $url
  172. * @param string $login
  173. * @param string $password
  174. * @param string $ref
  175. * @return
  176. */
  177. private function fsockopen_Get ( )
  178. {
  179. if ( strtolower( $this->url_info['scheme'] ) == 'https' )
  180. {
  181. $this->url_info['host'] = "ssl://" . $this->url_info['host'];
  182. $this->url_info['port'] = 443;
  183. }
  184. $fp = @fsockopen( $this->url_info['host'], $this->url_info['port'], $errno, $errstr, 30 );
  185. if ( ! $fp )
  186. {
  187. return false;
  188. }
  189. $request = "GET " . $this->url_info['path'] . $this->url_info["query"];
  190. $request .= " HTTP/1.0\r\n";
  191. $request .= "Host: " . $this->url_info['host'];
  192. if ( $this->url_info['port'] != 80 )
  193. {
  194. $request .= ":" . $info['port'];
  195. }
  196. $request .= "\r\n";
  197. $request .= "Connection: Close\r\n";
  198. $request .= "User-Agent: " . $this->user_agent . "\r\n\r\n";
  199. if ( function_exists( 'gzinflate' ) )
  200. {
  201. $request .= "Accept-Encoding: gzip,deflate\r\n";
  202. }
  203. $request .= "Accept: */*\r\n";
  204. if ( ! empty( $this->ref ) )
  205. {
  206. $request .= "Referer: " . urlencode( $this->ref ) . "\r\n";
  207. }
  208. else
  209. {
  210. $request .= "Referer: " . $this->url_info['uri'] . "\r\n";
  211. }
  212. if ( ! empty( $this->login ) )
  213. {
  214. $request .= "Authorization: Basic ";
  215. $request .= base64_encode( $this->login . ':' . $this->password );
  216. $request .= "\r\n";
  217. }
  218. $request .= "\r\n";
  219. if ( @fwrite( $fp, $request ) === false )
  220. {
  221. @fclose( $fp );
  222. return false;
  223. }
  224. @stream_set_blocking( $fp, true );
  225. @stream_set_timeout( $fp, 30 );
  226. $in_f = @stream_get_meta_data( $fp );
  227. $response = "";
  228. while ( ( ! @feof( $fp ) ) && ( ! $in_f['timed_out'] ) )
  229. {
  230. $response .= @fgets( $fp, 4096 );
  231. $inf = @stream_get_meta_data( $fp );
  232. if ( $inf['timed_out'] )
  233. {
  234. @fclose( $fp );
  235. return false;
  236. }
  237. }
  238. if ( function_exists( 'gzinflate' ) and substr( $response, 0, 8 ) == "\x1f\x8b\x08\x00\x00\x00\x00\x00" )
  239. {
  240. $response = substr( $response, 10 );
  241. $response = gzinflate( $response );
  242. }
  243. @fclose( $fp );
  244. list( $header, $result ) = preg_split( "/\r?\n\r?\n/", $response, 2 );
  245. unset( $matches );
  246. preg_match( "/^HTTP\/[0-9\.]+\s+(\d+)\s+/", $header, $matches );
  247. if ( $matches == array() || $matches[1] != 200 )
  248. {
  249. return false;
  250. }
  251. return $result;
  252. }
  253. /**
  254. * UrlGetContents::fopen_Get()
  255. *
  256. * @param mixed $url
  257. * @return
  258. */
  259. private function fopen_Get ( )
  260. {
  261. if ( ( $fd = @fopen( $this->url_info['uri'], "rb" ) ) === false )
  262. {
  263. return false;
  264. }
  265. $result = '';
  266. while ( ( $data = fread( $fd, 4096 ) ) != "" )
  267. {
  268. $result .= $data;
  269. }
  270. fclose( $fd );
  271. return $result;
  272. }
  273. /**
  274. * UrlGetContents::file_get_contents_Get()
  275. *
  276. * @param mixed $url
  277. * @return
  278. */
  279. private function file_get_contents_Get ( )
  280. {
  281. return file_get_contents( $this->url_info['uri'] );
  282. }
  283. /**
  284. * UrlGetContents::file_Get()
  285. *
  286. * @param mixed $url
  287. * @return void
  288. */
  289. private function file_Get ( )
  290. {
  291. $result = file( $this->url_info['uri'] );
  292. if ( $result ) return implode( $result );
  293. return '';
  294. }
  295. /**
  296. * UrlGetContents::url_get_info()
  297. *
  298. * @param mixed $url
  299. * @return
  300. */
  301. private function url_get_info ( $url )
  302. {
  303. //URL: http://username:password@www.example.com:80/dir/page.php?foo=bar&foo2=bar2#bookmark
  304. $url_info = @parse_url( $url );
  305. //[host] => www.example.com
  306. if ( ! isset( $url_info['host'] ) )
  307. {
  308. return false;
  309. }
  310. //[port] => :80
  311. $url_info['port'] = isset( $url_info['port'] ) ? $url_info['port'] : 80;
  312. //[login] => username:password@
  313. $url_info['login'] = '';
  314. if ( isset( $url_info['user'] ) )
  315. {
  316. $url_info['login'] = $url_info['user'];
  317. if ( isset( $url_info['pass'] ) )
  318. {
  319. $url_info['login'] .= ':' . $url_info['pass'];
  320. }
  321. $url_info['login'] .= '@';
  322. }
  323. //[path] => /dir/page.php
  324. if ( isset( $url_info['path'] ) )
  325. {
  326. if ( substr( $url_info['path'], 0, 1 ) != '/' )
  327. {
  328. $url_info['path'] = '/' . $url_info['path'];
  329. }
  330. }
  331. else
  332. {
  333. $url_info['path'] = '/';
  334. }
  335. //[query] => ?foo=bar&foo2=bar2
  336. $url_info['query'] = ( isset( $url_info['query'] ) and ! empty( $url_info['query'] ) ) ? '?' . $url_info['query'] : '';
  337. //[fragment] => bookmark
  338. $url_info['fragment'] = isset( $url_info['fragment'] ) ? $url_info['fragment'] : '';
  339. //[file] => page.php
  340. $url_info['file'] = explode( '/', $url_info['path'] );
  341. $url_info['file'] = array_pop( $url_info['file'] );
  342. //[dir] => /dir
  343. $url_info['dir'] = substr( $url_info['path'], 0, strrpos( $url_info['path'], '/' ) );
  344. //[base] => http://www.example.com/dir
  345. $url_info['base'] = $url_info['scheme'] . '://' . $url_info['host'] . $url_info['dir'];
  346. //[uri] => http://username:password@www.example.com:80/dir/page.php?#bookmark
  347. $url_info['uri'] = $url_info['scheme'] . '://' . $url_info['login'] . $url_info['host'];
  348. if ( $url_info['port'] != 80 )
  349. {
  350. $url_info['uri'] .= ':' . $url_info['port'];
  351. }
  352. $url_info['uri'] .= $url_info['path'] . $url_info['query'];
  353. /*if ( $url_info['fragment'] != '' )
  354. {
  355. $url_info['uri'] .= '#' . $url_info['fragment'];
  356. }*/
  357. return $url_info;
  358. }
  359. /**
  360. * UrlGetContents::get()
  361. *
  362. * @param mixed $url
  363. * @param string $login
  364. * @param string $password
  365. * @param string $ref
  366. * @return
  367. */
  368. public function get ( $url, $login = '', $password = '', $ref = '' )
  369. {
  370. $this->url_info = $this->url_get_info( $url );
  371. if ( ! $this->url_info or ! isset( $this->url_info['scheme'] ) )
  372. {
  373. return false;
  374. }
  375. $this->login = ( string )$login;
  376. $this->password = ( string )$password;
  377. $this->ref = ( string )$ref;
  378. if ( ! empty( $this->allow_methods ) )
  379. {
  380. foreach ( $this->allow_methods as $method )
  381. {
  382. $result = call_user_func( array(
  383. &$this, $method . '_Get'
  384. ) );
  385. if ( ! empty( $result ) )
  386. {
  387. return $result;
  388. break;
  389. }
  390. }
  391. }
  392. return '';
  393. }
  394. }
  395. ?>