chmod a-x editplant.pg
[L-seed.git] / web / php / Server.php
1 <?php
2
3         class Server
4         {
5                 protected $m_Controller;
6                 
7                 public function __construct($controller) {
8                         $this->m_Controller = $controller;
9                 }
10
11                 public function ReturnContent($content) {
12                         switch ($content) {
13                                 case "myplants":
14                                 case "createplant":
15                                 case "testplant":
16                                         new ContentMessage($content).send();
17                                         break;
18                                 case "nav":
19                                         if ($this->IsAdmin() != "false") {
20                                                 new ContentMessage("adminnav").send();
21                                         } else {
22                                                 new ContentMessage($content).send();
23                                         }
24                                 break;
25                                 case "debug":
26                                         if ($this->IsAdmin() != "false") {
27                                                 new ContentMessage($content).send();
28                                         }
29                                 break;
30                         }
31                 }
32
33                 public function IsLoggedIn() {
34                         $result = "false";
35                         
36                         if (isset($_SESSION['user']) and isset($_SESSION['pw'])) {
37                                 $user = $_SESSION['user'];
38                                 $md5pw = $_SESSION['pw'];
39
40                                 $userobj = $this->m_Controller->GetDatabase()->GetUser($user);
41                                 if ($userobj != null and $userobj->Password == $md5pw) {
42                                         $result = "true";
43                                 }
44                         }
45
46                         return $result;
47                 }
48
49                 public function IsAdmin() {
50                         $result = "false";
51                         
52                         $user = $controller->GetUser();
53                         if ($user != null) {
54                                 if ($user->IsAdmin) {
55                                         $result = "true";
56                                 }
57                         }
58
59                         return $result;
60                 }
61
62                 public function LogIn($username, $md5pw) {
63                         $result = "false";
64                         $user = $this->m_Controller->GetDatabase()->GetUser($username);
65                         
66                         if ($user != null and $user->Name == $username and $user->Password == $md5pw) {
67                                 $_SESSION['user'] = $user->Name;
68                                 $_SESSION['pw'] = $md5pw;
69
70                                 $result = "true";
71                         }
72
73                         return $result;
74                 }
75
76                 public function LogOut() {
77                         $_SESSION['user'] = "";
78                         $_SESSION['pw'] = "";
79                         $user = $this->m_Controller->SetUser(null);
80                         return "true";
81                 }
82
83                 public function Register($username, $md5pw) {
84                         $result = "{ success: true, msg: '' }";
85                         
86                         $userobj = $this->m_Controller->GetDatabase()->GetUser($username);
87
88                         if ($userobj == null) {
89                                 if (!$this->m_Controller->GetDatabase()->CreateUser($username, $md5pw)) {
90                                         $result = "{ success: false, msg: 'Benutzer konnte nicht erstellt werden.' }";
91                                 } else {
92                                         if ($this->LogIn($username, $md5pw) == "true") {
93                                                 $result = "{ success: true, msg: 'Benutzer erstellt und erfolgreich angemeldet.' }";
94                                         } else {
95                                                 $result = "{ success: true, msg: 'Benutzer erstellt, die Anmeldung schlug jedoch fehl.' }";
96                                         }
97                                 }
98                         } else {
99                                 $result = "{ success: false, msg: 'Benutzername bereits vergeben.' }";
100                         }
101
102                         return $result;
103                 }
104         }
105 ?>