-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathUrl.php
109 lines (100 loc) · 2.83 KB
/
Url.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Gelato Library
*
* This source file is part of the Gelato Library. More information,
* documentation and tutorials can be found at http://gelato.monstra.org
*
* @package Gelato
*
* @author Romanenko Sergey / Awilum <[email protected]>
* @copyright 2012-2014 Romanenko Sergey / Awilum <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class Url
{
/**
* Protected constructor since this is a static class.
*
* @access protected
*/
protected function __construct()
{
// Nothing here
}
/**
* Takes a long url and uses the TinyURL API to return a shortened version.
*
* <code>
* echo Url::tiny('http:://sitename.com');
* </code>
*
* @param string $url Long url
* @return string
*/
public static function tiny($url)
{
return file_get_contents('http://tinyurl.com/api-create.php?url='.(string) $url);
}
/**
* Check is url exists
*
* <code>
* if (Url::exists('http:://sitename.com')) {
* // Do something...
* }
* </code>
*
* @param string $url Url
* @return boolean
*/
public static function exists($url)
{
$a_url = parse_url($url);
if ( ! isset($a_url['port'])) $a_url['port'] = 80;
$errno = 0;
$errstr = '';
$timeout = 30;
if (isset($a_url['host']) && $a_url['host']!=gethostbyname($a_url['host'])) {
$fid = fsockopen($a_url['host'], $a_url['port'], $errno, $errstr, $timeout);
if ( ! $fid) return false;
$page = isset($a_url['path']) ? $a_url['path'] : '';
$page .= isset($a_url['query']) ? '?'.$a_url['query'] : '';
fputs($fid, 'HEAD '.$page.' HTTP/1.0'."\r\n".'Host: '.$a_url['host']."\r\n\r\n");
$head = fread($fid, 4096);
fclose($fid);
return preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head);
} else {
return false;
}
}
/**
* Gets the base URL
*
* <code>
* echo Url::base();
* </code>
*
* @return string
*/
public static function base()
{
$https = (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on') ? 'https://' : 'http://';
return $https . rtrim(rtrim($_SERVER['HTTP_HOST'], '\\/') . dirname($_SERVER['PHP_SELF']), '\\/');
}
/**
* Gets current URL
*
* <code>
* echo Url::current();
* </code>
*
* @return string
*/
public static function current()
{
return (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
}
}