-
Notifications
You must be signed in to change notification settings - Fork 0
/
registrar_api.module
575 lines (513 loc) · 15.6 KB
/
registrar_api.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
<?php
// $Id$
/**
* @file
* The registrar_api module provides basic API calls
* to registrars with available API's
*/
/**
* Implementation of hook_help().
*/
function registrar_api_help($path, $arg) {
switch ($path) {
case 'admin/modules#name':
return t('Registrar API');
case 'admin/modules#description':
return t("Connects to various domain registrar API's to allow domain registration and management through Drupal");
case 'admin/help#registrar_api':
return '<p>' . t("Connects to various domain registrar API's to allow domain registration and management through Drupal") . '</p>';
}
}
/**
* Implementation of hook_init().
*/
function registrar_api_init() {
module_load_include('inc', 'registrar_api');
module_load_include('inc', 'registrar_api', 'registrar_api.node');
}
/**
* Implementation of hook_menu().
*/
function registrar_api_menu() {
$items['registrar/company/autocomplete'] = array(
'title' => 'Company name autocomplete',
'page callback' => 'registrar_api_company_autocomplete',
'access arguments' => array("create registrar_contact content"),
'type' => MENU_CALLBACK,
'file' => 'autocomplete.inc',
);
$items['registrar/handle/autocomplete'] = array(
'title' => 'Handle autocomplete',
'page callback' => 'registrar_api_handle_autocomplete',
'access arguments' => array("create registrar_contact content"),
'type' => MENU_CALLBACK,
'file' => 'autocomplete.inc',
);
$items['user/%user/registrar_contacts'] = array(
'title' => t('Registrar Contacts'),
'page callback' => 'registrar_api_user_page',
'page arguments' => array(1),
'access callback' => '_registrar_api_user_tab_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
$items['admin/settings/registrar_api'] = array(
'title' => 'Registrar API Settings',
'description' => 'Manage the registrar API settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('registrar_api_admin_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$registrars = registrar_api_get_registars();
foreach ($registrars as $rname => $v) {
$items['admin/settings/registrar_api/' . $rname] = array(
'title' => $v['name'],
'description' => $v['name'] . 'Manage the registrar API settings',
'page callback' => 'drupal_get_form',
'page arguments' => array($rname . '_admin_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_LOCAL_TASK,
);
}
return $items;
}
/**
* Implementation of hook_perm().
*/
function registrar_api_perm() {
return array('create registrar_contact content', 'edit own registrar_contact content', 'manage contacts');
}
/**
* Implementation of hook_access().
*/
function registrar_contact_access($op, $node, $account) {
switch ($op) {
case 'create':
return user_access('create registrar_contact content', $account);
break;
case 'update':
return user_access('edit own registrar_contact content', $account) && ($account->uid == $node->uid) || user_access('manage contacts', $account);
break;
}
}
/**
* Implementation of hook_user().
*/
function registrar_api_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'view':
if (!isset($account->content['registrar_api'])) {
$account->content['registrar_api'] = array(
'#type' => 'registrar_api',
'#name' => 'Registrar contacts',
);
}
break;
}
}
/**
* Callback function for the registrar_api admin settings form
*/
function registrar_api_admin_form($arg) {
$form = array();
$registrars = registrar_api_get_registars();
dsm($registrars);
dsm($arg);
foreach($registrars as $k => $v) {
$registrars_list[$k] = $v['name'];
}
$default_registrar = variable_get('registrar_api_default_registrar', '');
$form['registrar_api']['registrar_api_default_registrar'] = array(
'#type' => 'select',
'#title' => t('Select the registrar API to use by default'),
'#default_value' => $default_registrar,
'#options' => $registrars_list,
);
return system_settings_form($form);
}
/**
* Provide a list of all available registrars
*/
function registrar_api_get_registars() {
return module_invoke_all('get_registrar_info');
}
/**
* Split a domain name into it's TLD and name parts.
*
* @param string $domain_name
*/
function registrar_api_split_domain_name($domain_name) {
$matches = array();
preg_match('/(.*)\.(.*)$/', $domain_name, $matches);
array_shift($matches);
return $matches;
}
/**** Pages ****/
function registrar_api_user_page($user) {
$result = db_query("
SELECT * FROM {registrar_api_user_handles} handles
LEFT JOIN {registrar_api_contact} contact
ON (handles.cid = contact.cid)
LEFT JOIN {node} node
ON (contact.nid = node.nid)
WHERE handles.uid = %d", $user->uid);
$data = "";
$table_header = array('Handle', 'Contact name', 'Company');
$table_data = array();
while ($contact = db_fetch_object($result)) {
$attr = array( 'title' => _registrar_api_format_contact_info($contact));
$table_data[] = array(l($contact->title, 'node/' . $contact->nid, array( 'attributes' => $attr) ), _registrar_api_full_contact_name($contact), $contact->company_name );
}
return theme_table($table_header,$table_data);
}
/**** Private helper functions ****/
/**
* This function builds the db query set string for the values that need to be saved for a contact.
*
* @param $values An obkect or array containing the fields and values to set.
* @return an SQL SET string
*/
function _registrar_api_get_set_str($values) {
$schema = drupal_get_schema('registrar_api_contact');
$values = is_object($values) ? (array)$values : $values;
$set_arr = array();
foreach ($values as $field => $value) {
if (in_array($field, array_keys($schema['fields']))) {
$set_arr[] = $field . "='" . db_escape_string($value) . "'";
}
}
return implode(", ", $set_arr);
}
/**
* This function returns a String with the full name of a contact
*/
function _registrar_api_full_contact_name($contact) {
return $contact->lastname . ", " . $contact->nameprefix . " " . $contact->initials . " (" . $contact->firstname . ")";
}
function _registrar_api_format_contact_info($contact, $html = FALSE) {
$data = _registrar_api_full_contact_name($contact) . ($html ? "<br/>" : "\n");
$data .= $contact->company_name . ($html ? "<br/>" : "\n");
$data .= $contact->street . " " . $contact->housenumber . " " . $contact->housesuffix . ($html ? "<br/>" : "\n");
$data .= $contact->city . ", " . $contact->zipcode;
return $data;
}
function _registrar_api_user_tab_access($account) {
global $user;
// You can view your own contacts
if ($account->uid == $user->uid) {
return TRUE;
}
// You can view all contacts if you have the administer contact privilege
if (user_access('manage contacts')) {
return TRUE;
}
return FALSE;
}
/**
* Returns an array of (country code, country) pairs.
*/
function _registrar_api_country_codes() {
return array(
'AF' => t('Afghanistan'),
'AL' => t('Albania'),
'DZ' => t('Algeria'),
'AS' => t('American Samoa'),
'AD' => t('Andorra'),
'AO' => t('Angola'),
'AI' => t('Anguilla'),
'AQ' => t('Antarctica'),
'AG' => t('Antigua and Barbuda'),
'AR' => t('Argentina'),
'AM' => t('Armenia'),
'AW' => t('Aruba'),
'AU' => t('Australia'),
'AT' => t('Austria'),
'AZ' => t('Azerbaijan'),
'BS' => t('Bahamas'),
'BH' => t('Bahrain'),
'BD' => t('Bangladesh'),
'BB' => t('Barbados'),
'BY' => t('Belarus'),
'BE' => t('Belgium'),
'BZ' => t('Belize'),
'BJ' => t('Benin'),
'BM' => t('Bermuda'),
'BT' => t('Bhutan'),
'BO' => t('Bolivia'),
'BA' => t('Bosnia and Herzegovina'),
'BW' => t('Botswana'),
'BV' => t('Bouvet Island'),
'BR' => t('Brazil'),
'BN' => t('Brunei Darussalam'),
'BG' => t('Bulgaria'),
'BF' => t('Burkina Faso'),
'BI' => t('Burundi'),
'KH' => t('Cambodia'),
'CM' => t('Cameroon'),
'CA' => t('Canada'),
'CV' => t('Cape Verde'),
'KY' => t('Cayman Islands'),
'CF' => t('Central African Republic'),
'TD' => t('Chad'),
'CL' => t('Chile'),
'CN' => t('China'),
'CX' => t('Christmas Island'),
'CC' => t('Cocos (Keeling) Islands'),
'CO' => t('Colombia'),
'KM' => t('Comoros'),
'CG' => t('Congo'),
'CK' => t('Cook Islands'),
'CR' => t('Costa Rica'),
'CI' => t('Cote D\'Ivoire'),
'HR' => t('Croatia (Hrvatska)'),
'CU' => t('Cuba'),
'CY' => t('Cyprus'),
'CZ' => t('Czech Republic'),
'CS' => t('Czechoslovakia'),
'DK' => t('Denmark'),
'DJ' => t('Djibouti'),
'DM' => t('Dominica'),
'DO' => t('Dominican Republic'),
'TP' => t('East Timor'),
'EC' => t('Ecuador'),
'EG' => t('Egypt'),
'SV' => t('El Salvador'),
'GQ' => t('Equatorial Guinea'),
'ER' => t('Eritrea'),
'EE' => t('Estonia'),
'ET' => t('Ethiopia'),
'FK' => t('Falkland Islands'),
'FO' => t('Faroe Islands'),
'FJ' => t('Fiji'),
'FI' => t('Finland'),
'FR' => t('France'),
'FX' => t('France, Metropolitan'),
'GF' => t('French Guiana'),
'PF' => t('French Polynesia'),
'TF' => t('French S Territories'),
'GA' => t('Gabon'),
'GM' => t('Gambia'),
'GE' => t('Georgia'),
'DE' => t('German'),
'GH' => t('Ghana'),
'GI' => t('Gibraltar'),
'GB' => t('Great Britain (UK)'),
'GR' => t('Greece'),
'GL' => t('Greenland'),
'GD' => t('Grenada'),
'GP' => t('Guadeloupe'),
'GU' => t('Guam'),
'GT' => t('Guatemala'),
'GN' => t('Guinea'),
'GW' => t('Guinea-Bissau'),
'GY' => t('Guyana'),
'HT' => t('Haiti'),
'HM' => t('Heard and McDonald Isls'),
'HN' => t('Honduras'),
'HK' => t('Hong Kong'),
'HU' => t('Hungary'),
'IS' => t('Iceland'),
'IN' => t('India'),
'ID' => t('Indonesia'),
'IR' => t('Iran'),
'IQ' => t('Iraq'),
'IE' => t('Ireland'),
'IL' => t('Israel'),
'IT' => t('Italy'),
'JM' => t('Jamaica'),
'JP' => t('Japan'),
'JO' => t('Jordan'),
'KZ' => t('Kazakhstan'),
'KE' => t('Kenya'),
'KI' => t('Kiribati'),
'KP' => t('Korea (North)'),
'KR' => t('Korea (South)'),
'KW' => t('Kuwait'),
'KG' => t('Kyrgyzstan'),
'LA' => t('Laos'),
'LV' => t('Latvia'),
'LB' => t('Lebanon'),
'LS' => t('Lesotho'),
'LR' => t('Liberia'),
'LY' => t('Libya'),
'LI' => t('Liechtenstein'),
'LT' => t('Lithuania'),
'LU' => t('Luxembourg'),
'MO' => t('Macau'),
'MK' => t('Macedonia'),
'MG' => t('Madagascar'),
'MW' => t('Malawi'),
'MY' => t('Malaysia'),
'MV' => t('Maldives'),
'ML' => t('Mali'),
'MT' => t('Malta'),
'MH' => t('Marshall Islands'),
'MQ' => t('Martinique'),
'MR' => t('Mauritania'),
'MU' => t('Mauritius'),
'YT' => t('Mayotte'),
'MX' => t('Mexico'),
'FM' => t('Micronesia'),
'MD' => t('Moldova'),
'MC' => t('Monaco'),
'MN' => t('Mongolia'),
'ME' => t('Montenegro'),
'MS' => t('Montserrat'),
'MA' => t('Morocco'),
'MZ' => t('Mozambique'),
'MM' => t('Myanmar'),
'NA' => t('Namibia'),
'NR' => t('Nauru'),
'NP' => t('Nepal'),
'NL' => t('Netherlands'),
'AN' => t('Netherlands Antilles'),
'NT' => t('Neutral Zone'),
'NC' => t('New Caledonia'),
'NZ' => t('New Zealand'),
'NI' => t('Nicaragua'),
'NE' => t('Niger'),
'NG' => t('Nigeria'),
'NU' => t('Niue'),
'NF' => t('Norfolk Island'),
'MP' => t('Northern Mariana Isls'),
'NO' => t('Norway'),
'OM' => t('Oman'),
'PK' => t('Pakistan'),
'PW' => t('Palau'),
'PA' => t('Panama'),
'PG' => t('Papua New Guinea'),
'PY' => t('Paraguay'),
'PE' => t('Peru'),
'PH' => t('Philippines'),
'PN' => t('Pitcairn'),
'PL' => t('Poland'),
'PT' => t('Portugal'),
'PR' => t('Puerto Rico'),
'QA' => t('Qatar'),
'RE' => t('Reunion'),
'RO' => t('Romania'),
'RU' => t('Russian Federation'),
'RW' => t('Rwanda'),
'KN' => t('Saint Kitts and Nevis'),
'LC' => t('Saint Lucia'),
'WS' => t('Samoa'),
'SM' => t('San Marino'),
'ST' => t('Sao Tome and Principe'),
'SA' => t('Saudi Arabia'),
'SN' => t('Senegal'),
'SC' => t('Seychelles'),
'SL' => t('Sierra Leone'),
'SG' => t('Singapore'),
'SK' => t('Slovak Republic'),
'SI' => t('Slovenia'),
'Sb' => t('Solomon Islands'),
'SO' => t('Somalia'),
'ZA' => t('South Africa'),
'ES' => t('Spain'),
'LK' => t('Sri Lanka'),
'VC' => t('St Vincent'),
'SH' => t('St. Helena'),
'PM' => t('St. Pierre and Miquelon'),
'SD' => t('Sudan'),
'SR' => t('Suriname'),
'SZ' => t('Swaziland'),
'SE' => t('Sweden'),
'CH' => t('Switzerland'),
'SY' => t('Syria'),
'TW' => t('Taiwan'),
'TJ' => t('Tajikistan'),
'TZ' => t('Tanzania'),
'TH' => t('Thailand'),
'TG' => t('Togo'),
'TK' => t('Tokelau'),
'TO' => t('Tonga'),
'TT' => t('Trinidad and Tobago'),
'TN' => t('Tunisia'),
'TR' => t('Turkey'),
'TM' => t('Turkmenistan'),
'TC' => t('Turks and Caicos Islands'),
'TV' => t('Tuvalu'),
'UG' => t('Uganda'),
'UA' => t('Ukraine'),
'AE' => t('United Arab Emirates'),
'UK' => t('United Kingdom'),
'US' => t('United States'),
'UY' => t('Uruguay'),
'UM' => t('US Minor Outlying Isls'),
'SU' => t('USSR (former)'),
'UZ' => t('Uzbekistan'),
'VU' => t('Vanuatu'),
'VA' => t('Vatican City St'),
'VE' => t('Venezuela'),
'VN' => t('Viet Nam'),
'VG' => t('Virgin Islands (British)'),
'VI' => t('Virgin Islands (U.S.)'),
'WF' => t('Wallis and Futuna Isls'),
'EH' => t('Western Sahara'),
'YE' => t('Yemen'),
'YU' => t('Yugoslavia'),
'ZR' => t('Zaire'),
'ZM' => t('Zambia'),
'ZW' => t('Zimbabwe'),
);
}
function _registrar_api_method() {
$args = func_get_args();
$method = array_shift($args);
$registrar = variable_get('registrar_api_default_registrar', '');
$method = $registrar . '_' . $method;
return call_user_func_array($method, $args);
}
/**
* Converts the keys in each contact array to it's CamelCase equivelant.
* i.e. first_name => FirstName
*
* This is the format that namecheap needs the data to be in.
*/
function _registrar_api_convert_contacts($registrant, $tech, $admin,
$auxbilling) {
// Convert the contact details' keys to CamelCase
$contact_copy = array(
'registrant' => array(), 'tech' => array(),
'admin' => array(), 'auxbilling' => array());
foreach ($contact_copy as $type => $values) {
foreach ($$type as $key => $value) {
$newkey = str_replace('_', ' ', $key);
$newkey = str_replace(' ', '', ucwords($newkey));
$contact_copy[$type][$newkey] = $value;
}
}
return $contact_copy;
}
function _registrar_api_convert_contacts_reverse(
$registrant, $tech, $admin, $aux_billing) {
// Convert the contact details' keys from CamelCase to lower with _'s
$field_map = array(
'OrganizationName' => 'organization_name',
'JobTitle' => 'job_title',
'FirstName' => 'first_name',
'LastName' => 'last_name',
'Address1' => 'address1',
'Address2' => 'address2',
'City' => 'city',
'StateProvince' => 'state_province',
'StateProvinceChoice' => 'state_province_choice',
'PostalCode' => 'postal_code',
'Country' => 'country',
'Phone' => 'phone',
'Fax' => 'fax',
'EmailAddress' => 'email_address',
'PhoneExt' => 'phone_ext'
);
$contact_copy = array('registrant' => array(), 'tech' => array(),
'admin' => array(), 'aux_billing' => array());
foreach ($contact_copy as $type => $values) {
foreach ($$type as $key => $value) {
$newkey = $field_map[$key];
$contact_copy[$type][$newkey] = $value;
}
}
return $contact_copy;
}