PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/includes/functions.php

https://bitbucket.org/mattyt/status-dashboard
PHP | 125 lines | 87 code | 15 blank | 23 comment | 10 complexity | 0ca53a562ace4d918e0141fab0dd6f6d MD5 | raw file
  1. <?php
  2. ///////////////////////////////////////
  3. // options //
  4. ///////////////////////////////////////
  5. function get_option($key) {
  6. $get_option_query = "SELECT value FROM options WHERE key = '$key'";
  7. $get_option_result = mysql_query($get_option_query);
  8. $get_option_array = mysql_fetch_array($get_option_result);
  9. $option = $get_option_array['value'];
  10. return $option;
  11. }
  12. function set_option($key, $value) {
  13. // check if option already exsists
  14. $check_option_query = mysql_query("SELECT value FROM options WHERE key = '$key'");
  15. $check_option_result = mysql_num_rows($check_option_query);
  16. if($check_option_result == 1) {
  17. // if option exsists update value
  18. $set_option_query = "UPDATE options SET value = '$value' WHERE key = '$key'";
  19. mysql_query($set_option_query);
  20. } else {
  21. // else create option
  22. $create_option_query = "INSERT INTO options (key, value) VALUES ('$key', '$value')";
  23. mysql_query($create_option_query);
  24. }
  25. }
  26. function get_past_dates($from = date("d/m/Y"), $number) {
  27. $count = 1
  28. $result = array();
  29. while($count <= $number) {
  30. // takes date and subtacks 1 day then adds result to the end of an array
  31. array_push($result, date($from, strtotime("-1 day")));
  32. $count++
  33. }
  34. return $result;
  35. }
  36. ///////////////////////////////////////
  37. // servers //
  38. ///////////////////////////////////////
  39. function get_servers() {
  40. $get_servers_query = "SELECT * FROM servers";
  41. $get_servers_result = mysql_query($get_servers_query);
  42. return $get_servers_result;
  43. }
  44. ///////////////////////////////////////
  45. // status //
  46. ///////////////////////////////////////
  47. function get_current_status($server_id) {
  48. $get_current_status_query = "SELECT * FROM status WHERE server_id = '$server_id' ORDER BY time, date DESC";
  49. $get_current_status_result = mysql_query($get_current_status_query);
  50. $num_rows = mysql_num_rows($get_current_status_result);
  51. if($num_rows != 0) {
  52. // if a status is set return status id, status (number), icon and name to detail page
  53. $get_current_status_array = mysql_fetch_array($get_current_status_result);
  54. $id = $get_current_status_array['id'];
  55. $status = $get_current_status_array['status'];
  56. // get name and icon for status
  57. $get_status_type_query = "SELECT * FROM status_type WHERE id = '$status'";
  58. $get_status_type_result = mysql_query($get_status_type_query);
  59. $get_status_type_array = mysql_fetch_array($get_status_type_result);
  60. $icon = $get_status_type_array['icon'];
  61. $name = $get_status_type_array['name'];
  62. $result = array('id' => $id, 'status' => $status, 'icon' => $icon, 'name' => $name);
  63. } else {
  64. // if no status is returned assume server is normal
  65. $status = get_option('default_status');
  66. // get name and icon for status
  67. $get_status_type_query = "SELECT * FROM status_type WHERE id = '$status'";
  68. $get_status_type_result = mysql_query($get_status_type_query);
  69. $get_status_type_array = mysql_fetch_array($get_status_type_result);
  70. $icon = $get_status_type_array['icon'];
  71. $name = $get_status_type_array['name'];
  72. $result = array('id' => "0", 'status' => $status, 'icon' => $icon, 'name' => $name);
  73. }
  74. return $result;
  75. }
  76. function get_prev_status_overview($server_id, $date) {
  77. // get defualt status for comparison
  78. $defualt_status = get_option('default_status');
  79. // get all statuses for server on date
  80. $get_prev_status_query = "SELECT * FROM status WHERE server_id = '$server_id' AND date = '$date' ORDER BY time ASC";
  81. $get_prev_status_result = mysql_query($get_prev_status_query);
  82. $num_rows = mysql_num_rows($get_prev_status_result);
  83. if($num_rows != 0) {
  84. // if status are returned return last on default
  85. while($row = mysql_fetch_array($get_prev_status_result)) {
  86. if($row['status'] > $defualt_status) {
  87. $status = $row['status'];
  88. $id = $row['id'];
  89. }
  90. }
  91. // get name and icon for status
  92. $get_status_type_query = "SELECT * FROM status_type WHERE id = '$status'";
  93. $get_status_type_result = mysql_query($get_status_type_query);
  94. $get_status_type_array = mysql_fetch_array($get_status_type_result);
  95. $icon = $get_status_type_array['icon'];
  96. $name = $get_status_type_array['name'];
  97. $result = array('id' => $id, 'status' => $status, 'icon' => $icon, 'name' => $name);
  98. } else {
  99. // if no status are retuned assume default status
  100. // get name and icon for status
  101. $get_status_type_query = "SELECT * FROM status_type WHERE id = '$status'";
  102. $get_status_type_result = mysql_query($get_status_type_query);
  103. $get_status_type_array = mysql_fetch_array($get_status_type_result);
  104. $icon = $get_status_type_array['icon'];
  105. $name = $get_status_type_array['name'];
  106. $result = array('id' => "0", 'status' => $defualt_status, 'icon' => $icon, 'name' => $name);
  107. }
  108. return $result;
  109. }
  110. ?>