Guest User

Untitled

a guest
Apr 20th, 2018
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.11 KB | None | 0 0
  1. <?php
  2. /*
  3.  *      rest.php
  4.  *      
  5.  *      Copyright 2011 Lana Krnic <lana.krnic@gmail.com>
  6.  *      
  7.  *      This program is free software; you can redistribute it and/or modify
  8.  *      it under the terms of the GNU General Public License as published by
  9.  *      the Free Software Foundation; either version 2 of the License, or
  10.  *      (at your option) any later version.
  11.  *      
  12.  *      This program is distributed in the hope that it will be useful,
  13.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *      GNU General Public License for more details.
  16.  *      
  17.  *      You should have received a copy of the GNU General Public License
  18.  *      along with this program; if not, write to the Free Software
  19.  *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20.  *      MA 02110-1301, USA.
  21.  */
  22.  
  23. echo "URL: ". $_GET['url'];
  24.  
  25. $url_parts = explode("/", $_GET['url']);
  26.  
  27. $service=$url_parts[0];
  28.  
  29. echo "Service: " . $service;
  30.  
  31. $dbconn = pg_connect("host=localhost dbname=rest user=hmp password=temperatime")
  32.     or die('Could not connect: ' . pg_last_error());
  33.  
  34.  
  35.  
  36. if($_SERVER['REQUEST_METHOD'] == 'GET') {
  37.     if($service == 'order') {
  38.         $id=$url_parts[1];
  39.         if(!$id) {
  40.             $result = pg_query($dbconn, "SELECT id, buyerid FROM orders") or die('Could not query: ' . pg_last_error());
  41.             while ($arr = pg_fetch_array($result)) {
  42.                 echo "<order id=$arr[0]>\n";  
  43.                 echo "\t<buyerid>$arr[1]</buyerid>\n";
  44.                 echo "</order>\n\n";
  45.             }
  46.         }
  47.         else {
  48.             $result = pg_query($dbconn, "SELECT buyerid FROM orders WHERE id = $id") or die('Could not query: ' . pg_last_error());
  49.             while ($arr = pg_fetch_array($result)) {
  50.                 echo "<order id=$id>\n";  
  51.                 echo "\t<buyerid>$arr[0]</buyerid>\n";
  52.                 $result2 = pg_query($dbconn, "SELECT itemname FROM items WHERE orderid = $id");
  53.                 while ($arr2 = pg_fetch_array($result2)) {
  54.                     echo "\t<item>$arr2[0]</item>\n";
  55.                 }  
  56.                 echo "</order>\n\n";
  57.             }
  58.         }
  59.     }
  60.     if ($service == 'customers') {
  61.         $id = $url_parts[1];
  62.         $orders = $url_parts[2];
  63.         if(!$id) {
  64.             $result = pg_query($dbconn, "SELECT id, name FROM customers") or die('Could not query: ' . pg_last_error());
  65.             while ($arr = pg_fetch_array($result)) {
  66.                 echo "<customer id=$arr[0]>\n";  
  67.                 echo "\t<name>$arr[1]</name>\n";
  68.                 echo "</customer>\n\n";
  69.             }
  70.         }
  71.         elseif(!$orders) {
  72.             $result = pg_query($dbconn, "SELECT id, name, age, city FROM customers WHERE id='$id'") or die('Could not query: ' . pg_last_error());
  73.             while ($arr = pg_fetch_array($result)) {
  74.                 echo "<customer id=$arr[0]>\n";  
  75.                 echo "\t<name>$arr[1]</name>\n";
  76.                 echo "\t<age>$arr[2]</age>\n";
  77.                 echo "\t<city>$arr[3]</city>\n";
  78.                 echo "</customer>\n\n";
  79.             }
  80.         }
  81.         elseif ($orders == 'orders') {
  82.             echo "<customer id=$id>\n";
  83.             $result = pg_query($dbconn, "SELECT id FROM orders WHERE buyerid = '$id'");
  84.             while ($arr = pg_fetch_array($result)) {
  85.                 echo "\t<order>$arr[0]</order>\n";
  86.             }
  87.             echo "</customer>\n\n";
  88.            
  89.         }
  90.            
  91.     }
  92. }
  93.  
  94. if($_SERVER['REQUEST_METHOD'] == 'POST') {
  95.     if($service == 'order') {
  96.         $id=$url_parts[1];
  97.         if(!$id) {
  98.             $buyerid = $_POST['buyerid'];
  99.             $result = pg_query ($dbconn, "INSERT INTO orders (buyerid) VALUES ('$buyerid')");
  100.         }
  101.         else
  102.         {
  103.             $item = $_POST['item'];
  104.             $result = pg_query ($dbconn, "INSERT INTO items (orderid, itemname) VALUES ('$id:*', '$item')");
  105.         }
  106.     }
  107.     if($service == 'customers') {
  108.         $id=$url_parts[1];
  109.         $orders=$url_parts[2];
  110.         if(!$id) {
  111.             $name = $_POST['name'];
  112.             $age = $_POST['age'];
  113.             $city = $_POST['city'];
  114.             $result = pg_query ($dbconn, "INSERT INTO customers (name, age, city) VALUES ('$name', '$age', '$city')");
  115.         }
  116.         if($orders) {
  117.             $result = pg_query ($dbconn, "INSERT INTO orders (buyerid) VAlUES ('$id')");
  118.         }
  119.     }
  120. }
  121.    
  122. if($_SERVER['REQUEST_METHOD'] == 'PUT') {
  123.     parse_str(file_get_contents("php://input"), $post_vars);
  124.     if($service == 'order') {
  125.         $id=$url_parts[1];
  126.         if($id) {
  127.             $buyerid = $post_vars['buyerid'];
  128.             $result = pg_query($dbconn, "UPDATE orders SET buyerid='$buyerid' WHERE id='$id'");
  129.         }
  130.     }
  131.     if($service == 'customers') {
  132.         $id=$url_parts[1];
  133.         $orders = $url_parts[2];
  134.         if($id && !$orders) {
  135.             $name = $post_vars['name'];
  136.             $city = $post_vars['city'];
  137.             $age=  $post_vars['age'];
  138.             if($name) $query .= ",name='" . $name ."'";
  139.             if($city) $query .= ",city='" . $city . "'";
  140.             if($age) $query .= ",age='" . $age ."'";
  141.             $query = substr($query, 1);
  142.             $result = pg_query($dbconn, "UPDATE customers SET $query WHERE id='$id'");
  143.         }
  144.     }
  145. }
  146.  
  147. if($_SERVER['REQUEST_METHOD'] == 'DELETE') {
  148.     parse_str(file_get_contents("php://input"), $post_vars);
  149.     if($service == 'order') {
  150.         $id=$url_parts[1];
  151.         if($id) {
  152.             $result = pg_query($dbconn, "DELETE FROM orders WHERE id='$id'");
  153.         }
  154.     }
  155.     if($service == 'customers') {
  156.         $id=$url_parts[1];
  157.         $orders = $url_parts[2];
  158.         if($id && !$orders) {
  159.             $result = pg_query($dbconn, "DELETE FROM customers WHERE id='$id'");
  160.         }
  161.         elseif($orders) {
  162.             $result = pg_query($dbconn, "DELETE FROM orders WHERE buyerid='$id'");
  163.         }
  164.     }
  165. }
  166. ?>
Add Comment
Please, Sign In to add comment