<?php

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

function mswPromoUrl($u, $c) {
  if (substr($u, -1) == '/' && (strpos($u, '=') !== true && strpos($u, '=') == 0)) {
    return $u . '?' . AFF_GET_PARAM . '=' . $c;
  } else if(strpos($u, '=') !== false || strpos($u, '=') > 0) {
    return $u . '&amp;' . AFF_GET_PARAM . '=' . $c;
  } else {
    return $u . '/?' . AFF_GET_PARAM . '=' . $c;
  }
}

function mswFCNV($size = 0, $precision = 2) {
  if ($size > 0) {
    $base     = log($size) / log(1024);
    $suffixes = array(
      'Bytes',
      'KB',
      'MB',
      'GB',
      'TB'
    );
    return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
  } else {
    return '0Bytes';
  }
}

function mswStatus($status, $arr) {
  return (isset($arr[$status]) ? $arr[$status] : 'N/A');
}

function mswMlr($e = '', $adde = '') {
  $ot = [];
  if ($adde) {
    $ot = array_map('trim', explode(',', $adde));
    if ($e == '') {
      $e = $ot[0];
      unset($ot[0]);
    }
  }
  return array(
    $e,
    (!empty($ot) ? implode(',', $ot) : '')
  );
}

function mswKeys($k) {
  return preg_replace('/[^0-9a-zA-Z_\s]/', '', $k);
}

function mswSSL() {
  return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'yes' : 'no');
}

function mswTO($m = '100', $l = 0) {
  $ini_get = ini_get_all();
  if (isset($ini_get['memory_limit'])) {
    ini_set('memory_limit', $m . 'M');
  }
  set_time_limit($l);
}

function mswEmVal($em) {
  if (!is_string($em) || $em == '') {
    return 'no';
  } else {
    if (function_exists('filter_var') && !filter_var($em, FILTER_VALIDATE_EMAIL)) {
      return 'no';
    } else {
      // HTML5 spec..
      if (!preg_match('/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}' .
        '[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/sD', $em)) {
        return 'no';
      }
    }
  }
  return 'yes';
}

function mswLang($s) {
  $base = PATH . 'content/language/';
  return $base . $s->language . '/';
}

function mswPerc($d = []) {
  return (($d['perc'] * $d['total']) / $d['div']);
}

function mswTmp($file, $silent = 'no') {
  // Check for input stream..
  if (substr($file, 0, 4) == 'php:') {
    return file_get_contents($file);
  // Check for open via file wrapper
  } elseif (substr($file, 0, 5) == 'http:' || substr($file, 0, 6) == 'https:')  {
    return file_get_contents($file);
  // Standard
  } else {
    if (file_exists($file)) {
      return trim(file_get_contents($file));
    }
    return ($silent == 'no' ? '"' . mswSH($file) . '" is missing' : '');
  }
}

function mswEnc($data) {
  if ($data == '') {
    $data = '';
  }
  return (function_exists('sha1') ? sha1($data) : md5($data));
}

function mswTZ($zne, $tzs) {
  date_default_timezone_set(($zne && in_array($zne, array_keys($tzs)) ? $zne : 'Europe/London'));
}

function mswIP($arr = false) {
  $ips = [];
  $types = array(
    'HTTP_CLIENT_IP',
    'HTTP_X_FORWARDED_FOR',
    'HTTP_X_FORWARDED',
    'HTTP_X_CLUSTER_CLIENT_IP',
    'HTTP_FORWARDED_FOR',
    'HTTP_FORWARDED',
    'REMOTE_ADDR'
  );
  foreach ($types AS $key) {
    if (array_key_exists($key, $_SERVER) === true) {
      foreach (array_map('trim', explode(',', $_SERVER[$key])) AS $ipA) {
        if (!in_array($ipA, $ips) && filter_var($ipA, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false) {
          $ips[] = $ipA;
        } else {
          // Double check for localhost..
          if (!in_array($ipA, $ips) && in_array($ipA, array('::1','127.0.0.1'))) {
            $ips[] = $ipA;
          }
        }
      }
    }
  }
  if ($arr) {
    return $ips;
  }
  return (!empty($ips) ? implode(',', $ips) : '');
}

function mswNFM($num, $dec = 0) {
  return number_format((float) $num, $dec);
}

function mswNFMDec($num, $dec = 0) {
  return number_format((float) $num, $dec, '.', '');
}

// New line to break..
function mswL2BR($text) {
  // Second param added in 5.3.0, else its not available..
  if (version_compare(phpversion(), '5.3.0', '<')) {
    return str_replace(mswNL(), '<br>', $text);
  }
  return nl2br($text, false);
}

function mswNL($br = 1) {
  if ($br > 1) {
    return str_repeat(PHP_EOL, $br);
  }
  return PHP_EOL;
}

function mswFPCE($price, $comma = false) {
  $sep = '';
  if (PRICE_THOUSANDS_SEPARATORS && $comma) {
    $sep = PRICE_THOUSANDS_SEPARATORS;
  }
  $price = number_format((float) $price, 2, '.', '');
  $price = preg_replace("/[^0-9\.]/", "", str_replace(',', '.', $price));
  if (substr($price, -3, 1) == '.') {
    $pennies = '.' . substr($price, -2);
    $price   = substr($price, 0, strlen($price) - 3);
  } elseif (substr($price, -2, 1) == '.') {
    $pennies = '.' . substr($price, -1);
    $price   = substr($price, 0, strlen($price) - 2);
  } else {
    $pennies = '.00';
  }
  $price = preg_replace("/[^0-9]/", "", $price);
  // Prevent formatting errors during imports..
  if ($price == '') {
    $price = '0';
  }
  if (rtrim($pennies, '.') == '') {
    $pennies = '.00';
  }
  return ($price . $pennies > 0 ? number_format((float) $price . $pennies, PRICE_FORMAT_DECIMAL_PLACES, '.', ($sep ? $sep : '')) : '0.00');
}

function mswCRFMT($price = '', $s = '') {
  return str_replace(array('{PRICE}','{COST}'), $price, (isset($s->priceformat) ? $s->priceformat : ''));
}

function mswQT($data) {
  if ($data == '') {
    $data = '';
  }
  $data = str_replace(array(
    '"'
  ), array(
    '&quot;'
  ), $data);
  return $data;
}

function mswSH($data) {
  if ($data == '') {
    $data = '';
  }
  $data = htmlspecialchars($data);
  $data = str_replace('&amp;#', '&#', $data);
  $data = str_replace('&amp;amp;', '&amp;', $data);
  return mswCD($data);
}

function mswCD($data) {
  return $data;
}

function mswSTG($data) {
  if ($data == '') {
    $data = '';
  }
  return ($data ? strip_tags(trim($data)) : '');
}

function mswQS($data) {
  return str_replace(array(
    '"',
    "'"
  ), [], $data);
}

function mswJSClean($js) {
  return str_replace("'", "\'", $js);
}

function mswMDAM($func, $arr) {
  $newArr = [];
  if (!empty($arr)) {
    foreach ($arr AS $key => $value) {
      $newArr[$key] = (is_array($value) ? mswMDAM($func, $value) : $func($value));
    }
  }
  return $newArr;
}

function mswDAP($data) {
  return str_replace("''", "'", $data);
}

function mswFoot($footcode, $loc = 'front') {
  switch($loc) {
    case 'admin':
      if (LICENCE_VER == 'unlocked' && $footcode) {
        $foot = tags::filter(mswCD($footcode));
      } else {
        $foot = 'Powered by: <a href="https://www.' . SCRIPT_URL . '" onclick="window.open(this);return false" title="' . mswQT(SCRIPT_NAME) . '">' . SCRIPT_NAME . '</a><br>
        &copy;' . (date('Y') == SCRIPT_RELEASE_YR ? SCRIPT_RELEASE_YR : SCRIPT_RELEASE_YR .' - ' . date('Y')) . ' <a href="https://www.maianmedia.com" title="Maian Media" onclick="window.open(this);return false">Maian Media</a>. All Rights Reserved.';
      }
      break;
    default:
      if (LICENCE_VER == 'unlocked' && $footcode) {
        $foot = tags::filter(mswCD($footcode));
      } else {
        $foot = 'Powered by: <a href="https://www.' . SCRIPT_URL . '" onclick="window.open(this);return false" title="' . mswQT(SCRIPT_NAME) . '">' . SCRIPT_NAME . '</a><br>
        &copy;' . (date('Y') == SCRIPT_RELEASE_YR ? SCRIPT_RELEASE_YR : SCRIPT_RELEASE_YR .' - ' . date('Y')) . ' <a href="https://www.maianmedia.com" title="Maian Media" onclick="window.open(this);return false">Maian Media</a>. All Rights Reserved.';
      }
      break;
  }
  return $foot;
}

function mswPK() {
  $_SERVER['HTTP_HOST']   = (isset($_SERVER['HTTP_HOST']) && $_SERVER['HTTP_HOST'] ? $_SERVER['HTTP_HOST'] : uniqid(rand(), 1));
  $_SERVER['REMOTE_ADDR'] = (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] ? $_SERVER['REMOTE_ADDR'] : uniqid(rand(), 1));
  if (function_exists('sha1')) {
    $c1      = sha1($_SERVER['HTTP_HOST'] . date('YmdHis') . $_SERVER['REMOTE_ADDR'] . time());
    $c2      = sha1(uniqid(rand(), 1) . time());
    $prodKey = substr($c1 . $c2, 0, 60);
  } elseif (function_exists('md5')) {
    $c1      = md5($_SERVER['HTTP_POST'] . date('YmdHis') . $_SERVER['REMOTE_ADDR'] . time());
    $c2      = md5(uniqid(rand(), 1), time());
    $prodKey = substr($c1 . $c2, 0, 60);
  } else {
    $c1      = str_replace('.', '', uniqid(rand(), 1));
    $c2      = str_replace('.', '', uniqid(rand(), 1));
    $c3      = str_replace('.', '', uniqid(rand(), 1));
    $prodKey = substr($c1 . $c2 . $c3, 0, 60);
  }
  return strtoupper($prodKey);
}

