-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.php
188 lines (167 loc) · 3.99 KB
/
functions.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* @file
* Script for testing a list of http redirects.
*/
/**
* Helper function to output print_r() surrounded by <pre> tags.
*
* @param string $input
* The text to output.
*/
function print_r_clean($input) {
echo '<pre>';
print_r($input);
echo '</pre>';
}
/**
* Check if a value is greater than 1.
*
* @param int $input
* The value to check.
* @return bool
* TRUE if the value is greater than 1.
*/
function more_than_1($input) {
return $input > 1;
}
/**
* Prepare a curl resource.
*
* @param string $proxy
* An http proxy to use, if required.
*
* @return resource
* a cURL handle on success, false on errors.
*/
function setup_curl($proxy = '', $user = '', $password = '') {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_NOBODY, TRUE);
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
if ($proxy) {
curl_setopt($curl, CURLOPT_PROXY, $proxy);
}
if ($user && $password) {
curl_setopt($curl, CURLOPT_USERPWD, $user . ":" . $password);
}
return $curl;
}
/**
* Check if a URL is invalid, according to FILTER_VALIDATE_URL.
*
* @param string $url
* @return bool
* TRUE if the URL should be considered invalid.
*/
function invalid_url($url) {
return filter_var($url, FILTER_VALIDATE_URL) === FALSE;
}
/**
* Visit a URL.
*
* @param resource $curl
* a cURL handle
*
* @param string $url
* The URL to visit.
*
* @return array
* Array containing:
* url
* status code
*/
function visit_url($curl, $url) {
$result = array();
if (substr($url, 0, 4) == "http") {
curl_setopt($curl, CURLOPT_URL, $url);
$response = @curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($status) {
preg_match('/\b(?:(?:https?|http):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i', $response, $matches);
if (!empty($matches[0])) {
$result['target'] = $matches[0];
}
}
$result['http_code'] = $status;
$result['url'] = $url;
}
return $result;
}
/**
* Check if two URLs match.
*
* @param string $expected_url
* @param string $actual_url
* @return bool
* TRUE if the URLs should be considered a match.
*/
function url_matches($expected_url, $actual_url) {
$match = FALSE;
// Exact match.
if ($expected_url == $actual_url) {
$match = TRUE;
}
// Has a trailing slash.
if ($expected_url . '/' == $actual_url) {
$match = TRUE;
}
return $match;
}
/**
* Convert an array to CSV output.
*
* @param array $array
* The input array
*
* @return null|string
* The CSV output, or NULL if the array is empty
*/
function array2csv(array &$array) {
if (empty($array)) {
return NULL;
}
ob_start();
$df = fopen("php://output", 'w');
fputcsv($df, array_keys(reset($array)));
foreach ($array as $row) {
fputcsv($df, $row);
}
fclose($df);
return ob_get_clean();
}
/**
* Output headers for a filename.
*
* @param string $filename
* The name of the file.
*/
function download_send_headers($filename) {
// Disable caching.
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// Force download.
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// Set disposition / encoding on response body.
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
function output_csv_results($resData) {
$results = unserialize($resData);
if (is_array($results)) {
download_send_headers("redirect-test-results_" . date("Y-m-d") . ".csv");
echo array2csv($results);
}
else {
print_r_clean($results);
}
}
function ifset($array, $element, $default = '')
{
return isset($array[$element]) ? $array[$element] : $default;
}