| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- namespace MGModule\DNSManager2\mgLibs\custom\dns\submodules\dns4psa;
- /*
- * 4PSA DNSManager SOAP API Client for PHP v1.1
- *
- * Class that prepares a soap client, by downloading locally the soap schemes
- *
- * Copyright (c) 2011 Rack-Soft (www.4psa.com). All rights reserved.
- *
- */
- use \SoapClient;
- class SoapClient4psa {
- /* the soap client */
- private $soap_client = null;
- /* the scheme files */
- private $scheme_files = array(
- 'dnsmanagerservice.wsdl',
- 'Common.xsd',
- 'HeaderData.xsd',
- 'Client' => array(
- 'Client.wsdl',
- 'ClientData.xsd',
- 'ClientMessages.xsd',
- 'ClientMessagesInfo.xsd',
- ),
- 'DNSZone' => array(
- 'DNSZone.wsdl',
- 'DNSZoneData.xsd',
- 'DNSZoneMessages.xsd',
- 'DNSZoneMessagesInfo.xsd',
- ),
- 'System' => array(
- 'System.wsdl',
- 'SystemData.xsd',
- 'SystemMessages.xsd',
- 'SystemMessagesInfo.xsd',
- ),
-
- );
-
- private $dnsmanager_ip;
- private $dnsmanager_port;
- private $dnsmanager_version;
- private $dnsmanager_secure;
-
- public $error;
- /**
- * The constructor of the class
- * Verifies each schema file if it needs to be downloaded and it downloads it, if it needs
- * Creates a soap client and puts it in $this->soap_client variable
- * @param string $local_location - the location of the local schemes, where the schemes will be downloaded or are already downloaded
- * @param array $options - the options used at the creation of the soap client
- */
- function __construct($local_location, $options,$dnsmanager_ip, $dnsmanager_port, $dnsmanager_secure, $dnsmanager_version) {
- $this->dnsmanager_ip = $dnsmanager_ip;
- $this->dnsmanager_port = $dnsmanager_port;
- $this->dnsmanager_version = $dnsmanager_version;
- $this->dnsmanager_secure = $dnsmanager_secure;
-
- $local_schema_subfolder = "tmp/wsdl/" . "{$this->dnsmanager_ip}" . "_{$this->dnsmanager_port}/soap/schema/{$this->dnsmanager_version}/";
- /* the location of the schemas on the local server */
- $local_schema_location = $local_location . $local_schema_subfolder;
- /* create the temporary folder if it does not exist */
- $this->create_temp_folder($local_schema_subfolder, $local_location);
-
-
- foreach ($this->scheme_files as $folder => $files) {
- if (is_array($files)) {
- foreach ($files as $file) {
- if ($this->mustUpdate($folder, $file, $local_schema_location)) {
- $this->update($folder, $file, $local_schema_location);
- }
- }
- } else {
- if ($this->mustUpdate(null, $files, $local_schema_location)) {
- $this->update(null, $files, $local_schema_location);
- }
- }
- }
- if($this->error != '')
- return false;
- /* creates the client based on the schemes downloaded locally */
- // $url = $local_schema_location.'/dnsmanagerservice.wsdl';
- // $options['location'] = $url;
- // $this->soap_client = new SoapClient($url, $options);
- $this->soap_client = new SoapClient("{$local_schema_location}dnsmanagerservice.wsdl", $options);
- }
- /**
- *
- * This method will try to create the temporary folder that contains the schema, if it does not exist. Otherwise, it does nothing
- *
- * @param <string> $schema_temp_subfolder : the subfolder that should contain the schema
- * @param <string> $main_temp_folder : the base path that contains the scripts and the temporary folder we will create.
- */
- function create_temp_folder($schema_temp_subfolder, $main_temp_folder) {
- $current_folder = $main_temp_folder;
- $tok = strtok($schema_temp_subfolder, "/\n");
- while ($tok !== false) {
- $current_folder = $current_folder . '/' . $tok;
- if(!is_dir($current_folder)) {
- mkdir($current_folder, 0777);
- }
- $tok = strtok("/\n");
- }
- }
- /**
- * Verifies if a local schema file must be updated or not based on mtime
- * @param string $folder - the parent folder of the scheme file or null if the parent folder is the version folder
- * @param string $file - the name of the schema file
- * @param string local_schema_location - the local location of the schema file
- * @return boolean - true if the scheme file must be downloaded, false if it doesn't
- */
- function mustUpdate($folder, $file, $local_schema_location) {
- /* check if the file exists locally */
- if (empty($folder)) {
- $local_filepath = $local_schema_location . $file;
- } else {
- $local_filepath = $local_schema_location . $folder . "/$file";
- }
- $glob_file = glob($local_filepath, GLOB_ERR);
- if (!empty($glob_file)) {
- /* the file exists */
- /* check the last time when it was modified */
- if (!$this->checkModifiedDate(filemtime($local_filepath))) {
- /* the file was modified */
- return false;
- } else {
- /* the file must be downloaded */
- return true;
- }
- } else {
- /* the file does not exist */
- return true;
- }
- }
-
- /**
- * Downloads a specific schema file
- * @param string $folder - the parent folder of the scheme file or null if the parent folder is the version folder
- * @param string $file - the name of the schema file
- * @param string local_schema_location - the local location of the schema file
- */
- function update($folder, $file, $local_schema_location) {
- /* the remote location of the schema file */
- if($this->dnsmanager_secure == 1)
- $remote_location = "https://";
- else
- $remote_location = "http://";
-
- $remote_location .= $this->dnsmanager_ip .':'.$this->dnsmanager_port."/soap/schema/" . $this->dnsmanager_version . "/";
- if (empty($folder)) {
- $remote_location = $remote_location . $file;
- $local_location = $local_schema_location . $file;
- $local_directory_location = $local_schema_location;
- } else {
- $remote_location = $remote_location . $folder . "/$file";
- $local_location = $local_schema_location . $folder . "/$file";
- $local_directory_location = "$local_schema_location" . $folder . "/";
- }
-
- /* the content of the schema file */
- if(!self::urlExists($remote_location)){
- $remote_location = str_replace('http://', 'https://', $remote_location);
- $this->dnsmanager_secure = 1;
- if(!self::urlExists($remote_location)){
- $this->error = "Could not create wsdl file. Please check connection parameters.";
- return false;
- }
- } var_dump($remote_location);
-
-
- $ch = curl_init();
-
- curl_setopt($ch, CURLOPT_URL, $remote_location);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
-
- $content = curl_exec($ch);
-
- if ($content) {
- /* create the folder location */
- if(!is_dir($local_directory_location)) {
- mkdir($local_directory_location, 0777);
- }
- /* check if the folder was created */
- if (!glob($local_directory_location, GLOB_ERR)) {
- $this->error = "Could not create $local_directory_location folder. Please check if the parent directory has permissions or create the folder by yourself.";
- return false;
- } else {
- file_put_contents($local_location, $content);
-
- }
- } else {
- $this->error = $msg_arr['err_dnsmanager_hint'];
- return false;
- }
-
- }
-
- /**
- * Checks if the modify timeout interval specified in config.php has passed since a specific date moment
- * @param int $lastModified - the date moment, specified as a timestamp
- * @return true - if the interval has passed, false otherwise
- */
- function checkModifiedDate($lastModified) {
- $modify_timeout = 9600;
- /* how many hours the schemes may not be modified */
- $hours_interval = $modify_timeout;
- $seconds_interval = $hours_interval * 60 * 60;
-
- if (time() - $seconds_interval > $lastModified) {
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Gets the soap client created in the constructor
- * @return soapclient $this->soap_client - the soap client created in the constructor
- */
- function getSoapClient() {
- return $this->soap_client;
- }
- /**
- *
- * Function that checks whether a given URL exists
- *
- * @param <string> $url : the URL to be checked
- * @return <boolean> : true, if it exists; false, otherwise
- */
- private static function urlExists($url) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, true);
- curl_setopt($ch, CURLOPT_NOBODY, true);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_TIMEOUT, 5);
- $data = curl_exec($ch);
- curl_close($ch);
- preg_match_all("/HTTP\/1\.[1|0]\s(\d{3})/",$data,$matches);
- $code = end($matches[1]);
- if(!$data) {
- return false;
- } else {
- if($code == 200) {
- return true;
- } else if($code == 404) {
- return false;
- }
- }
- }
- }
|