total_cpes = count(preg_grep("/count = 0; $conn = new mysqli(DB_SERVER, "web", db::decrypt_pwd(), 'sagacity'); $this->db = new db_helper($conn); $this->db->update("settings", ['meta_value' => new DateTime($date)], [ [ 'field' => 'meta_key', 'op' => '=', 'value' => 'cpe-load-date' ] ]); $this->db->execute(); $this->db->select("software", ['cpe']); $cpes = $this->db->execute(); if (!is_null($cpes) && is_array($cpes) && count($cpes)) { foreach ($cpes as $cpe) { if (isset($cpe['cpe'])) { $this->existing_cpes["{$cpe['cpe']}"] = 1; } } } if (!isset($this->existing_cpes["cpe:/o:generic:generic:-"])) { $this->new_cpes[] = [ "cpe:/o:generic:generic:-", "cpe:2.3:o:generic:generic:*:*:*:*:*:*:*", "Generic Generic OS", "Generic" ]; } if (!isset($this->existing_cpes["cpe:/a:generic:generic:-"])) { $this->new_cpes[] = [ "cpe:/a:generic:generic:-", "cpe:2.3:a:generic:generic:*:*:*:*:*:*:*", "Generic Generic", "Generic" ]; } } /** * Start function for <cpe-list>/<cpe-item> element * * @param array $attrs */ public function cpe_list_cpe_item($attrs) { if (isset($attrs['deprecated']) && $attrs['deprecated'] == 'true') { $this->skip = true; if (isset($attrs['name'])) { $this->cpe = $attrs['name']; } return; } $match = []; if (isset($attrs['name'])) { $this->cpe = $attrs['name']; $this->cpe_arr = explode(':', $attrs['name']); } switch ($this->cpe_arr[2]) { case 'microsoft': $this->short_string = ''; break; case 'redhat': $this->short_string = 'RH'; break; case 'opensuse_project': $this->short_string = 'openSuSE'; break; default: $this->short_string = ucfirst($this->cpe_arr[2]); } switch ($this->cpe_arr[3]) { case 'windows': case 'windows_nt': $this->short_string .= 'Win'; break; case (preg_match("/windows_([\d\.]+)(_server)?$/", $this->cpe_arr[3], $match) ? true : false): if (isset($match[2]) && $match[2]) { $this->short_string .= "Win Server {$match[1]}"; } else { $this->short_string .= "Win {$match[1]}"; } break; case (preg_match("/windows_server_([\d]+)$/", $this->cpe_arr[3], $match) ? true : false): $this->short_string .= "Win Server {$match[1]}"; break; case (preg_match("/windows_(vista|xp)$/", $this->cpe_arr[3], $match) ? true : false): $this->short_string .= "Win {$match[1]}"; break; case 'pocket_ie': case 'pocket_internet_explorer': case 'internet_explorer': case 'ie': $this->short_string .= "IE"; break; case 'enterprise_linux_server': $this->short_string .= " EL"; break; case 'enterprise_linux_workstation': $this->short_string .= " EL"; break; default: $this->short_string .= " " . ucfirst(str_replace(array('-', '_'), ' ', $this->cpe_arr[3])); } if (isset($this->cpe_arr[4]) && ($this->cpe_arr[4] != '-' || $this->cpe_arr[4] != '*')) { switch ($this->cpe_arr[4]) { case (preg_match("/([R\d\.z]+)/", $this->cpe_arr[4], $match) ? true : false): $this->short_string .= " {$match[1]}"; break; default: $this->short_string .= " " . $this->cpe_arr[4]; } } if (isset($this->cpe_arr[6]) && $this->cpe_arr[6]) { $this->short_string .= " " . str_replace('~', '', $this->cpe_arr[6]); } if (isset($this->cpe_arr[5]) && !empty($this->cpe_arr[5]) && $this->cpe_arr[5] != '-') { //die(print_r($this->cpe_arr, true)); switch ($this->cpe_arr[5]) { case (preg_match("/sp([\d]+)/", $this->cpe_arr[5], $match) ? true : false): $this->short_string .= " SP{$match[1]}"; break; default: $this->short_string .= " " . strtoupper($this->cpe_arr[5]); } } } /** * Start function for <cpe-list>/<cpe-item>/<title> element * * @param array $attrs * Name/value pair of attributes */ public function cpe_list_cpe_item_title($attrs) { if (isset($attrs['xml:lang']) && $attrs['xml:lang'] != 'en-US') { $this->skip = true; } } /** * Character data function for <cpe-list>/<cpe-item>/<title> element * * @param string $data * The value within the tags */ public function cpe_list_cpe_item_title_data($data) { $this->sw_string = trim($data); } /** * Start function for <cpe-list>/<cpe-item>/<cpe-23:cpe23-item> element * * @param array $attrs * Name/value pairs of attributes */ public function cpe_list_cpe_item_cpe_23_cpe23_item($attrs) { if (isset($attrs['name'])) { $this->cpe23 = $attrs['name']; } } /** * End function for <cpe-list>/<cpe-item> element */ public function cpe_list_cpe_item_end() { // if we are supposed to skip this CPE (because of deprecation or the title is not english) then delete it from the database if ($this->skip) { $this->cpes_to_remove[] = $this->cpe; $this->skip = false; PHP_SAPI == "cli" ? print "-" : null; } // look for current item in the existing list elseif (!isset($this->existing_cpes["{$this->cpe}"])) { $this->new_cpes[] = [ $this->cpe, $this->cpe23, $this->sw_string, $this->short_string ]; PHP_SAPI == "cli" ? print "*" : null; } else { // current cpe is already in the database, so just print "." PHP_SAPI == 'cli' ? print "." : null; } $this->count++; // every 100 CPEs, print the count and execute the SQL. if ($this->count % 100 == 0) { print "\t$this->count completed" . PHP_EOL; $this->db->update("settings", ['meta_value' => number_format(($this->count / $this->total_cpes * 100), 2)], [ [ 'field' => 'meta_key', 'op' => '=', 'value' => 'cpe-progress' ] ]); $this->db->execute(); if (is_array($this->new_cpes) && count($this->new_cpes)) { $this->db->extended_insert('software', ['cpe', 'cpe23', 'sw_string', 'short_sw_string'], $this->new_cpes, true); $this->new += $this->db->execute(); unset($this->new_cpes); $this->{'new_cpes'} = []; } if (is_array($this->cpes_to_remove) && count($this->cpes_to_remove)) { $this->db->delete("software", null, [ [ 'field' => 'cpe', 'op' => IN, 'value' => $this->cpes_to_remove ] ]); $this->deleted += $this->db->execute(); unset($this->cpes_to_remove); $this->{'cpes_to_remove'} = []; } } // reset cpe, cpe23, and sw_string for next cpe item $this->cpe = ''; $this->cpe23 = ''; $this->sw_string = ''; $this->short_string = ''; } /** * End function for <cpe-list> element */ public function cpe_list_end() { // execute what is left in the SQL just incase there are some leftover if (is_array($this->new_cpes) && count($this->new_cpes)) { $this->db->extended_insert('software', ['cpe', 'cpe23', 'sw_string', 'short_sw_string'], $this->new_cpes, true); $this->db->execute(); } if (is_array($this->cpes_to_remove) && count($this->cpes_to_remove)) { $this->db->delete("software", null, [ [ 'field' => 'cpe', 'op' => IN, 'value' => $this->cpes_to_remove ] ]); $this->deleted += $this->db->execute(); } $this->db->update("settings", ['meta_value' => 100], [ [ 'field' => 'meta_key', 'op' => IN, 'value' => ['cpe-dl-progress', 'cpe-progress'] ] ]); $this->db->execute(); } } $xml = new cpe_parser($cmd['f'], $cmd['d']); $xml->debug = false; if (isset($cmd['debug'])) { $xml->debug = true; } elseif (LOG_LEVEL == E_DEBUG) { $xml->debug = true; } //Enter xml code here $xml->parse(); $unchanged = $xml->count - $xml->new - $xml->deleted; print <<new Deleted CPEs: $xml->deleted EOO; function usage() { print <<