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

/wp-content/plugins/wordpress-https/lib/WordPressHTTPS/Url.php

https://gitlab.com/gregtyka/helloworld1234
PHP | 555 lines | 231 code | 59 blank | 265 comment | 39 complexity | cadd178532babe5f91eb17713a944615 MD5 | raw file
  1. <?php
  2. /**
  3. * URL Class for the WordPress plugin WordPress HTTPS
  4. *
  5. * This class and its properties are heavily based on parse_url()
  6. *
  7. * @author Mike Ems
  8. * @package WordPressHTTPS
  9. *
  10. */
  11. class WordPressHTTPS_Url {
  12. /**
  13. * The scheme of a network host; for example, http or https
  14. *
  15. * @var string
  16. */
  17. protected $_scheme;
  18. /**
  19. * The domain name of a network host, or an IPv4 address as a set of four decimal digit groups separated by literal periods; for example, www.php.net or babelfish.altavista.com
  20. *
  21. * @var string
  22. */
  23. protected $_host;
  24. /**
  25. * The base domain of a network host; for example, php.net or altavista.com
  26. *
  27. * @var string
  28. */
  29. protected $_base_host;
  30. /**
  31. * The port being accessed. In the URL http://www.some_host.com:443/, 443 is the port component.
  32. *
  33. * @var int
  34. */
  35. protected $_port;
  36. /**
  37. * The username being passed for authentication. In the URL ftp://some_user:some_password@ftp.host.com/, some_user would be the user component.
  38. *
  39. * @var string
  40. */
  41. protected $_user;
  42. /**
  43. * The password being passed for authentication. In the above example, some_password would be the pass component.
  44. *
  45. * @var string
  46. */
  47. protected $_pass;
  48. /**
  49. * The path component contains the location to the requested resource on the given host. In the URL http://www.foo.com/test/test.php, /test/test.php is the path component.
  50. *
  51. * @var string
  52. */
  53. protected $_path;
  54. /**
  55. * The filename, if available, is the specified resource being requested. In the URL http://www.foo.com/test/test.jpg, test.jpg is the filename.
  56. *
  57. * @var string
  58. */
  59. protected $_filename;
  60. /**
  61. * The file extension of the filename, if available. In the URL http://www.foo.com/test/test.jpg, .jpg is the file extension.
  62. *
  63. * @var string
  64. */
  65. protected $_extension;
  66. /**
  67. * The query string for the request. In the URL http://www.foo.com/?page=bar, page=bar is the query component.
  68. *
  69. * @var string
  70. */
  71. protected $_query;
  72. /**
  73. * The fragment string for the request. In the URL http://www.foo.com/?page=bar#test, #test is the fragment component.
  74. *
  75. * @var string
  76. */
  77. protected $_fragment;
  78. /**
  79. * The response body of the request.
  80. *
  81. * @var string
  82. */
  83. protected $_content;
  84. /**
  85. * Set Scheme
  86. *
  87. * @param string $scheme
  88. * @return object $this
  89. */
  90. public function setScheme( $scheme ) {
  91. $this->_scheme = $scheme;
  92. return $this;
  93. }
  94. /**
  95. * Get Scheme
  96. *
  97. * @param none
  98. * @return string
  99. */
  100. public function getScheme() {
  101. return $this->_scheme;
  102. }
  103. /**
  104. * Set Host
  105. *
  106. * @param string $host
  107. * @return object $this
  108. */
  109. public function setHost( $host ) {
  110. $this->_host = $host;
  111. return $this;
  112. }
  113. /**
  114. * Get Host
  115. *
  116. * @param none
  117. * @return string
  118. */
  119. public function getHost() {
  120. return $this->_host;
  121. }
  122. /**
  123. * Set Base Host
  124. *
  125. * @param string $base_host
  126. * @return object $this
  127. */
  128. public function setBaseHost( $base_host ) {
  129. $this->_base_host = $base_host;
  130. return $this;
  131. }
  132. /**
  133. * Gets the base host of the URL
  134. *
  135. * @param none
  136. * @return string
  137. */
  138. public function getBaseHost() {
  139. $return_url = clone $this;
  140. $test_url = clone $this;
  141. $test_url->setPath('');
  142. $host_parts = explode('.', $test_url->getHost());
  143. for ( $i = 0; $i <= sizeof($host_parts); $i++ ) {
  144. $test_url->setHost( str_replace($host_parts[$i] . '.', '', $test_url->getHost()) );
  145. if ( $test_url->isValid() ) {
  146. $return_url = clone $test_url;
  147. } else {
  148. break;
  149. }
  150. }
  151. return $return_url->getHost();
  152. }
  153. /**
  154. * Set Port
  155. *
  156. * @param string $port
  157. * @return object $this
  158. */
  159. public function setPort( $port ) {
  160. $this->_port = $port;
  161. return $this;
  162. }
  163. /**
  164. * Get Port
  165. *
  166. * @param none
  167. * @return string
  168. */
  169. public function getPort() {
  170. return $this->_port;
  171. }
  172. /**
  173. * Set User
  174. *
  175. * @param string $user
  176. * @return object $this
  177. */
  178. public function setUser( $user ) {
  179. $this->_user = $user;
  180. return $this;
  181. }
  182. /**
  183. * Get User
  184. *
  185. * @param none
  186. * @return string
  187. */
  188. public function getUser() {
  189. return $this->_user;
  190. }
  191. /**
  192. * Set Pass
  193. *
  194. * @param string $pass
  195. * @return object $this
  196. */
  197. public function setPass( $pass ) {
  198. $this->_pass = $pass;
  199. return $this;
  200. }
  201. /**
  202. * Get Pass
  203. *
  204. * @param none
  205. * @return string
  206. */
  207. public function getPass() {
  208. return $this->_pass;
  209. }
  210. /**
  211. * Set Path
  212. *
  213. * Ensures the path begins with a forward slash
  214. *
  215. * @param none
  216. * @return string
  217. */
  218. public function setPath( $path ) {
  219. $this->_path = ltrim($path, '/');
  220. $this->_path = '/' . $this->_path;
  221. $filename = basename($this->_path);
  222. $pathinfo = pathinfo($filename);
  223. if ( $pathinfo && isset($pathinfo['extension']) ) {
  224. $this->setExtension($pathinfo['extension']);
  225. $this->setFilename($filename);
  226. }
  227. return $this;
  228. }
  229. /**
  230. * Get Path
  231. *
  232. * Ensures the path begins with a forward slash
  233. *
  234. * @param none
  235. * @return string
  236. */
  237. public function getPath() {
  238. return $this->_path;
  239. }
  240. /**
  241. * Set Filename
  242. *
  243. * @param string $filename
  244. * @return object $this
  245. */
  246. public function setFilename( $filename ) {
  247. $this->_filename = $filename;
  248. return $this;
  249. }
  250. /**
  251. * Get Filename
  252. *
  253. * @param none
  254. * @return string
  255. */
  256. public function getFilename() {
  257. return $this->_filename;
  258. }
  259. /**
  260. * Set Extension
  261. *
  262. * @param string $extension
  263. * @return object $this
  264. */
  265. public function setExtension( $extension ) {
  266. $this->_extension = $extension;
  267. return $this;
  268. }
  269. /**
  270. * Get Extension
  271. *
  272. * @param none
  273. * @return string
  274. */
  275. public function getExtension() {
  276. return $this->_extension;
  277. }
  278. /**
  279. * Set Query
  280. *
  281. * @param string $query
  282. * @return object $this
  283. */
  284. public function setQuery( $query ) {
  285. $this->_query = $query;
  286. return $this;
  287. }
  288. /**
  289. * Get Query
  290. *
  291. * @param none
  292. * @return string
  293. */
  294. public function getQuery() {
  295. return $this->_query;
  296. }
  297. /**
  298. * Set Fragment
  299. *
  300. * @param string $fragment
  301. * @return object $this
  302. */
  303. public function setFragment( $fragment ) {
  304. $this->_fragment = $fragment;
  305. return $this;
  306. }
  307. /**
  308. * Get Fragment
  309. *
  310. * @param none
  311. * @return string
  312. */
  313. public function getFragment() {
  314. return $this->_fragment;
  315. }
  316. /**
  317. * Set Content
  318. *
  319. * @param string $content
  320. * @return object $this
  321. */
  322. public function setContent( $content ) {
  323. $this->_content = $content;
  324. return $this;
  325. }
  326. /**
  327. * Get the contents of the URL
  328. *
  329. * @param boolean $verify_ssl
  330. * @return boolean
  331. */
  332. public function getContent( $verify_ssl = false ) {
  333. if ( $this->_content ) {
  334. return $this->_content;
  335. }
  336. $this->_content = false;
  337. if ( function_exists('curl_init') ) {
  338. $ch = curl_init();
  339. curl_setopt($ch, CURLOPT_URL, $this->toString());
  340. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
  341. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify_ssl);
  342. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  343. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  344. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  345. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  346. curl_setopt($ch, CURLOPT_HEADER, false);
  347. curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
  348. $content = curl_exec($ch);
  349. $info = curl_getinfo($ch);
  350. curl_close($ch);
  351. if ( isset($info['http_code']) && !( $info['http_code'] == 0 || $info['http_code'] == 404 ) ) {
  352. $this->_content = $content;
  353. }
  354. }
  355. if ( !$this->_content && @ini_get('allow_url_fopen') ) {
  356. if ( ($content = @file_get_contents($this->toString())) !== false ) {
  357. $this->_content = $content;
  358. }
  359. }
  360. return $this->_content;
  361. }
  362. /**
  363. * Validates the existence of the URL with cURL or file_get_contents()
  364. *
  365. * @param boolean $verify_ssl
  366. * @return boolean
  367. */
  368. public function isValid( $verify_ssl = false ) {
  369. if ( function_exists('curl_init') ) {
  370. $ch = curl_init();
  371. curl_setopt($ch, CURLOPT_URL, $this->toString());
  372. curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
  373. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify_ssl);
  374. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  375. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  376. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  377. @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  378. curl_setopt($ch, CURLOPT_HEADER, false);
  379. curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
  380. $content = curl_exec($ch);
  381. $info = curl_getinfo($ch);
  382. curl_close($ch);
  383. if ( !$info['http_code'] || ( $info['http_code'] == 0 || $info['http_code'] == 404 ) ) {
  384. return false;
  385. } else {
  386. return true;
  387. }
  388. } else if ( @ini_get('allow_url_fopen') ) {
  389. if ( @file_get_contents($url) !== false ) {
  390. return true;
  391. }
  392. }
  393. return false;
  394. }
  395. /**
  396. * Compares URL objects to determine if either of them are a subdomain of the other.
  397. *
  398. * @param WordPressHTTPS_Url $url
  399. * @return boolean
  400. */
  401. public function isSubdomain( WordPressHTTPS_Url $url ) {
  402. $this_host = $this->getBaseHost();
  403. $other_host = $url->getBaseHost();
  404. if ( $this_host == $other_host ) {
  405. return true;
  406. }
  407. return false;
  408. }
  409. /**
  410. * Factory object from an array provided by the parse_url function
  411. *
  412. * Example of usage:
  413. * $site_url = WordPressHTTPS_Url::fromArray( parse_url( site_url() ) );
  414. *
  415. * @param array $array
  416. * @return $url WordPressHTTPS_Url
  417. */
  418. public static function fromArray( $array = array() ) {
  419. if ( sizeof($array) <= 1 ) {
  420. return false;
  421. }
  422. $url = new WordPressHTTPS_Url;
  423. $url->setScheme(@$array['scheme']);
  424. $url->setUser(@$array['user']);
  425. $url->setPass(@$array['pass']);
  426. $url->setHost(@$array['host']);
  427. $url->setPort(@$array['port']);
  428. $url->setPath(@$array['path']);
  429. $url->setQuery(@$array['query']);
  430. $url->setFragment(@$array['fragment']);
  431. return $url;
  432. }
  433. /**
  434. * Factory object from a string that contains a URL
  435. *
  436. * Example of usage:
  437. * $site_url = WordPressHTTPS_Url::fromString( site_url() );
  438. *
  439. * @param string $string
  440. * @return $url WordPressHTTPS_Url
  441. */
  442. public static function fromString( $string ) {
  443. $url = new WordPressHTTPS_Url;
  444. @preg_match_all('/((http|https):\/\/[^\'"]+)[\'"\)]?/i', $string, $url_parts);
  445. if ( isset($url_parts[1][0]) ) {
  446. if ( $url_parts = parse_url( $url_parts[1][0] ) ) {
  447. $url->setScheme(@$url_parts['scheme']);
  448. $url->setUser(@$url_parts['user']);
  449. $url->setPass(@$url_parts['pass']);
  450. $url->setHost(@$url_parts['host']);
  451. $url->setPort(@$url_parts['port']);
  452. $url->setPath(@$url_parts['path']);
  453. $url->setQuery(@$url_parts['query']);
  454. $url->setFragment(@$url_parts['fragment']);
  455. return $url;
  456. }
  457. } else {
  458. return false;
  459. }
  460. return $url;
  461. }
  462. /**
  463. * Returns an array of all URL properties
  464. *
  465. * @param none
  466. * @return array parse_url
  467. */
  468. public function toArray() {
  469. return parse_url( $this->toString() );
  470. }
  471. /**
  472. * Formats the current URL object to a string
  473. *
  474. * @param none
  475. * @return string
  476. */
  477. public function toString() {
  478. $string = ( $this->getScheme() ? $this->getScheme() . '://' : '' ) .
  479. ( $this->getUser() ? $this->getUser() . ( $this->getPass() ? ':' . $this->getPass() : '' ) . '@' : '' ) .
  480. $this->getHost() .
  481. ( $this->getPort() && ( ( $this->getPort() != 80 && $this->getScheme() == 'http' ) || ( $this->getPort() != 443 && $this->getScheme() == 'https' ) ) ? ':' . $this->getPort() : '' ) .
  482. $this->getPath() .
  483. ( $this->getQuery() ? '?' . $this->getQuery() : '' ) .
  484. ( $this->getFragment() ? '#' . $this->getFragment() : '' );
  485. return $string;
  486. }
  487. /**
  488. * Magic __toString method that is called when the object is casted to a string
  489. *
  490. * @param none
  491. * @return string
  492. */
  493. public function __toString() {
  494. return $this->toString();
  495. }
  496. }