function mswAPM($module) {
  return (function_exists('apache_get_modules') && in_array($module, apache_get_modules()) ? 'yes' : 'no');
}

function mswPUT($file, $data) {
  if ($data == '') {
    $data = '';
  }
  file_put_contents($file, $data, FILE_APPEND);
}

function mswAff($s, $u) {
  if (is_array($u) && isset($u['id'])) {
    $num_prfx = ($u['id'] > 9 ? $u['id'] : '0' . $u['id']);
    return ($s->affcode ? $s->affcode : '') . ($u['code'] ? $u['code'] : '') . ($s->affcode || $u['code'] ? '-' : '') . $num_prfx;
  } else {
    $num_prfx = ($u->id > 9 ? $u->id : '0' . $u->id);
    return ($s->affcode ? $s->affcode : '') . ($u->code ? $u->code : '') . ($s->affcode || $u->code ? '-' : '') . $num_prfx;
  }
}

function mswPassHash($d = []) {
  switch($d['type']) {
    case 'add':
      return password_hash($d['pass'], PASSWORD_BCRYPT);
      break;
    case 'calc':
      return password_verify($d['val'], $d['hash']);
      break;
  }
}

function mswFLCTR() {
  if (!file_exists(GPATH . 'control/system/core/mm.php')) {
    die(base64_decode('W0ZBVEFMIEVSUk9SXSBMaWNlbmNlIGVycm9yLiBQbGVhc2UgdXBsb2FkIGFsbCBmaWxlcyBhZ2Fpbg=='));
  }
}

$_GET  = mswMDAM('mswQS', $_GET);
$_GET  = mswMDAM('htmlspecialchars', $_GET);
$_POST = mswMDAM('trim', $_POST);

?>
