Separate ValidatePlant and CheckSyntax
[L-seed.git] / web / php / Plant.php
1 <?php
2         
3         class Plant
4         {
5             public $ID;
6             public $UserID;
7             public $Name;
8             public $Code;
9                 public $IsActive;
10                 public $m_Database;
11
12                 public function __construct($id, $userid, $name, $code, $database) {
13                         $this->ID = $id;
14                         $this->UserID = $userid;
15                         $this->Name = $name;
16                         $this->Code = $code;
17                         $this->IsActive = false;
18                         $this->m_Database = $database;
19                         if (!isset($GLOBALS['ValidatorFile'])) {
20                                 $GLOBALS['ValidatorFile'] = '/home/lseed/.cabal/bin/validate';
21                                 #if ($GLOBALS['WINDOWS']) {
22                                 #       $GLOBALS['ValidatorFile'] = '../cgi/validate.exe';
23                                 #}
24                         }
25                 }
26
27                 public function ToJson() {
28                         
29                         $active = 'false';
30                         if ($this->IsActive) { $active = 'true'; } else { $active = 'false'; }
31                         return "{ ID: " . $this->ID . ", Name: '" . $this->Name . "', Code: '" . $this->Code . "', IsActive: " . $active . " }";
32                 }
33
34                 public function ToJsonArray() {
35                         return "[ " . $this->ID . ", '" . $this->Name . "', '" . $this->Code . "' ]";
36                 }
37
38                 public function Save($code) {
39                         $result = "{ success: true, msg: '' }";
40                         
41                         $this->Code = $code;
42
43                         $user = $this->m_Database->GetUserByID($this->UserID);
44                         if ($user) {
45                                 $plant = $this->m_Database->GetPlant($user->ID, $this->Name);
46                                 
47                                 if ($plant == null) {
48                                         if (!$this->m_Database->InsertNewPlant($user->ID, $this->Name, $this->Code)) {
49                                                 $result = "{ success: false, msg: 'Pflanze konnte nicht erstellt werden.' }";
50                                         }
51                                 } else {
52                                         $plant->Code = $code;
53                                         if (!$this->m_Database->UpdatePlant($plant)) {
54                                                 $result = "{ success: false, msg: 'Pflanze konnte nicht aktualisiert werden.' }";
55                                         }
56                                 }
57                         } else {
58                                 $result = "{ success: false, msg: 'User (id: \'".$this->UserID."\' could not be found.' }";
59                         }
60
61                         return $result;
62                 }
63
64                 public function Delete() {
65                         $result = "{ success: false, msg: 'Pflanze konnte nicht gelöscht werden.' }";
66
67                         if ($this->m_Database->DropPlant($this)) {
68                                 $result = "{ success: true, msg: '' }";
69                         }
70
71                         return $result;
72                 }
73                 
74                 public function Validate() {
75                         return $this->ValidateCode();
76                 }
77                 
78                 public function ValidateCode() {
79                         $result = "{valid: false, line: 0, column: 0, msg: 'Interner Server Fehler'}";
80                         
81                         $descriptorspec = array(
82                            0 => array("pipe", "r"),  // STDIN ist eine Pipe, von der das Child liest
83                            1 => array("pipe", "w"),  // STDOUT ist eine Pipe, in die das Child schreibt
84                            2 => array("pipe", "w")   // STDERR
85                         );
86
87                         $cwd = realpath(".");
88
89                         $process = proc_open($GLOBALS['ValidatorFile'], $descriptorspec, $pipes, $cwd, array());
90
91                         if (is_resource($process)) {
92                                 // $pipes sieht nun so aus:
93                                 // 0 => Schreibhandle, das auf das Child STDIN verbunden ist
94                                 // 1 => Lesehandle, das auf das Child STDOUT verbunden ist
95                                 // Jedwede Fehlerausgaben werden an /tmp/error-output.txt angefügt
96
97                                 fwrite($pipes[0], $this->Code);
98                                 fclose($pipes[0]);
99
100                                 $output = stream_get_contents($pipes[1]);
101                                 fclose($pipes[1]);
102
103                                 //echo stream_get_contents($pipes[2]);
104                                 fclose($pipes[2]);
105
106                                 $return_value = proc_close($process);
107                                 
108                                 //echo $return_value;
109                                 if ($return_value == 0) {
110                                         $result = $output;
111                                 }
112                         }
113
114                         return $result;
115                 }
116                 
117                 public function Activate() {
118                         $result = "{ success: false, msg: 'Ihre Pflanze konnte nicht aktiviert werden.' }";
119                         
120                         if ($this->IsActive) {
121                                 $result = "{ success: false, msg: 'Ihre Pflanze ist bereits aktiv.' }";
122                         } else {
123                                 if ($this->m_Database->SetUsersNextSeed($this->UserID, $this->ID)) {
124                                         $result = "{ success: true, msg: '' }";
125                                         $this->IsActive = true;
126                                 }
127                         }
128                         
129                         return $result;
130                 }
131         }
132 ?>