Completed
Push — development ( 86ad30...7b6aa4 )
by Sebastian
05:00
created

include/classes/monitoring.class.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
3
4
class Monitoring extends Base {
5
   protected $table = 'monitoring';
6
7
  /**
8
   * Store Uptime Robot status information as JSON in settings table
9
   * @param none
10
   * @return bool true on success, false on error
11
   **/
12
  public function storeUptimeRobotStatus() {
13
    if ($api_keys = $this->setting->getValue('monitoring_uptimerobot_api_keys')) {
14
      $aJSONData = array();
15
      $url = 'https://api.uptimerobot.com';
16
      $aMonitors = explode(',', $api_keys);
17
      foreach ($aMonitors as $aData) {
18
        $temp = explode('|', $aData);
19
        $aMonitor['api_key'] = trim($temp[0]);
20
        $aMonitor['monitor_id'] = trim($temp[1]);
21
        $target = '/getMonitors?apiKey=' . $aMonitor['api_key'] . '&monitors=' . $aMonitor['monitor_id'] . '&format=json&noJsonCallback=1&customUptimeRatio=1-7-30&logs=1';
22
        $aMonitorStatus = $this->tools->getApi($url, $target);
23
        if (!$aMonitorStatus || @$aMonitorStatus['stat'] == 'fail') {
24
          if (is_array($aMonitorStatus) && array_key_exists('message', @$aMonitorStatus)) {
25
            $this->setErrorMessage($this->getErrorMsg('E0032', $aMonitorStatus['message']));
26
          } else {
27
            $this->setErrorMessage($this->getErrorMsg('E0032', $this->tools->getError()));
28
          }
29
          return false;
30
        }
31
        $aMonitorStatus['monitors']['monitor'][0]['customuptimeratio'] = explode('-', $aMonitorStatus['monitors']['monitor'][0]['customuptimeratio']);
32
        $aAllMonitorsStatus[] = $aMonitorStatus['monitors']['monitor'][0];
33
      }
34
      if (!$this->setting->setValue('monitoring_uptimerobot_status', json_encode($aAllMonitorsStatus)) || !$this->setting->setValue('monitoring_uptimerobot_lastcheck', time())) {
35
        $this->setErrorMessage($this->getErrorMsg('E0033'), $setting->getError());
36
        return false;
37
      }
38
    }
39
    return true;
40
  }
41
42
  /**
43
   * Fetch Uptime Robot Status from settings table
44
   * @param none
45
   * @return array Data on success, false on failure
46
   **/
47
  public function getUptimeRobotStatus() {
48
    if ($json = $this->setting->getValue('monitoring_uptimerobot_status'))
49
      return json_decode($json, true);
50
    return false;
51
  }
52
53
  /**
54
   * Check that our cron is currently activated
55
   * @param name string Cronjob name
56
   * @return bool true or false
57
   **/
58
  public function isDisabled($name) {
59
    $aStatus = $this->getStatus($name . '_disabled');
60
    return $aStatus['value'];
61
  }
62
63
  /**
64
   * Fetch a value from our table
65
   * @param name string Setting name
66
   * @return value string Value
67
   **/
68
  public function getStatus($name) {
69
    $query = $this->mysqli->prepare("SELECT * FROM $this->table WHERE name = ? LIMIT 1");
70
    if ($query && $query->bind_param('s', $name) && $query->execute() && $result = $query->get_result()) {
71
      return $result->fetch_assoc();
72
    } else {
73
      return $this->sqlError();
74
    }
75
  }
76
77
  /**
78
   * Insert or update a setting
79
   * @param name string Name of the variable
80
   * @param value string Variable value
81
   * @return bool
82
   **/
83 View Code Duplication
  public function setStatus($name, $type, $value) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
    $stmt = $this->mysqli->prepare("
85
      INSERT INTO $this->table (name, type, value)
86
      VALUES (?, ?, ?)
87
      ON DUPLICATE KEY UPDATE value = ?
88
      ");
89
    if ($stmt && $stmt->bind_param('ssss', $name, $type, $value, $value) && $stmt->execute())
90
      return true;
91
    $this->debug->append("Failed to set $name to $value");
92
    return false;
93
  }
94
95
  /**
96
   * Start a cronjob, mark various fields properly
97
   * @param cron_name string Cronjob name
98
   **/
99
  public function startCronjob($cron_name) {
100
    $aStatus = $this->getStatus($cron_name . '_active');
101
    if ($aStatus['value'] == 1) {
102
      $this->setErrorMessage('Cron is already active in database: ' . $cron_name . '_active is 1, please force run with -f once ensured it\' not running');
103
      return false;
104
    }
105
    $this->setStatus($cron_name . "_active", "yesno", 1);
106
    $this->setStatus($cron_name . '_starttime', 'date', time());
107
    return true;
108
  }
109
110
  /**
111
   * End cronjob with an error message
112
   * @param cron_name string Cronjob Name
113
   * @param msgCode string Message code as stored in error_codes array
114
   * @param exitCode int Exit code to pass on to exit function and monitor report
115
   * @param fatal boolean Should we exit out entirely
116
   * @return none
117
   **/
118
  public function endCronjob($cron_name, $msgCode, $exitCode=0, $fatal=false, $mail=true) {
119
    $this->setStatus($cron_name . "_active", "yesno", 0);
120
    $this->setStatus($cron_name . "_message", "message", $this->getErrorMsg($msgCode));
121
    $this->setStatus($cron_name . "_status", "okerror", $exitCode);
122
    $this->setStatus($cron_name . "_endtime", "date", time());
123
    if ($mail) {
124
      $aMailData = array(
125
        'email' => $this->setting->getValue('system_error_email'),
126
        'subject' => 'Cronjob Failure',
127
        'Error Code' => $msgCode,
128
        'Error Message' => $this->getErrorMsg($msgCode)
129
      );
130
      if (!$this->mail->sendMail('notifications/error', $aMailData))
131
        $this->setErrorMessage('Failed to send mail notification');
132
    }
133
    if ($fatal) {
134
      if ($exitCode == 1) $this->setStatus($cron_name . "_disabled", "yesno", 1);
135
      exit($exitCode);
136
    }
137
  }
138
}
139
140
$monitoring = new Monitoring();
141
$monitoring->setErrorCodes($aErrorCodes);
142
$monitoring->setConfig($config);
143
$monitoring->setDebug($debug);
144
$monitoring->setMail($mail);
145
$monitoring->setMysql($mysqli);
146
$monitoring->setSetting($setting);
147