| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Trapdirector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Trapdirector\Logging; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use PDO; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | class Mib | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     protected $logging; //< logging class | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     protected $trapsDB; //< Database class | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |      * Setup Mib Class | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |      * @param Logging $logClass : where to log | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |      * @param Database $dbClass : Database | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     function __construct($logClass,$dbClass) | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         $this->logging=$logClass; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         $this->trapsDB=$dbClass;        | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** Create database schema | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      *	@param $schema_file	string File to read schema from | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      *	@param $table_prefix string to replace #PREFIX# in schema file by this | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     public function create_schema($schema_file,$table_prefix) | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         //Read data from snmptrapd from stdin | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $input_stream=fopen($schema_file, 'r'); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         if ($input_stream=== false) | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             $this->logging->log("Error reading schema !",ERROR,''); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         $newline=''; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $cur_table=''; | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $cur_table_array=array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $db_conn=$this->trapsDB->db_connect_trap(); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         while (($line=fgets($input_stream)) !== false) | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |             if (preg_match('/; *$/', $newline)) | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |                 $sql= $newline; | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |                 if ($db_conn->query($sql) === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                     $this->logging->log('Error create schema : '.$sql,ERROR,''); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |                     return; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |                 if (preg_match('/^ *CREATE TABLE ([^ ]+)/',$newline,$cur_table_array)) | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                     $cur_table='table '.$cur_table_array[1]; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |                 else | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |                 { | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |                     $cur_table='secret SQL stuff :-)'; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |                 $this->logging->log('Creating : ' . $cur_table,INFO ); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 $newline=''; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         $sql= $newline; | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         if ($sql != '' ) | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |             if ($db_conn->query($sql) === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |                 $this->logging->log('Error create schema : '.$sql,ERROR,''); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |                 return; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         $this->logging->log('Schema created',INFO); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * Update database schema from current (as set in db) to $target_version | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      *     @param $prefix string file prefix of sql update File | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      *     @param $target_version int target db version number | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      *     @param $table_prefix string to replace #PREFIX# in schema file by this | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      *     @param bool $getmsg : only get messages from version upgrades | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      *     @return string : if $getmsg=true, return messages. | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public function update_schema($prefix,$target_version,$table_prefix,$getmsg=false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         // Get current db number | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $db_conn=$this->trapsDB->db_connect_trap(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $sql='SELECT id,value from '.$this->db_prefix.'db_config WHERE name=\'db_version\' '; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->logging->log('SQL query : '.$sql,DEBUG ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         if (($ret_code=$db_conn->query($sql)) === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             $this->logging->log('Cannot get db version. Query : ' . $sql,2,''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $version=$ret_code->fetchAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $cur_version=$version[0]['value']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $db_version_id=$version[0]['id']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         if ($this->trapsDB->trapDBType == 'pgsql') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $prefix .= 'update_pgsql/schema_'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $prefix .= 'update_sql/schema_'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         //echo "version all :\n";print_r($version);echo " \n $cur_ver \n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         if ($getmsg === true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $message=''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $this->logging->log('getting message for upgrade',DEBUG ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             while($cur_version<$target_version) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 $cur_version++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 $input_stream=fopen($updateFile, 'r'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 if ($input_stream=== false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     $this->logging->log("Error reading update file ". $updateFile,2,''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                     return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 do { $line=fgets($input_stream); } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 while ($line !== false && !preg_match('/#MESSAGE/',$line)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 if ($line === false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                     $this->logging->log("No message in file ". $updateFile,2,''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 $message .= ($cur_version-1) . '->' . $cur_version. ' : ' . preg_replace('/#MESSAGE : /','',$line)."\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             return $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         while($cur_version<$target_version) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         { // tODO : execute pre & post scripts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             $cur_version++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             $this->logging->log('Updating to version : ' .$cur_version ,INFO ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             $updateFile=$prefix.'v'.($cur_version-1).'_v'.$cur_version.'.sql'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             $input_stream=fopen($updateFile, 'r'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             if ($input_stream=== false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 $this->logging->log("Error reading update file ". $updateFile,2,''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             $newline=''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             $db_conn=$this->trapsDB->db_connect_trap(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             while (($line=fgets($input_stream)) !== false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |                 if (preg_match('/^#/', $line)) continue; // ignore comment lines | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |                 $newline.=chop(preg_replace('/#PREFIX#/',$table_prefix,$line)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                 if (preg_match('/; *$/', $newline)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |                 { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                     $sql_req=$db_conn->prepare($newline); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                     if ($sql_req->execute() === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                         $this->logging->log('Error create schema : '.$newline,1,''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                     $cur_table_array=array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                     if (preg_match('/^ *([^ ]+) TABLE ([^ ]+)/',$newline,$cur_table_array)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |                         $cur_table=$cur_table_array[1] . ' SQL table '.$cur_table_array[2]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                     else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                         $cur_table='secret SQL stuff :-)'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                         //$cur_table=$newline; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                     $this->logging->log('Doing : ' . $cur_table,INFO ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                     $newline=''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             fclose($input_stream); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             //$sql= $newline; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             //if ($db_conn->query($sql) === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             //    $this->logging->log('Error updating schema : '.$sql,1,''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             //} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             $sql='UPDATE '.$this->db_prefix.'db_config SET value='.$cur_version.' WHERE ( id = '.$db_version_id.' )'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             $this->logging->log('SQL query : '.$sql,DEBUG ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             if ($db_conn->query($sql) === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |                 $this->logging->log('Cannot update db version. Query : ' . $sql,2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |                 return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |              | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 188 |  |  |             $this->logging->log('Schema updated to version : '.$cur_version ,INFO); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 189 |  |  |         } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 190 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 193 |  |  |      | 
            
                                                        
            
                                    
            
            
                | 194 |  |  | } |