API-Funktionen um Kontakte hochzuladen NIC-Handles werden automatisch bei Änderungen hochgeladen
Bernd Wurst

Bernd Wurst commited on 2018-01-24 10:34:58
Zeige 4 geänderte Dateien mit 78 Einfügungen und 6 Löschungen.

... ...
@@ -14,6 +14,7 @@ http://creativecommons.org/publicdomain/zero/1.0/
14 14
 Nevertheless, in case you use a significant part of this code, we ask (but not require, see the license) that you keep the authors' names in place and return your changes to the public. We would be especially happy if you tell us what you're going to do with this code.
15 15
 */
16 16
 
17
+require_once('contacts.php');
17 18
 require_once('inc/debug.php');
18 19
 
19 20
 function api_request($method, $input_data) 
... ...
@@ -39,3 +40,46 @@ function api_request($method, $input_data)
39 40
     DEBUG($output_data);
40 41
     return $output_data;
41 42
 }
43
+
44
+
45
+
46
+function contact_to_apicontact($c) 
47
+{
48
+    $ac = array();
49
+    $ac['id'] = $c['nic_id'];
50
+    $ac['handle'] = $c['nic_handle'];
51
+    $ac['type'] = 'person';
52
+    $ac['name'] = $c['name'];
53
+    $ac['organization'] = $c['company'];
54
+    $ac['street'] = explode("\n", $c['address'], 3);
55
+    $ac['postalCode'] = $c['zip'];
56
+    $ac['city'] = $c['city'];
57
+    $ac['country'] = strtolower($c['country']);
58
+    $ac['emailAddress'] = $c['email'];
59
+    $ac['phoneNumber'] = $c['phone'];
60
+    $ac['faxNumber'] = $c['fax'];
61
+
62
+    return $ac;
63
+}
64
+
65
+
66
+function upload_contact($c)
67
+{
68
+    $ac = contact_to_apicontact($c);
69
+    if ($ac['id'] || $ac['handle']) {
70
+        // Update
71
+        $data = array("contact" => $ac);
72
+        $result = api_request('contactUpdate', $data);
73
+    } else {
74
+        // create
75
+        $data = array("contact" => $ac);
76
+        $result = api_request('contactCreate', $data);
77
+        if ($result['status'] == 'success') {
78
+            $c['nic_handle'] = $result['response']['handle'];
79
+            $c['nic_id'] = $result['response']['id'];
80
+            save_contact($c);
81
+        }
82
+    }
83
+}
84
+
85
+
... ...
@@ -17,6 +17,8 @@ Nevertheless, in case you use a significant part of this code, we ask (but not r
17 17
 require_once('inc/debug.php');
18 18
 require_role(array(ROLE_CUSTOMER));
19 19
 
20
+require_once('api.php');
21
+
20 22
 /*
21 23
 Todo:
22 24
     - Wenn ein Domain-Handle aktualisiert wird, das beim Provider ändern
... ...
@@ -76,13 +78,20 @@ function possible_domainholders() {
76 78
     $allcontacts = get_contacts();
77 79
     $ret = array();
78 80
     foreach ($allcontacts as $id => $c) {
79
-        if ($c['name'] && $c['address'] && $c['zip'] && $c['city'] && $c['country'] && $c['phone'] && $c['email']) {
81
+        if (possible_domainholder($c)) {
80 82
             $ret[$id] = $c;
81 83
         }
82 84
     }
83 85
     return $ret;
84 86
 }
85 87
 
88
+function possible_domainholder($c)
89
+{
90
+    if ($c['name'] && $c['address'] && $c['zip'] && $c['city'] && $c['country'] && $c['phone'] && $c['email']) {
91
+        return true;
92
+    }
93
+    return false;
94
+}
86 95
 
87 96
 function have_mailaddress($email) 
88 97
 {
... ...
@@ -116,22 +125,25 @@ function save_emailaddress($id, $email)
116 125
 
117 126
 function save_contact($c)
118 127
 {
128
+    if ($c['nic_id']) {
129
+        if (! possible_domainholder($c)) {
130
+            system_failure("Sie haben ein Feld geleert, das für die Eigenschaft als Domaininhaber erhalten bleiben muss. Ihre Änderungen wurden nicht gespeichert.");
131
+        }
132
+    }
119 133
     for ($i=0;array_key_exists($i, $c);$i++) {
120 134
         unset($c[$i]);
121 135
     }
122 136
     unset($c['state']);
123 137
     unset($c['lastchange']);
124
-    unset($c['nic_id']);
125
-    unset($c['nic_handle']);
126 138
     unset($c['email']);
127 139
     $c['customer'] = (int) $_SESSION['customerinfo']['customerno'];
128 140
     if ($c['id']) {
129 141
         // Kontakt bestaht schon, Update
130
-        db_query("UPDATE kundendaten.contacts SET company=:company, name=:name, address=:address, zip=:zip, city=:city, country=:country, phone=:phone, mobile=:mobile, fax=:fax, pgp_id=:pgp_id, pgp_key=:pgp_key WHERE id=:id AND customer=:customer", $c);
142
+        db_query("UPDATE kundendaten.contacts SET nic_id=:nic_id, nic_handle=:nic_handle, company=:company, name=:name, address=:address, zip=:zip, city=:city, country=:country, phone=:phone, mobile=:mobile, fax=:fax, pgp_id=:pgp_id, pgp_key=:pgp_key WHERE id=:id AND customer=:customer", $c);
131 143
     } else {
132 144
         unset($c['id']);
133 145
         // Neu anlegen
134
-        db_query("INSERT INTO kundendaten.contacts (customer, company, name, address, zip, city, country, phone, mobile, fax, pgp_id, pgp_key) VALUES (:customer, :company, :name, :address, :zip, :city, :country, :phone, :mobile, :fax, :pgp_id, :pgp_key)", $c);
146
+        db_query("INSERT INTO kundendaten.contacts (nic_id, nic_handle, customer, company, name, address, zip, city, country, phone, mobile, fax, pgp_id, pgp_key) VALUES (:nic_id, :nic_handle, :customer, :company, :name, :address, :zip, :city, :country, :phone, :mobile, :fax, :pgp_id, :pgp_key)", $c);
135 147
         $c['id'] = db_insert_id();
136 148
     }
137 149
     return $c['id'];
... ...
@@ -39,8 +39,18 @@ if (! $new) {
39 39
     $c = get_contact($_REQUEST['id']);
40 40
 }
41 41
 
42
+if (!isset($_REQUEST['firma'])) {
43
+    $_REQUEST['firma'] = $c['company'];
44
+}
45
+if (!isset($_REQUEST['name'])) {
46
+    $_REQUEST['name'] = $c['name'];
47
+}
48
+if (!isset($_REQUEST['land'])) {
49
+    $_REQUEST['land'] = $c['country'];
50
+}
51
+
42 52
 if ($c['nic_handle'] != NULL) {
43
-    if (c['name'] != $_REQUEST['name'] || $c['company'] != $_REQUEST['firma'] || $c['country'] != $_REQUEST['land']) {
53
+    if ($c['name'] != $_REQUEST['name'] || $c['company'] != $_REQUEST['firma'] || $c['country'] != $_REQUEST['land']) {
44 54
         system_failure('Name/Firma/Land kann bei diesem Kontakt nicht geändert werden.');
45 55
     }
46 56
 }
... ...
@@ -103,6 +113,10 @@ if ($c['email'] != $_REQUEST['email']) {
103 113
         send_emailchange_token($c['id'], $_REQUEST['email']);
104 114
     }
105 115
 }
116
+if ($c['nic_id']) {
117
+    $c = get_contact($c['id']);
118
+    upload_contact($c);
119
+}
106 120
 
107 121
 
108 122
 if (! $debugmode)
... ...
@@ -28,6 +28,8 @@ if (isset($_REQUEST['token']))
28 28
     system_failure('Die E-Mail-Adresse konnte nicht verifiziert werden. Vielleicht ist der Link bereits abgelaufen.');
29 29
   } else  {
30 30
     update_mailaddress($daten);
31
+    $c = get_contact($daten['contact']);
32
+    upload_contact($c);
31 33
     success_msg('Die E-Mail-Adresse wurde erfolgreich geändert');
32 34
     header('Location: /');
33 35
   }
34 36