├── README.md ├── .gitignore ├── config.sample.php ├── importer.php └── classes ├── NamiConnector.php ├── NamiCleverreachConnector.php └── rest_client.php /README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | config.php 3 | index.php 4 | mailtest/ 5 | wpcs/ -------------------------------------------------------------------------------- /config.sample.php: -------------------------------------------------------------------------------- 1 | compare_nami_and_cleverreach( 'pfadfinder', 958461 ); 18 | $nami_cleverreach_connector->reset_members(); //Reset Stored data to get ready for next import -------------------------------------------------------------------------------- /classes/NamiConnector.php: -------------------------------------------------------------------------------- 1 | https = $https; 14 | $this->url = $url; 15 | $this->Login = "API"; 16 | //$this->username = $credentials["username"]"78175"; // NamiNumber 17 | //$this->password = $credentials["password"]; 18 | $this->curlOptions = array("CURLOPT_COOKIEJAR" => COOKIE_PATH ,"CURLOPT_COOKIEFILE" => COOKIE_PATH); 19 | //$this->curlOptions = array("CURLOPT_COOKIEJAR" => "/www/htdocs/w00f0942/tmp/cookie.txt" ,"CURLOPT_COOKIEFILE" => "/www/htdocs/w00f0942/tmp/cookie.txt"); 20 | 21 | //$this->gid = $this->get_groupid(); 22 | $this->gid = "070901"; 23 | } 24 | 25 | function call_api($service,$fields = null) { 26 | 27 | if ($this->https == true) { 28 | $serviceurl = "https://" . $this->url . $service; 29 | } else { 30 | $serviceurl = "http://" . $this->url . $service; 31 | } 32 | 33 | $ch = curl_init(); 34 | 35 | // setze die URL und andere Optionen 36 | curl_setopt($ch, CURLOPT_URL, $serviceurl); 37 | 38 | if (isset($fields)) { 39 | foreach ($fields as $key => $value) 40 | { 41 | $post_data[$key] = urlencode($value); 42 | } 43 | 44 | //url-ify the data for the POST 45 | $fields_string = ''; 46 | foreach($post_data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; /*echo 'test';*/} 47 | rtrim($fields_string,'&'); 48 | 49 | curl_setopt($ch, CURLOPT_POST, count($post_data)); 50 | curl_setopt($ch, CURLOPT_POSTFIELDS,$fields_string); 51 | } 52 | 53 | curl_setopt($ch, CURLOPT_COOKIEFILE, $this->curlOptions["CURLOPT_COOKIEFILE"]); 54 | curl_setopt($ch, CURLOPT_COOKIEJAR, $this->curlOptions["CURLOPT_COOKIEJAR"]); 55 | 56 | //curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: JSESSIONID=gFUp0xsJeOFSo5e49w-Ryn9n.undefined")); 57 | 58 | 59 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 60 | curl_setopt($ch, CURLOPT_HEADER, false); 61 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 62 | 63 | if ($this->https == true) { 64 | // These options are for https!!! 65 | // Turns off certificate verification --- be carefull! 66 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 67 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 68 | curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 ); 69 | curl_setopt($ch, CURLOPT_PORT , 443); 70 | } 71 | 72 | if(curl_exec($ch) === false) 73 | { 74 | $error ='Curl-Fehler: ' . curl_error($ch); 75 | } 76 | else 77 | { 78 | // führe die Aktion aus und gebe die Daten an den Browser weiter 79 | $result = curl_exec($ch); 80 | //var_dump($result); 81 | $result = json_decode($result , true); 82 | //print_r($result); 83 | $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); 84 | //print_r($http_status); 85 | if ( isset($result["statusCode"] ) && $result["statusCode"] <> 0 ) { $error = 'Api-Fehler: ' . $result["statusMessage"];} 86 | } 87 | // var_dump($result); 88 | 89 | if (isset($error)) {echo $error; echo 'error';} 90 | else {return($result);} 91 | 92 | // schließe den cURL-Handle und gebe die Systemresourcen frei 93 | curl_close($ch); 94 | } 95 | 96 | function login($credentials) { 97 | 98 | // preparing the post data 99 | $fields = array( 100 | 'Login' => $this->Login, 101 | 'username' => $credentials["username"], 102 | 'password' => $credentials["password"], 103 | 'redirectTo' => 'app.jsp', 104 | 105 | ); 106 | 107 | return $this->call_api("/ica/rest/nami/auth/manual/sessionStartup",$fields); 108 | } 109 | 110 | function get_groups() { 111 | //$result = $this->call_api("/ica/rest/orgadmin/gruppierung/flist?_dc=1353416298860&page=1&start=0&limit=4000"); 112 | $result = $this->call_api("/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root?_dc=1477418659231&sort=%5B%7B%22property%22%3A%22leaf%22%2C%22direction%22%3A%22ASC%22%7D%5D&node=root"); 113 | //return $result; 114 | //var_dump($result); 115 | $response = $result; 116 | if ($response["responseType"] == "EXCEPTION") {echo $response["message"];} else { 117 | return $response; 118 | } 119 | } 120 | 121 | 122 | function get_groupid() { 123 | // Holt die Gruppierungs-ID des Stammes aus der Nami 124 | $response = $this->get_groups(); 125 | $gruppierungen = $response["data"]; 126 | 127 | print_r($gruppierungen); 128 | 129 | /*foreach($gruppierungen as $gruppierung) { 130 | if (substr_count($gruppierung["descriptor"],"Essen-Stoppenberg, St. Nikolaus") == 1) { 131 | return $gruppierung["id"]; 132 | } else { 133 | echo "Fehler: Der Stamm konnte in der Nami nicht gefunden werden."; 134 | } 135 | }*/ 136 | } 137 | 138 | function get_members( $filterString = '', $searchString = '' ) { 139 | $result = $this->call_api("/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/". $this->gid ."/flist?_dc=1477420805317&page=1&start=0&limit=500&filterString=" . $filterString . "&searchString=" . $searchString); 140 | //var_dump($result); 141 | $response = $result; 142 | if ($response["responseType"] == "EXCEPTION") {echo $response["message"];} else {return $response["data"];} 143 | } 144 | 145 | function get_memberdata($name) { 146 | $members = $this->get_members(); 147 | 148 | foreach ($members as $member) { 149 | if (substr_count($member["descriptor"],$name) == 1) { 150 | $data = $member["entries"]; 151 | return $data; 152 | } 153 | } 154 | } 155 | 156 | function get_membernumber($name) { 157 | $data = $this->get_memberdata($name); 158 | return $data["mitgliedsNummer"]; 159 | } 160 | } 161 | ?> -------------------------------------------------------------------------------- /classes/NamiCleverreachConnector.php: -------------------------------------------------------------------------------- 1 | nami_password = $nami_password; 25 | $this->nami_username = $nami_username; 26 | $this->cleverreach_client_id = $cleverreach_client_id; 27 | $this->cleverreach_login = $cleverreach_login; 28 | $this->cleverreach_password = $cleverreach_password; 29 | 30 | $this->connect_to_apis(); 31 | } 32 | 33 | private function connect_to_apis(){ 34 | 35 | $this->nami_login(); 36 | $this->cleverreach_login(); 37 | 38 | } 39 | 40 | 41 | private function cleverreach_login() { 42 | 43 | $this->cleverReachConnector = new CR\tools\rest("https://rest.cleverreach.com/v1"); 44 | $this->cleverReachConnector->throwExceptions = true; //default 45 | 46 | $token = $this->cleverReachConnector->post('/login', 47 | array( 48 | "client_id"=> $this->cleverreach_client_id, 49 | "login"=> $this->cleverreach_login, 50 | "password"=>$this->cleverreach_password 51 | ) 52 | ); 53 | //no error, lets use the key 54 | $this->cleverReachConnector->setAuthMode("jwt", $token); 55 | 56 | } 57 | 58 | private function nami_login() { 59 | 60 | $this->nami = new NamiConnector( true, 'nami.dpsg.de' ); 61 | $this->nami->login( array( 62 | "username" => $this->nami_username, 63 | "password" => $this->nami_password 64 | ) 65 | ); 66 | 67 | } 68 | 69 | private function get_group_receivers( $group_id ) { 70 | 71 | //echo "### Return all receivers from group ###\n"; 72 | $receivers = $this->cleverReachConnector->get("/groups/" . $group_id . "/receivers"); 73 | foreach ($receivers as $receiver) { 74 | $this->cleverreach_group_receivers[] = $receiver->email; 75 | } 76 | 77 | //var_dump( $this->cleverreach_group_receivers ); 78 | 79 | } 80 | 81 | 82 | private function get_nami_members( $filterString, $searchString ) { 83 | $this->nami_members = $this->nami->get_members( $filterString, $searchString ); 84 | 85 | foreach ($this->nami_members as $member) { 86 | echo $member['descriptor'] . '
'; 87 | } 88 | } 89 | 90 | public function compare_nami_and_cleverreach( $filter, $searchString = '', $groupid ) { 91 | $this->get_group_receivers( $groupid ); 92 | $this->get_nami_members($filter, $searchString); 93 | 94 | foreach ( $this->nami_members as $member ) { 95 | 96 | if (!empty($member["entries"]["emailVertretungsberechtigter"])) { 97 | 98 | /*$this->all_nami_member_mails[] = $member["entries"]["emailVertretungsberechtigter"]; 99 | 100 | // Check "Vertretungsberechtigter" 101 | if ( in_array( $member["entries"]["emailVertretungsberechtigter"], $this->cleverreach_group_receivers ) ) { 102 | $this->updated_reveivers[] = array( 103 | 104 | "email" => $member["entries"]["emailVertretungsberechtigter"], 105 | ); 106 | }else{ 107 | $this->new_reveivers[] = array( 108 | 109 | "email" => $member["entries"]["emailVertretungsberechtigter"], 110 | "registered" => time(), //current date 111 | "activated" => time(), 112 | "global_attributes" => array( 113 | "mitgliedsNummer" => $member["entries"]["mitgliedsNummer"], 114 | "Vertretungsberechtigter" => "0", 115 | ), 116 | ); 117 | }*/ 118 | } 119 | 120 | // Check Mitglied 121 | 122 | if ( !empty( $member["entries"]["email"] ) ) { 123 | 124 | $this->all_nami_member_mails[] = $member["entries"]["email"]; 125 | 126 | if ( in_array( $member["entries"]["email"] , $this->cleverreach_group_receivers ) ) { 127 | $this->updated_reveivers[] = array( 128 | "email" => $member["entries"]["email"], 129 | ); 130 | }else{ 131 | 132 | $this->new_reveivers[] = array( 133 | "email" => $member["entries"]["email"], 134 | "registered" => time(), //current date 135 | "activated" => time(), 136 | "attributes" => array( 137 | "firstname" => $member["entries"]["vorname"], 138 | "lastname" => $member["entries"]["nachname"], 139 | ), 140 | "global_attributes" => array( 141 | "mitgliedsNummer" => $member["entries"]["mitgliedsNummer"], 142 | "Vertretungsberechtigter" => "0", 143 | ), 144 | ); 145 | } 146 | 147 | } //check if mail is empty 148 | 149 | } 150 | 151 | echo "

