· 9 years ago · Sep 16, 2017, 01:58 PM
1<?php
2
3/**
4 * @class Backend Request
5 * @description A class to control all backend operations of the website.
6 * @author Luke Channings
7 * @licence CC 2011
8 * @depends Imagick
9 *
10 * -- Return Values for the client-side --
11 * @return 0 - Invalid action.
12 * @return 1 - Operation completed successfully.
13 * @return 2 - Incomplete data.
14 * @return 3 - Operation failed.
15 * @return 4 - No operation.
16 * @return 5 - Dependencies were not satisfied.
17 */
18class backend_request{
19
20 /**
21 * @method __construct
22 * @description Initialisation method, will connect to the database and perform an action.
23 * @return void
24 **/
25 function __construct($action){
26
27 // Set MySQL database variables.
28 $host = "sophia";
29 $username = "root";
30 $password = "";
31 $database = "carpetkings";
32
33 // Create a new MySQL connection.
34 try {
35
36 $this->sock = new PDO("mysql:host=$host;dbname=$database",$username,$password);
37 $this->sock->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
38
39 } catch (PDOException $e) {
40
41 echo 'Connection failed: ' . $e->getMessage();
42
43 }
44
45 // Check for Image Magick.
46 if ( !class_exists("Imagick") ){
47
48 echo 5;
49 return;
50
51 }
52
53 // Make sure the method exists before calling it.
54 if ( method_exists($this,$action) && count($_POST) !== 0 ){
55
56 // Echo the result of the method.
57 echo $this->$action();
58
59 }else{
60
61 // If there is no method echo 0.
62 echo 0;
63
64 }
65
66 }
67
68 /**
69 * @method __destruct
70 * @description Method to be executed when the class is destroyed. (Will close the MySQL socket.)
71 * @return void
72 **/
73 function __destruct(){
74
75 // Close the MySQL socket.
76 $this->sock = null;
77
78 }
79
80
81 /**
82 * @method Add
83 * @description Function to add carpets to the database.
84 * @return int
85 */
86 protected function add(){
87
88 $nextid = $this->get_next_id();
89
90 foreach($_POST as $group => $item){
91
92 // Ignore the add item. (It's there for telling the script which method to run.)
93 if ( $item === "add" ) continue;
94
95 // Check that all of the neccessary fields are filled in.
96 if ( $item["name"] && $item["price"] && $item["description"] && $_FILES[$group] ){
97
98 // Sanitise the POST input.
99 $name = htmlentities($item["name"]);
100 $price = floatval($item["price"]);
101 $desc = htmlentities($item["description"]);
102 $fname = $nextid . "-" . $_FILES[$group]["name"]["thumbnail"];
103
104 // Make an SQL string.
105 $sql = "INSERT INTO carpets (id,name,price,description,image) VALUES ($nextid,'$name',$price,'$desc','$fname')";
106
107 // Insert the current entry into the database.
108 $this->sock->exec($sql);
109
110 // cd to the carpets directory. (If it exists.)
111 if ( file_exists("../carpets") ) chdir("../carpets");
112 else mkdir("../carpets",0777); chdir("../carpets");
113
114 // Check the the thumbs and large directory exist.
115 if ( !file_exists("thumbs") || !file_exists("large") ){
116
117 // Make sure that the thumbs directory exists.
118 if ( !file_exists("thumbs") ) mkdir("thumbs",0777);
119 else mkdir("large",0777);
120
121 }
122
123 // Move the uploaded image to the current directory.
124 move_uploaded_file($_FILES[$group]["tmp_name"]["thumbnail"], $fname);
125
126 // Make an instance of Imagick.
127 $image = new Imagick($fname);
128
129 // Make a large(r) image for the side panel.
130 $image->thumbnailImage(300,0);
131
132 // Write the large image.
133 $image->writeImages("large/" . $fname);
134
135 // Make a thumbnail image.
136 $image->thumbnailImage(150,0);
137
138 // Write the thumbnail image.
139 $image->writeImages("thumbs/" . $fname);
140
141 // delete the uploaded file.
142 unlink($fname);
143
144 // Increment the next ID. (This way we don't have to call get_next_id for each loop.)
145 $nextid++;
146
147 }
148 else{
149
150 // Skip an entry if it is incomplete.
151 continue;
152
153 }
154
155
156 }
157
158 return 1;
159
160 }
161
162 /**
163 * @method Delete
164 * @description Method to delete a database entry by ID.
165 * @return int
166 */
167 protected function delete(){
168
169 $id = intval($_POST["id"]);
170
171 if ( !empty($id) ){
172
173 // Delete the entry from the database.
174 $this->sock->exec("DELETE FROM carpets WHERE id = {$id}");
175
176 // Check if the operation completed.
177 if ( $this->sock->errorCode() ) return 1;
178
179 // If it didn't then let the client know.
180 else return 3;
181 }
182 else{
183
184 // Tell the client there was no ID.
185 return 2;
186
187 }
188
189 }
190
191 /**
192 * @method Truncate
193 * @description Method to truncate the database. (Deletes all values and resets ID counter to default.)
194 * @return int
195 */
196 protected function truncate(){
197
198 // Truncate the table.
199 $this->sock->exec("TRUNCATE carpets");
200
201 // Check whether the operation completed successfully.
202 if ( $this->sock->errorCode() == 0 ){
203 return 1;
204 }
205 else{
206 return 0;
207 }
208
209 }
210
211 /**
212 * @method List Carpets (JSON)
213 * @description Method returns a list of all carpets in the database in JSON form.
214 * @return string
215 */
216 protected function list_carpets_json(){
217
218 // Loop through the results.
219 foreach( $this->sock->query("SELECT * FROM carpets") as $row){
220
221 // Append the current row to the result.
222 $result[] = $row;
223
224 }
225
226 // Return the JSON string.
227 return json_encode($result);
228
229 }
230
231 /**
232 * @method List Carpets (HTML)
233 * @description Method returns a list of all carpets in the database in JSON form.
234 * @return string
235 */
236 protected function list_carpets_html(){
237
238 // Loop through the results.
239 foreach( $this->sock->query("SELECT * FROM carpets") as $row){
240
241 // Construct a list item.
242 $result .= '<li><a href="#" rel="' . $row["price"] . '" onmouseover="preloader(carpets/large/';
243 $result .= $row["image"] . ')"><img src="carpets/thumbs/' . $row["image"] . '" alt="';
244 $result .= $row["name"] . '" /></a><p class="caption">' . $row["name"] . '</p></li>' . "\n";
245
246 }
247
248 // Return the list items.
249 return $result;
250 }
251
252 /**
253 * @method get_next_id
254 * @description Will return ID for the next table item.
255 * @return int
256 */
257 protected function get_next_id(){
258
259 // Make a query to find the current highest ID.
260 $query = $this->sock->query("SELECT id FROM carpets ORDER BY id DESC LIMIT 1");
261
262 // Fetch the result into a variable.
263 $result = $query->fetch();
264
265 // Return the result plus one.
266 return $result["id"] + 1;
267
268 }
269
270}
271
272if ( count($_POST) !== 0 ){
273
274 if ( !empty($_POST["action"]) ) $instance = new backend_request($_POST["action"]);
275 else echo 0;
276
277}
278
279
280?>