1<?php
2	class session_controller extends controller {
3		private function show_sessions() {
4			if (($sessions = $this->model->get_sessions()) === false) {
5				$this->output->add_tag("result", "Error fetching session information.");
6				return;
7			}
8
9			$this->output->open_tag("sessions");
10			foreach ($sessions as $session) {
11				$session["owner"] = ($session["session_id"] == $_COOKIE[SESSION_NAME]) ? "current" : "other";
12				$session["expire"] = date("j F Y, H:i:s", $session["expire"]);
13				$this->output->record($session, "session");
14			}
15			$this->output->close_tag();
16		}
17
18		private function show_session_form($session) {
19			$this->output->open_tag("edit");
20
21			$this->output->record($session, "session");
22
23			$this->output->close_tag();
24		}
25
26		public function execute() {
27			if ($this->user->logged_in == false) {
28				$this->output->add_tag("result", "The session manager should not be accessible for non-authenticated visitors!");
29				return;
30			} else if ($this->user->session_via_database == false) {
31				$this->output->add_tag("result", "The database is not being used to store sessions, so there is nothing to manage.");
32				return;
33			}
34
35			if ($_SERVER["REQUEST_METHOD"] == "POST") {
36				if ($_POST["submit_button"] == "Update session") {
37					/* Edit session
38				 	 */
39					if ($this->model->update_session($_POST) == false) {
40						$this->output->add_tag("result", "Error while updateing session.");
41					} else {
42						$this->show_sessions();
43					}
44				} else if ($_POST["submit_button"] == "Delete session") {
45					/* Delete session
46					 */
47					if ($this->model->delete_session($_POST["id"]) == false) {
48						$this->output->add_tag("result", "Error while deleting session.");
49					} else {
50						$this->show_sessions();
51					}
52				} else {
53					$this->show_sessions();
54				}
55			} else if (isset($this->page->pathinfo[1])) {
56				/* Edit session
57				 */
58				if (($session = $this->model->get_session($this->page->pathinfo[1])) == false) {
59					$this->output->add_tag("result", "Session not found.");
60				} else {
61					$session["expire"] = date("j F Y, H:i:s", $session["expire"]);
62					$this->show_session_form($session);
63				}
64			} else {
65				/* Show overview
66				 */
67				$this->show_sessions();
68			}
69		}
70	}
71?>
72