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