Advertisement
Guest User

lukiss rest api

a guest
Nov 6th, 2017
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.34 KB | None | 0 0
  1. <?php
  2. header("Access-Control-Allow-Origin: *");
  3. header("Access-Control-Allow-Headers: access");
  4. header("Access-Control-Allow-Methods: GET");
  5. header("Access-Control-Allow-Credentials: true");
  6. header('Content-Type: application/json');
  7. header('Cache-Control: no-cache');
  8. header("Authorization: Basic " . base64_encode('username:password')); // je tohle vubec treba?
  9.  
  10.     require '../../myautoload.php';
  11.     require '../../vendor/autoload.php';
  12.  
  13.     use app\manager;
  14.     use app\model;
  15.  
  16.     if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) { //nemělo by se to spíš brát z hlavičky Authorizaion?
  17.         $auth = new model\Model_Authenticator();
  18.         $db = $auth->verifyAndReturnDatabase($_SERVER['PHP_AUTH_USER'], $_SERVER["PHP_AUTH_PW"]); // neměl bych nějak trimovat tyhle hodnoty
  19.         if($db) {
  20.             $devices = new manager\Manager_Devices($db);
  21.             $devices->load();
  22.             if($_GET["id"] ?? null && is_int($_GET["id"])) {
  23.                 print_r(json_encode($devices->get($_GET["id"])));
  24.             }
  25.             else {
  26.                 print_r(json_encode($devices->table()));
  27.             }
  28.             header('HTTP/1.0 200 OK');
  29.             exit;
  30.         }
  31.         else {
  32.             header('WWW-Authenticate: Basic realm="My Realm"');
  33.             header('HTTP/1.0 401 Unauthorized');
  34.             exit;  
  35.         }
  36.  
  37.     }
  38.     else {
  39.         header('WWW-Authenticate: Basic realm="My Realm"');
  40.         header('HTTP/1.0 401 Unauthorized');
  41.         exit;
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement