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








