forked from sergeychernyshev/showslow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagetest.php
105 lines (86 loc) · 2.83 KB
/
pagetest.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
<?php
require_once(dirname(__FILE__).'/global.php');
require_once(dirname(__FILE__).'/users/users.php');
if (!is_null($webPageTestKey) && array_key_exists('url', $_POST))
{
$url_id = getUrlId($_POST['url']);
$runtest = $webPageTestBase.'runtest.php?k='.urlencode($webPageTestKey).'&'.($webPageTestExtraParams ? $webPageTestExtraParams.'&' : '').'f=xml&r=yes&url='.urlencode($_POST['url']);
$location = null;
$private = false;
if (array_key_exists('location', $_POST)) {
$location = $_POST['location'];
$runtest.='&location='.$location;
} else {
header('Location: '.$showslow_base.'#no-pagetest-location');
exit;
}
if (array_key_exists('private', $_POST)) {
$private = $_POST['private'];
$runtest.='&private='.$_POST['private'];
}
if (array_key_exists('fvonly', $_POST)) {
$runtest.='&fvonly='.$_POST['fvonly'];
}
// fetching locations only when needed
getPageTestLocations();
if (!array_key_exists($location, $webPageTestLocationsById))
{
// location doesn't exist
error_log("PageTest Location doesn't exist: $location");
header('Location: '.$showslow_base.'#pagetest-location-doesn-exist');
exit;
} else if ($webPageTestLocationsById[$location]['tests'] > 50) {
// location is overloaded
error_log("PageTest Location is overloaded: $location");
header('Location: '.$showslow_base.'#pagetest-location-overloaded');
exit;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $runtest);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
if (empty($output)) {
$err = curl_error($ch);
curl_close($ch);
failWithMessage("API call ($runtest) failed: ".$err);
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code != 200) {
curl_close($ch);
failWithMessage("PageTest didn't accept the request: $code");
}
curl_close($ch);
$xml = new SimpleXMLElement($output);
if (empty($xml)) {
failWithMessage("Failed to parse XML response");
}
if ($xml->statusCode != 200) {
failWithMessage("PageTest returned failure status code: ".$xml->statusCode." (".$xml->statusText.")");
}
$testId = $xml->data->testId;
if (!$private || $keepPrivatePageTests) {
# adding new entry
$query = sprintf("INSERT INTO pagetest (url_id, test_id, location)
VALUES ('%d', '%s', '%s')",
mysql_real_escape_string($url_id),
mysql_real_escape_string($testId),
mysql_real_escape_string($location)
);
if (!mysql_query($query))
{
failWithMessage(mysql_error());
}
# updating modification date for the URL
$query = sprintf("UPDATE urls SET last_update = now() WHERE id = %d",
mysql_real_escape_string($url_id)
);
$result = mysql_query($query);
}
$current_user = User::get();
if (!is_null($current_user)) {
$current_user->recordActivity(SHOWSLOW_ACTIVITY_PAGETEST_START);
}
header('Location: '.$webPageTestBase.'result/'.$testId.'/');
exit;
}
header('Location: '.$showslow_base);