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/include/imports.php

    r433 r485  
    11<?php 
    22        class phpbmsImport{ 
    3                  
     3 
    44                var $table; 
    55                var $error = ""; 
     
    1010                var $data; 
    1111                var $revertID = 0; 
    12                  
     12 
    1313                // Do not manually override 
    1414                var $transactionRecords = array(); 
    1515                var $tempFileID = 0; 
    1616                var $pageType = "main"; 
    17                  
     17 
    1818                function phpbmsImport($table, $importType = "csv"){ 
    19                          
     19 
    2020                        $this->table = $table; 
    2121                        $this->importType = $importType; 
     
    2525                                break; 
    2626                        }//end switch 
    27                          
     27 
    2828                        $this->table->db->logError = true; 
    2929                        //So, that, when there is a db error, it will go all the way through and not just stop 
    3030                        $this->table->db->stopOnError = false; 
    31                         //Won't display db errors, just log them.        
     31                        //Won't display db errors, just log them. 
    3232                        $this->table->db->showError = false; 
    3333                        if(isset($_POST["pageType"])) 
    3434                                $this->pageType = $_POST["pageType"]; 
    35                          
     35 
    3636                        if(isset($_POST["tempFileID"])) 
    3737                                $this->tempFileID = ((int)$_POST["tempFileID"]); 
    38                          
     38 
    3939                }//end method --imports-- 
    40                  
     40 
    4141                function _parseFromData($data){ 
    42                          
     42 
    4343                        switch($this->importType){ 
    44                                  
     44 
    4545                                case "csv": 
    46                                          
     46 
    4747                                        if(is_readable($data)){ 
    4848                                                $contents = $this->_getFile($data); 
    49                                          
     49 
    5050                                                if(is_readable($contents)){ 
    5151                                                        $this->docError = "invalid csv document"; 
    5252                                                        return false; 
    5353                                                }//end if 
    54                                                  
     54 
    5555                                        }//end if 
    56                                          
     56 
    5757                                        $this->parser->parse($data); 
    58                                                  
     58 
    5959                                        if(!count($this->parser->titles) || !count($this->parser->data)){ 
    6060                                                $this->docError = "invalid csv document"; 
    61                                                 return false;    
     61                                                return false; 
    6262                                        }//end if 
    63                                          
     63 
    6464                                        return true; 
    6565                                break; 
    66                          
     66 
    6767                        }//end swtich 
    68                  
     68 
    6969                }//end method --_parseFromFile-- 
    70                  
    71                  
     70 
     71 
    7272                function _getTransactionData(){ 
    7373                //needs to be changed for more complicated tables 
     
    7575                        foreach($this->transactionIDs as $theid) 
    7676                                $inStatement .= $theid.","; 
    77                          
     77 
    7878                        if($inStatement){ 
    79                                  
     79 
    8080                                $inStatement = substr($inStatement, 0, -1); 
    81                          
     81 
    8282                                $querystatement = " 
    8383                                        SELECT 
     
    8888                                                `id` IN (".$inStatement."); 
    8989                                        "; 
    90                                  
     90 
    9191                                $queryresult = $this->table->db->query($querystatement); 
    92                                  
     92 
    9393                                while($therecord = $this->table->db->fetchArray($queryresult)) 
    9494                                        $this->transactionRecords[] = $therecord; 
    95                                          
    96                         }//end if  
    97                          
     95 
     96                        }//end if 
     97 
    9898                }//end method --_getTransactionData-- 
    99                  
    100                  
     99 
     100 
    101101                function _getFile($fileName){ 
    102102                        if(function_exists('file_get_contents')){ 
     
    106106                                $file = addslashes(fread(fopen($fileName, 'r'), filesize($fileName))); 
    107107                        }//end if 
    108                          
     108 
    109109                        return $file; 
    110110                }//end method --_getFile-- 
    111                  
     111 
    112112                //DO NOT CALL IN TRANSACTION 
    113113                function _storeTempCSV($fileName){ 
    114                          
     114 
    115115                        $querystatement = " 
    116116                                INSERT INTO 
     
    136136                                                '".$_SESSION["userinfo"]["id"]."', 
    137137                                                '".$_SESSION["userinfo"]["id"]."' 
    138                                                  
     138 
    139139                                        ) 
    140140                                "; 
    141                          
     141 
    142142                        $this->table->db->query($querystatement); 
    143                          
     143 
    144144                        $id = $this->table->db->insertId(); 
    145                          
     145 
    146146                        if($id) 
    147147                                $this->tempFileID = ((int) $id); 
    148148                        else 
    149149                                $this->error .= '<li> inserting temporary file failure </li>'; 
    150                          
     150 
    151151                }//end method --_storeTempCSV-- 
    152                  
    153                  
     152 
     153 
    154154                function _getTempCSV($tempFileID){ 
    155                          
     155 
    156156                        if($tempFileID){ 
    157                                  
     157 
    158158                                $querystatement = " 
    159159                                        SELECT 
     
    164164                                                id = ".((int)$tempFileID)." 
    165165                                        "; 
    166                                  
     166 
    167167                                $queryresult = $this->table->db->query($querystatement); 
    168                                  
     168 
    169169                                $therecord = $this->table->db->fetchArray($queryresult); 
    170                                  
     170 
    171171                                return $therecord["file"]; 
    172                          
     172 
    173173                        }//end if 
    174                          
     174 
    175175                        return false; 
    176                          
     176 
    177177                }//end method --_getTempCSV-- 
    178                  
     178 
    179179                //DO NOT CALL IN TRANSACTION 
    180180                function _removeTempCSV($tempFileID = 0){ 
    181                          
     181 
    182182                        $querystatement = " 
    183183                                DELETE FROM 
     
    192192                                        ); 
    193193                                "; 
    194                                  
     194 
    195195                        $queryresult = $this->table->db->query($querystatement); 
    196                          
     196 
    197197                        $querystatement = " 
    198198                                ALTER TABLE 
     
    200200                                AUTO_INCREMENT = ".((int) $tempFileID)."; 
    201201                                "; 
    202                          
     202 
    203203                        $queryresult = $this->table->db->query($querystatement); 
    204                          
     204 
    205205                }//end method --_removeTempCSV-- 
    206                  
     206 
    207207                //DO NOT USE THIS METHOD INSIDE AN OPEN TRANSACTION. 
    208208                //IT WILL AUTOMATICALLY COMMIT THE TRANSACTION 
    209209                function _revertAutoIncrement($revertID = 0){ 
    210                          
     210 
    211211                        //check to see if there is a revert id (i.e. there was a valid insert) 
    212212                        if($revertID) 
    213213                                if(is_numeric($revertID)){ 
    214                                          
     214 
    215215                                        $querystatement = " 
    216216                                                ALTER TABLE 
     
    218218                                                AUTO_INCREMENT = ".((int) $revertID)."; 
    219219                                                "; 
    220                                          
     220 
    221221                                        $this->table->db->query($querystatement); 
    222                                          
     222 
    223223                                }//end if 
    224                          
     224 
    225225                }//end method --_revertAutoIncrement-- 
    226                  
    227                  
     226 
     227 
    228228                function importRecords($rows, $titles){ 
    229                          
     229 
    230230                        switch($this->importType){ 
    231                                  
    232                                 case "csv":                      
     231 
     232                                case "csv": 
    233233                                        //count total fieldnames (top row of csv document) 
    234234                                        $fieldNum = count($titles); 
    235                                          
     235 
    236236                                        //the file starts at line number 1, but since line 1 is 
    237237                                        //supposed to be the fieldnames in the table(s), the lines 
    238238                                        //being insereted start @ 2. 
    239239                                        $rowNum = 2; 
    240                                          
     240 
    241241                                        //get the data one row at a time 
    242242                                        foreach($rows as $rowData){ 
    243                                                  
    244                                                 $theid = 0; 
    245                                                  
     243 
     244                                                $theid = 0; // set for when verifification does not pass 
     245                                                $verify = array(); //set for when number of field rows does not match number of titles 
     246 
    246247                                                //trim off leading/trailing spaces 
    247248                                                $trimmedRowData = array(); 
     249 
    248250                                                foreach($rowData as $name => $data) 
    249251                                                        $trimmedRowData[$name] = trim($data); 
    250                                                  
     252 
    251253                                                //check to see if number of fieldnames is consistent for each row 
    252254                                                $rowFieldNum = count($trimmedRowData); 
    253                                                  
     255 
    254256                                                //if valid, insert, if not, log error and don't insert. 
    255                                                 if($rowFieldNum == $fieldNum) 
    256                                                         $theid = $this->table->insertRecord($trimmedRowData); 
    257                                                 else 
     257                                                if($rowFieldNum == $fieldNum){ 
     258                                                        $verify = $this->table->verifyVariables($trimmedRowData); 
     259                                                        if(!count($verify)) 
     260                                                                $theid = $this->table->insertRecord($trimmedRowData); 
     261                                                }else 
    258262                                                        $this->error .= '<li> incorrect amount of fields for line number '.$rowNum.'.</li>'; 
    259                                                  
     263 
    260264                                                if($theid){ 
    261265                                                        //keep track of the ids in the transaction to be able to select them 
    262266                                                        //for preview purposes 
    263267                                                        $this->transactionIDs[] = $theid; 
    264                                                          
     268 
    265269                                                        //get first id to correct auto increment 
    266270                                                        if(!$this->revertID) 
     
    268272                                                }else 
    269273                                                        $this->error .= '<li> failed insert for line number '.$rowNum.'.</li>'; 
    270                                                  
     274 
     275                                                foreach($verify as $error) 
     276                                                        $this->error .= '<li class="subError">'.$error.'</li>'; 
     277 
    271278                                                $rowNum++; 
    272                                                  
     279 
    273280                                        }//end foreach 
    274281                                break; 
    275                                  
     282 
    276283                        }//end switch 
    277                          
     284 
    278285                }//end method --importRecords-- 
    279                  
    280                  
     286 
     287 
    281288                function displayTransaction($recordsArray, $fieldsArray){ 
    282289                //needs to be changed for more complicated tables 
     
    317324                                <?php 
    318325                        }//end if 
    319                          
     326 
    320327                }//end method --displayTransaction-- 
    321                  
    322                  
     328 
     329 
    323330                function processImportPage(){ 
    324                          
     331 
    325332                        $this->table->getTableInfo(); 
    326                          
     333 
    327334                        if(!isset($_POST["command"])){ 
    328                                  
     335 
    329336                                //happens upon first coming to page 
    330                                  
     337 
    331338                                //remove any other temporary csv files in the `files` table 
    332339                                //present from previous imports 
    333340                                $this->_removeTempCSV(); 
    334                                  
     341 
    335342                                //check to see if user has the rights to be here. 
    336343                                //If not, kick him to the no access page. 
    337344                                if(!hasRights($this->table->importroleid)) 
    338345                                        goURL(APP_PATH."noaccess.php"); 
    339                                  
     346 
    340347                        }else{ 
    341348                                //form has been submitted 
    342349                                switch($_POST["command"]){ 
    343                                          
     350 
    344351                                        //cancel button pressed. 
    345352                                        case "cancel": 
     
    354361                                                }//end if 
    355362                                        break; 
    356                                  
     363 
    357364                                        case "upload": 
    358                                                  
     365 
    359366                                                //check for valid file upload 
    360367                                                if(!$_FILES["import"]["error"] && ($_FILES["import"]["size"] > 0)){ 
    361                                                          
     368 
    362369                                                        //check and parse the file 
    363370                                                        if($this->_parseFromData($_FILES["import"]["tmp_name"])){ 
    364                                                                  
     371 
    365372                                                                //start transaction 
    366373                                                                $this->table->db->startTransaction(); 
    367                                                                  
     374 
    368375                                                                $this->importRecords($this->parser->data, $this->parser->titles); 
    369                                                                  
     376 
    370377                                                                //get data for preview purposes 
    371378                                                                $this->_getTransactionData(); 
    372379                                                                //"undo" any inserts 
    373380                                                                $this->table->db->rollbackTransaction(); 
    374                                                                  
     381 
    375382                                                                //DO NOT CALL IN TRANSACTION 
    376383                                                                //ALTER TABLES AUTO COMMIT AND THE FILE NEEDS TO CARRY 
     
    378385                                                                $this->_revertAutoIncrement($this->revertID); 
    379386                                                                $this->_storeTempCSV($_FILES["import"]["tmp_name"]); 
    380                                                                  
     387 
    381388                                                        }//end if 
    382                                                          
     389 
    383390                                                }else 
    384391                                                        $this->docError .= "failed file upload"; 
    385                                                  
     392 
    386393                                                //switch page types 
    387394                                                $this->pageType = "confirm"; 
    388                                                  
     395 
    389396                                                if(!$this->error && !$this->docError){ 
    390397                                                        $therecord["phpbmsStatus"] = "Confirm Import"; 
     
    394401                                                }else 
    395402                                                        $therecord["phpbmsStatus"] = "Import Error"; 
    396                                                  
     403 
    397404                                        break; 
    398                                          
     405 
    399406                                        case "import": 
    400                                                  
     407 
    401408                                                //get the contents of the stored csv document 
    402409                                                $CSVcontents = $this->_getTempCSV($this->tempFileID); 
    403                                                  
     410 
    404411                                                //parser uses newline character to be able to parse the last line 
    405412                                                if(substr($CSVcontents,-1,1) != "\n") 
    406413                                                        $CSVcontents .= "\n"; 
    407                                                  
    408                                                  
     414 
     415 
    409416                                                $this->parser->parse($CSVcontents); 
    410                                                  
     417 
    411418                                                $this->importRecords($this->parser->data, $this->parser->titles); 
    412                                                  
     419 
    413420                                                $this->table->db->commitTransaction(); 
    414                                                  
     421 
    415422                                                //DO NOT CALL IN TRANSACTION 
    416                                                  
     423 
    417424                                                //get rid of temporary csv document 
    418425                                                $this->_removeTempCSV($this->tempFileID); 
    419                                                  
     426 
    420427                                                $therecord["phpbmsStatus"] = "Record(s) Imported"; 
    421428                                                //change page type 
    422429                                                $this->pageType = "main"; 
    423430                                        break; 
    424                                          
     431 
    425432                                }//end command switch 
    426                                          
     433 
    427434                        }// end if 
    428                          
     435 
    429436                        //display the title 
    430437                        $therecord["title"] = $this->table->displayname." Import"; 
    431438                        return $therecord; 
    432                          
     439 
    433440                }//end method --imports-- 
    434                  
     441 
    435442        }//end class --imports-- 
    436          
    437          
     443 
     444 
    438445        //this class is to have different buttons, and no created/modified. 
    439446        if(class_exists("phpbmsForm")){ 
    440447                class importForm extends phpbmsForm{ 
    441                          
     448 
    442449                        function importForm($action = NULL, $method="post", $name="record", $onsubmit="return validateForm(this);", $dontSubmit = true){ 
    443                                  
     450 
    444451                                parent::phpbmsForm($action,$method,$name,$onsubmit,$dontSubmit); 
    445                                  
     452 
    446453                        }//end method --importForm-- 
    447                          
    448                         function startForm($pageTitle, $pageType){ 
    449                  
    450                                 ?><form action="<?php echo str_replace("&","&amp;",$this->action) ?>" method="<?php echo $this->method?>" name="<?php echo $this->name?>" onsubmit="<?php echo $this->onsubmit?>" <?php  
     454 
     455                        function startForm($pageTitle, $pageType, $numberOfRecords = 0){ 
     456 
     457                                ?><form action="<?php echo str_replace("&","&amp;",$this->action) ?>" method="<?php echo $this->method?>" name="<?php echo $this->name?>" onsubmit="<?php echo $this->onsubmit?>" <?php 
    451458                                        if(isset($this->enctype)) echo ' enctype="'.$this->enctype.'" '; 
    452459                                        if(isset($this->id)) echo ' id="'.$this->id.'" '; 
    453                                 ?>><?php  
     460                                ?>><?php 
    454461                                if($this->dontSubmit){ 
    455462                                        ?><div id="dontSubmit"><input type="submit" value=" " onclick="return false;" /></div><?php 
    456463                                } ?> 
    457                                 <div id="topButtons"><?php $this->showButtons(1, $pageType); ?></div> 
    458                                 <h1 id="h1Title"><span><?php echo $pageTitle ?></span></h1><?php         
    459                                  
     464                                <div id="topButtons"><?php $this->showButtons(1, $pageType, $numberOfRecords); ?></div> 
     465                                <h1 id="h1Title"><span><?php echo $pageTitle ?></span></h1><?php 
     466 
    460467                        }//end method --startForm-- 
    461                          
    462                         function showButtons($ids = 1, $pageType = "main"){ 
     468 
     469                        function showButtons($ids = 1, $pageType = "main", $numberOfRecords = 0){ 
    463470                                ?> 
    464471                                <div class="importCancels"> 
    465472                                        <?php if($pageType == "main"){ ?> 
    466                                         <input <?php if($ids==1) {?>accesskey="i"<?php }?> title="Upload (alt+u)" id="uploadButton<?php echo $ids?>" name="command" type="submit" value="upload" class="Buttons" /> 
     473                                        <input <?php if($ids==1) {?>accesskey="u"<?php }?> title="Upload (alt+u)" id="uploadButton<?php echo $ids?>" name="command" type="submit" value="upload" class="Buttons" /> 
    467474                                        <input id="cancelButton<?php echo $ids?>" name="command" type="submit" value="cancel" class="Buttons" <?php if($ids==1) {?>accesskey="x" <?php }?> title="(access key+x)" /> 
    468475                                        <?php }else{?> 
    469                                         <input type="submit" class="Buttons" value="import" name="command" id="import<?php echo $ids?>" title="commit"/> 
     476                                        <input type="submit" class="Buttons" value="import" name="command" id="import<?php echo $ids?>" title="commit" <?php echo ($numberOfRecords? '':'disabled="disabled"') ?>/> 
    470477                                        <input type="submit" class="Buttons" value="cancel" name="command" id="cancelButton<?php echo $ids?>" title="rollback"/> 
    471478                                        <?php }//end if ?> 
    472479                                </div><?php 
    473480                        }//end method --showButtons-- 
    474                  
     481 
    475482                }//end class --importForm-- 
    476483        } 
phpBMS vulnerability assesment provided by Orvant Inc. Copyright © 2010 Kreotek, LLC. All Rights reserved.