phpBMS

Show
Ignore:
Timestamp:
01/07/10 18:13:51 (2 years ago)
Author:
brieb
Message:
  • more sucrity tightening with roles.
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/phpbms/modules/mailchimp/include/list_sync.php

    r684 r730  
    11<?php 
     2/* 
     3 $Rev: 267 $ | $LastChangedBy: brieb $ 
     4 $LastChangedDate: 2007-08-14 13:08:27 -0600 (Tue, 14 Aug 2007) $ 
     5 +-------------------------------------------------------------------------+ 
     6 | Copyright (c) 2004 - 2010, Kreotek LLC                                  | 
     7 | All rights reserved.                                                    | 
     8 +-------------------------------------------------------------------------+ 
     9 |                                                                         | 
     10 | Redistribution and use in source and binary forms, with or without      | 
     11 | modification, are permitted provided that the following conditions are  | 
     12 | met:                                                                    | 
     13 |                                                                         | 
     14 | - Redistributions of source code must retain the above copyright        | 
     15 |   notice, this list of conditions and the following disclaimer.         | 
     16 |                                                                         | 
     17 | - Redistributions in binary form must reproduce the above copyright     | 
     18 |   notice, this list of conditions and the following disclaimer in the   | 
     19 |   documentation and/or other materials provided with the distribution.  | 
     20 |                                                                         | 
     21 | - Neither the name of Kreotek LLC nor the names of its contributore may | 
     22 |   be used to endorse or promote products derived from this software     | 
     23 |   without specific prior written permission.                            | 
     24 |                                                                         | 
     25 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS     | 
     26 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT       | 
     27 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | 
     28 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT      | 
     29 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   | 
     30 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT        | 
     31 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,   | 
     32 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY   | 
     33 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT     | 
     34 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   | 
     35 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.    | 
     36 |                                                                         | 
     37 +-------------------------------------------------------------------------+ 
     38*/ 
    239class listSync{ 
    3      
     40 
    441    /** 
    542      *  $lastSyncDate 
     
    1855      */ 
    1956    var $listId; 
    20      
     57 
    2158    /** 
    2259      *  $api 
     
    4178      */ 
    4279    var $stopScript = false; 
    43      
    44      
     80 
     81 
    4582    /** 
    4683      *  function listSync 
     
    5491      *  @param bool $secure Whether to send info over ssl 
    5592      */ 
    56      
     93 
    5794    function listSync($db, $apiKey, $listId, $lastSyncDate = NULL, $batchlimit = NULL, $secure = false){ 
    58          
     95 
    5996        $this->db = $db; 
    6097        $this->listId = $listId; 
    61          
     98 
    6299        /** 
    63100          *   Check for a valid datetime format? 
     
    65102        if($lastSyncDate) 
    66103            $this->lastSyncDate = $lastSyncDate; 
    67              
     104 
    68105        if((int)$batchlimit > 0) 
    69106           $this->batchlimit = (int)$batchlimit; 
    70          
     107 
    71108        $this->api = new MCAPI($apiKey, NULL, $secure); 
    72109        $this->api->ping(); 
     
    75112            return false; 
    76113        }//end if 
    77          
     114 
    78115        /** 
    79116          *   check to see if there is a uuid field 
     
    85122            return false; 
    86123        }//end if 
    87              
     124 
    88125        foreach($mergeVars as $mergeVar){ 
    89              
     126 
    90127            if($mergeVar["tag"] == "UUID"){ 
    91128                $hasUuid = true; 
    92129                break; 
    93130            }//end if 
    94              
     131 
    95132        }//end foreach 
    96          
     133 
    97134        if(!$hasUuid){ 
    98135            $this->_addError("The list does not have a merge variable with tag of 'UUID'.", NULL, true); 
    99136            return false; 
    100137        } 
    101          
     138 
    102139    }//end function 
    103      
     140 
    104141    /** 
    105142      *   function _addError 
    106143      * 
    107144      *   Add an error to the class' error array. 
    108       *    
     145      * 
    109146      *   @param string $message Text of error message 
    110147      *   @param int $errorCode Error number 
     
    112149      *   function needs to be stopped. 
    113150      */ 
    114      
     151 
    115152    function _addError($message, $errorCode = NULL, $fatal = false){ 
    116          
     153 
    117154        $tempArray["message"] = $message; 
    118155        $tempArray["code"] = $errorCode; 
    119          
     156 
    120157        if($fatal){ 
    121158            $this->stopScript = true; 
     
    124161            $tempArray["errorType"] = "warning"; 
    125162        }//end if 
    126          
     163 
    127164        $this->errors[] = $tempArray; 
    128          
     165 
    129166    }//end function 
    130      
    131      
     167 
     168 
    132169    /** 
    133170      *   function _reportResult 
     
    140177      *   code pairs. 
    141178      */ 
    142      
     179 
    143180    function _reportResult(){ 
    144          
     181 
    145182        $return = array(); 
    146          
     183 
    147184        if(count($this->errors)){ 
    148              
     185 
    149186            if($this->stopScript) 
    150187                $return["type"] = "error"; 
    151188            else 
    152189                $return["type"] = "warning"; 
    153              
     190 
    154191            $return["details"] = $this->errors; 
    155              
     192 
    156193        }else{ 
    157              
     194 
    158195            $return["type"] = "success"; 
    159196            $return["details"] = array(); 
    160              
    161         }//end if 
    162              
     197 
     198        }//end if 
     199 
    163200        return $return; 
    164          
     201 
    165202    }//end foreach 
    166      
    167      
     203 
     204 
    168205    /** 
    169206      *  function pullChanges 
     
    173210      *  address. 
    174211      */ 
    175      
     212 
    176213    function pullChanges(){ 
    177          
    178         /** 
    179           *  pull all the unsubscribed  
     214 
     215        /** 
     216          *  pull all the unsubscribed 
    180217          */ 
    181218        $unsubscribed = array(); 
    182219        $start = 0; 
    183220        do{ 
    184              
     221 
    185222            $members = $this->api->listMembers($this->listId, 'unsubscribed', $this->lastSyncDate, $start, $this->batchLimit); 
    186223            if($this->api->errorCode){ 
     
    188225                return false; 
    189226            }//end if 
    190              
     227 
    191228            foreach($members as $member){ 
    192                  
     229 
    193230                $info = $this->api->listMemberInfo($this->listId, $member["email"]); 
    194231                if($this->api->errorCode){ 
     
    196233                    return false; 
    197234                }//end if 
    198                      
     235 
    199236                $unsubscribed[] = $info["merges"]["UUID"]; 
    200                  
     237 
    201238            }//end foreach 
    202              
     239 
    203240            $start++; 
    204241        }while(count($members) == $this->batchLimit); 
    205          
    206         /** 
    207           *  pull all the cleaned  
     242 
     243        /** 
     244          *  pull all the cleaned 
    208245          */ 
    209246        $start = 0; 
    210247        do{ 
    211              
     248 
    212249            $members = $this->api->listMembers($this->listId, 'cleaned', $this->lastSyncDate, $start, $this->batchLimit); 
    213250            if($this->api->errorCode){ 
     
    215252                return false; 
    216253            }//end ifd 
    217              
     254 
    218255            foreach($members as $member){ 
    219                  
     256 
    220257                $info = $this->api->listMemberInfo($this->listId, $member["email"]); 
    221258                if($this->api->errorCode){ 
     
    224261                }//end if 
    225262                $unsubscribed[] = $info["merges"]["UUID"]; 
    226                  
     263 
    227264            }//end foreach 
    228              
     265 
    229266            $start++; 
    230267        }while(count($members) == $this->batchLimit); 
    231          
    232          
     268 
     269 
    233270        /** 
    234271          *  If there are records to unsubscribe, set their `canemail` to '0' 
    235272          */ 
    236273        if(count($unsubscribed)){ 
    237              
     274 
    238275            /** 
    239               *  construct the in statement  
     276              *  construct the in statement 
    240277              */ 
    241278            $inValues = ""; 
    242279            foreach($unsubscribed as $uuid) 
    243280                $inValues .= ",'".$uuid."'"; 
    244                  
     281 
    245282            $inValues = substr($inValues, 1); 
    246              
     283 
    247284            /** 
    248285              *  set the cleaned/unsubscribed to canemail = 0 
     
    256293                    `uuid` IN (".$inValues.") 
    257294            "; 
    258              
     295 
    259296            $queryresult = $this->db->query($querystatement); 
    260              
    261         }//end if 
    262          
     297 
     298        }//end if 
     299 
    263300    }//end function 
    264      
     301 
    265302    /** 
    266303      *   function unsubscribeInvalid 
    267       *    
     304      * 
    268305      *   Unsubscribe the emails that are related to 
    269306      *   client records those that don't have an email (or don't exist), 
    270307      *   or that can't be emailed anymore. 
    271308      */ 
    272      
     309 
    273310    function unsubscribeInvalid(){ 
    274          
     311 
    275312        /** 
    276313          *   Get rid of the temorary table. 
     
    281318        "; 
    282319        $this->db->query($dropTableStatement); 
    283          
    284         /** 
    285           *  Create a temporary table  
     320 
     321        /** 
     322          *  Create a temporary table 
    286323          */ 
    287324        $createTableStatement = " 
     
    291328                `email` varchar(128) default NULL 
    292329            ) ENGINE=INNODB"; 
    293          
     330 
    294331        $this->db->query($createTableStatement); 
    295          
    296         /** 
    297           *  pull all the subscribed  
     332 
     333        /** 
     334          *  pull all the subscribed 
    298335          */ 
    299336        $start = 0; 
    300         do{     
     337        do{ 
    301338            $valuesClause = ""; 
    302                          
     339 
    303340            $members = $this->api->listMembers($this->listId, 'subscribed', NULL, $start, $this->batchLimit); 
    304341 
     
    307344                return false; 
    308345            }//end if 
    309              
     346 
    310347            foreach($members as $member) 
    311348                $valuesClause .= ",('".$member["email"]."')"; 
    312              
     349 
    313350            $valuesClause = substr($valuesClause, 1); 
    314              
    315              
     351 
     352 
    316353            /** 
    317               *  Put the subscribed into a temporary table  
     354              *  Put the subscribed into a temporary table 
    318355              */ 
    319356            if($valuesClause){ 
     
    324361                        VALUES 
    325362                    ".$valuesClause; 
    326                  
     363 
    327364                $this->db->query($insertStatement); 
    328365            }//end if 
    329              
     366 
    330367            $start++; 
    331368        }while(count($members) == $this->batchLimit); 
    332369 
    333          
     370 
    334371        /** 
    335372          *  Get all the emails of client records that are subscribed 
     
    346383                `clients`.`email` IS NULL 
    347384        "; 
    348          
     385 
    349386        $selectresult = $this->db->query($selectStatement); 
    350          
     387 
    351388        /** 
    352389          *   Unsubscribe them from the mailchimp list 
     
    355392        while($therecord = $this->db->fetchArray($selectresult)) 
    356393            $unsubscribeList[] = $therecord["email"]; 
    357          
    358         /** 
    359           *  If there are records to unsubscribe (deleted), do so.  
     394 
     395        /** 
     396          *  If there are records to unsubscribe (deleted), do so. 
    360397          */ 
    361398        if(count($unsubscribeList)){ 
    362              
     399 
    363400            $return = $this->api->listBatchUnsubscribe($this->listId, $unsubscribeList, true, false); 
    364401            if ($this->api->errorCode){ 
     
    368405                foreach($return['errors'] as $val) 
    369406                    $this->_addError("Unsubscribing email ".$val["email"]." failed: ".$val["message"], $val["code"]); 
    370                      
    371         }//end if 
    372              
     407 
     408        }//end if 
     409 
    373410        /** 
    374411          *   Get rid of the temorary table. 
     
    379416        "; 
    380417        $this->db->query($dropTableStatement); 
    381          
     418 
    382419    }//end function 
    383      
    384      
     420 
     421 
    385422    /** 
    386423      *  function pushChanges 
     
    390427      */ 
    391428    function pushChanges(){ 
    392          
     429 
    393430        /** 
    394431          *   Get the changed records / fields 
     
    411448                `canemail` != '0' 
    412449        "; 
    413          
     450 
    414451        /** 
    415452          *  If there is a last sync *date*, limit the records by their modified 
     
    418455        if($this->lastSyncDate) 
    419456            $querystatement .= " AND `modifieddate` > '".$this->lastSyncDate."'"; 
    420              
    421          
     457 
     458 
    422459        $queryresult = $this->db->query($querystatement); 
    423          
     460 
    424461        /** 
    425462          *   Format the variables to be interpreted by Mailchimp. 
     
    427464        $batchVars = array(); 
    428465        while($therecord = $this->db->fetchArray($queryresult)){ 
    429              
     466 
    430467            $tempArray["EMAIL"] = $therecord["email"]; 
    431468            $tempArray["FNAME"] = $therecord["firstname"]; 
     
    434471            $tempArray["TYPE"] = $therecord["type"]; 
    435472            $tempArray["UUID"] = $therecord["uuid"]; 
    436              
     473 
    437474            $batchVars[] = $tempArray; 
    438475        }//end while 
    439          
    440         /** 
    441           *  Update / Insert the changes  
     476 
     477        /** 
     478          *  Update / Insert the changes 
    442479          */ 
    443480        if(count($batchVars)){ 
    444              
     481 
    445482            $return = $this->api->listBatchSubscribe($this->listId, $batchVars, false, true); 
    446483            if($this->api->errorCode){ 
     
    448485                return false; 
    449486            }else{ 
    450                  
     487 
    451488                $instatement = ""; 
    452                  
     489 
    453490                foreach($return['errors'] as $val){ 
    454                      
     491 
    455492                    $this->_addError("Subscribing or updating uuid '".$val["row"]["UUID"]."' failed: ".$val["message"], $val["code"]); 
    456                      
     493 
    457494                    $memberInfo = $this->api->listMemberInfo($this->listId, $val["row"]["EMAIL"]); 
    458                      
     495 
    459496                    if($memberInfo["status"] != "subcribed") 
    460497                        $instatement .= ", '".$val["row"]["UUID"]."'"; 
    461                      
     498 
    462499                }//end foreach 
    463                  
     500 
    464501                if($instatement){ 
    465                      
     502 
    466503                    $instatement = substr($instatement, 2); 
    467504                    $instatement = "(".$instatement.")"; 
    468                      
     505 
    469506                    $updatestatement = " 
    470507                        UPDATE 
     
    475512                        WHERE 
    476513                            `uuid` IN ".$instatement; 
    477                      
     514 
    478515                    $this->db->query($updatestatement); 
    479                      
     516 
    480517                }//end if 
    481                  
     518 
    482519            }//end if 
    483                      
    484         }//end if 
    485          
     520 
     521        }//end if 
     522 
    486523    }//end function 
    487      
    488      
     524 
     525 
    489526    /* 
    490527     * function resetSyncDate 
     
    492529     * Set the last sync date in the settings table to be NOW() 
    493530     */ 
    494      
     531 
    495532    function resetSyncDate() { 
    496          
     533 
    497534        $querystatement = " 
    498535            UPDATE 
     
    503540                `name` = 'mailchimp_last_sync_date' 
    504541        "; 
    505          
     542 
    506543        $queryresult = $this->db->query($querystatement); 
    507          
     544 
    508545    }//end if 
    509      
    510      
     546 
     547 
    511548    /** 
    512549      *  function process 
    513       *   
     550      * 
    514551      *  Perform the list sync 
    515552      */ 
    516553    function process(){ 
    517          
     554 
    518555        if(!$this->stopScript) 
    519556            $this->pullChanges(); 
     
    524561        if(!$this->stopScript) 
    525562            $this->resetSyncDate(); 
    526          
     563 
    527564        return $this->_reportResult(); 
    528          
     565 
    529566    }//end function 
    530      
     567 
    531568}//end class 
    532569?> 
phpBMS vulnerability assesment provided by Orvant Inc. Copyright © 2010 Kreotek, LLC. All Rights reserved.