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/tables.php

    r427 r485  
    3939 
    4040 
    41 class phpbmsTable{ 
    42          
    43         var $db = NULL; 
    44         var $backurl = NULL; 
    45          
    46         // The table definition record id. 
    47         var $id=0; 
    48          
    49         var $fields = array(); 
    50  
    51         function phpbmsTable($db,$tabledefid = 0,$backurl = NULL){ 
    52          
    53                 if(is_object($db)) 
    54                         if(get_class($db)=="db") 
    55                                 $this->db = $db; 
    56                 if($this->db === NULL) 
    57                         $error = new appError(-800,"database object is required for parameter 1.","Initializing phpbmsTable Class"); 
    58  
    59                 $this->id = ((int) $tabledefid); 
    60                  
    61                 if($backurl == NULL) 
    62                         $this->backurl = APP_PATH."search.php?id=".$this->id; 
    63                 else  
    64                         $this->backurl = $backurl; 
    65                          
    66                 if(!$this->getTableInfo()) 
    67                         $error = new appError(-810,"Table definition not found for id ".$this->id,"Initializing phpbmsTable Class"); 
    68         } 
    69          
    70          
    71         function getTableInfo(){ 
    72                 $querystatement = "SELECT * FROM tabledefs WHERE id=".$this->id; 
    73                  
    74                 $queryresult = $this->db->query($querystatement); 
    75                  
    76                 if($this->db->numRows($queryresult)){ 
    77                         foreach($this->db->fetchArray($queryresult) as $key => $value) 
    78                                 $this->$key = $value; 
    79                          
    80                         $this->fields = $this->db->tableInfo($this->maintable); 
    81                          
    82                         return true;                                             
    83                 } else 
    84                         return false; 
    85         } 
    86          
    87         function getDefaultByType($fieldtype){ 
    88                         $default = NULL; 
    89                          
    90                         switch ($fieldtype){ 
    91                                 case "blob": 
    92                                 case "string": 
    93                                         $default = ""; 
    94                                 break; 
    95                                 case "real": 
    96                                 case "int": 
    97                                         $default = 0; 
    98                                 break; 
    99                                 case "date": 
    100                                         $default=dateToString(mktime(),"SQL"); 
    101                                 break; 
    102                                 case "time": 
    103                                         $default=timeToString(mktime(),"SQL"); 
    104                                 break; 
    105                                 case "year": 
    106                                         $default=strftime("%Y"); 
    107                                 break; 
    108                                 case "datetime": 
    109                                 case "timestamp": 
    110                                         $default = dateToString(mktime(),"SQL")." ".timeToString(mktime(),"24 Hour"); 
    111                                 break; 
    112                         } 
    113                          
    114                         return $default; 
    115                          
    116         } 
    117          
    118         function prepareFieldForSQL($value,$type,$flags){ 
    119                         switch ($type){ 
    120                                  
    121                                 case "blob": 
    122                                 case "string": 
    123                                         if($value === "" or $value === NULL){ 
    124                                                 if(strpos($flags,"not_null") === false) 
    125                                                         $value = NULL; 
    126                                                 else 
    127                                                         $value = "''"; 
    128                                         } else                                   
    129                                                 $value = "'".$value."'"; 
    130                                 break; 
    131                                  
    132                                 case "real": 
    133                                         if($value === "" or $value === NULL){ 
    134                                                 if(strpos($flags,"not_null") === false) 
    135                                                         $value = NULL; 
    136                                                 else 
    137                                                         $value = 0; 
    138                                         } else                                   
    139                                                 $value = (real) $value; 
    140                                 break; 
    141                                  
    142                                 case "int": 
    143                                         if($value === "" or $value === NULL){ 
    144                                                 if(strpos($flags,"not_null") === false) 
    145                                                         $value = NULL; 
    146                                                 else 
    147                                                         $value = 0; 
    148                                         } else 
    149                                                 $value = (int) $value; 
    150                                 break; 
    151                                  
    152                                 case "date": 
    153                                         if($value === "" or $value === NULL){ 
    154                                                 if(strpos($flags,"not_null") === false) 
    155                                                         $value = NULL; 
    156                                                 else 
    157                                                         $value = "'".dateToString(mktime(),"SQL")."'"; 
    158                                         } else 
    159                                                 $value = "'".sqlDateFromString($value)."'"; 
    160                                 break; 
    161  
    162                                 case "time": 
    163                                         if($value === "" or $value === NULL){ 
    164                                                 if(strpos($flags,"not_null") === false) 
    165                                                         $value = NULL; 
    166                                                 else 
    167                                                         $value = "'".timeToString(mktime(),"SQL")."'"; 
    168                                         } else 
    169                                                 $value = "'".sqlTimeFromString($value)."'"; 
    170                                 break; 
    171                                  
    172                                 case "year": 
    173                                         if($value === "" or $value === NULL) 
    174                                                 if(strpos($flags,"not_null") === false) 
    175                                                         $value = NULL; 
    176                                                 else 
    177                                                         $value = strftime("%Y"); 
    178                                 break; 
    179                                  
    180                                 case "datetime": 
    181                                 case "timestamp": 
    182                                         if($value === "" or $value === NULL){ 
    183                                                 if(strpos($flags,"not_null") === false) 
    184                                                         $value = NULL; 
    185                                                 else 
    186                                                         $value = "'".dateToString(mktime(),"SQL")." ".timeToString(mktime(),"24 Hour")."'"; 
    187                                         } else{ 
    188                                                 $datetimearray = explode(" ",$value); 
    189                                                 if(count($datetimearray) > 1){ 
    190                                                         $value = "'".sqlDateFromString($datetimearray[0])." ".sqlTimeFromString($datetimearray[1])."'"; 
    191                                                 } else  
    192                                                 $value = "'".$value."'"; 
    193                                         } 
    194                                 break; 
    195                                 case "password": 
    196                                         $value = "ENCODE('".$value."','".ENCRYPTION_SEED."')"; 
    197                                 break;           
    198                         }//end case 
    199  
    200  
    201                         if($value === NULL) 
    202                                 $value = "NULL"; 
    203                         return $value; 
    204         }//end method 
    205          
    206          
    207         function getDefaults(){ 
    208                 $therecord = array(); 
    209                  
    210                 foreach($this->fields as $fieldname => $thefield){ 
    211                         switch($fieldname){ 
    212                                 case "id": 
    213                                 case "modifiedby":                       
    214                                 case "modifieddate": 
    215                                         $therecord[$fieldname] = NULL; 
    216                                 break; 
    217                                  
    218                                 case "createdby": 
    219                                         $therecord["createdby"] = $_SESSION["userinfo"]["id"]; 
    220                                 break; 
    221                                                                  
    222                                 default: 
    223                                         if(strpos($thefield["flags"],"not_null") === false) 
    224                                                 $therecord[$fieldname] = NULL; 
    225                                         else { 
    226                                                 $therecord[$fieldname] = $this->getDefaultByType($thefield["type"]); 
    227                                         } 
    228                                 break; 
    229                         }//end switch 
    230                 }//end foreach 
    231                  
    232                 return $therecord; 
    233         } 
    234          
    235          
    236         function getRecord($id = 0){ 
    237                 $id = (int) $id; 
    238                  
    239                 $querystatement = "SELECT "; 
    240                                  
    241                 foreach($this->fields as $fieldname => $thefield){ 
    242                         if(isset($thefield["select"])) 
    243                                 $querystatement .= "(".$thefield["select"].") AS `".$fieldname."`, "; 
     41        class phpbmsTable{ 
     42 
     43                var $db = NULL; 
     44                var $backurl = NULL; 
     45                var $verifyErrors = array(); 
     46 
     47                // The table definition record id. 
     48                var $id=0; 
     49 
     50                var $fields = array(); 
     51 
     52                function phpbmsTable($db,$tabledefid = 0,$backurl = NULL){ 
     53 
     54                        if(is_object($db)) 
     55                                if(get_class($db)=="db") 
     56                                        $this->db = $db; 
     57                        if($this->db === NULL) 
     58                                $error = new appError(-800,"database object is required for parameter 1.","Initializing phpbmsTable Class"); 
     59 
     60                        $this->id = ((int) $tabledefid); 
     61 
     62                        if($backurl == NULL) 
     63                                $this->backurl = APP_PATH."search.php?id=".$this->id; 
    24464                        else 
    245                                 $querystatement .= "`".$fieldname."`, "; 
    246                 }//end foreach 
    247                 $querystatement = substr($querystatement, 0, strlen($querystatement)-2); 
    248                  
    249                 $querystatement .= " FROM `".$this->maintable."` WHERE `".$this->maintable."`.`id` = ".$id; 
    250                                  
    251                 $queryresult = $this->db->query($querystatement); 
    252                  
    253                 if($this->db->numRows($queryresult)) 
    254                         $therecord = $this->db->fetchArray($queryresult); 
    255                 else  
    256                         $therecord = $this-> getDefaults(); 
    257                  
    258                 return $therecord; 
    259         }//end getRecord function 
    260          
    261          
    262         function updateRecord($variables, $modifiedby = NULL){ 
    263                 $variables = addSlashesToArray($variables); 
    264                  
    265                 if($modifiedby === NULL) 
    266                         if(isset($_SESSION["userinfo"]["id"])) 
    267                                 $modifiedby = $_SESSION["userinfo"]["id"]; 
    268                         else 
    269                                 $error = new appError(-840,"Session Timed Out.","Creating New Record"); 
    270  
    271                 if(!isset($variables["id"])) 
    272                         $error = new appError(-820,"id not set","Updating Record"); 
    273                  
    274                 $updatestatement = "UPDATE `".$this->maintable."` SET "; 
    275                  
    276                 foreach($this->fields as $fieldname => $thefield){ 
    277                         if(!isset($thefield["select"])){ 
     65                                $this->backurl = $backurl; 
     66 
     67                        if(!$this->getTableInfo()) 
     68                                $error = new appError(-810,"Table definition not found for id ".$this->id,"Initializing phpbmsTable Class"); 
     69                } 
     70 
     71 
     72                function getTableInfo(){ 
     73                        $querystatement = "SELECT * FROM tabledefs WHERE id=".$this->id; 
     74 
     75                        $queryresult = $this->db->query($querystatement); 
     76 
     77                        if($this->db->numRows($queryresult)){ 
     78                                foreach($this->db->fetchArray($queryresult) as $key => $value) 
     79                                        $this->$key = $value; 
     80 
     81                                $this->fields = $this->db->tableInfo($this->maintable); 
     82 
     83                                return true; 
     84                        } else 
     85                                return false; 
     86                } 
     87 
     88                function getDefaultByType($fieldtype){ 
     89                                $default = NULL; 
     90 
     91                                switch ($fieldtype){ 
     92                                        case "blob": 
     93                                        case "string": 
     94                                                $default = ""; 
     95                                        break; 
     96                                        case "real": 
     97                                        case "int": 
     98                                                $default = 0; 
     99                                        break; 
     100                                        case "date": 
     101                                                $default=dateToString(mktime(),"SQL"); 
     102                                        break; 
     103                                        case "time": 
     104                                                $default=timeToString(mktime(),"SQL"); 
     105                                        break; 
     106                                        case "year": 
     107                                                $default=strftime("%Y"); 
     108                                        break; 
     109                                        case "datetime": 
     110                                        case "timestamp": 
     111                                                $default = dateToString(mktime(),"SQL")." ".timeToString(mktime(),"24 Hour"); 
     112                                        break; 
     113                                } 
     114 
     115                                return $default; 
     116 
     117                } 
     118 
     119                function prepareFieldForSQL($value,$type,$flags){ 
     120                                switch ($type){ 
     121 
     122                                        case "blob": 
     123                                        case "string": 
     124                                                if($value === "" or $value === NULL){ 
     125                                                        if(strpos($flags,"not_null") === false) 
     126                                                                $value = NULL; 
     127                                                        else 
     128                                                                $value = "''"; 
     129                                                } else 
     130                                                        $value = "'".$value."'"; 
     131                                        break; 
     132 
     133                                        case "real": 
     134                                                if($value === "" or $value === NULL){ 
     135                                                        if(strpos($flags,"not_null") === false) 
     136                                                                $value = NULL; 
     137                                                        else 
     138                                                                $value = 0; 
     139                                                } else 
     140                                                        $value = (real) $value; 
     141                                        break; 
     142 
     143                                        case "int": 
     144                                                if($value === "" or $value === NULL){ 
     145                                                        if(strpos($flags,"not_null") === false) 
     146                                                                $value = NULL; 
     147                                                        else 
     148                                                                $value = 0; 
     149                                                } else 
     150                                                        $value = (int) $value; 
     151                                        break; 
     152 
     153                                        case "date": 
     154                                                if($value === "" or $value === NULL){ 
     155                                                        if(strpos($flags,"not_null") === false) 
     156                                                                $value = NULL; 
     157                                                        else 
     158                                                                $value = "'".dateToString(mktime(),"SQL")."'"; 
     159                                                } else 
     160                                                        $value = "'".sqlDateFromString($value)."'"; 
     161                                        break; 
     162 
     163                                        case "time": 
     164                                                if($value === "" or $value === NULL){ 
     165                                                        if(strpos($flags,"not_null") === false) 
     166                                                                $value = NULL; 
     167                                                        else 
     168                                                                $value = "'".timeToString(mktime(),"SQL")."'"; 
     169                                                } else 
     170                                                        $value = "'".sqlTimeFromString($value)."'"; 
     171                                        break; 
     172 
     173                                        case "year": 
     174                                                if($value === "" or $value === NULL) 
     175                                                        if(strpos($flags,"not_null") === false) 
     176                                                                $value = NULL; 
     177                                                        else 
     178                                                                $value = strftime("%Y"); 
     179                                        break; 
     180 
     181                                        case "datetime": 
     182                                        case "timestamp": 
     183                                                if($value === "" or $value === NULL){ 
     184                                                        if(strpos($flags,"not_null") === false) 
     185                                                                $value = NULL; 
     186                                                        else 
     187                                                                $value = "'".dateToString(mktime(),"SQL")." ".timeToString(mktime(),"24 Hour")."'"; 
     188                                                } else{ 
     189                                                        $datetimearray = explode(" ",$value); 
     190                                                        if(count($datetimearray) > 1){ 
     191                                                                $value = "'".sqlDateFromString($datetimearray[0])." ".sqlTimeFromString($datetimearray[1])."'"; 
     192                                                        } else 
     193                                                        $value = "'".$value."'"; 
     194                                                } 
     195                                        break; 
     196                                        case "password": 
     197                                                $value = "ENCODE('".$value."','".ENCRYPTION_SEED."')"; 
     198                                        break; 
     199                                }//end case 
     200 
     201 
     202                                if($value === NULL) 
     203                                        $value = "NULL"; 
     204                                return $value; 
     205                }//end method 
     206 
     207 
     208                function getDefaults(){ 
     209                        $therecord = array(); 
     210 
     211                        foreach($this->fields as $fieldname => $thefield){ 
    278212                                switch($fieldname){ 
    279213                                        case "id": 
    280                                         case "creationdate": 
     214                                        case "modifiedby": 
     215                                        case "modifieddate": 
     216                                                $therecord[$fieldname] = NULL; 
     217                                        break; 
     218 
    281219                                        case "createdby": 
    282                                         break; 
     220                                                $therecord["createdby"] = $_SESSION["userinfo"]["id"]; 
     221                                        break; 
     222 
     223                                        default: 
     224                                                if(strpos($thefield["flags"],"not_null") === false) 
     225                                                        $therecord[$fieldname] = NULL; 
     226                                                else { 
     227                                                        $therecord[$fieldname] = $this->getDefaultByType($thefield["type"]); 
     228                                                } 
     229                                        break; 
     230                                }//end switch 
     231                        }//end foreach 
     232 
     233                        return $therecord; 
     234                } 
     235 
     236 
     237                function getRecord($id = 0){ 
     238                        $id = (int) $id; 
     239 
     240                        $querystatement = "SELECT "; 
     241 
     242                        foreach($this->fields as $fieldname => $thefield){ 
     243                                if(isset($thefield["select"])) 
     244                                        $querystatement .= "(".$thefield["select"].") AS `".$fieldname."`, "; 
     245                                else 
     246                                        $querystatement .= "`".$fieldname."`, "; 
     247                        }//end foreach 
     248                        $querystatement = substr($querystatement, 0, strlen($querystatement)-2); 
     249 
     250                        $querystatement .= " FROM `".$this->maintable."` WHERE `".$this->maintable."`.`id` = ".$id; 
     251 
     252                        $queryresult = $this->db->query($querystatement); 
     253 
     254                        if($this->db->numRows($queryresult)) 
     255                                $therecord = $this->db->fetchArray($queryresult); 
     256                        else 
     257                                $therecord = $this-> getDefaults(); 
     258 
     259                        return $therecord; 
     260                }//end getRecord function 
     261 
     262 
     263                function prepareVariables($variables){ 
     264 
     265                        return $variables; 
     266 
     267                }//end method --prepareVariables-- 
     268 
     269 
     270                function verifyVariables($variables){ 
     271 
     272                        $thereturn = array(); 
     273 
     274                        if(!isset($this->verifyErrors)) 
     275                                $this->verifyErrors = array(); 
     276 
     277                        if(isset($variables["id"])) 
     278                                if(!is_numeric($variables["id"]) && $variables["id"]) 
     279                                        $this->verifyErrors[] = "The `id` field must be numeric or equivalent to zero (although positive is reccomended)."; 
     280 
     281                        if(isset($variables["inactive"])) 
     282                                if($variables["inactive"] && $variables["inactive"] != 1) 
     283                                        $this->verifyErrors[] = "The `inactive` field must be a boolean (equivalent to 0 or exactly 1)."; 
    283284                                         
    284                                         case "modifiedby": 
    285                                                 $updatestatement .= "`modifiedby` = ".((int) $modifiedby).", "; 
    286                                         break; 
    287          
    288                                         case "modifieddate": 
    289                                                 $updatestatement .= "`modifieddate` = NOW(), "; 
    290                                         break; 
    291                                          
    292                                         default: 
    293                                                 if(!isset($variables[$fieldname]) && strpos($thefield["flags"],"not_null") !== false) 
    294                                                         $variables[$fieldname] = $this->getDefaultByType($thefield["type"],true); 
    295                                                  
    296                                                 if(isset($variables[$fieldname])) 
    297                                                         $updatestatement .= "`".$fieldname."` = ".$this->prepareFieldForSQL($variables[$fieldname],$thefield["type"],$thefield["flags"]).", "; 
    298                                         break; 
    299                                 }//end switch field name 
    300                         }//end if 
    301                 }//end foreach 
    302                 $updatestatement = substr($updatestatement, 0, strlen($updatestatement)-2); 
    303                  
    304                 $updatestatement .= " WHERE `id`=".((int) $variables["id"]); 
    305  
    306                 $updateresult = $this->db->query($updatestatement); 
    307                  
    308                 return true; 
    309         } 
    310          
    311          
    312         function insertRecord($variables,$createdby = NULL, $overrideID = false){ 
    313          
    314                 if($createdby === NULL) 
    315                         if(isset($_SESSION["userinfo"]["id"])) 
    316                                 $createdby = $_SESSION["userinfo"]["id"]; 
     285                        if(count($this->verifyErrors)) 
     286                                $thereturn = $this->verifyErrors; 
     287 
     288                        unset($this->verifyErrors); 
     289 
     290                        return $thereturn; 
     291 
     292                }//end method --verifyVariables-- 
     293 
     294 
     295                function updateRecord($variables, $modifiedby = NULL){ 
     296 
     297                        $variables = addSlashesToArray($variables); 
     298 
     299                        if($modifiedby === NULL) 
     300                                if(isset($_SESSION["userinfo"]["id"])) 
     301                                        $modifiedby = $_SESSION["userinfo"]["id"]; 
     302                                else 
     303                                        $error = new appError(-840,"Session Timed Out.","Creating New Record"); 
     304 
     305                        if(!isset($variables["id"])) 
     306                                $error = new appError(-820,"id not set","Updating Record"); 
     307 
     308                        $updatestatement = "UPDATE `".$this->maintable."` SET "; 
     309 
     310                        foreach($this->fields as $fieldname => $thefield){ 
     311                                if(!isset($thefield["select"])){ 
     312                                        switch($fieldname){ 
     313                                                case "id": 
     314                                                case "creationdate": 
     315                                                case "createdby": 
     316                                                break; 
     317 
     318                                                case "modifiedby": 
     319                                                        $updatestatement .= "`modifiedby` = ".((int) $modifiedby).", "; 
     320                                                break; 
     321 
     322                                                case "modifieddate": 
     323                                                        $updatestatement .= "`modifieddate` = NOW(), "; 
     324                                                break; 
     325 
     326                                                default: 
     327                                                        if(!isset($variables[$fieldname]) && strpos($thefield["flags"],"not_null") !== false) 
     328                                                                $variables[$fieldname] = $this->getDefaultByType($thefield["type"],true); 
     329 
     330                                                        if(isset($variables[$fieldname])) 
     331                                                                $updatestatement .= "`".$fieldname."` = ".$this->prepareFieldForSQL($variables[$fieldname],$thefield["type"],$thefield["flags"]).", "; 
     332                                                break; 
     333                                        }//end switch field name 
     334                                }//end if 
     335                        }//end foreach 
     336                        $updatestatement = substr($updatestatement, 0, strlen($updatestatement)-2); 
     337 
     338                        $updatestatement .= " WHERE `id`=".((int) $variables["id"]); 
     339 
     340                        $updateresult = $this->db->query($updatestatement); 
     341 
     342 
     343                        return true; 
     344                } 
     345 
     346 
     347                function insertRecord($variables,$createdby = NULL, $overrideID = false){ 
     348 
     349                        if($createdby === NULL) 
     350                                if(isset($_SESSION["userinfo"]["id"])) 
     351                                        $createdby = $_SESSION["userinfo"]["id"]; 
     352                                else 
     353                                        $error = new appError(-840,"Session Timed Out.","Creating New Record"); 
     354 
     355 
     356                        $variables = addSlashesToArray($variables); 
     357 
     358                        $fieldlist = ""; 
     359                        $insertvalues = ""; 
     360                        foreach($this->fields as $fieldname => $thefield){ 
     361                                if(!isset($thefield["select"])){ 
     362                                        switch($fieldname){ 
     363                                                case "id": 
     364                                                        if(isset($variables["id"])) 
     365                                                                if($overrideID && $variables["id"]){ 
     366                                                                        $fieldlist .= "id, "; 
     367                                                                        $insertvalues .= ((int) $variables["id"]).", "; 
     368                                                                }//endif 
     369                                                        break; 
     370 
     371                                                case "createdby": 
     372                                                case "modifiedby": 
     373                                                        $fieldlist .= $fieldname.", "; 
     374                                                        $insertvalues .= ((int) $createdby).", "; 
     375                                                        break; 
     376 
     377                                                case "creationdate": 
     378                                                case "modifieddate": 
     379                                                        $fieldlist .= $fieldname.", "; 
     380                                                        $insertvalues .= "NOW(), "; 
     381                                                        break; 
     382 
     383                                                default: 
     384                                                        if(!isset($variables[$fieldname]) && strpos($thefield["flags"],"not_null") !== false) 
     385                                                                $variables[$fieldname] = $this->getDefaultByType($thefield["type"],true); 
     386 
     387                                                        if(isset($variables[$fieldname])){ 
     388                                                                $fieldlist .= "`".$fieldname."`, "; 
     389                                                                $insertvalues .= $this->prepareFieldForSQL($variables[$fieldname],$thefield["type"],$thefield["flags"]).", "; 
     390                                                        }//endif - fieldname 
     391                                                        break; 
     392                                        }//end switch field name 
     393                                }//end if 
     394                        }//end foreach 
     395                        $fieldlist = substr($fieldlist, 0, strlen($fieldlist)-2); 
     396                        $insertvalues = substr($insertvalues, 0, strlen($insertvalues)-2); 
     397 
     398                        $insertstatement = "INSERT INTO ".$this->maintable." (".$fieldlist.") VALUES (".$insertvalues.")"; 
     399                        $insertresult = $this->db->query($insertstatement); 
     400 
     401                        if($insertresult) 
     402                                return $this->db->insertId(); 
    317403                        else 
    318                                 $error = new appError(-840,"Session Timed Out.","Creating New Record"); 
    319  
    320                  
    321                 $variables = addSlashesToArray($variables); 
    322  
    323                 $fieldlist = ""; 
    324                 $insertvalues = ""; 
    325                 foreach($this->fields as $fieldname => $thefield){ 
    326                         if(!isset($thefield["select"])){ 
    327                                 switch($fieldname){ 
    328                                         case "id": 
    329                                                 if(isset($variables["id"])) 
    330                                                         if($overrideID && $variables["id"]){ 
    331                                                                 $fieldlist .= "id, "; 
    332                                                                 $insertvalues .= ((int) $variables["id"]).", "; 
    333                                                         }//endif 
    334                                                 break; 
    335                                          
    336                                         case "createdby": 
    337                                         case "modifiedby": 
    338                                                 $fieldlist .= $fieldname.", ";                                   
    339                                                 $insertvalues .= ((int) $createdby).", "; 
    340                                                 break; 
    341          
    342                                         case "creationdate": 
    343                                         case "modifieddate": 
    344                                                 $fieldlist .= $fieldname.", "; 
    345                                                 $insertvalues .= "NOW(), "; 
    346                                                 break; 
    347                                          
    348                                         default: 
    349                                                 if(!isset($variables[$fieldname]) && strpos($thefield["flags"],"not_null") !== false) 
    350                                                         $variables[$fieldname] = $this->getDefaultByType($thefield["type"],true); 
    351                                                  
    352                                                 if(isset($variables[$fieldname])){ 
    353                                                         $fieldlist .= "`".$fieldname."`, "; 
    354                                                         $insertvalues .= $this->prepareFieldForSQL($variables[$fieldname],$thefield["type"],$thefield["flags"]).", "; 
    355                                                 }//endif - fieldname 
    356                                                 break; 
    357                                 }//end switch field name 
    358                         }//end if 
    359                 }//end foreach 
    360                 $fieldlist = substr($fieldlist, 0, strlen($fieldlist)-2); 
    361                 $insertvalues = substr($insertvalues, 0, strlen($insertvalues)-2); 
    362  
    363                 $insertstatement = "INSERT INTO ".$this->maintable." (".$fieldlist.") VALUES (".$insertvalues.")"; 
    364                 $insertresult = $this->db->query($insertstatement); 
    365                  
    366                 if($insertresult) 
    367                         return $this->db->insertId(); 
    368                 else 
    369                         return false; 
    370         } 
    371          
    372          
    373         function processAddEditPage(){ 
    374                 if(!isset($_POST["command"])){ 
    375                          
    376                         if(isset($_GET["id"])){ 
    377                                 //editing 
    378                                 if(!hasRights($this->editroleid)) 
    379                                         goURL(APP_PATH."noaccess.php"); 
    380                                 else 
    381                                         return $this->getRecord((integer) $_GET["id"]); 
    382                         } else { 
    383                                 if(!hasRights($this->addroleid)) 
    384                                         goURL(APP_PATH."noaccess.php"); 
    385                                 else     
    386                                         return $this->getDefaults(); 
     404                                return false; 
     405                } 
     406 
     407 
     408                function processAddEditPage(){ 
     409                        if(!isset($_POST["command"])){ 
     410 
     411                                if(isset($_GET["id"])){ 
     412                                        //editing 
     413                                        if(!hasRights($this->editroleid)) 
     414                                                goURL(APP_PATH."noaccess.php"); 
     415                                        else 
     416                                                return $this->getRecord((integer) $_GET["id"]); 
     417                                } else { 
     418                                        if(!hasRights($this->addroleid)) 
     419                                                goURL(APP_PATH."noaccess.php"); 
     420                                        else 
     421                                                return $this->getDefaults(); 
     422                                } 
    387423                        } 
    388                 } 
    389                 else 
    390                 { 
    391                         switch($_POST["command"]){ 
    392                                 case "cancel": 
    393                                         // if we needed to do any clean up (deleteing temp line items) 
    394                                         if(!isset($_POST["id"])) $_POST["id"]=0; 
    395  
    396                                         $theurl = $this->backurl; 
    397                                          
    398                                         if(isset($_POST["id"])) 
    399                                                 $theurl .= "#".((int) $_POST["id"]); 
    400                                         goURL($theurl);                                  
    401                                 break; 
    402                                 case "save": 
    403                                         if($_POST["id"]) { 
    404                                                 $this->updateRecord($_POST);                                             
    405                                                 $theid = $_POST["id"]; 
    406                                                 //get record 
    407                                                 $therecord = $this->getRecord($theid); 
    408                                                 $therecord["phpbmsStatus"]="Record Updated"; 
    409  
    410                                                 return $therecord; 
    411                                         } 
    412                                         else { 
    413                                                 $theid = $this->insertRecord($_POST); 
    414                                                  
    415                                                 //get record 
    416                                                 $therecord=$this->getRecord($theid); 
    417                                                 $therecord["phpbmsStatus"] = "<div style=\"float:right;margin-top:-3px;\"><button type=\"button\" class=\"smallButtons\" onclick=\"document.location='".str_replace("&","&amp;",$_SERVER["REQUEST_URI"])."'\">add new</button></div>"; 
    418                                                 $therecord["phpbmsStatus"] .= "Record Created"; 
    419                                                  
    420                                                 return $therecord; 
    421                                         } 
    422                                 break; 
    423                         }//end command switch                    
    424                 }// end if 
    425         }// end function 
    426 } 
     424                        else 
     425                        { 
     426                                switch($_POST["command"]){ 
     427                                        case "cancel": 
     428                                                // if we needed to do any clean up (deleteing temp line items) 
     429                                                if(!isset($_POST["id"])) $_POST["id"]=0; 
     430 
     431                                                $theurl = $this->backurl; 
     432 
     433                                                if(isset($_POST["id"])) 
     434                                                        $theurl .= "#".((int) $_POST["id"]); 
     435                                                goURL($theurl); 
     436                                        break; 
     437                                        case "save": 
     438 
     439                                                $variables = $this->prepareVariables($_POST); 
     440                                                $errorArray = $this->verifyVariables($variables); 
     441 
     442                                                if($_POST["id"]) { 
     443 
     444                                                        $theid = $variables["id"]; 
     445 
     446                                                        if(!count($errorArray)){ 
     447 
     448                                                                $this->updateRecord($variables); 
     449 
     450                                                                //get record 
     451                                                                $therecord = $this->getRecord($theid); 
     452                                                                $therecord["phpbmsStatus"] = "Record Updated"; 
     453                                                        }else{ 
     454                                                                foreach($errorArray as $error) 
     455                                                                        $logError = new appError(-900, $error, "Verification Error"); 
     456 
     457                                                                //get record 
     458                                                                $therecord = $this->getRecord($theid); 
     459                                                                $therecord["phpbmsStatus"] = "Data Verification Error"; 
     460                                                        }//end if 
     461 
     462 
     463 
     464                                                        return $therecord; 
     465                                                } 
     466                                                else { 
     467 
     468                                                        $theid = 0; 
     469 
     470                                                        if(!count($errorArray)){ 
     471                                                                $theid = $this->insertRecord($variables); 
     472                                                                //get record 
     473                                                                $therecord = $this->getRecord($theid); 
     474                                                                $therecord["phpbmsStatus"] = "<div style=\"float:right;margin-top:-3px;\"><button type=\"button\" class=\"smallButtons\" onclick=\"document.location='".str_replace("&","&amp;",$_SERVER["REQUEST_URI"])."'\">add new</button></div>"; 
     475                                                                $therecord["phpbmsStatus"] .= "Record Created"; 
     476                                                        }else{ 
     477                                                                foreach($errorArray as $error) 
     478                                                                        $logError = new appError(-900, $error, "Verification Error"); 
     479 
     480                                                                //get record 
     481                                                                $therecord = $this->getRecord($theid); 
     482                                                                $therecord["phpbmsStatus"] .= "Data Verification Error"; 
     483                                                        }//end if 
     484 
     485                                                        return $therecord; 
     486                                                } 
     487                                        break; 
     488                                }//end command switch 
     489                        }// end if 
     490                }// end function 
     491        }//end class 
    427492?> 
phpBMS vulnerability assesment provided by Orvant Inc. Copyright © 2010 Kreotek, LLC. All Rights reserved.