Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.66 KB | None | 0 0
  1. <?php
  2. $method = $_SERVER['REQUEST_METHOD'];
  3. $request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
  4. $rest = new RestService("postgres","infotel#1","192.168.187.123","sms_template");
  5. $response = null;
  6. switch ($request[0]){
  7. case RestCommand::addabonent:
  8.     $response = $rest->AddAbonent("ddddd-ddsfsdsd-sdasdasdads");
  9.         break;
  10.     case RestCommand::update:
  11.         $rest->Update();
  12.         break;
  13.        
  14. }
  15. echo json_encode($response);  
  16.  
  17. class RestService{
  18.     private $user;//administrator  
  19.     private $pass;//haslo admina
  20.     private $host;//adres servera
  21.     private $template_db; //bazadanych zawierajaca grafik
  22.     private $db;//pdo obj do polaczenia z baza
  23.     function __construct($u,$p,$h,$t){
  24.         $this->user = $u;
  25.         $this->pass = $p;
  26.         $this->host = $h;
  27.         $this->template_db = $t;
  28.     }
  29.     public function AddAbonent($serial){
  30.         $response = new Response();
  31.         $dbname = $this->ConvertSerialToDbName($serial);
  32.         $this->CheckIfDbExist($dbname);
  33.         $this->CreateDb($dbname);        
  34.         $this->GrantUserPrivileges($this->user,$dbname);
  35.         $this->GrantUserAccess($this->user,$dbname);
  36.         $response->status  = Status::ok;
  37.         return $response;
  38.     }
  39.     public function ConvertSerialToDbName($serial){
  40.         $res = "db_".strtolower(str_replace("-","_",$serial));
  41.         return $res;
  42.     }
  43.     public function ConnectDb($dbname){
  44.         try
  45.         {
  46.             $this->db = new PDO('pgsql:dbname='.$dbname.';host='.$this->host.';user='.$this->user.';password='.$this->pass);
  47.             $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  48.         }
  49.         catch (PDOException $e) {
  50.            $this->handleError($e->getMessage(),Status::connection_failed);
  51.         }    
  52.     }
  53.     public function DisconnectDb(){
  54.         $this->db = null;
  55.     }
  56.     public function CreateDb($dbname){
  57.         $res= null;
  58.         $this->ConnectDb($this->template_db);
  59.         try{
  60.             $res = $this->db->query(  "create database ".$dbname." with template ".$this->template_db  )->queryString;
  61.         }
  62.         catch (PDOException $e) {
  63.            $this->handleError($e->getMessage(),Status::create_db_failed);
  64.         }    
  65.         $this->DisconnectDb();
  66.         return $res;
  67.     }
  68.     public function CheckIfDbExist($dbname){
  69.         $this->ConnectDb($this->template_db);
  70.         try{
  71.             $database = $this->db->query("SELECT datname FROM pg_database WHERE datistemplate = false and datname = '".$dbname."'")->fetchAll(PDO::FETCH_OBJ);
  72.             if(sizeof($database)>0){
  73.                 $this->handleError(null,Status::dbexist);
  74.             }
  75.         }
  76.         catch(PDOException $e){
  77.             $this->handleError($e->getMessage(),Status::checkdb_failed);
  78.         }
  79.         $this->DisconnectDb();
  80.     }
  81.     public function GrantUserAccess($username,$dbname){
  82.         $this->ConnectDb($dbname);
  83.         try{
  84.             $this->db->query("GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO ".$username)->queryString;
  85.         }
  86.         catch (PDOException $e) {
  87.             $this->handleError($e->getMessage(),Status::grand_user_access_failed);
  88.         }    
  89.         $this->DisconnectDb();
  90.     }
  91.     //nadanie praw do tabel uzytkownikowi do wybranej bazy
  92.     public function GrantUserPrivileges($username,$dbname){
  93.         $this->ConnectDb($dbname);
  94.         try{
  95.             $this->db->query("GRANT ALL PRIVILEGES ON DATABASE ".$dbname." to ".$username)->queryString;
  96.         }
  97.         catch (PDOException $e) {
  98.             $this->handleError($e->getMessage(),Status::grand_user_privi_failed);
  99.         }    
  100.         $this->DisconnectDb();
  101.     }
  102.    
  103.     public function CreateUser(){
  104.         $this->ConnectDb($dbname);
  105.         echo '<p style="color:blue"><strong>DODANIE UZYTKOWNIKA: '.$this->newusername.'</strong></p>';
  106.         echo '<p>'.$this->db->query(  "CREATE USER ".$this->newusername." WITH PASSWORD '".$this->newuser_pass."'" )->queryString.'</p>';
  107.         $this->DisconnectDb();
  108.     }
  109.     private function handleError($exception,$status){
  110.         $this->DisconnectDb();
  111.         $response = new Response();
  112.         $response->status = $status;
  113.         $response->content = $exception;
  114.         exit(json_encode($response));
  115.     }
  116.  
  117. }
  118. class RestCommand {
  119.     const addabonent="addabonent";
  120.     const getlist="list";
  121. }
  122. class Response{
  123.     public $status;
  124.     public $content;
  125. }
  126. class Status{
  127.     const ok=1;
  128.     const dbexist=2;
  129.     const connection_failed =3;
  130.     const create_db_failed =4;
  131.     const grand_user_access_failed =5;
  132.     const grand_user_privi_failed = 6;
  133.     const checkdb_failed =7;
  134. }
  135. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement