<?php

/* INSTALLER - FUNCTIONS
--------------------------------------------------------*/

function mswCheckTable($table, $db) {
  $q = $db->db_query("SHOW TABLES WHERE `Tables_in_" . DB_NAME . "` = '" . DB_PREFIX . $table . "'");
  $c = $db->db_object($db->db_query("SELECT FOUND_ROWS() AS `rows`"));
  $f = (isset($c->rows) ? $c->rows : '0');
  return ($f > 0 ? 'yes' : 'no');
}

function mswCheckColumn($table, $col, $db) {
  $q = $db->db_query("SELECT count(*) AS `c` FROM `information_schema`.`COLUMNS` WHERE `TABLE_SCHEMA` = '" . DB_NAME . "'
       AND `TABLE_NAME`  = '" . DB_PREFIX . $table . "'
       AND `COLUMN_NAME` = '{$col}'
       ");
  $R = $db->db_object($q);
  $f = (isset($R->c) ? $R->c : '0');
  return ($f > 0 ? 'yes' : 'no');
}

function mswCheckColumnType($table, $field, $string, $db) {
  $q = $db->db_query("SHOW FIELDS FROM `" . DB_PREFIX . $table . "` WHERE `Field` = '{$field}'");
  $R = $db->db_object($q);
  $f = (isset($R->Type) ? strtolower($R->Type) : '');
  return (strpos($f, strtolower($string)) !== false ? 'yes' : 'no');
}

function mswCheckIndex($table, $index, $db) {
  $q = $db->db_query("SHOW INDEX FROM " . DB_PREFIX . $table . " WHERE `Key_name` = '{$index}'");
  $c = $db->db_object($db->db_query("SELECT FOUND_ROWS() AS `rows`"));
  $f = (isset($c->rows) ? $c->rows : '0');
  return ($f > 0 ? 'yes' : 'no');
}

function mswInsLog($table, $error = '', $code = '', $line = '', $file = '', $db = [], $type = 'Create') {
  $header = '';
  if ($error == 'instruction') {
    $type = $table;
  }
  if (MSW_INSTALL_LOG) {
    if (!file_exists(IPATH . 'logs/' . MSW_INSTALL_LOG_FILE)) {
      $header = 'Script: ' . SCRIPT_NAME . mswNL();
      $header .= 'Script Version: ' . SCRIPT_VERSION . mswNL();
      $header .= 'PHP Version: ' . phpVersion() . mswNL();
      if (is_object($db) && method_exists($db, 'db_version')) {
        $header .= 'DB Version: ' . $db->db_version() . mswNL();
      }
      if (isset($_SERVER['SERVER_SOFTWARE'])) {
        $header .= 'Server Software: ' . $_SERVER['SERVER_SOFTWARE'] . mswNL();
      }
      if (isset($_SERVER["HTTP_USER_AGENT"])) {
        if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'win')) {
          $platform = 'Windows';
        } else if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'mac')) {
          $platform = 'Mac';
        } else {
          $platform = 'Other';
        }
        $header .= 'Platform: ' . $platform . mswNL();
      }
      $header .= str_repeat('=', 75) . mswNL();
    }
    if ($error != 'instruction') {
      if ($table) {
        $string = 'Table: ' . $table . mswNL();
      } else {
        $string = '';
      }
    } else {
      $string = '';
    }
    $string .= 'Operation/Detail: ' . $type . mswNL();
    if ($code && $error != 'instruction') {
      $string .= 'Error Code: ' . $code . mswNL();
    }
    if ($error && $error != 'instruction') {
      $string .= 'Error Msg: ' . $error . mswNL();
    }
    if ($line && $error != 'instruction') {
      $string .= 'On Line: ' . $line . mswNL();
    }
    if ($file && $error != 'instruction') {
      $string .= 'In File: ' . $file . mswNL();
    }
    $string .= str_repeat('-', 75) . mswNL();
    mswPUT(IPATH . 'logs/' . MSW_INSTALL_LOG_FILE, $header . $string);
  }
}

function mswUpLog($table, $error = '', $code = '', $line = '', $file = '', $db = [], $type = 'Create') {
  $header = '';
  if ($error == 'instruction') {
    $type = $table;
  }
  if (MSW_UPGRADE_LOG) {
    if (!file_exists(IPATH . 'logs/' . MSW_UPGRADE_LOG_FILE)) {
      $header = 'Script: ' . SCRIPT_NAME . mswNL();
      $header .= 'Script Version: ' . SCRIPT_VERSION . mswNL();
      $header .= 'PHP Version: ' . phpVersion() . mswNL();
      if (is_object($db) && method_exists($db, 'db_version')) {
        $header .= 'DB Version: ' . $db->db_version() . mswNL();
      }
      if (isset($_SERVER['SERVER_SOFTWARE'])) {
        $header .= 'Server Software: ' . $_SERVER['SERVER_SOFTWARE'] . mswNL();
      }
      if (isset($_SERVER["HTTP_USER_AGENT"])) {
        if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'win')) {
          $platform = 'Windows';
        } else if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'mac')) {
          $platform = 'Mac';
        } else {
          $platform = 'Other';
        }
        $header .= 'Platform: ' . $platform . mswNL();
      }
      $header .= str_repeat('=', 75) . mswNL();
    }
    if ($error != 'instruction') {
      if ($table) {
        $string = 'Table: ' . $table . mswNL();
      } else {
        $string = '';
      }
    } else {
      $string = '';
    }
    $string .= 'Operation/Detail: ' . $type . mswNL();
    if ($code && $error != 'instruction') {
      $string .= 'Error Code: ' . $code . mswNL();
    }
    if ($error && $error != 'instruction') {
      $string .= 'Error Msg: ' . $error . mswNL();
    }
    if ($line && $error != 'instruction') {
      $string .= 'On Line: ' . $line . mswNL();
    }
    if ($file && $error != 'instruction') {
      $string .= 'In File: ' . $file . mswNL();
    }
    $string .= str_repeat('-', 75) . mswNL();
    mswPUT(IPATH . 'logs/' . MSW_UPGRADE_LOG_FILE, $header . $string);
  }
}

function mswSessActv() {
  $ini_get = ini_get_all();
  if (isset($ini_get['session.save_handler']['global_value']) && 
    $ini_get['session.save_handler']['global_value'] == 'files') {
    if (isset($ini_get['session.save_path']['global_value']) &&
      (!is_dir($ini_get['session.save_path']['global_value']) || !is_writeable($ini_get['session.save_path']['global_value']))) {
      return [
        'fail',
        $ini_get['session.save_path']['global_value']
      ];
    }
  }
  return ['ok', ''];
}

?>