| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2020-11-05 16:54:04 +03:00
										 |  |  | require_once(__DIR__ . "/notification.php"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |  * Class for creating and rendering an incident | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-04-13 21:31:03 +02:00
										 |  |  | class Incident implements JsonSerializable | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  | { | 
					
						
							|  |  |  |   private $id; | 
					
						
							|  |  |  |   private $date; | 
					
						
							|  |  |  |   private $end_date; | 
					
						
							| 
									
										
										
										
											2018-04-13 22:38:10 +02:00
										 |  |  |   private $timestamp; | 
					
						
							|  |  |  |   private $end_timestamp; | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |   private $text; | 
					
						
							|  |  |  |   private $type; | 
					
						
							|  |  |  |   private $title; | 
					
						
							|  |  |  |   private $username; | 
					
						
							| 
									
										
										
										
											2018-11-21 10:50:21 +01:00
										 |  |  |   private $service_id; | 
					
						
							|  |  |  |   private $service_name; | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 20:39:10 +01:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Constructs service from its data. | 
					
						
							|  |  |  |    * @param array $data incident data | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |   function __construct($data) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     //TODO: Maybe get data from id?
 | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |     $this->id = $data['status_id']; | 
					
						
							| 
									
										
										
										
											2018-04-13 22:38:10 +02:00
										 |  |  |     $this->timestamp = $data['time']; | 
					
						
							|  |  |  |     $this->end_timestamp = $data['end_time']; | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     $this->date = new DateTime("@" . $data['time']); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |     $this->date = $this->date->format('Y-m-d H:i:sP'); | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     if ($data['end_time'] > 0) { | 
					
						
							|  |  |  |       $this->end_date = new DateTime("@" . $data['end_time']); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |       $this->end_date = $this->end_date->format('Y-m-d H:i:sP'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     $this->type = $data['type']; | 
					
						
							|  |  |  |     $this->title = $data['title']; | 
					
						
							|  |  |  |     $this->text = $data['text']; | 
					
						
							|  |  |  |     $this->username = $data['username']; | 
					
						
							| 
									
										
										
										
											2018-11-21 10:50:21 +01:00
										 |  |  |     $this->service_id = $data['service_id']; | 
					
						
							|  |  |  |     $this->service_name = $data['service_name']; | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 20:39:10 +01:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Deletes incident by ID. | 
					
						
							|  |  |  |    * @param int ID | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |   public static function delete($id) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2018-01-13 00:16:38 +01:00
										 |  |  |     global $mysqli, $message, $user; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     if ($user->get_rank() > 1) { | 
					
						
							| 
									
										
										
										
											2018-01-13 00:16:38 +01:00
										 |  |  |       $stmt = $mysqli->prepare("SELECT count(*) as count FROM status WHERE id= ? AND user_id = ?"); | 
					
						
							|  |  |  |       $stmt->bind_param("ii", $id, $_SESSION['user']); | 
					
						
							|  |  |  |       $stmt->execute(); | 
					
						
							|  |  |  |       $query = $stmt->get_result(); | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |       if (!$query->fetch_assoc()['count']) { | 
					
						
							| 
									
										
										
										
											2018-01-13 00:16:38 +01:00
										 |  |  |         $message = _("You don't have permission to do that!"); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $stmt = $mysqli->prepare("DELETE FROM services_status WHERE status_id = ?"); | 
					
						
							|  |  |  |     $stmt->bind_param("i", $id); | 
					
						
							|  |  |  |     $stmt->execute(); | 
					
						
							|  |  |  |     $query = $stmt->get_result(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $stmt = $mysqli->prepare("DELETE FROM status WHERE id= ?"); | 
					
						
							|  |  |  |     $stmt->bind_param("i", $id); | 
					
						
							|  |  |  |     $stmt->execute(); | 
					
						
							|  |  |  |     $query = $stmt->get_result(); | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     header("Location: " . WEB_URL . "/admin"); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-07 20:39:10 +01:00
										 |  |  |   /** | 
					
						
							| 
									
										
										
										
											2020-11-05 16:54:04 +03:00
										 |  |  |    * Processes submitted form and adds incident unless problem is encountered, | 
					
						
							| 
									
										
										
										
											2018-01-18 22:59:34 +01:00
										 |  |  |    * calling this is possible only for admin or higher rank. Also checks requirements | 
					
						
							| 
									
										
										
										
											2018-01-07 20:39:10 +01:00
										 |  |  |    * for char limits. | 
					
						
							|  |  |  |    * @return void | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |   public static function add() | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     global $mysqli, $message; | 
					
						
							| 
									
										
										
										
											2020-11-05 16:54:04 +03:00
										 |  |  |     //Sould be a better way to get this array...
 | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     $statuses = array(_("Major outage"), _("Minor outage"), _("Planned maintenance"), _("Operational")); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:54:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |     $user_id = $_SESSION['user']; | 
					
						
							|  |  |  |     $type = $_POST['type']; | 
					
						
							| 
									
										
										
										
											2020-06-03 09:13:54 +02:00
										 |  |  |     $title = strip_tags($_POST['title']); | 
					
						
							|  |  |  |     $text = strip_tags($_POST['text'], '<br>'); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     if (strlen($title) == 0) { | 
					
						
							| 
									
										
										
										
											2017-12-31 00:41:58 +01:00
										 |  |  |       $message = _("Please enter title"); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |       return; | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     } else if (strlen($title) > 50) { | 
					
						
							| 
									
										
										
										
											2017-12-31 00:41:58 +01:00
										 |  |  |       $message = _("Title too long! Character limit is 50"); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     if (strlen($title) == 0) { | 
					
						
							| 
									
										
										
										
											2017-12-31 00:41:58 +01:00
										 |  |  |       $message = _("Please enter text"); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     if ($type == 2 && (!strlen(trim($_POST['time'])) || !strlen(trim($_POST['end_time'])))) { | 
					
						
							| 
									
										
										
										
											2017-12-31 00:41:58 +01:00
										 |  |  |       $message = _("Please set start and end time! Use ISO 8601 format."); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     if (empty($_POST['services'])) { | 
					
						
							| 
									
										
										
										
											2017-12-31 00:41:58 +01:00
										 |  |  |       $message = _("Please select at least one service"); | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     } else { | 
					
						
							|  |  |  |       if (!is_array($_POST['services'])) { | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |         $services = array($_POST['services']); | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |         $services = $_POST['services']; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |       if (!empty($_POST['time']) && $type == 2) { | 
					
						
							|  |  |  |         $input_time = (!empty($_POST['time_js']) ? $_POST['time_js'] : $_POST['time']); | 
					
						
							|  |  |  |         $input_end_time = (!empty($_POST['end_time_js']) ? $_POST['end_time_js'] : $_POST['end_time']); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:54:04 +03:00
										 |  |  |         $time = strtotime($input_time); | 
					
						
							| 
									
										
										
										
											2018-04-13 23:41:05 +02:00
										 |  |  |         $end_time = strtotime($input_end_time); | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |         if (!$time) { | 
					
						
							| 
									
										
										
										
											2017-12-31 00:41:58 +01:00
										 |  |  |           $message = _("Start date format is not recognized. Please use ISO 8601 format."); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |         if (!$end_time) { | 
					
						
							| 
									
										
										
										
											2017-12-31 00:41:58 +01:00
										 |  |  |           $message = _("End date format is not recognized. Please use ISO 8601 format."); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-23 20:37:45 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |         if ($time >= $end_time) { | 
					
						
							| 
									
										
										
										
											2018-08-23 20:37:45 +02:00
										 |  |  |           $message = _("End time is either the same or earlier than start time!"); | 
					
						
							|  |  |  |           return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |         $time = time(); | 
					
						
							|  |  |  |         $end_time = ''; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-11-05 16:54:04 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 15:01:16 +01:00
										 |  |  |       $stmt = $mysqli->prepare("INSERT INTO status VALUES (NULL,?, ?, ?, ?, ?, ?)"); | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |       $stmt->bind_param("issiii", $type, $title, $text, $time, $end_time, $user_id); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |       $stmt->execute(); | 
					
						
							|  |  |  |       $query = $stmt->get_result(); | 
					
						
							|  |  |  |       $status_id = $mysqli->insert_id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       foreach ($services as $service) { | 
					
						
							| 
									
										
										
										
											2020-11-05 16:54:04 +03:00
										 |  |  |         $stmt = $mysqli->prepare("INSERT INTO services_status VALUES (NULL,?, ?)"); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |         $stmt->bind_param("ii", $service, $status_id); | 
					
						
							|  |  |  |         $stmt->execute(); | 
					
						
							|  |  |  |         $query = $stmt->get_result(); | 
					
						
							| 
									
										
										
										
											2018-06-07 16:30:24 +02:00
										 |  |  |       } | 
					
						
							| 
									
										
										
										
											2020-11-05 16:54:04 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Perform notification to subscribers
 | 
					
						
							|  |  |  |       $notify = new Notification(); | 
					
						
							|  |  |  |       $notify->populate_impacted_services($status_id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $notify->type = $type; | 
					
						
							|  |  |  |       $notify->time = $time; | 
					
						
							|  |  |  |       $notify->title = $title; | 
					
						
							|  |  |  |       $notify->text = $text; | 
					
						
							|  |  |  |       $notify->status = $statuses[$type]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       $notify->notify_subscribers(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |       header("Location: " . WEB_URL . "/admin?sent=true"); | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-01-07 20:39:10 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Renders incident | 
					
						
							|  |  |  |    * @param Boolean $admin - decides whether admin controls should be rendered | 
					
						
							|  |  |  |    * @return void | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |   public function render($admin = 0) | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |     global $icons; | 
					
						
							| 
									
										
										
										
											2018-11-21 10:50:21 +01:00
										 |  |  |     global $classes, $user; | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |     $admin = $admin && (($user->get_rank() <= 1) || ($user->get_username() == $this->username)); | 
					
						
							| 
									
										
										
										
											2020-11-05 16:54:04 +03:00
										 |  |  |     $Parsedown = new Parsedown(); | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  | ?>
 | 
					
						
							|  |  |  |     <article class="card border-<?php echo $classes[$this->type]; ?> mb-3"> | 
					
						
							|  |  |  |       <div class="card-colore icon bg-<?php echo $classes[$this->type]; ?>"><i class="<?php echo $icons[$this->type]; ?>"></i></div> | 
					
						
							|  |  |  |       <div class="card-header bg-<?php echo $classes[$this->type]; ?> border-<?php echo $classes[$this->type]; ?>"> | 
					
						
							|  |  |  |         <?php echo $this->title; ?>
 | 
					
						
							|  |  |  |         <div class="float-end"> | 
					
						
							|  |  |  |           <?php if ($admin) { | 
					
						
							| 
									
										
										
										
											2021-03-18 23:20:41 +01:00
										 |  |  |             echo '<a href="' . WEB_URL . '/admin/?delete=' . $this->id . '" class="delete"><i class="fa fa-trash"></i></a>'; | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |           } ?>
 | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |         </div> | 
					
						
							| 
									
										
										
										
											2021-03-18 23:20:41 +01:00
										 |  |  |         <time class="float-end timeago" datetime="<?php echo $this->date; ?>"><?php echo $this->date; ?></time>
 | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |       </div> | 
					
						
							|  |  |  |       <div class="card-body"> | 
					
						
							|  |  |  |         <?php echo $Parsedown->setBreaksEnabled(true)->text($this->text); ?>
 | 
					
						
							|  |  |  |       </div> | 
					
						
							|  |  |  |       <div class="card-footer bg-transparent border-<?php echo $classes[$this->type]; ?>"> | 
					
						
							| 
									
										
										
										
											2021-03-20 19:13:43 +01:00
										 |  |  |         <p class="card-title"> | 
					
						
							|  |  |  |           <?php echo _("Impacted service(s): "); ?>
 | 
					
						
							|  |  |  |           <?php if (isset($this->end_date)) { ?>
 | 
					
						
							|  |  |  |             <span class="float-end"><?php echo strtotime($this->end_date) > time() ? _("Ending") : _("Ended"); ?>: <time class="timeago" datetime="<?php echo $this->end_date; ?>"><?php echo $this->end_date; ?></time></span>
 | 
					
						
							|  |  |  |           <?php } ?>
 | 
					
						
							|  |  |  |         </p> | 
					
						
							|  |  |  |         <p class="card-badge"> | 
					
						
							|  |  |  |           <?php foreach ($this->service_name as $value) { | 
					
						
							|  |  |  |             echo '<span class="badge bg-secondary">' . $value . '</span> '; | 
					
						
							|  |  |  |           } ?>
 | 
					
						
							|  |  |  |         </p> | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |       </div> | 
					
						
							|  |  |  |     </article> | 
					
						
							|  |  |  | <?php | 
					
						
							| 
									
										
										
										
											2017-11-24 00:09:36 +01:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-04-13 21:31:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-18 18:26:03 +01:00
										 |  |  |   public function jsonSerialize() | 
					
						
							|  |  |  |   { | 
					
						
							| 
									
										
										
										
											2018-04-13 21:31:03 +02:00
										 |  |  |     return [ | 
					
						
							|  |  |  |       "id" => $this->id, | 
					
						
							| 
									
										
										
										
											2018-04-13 22:38:10 +02:00
										 |  |  |       "date" => $this->timestamp, | 
					
						
							|  |  |  |       "end_date" => $this->end_timestamp, | 
					
						
							| 
									
										
										
										
											2018-04-13 21:31:03 +02:00
										 |  |  |       "text" => $this->text, | 
					
						
							|  |  |  |       "type" => $this->type, | 
					
						
							|  |  |  |       "title" => $this->title, | 
					
						
							|  |  |  |       "username" => $this->username | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-11-05 16:54:04 +03:00
										 |  |  | } |