phpBMS

Show
Ignore:
Timestamp:
04/07/09 11:44:18 (3 years ago)
Author:
nate
Message:
  • Merged Nathan branch back into trunk.
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/phpbms/modules/bms/include/addresstorecord.php

    r427 r485  
    3939if(class_exists("addresses")){ 
    4040        class addresstorecord extends addresses{ 
    41          
     41 
     42                var $availableTabledefIDs = array(); 
     43                var $availableTabledefNames = array(); 
    4244 
    4345                function createAddressToRecord($variables, $addressid, $createdby = NULL){ 
    4446                        // This functions adds the addresstorecord record tying the client and address 
    4547                        // records. 
    46                  
     48 
    4749                        if(!$createdby && isset($_SESSION["userinfo"]["id"])) 
    4850                                $createdby = $_SESSION["userinfo"]["id"]; 
    49                                  
     51 
    5052                        $insertstatement = " 
    5153                                INSERT INTO 
     
    7072                                        NOW(), 
    7173                                        ".((int) $createdby).", 
    72                                         NOW()                                    
     74                                        NOW() 
    7375                                )"; 
    74                          
     76 
    7577                        $insertresult = $this->db->query($insertstatement); 
    76                          
     78 
    7779                        if($insertresult) 
    7880                                return $this->db->insertId(); 
    7981                        else 
    8082                                return false; 
    81                          
     83 
    8284                }//end method - createAddressToRecord 
    83                  
     85 
    8486                // CLASS OVERRIDES =============================================== 
    8587                // =============================================================== 
    8688                function getRecord($id){ 
    8789                        $id = (int) $id; 
    88                          
     90 
    8991                        $querystatement = " 
    9092                                SELECT 
    91                                         *        
    92                                 FROM  
    93                                         addresstorecord 
    94                                 WHERE  
    95                                         id = ".$id;                      
    96                                          
     93                                        * 
     94                                FROM 
     95                                        addresstorecord 
     96                                WHERE 
     97                                        id = ".$id; 
     98 
    9799                        $queryresult = $this->db->query($querystatement); 
    98                         if($this->db->numRows($queryresult)) {  
    99                          
     100                        if($this->db->numRows($queryresult)) { 
     101 
    100102                                $therecord = $this->db->fetchArray($queryresult); 
    101103                                $addressrecord = parent::getRecord($therecord["addressid"]); 
    102104 
    103105                                unset($addressrecord["id"], $addressrecord["createdby"], $addressrecord["creationdate"], $addressrecord["modifiedby"], $addressrecord["modifieddate"]); 
    104                                  
     106 
    105107                                $therecord = array_merge($addressrecord, $therecord); 
    106                                  
    107                         } else  
     108 
     109                        } else 
    108110                                $therecord = $this->getDefaults(); 
    109111 
    110112                        return $therecord; 
    111          
     113 
    112114                }//end method - getRecord 
    113                  
    114                  
     115 
     116 
    115117                function getDefaults(){ 
    116                  
     118 
    117119                        $therecord = parent::getDefaults(); 
    118                          
     120 
    119121                        $therecord["addressid"] = 0; 
    120122                        $therecord["tabledefid"] = 0; 
    121123                        $therecord["recordid"] = NULL; 
    122124                        $therecord["defaultshipto"] = 0; 
    123                         $therecord["primary"] = 0;                       
    124                          
    125                         return $therecord;       
    126          
     125                        $therecord["primary"] = 0; 
     126 
     127                        return $therecord; 
     128 
    127129                }//end method - getDefauls 
    128                  
    129                  
    130  
    131                 function formatVariables($variables){ 
    132                          
     130 
     131 
     132                function populateTabledefArrays(){ 
     133 
     134                        $this->availableTabledefIDs = array(); 
     135                        $this->availableTabledefNames = array(); 
     136 
     137                        $querystatement = " 
     138                                SELECT 
     139                                        `id`, 
     140                                        `maintable` 
     141                                FROM 
     142                                        `tabledefs`; 
     143                                "; 
     144 
     145                        $queryresult = $this->db->query($querystatement); 
     146 
     147                        if($this->db->numRows($queryresult)){ 
     148                                while($therecord = $this->db->fetchArray($queryresult)){ 
     149                                        $this->availableTabledefIDs[] = $therecord["id"]; 
     150                                        $this->availableTabledefNames[$therecord["id"]] = $therecord["maintable"]; 
     151                                }//end while 
     152                        }else{ 
     153                                $this->availableTabledefIDs[] = "none"; 
     154                                $this->availableTabledefNames[] = "none"; 
     155                        }//end if 
     156 
     157                }//end method --populateTabledefArray-- 
     158 
     159 
     160                function checkRecordID($recordid, $tablename){ 
     161 
     162                        $recordid = ((int) $recordid); 
     163                        $tablename = addslashes($tablename); 
     164 
     165                        $querystatement = " 
     166                                SELECT 
     167                                        `id` 
     168                                FROM 
     169                                        `".$tablename."` 
     170                                WHERE 
     171                                        `id` = '".$recordid."'; 
     172                                "; 
     173 
     174                        $queryresult = $this->db->query($querystatement); 
     175 
     176                        return $this->db->numRows($queryresult); 
     177 
     178                }//end method --checkRecordID-- 
     179 
     180 
     181                function verifyVariables($variables){ 
     182                        //POSSIBLY CHANGE>>>> NOT FINISHIED >>>> 
     183                        //Check tabledefs 
     184                        if(isset($variables["tabledefid"])){ 
     185 
     186                                if(is_numeric($variables["tabledefid"])){ 
     187 
     188                                        if(!count($this->availableTabledefIDs) || !count($this->availableTabledefNames)) 
     189                                                $this->populateTabledefArrays(); 
     190 
     191                                        if(in_array($variables["tabledefid"], $this->availableTabledefIDs)){ 
     192 
     193                                                //check recordid 
     194                                                if(isset($variables["recordid"])){ 
     195 
     196                                                        if((int) $variables["recordid"] > 0){ 
     197 
     198                                                                if(!count($this->availableTabledefIDs) || !count($this->availableTabledefNames)) 
     199                                                                        $this->populateTabledefArrays(); 
     200 
     201                                                                if(!$this->checkRecordID($variables["recordid"], $this->availableTabledefNames[$variables["tabledefid"]])) 
     202                                                                        $this->verifyErrors[] = "The `recordid` field does match an id number in ".$this->availableTabledefNames[$variables["tabledefid"]]."."; 
     203 
     204                                                        }else 
     205                                                                $this->verifyErrors[] = "The `recordid` field must be a positive number."; 
     206 
     207                                                }else 
     208                                                        $this->verifyErrors[] = "The `recordid` field must be set."; 
     209 
     210                                        }else 
     211                                                $this->verifyErrors[] = "The `tabledefid` field does not give an existing/acceptable table definition id number."; 
     212 
     213                                }else 
     214                                        $this->verifyErrors[] = "The `tabledefid` field must be numeric."; 
     215 
     216                        }else 
     217                                $this->verifyErrors[] = "The `tabledefid` field must be set."; 
     218 
     219                        return parent::verifyVariables($variables); 
     220 
     221                }//end method --verifyVariables-- 
     222 
     223 
     224                function prepareVariables($variables){ 
     225 
    133226                        if(!isset($variables['primary'])) 
    134227                                $variables['primary'] = 0; 
     
    136229                        if(!isset($variables['defaultshipto'])) 
    137230                                $variables['defaultshipto'] = 0; 
    138                          
     231 
    139232                        if(!isset($variables["existingaddressid"])) 
    140233                                $variables["existingaddressid"] = false; 
    141                                                  
    142                         return $variables;                       
    143                  
     234 
     235                        if(isset($variables["id"])) 
     236                                if($variables["id"]){// if update 
     237 
     238                                        $variables["id"] = $variables["addressid"]; 
     239 
     240                                }//end if 
     241 
     242                        return $variables; 
     243 
    144244                }//end function 
    145245 
    146246 
    147  
    148                 function updateRecord($variables, $modifiedby = NULL){ 
    149          
    150                         $variables = $this->formatVariables($variables); 
    151  
    152                         $variables["id"] = $variables["addressid"]; 
    153          
    154                         $thereturn = parent::updateRecord($variables, $modifiedby); 
    155                                                  
    156                         return $thereturn; 
    157                          
    158                 }//end method 
    159                  
    160          
    161247                function insertRecord($variables, $createdby = NULL){ 
    162248 
    163                         $variables = $this->formatVariables($variables); 
     249                        //$variables = $this->prepareVariables($variables); 
    164250 
    165251                        if($variables["existingaddressid"]){ 
    166                          
     252 
    167253                                $this->createAddressToRecord($variables, $variables["existingaddressid"], $createdby); 
    168                                  
     254 
    169255                                $newAtrID = $variables["existingaddressid"]; 
    170                          
     256 
    171257                        } else { 
    172258 
    173259                                $newid = parent::insertRecord($variables, $createdby); 
    174                                  
     260 
    175261                                //create the addresstorecord 
    176262                                $newAtrID = $this->createAddressToRecord($variables, $newid, $createdby); 
    177                                  
     263 
    178264                        }//endif - existingaddressid 
    179                          
     265 
    180266                        return $newAtrID; 
    181                                          
     267 
    182268                }//end method 
    183                  
     269 
    184270        }//end class 
    185271}//end if 
     
    192278 
    193279                        $whereclause = $this->buildWhereClause(); 
    194                          
     280 
    195281                        //We need to itterate trhough each record 
    196                         // to check for cross-record addresses                   
     282                        // to check for cross-record addresses 
    197283                        $querystatement = " 
    198284                                SELECT 
     
    201287                                        addresstorecord 
    202288                                WHERE ".$whereclause; 
    203                          
     289 
    204290                        $queryresult = $this->db->query($querystatement); 
    205                          
     291 
    206292                        $removedCount = 0; 
    207293                        $deletedCount = 0; 
    208294                        $beenMarked = false; 
    209                          
     295 
    210296                        while($therecord = $this->db->fetchArray($queryresult)){ 
    211297 
     
    213299                                // cannot be removed 
    214300                                if(!$therecord["primary"] && !$therecord["defaultshipto"]){ 
    215                                  
     301 
    216302                                        //look up address to see if it is associated with other records 
    217303                                        $querystatement = " 
     
    224310                                                        AND tabledefid = ".$therecord["tabledefid"]." 
    225311                                                        AND recordid != ".$therecord["recordid"]; 
    226                                                          
     312 
    227313                                        $lookupResult = $this->db->query($querystatement); 
    228314 
    229315                                        if(!$this->db->numRows($lookupResult)){ 
    230                                          
     316 
    231317                                                //we can safely delete the address (no other associations) 
    232318                                                $deletestatement = " 
     
    237323 
    238324                                                $this->db->query($deletestatement); 
    239                                                  
     325 
    240326                                                $deletedCount++; 
    241327                                                $removedCount--; 
    242                                                  
     328 
    243329                                        }//end if - numRows 
    244                                  
     330 
    245331                                        //remove the connecting record 
    246332                                        $deletestatement = " 
     
    249335                                                WHERE 
    250336                                                        id =".$therecord["id"]; 
    251                                          
     337 
    252338                                        $this->db->query($deletestatement); 
    253                                          
     339 
    254340                                        $removedCount++; 
    255                                  
     341 
    256342                                } else { 
    257                                  
    258                                         $beenMarked = true;                      
    259                                          
     343 
     344                                        $beenMarked = true; 
     345 
    260346                                }//endif - primary or defaultshipto 
    261                                  
     347 
    262348                        }//endwhile - fetchArray 
    263349 
    264350                        //next, craft the response 
    265                          
     351 
    266352                        $message = ""; 
    267                          
    268                         if($removedCount){       
    269                                  
     353 
     354                        if($removedCount){ 
     355 
    270356                                $message .= $removedCount." address"; 
    271                                  
     357 
    272358                                if($removedCount > 1) 
    273359                                        $message .= "es"; 
    274                                  
     360 
    275361                                $message .= " dissociated from record. "; 
    276                                  
     362 
    277363                        }//endif removedCount 
    278                          
    279                         if($deletedCount){       
    280                                  
     364 
     365                        if($deletedCount){ 
     366 
    281367                                $message .= $deletedCount." address"; 
    282                                  
     368 
    283369                                if($deletedCount > 1) 
    284370                                        $message .= "es"; 
    285                                  
     371 
    286372                                $message .= " deleted. "; 
    287                                  
     373 
    288374                        }//endif removedCount 
    289375 
     
    294380                                $message .= "(Addresses marked primary or default ship to cannot be removed.)"; 
    295381                        return $message; 
    296                          
     382 
    297383                }//end method - delete 
    298384 
    299                  
     385 
    300386                function markPrimary(){ 
    301                  
     387 
    302388                        return $this->_markAs("primary")." primary address."; 
    303                  
     389 
    304390                }//end method - markPrimary 
    305                  
    306                  
     391 
     392 
    307393                function markDefaultShipTo(){ 
    308394 
     
    323409                                WHERE 
    324410                                        id =".((int) $this->idsArray[0]); 
    325                          
     411 
    326412                        $relatedInfo = $this->db->fetchArray($this->db->query($querystatement)); 
    327                          
     413 
    328414                        //Next, mark all addresses associated with record as false 
    329415                        $updatestatement = " 
     
    335421                                        tabledefid = ".$relatedInfo["tabledefid"]." 
    336422                                        AND recordid = ".$relatedInfo["recordid"]; 
    337                                          
     423 
    338424                        $this->db->query($updatestatement); 
    339                          
     425 
    340426                        //Finally, mark the first record. 
    341427                        $updatestatement = " 
     
    346432                                WHERE 
    347433                                        id = ".((int) $this->idsArray[0]); 
    348                          
     434 
    349435                        $this->db->query($updatestatement); 
    350436 
     
    357443 
    358444                        return $message; 
    359                          
     445 
    360446                }//end method _markAs 
    361447 
phpBMS vulnerability assesment provided by Orvant Inc. Copyright © 2010 Kreotek, LLC. All Rights reserved.