<commit_before><?php
/**
#  Copyright 2003-2015 Opmantek Limited (www.opmantek.com)
#
#  ALL CODE MODIFICATIONS MUST BE SENT TO <EMAIL>
#
#  This file is part of Open-AudIT.
#
#  Open-AudIT is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published
#  by the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Open-AudIT is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with Open-AudIT (most likely in a file named LICENSE).
#  If not, see <http://www.gnu.org/licenses/>
#
#  For further information on Open-AudIT or for a license other than AGPL please see
#  www.opmantek.com or email <EMAIL>
#
# *****************************************************************************
*
**/

$this->log_db('Upgrade database to 2.3.2 commenced');

$this->load->library('encrypt');

# attributes
$sql = "DELETE from attributes WHERE resource = 'devices' AND `type` = 'type' AND `name` = 'Unclassified'";
$this->db->query($sql);
$this->log_db($this->db->last_query());

$sql = "INSERT INTO attributes VALUES (NULL, 1, 'devices', 'type', 'Unclassified', 'unclassified', 'system', '2000-01-01 00:00:00')";
$this->db->query($sql);
$this->log_db($this->db->last_query());

# configuration
$sql = "DELETE FROM configuration WHERE name = 'discovery_default_scan_option'";
$this->db->query($sql);
$this->log_db($this->db->last_query());

$sql = "INSERT INTO `configuration` VALUES (NULL,'discovery_default_scan_option','1','number','y','system','2000-01-01 00:00:00','The default discovery options for Nmap.')";
$this->db->query($sql);
$this->log_db($this->db->last_query());

$sql = "UPDATE configuration SET description = 'The URL used by external devices to talk to Open-AudIT.' WHERE name = 'default_network_address'";
$this->db->query($sql);
$this->log_db($this->db->last_query());

