Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @class Backend Request
- * @description A class to control all backend operations of the website.
- * @author Luke Channings
- * @licence CC 2011
- * @depends Imagick
- *
- * -- Return Values for the client-side --
- * @return 0 - Invalid action.
- * @return 1 - Operation completed successfully.
- * @return 2 - Incomplete data.
- * @return 3 - Operation failed.
- * @return 4 - No operation.
- * @return 5 - Dependencies were not satisfied.
- */
- class backend_request{
- /**
- * @method __construct
- * @description Initialisation method, will connect to the database and perform an action.
- * @return void
- **/
- function __construct($action){
- // Set MySQL database variables.
- $host = "sophia";
- $username = "root";
- $password = "";
- $database = "carpetkings";
- // Create a new MySQL connection.
- try {
- $this->sock = new PDO("mysql:host=$host;dbname=$database",$username,$password);
- $this->sock->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
- } catch (PDOException $e) {
- echo 'Connection failed: ' . $e->getMessage();
- }
- // Check for Image Magick.
- if ( !class_exists("Imagick") ){
- echo 5;
- return;
- }
- // Make sure the method exists before calling it.
- if ( method_exists($this,$action) && count($_POST) !== 0 ){
- // Echo the result of the method.
- echo $this->$action();
- }else{
- // If there is no method echo 0.
- echo 0;
- }
- }
- /**
- * @method __destruct
- * @description Method to be executed when the class is destroyed. (Will close the MySQL socket.)
- * @return void
- **/
- function __destruct(){
- // Close the MySQL socket.
- $this->sock = null;
- }
- /**
- * @method Add
- * @description Function to add carpets to the database.
- * @return int
- */
- protected function add(){
- $nextid = $this->get_next_id();
- foreach($_POST as $group => $item){
- // Ignore the add item. (It's there for telling the script which method to run.)
- if ( $item === "add" ) continue;
- // Check that all of the neccessary fields are filled in.
- if ( $item["name"] && $item["price"] && $item["description"] && $_FILES[$group] ){
- // Sanitise the POST input.
- $name = htmlentities($item["name"]);
- $price = floatval($item["price"]);
- $desc = htmlentities($item["description"]);
- $fname = $nextid . "-" . $_FILES[$group]["name"]["thumbnail"];
- // Make an SQL string.
- $sql = "INSERT INTO carpets (id,name,price,description,image) VALUES ($nextid,'$name',$price,'$desc','$fname')";
- // Insert the current entry into the database.
- $this->sock->exec($sql);
- // cd to the carpets directory. (If it exists.)
- if ( file_exists("../carpets") ) chdir("../carpets");
- else mkdir("../carpets",0777); chdir("../carpets");
- // Check the the thumbs and large directory exist.
- if ( !file_exists("thumbs") || !file_exists("large") ){
- // Make sure that the thumbs directory exists.
- if ( !file_exists("thumbs") ) mkdir("thumbs",0777);
- else mkdir("large",0777);
- }
- // Move the uploaded image to the current directory.
- move_uploaded_file($_FILES[$group]["tmp_name"]["thumbnail"], $fname);
- // Make an instance of Imagick.
- $image = new Imagick($fname);
- // Make a large(r) image for the side panel.
- $image->thumbnailImage(300,0);
- // Write the large image.
- $image->writeImages("large/" . $fname);
- // Make a thumbnail image.
- $image->thumbnailImage(150,0);
- // Write the thumbnail image.
- $image->writeImages("thumbs/" . $fname);
- // delete the uploaded file.
- unlink($fname);
- // Increment the next ID. (This way we don't have to call get_next_id for each loop.)
- $nextid++;
- }
- else{
- // Skip an entry if it is incomplete.
- continue;
- }
- }
- return 1;
- }
- /**
- * @method Delete
- * @description Method to delete a database entry by ID.
- * @return int
- */
- protected function delete(){
- $id = intval($_POST["id"]);
- if ( !empty($id) ){
- // Delete the entry from the database.
- $this->sock->exec("DELETE FROM carpets WHERE id = {$id}");
- // Check if the operation completed.
- if ( $this->sock->errorCode() ) return 1;
- // If it didn't then let the client know.
- else return 3;
- }
- else{
- // Tell the client there was no ID.
- return 2;
- }
- }
- /**
- * @method Truncate
- * @description Method to truncate the database. (Deletes all values and resets ID counter to default.)
- * @return int
- */
- protected function truncate(){
- // Truncate the table.
- $this->sock->exec("TRUNCATE carpets");
- // Check whether the operation completed successfully.
- if ( $this->sock->errorCode() == 0 ){
- return 1;
- }
- else{
- return 0;
- }
- }
- /**
- * @method List Carpets (JSON)
- * @description Method returns a list of all carpets in the database in JSON form.
- * @return string
- */
- protected function list_carpets_json(){
- // Loop through the results.
- foreach( $this->sock->query("SELECT * FROM carpets") as $row){
- // Append the current row to the result.
- $result[] = $row;
- }
- // Return the JSON string.
- return json_encode($result);
- }
- /**
- * @method List Carpets (HTML)
- * @description Method returns a list of all carpets in the database in JSON form.
- * @return string
- */
- protected function list_carpets_html(){
- // Loop through the results.
- foreach( $this->sock->query("SELECT * FROM carpets") as $row){
- // Construct a list item.
- $result .= '<li><a href="#" rel="' . $row["price"] . '" onmouseover="preloader(carpets/large/';
- $result .= $row["image"] . ')"><img src="carpets/thumbs/' . $row["image"] . '" alt="';
- $result .= $row["name"] . '" /></a><p class="caption">' . $row["name"] . '</p></li>' . "\n";
- }
- // Return the list items.
- return $result;
- }
- /**
- * @method get_next_id
- * @description Will return ID for the next table item.
- * @return int
- */
- protected function get_next_id(){
- // Make a query to find the current highest ID.
- $query = $this->sock->query("SELECT id FROM carpets ORDER BY id DESC LIMIT 1");
- // Fetch the result into a variable.
- $result = $query->fetch();
- // Return the result plus one.
- return $result["id"] + 1;
- }
- }
- if ( count($_POST) !== 0 ){
- if ( !empty($_POST["action"]) ) $instance = new backend_request($_POST["action"]);
- else echo 0;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement