mailin
Last commit date
css
13 years ago
js
13 years ago
lang
13 years ago
api_form.php
13 years ago
compatibility.php
13 years ago
cron.php
13 years ago
listings.php
13 years ago
mailin.php
13 years ago
mailin_widget.php
13 years ago
mailinapi.class.php
13 years ago
readme.txt
13 years ago
mailinapi.class.php
517 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This class has functions that supports interaction with Mailin server |
| 5 | */ |
| 6 | |
| 7 | class mailin_API { |
| 8 | |
| 9 | /** |
| 10 | error messages default set to blank |
| 11 | */ |
| 12 | public $_mailin_error = array(); |
| 13 | |
| 14 | /** |
| 15 | success messages default set to blank |
| 16 | */ |
| 17 | public $_mailin_success = array(); |
| 18 | |
| 19 | /** |
| 20 | * Returns all lists from databse |
| 21 | * @return Array |
| 22 | */ |
| 23 | public function getListDetails($list_id){ |
| 24 | |
| 25 | $lists = get_option('mailin_lists'); |
| 26 | $lists = unserialize($lists); |
| 27 | |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Updates lists in database after fetching from mailin |
| 32 | * @return Array |
| 33 | */ |
| 34 | public function updateUserLists($api_key = null){ |
| 35 | |
| 36 | if($api_key == null){ |
| 37 | $api_key = get_option('mailin_apikey'); |
| 38 | } |
| 39 | |
| 40 | if($api_key == ''){ |
| 41 | return ; |
| 42 | } |
| 43 | |
| 44 | $returnData = $this->getUserLists($api_key); |
| 45 | |
| 46 | if(isset($returnData->result) && !empty($returnData->result)){ |
| 47 | $listData = $returnData->result; |
| 48 | $lists = serialize($listData); |
| 49 | update_option('mailin_lists', $lists); |
| 50 | } |
| 51 | |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Updates user campaigns in database after fetching from mailin |
| 56 | * @return array of list |
| 57 | */ |
| 58 | public function updateUserCampaigns($api_key = null){ |
| 59 | |
| 60 | if($api_key == null){ |
| 61 | $api_key = get_option('mailin_apikey'); |
| 62 | } |
| 63 | |
| 64 | if($api_key == ''){ |
| 65 | return ; |
| 66 | } |
| 67 | |
| 68 | $returnData = $this->getUserCampaigns($api_key); |
| 69 | |
| 70 | if(isset($returnData->result) && !empty($returnData->result)){ |
| 71 | $campaigns = $returnData->result; |
| 72 | $campaigns = serialize($campaigns); |
| 73 | update_option('mailin_campaigns', $campaigns); |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Validate API key entered by user |
| 81 | * @return array with error message (if invalid), user's lists (if valid) |
| 82 | */ |
| 83 | public function validateAPIkey($api_key = null){ |
| 84 | |
| 85 | $isValid = false; |
| 86 | $error = ''; |
| 87 | $listData = array(); |
| 88 | |
| 89 | if($api_key == ''){ |
| 90 | |
| 91 | $error = 'Please enter API key'; |
| 92 | |
| 93 | }else{ |
| 94 | |
| 95 | $returnData = $this->getUserLists($api_key); |
| 96 | |
| 97 | if(!is_object($returnData)){ |
| 98 | |
| 99 | $error = 'Oops!, unable to connect!'; |
| 100 | |
| 101 | }else if(isset($returnData->errorMsg)){ |
| 102 | |
| 103 | $error = $returnData->errorMsg; |
| 104 | |
| 105 | }else if(isset($returnData->result)){ |
| 106 | |
| 107 | $isValid = true; |
| 108 | if(!empty($returnData->result)){ |
| 109 | $listData = $returnData->result; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | $returnData = array('isValid' => $isValid , 'data' => $listData , 'error' => $error); |
| 115 | return $returnData; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * Validate API key, saves API key in DB |
| 122 | * Saves users campaigns, Saves users lists |
| 123 | * @return void |
| 124 | */ |
| 125 | public function handle_apikey_form_submit($api_key = null){ |
| 126 | |
| 127 | $api_key = strip_tags(trim($api_key)); |
| 128 | |
| 129 | $returnData = $this->validateAPIkey($api_key); |
| 130 | |
| 131 | if($returnData['error'] != ''){ |
| 132 | |
| 133 | $this->_mailin_error[] = __( $returnData['error'] , 'mailin_i18n'); |
| 134 | |
| 135 | }else if(!$returnData['isValid']){ |
| 136 | |
| 137 | $this->_mailin_error[] = __( $returnData['error'], 'mailin_i18n'); |
| 138 | |
| 139 | }else{ |
| 140 | |
| 141 | $this->_mailin_success[] = __('Your API is valid, your lists are fetched and saved in the database.', 'mailin_i18n'); |
| 142 | |
| 143 | $this->updateUserLists($api_key); |
| 144 | $this->updateUserCampaigns($api_key); |
| 145 | |
| 146 | if(!empty($returnData['data'])){ |
| 147 | |
| 148 | $lists = serialize($returnData['data']); |
| 149 | update_option('mailin_lists', $lists); |
| 150 | |
| 151 | } |
| 152 | update_option('mailin_apikey', $api_key); |
| 153 | update_option('mailin_list_selected', ''); |
| 154 | |
| 155 | } |
| 156 | |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /** |
| 161 | * Saves selected lists (in which newsletter subscriptions will be added) |
| 162 | * @return void |
| 163 | */ |
| 164 | public function handle_updatelist_form_submit($list_id){ |
| 165 | |
| 166 | if(!empty($list_id)){ |
| 167 | |
| 168 | $this->updateUserLists(); |
| 169 | $list_id = implode('|', $list_id); |
| 170 | update_option('mailin_list_selected', $list_id); |
| 171 | $this->_mailin_success[] = __('List updated successfully', 'mailin_i18n'); |
| 172 | |
| 173 | }else{ |
| 174 | |
| 175 | $this->_mailin_error[] = __('Please select a list' , 'mailin_i18n'); |
| 176 | |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Removes api key, mailin lists and selected lists from DB |
| 182 | * @return void |
| 183 | */ |
| 184 | public function handle_logout_form_submit(){ |
| 185 | |
| 186 | update_option('mailin_apikey', ''); |
| 187 | update_option('mailin_lists', ''); |
| 188 | update_option('mailin_list_selected', ''); |
| 189 | |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /** |
| 194 | * Validate newsletter form and sets up error messages accordingly. |
| 195 | * @return void |
| 196 | */ |
| 197 | function validate_newsletter_form($email, $fname , $lname) { |
| 198 | |
| 199 | $result = true; |
| 200 | |
| 201 | if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) { |
| 202 | //$result = false; |
| 203 | $this->_mailin_error['email'] = __("Please enter valid e-mail" , 'mailin_i18n'); |
| 204 | } |
| 205 | |
| 206 | if($fname == ''){ |
| 207 | $this->_mailin_error['fname'] = __("Please enter your first name" , 'mailin_i18n'); |
| 208 | } |
| 209 | |
| 210 | if($lname == ''){ |
| 211 | $this->_mailin_error['lname'] = __("Please enter your last name" , 'mailin_i18n'); |
| 212 | } |
| 213 | return $result; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | |
| 218 | /** |
| 219 | * Validate newsletter form and sets up error messages accordingly. |
| 220 | * @return void |
| 221 | */ |
| 222 | public function handle_newsletter_subscribe_submit(){ |
| 223 | |
| 224 | $email = trim(strip_tags($_POST['mailin_email'])); |
| 225 | $email = strtolower($email); |
| 226 | |
| 227 | $fname = isset($_POST['fname']) ? trim(strip_tags($_POST['fname'])) : ''; |
| 228 | $lname = isset($_POST['lname']) ? trim(strip_tags($_POST['lname'])) : ''; |
| 229 | |
| 230 | $this->validate_newsletter_form($email, $fname , $lname); |
| 231 | |
| 232 | if(empty($this->_mailin_error)){ |
| 233 | |
| 234 | $api_key = get_option('mailin_apikey'); |
| 235 | $selected_list = get_option('mailin_list_selected'); |
| 236 | |
| 237 | $returnData = $this->createUser($api_key ,$email , $selected_list); |
| 238 | |
| 239 | if(!is_object($returnData)){ |
| 240 | |
| 241 | $error = 'Oops!, unable to connect!'; |
| 242 | |
| 243 | }else if((isset($returnData->msgTy) && $returnData->msgTy == 'success') || isset($returnData->result)){ |
| 244 | |
| 245 | $this-> updateUserLists($api_key); |
| 246 | |
| 247 | $this-> updateSubscribers($email , $selected_list, $fname , $lname); |
| 248 | |
| 249 | $this->_mailin_success[] = __('You have successfully subscribed Mailin newsletter!','mailin_i18n' ); |
| 250 | |
| 251 | }else if(isset($returnData->msgTy) && $returnData->msgTy == 'error'){ |
| 252 | |
| 253 | $this->_mailin_error[] = __($returnData->msg,'mailin_i18n'); |
| 254 | |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | } |
| 259 | |
| 260 | public function add_subscription($email){ |
| 261 | |
| 262 | get_option('USERCREADIT'); |
| 263 | //add user through ADD USER FUNCTION |
| 264 | |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Makes curl request to Mailin server with array of vars |
| 269 | * @return array returned by API |
| 270 | */ |
| 271 | function curl_request($data) { |
| 272 | |
| 273 | $url = 'http://ws.mailin.fr/'; //WS URL |
| 274 | $ch = curl_init(); |
| 275 | // prepate data for curl post |
| 276 | |
| 277 | $ndata=''; |
| 278 | if(is_array($data)){ |
| 279 | foreach($data AS $key=>$value){ |
| 280 | $ndata .=$key.'='.urlencode($value).'&'; |
| 281 | } |
| 282 | }else { |
| 283 | $ndata=$data; |
| 284 | } |
| 285 | curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); |
| 286 | curl_setopt($ch, CURLOPT_POST ,1); |
| 287 | curl_setopt ($ch, CURLOPT_POSTFIELDS,$ndata); |
| 288 | curl_setopt($ch, CURLOPT_HEADER, 0); |
| 289 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser. |
| 290 | curl_setopt($ch, CURLOPT_URL, $url); |
| 291 | $data = curl_exec($ch); |
| 292 | curl_close($ch); |
| 293 | return $data; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /** |
| 298 | * Creates a user under the provided list id |
| 299 | * @return Array |
| 300 | */ |
| 301 | public function createUser($api_key = null, $email, $list_id){ |
| 302 | |
| 303 | $data['key']= $api_key ; |
| 304 | $data['webaction']='USERCREADIT'; |
| 305 | $data['email']= $email; |
| 306 | $data['id']=''; |
| 307 | $data['blacklisted']=''; |
| 308 | $data['attributes_name']=''; |
| 309 | $data['attributes_value']=''; |
| 310 | $data['listid']= $list_id; |
| 311 | |
| 312 | $return = $this->curl_request($data); |
| 313 | $return = json_decode($return); |
| 314 | return $return; |
| 315 | |
| 316 | } |
| 317 | |
| 318 | |
| 319 | /** |
| 320 | * Return list data from Mailin server |
| 321 | * @return Array |
| 322 | */ |
| 323 | public function getUserLists($api_key = null , $list_id = null){ |
| 324 | |
| 325 | if($api_key == '') { |
| 326 | return ; |
| 327 | } |
| 328 | |
| 329 | $data = array(); |
| 330 | $data['key'] = $api_key; |
| 331 | $data['webaction']= 'DISPLAYLISTDATA'; |
| 332 | |
| 333 | if($list_id != null){ |
| 334 | $data['listids'] = $list_id; |
| 335 | } |
| 336 | |
| 337 | $return = $this->curl_request($data); |
| 338 | $return = json_decode($return); |
| 339 | |
| 340 | return $return; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | |
| 345 | /** |
| 346 | * Returns user data of provided list Ids from Mailin server |
| 347 | * @return Array; |
| 348 | */ |
| 349 | function getListUsers($api_key, $list_ids){ |
| 350 | |
| 351 | |
| 352 | if($api_key == '') { |
| 353 | return ; |
| 354 | } |
| 355 | |
| 356 | $data = array(); |
| 357 | $data['webaction']='DISPLAYLISTDATABLACK'; |
| 358 | $data['key']=$api_key; |
| 359 | |
| 360 | $data['listids']= $list_ids; |
| 361 | |
| 362 | $return = $this->curl_request($data); |
| 363 | $return = json_decode($return); |
| 364 | |
| 365 | return $return; |
| 366 | |
| 367 | } |
| 368 | |
| 369 | |
| 370 | /** |
| 371 | * Returns user's campaigns from Mailin |
| 372 | * @return Array; |
| 373 | */ |
| 374 | public function getUserCampaigns($api_key = null){ |
| 375 | |
| 376 | if($api_key == '') { |
| 377 | return ; |
| 378 | } |
| 379 | |
| 380 | $data = array(); |
| 381 | $data['key'] = $api_key; |
| 382 | $data['webaction']= 'CAMPAIGNDETAIL'; |
| 383 | $data['show']='ALL'; |
| 384 | |
| 385 | $return = $this->curl_request($data); |
| 386 | $return = json_decode($return); |
| 387 | return $return; |
| 388 | } |
| 389 | |
| 390 | |
| 391 | |
| 392 | /** |
| 393 | * Returns user details of email provided from Mailin server |
| 394 | * @return Array; |
| 395 | */ |
| 396 | public function validateUser($api_key, $email){ |
| 397 | |
| 398 | if($api_key == '') { |
| 399 | return ; |
| 400 | } |
| 401 | |
| 402 | $data = array(); |
| 403 | $data['key'] = $api_key; |
| 404 | $data['webaction']= 'DISPLAYUSERDETAIL'; |
| 405 | $data['email']= $email; |
| 406 | |
| 407 | $return = $this->curl_request($data); |
| 408 | $return = json_decode($return); |
| 409 | return $return; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | |
| 414 | /** |
| 415 | * Inserts/updates(user status) in subscription table |
| 416 | * @return void; |
| 417 | */ |
| 418 | public function updateSubscribers($email , $selected_list, $fname , $lname){ |
| 419 | |
| 420 | $selected_list = explode('|', $selected_list); |
| 421 | $selected_list = implode(',', $selected_list); |
| 422 | |
| 423 | global $wpdb; |
| 424 | $myrows = $wpdb->get_results( "SELECT id FROM ".MAILIN_SUBSCRIBERS." WHERE email = '".$email."' LIMIT 1"); |
| 425 | |
| 426 | if(empty($myrows)){ |
| 427 | |
| 428 | $sql = "INSERT INTO ".MAILIN_SUBSCRIBERS." |
| 429 | SET email= '".$email."', fname = '".$fname."' , |
| 430 | lname = '".$lname."' , list = '".$selected_list."' , create_date= '".date('Y-m-d H:i:s')."' "; |
| 431 | |
| 432 | $wpdb->query($sql); |
| 433 | |
| 434 | }else{ |
| 435 | |
| 436 | $sql = "UPDATE ".MAILIN_SUBSCRIBERS." SET list = '".$selected_list."' , fname = '".$fname."' , |
| 437 | lname = '".$lname."' , create_date= '".date('Y-m-d H:i:s')."' WHERE id = ".$myrows[0]->id." "; |
| 438 | $wpdb->query($sql); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | |
| 443 | /** |
| 444 | * Returns subscribers from subscription table |
| 445 | * @return void; |
| 446 | */ |
| 447 | public function getAllSubscribers($subs = 1){ |
| 448 | |
| 449 | global $wpdb; |
| 450 | |
| 451 | $sql = "SELECT * FROM ".MAILIN_SUBSCRIBERS." WHERE 1=1 "; |
| 452 | |
| 453 | if($subs == 1){ |
| 454 | $sql .= " AND subscribed = '1' "; |
| 455 | } |
| 456 | |
| 457 | $myrows = $wpdb->get_results($sql); |
| 458 | return $myrows; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | /** |
| 463 | * Synchronize the user's status in subscription table with user's status on Mailin |
| 464 | * @return bool; |
| 465 | */ |
| 466 | public function syncUsers(){ |
| 467 | |
| 468 | global $wpdb; |
| 469 | $api_key = get_option('mailin_apikey'); |
| 470 | |
| 471 | $lists = get_option('mailin_lists'); |
| 472 | $lists = unserialize($lists); |
| 473 | |
| 474 | $final_data = array(); |
| 475 | foreach($lists as $data){ |
| 476 | $final_data[] = $data->id; |
| 477 | } |
| 478 | |
| 479 | $list_ids = ''; |
| 480 | if(!empty($final_data)){ |
| 481 | $list_ids = implode('|' , $final_data); |
| 482 | } |
| 483 | |
| 484 | if($list_ids == ''){ |
| 485 | return ; |
| 486 | } |
| 487 | |
| 488 | $list_users = $this->getListUsers($api_key , $list_ids); |
| 489 | |
| 490 | |
| 491 | if(!empty($list_users->result)){ |
| 492 | |
| 493 | foreach($list_users->result as $key=>$lists){ |
| 494 | |
| 495 | if(!empty($lists)){ |
| 496 | foreach($lists as $users){ |
| 497 | |
| 498 | if(isset($users->blacklisted)){ |
| 499 | |
| 500 | if($users->blacklisted == '1'){ |
| 501 | $sql = "UPDATE ".MAILIN_SUBSCRIBERS." SET subscribed = '0' WHERE email = '".strtolower(trim($users->email))."' " ; |
| 502 | }else{ |
| 503 | $sql = "UPDATE ".MAILIN_SUBSCRIBERS." SET subscribed = '1' WHERE email = '".strtolower(trim($users->email))."' " ; |
| 504 | |
| 505 | } |
| 506 | $myrows = $wpdb->query($sql); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | return true; |
| 513 | } |
| 514 | |
| 515 | } |
| 516 | ?> |
| 517 |