Updated Reveivers

"; 152 | var_dump($this->updated_reveivers); 153 | 154 | echo "

New Reveivers

"; 155 | var_dump($this->new_reveivers); 156 | 157 | $this->define_deprecated_mails(); 158 | 159 | echo "

Deprecated Reveivers

"; 160 | var_dump( $this->deprecated_mails ); 161 | 162 | //$this->add_new_receivers( $groupid ); 163 | $this->delete_deprecated_receivers( $groupid ); 164 | 165 | } 166 | 167 | private function define_deprecated_mails() { 168 | var_dump($this->cleverreach_group_receivers); 169 | foreach ( $this->cleverreach_group_receivers as $group_receiver ) { 170 | if ( !in_array($group_receiver, $this->all_nami_member_mails )) { 171 | $this->deprecated_mails[] = $group_receiver; 172 | } 173 | } 174 | 175 | 176 | } 177 | 178 | 179 | private function add_new_receivers( $groupid ) { 180 | 181 | try { 182 | 183 | $this->cleverReachConnector->post("/groups/{$groupid}/receivers", $this->new_reveivers); 184 | 185 | } catch (\Exception $e){ 186 | echo "!!!! Batcomputer Error: {$this->cleverReachConnector->error} !!!!"; 187 | exit(); 188 | } 189 | 190 | } 191 | 192 | private function delete_deprecated_receivers( $groupid ) { 193 | 194 | if ( count( $this->deprecated_mails) > 0 ) { 195 | # code... 196 | foreach ($this->deprecated_mails as $mail) { 197 | try { 198 | $this->cleverReachConnector->put("/groups/{$groupid}/receivers/{$mail}/setinactive", array( "email" => $mail )); 199 | 200 | } catch (\Exception $e){ 201 | echo "!!!! Batcomputer Error: {$this->cleverReachConnector->error} !!!!"; 202 | exit(); 203 | } 204 | } 205 | } 206 | 207 | 208 | } 209 | 210 | 211 | public function reset_members(){ 212 | $this->cleverreach_group_receivers = array(); 213 | $this->deprecated_mails = array(); 214 | $this->new_reveivers = array(); 215 | $this->updated_reveivers = array(); 216 | $this->all_nami_member_mails = array(); 217 | } 218 | 219 | } -------------------------------------------------------------------------------- /classes/rest_client.php: -------------------------------------------------------------------------------- 1 | url = rtrim($url, '/'); 27 | $this->authModeSettings = new \stdClass; 28 | $this->debugValues = new \stdClass; 29 | 30 | } 31 | 32 | /** 33 | * sets AuthMode (jwt, webauth, etc) 34 | * @param string jwt, webauth,none 35 | * @param mixed 36 | */ 37 | public function setAuthMode($mode = "none", $value = false) 38 | { 39 | switch ($mode) { 40 | case 'jwt': 41 | $this->authMode = "jwt"; 42 | $this->authModeSettings->token = $value; 43 | break; 44 | 45 | case 'bearer': 46 | $this->authMode = "bearer"; 47 | $this->authModeSettings->token = $value; 48 | break; 49 | 50 | case 'webauth': 51 | $this->authMode = "webauth"; 52 | $this->authModeSettings->login = $value->login; 53 | $this->authModeSettings->password = $value->password; 54 | 55 | break; 56 | 57 | default: 58 | # code... 59 | break; 60 | } 61 | } 62 | 63 | ################################################################################################ 64 | 65 | /** 66 | * makes a GET call 67 | * @param array 68 | * @param string get/put/delete 69 | * @return mixed 70 | */ 71 | public function get($path, $data = false, $mode = "get") 72 | { 73 | $this->resetDebug(); 74 | if (is_string($data)) { 75 | if (!$data = json_decode($data)) { 76 | throw new \Exception("data is string but no JSON"); 77 | } 78 | } 79 | 80 | $url = sprintf("%s?%s", $this->url . $path, ($data ? http_build_query($data) : "")); 81 | $this->debug("url", $url); 82 | 83 | $curl = curl_init($url); 84 | $this->setupCurl($curl); 85 | 86 | switch ($mode) { 87 | case 'delete': 88 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, strtoupper($mode)); 89 | $this->debug("mode", strtoupper($mode)); 90 | break; 91 | 92 | default: 93 | $this->debug("mode", "GET"); 94 | break; 95 | } 96 | 97 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 98 | $curl_response = curl_exec($curl); 99 | $headers = curl_getinfo($curl); 100 | curl_close($curl); 101 | 102 | $this->debugEndTimer(); 103 | 104 | return $this->returnResult($curl_response, $headers); 105 | 106 | } 107 | 108 | /** 109 | * makes a DELETE call 110 | * @param array 111 | * @return mixed 112 | */ 113 | public function delete($path, $data = false) 114 | { 115 | return $this->get($path, $data, "delete"); 116 | } 117 | 118 | /** 119 | * makes a put call 120 | * @param array 121 | * @return mixed 122 | */ 123 | public function put($path, $data = false) 124 | { 125 | return $this->post($path, $data, "put"); 126 | } 127 | 128 | /** 129 | * does POST 130 | * @param [type] 131 | * @return [type] 132 | */ 133 | public function post($path, $data, $mode = "post") 134 | { 135 | $this->resetDebug(); 136 | $this->debug("url", $this->url . $path); 137 | if (is_string($data)) { 138 | if (!$data = json_decode($data)) { 139 | throw new \Exception("data is string but no JSON"); 140 | } 141 | } 142 | $curl_post_data = $data; 143 | 144 | $curl = curl_init($this->url . $path); 145 | $this->setupCurl($curl); 146 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 147 | 148 | switch ($mode) { 149 | case 'put': 150 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); 151 | break; 152 | 153 | default: 154 | curl_setopt($curl, CURLOPT_POST, true); 155 | break; 156 | } 157 | 158 | $this->debug("mode", strtoupper($mode)); 159 | 160 | if ($this->postFormat == "json") { 161 | $curl_post_data = json_encode($curl_post_data); 162 | } 163 | 164 | curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data); 165 | $curl_response = curl_exec($curl); 166 | $headers = curl_getinfo($curl); 167 | curl_close($curl); 168 | 169 | $this->debugEndTimer(); 170 | return $this->returnResult($curl_response, $headers); 171 | 172 | } 173 | 174 | ########################################################################## 175 | 176 | /** 177 | * [resetDebug description] 178 | * @return [type] 179 | */ 180 | private function resetDebug() 181 | { 182 | $this->debugValues = new \stdClass; 183 | $this->error = false; 184 | $this->debugStartTimer(); 185 | } 186 | 187 | /** 188 | * set debug keys 189 | * @param string 190 | * @param mixed 191 | * @return [type] 192 | */ 193 | private function debug($key, $value) 194 | { 195 | $this->debugValues->$key = $value; 196 | } 197 | 198 | private function debugStartTimer() 199 | { 200 | $this->debugValues->time = $this->microtime_float(); 201 | } 202 | 203 | private function debugEndTimer() 204 | { 205 | $this->debugValues->time = $this->microtime_float() - $this->debugValues->time; 206 | } 207 | 208 | /** 209 | * prepapres curl with settings amd ein object 210 | * @param pointer_curl 211 | */ 212 | private function setupCurl(&$curl) 213 | { 214 | 215 | $header = array(); 216 | 217 | switch ($this->postFormat) { 218 | case 'json': 219 | $header['content'] = 'Content-Type: application/json'; 220 | break; 221 | 222 | default: 223 | $header['content'] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8'; 224 | break; 225 | } 226 | 227 | switch ($this->authMode) { 228 | case 'webauth': 229 | curl_setopt($curl, CURLOPT_USERPWD, $this->authModeSettings->login . ":" . $this->authModeSettings->password); 230 | break; 231 | 232 | case 'jwt': 233 | $header['token'] = 'X-ACCESS-TOKEN: ' . $this->authModeSettings->token; 234 | // $header['token'] = 'Authorization: Bearer ' . $this->authModeSettings->token; 235 | break; 236 | 237 | case 'bearer': 238 | $header['token'] = 'Authorization: Bearer ' . $this->authModeSettings->token; 239 | break; 240 | 241 | default: 242 | # code... 243 | break; 244 | } 245 | 246 | $this->debugValues->header = $header; 247 | curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 248 | } 249 | 250 | /** 251 | * returls formated based on given obj settings 252 | * @param string 253 | * @return mixed 254 | */ 255 | private function returnResult($in, $header = false) 256 | { 257 | $this->header = $header; 258 | 259 | if ($this->checkHeader && isset($header["http_code"])) { 260 | if ($header["http_code"] < 200 || $header["http_code"] >= 300) { 261 | //error!? 262 | $this->error = $in; 263 | $message = var_export($in, true); 264 | if ($tmp = json_decode($in)) { 265 | if (isset($tmp->error->message)) { 266 | $message = $tmp->error->message; 267 | } 268 | } 269 | if ($this->throwExceptions) { 270 | throw new \Exception('' . $header["http_code"] . ';' . $message); 271 | } 272 | $in = null; 273 | 274 | } 275 | 276 | } 277 | 278 | switch ($this->returnFormat) { 279 | case 'json': 280 | return json_decode($in); 281 | break; 282 | 283 | default: 284 | return $in; 285 | break; 286 | } 287 | return $in; 288 | } 289 | 290 | public function microtime_float() 291 | { 292 | list($usec, $sec) = explode(" ", microtime()); 293 | return ((float) $usec + (float) $sec); 294 | } 295 | 296 | } 297 | --------------------------------------------------------------------------------