Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $method = $_SERVER['REQUEST_METHOD'];
- $request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
- $rest = new RestService("postgres","infotel#1","192.168.187.123","sms_template");
- $response = null;
- switch ($request[0]){
- case RestCommand::addabonent:
- $response = $rest->AddAbonent("ddddd-ddsfsdsd-sdasdasdads");
- break;
- case RestCommand::update:
- $rest->Update();
- break;
- }
- echo json_encode($response);
- class RestService{
- private $user;//administrator
- private $pass;//haslo admina
- private $host;//adres servera
- private $template_db; //bazadanych zawierajaca grafik
- private $db;//pdo obj do polaczenia z baza
- function __construct($u,$p,$h,$t){
- $this->user = $u;
- $this->pass = $p;
- $this->host = $h;
- $this->template_db = $t;
- }
- public function AddAbonent($serial){
- $response = new Response();
- $dbname = $this->ConvertSerialToDbName($serial);
- $this->CheckIfDbExist($dbname);
- $this->CreateDb($dbname);
- $this->GrantUserPrivileges($this->user,$dbname);
- $this->GrantUserAccess($this->user,$dbname);
- $response->status = Status::ok;
- return $response;
- }
- public function ConvertSerialToDbName($serial){
- $res = "db_".strtolower(str_replace("-","_",$serial));
- return $res;
- }
- public function ConnectDb($dbname){
- try
- {
- $this->db = new PDO('pgsql:dbname='.$dbname.';host='.$this->host.';user='.$this->user.';password='.$this->pass);
- $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- }
- catch (PDOException $e) {
- $this->handleError($e->getMessage(),Status::connection_failed);
- }
- }
- public function DisconnectDb(){
- $this->db = null;
- }
- public function CreateDb($dbname){
- $res= null;
- $this->ConnectDb($this->template_db);
- try{
- $res = $this->db->query( "create database ".$dbname." with template ".$this->template_db )->queryString;
- }
- catch (PDOException $e) {
- $this->handleError($e->getMessage(),Status::create_db_failed);
- }
- $this->DisconnectDb();
- return $res;
- }
- public function CheckIfDbExist($dbname){
- $this->ConnectDb($this->template_db);
- try{
- $database = $this->db->query("SELECT datname FROM pg_database WHERE datistemplate = false and datname = '".$dbname."'")->fetchAll(PDO::FETCH_OBJ);
- if(sizeof($database)>0){
- $this->handleError(null,Status::dbexist);
- }
- }
- catch(PDOException $e){
- $this->handleError($e->getMessage(),Status::checkdb_failed);
- }
- $this->DisconnectDb();
- }
- public function GrantUserAccess($username,$dbname){
- $this->ConnectDb($dbname);
- try{
- $this->db->query("GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO ".$username)->queryString;
- }
- catch (PDOException $e) {
- $this->handleError($e->getMessage(),Status::grand_user_access_failed);
- }
- $this->DisconnectDb();
- }
- //nadanie praw do tabel uzytkownikowi do wybranej bazy
- public function GrantUserPrivileges($username,$dbname){
- $this->ConnectDb($dbname);
- try{
- $this->db->query("GRANT ALL PRIVILEGES ON DATABASE ".$dbname." to ".$username)->queryString;
- }
- catch (PDOException $e) {
- $this->handleError($e->getMessage(),Status::grand_user_privi_failed);
- }
- $this->DisconnectDb();
- }
- public function CreateUser(){
- $this->ConnectDb($dbname);
- echo '<p style="color:blue"><strong>DODANIE UZYTKOWNIKA: '.$this->newusername.'</strong></p>';
- echo '<p>'.$this->db->query( "CREATE USER ".$this->newusername." WITH PASSWORD '".$this->newuser_pass."'" )->queryString.'</p>';
- $this->DisconnectDb();
- }
- private function handleError($exception,$status){
- $this->DisconnectDb();
- $response = new Response();
- $response->status = $status;
- $response->content = $exception;
- exit(json_encode($response));
- }
- }
- class RestCommand {
- const addabonent="addabonent";
- const getlist="list";
- }
- class Response{
- public $status;
- public $content;
- }
- class Status{
- const ok=1;
- const dbexist=2;
- const connection_failed =3;
- const create_db_failed =4;
- const grand_user_access_failed =5;
- const grand_user_privi_failed = 6;
- const checkdb_failed =7;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement