Connection.class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. class Connection extends Entity{
  3. public $id,$label,$meta,$handler;
  4. public $TABLE_NAME = 'statistic_connection';
  5. public $fields = array(
  6. 'id'=>'key',
  7. 'label'=>'longstring',
  8. 'meta'=>'longstring',
  9. 'handler'=>'string'
  10. );
  11. public function realString(){
  12. $meta = json_decode($this->meta,true);
  13. require_once(__ROOT__.SLASH.'connector'.SLASH.$this->handler.'.class.php');
  14. $connection = $this->handler::connection;
  15. foreach ($meta as $key => $value) {
  16. $connection = str_replace('{{'.$key.'}}',$value,$connection);
  17. }
  18. $connection = str_replace('{{ROOT}}',__ROOT__,$connection);
  19. return $connection;
  20. }
  21. public function test(){
  22. require_once(__ROOT__.SLASH.'connector'.SLASH.$this->handler.'.class.php');
  23. $pdo = $this->getPdo();
  24. if(!isset($pdo)) throw new Exception("Impossible de se connecter à la base");
  25. $statment = $pdo->prepare($this->handler::show_tables());
  26. $statment->execute(array());
  27. $rows = $statment->fetchAll();
  28. if(!is_array($rows)) throw new Exception("Impossible de requeter la base");
  29. return count($rows);
  30. }
  31. public function getPdo(){
  32. $fileHandler = __ROOT__.SLASH.'connector'.SLASH.$this->handler.'.class.php';
  33. if(!file_exists($fileHandler)) return null;
  34. require_once($fileHandler);
  35. $string = $this->realString();
  36. $meta = json_decode($this->meta,true);
  37. $login = isset($meta['login']) ? $meta['login'] : null;
  38. $password = isset($meta['password']) ? $meta['password'] : null;
  39. try{
  40. $connection = new PDO($string, $login,$password,$this->handler::pdo_attributes());
  41. if(method_exists ( $this->handler , 'beforeTransaction' ))
  42. $this->handler::beforeTransaction($connection);
  43. return $connection;
  44. }catch(Exception $e){
  45. return null;
  46. }
  47. }
  48. public function readOnly(){
  49. $pdo = $this->getPdo();
  50. if(strrpos($this->realString(), 'mysql:')!==false){
  51. $query = $pdo->query('SHOW GRANTS');
  52. $response = $query->fetchAll();
  53. if(isset($response[0])){
  54. if(!preg_match("/GRANT SELECT ON .* TO /i",$response[0][0])) return false;
  55. }
  56. }
  57. return true;
  58. }
  59. public function tables(){
  60. $fileHandler = __ROOT__.SLASH.'connector'.SLASH.$this->handler.'.class.php';
  61. if(!file_exists($fileHandler)) return array();
  62. require_once($fileHandler);
  63. $pdo = $this->getPDO();
  64. $tables = array();
  65. if(is_null($pdo)) return $tables;
  66. $lines = array();
  67. $query = $pdo->query($this->handler::show_tables());
  68. $lines = $query->fetchAll();
  69. foreach($lines as $line)
  70. $tables[] = $line[0];
  71. return $tables;
  72. }
  73. public function columns($table){
  74. $fileHandler = __ROOT__.SLASH.'connector'.SLASH.$this->handler.'.class.php';
  75. if(!file_exists($fileHandler)) return array();
  76. require_once($fileHandler);
  77. $pdo = $this->getPDO();
  78. $columns = array();
  79. if(is_null($pdo)) return array();
  80. $lines = array();
  81. $query = str_replace('{{table}}',$table,$this->handler::show_columns());
  82. $query = $pdo->query($query);
  83. $lines = $query->fetchAll();
  84. foreach($lines as $line){
  85. if(!isset($line['column'])){
  86. if(isset($line['name'])) $line['column'] = $line['name'];
  87. }
  88. $columns[] = $line;
  89. }
  90. return $columns;
  91. }
  92. }
  93. ?>