/
UriUtils.inc
executable file
·149 lines (125 loc) · 4.21 KB
/
UriUtils.inc
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
require_once("parapara.inc");
function getFilenameForId($id)
{
global $config;
return '../' . $config['characters']['path'] . '/' . $id . ".svg";
}
function getGalleryUrlForId($id)
{
// Return the raw url without the prefix which we map to the gallery page with
// a rewrite rule
return substr(getRawUrlForId($id), 0, -4);
}
function getRawUrlForId($id)
{
return fileToUrl(getFilenameForId($id));
}
function shortenUrl($url)
{
global $config;
if (!isset($config['bitly']) ||
!@$config['bitly']['login'] ||
!@$config['bitly']['apiKey'])
return $url;
$login = $config['bitly']['login'];
$apiKey = $config['bitly']['apiKey'];
$shorten_url = "http://api.bit.ly/v3/shorten?login=$login&apiKey=$apiKey" .
"&uri=" . urlencode($url) . "&format=txt";
$shortened = getUrl($shorten_url);
// If there is an error, return the original url
return $shortened ? trim($shortened) : $url;
}
function getUrl($url, $data = null) {
return fetchUrl($url, $data, 'get');
}
function postUrl($url, $data = null) {
return fetchUrl($url, $data, 'post');
}
function fetchUrl($url, $data, $type) {
global $config;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
if (strtolower($type) == 'post') {
curl_setopt($ch, CURLOPT_POST, TRUE);
}
if ($data) {
$data_string = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
}
if (substr($url, 0, 5) == 'https') {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, $config['net']['certfile']);
}
$result = curl_exec($ch);
if ($result === FALSE && curl_errno($ch) == 60) {
error_log("Failed to verify SSL certificate when loading $url. " .
"Add the CA certificate to the CACerts folder");
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $result !== FALSE && $httpCode == 200 ? $result : null;
}
// Take a path (relative or absolute) and generate a corresponding absolute URL
// This isn't perfect. Symlinks can trip it up but it should work for simple
// cases.
//
// It works by comparing SCRIPT_FILENAME and SCRIPT_NAME to find how files map
// to urls and then appends the base URL path to the given file.
function fileToUrl($file)
{
// Get full path for $file
$filePath = realpath($file);
if (!$filePath)
return false; // File not found
// Get full path for script
$scriptPath = realpath($_SERVER['SCRIPT_FILENAME']);
if (!$scriptPath)
return false; // Script not found
// Find common part of paths
$len = min(strlen($filePath), strlen($scriptPath));
if ($len < 1)
return false; // One of the paths is empty
$i = 0;
for (; $i < $len; $i++) {
if ($filePath[$i] !== $scriptPath[$i])
break;
}
if ($i == 0)
return false; // Paths share nothing in common
// Remove common part
$filePath = substr($filePath, $i);
$scriptPath = substr($scriptPath, $i);
// Convert directory separator to url syntax
if (DIRECTORY_SEPARATOR != '/') {
$filePath = strtr($filePath, DIRECTORY_SEPARATOR, '/');
$scriptPath = strtr($scriptPath, DIRECTORY_SEPARATOR, '/');
}
// Check we're not going below the doc root
if (strlen($scriptPath) > strlen($_SERVER['SCRIPT_NAME']))
return false;
// Remove non-common part of url
if ($scriptPath) {
$basePath = substr($_SERVER['SCRIPT_NAME'], 0, -strlen($scriptPath));
} else {
$basePath = $_SERVER['SCRIPT_NAME'];
}
// Append file path
$path = $basePath . $filePath;
// Make up url append the scheme, hostname, port, etc.
return getCurrentServer() . $path;
}
function getCurrentServer() {
$scheme = @$_SERVER['HTTPS'] ? 'https://' : 'http://';
$defaultPort = @$_SERVER['HTTPS'] ? '443' : '80';
$port = $_SERVER['SERVER_PORT'] != $defaultPort
? ':' . $_SERVER['SERVER_PORT']
: '';
return $scheme . $_SERVER['SERVER_NAME'] . $port;
}