PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/my.php

https://github.com/rakshithkr/showslow
PHP | 264 lines | 217 code | 46 blank | 1 comment | 64 complexity | 376008e6c693e7df2431888ad87b058e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. require_once(dirname(__FILE__).'/global.php');
  3. require_once(dirname(__FILE__).'/users/users.php');
  4. $current_user = User::require_login();
  5. if (array_key_exists('delete', $_POST) && is_array($_POST['delete'])) {
  6. $delete = array_keys($_POST['delete']);
  7. $first = true;
  8. $deleteids = '';
  9. foreach ($delete as $id) {
  10. if (!is_numeric($id)) {
  11. next;
  12. }
  13. if ($first) {
  14. $first = false;
  15. }
  16. else
  17. {
  18. $deleteids.=', ';
  19. }
  20. $deleteids.=$id;
  21. }
  22. if (!$first && $deleteids != '') {
  23. $query = sprintf("DELETE FROM user_urls WHERE user_id = %d AND url_id IN (%s)",
  24. $current_user->getID(),
  25. $deleteids
  26. );
  27. $result = mysql_query($query);
  28. if (!$result) {
  29. error_log(mysql_error());
  30. }
  31. }
  32. header('Location: '.$showslow_base.'my.php#deleted');
  33. exit;
  34. }
  35. if (in_array($current_user->getID(), $noMaxURLsForUsers)) {
  36. $maxURLsPerUser = false;
  37. }
  38. $noMoreURLs = false;
  39. if ($maxURLsPerUser)
  40. {
  41. $query = sprintf('SELECT count(*) AS cnt FROM user_urls WHERE user_urls.user_id = %d', $current_user->getID());
  42. $result = mysql_query($query);
  43. if (!$result) {
  44. error_log(mysql_error());
  45. }
  46. $cnt = mysql_fetch_row($result);
  47. if (is_array($cnt) && $cnt[0] >= $maxURLsPerUser)
  48. {
  49. $noMoreURLs = true;
  50. $MESSAGES[] = $maxURLsMessage;
  51. }
  52. mysql_free_result($result);
  53. }
  54. if (!$noMoreURLs && array_key_exists('url', $_REQUEST)) {
  55. $url_id = getUrlId(resolveRedirects($_REQUEST['url']), false);
  56. if (is_null($url_id)) {
  57. header('Location: '.$showslow_base.'my.php#invalid');
  58. exit;
  59. }
  60. $query = sprintf("INSERT IGNORE INTO user_urls (user_id, url_id) VALUES (%d, %d)",
  61. $current_user->getID(),
  62. $url_id
  63. );
  64. $result = mysql_query($query);
  65. if (!$result) {
  66. error_log(mysql_error());
  67. }
  68. $current_user->recordActivity(SHOWSLOW_ACTIVITY_ADD_URL);
  69. if (is_callable($onNewMonitoredURL)) {
  70. // only call when URL was never monitored
  71. $query = "SELECT DISTINCT url FROM urls INNER JOIN user_urls on user_urls.url_id = urls.id
  72. WHERE urls.id = %d AND DATE_ADD(added, INTERVAL %d HOUR) > NOW()";
  73. foreach ($all_metrics as $provider_name => $provider) {
  74. $query .= " AND ".$provider['table'].'_last_id IS NULL';
  75. }
  76. $query .= ' LIMIT 0, 1';
  77. $query = sprintf($query, $url_id, $monitoringPeriod);
  78. $result = mysql_query($query);
  79. if (!$result) {
  80. error_log(mysql_error());
  81. }
  82. if ($row = mysql_fetch_assoc($result)) {
  83. $url = $row['url'];
  84. call_user_func($onNewMonitoredURL, $url, $current_user);
  85. }
  86. }
  87. header('Location: '.$showslow_base.'my.php#added');
  88. exit;
  89. }
  90. $query = sprintf("SELECT urls.id as id, url, last_update,
  91. yslow2.o as o,
  92. pagespeed.o as ps_o,
  93. dynatrace.rank as dt_o
  94. FROM urls INNER JOIN user_urls ON urls.id = user_urls.url_id
  95. LEFT JOIN yslow2 ON urls.yslow2_last_id = yslow2.id
  96. LEFT JOIN pagespeed ON urls.pagespeed_last_id = pagespeed.id
  97. LEFT JOIN dynatrace ON urls.dynatrace_last_id = dynatrace.id
  98. WHERE user_urls.user_id = %d ORDER BY url", $current_user->getID());
  99. $result = mysql_query($query);
  100. if (!$result) {
  101. error_log(mysql_error());
  102. }
  103. $yslow = false;
  104. $pagespeed = false;
  105. $dynatrace = false;
  106. $rows = array();
  107. $cols = 0;
  108. while ($row = mysql_fetch_assoc($result)) {
  109. $rows[] = $row;
  110. if ($enabledMetrics['yslow'] && !$yslow && !is_null($row['o'])) {
  111. $yslow = true;
  112. $cols += 1;
  113. }
  114. if ($enabledMetrics['pagespeed'] && !$pagespeed && !is_null($row['ps_o'])) {
  115. $pagespeed = true;
  116. $cols += 1;
  117. }
  118. if ($enabledMetrics['dynatrace'] && !$dynatrace && !is_null($row['dt_o'])) {
  119. $dynatrace = true;
  120. $cols += 1;
  121. }
  122. }
  123. $TITLE = 'My URLs';
  124. $SECTION = 'my';
  125. require_once(dirname(__FILE__).'/header.php');
  126. ?>
  127. <style>
  128. td, th { white-space: nowrap; }
  129. .score {
  130. text-align: right;
  131. padding: 0 10px 0 10px;
  132. }
  133. .url {
  134. padding-left:10px;
  135. }
  136. </style>
  137. <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>
  138. <?php
  139. if (count($rows))
  140. {
  141. ?>
  142. <form action="" method="POST" style="width: 100%; overflow: hidden">
  143. <table border="0" style="margin-top: 1em">
  144. <tr style="font-size: smaller; font-weight: bold">
  145. <td style="text-align: left; padding-right: 0.7em">Timestamp</td>
  146. <?php if ($yslow) { ?><th colspan="2">YSlow grade</th><?php } ?>
  147. <?php if ($pagespeed) { ?><th colspan="2">Page Speed score</th><?php } ?>
  148. <?php if ($dynatrace) { ?><th colspan="2">dynaTrace rank</th><?php } ?>
  149. <td style="text-align: center">Remove</td>
  150. <td style="padding-left: 1em">URL</td>
  151. </tr>
  152. <?php
  153. foreach ($rows as $row) {
  154. $link = true;
  155. ?><tr>
  156. <?php if (shouldBeIgnoredAsNonHTTP($row['url'])) {
  157. $link = false;
  158. ?>
  159. <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>
  160. <td colspan="<?php echo $cols*2 ?>"/>
  161. <?php } else if (!isURLAllowed($row['url'])) {
  162. $link = false;
  163. ?>
  164. <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>
  165. <td colspan="<?php echo $cols*2 ?>"/>
  166. <?php } else if (isURLIgnored($row['url'])) {
  167. $link = false;
  168. ?>
  169. <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>
  170. <td colspan="<?php echo $cols*2 ?>"/>
  171. <?php } else if (!is_null($row['o']) || !is_null($row['ps_o']) || !is_null($row['dt_o'])) { ?>
  172. <td style="text-align: right; padding-right: 1em"><a title="Time of last check for this URL" href="<?php echo detailsUrl($row['id'], $row['url'])?>"><?php echo htmlentities($row['last_update']); ?></a></td>
  173. <?php if (!$yslow) {?>
  174. <?php } else if (!is_null($row['o'])) {?>
  175. <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>
  176. <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>
  177. <?php } else { ?>
  178. <td class="score" style="color: silver" title="No data collected">no data</td>
  179. <td><div class="gbox" title="No data collected"><div class="bar"/></div></td>
  180. <?php } ?>
  181. <?php if (!$pagespeed) {?>
  182. <?php } else if (!is_null($row['ps_o'])) {?>
  183. <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>
  184. <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>
  185. <?php } else { ?>
  186. <td class="score" style="color: silver" title="No data collected">no data</td>
  187. <td><div class="gbox" title="No data collected"><div class="bar"/></div></td>
  188. <?php } ?>
  189. <?php if (!$dynatrace) {?>
  190. <?php } else if (!is_null($row['dt_o'])) {?>
  191. <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>
  192. <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>
  193. <?php }else{?>
  194. <td class="score" style="color: silver" title="No data collected">no data</td>
  195. <td><div class="gbox" title="No data collected"><div class="bar"/></div></td>
  196. <?php } ?>
  197. <?php } else { ?>
  198. <td style="text-align: right; padding-right: 1em" title="Data for this URL is being collected"><i>collecting data</i></td>
  199. <?php for($i=0; $i<$cols; $i++) {?>
  200. <td class="score" style="color: silver" title="Collecting data"><img style="vertical-align: text-bottom" src="<?php echo assetURL('clock.png')?>"/></td>
  201. <td title="Collecting data"><div class="gbox"><div class="bar ccol"/></div></td>
  202. <?php } ?>
  203. <?php } ?>
  204. <td style="text-align: center"><input class="btn btn-mini" type="submit" name="delete[<?php echo htmlentities($row['id'])?>]" value="×" style="font-size: xx-small" title="Stop monitoring this URL" onclick="return confirm('Are you sure you want to remove this URL?')"/></td>
  205. <?php if ($link) {?>
  206. <td style="padding-left: 1em; overflow: hidden; white-space: nowrap;"><a href="<?php echo detailsUrl($row['id'], $row['url'])?>"><?php echo htmlentities(substr($row['url'], 0, 100))?><?php if (strlen($row['url']) > 100) { ?>...<?php } ?></a></td>
  207. <?php } else { ?>
  208. <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>
  209. <?php } ?>
  210. </tr><?php
  211. }
  212. mysql_free_result($result);
  213. ?>
  214. </table>
  215. </form>
  216. <?php
  217. }
  218. require_once(dirname(__FILE__).'/footer.php');