$this->load->helper('security');
if (php_uname('s') != 'Windows NT') {
  # re-encode or credentials
  $sql = "SELECT * FROM `credentials` WHERE (`edited_by` != 'system' or `id` = 1)";
  $query = $this->db->query($sql);
  $this->log_db($this->db->last_query());
  $result = $query->result();
  if (!empty($result)) {
      foreach ($result as $item) {
          $credentials = json_decode(simpleDecrypt($item->credentials));
          if (!empty($credentials)) {
              $item->credentials = simpleEncrypt(json_encode($credentials));
              $sql = "UPDATE `credentials` SET `credentials` = ?, `edited_by` = 'system', `edited_date` = NOW() WHERE `id` = ?";
              $data = array($item->credentials, intval($item->id));
              $this->db->query($sql, $data);
              $this->log_db($this->db->last_query());
          } else {
              $this->log_db("Could not unencrypt credentials " . $item->name);
          }
      }
  }

  # device specific credentials
  $sql = "SELECT * FROM `credential` WHERE `edited_by` != 'system'";
  $query = $this->db->query($sql);
  $this->log_db($this->db->last_query());
  $result = $query->result();
  if (!empty($result)) {
      foreach ($result as $item) {
          $credentials = json_decode(simpleDecrypt($item->credentials));
          if (!empty($credentials)) {
              $item->credentials = simpleEncrypt(json_encode($credentials));
              $sql = "UPDATE `credential` SET `credentials` = ?, `edited_by` = 'system', `edited_date` = NOW() WHERE `id` = ?";
              $data = array($item->credentials, intval($item->id));
              $this->db->query($sql, $data);
              $this->log_db($this->db->last_query());
          } else {
              $this->log_db("Could not unencrypt device credentials " . $item->name);
          }
      }
  }

  # re-encode the ldap_servers.dn_password
  $sql = "SELECT * FROM `ldap_servers` WHERE `edited_by` != 'system'";
  $query = $this->db->query($sql);
  $this->log_db($this->db->last_query());
  $result = $query->result();
  if (!empty($result)) {
      foreach ($result as $item) {
          $dn_password = <PASSWORD>Decrypt($item->dn_password);
          if (!empty($dn_password)) {
              $item->dn_password = <PASSWORD>($<PASSWORD>);
              $sql = "UPDATE `ldap_servers` SET `dn_password` = ?, `edited_by` = 'system', `edited_date` = NOW() WHERE `id` = ?";
              $data = array($item->dn_password, intval($item->id));
              $this->db->query($sql, $data);
              $this->log_db($this->db->last_query());
          } else {
              $this->log_db("Could not unencrypt ldap credentials " . $item->name);
          }
      }
  }

  $sql = "SELECT * FROM `clouds` WHERE `edited_by` != 'system'";
  $query = $this->db->query($sql);
  $this->log_db($this->db->last_query());
  $result = $query->result();
  if (!empty($result)) {
      foreach ($result as $item) {
          $credentials = json_decode(simpleDecrypt($item->credentials));
          if (!empty($credentials)) {
              $item->credentials = simpleEncrypt(json_encode($credentials));
              $sql = "UPDATE `clouds` SET `credentials` = ?, `edited_by` = 'system', `edited_date` = NOW() WHERE `id` = ?";
              $data = array($item->credentials, intval($item->id));
              $this->db->query($sql, $data);
              $this->log_db($this->db->last_query());
          } else {
              $this->log_db("Could not unencrypt cloud credentials " . $item->name);
          }
      }
  }
} else {
    # We're upgrading a Windows install from XamppLite (PHP 5.3.1) to Xampp (PHP 7.3.1)
    # The installer should have created a file c:\xampplite\open-audit\migrate.json containing the unencrypted credentials
    $string = file_get_contents('c:\\xampplite\\open-audit\\migrate.json');
    if (empty($string)) {
        $this->log_db("We have no migrate.json file to import credentials from. You will need to manually recreate these items.");
        $this->data['alert'] = 'ALERT - We have upgraded your install to 3.0.0, however we could not open the file created by the installer that contains the migrated credentials. You will need to manually delete and recreate the items in the credentials, credential, clouds and ldap_servers database tables.';
    } else {
        $json = @json_decode($string);<commit_msg>Add extra warnings about not being able to decode credentials for 2.3.2 upgrade.<commit_after><?php
/**
#  Copyright 2003-2015 Opmantek Limited (www.opmantek.com)
#
#  ALL CODE MODIFICATIONS MUST BE SENT TO <EMAIL>
#
#  This file is part of Open-AudIT.
#
#  Open-AudIT is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero General Public License as published
#  by the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  Open-AudIT is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Affero General Public License for more details.
#
#  You should have received a copy of the GNU Affero General Public License
#  along with Open-AudIT (most likely in a file named LICENSE).
#  If not, see <http://www.gnu.org/licenses/>
#
#  For further information on Open-AudIT or for a license other than AGPL please see
#  www.opmantek.com or email <EMAIL>
#
# *****************************************************************************
*
**/

$this->log_db('Upgrade database to 2.3.2 commenced');

$this->load->library('encrypt');

# attributes
$sql = "DELETE from attributes WHERE resource = 'devices' AND `type` = 'type' AND `name` = 'Unclassified'";
$this->db->query($sql);
$this->log_db($this->db->last_query());

$sql = "INSERT INTO attributes VALUES (NULL, 1, 'devices', 'type', 'Unclassified', 'unclassified', 'system', '2000-01-01 00:00:00')";
$this->db->query($sql);
$this->log_db($this->db->last_query());

# configuration
$sql = "DELETE FROM configuration WHERE name = 'discovery_default_scan_option'";
$this->db->query($sql);
$this->log_db($this->db->last_query());

$sql = "INSERT INTO `configuration` VALUES (NULL,'discovery_default_scan_option','1','number','y','system','2000-01-01 00:00:00','The default discovery options for Nmap.')";
$this->db->query($sql);
$this->log_db($this->db->last_query());

$sql = "UPDATE configuration SET description = 'The URL used by external devices to talk to Open-AudIT.' WHERE name = 'default_network_address'";
$this->db->query($sql);
$this->log_db($this->db->last_query());

$this->load->helper('security');
if (php_uname('s') != 'Windows NT') {
  # re-encode or credentials
  $sql = "SELECT * FROM `credentials` WHERE (`edited_by` != 'system' or `id` = 1)";
  $query = $this->db->query($sql);
  $this->log_db($this->db->last_query());
  $result = $query->result();
  if (!empty($result)) {
      foreach ($result as $item) {
          $credentials = json_decode(simpleDecrypt($item->credentials));
          if (!empty($credentials)) {
              $item->credentials = simpleEncrypt(json_encode($credentials));
              $sql = "UPDATE `credentials` SET `credentials` = ?, `edited_by` = 'system', `edited_date` = NOW() WHERE `id` = ?";
              $data = array($item->credentials, intval($item->id));
              $this->db->query($sql, $data);
              $this->log_db($this->db->last_query());
          } else {
              $this->log_db("WARNING - Could not unencrypt credentials " . $item->name);
              $this->data['alert'] = "WARNING - Could not unencrypt credentials, you will need to recreate all credentials, ldap_servers, credential and clouds items.";
          }
      }
  }

  # device specific credentials
  $sql = "SELECT * FROM `credential` WHERE `edited_by` != 'system'";
  $query = $this->db->query($sql);
  $this->log_db($this->db->last_query());
  $result = $query->result();
  if (!empty($result)) {
      foreach ($result as $item) {
          $credentials = json_decode(simpleDecrypt($item->credentials));
          if (!empty($credentials)) {
              $item->credentials = simpleEncrypt(json_encode($credentials));
              $sql = "UPDATE `credential` SET `credentials` = ?, `edited_by` = 'system', `edited_date` = NOW() WHERE `id` = ?";
              $data = array($item->credentials, intval($item->id));
              $this->db->query($sql, $data);
              $this->log_db($this->db->last_query());
          } else {
              $this->log_db("WARNING - Could not unencrypt device credentials " . $item->name);
              $this->data['alert'] = "WARNING - Could not unencrypt credentials, you will need to recreate all credentials, ldap_servers, credential and clouds items.";
          }
      }
  }

  # re-encode the ldap_servers.dn_password
  $sql = "SELECT * FROM `ldap_servers` WHERE `edited_by` != 'system'";
  $query = $this->db->query($sql);
  $this->log_db($this->db->last_query());
  $result = $query->result();
  if (!empty($result)) {
      foreach ($result as $item) {
          $dn_password = <PASSWORD>($item->dn_password);
          if (!empty($dn_password)) {
              $item->dn_password = <PASSWORD>($<PASSWORD>);
              $sql = "UPDATE `ldap_servers` SET `dn_password` = ?, `edited_by` = 'system', `edited_date` = NOW() WHERE `id` = ?";
              $data = array($item->dn_password, intval($item->id));
              $this->db->query($sql, $data);
              $this->log_db($this->db->last_query());
          } else {
              $this->log_db("WARNING - Could not unencrypt ldap credentials " . $item->name);
              $this->data['alert'] = "WARNING - Could not unencrypt credentials, you will need to recreate all credentials, ldap_servers, credential and clouds items.";
          }
      }
  }

  $sql = "SELECT * FROM `clouds` WHERE `edited_by` != 'system'";
  $query = $this->db->query($sql);
  $this->log_db($this->db->last_query());
  $result = $query->result();
  if (!empty($result)) {
      foreach ($result as $item) {
          $credentials = json_decode(simpleDecrypt($item->credentials));
          if (!empty($credentials)) {
              $item->credentials = simpleEncrypt(json_encode($credentials));
              $sql = "UPDATE `clouds` SET `credentials` = ?, `edited_by` = 'system', `edited_date` = NOW() WHERE `id` = ?";
              $data = array($item->credentials, intval($item->id));
              $this->db->query($sql, $data);
              $this->log_db($this->db->last_query());
          } else {
              $this->log_db("WARNING - Could not unencrypt cloud credentials " . $item->name);
              $this->data['alert'] = "WARNING - Could not cloud credentials, you will need to recreate all credentials, ldap_servers, credential and clouds items.";
          }
      }
  }
} else {
    # We're upgrading a Windows install from XamppLite (PHP 5.3.1) to Xampp (PHP 7.3.1)
    # The installer should have created a file c:\xampplite\open-audit\migrate.json containing the unencrypted credentials
    $string = file_get_contents('c:\\xampplite\\open-audit\\migrate.json');
    if (empty($string)) {
        $this->log_db("We have no migrate.json file to import credentials from. You will need to manually recreate these items.");
        $this->data['alert'] = 'ALERT - We have upgraded your install to 3.0.0, however we could not open the file created by the installer that contains the migrated credentials. You will need to manually delete and recreate the items in the credentials, credential, clouds and ldap_servers database tables.';
    } else {
        $json = @json_decode($string);
{'fraction_non_alphanumeric': 0.17419215233698787, 'fraction_numerical': 0.0077178303519907674, 'mean_word_length': 3.3112562189054726, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 219, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '13797', 'n_tokens_mistral': 4346, 'n_tokens_neox': 4202, 'n_words': 1143}