PageRenderTime 225ms CodeModel.GetById 120ms app.highlight 16ms RepoModel.GetById 85ms app.codeStats 0ms

/my.php

http://showslow.googlecode.com/
PHP | 250 lines | 213 code | 37 blank | 0 comment | 60 complexity | e1b96f08ff49d6adcda7961fbccd1043 MD5 | raw file
  1<?php
  2require_once(dirname(__FILE__).'/global.php');
  3require_once(dirname(__FILE__).'/users/users.php');
  4
  5$current_user = User::require_login();
  6
  7if (array_key_exists('delete', $_POST) && is_array($_POST['delete'])) {
  8	$delete = array_keys($_POST['delete']);
  9
 10	$first = true;
 11	$deleteids = '';
 12	foreach ($delete as $id) {
 13		if (!is_numeric($id)) {
 14			next;
 15		}
 16
 17		if ($first) {
 18			$first = false;
 19		}
 20		else
 21		{
 22			$deleteids.=', ';
 23		}
 24
 25		$deleteids.=$id;
 26	}
 27
 28	if (!$first && $deleteids != '') {
 29		$query = sprintf("DELETE FROM user_urls WHERE user_id = %d AND url_id IN (%s)",
 30			$current_user->getID(),
 31			$deleteids
 32		);
 33
 34		$result = mysql_query($query);
 35
 36		if (!$result) {
 37			error_log(mysql_error());
 38		}
 39	}
 40	header('Location: '.$showslow_base.'my.php#deleted');
 41	exit;
 42}
 43
 44if (in_array($current_user->getID(), $noMaxURLsForUsers)) {
 45	$maxURLsPerUser = false;
 46}
 47
 48$noMoreURLs = false;
 49if ($maxURLsPerUser)
 50{
 51	$query = sprintf('SELECT count(*) AS cnt FROM user_urls	WHERE user_urls.user_id = %d', $current_user->getID());
 52
 53	$result = mysql_query($query);
 54
 55	if (!$result) {
 56		error_log(mysql_error());
 57	}
 58
 59	if ($cnt = mysql_fetch_row($result) && $cnt[0] >= $maxURLsPerUser)
 60	{
 61		$noMoreURLs = true;
 62	}
 63	mysql_free_result($result);
 64}
 65
 66if (!$noMoreURLs && array_key_exists('url', $_REQUEST)) {
 67	$url_id = getUrlId(resolveRedirects($_REQUEST['url']), false);
 68
 69	if (is_null($url_id)) {
 70		header('Location: '.$showslow_base.'my.php#invalid');
 71		exit;
 72	}
 73
 74	$query = sprintf("INSERT IGNORE INTO user_urls (user_id, url_id) VALUES (%d, %d)",
 75		$current_user->getID(),
 76		$url_id
 77	);
 78
 79	$result = mysql_query($query);
 80
 81	if (!$result) {
 82		error_log(mysql_error());
 83	}
 84
 85	$current_user->recordActivity(SHOWSLOW_ACTIVITY_ADD_URL);
 86
 87	header('Location: '.$showslow_base.'my.php#added');
 88	exit;
 89}
 90
 91$query = sprintf("SELECT urls.id as id, url, last_update,
 92		yslow2.o as o,
 93		pagespeed.o as ps_o,
 94		dynatrace.rank as dt_o
 95	FROM urls INNER JOIN user_urls ON urls.id = user_urls.url_id
 96		LEFT JOIN yslow2 ON urls.yslow2_last_id = yslow2.id
 97		LEFT JOIN pagespeed ON urls.pagespeed_last_id = pagespeed.id
 98		LEFT JOIN dynatrace ON urls.dynatrace_last_id = dynatrace.id
 99	WHERE user_urls.user_id = %d ORDER BY url", $current_user->getID());
100
101$result = mysql_query($query);
102
103if (!$result) {
104	error_log(mysql_error());
105}
106
107$yslow = false;
108$pagespeed = false;
109$dynatrace = false;
110
111$rows = array();
112$cols = 0;
113while ($row = mysql_fetch_assoc($result)) {
114	$rows[] = $row;
115
116	if (!$yslow && !is_null($row['o'])) {
117		$yslow = true;
118		$cols += 1;
119	}
120	if (!$pagespeed && !is_null($row['ps_o'])) {
121		$pagespeed = true;
122		$cols += 1;
123	}
124	if (!$dynatrace && !is_null($row['dt_o'])) {
125		$dynatrace = true;
126		$cols += 1;
127	}
128}
129
130$TITLE = 'My URLs';
131$SECTION = 'my';
132require_once(dirname(__FILE__).'/header.php');
133?>
134<style>
135td, th { white-space: nowrap; }
136
137.score {
138	text-align: right;
139	padding: 0 10px 0 10px;
140}
141
142.url {
143	padding-left:10px;
144}
145</style>
146<h1 style="margin-bottom: 0">Add URLs to monitor</h1>
147<div style="font-size: small; margin-bottom: 1em">User: <a href="users/edit.php"><?php echo $current_user->getName(); ?></a></div>
148
149<p>If you don't want to <a href="<?php echo $showslow_base; ?>configure.php">run YSlow, Page Speed and dynaTrace on your desktop</a>, you can add a URL to the list below and it'll be measured automatically every <?php echo $monitoringPeriod ?> hours.</p>
150
151<form action="" method="POST">
152<?php
153if ($noMoreURLs)
154{
155?>
156<p><?php echo $maxURLsMessage; ?></p>
157<div title="<?php echo htmlentities(strip_tags($maxURLsMessage)); ?>">Add URL: <input type="text" size="80" name="url" disabled="disabled"/><input type="submit" name="add" value="add" disabled="disabled"/></div>
158<?php } else { ?>
159Add URL: <input type="text" size="80" name="url"/><input type="submit" name="add" value="add" title="add URL to be measured"/>
160<?php
161}
162
163if (count($rows))
164{
165?>
166<div style="width: 100%; overflow: hidden">
167	<table border="0" style="margin-top: 1em">
168	<tr style="font-size: smaller; font-weight: bold">
169	<td style="text-align: left; padding-right: 0.7em">Timestamp</td>
170	<?php if ($yslow) { ?><th colspan="2">YSlow grade</th><?php } ?>
171	<?php if ($pagespeed) { ?><th colspan="2">Page Speed score</th><?php } ?>
172	<?php if ($dynatrace) { ?><th colspan="2">dynaTrace rank</th><?php } ?>
173	<td style="text-align: center">Remove</td>
174	<td style="padding-left: 1em">URL</td>
175	</tr>
176
177	<?php
178	foreach ($rows as $row) {
179		$link = true;
180	?><tr>
181		<?php if (shouldBeIgnoredAsNonHTTP($row['url'])) {
182			$link = false;
183		?>
184			<td style="color: red; text-align: right; padding-right: 1em"><i title="This instance of Show Slow only allows HTTP(S) URLs">non-HTTP(s) URL</i></td>
185			<td colspan="<?php echo $cols*2 ?>"/>
186		<?php } else if (!isURLAllowed($row['url'])) {
187			$link = false;
188		?>
189			<td style="color: red; text-align: right; padding-right: 1em"><i title="URL is not allowed to be reported to this instance of Show Slow">not allowed</i></td>
190			<td colspan="<?php echo $cols*2 ?>"/>
191		<?php } else if (isURLIgnored($row['url'])) {
192			$link = false;
193		?>
194			<td style="color: red; text-align: right; padding-right: 1em"><i title="This URL is ignored by this instance of Show Slow">ignored</i></td>
195			<td colspan="<?php echo $cols*2 ?>"/>
196		<?php } else if (!is_null($row['o']) || !is_null($row['ps_o']) || !is_null($row['dt_o'])) { ?>
197			<td style="text-align: right; padding-right: 1em"><a title="Time of last check for this URL" href="details/?url=<?php echo urlencode($row['url']); ?>"><?php echo htmlentities($row['last_update']); ?></a></td>
198			<?php if (!$yslow) {?>
199			<?php } else if (!is_null($row['o'])) {?>
200				<td class="score" title="Current YSlow grade: <?php echo prettyScore($row['o'])?> (<?php echo $row['o']?>)"><?php echo prettyScore($row['o'])?> (<?php echo $row['o']?>)</td>
201				<td title="Current YSlow grade: <?php echo prettyScore($row['o'])?> (<?php echo $row['o']?>)"><div class="gbox"><div style="width: <?php echo $row['o']+1?>px" class="bar c<?php echo scoreColorStep($row['o'])?>"/></div></td>
202			<?php } else { ?>
203				<td class="score" style="color: silver" title="No data collected">no data</td>
204				<td><div class="gbox" title="No data collected"><div class="bar"/></div></td>
205			<?php } ?>
206
207			<?php if (!$pagespeed) {?>
208			<?php } else if (!is_null($row['ps_o'])) {?>
209				<td class="score" title="Current Page Speed score: <?php echo prettyScore($row['ps_o'])?> (<?php echo $row['ps_o']?>)"><?php echo prettyScore($row['ps_o'])?> (<?php echo $row['ps_o']?>)</td>
210				<td title="Current Page Speed score: <?php echo prettyScore($row['ps_o'])?> (<?php echo $row['ps_o']?>)"><div class="gbox"><div style="width: <?php echo $row['ps_o']+1?>px" class="bar c<?php echo scoreColorStep($row['ps_o'])?>"/></div></td>
211			<?php } else { ?>
212				<td class="score" style="color: silver" title="No data collected">no data</td>
213				<td><div class="gbox" title="No data collected"><div class="bar"/></div></td>
214			<?php } ?>
215
216			<?php if (!$dynatrace) {?>
217			<?php } else if (!is_null($row['dt_o'])) {?>
218				<td class="score" title="Current dynaTrace score: <?php echo prettyScore($row['dt_o'])?> (<?php echo $row['dt_o']?>)"><?php echo prettyScore($row['dt_o'])?> (<?php echo $row['dt_o']?>)</td>
219				<td title="Current dynaTrace score: <?php echo prettyScore($row['dt_o'])?> (<?php echo $row['dt_o']?>)"><div class="gbox"><div style="width: <?php echo $row['dt_o']+1?>px" class="bar c<?php echo scoreColorStep($row['dt_o'])?>"/></div></td>
220			<?php }else{?>
221				<td class="score" style="color: silver" title="No data collected">no data</td>
222				<td><div class="gbox" title="No data collected"><div class="bar"/></div></td>
223			<?php } ?>
224		<?php } else { ?>
225			<td style="text-align: right; padding-right: 1em" title="Data for this URL is being collected"><i>collecting data</i></td>
226			<?php for($i=0; $i<$cols; $i++) {?>
227			<td class="score" style="color: silver" title="Collecting data"><img style="vertical-align: text-bottom" src="<?php echo assetURL('clock.png')?>"/></td>
228			<td title="Collecting data"><div class="gbox"><div class="bar ccol"/></div></td>
229			<?php } ?>
230		<?php } ?>
231		<td style="text-align: center"><input type="submit" name="delete[<?php echo htmlentities($row['id'])?>]" value="X" style="font-size: xx-small" title="Stop monitoring this URL" onclick="return confirm('Are you sure you want to remove this URL?')"/></td>
232
233		<?php if ($link) {?>
234		<td style="padding-left: 1em; overflow: hidden; white-space: nowrap;"><a href="details/?url=<?php echo urlencode($row['url'])?>"><?php echo htmlentities(substr($row['url'], 0, 100))?><?php if (strlen($row['url']) > 100) { ?>...<?php } ?></a></td>
235		<?php } else { ?>
236		<td style="padding-left: 1em; overflow: hidden; white-space: nowrap;"><i title="Time of last check for this URL"><?php echo htmlentities(substr($row['url'], 0, 100))?><?php if (strlen($row['url']) > 100) { ?>...<?php } ?></i></td>
237		<?php } ?>
238	</tr><?php
239	}
240
241	mysql_free_result($result);
242	?>
243	</table>
244	</div>
245<?php 
246}
247?>
248</form>
249<?php
250require_once(dirname(__FILE__).'/footer.php');