SophosUtm.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. class SophosUtm{
  3. public $login,$password,$host,$port;
  4. public function getVHost($ref = null){
  5. $hosts = $this->rest('GET','/objects/reverse_proxy/frontend');
  6. if(!isset($ref)) return $hosts;
  7. foreach ($hosts as $host) {
  8. if($host['_ref']==$ref) return $host;
  9. }
  10. return false;
  11. }
  12. public function setVHost($ref = 'REF_RevFroDockeWordpMulti',$vhost = array()){
  13. //On supprime les attributs readonly (débutant par _)
  14. foreach ($vhost as $key => $value)
  15. if(substr($key, 0,1)=='_') unset($vhost[$key]);
  16. $body = json_encode($vhost,JSON_PRETTY_PRINT);
  17. return $this->rest('PUT','/objects/reverse_proxy/frontend/'.$ref,$body);
  18. }
  19. public function getCertificates(){
  20. return $this->rest('GET','/objects/ca/host_key_cert/');
  21. }
  22. public function createLocation($label,$realWebServer){
  23. $attributes = array (
  24. "access_control"=> "0",
  25. "allowed_networks"=> array(
  26. "REF_NetworkAny"
  27. ),
  28. "auth_profile"=> "",
  29. "backend"=> array(
  30. $realWebServer
  31. ),
  32. "be_path"=> "",
  33. "comment"=> "",
  34. "denied_networks"=> array(),
  35. "hot_standby"=> false,
  36. "name"=> "/ (".sha1(time().'doc').")",
  37. "path"=> "/",
  38. "status"=> true,
  39. "stickysession_id"=> "ROUTEID",
  40. "stickysession_status"=> false,
  41. "websocket_passthrough"=> false
  42. );
  43. $body = json_encode($attributes,JSON_PRETTY_PRINT);
  44. return $this->rest('POST','/objects/reverse_proxy/location/',$body);
  45. }
  46. public function createVHost($label,$domains=array(),$port=80,$serverRef = '',$certificateRef = ''){
  47. $location = $this->createLocation($domains[0],$serverRef);
  48. if(empty($location['_ref'])) throw new Exception("Impossible de créer le chemin pour ce domaine, peut être existe t-il déjà dans l'utm ?");
  49. $attributes = array (
  50. 'add_content_type_header' => true,
  51. 'address' => 'REF_NetIntLiveb21223Addre3',
  52. 'allowed_networks' => array('REF_NetworkAny'),
  53. 'blockpagetheme' => '',
  54. 'certificate' => $certificateRef,
  55. 'comment' => '',
  56. 'disable_compression' => false,
  57. 'domain' => $domains,
  58. 'exceptions' => array(),
  59. 'htmlrewrite' => false,
  60. 'htmlrewrite_cookies' => true,
  61. 'implicitredirect' => true,
  62. 'lbmethod' => 'bybusyness',
  63. 'locations' => array ($location['_ref']),
  64. 'name' => $label,
  65. 'port' => $port,
  66. 'preservehost' => true,
  67. 'profile' => '',
  68. 'status' => true,
  69. 'type' => 'http'.($port==443?'s':''),
  70. 'waflocalpath' => '/.waf',
  71. 'xheaders' => false
  72. );
  73. $body = json_encode($attributes,JSON_PRETTY_PRINT);
  74. return $this->rest('POST','/objects/reverse_proxy/frontend/',$body);
  75. }
  76. //Requete rest
  77. public function rest($method,$action,$body='',$headers=array()){
  78. $url = $this->host.':'.$this->port.'/api'.$action;
  79. $ch = curl_init();
  80. $options[CURLOPT_URL] = $url;
  81. $options[CURLOPT_RETURNTRANSFER] = true;
  82. $options[CURLOPT_SSL_VERIFYPEER] = false;
  83. $options[CURLOPT_FOLLOWLOCATION] = true;
  84. $options[CURLOPT_SSL_VERIFYPEER] = false;
  85. $options[CURLOPT_USERAGENT] = 'Awesome erp';
  86. $options[CURLOPT_USERPWD] = $this->login . ":" . $this->password;
  87. $options[CURLOPT_CUSTOMREQUEST] = $method;
  88. if(!empty($body)) $options[CURLOPT_POSTFIELDS] = $body;
  89. $headers = array();
  90. $headers[] = 'Content-Type: application/json';
  91. $headers[] = 'Accept: application/json';
  92. $options[CURLOPT_HTTPHEADER] = $headers;
  93. curl_setopt_array($ch,$options);
  94. $response = curl_exec($ch);
  95. if($response === false) throw new Exception(curl_error($ch));
  96. curl_close($ch);
  97. return json_decode($response,true);
  98. }
  99. }
  100. ?>