Developer Area

root/apps/php/trunk/Telhosting_Client.php

Revision 528, 10.6 kB (checked in by henri, 3 years ago)

Added support for getting/setting the DDS title of a domain. (by Mark Kolb <kprobe.tel)

  • Property svn:executable set to *
Line 
1#!/opt/local/bin/php -q
2<?php
3
4/**
5 * @author Pierre Clisson <clisson.tel>
6 * modified by Henri Asseily <henri.tel>
7 * modified by Swen Wojciechowski <swen.tel>
8 * modified by Mark Kolb <kprobe.tel>
9 *
10 * INSTRUCTIONS: PLEASE READ CAREFULLY
11 *
12 * 1.   Download the client WSDL from YOUR registrar, save it in the same directory as this file
13 *      and name the downloaded file: client.wsdl
14 *
15 * 1.5  Don't know where your registrar put the WSDL file? Open an command-line client on UNIX
16 *      and write:  dig _soap._nspapi.yourdomain.tel naptr
17 *      where you replace yourdomain with your actual domain. Then take either the http or https
18 *      url and append to it "?wsdl" (so it will end in "...client?wsdl")
19 *      That's the url for your registrar's WSDL file.
20 *
21 * 2.   Edit client.wsdl -- find the string: "http://www.w3.org/2003/05/soap/bindings/HTTP/" and
22 *      replace it by: "http://schemas.xmlsoap.org/soap/http". If you don't do that, PHP will
23 *      throw a fatal error: "SOAP-ERROR: Parsing WSDL: PHP-SOAP doesn't support transport
24 *      'http://www.w3.org/2003/05/soap/bindings/HTTP/'".
25 *
26 * 3.   Do *not* edit this file by correcting $config['login'] and $config['password']
27 *      if login credentials are specified in another php file which uses this file
28 *      (i.e. sample_client.php or sample_create_mx.php)
29 *
30 * Enjoy.
31 *
32 */
33
34class Telhosting_Client {
35
36        /**
37         * @var object the SOAP Client object, once connected
38         */
39        var $soap_client;
40       
41        /**
42         * @var array the config array
43         */
44        var $config;
45       
46        /**
47         * @var string last SOAP error
48         */
49        var $last_soap_error;
50       
51        /**
52         * Constructor
53         */
54        function __construct($config) {
55                // Store config
56                $this->config = $config;
57                // Do not cache WSDL. Comment this line in production environment
58                ini_set("soap.wsdl_cache_enabled", "0");
59                // Connect to the SOAP server
60                return $this->soap_connect();
61        }
62       
63        /**
64         * Connect to the SOAP server
65         *
66         * @return bool
67         */
68        function soap_connect() {
69                $options = array( 
70                        'soap_version' => SOAP_1_2,
71                        'encoding' => 'UTF-8',
72                        'login' => $this->config['login'],
73                        'password' => $this->config['password'],
74     // 'trace' => true,
75                        'exceptions' => 0
76                );
77                try {
78                        $this->soap_client = new SoapClient($this->config['wsdl'], $options);
79                } catch (SoapFault $fault) {
80                        return FALSE;
81                }
82                return TRUE;
83        }
84       
85        /**
86         * A very basic logging system
87         *
88         * @param string $type
89         * @param string $message
90         */
91        function log_message($type, $message) {
92                if ($type == 'soap') {
93                        $this->last_soap_error = $message;
94                }
95                echo "$message\n";
96        }
97       
98        /**
99         * Keyword constructor
100   *
101   * @param string $f : field name
102   * @param string $v : value
103   * @param array $k  : array of secondary keywords
104         */
105  function new_keyword($f, $v, $k = NULL) {
106    $self = new stdClass();
107    $self->field = $f;
108    $self->value = $v;
109    if ($k != NULL) {
110      $self->keyword = $k;
111    }
112    return $self;
113  }
114
115
116        /**
117         * List all methdods
118         */
119        function list_methods() {
120                $response = $this->soap_client->__getFunctions();
121                if (is_soap_fault($response)) {
122                        $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
123                        return FALSE;
124                } else {
125                        return $response;
126                }
127        }
128       
129        /**
130         * List domains
131         */
132        function list_domains() {
133                $response = $this->soap_client->listDomains();
134                if (is_soap_fault($response)) {
135                        $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
136                        return FALSE;
137                } else {
138                        return $response;
139                }
140        }
141
142  /**
143   * Get a Specific Domain's Information (DDS Title, etc...)
144   *
145   * @param string $domain
146   */
147  function get_domain($domain) {
148    $params = array('domainName' => $domain, 'effUserName' => '');
149    $response = $this->soap_client->getDomain($params);
150    if (is_soap_fault($response)) {
151      $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
152      return FALSE;
153    } else {
154      return $response;
155    }
156  }
157
158  /**
159   * Update Domain Information (Title, etc...)
160   *
161   * @param string $domain
162   * @param string $title DDS tile
163   */
164  function update_domain($domain,$title) {
165    $params = array('domainName' => $domain, 'displayString' => $title);
166    $response = $this->soap_client->updateDomain($params);
167    if (is_soap_fault($response)) {
168      $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
169      return FALSE;
170    } else {
171      return $response;
172    }
173  }
174
175        /**
176         * Create domain
177         * Note that this method does NOT create a link to the newly created subdomain
178   * You need to explicitly call store_record() and create a non-terminal NAPTR to point to it
179   *
180         * @param string $domain
181         */
182        function create_domain($domain) {
183                $params = array('domainName' => $domain);
184                $response = $this->soap_client->createDomain($params);
185                if (is_soap_fault($response)) {
186                        $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
187                        return FALSE;
188                } else {
189                        return $response;
190                }
191        }
192
193        /**
194         * Delete domain
195   *
196         * @param string $domain
197         */
198        function delete_domain($domain) {
199                $params = array('domainName' => $domain);
200                $response = $this->soap_client->deleteDomain($params);
201                if (is_soap_fault($response)) {
202                        $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
203                        return FALSE;
204                } else {
205                        return $response;
206                }
207        }
208
209        /**
210         * List profiles
211         *
212         * @param string $domain
213         */
214        function list_profiles($domain) {
215                $params = array('domainName' => $domain);
216                $response = $this->soap_client->listProfiles($params);
217                if (is_soap_fault($response)) {
218                        $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
219                        return FALSE;
220                } else {
221                        return $response;
222                }
223        }
224
225
226        /**
227         * get active profile
228         *
229         * @param string $domain
230         */
231        function get_active_profile($domain) {
232                $params = array('domainName' => $domain);
233                $response = $this->soap_client->getActiveProfile($params);
234                if (is_soap_fault($response)) {
235                        $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
236                        return FALSE;
237                } else {
238                        return $response;
239                }
240        }
241
242
243        /**
244         * List records
245         *
246         * @param string $domain
247         */
248        function list_records($domain) {
249                $params = array('domainName' => $domain);
250                $response = $this->soap_client->listRecords($params);
251                if (is_soap_fault($response)) {
252                        $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
253                        return FALSE;
254                } else {
255                        return $response;
256                }
257        }
258       
259        /**
260         * Delete a record
261         *
262         * @param string $domain
263         * @param int $id
264         */
265        function delete_record($domain, $id) {
266                $params = array(
267                        'domainName' => $domain,
268                        'id' => $id
269                );
270                $response = $this->soap_client->deleteRecord($params);
271                if (is_soap_fault($response)) {
272                        $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
273                        return FALSE;
274                } else {
275                        return $response;
276                }
277        }
278       
279        /**
280         * Insert a record
281         *
282         * @param string $domain
283         * @param string $type txt or naptr
284         * @param array $data
285         */
286        function store_record($domain, $type, $data) {
287                $params = array(
288                        'domainName' => $domain,
289                        $type => $data
290                );
291                $response = $this->soap_client->storeRecord($params);
292                if (is_soap_fault($response)) {
293                        $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
294                        return FALSE;
295                } else {
296                        return $response;
297                }       
298        }
299
300  // Keyword management
301
302        /**
303         * List keywords
304         *
305         * @param string $domain
306         */
307        function get_search_data($domain) {
308                $params = array('domainName' => $domain);
309                $response = $this->soap_client->getSearchData($params);
310                if (is_soap_fault($response)) {
311                        $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
312                        return FALSE;
313                } else {
314                        return $response;
315                }
316        }
317
318  function keyword_to_string($k) {
319    $s = $k->field . ': ' . $k->value;
320    if (isset($k->keyword)) {
321      foreach($k->keyword as $value) {
322        $s = $s . ' - ' . $this->keyword_to_string($value);
323      }
324    }
325    return $s;
326  }
327        /**
328         * Replace ALL keywords
329         *
330         * @param string $domain
331         * @param array $data
332   * @param bool $removeDuplicates
333   *
334   * inside $data: an array of all keywords
335   * Set $removeDuplicates to FALSE to disable removal of duplicate keywords
336   * $removeDuplicates is set to TRUE by default
337         */
338        function set_search_data($domain, $data, $removeDuplicates = TRUE) {
339    if ($removeDuplicates) {
340      $unik = array();
341      foreach($data->keyword as $value) {
342        $ks = $this->keyword_to_string($value);
343        if (isset($unik[$ks])) {
344          continue;
345        }
346        $unik[$ks] = 1;
347        $newkeyword[] = $value;
348      }
349      $data->keyword = $newkeyword;
350    }
351                $params = array(
352                        'domainName' => $domain,
353                        'searchData' => $data
354                );
355                $response = $this->soap_client->setSearchData($params);
356                if (is_soap_fault($response)) {
357                        $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
358                        return FALSE;
359                } else {
360                        return $response;
361                }       
362        }
363
364
365        /**
366         * Export Data
367         *
368         * @param array $domains
369         * @param array $zones
370         */
371        function export_data($domains = '', $zones = '') {
372    $params = array();
373    if ($domains != '') {
374      $params['domains'] = $domains;
375    }
376    if ($zones != '') {
377      $params['zones'] = $zones;
378    }
379                $response = $this->soap_client->exportData($params);
380                if (is_soap_fault($response)) {
381                        $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
382                        return FALSE;
383                } else {
384                        return $response;
385                }
386        }
387
388
389        /**
390         * Import Data
391         *
392         * @param array $domains
393         * @param array $zones
394         * @param array $data
395         */
396        function import_data($domains = '', $zones = '', $data = '', $overwrite = TRUE) {
397    $params = array(
398        'overwrite' => $overwrite,
399        'data' => $data
400        );
401    if ($domains != '') {
402      $params['domains'] = $domains;
403    }
404    if ($zones != '') {
405      $params['zones'] = $zones;
406    }
407                $response = $this->soap_client->importData($params);
408                if (is_soap_fault($response)) {
409                        $this->log_message('soap', "SOAP Fault: (faultcode: {". $response->faultcode ."}, faultstring: {".$response->faultstring."})");
410                        return FALSE;
411                } else {
412                        return $response;
413                }
414        }
415}
416?>
Note: See TracBrowser for help on using the browser.
Telnic
Search This Site
Partners
Neustar
ICANN
Main site | WHOIS | Sell .tel | FAQ | Archived Site | About Telnic | Contact Us