SoapClient4psa.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules\dns4psa;
  3. /*
  4. * 4PSA DNSManager SOAP API Client for PHP v1.1
  5. *
  6. * Class that prepares a soap client, by downloading locally the soap schemes
  7. *
  8. * Copyright (c) 2011 Rack-Soft (www.4psa.com). All rights reserved.
  9. *
  10. */
  11. use \SoapClient;
  12. class SoapClient4psa {
  13. /* the soap client */
  14. private $soap_client = null;
  15. /* the scheme files */
  16. private $scheme_files = array(
  17. 'dnsmanagerservice.wsdl',
  18. 'Common.xsd',
  19. 'HeaderData.xsd',
  20. 'Client' => array(
  21. 'Client.wsdl',
  22. 'ClientData.xsd',
  23. 'ClientMessages.xsd',
  24. 'ClientMessagesInfo.xsd',
  25. ),
  26. 'DNSZone' => array(
  27. 'DNSZone.wsdl',
  28. 'DNSZoneData.xsd',
  29. 'DNSZoneMessages.xsd',
  30. 'DNSZoneMessagesInfo.xsd',
  31. ),
  32. 'System' => array(
  33. 'System.wsdl',
  34. 'SystemData.xsd',
  35. 'SystemMessages.xsd',
  36. 'SystemMessagesInfo.xsd',
  37. ),
  38. );
  39. private $dnsmanager_ip;
  40. private $dnsmanager_port;
  41. private $dnsmanager_version;
  42. private $dnsmanager_secure;
  43. public $error;
  44. /**
  45. * The constructor of the class
  46. * Verifies each schema file if it needs to be downloaded and it downloads it, if it needs
  47. * Creates a soap client and puts it in $this->soap_client variable
  48. * @param string $local_location - the location of the local schemes, where the schemes will be downloaded or are already downloaded
  49. * @param array $options - the options used at the creation of the soap client
  50. */
  51. function __construct($local_location, $options,$dnsmanager_ip, $dnsmanager_port, $dnsmanager_secure, $dnsmanager_version) {
  52. $this->dnsmanager_ip = $dnsmanager_ip;
  53. $this->dnsmanager_port = $dnsmanager_port;
  54. $this->dnsmanager_version = $dnsmanager_version;
  55. $this->dnsmanager_secure = $dnsmanager_secure;
  56. $local_schema_subfolder = "tmp/wsdl/" . "{$this->dnsmanager_ip}" . "_{$this->dnsmanager_port}/soap/schema/{$this->dnsmanager_version}/";
  57. /* the location of the schemas on the local server */
  58. $local_schema_location = $local_location . $local_schema_subfolder;
  59. /* create the temporary folder if it does not exist */
  60. $this->create_temp_folder($local_schema_subfolder, $local_location);
  61. foreach ($this->scheme_files as $folder => $files) {
  62. if (is_array($files)) {
  63. foreach ($files as $file) {
  64. if ($this->mustUpdate($folder, $file, $local_schema_location)) {
  65. $this->update($folder, $file, $local_schema_location);
  66. }
  67. }
  68. } else {
  69. if ($this->mustUpdate(null, $files, $local_schema_location)) {
  70. $this->update(null, $files, $local_schema_location);
  71. }
  72. }
  73. }
  74. if($this->error != '')
  75. return false;
  76. /* creates the client based on the schemes downloaded locally */
  77. // $url = $local_schema_location.'/dnsmanagerservice.wsdl';
  78. // $options['location'] = $url;
  79. // $this->soap_client = new SoapClient($url, $options);
  80. $this->soap_client = new SoapClient("{$local_schema_location}dnsmanagerservice.wsdl", $options);
  81. }
  82. /**
  83. *
  84. * This method will try to create the temporary folder that contains the schema, if it does not exist. Otherwise, it does nothing
  85. *
  86. * @param <string> $schema_temp_subfolder : the subfolder that should contain the schema
  87. * @param <string> $main_temp_folder : the base path that contains the scripts and the temporary folder we will create.
  88. */
  89. function create_temp_folder($schema_temp_subfolder, $main_temp_folder) {
  90. $current_folder = $main_temp_folder;
  91. $tok = strtok($schema_temp_subfolder, "/\n");
  92. while ($tok !== false) {
  93. $current_folder = $current_folder . '/' . $tok;
  94. if(!is_dir($current_folder)) {
  95. mkdir($current_folder, 0777);
  96. }
  97. $tok = strtok("/\n");
  98. }
  99. }
  100. /**
  101. * Verifies if a local schema file must be updated or not based on mtime
  102. * @param string $folder - the parent folder of the scheme file or null if the parent folder is the version folder
  103. * @param string $file - the name of the schema file
  104. * @param string local_schema_location - the local location of the schema file
  105. * @return boolean - true if the scheme file must be downloaded, false if it doesn't
  106. */
  107. function mustUpdate($folder, $file, $local_schema_location) {
  108. /* check if the file exists locally */
  109. if (empty($folder)) {
  110. $local_filepath = $local_schema_location . $file;
  111. } else {
  112. $local_filepath = $local_schema_location . $folder . "/$file";
  113. }
  114. $glob_file = glob($local_filepath, GLOB_ERR);
  115. if (!empty($glob_file)) {
  116. /* the file exists */
  117. /* check the last time when it was modified */
  118. if (!$this->checkModifiedDate(filemtime($local_filepath))) {
  119. /* the file was modified */
  120. return false;
  121. } else {
  122. /* the file must be downloaded */
  123. return true;
  124. }
  125. } else {
  126. /* the file does not exist */
  127. return true;
  128. }
  129. }
  130. /**
  131. * Downloads a specific schema file
  132. * @param string $folder - the parent folder of the scheme file or null if the parent folder is the version folder
  133. * @param string $file - the name of the schema file
  134. * @param string local_schema_location - the local location of the schema file
  135. */
  136. function update($folder, $file, $local_schema_location) {
  137. /* the remote location of the schema file */
  138. if($this->dnsmanager_secure == 1)
  139. $remote_location = "https://";
  140. else
  141. $remote_location = "http://";
  142. $remote_location .= $this->dnsmanager_ip .':'.$this->dnsmanager_port."/soap/schema/" . $this->dnsmanager_version . "/";
  143. if (empty($folder)) {
  144. $remote_location = $remote_location . $file;
  145. $local_location = $local_schema_location . $file;
  146. $local_directory_location = $local_schema_location;
  147. } else {
  148. $remote_location = $remote_location . $folder . "/$file";
  149. $local_location = $local_schema_location . $folder . "/$file";
  150. $local_directory_location = "$local_schema_location" . $folder . "/";
  151. }
  152. /* the content of the schema file */
  153. if(!self::urlExists($remote_location)){
  154. $remote_location = str_replace('http://', 'https://', $remote_location);
  155. $this->dnsmanager_secure = 1;
  156. if(!self::urlExists($remote_location)){
  157. $this->error = "Could not create wsdl file. Please check connection parameters.";
  158. return false;
  159. }
  160. } var_dump($remote_location);
  161. $ch = curl_init();
  162. curl_setopt($ch, CURLOPT_URL, $remote_location);
  163. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  164. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  165. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  166. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  167. $content = curl_exec($ch);
  168. if ($content) {
  169. /* create the folder location */
  170. if(!is_dir($local_directory_location)) {
  171. mkdir($local_directory_location, 0777);
  172. }
  173. /* check if the folder was created */
  174. if (!glob($local_directory_location, GLOB_ERR)) {
  175. $this->error = "Could not create $local_directory_location folder. Please check if the parent directory has permissions or create the folder by yourself.";
  176. return false;
  177. } else {
  178. file_put_contents($local_location, $content);
  179. }
  180. } else {
  181. $this->error = $msg_arr['err_dnsmanager_hint'];
  182. return false;
  183. }
  184. }
  185. /**
  186. * Checks if the modify timeout interval specified in config.php has passed since a specific date moment
  187. * @param int $lastModified - the date moment, specified as a timestamp
  188. * @return true - if the interval has passed, false otherwise
  189. */
  190. function checkModifiedDate($lastModified) {
  191. $modify_timeout = 9600;
  192. /* how many hours the schemes may not be modified */
  193. $hours_interval = $modify_timeout;
  194. $seconds_interval = $hours_interval * 60 * 60;
  195. if (time() - $seconds_interval > $lastModified) {
  196. return true;
  197. } else {
  198. return false;
  199. }
  200. }
  201. /**
  202. * Gets the soap client created in the constructor
  203. * @return soapclient $this->soap_client - the soap client created in the constructor
  204. */
  205. function getSoapClient() {
  206. return $this->soap_client;
  207. }
  208. /**
  209. *
  210. * Function that checks whether a given URL exists
  211. *
  212. * @param <string> $url : the URL to be checked
  213. * @return <boolean> : true, if it exists; false, otherwise
  214. */
  215. private static function urlExists($url) {
  216. $ch = curl_init();
  217. curl_setopt($ch, CURLOPT_URL, $url);
  218. curl_setopt($ch, CURLOPT_HEADER, true);
  219. curl_setopt($ch, CURLOPT_NOBODY, true);
  220. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  221. curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
  222. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  223. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  224. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  225. $data = curl_exec($ch);
  226. curl_close($ch);
  227. preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
  228. $code = end($matches[1]);
  229. if(!$data) {
  230. return false;
  231. } else {
  232. if($code == 200) {
  233. return true;
  234. } else if($code == 404) {
  235. return false;
  236. }
  237. }
  238. }
  239. }