<?php

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

function mswCSV($d) {
  $d = str_replace('"', '', (string) $d);
  return '"' . mswCD($d) . '"';
}

function mswGST($flag) {
  return ($flag == 'yes' ? '<i class="fa fa-check-circle fa-fw msw-green"></i>' : '<i class="fa fa-times-circle fa-fw msw-red"></i>');
}

function mswAL($l, $pg) {
  if (in_array($pg, array('dashboard','login','bb','purchase','aff-commissions','aff-referrals','referral'))) {
    return true;
  }
  foreach (array_keys($l) AS $p) {
    if (isset($l[$p]['links']) && in_array($pg, array_keys($l[$p]['links']))) {
      return true;
    }
  }
  return false;
}

function mswHelp($pg) {
  switch($pg) {
    case 'abc':
      $lnk = 'ad-' . $pg . '.html';
      break;
    case 'purchase':
      return 'https://www.' . SCRIPT_URL . '/purchase.html';
      break;
    default:
      if (defined('SALES_UPDATE_RUNNING')) {
        $lnk = 'ad-sales-update.html';
      } elseif (isset($_GET['html'])) {
        $lnk = 'api.html';
      } else {
        $lnk = 'ad-' . $pg . '.html';
      }
      break;
  }
  return '../docs/' . $lnk;
}

function mswUsr($s, $db, $ssn) {
  $user = [];
  // Check cookie..
  if ($ssn->active_c('adm_maff_cookie') == 'yes') {
    $a = $ssn->get_c('adm_maff_cookie');
    if ($a == mswEnc(SECRET_KEY . mswEnc('gl0bal'))) {
      $user[0] = LG_USER;
      $user[1] = 'global';
      $user[2] = 0;
    }
  } else {
    if (defined('LG_USER') && defined('LG_PASS') && $ssn->active('adm_maff_user') == 'yes' &&
        $ssn->get('adm_maff_user') == LG_USER && $ssn->active('adm_maff_global') == 'yes' &&
        $ssn->get('adm_maff_global') == mswEnc(SECRET_KEY . mswEnc('gl0bal'))) {
      $user[0] = LG_USER;
      $user[1] = 'global';
      $user[2] = 0;
    }
  }
  return $user;
}

function mswPerms($usr, $cmd, $ssn) {
  $login = ($cmd == 'login' ? true : false);
  if (!$login) {
    if (!isset($usr[0], $usr[1]) || $usr[0] == '' || $usr[1] == '') {
      header("Location: index.php?p=login");
      exit;
    }
  }
}

function mswCatList($id, $col, $table, $fld, $db) {
  $cats = [];
  $Q  = $db->db_query("SELECT `" . $fld . "` FROM `" . DB_PREFIX . $table . "`
        WHERE `" . $col . "` = '{$id}'
        ORDER BY `id`");
  while ($C = $db->db_object($Q)) {
    $cats[] = $C->{$fld};
  }
  return $cats;
}

?>
