<?php

/* AJAX OPS
--------------------------------------------------------*/

if (!defined('PARENT') || !isset($_GET['ajax'])) {
  exit;
}

$arr = array('status' => 'err', 'txt' => array($msw_ajax, $msw_ajax2));

// Check for GET token
if (CSRF_GET_VAL == 'yes') {
  $_SERVER['HTTP_X_CSRF_TOKEN'] = preg_replace('/[^A-Za-z0-9]/', '', $_GET['csrf']);
}

if (isset($_GET['ajax'], $_SERVER['HTTP_X_CSRF_TOKEN'], $_GET['id']) && $_SERVER['HTTP_X_CSRF_TOKEN'] == $SSN->get('csrf_token')) {
  switch($_GET['ajax']) {
    // Payments
    case 'payments-add':
    case 'payments-add-edit':
    case 'payments-manage-del':
    case 'payments-export':
    case 'payments-export-dl':
      include(MSW_ADM_LANG . 'payments-add.php');
      include(PATH . 'control/classes/class.payments.php');
      $PAY           = new payments();
      $PAY->dt       = $DT;
      $PAY->settings = $SETTINGS;
      $PAY->sys      = $SYS;
      switch($_GET['ajax-ops']) {
        case 'payments-add':
        case 'payments-add-edit':
          $fm = array(
            'ts' => (isset($_POST['fm']['ts']) ? $DT->jstots($_POST['fm']['ts']) : $DT->ts()),
            'payamount' => (isset($_POST['fm']['payamount']) ? $_POST['fm']['payamount'] : ''),
            'paymethod' => (isset($_POST['fm']['paymethod']) ? $_POST['fm']['paymethod'] : ''),
            'notes' => (isset($_POST['fm']['notes']) ? $_POST['fm']['notes'] : ''),
            'email' => (isset($_POST['fm']['email']) ? 'yes' : 'no'),
            'aff_id' => (isset($_POST['fm']['aff_id']) ? (int) $_POST['fm']['aff_id'] : '0'),
            'id' => (isset($_POST['fm']['id']) ? (int) $_POST['fm']['id'] : '0')
          );
          if ($_GET['ajax-ops'] == 'payments-add') {
            if (in_array($fm['payamount'], array('0','0.00')) || $fm['paymethod'] == '' || $fm['aff_id'] == '0') {
              $arr['txt'][1] = $msw_adm_payments_add_err;
            } else {
              $id = $PAY->add($fm);
              // Send email
              if ($fm['email'] == 'yes') {
                $Q  = $DB->db_query("SELECT * FROM `" . DB_PREFIX . "affiliates` WHERE `id` = '{$fm['aff_id']}'");
                $AFF = $DB->db_object($Q);
                if (isset($AFF->id)) {
                  include(GPATH . 'control/mail-init.php');
                  $msg = mswTmp(MSW_EM_LANG . LANG_FLDR_ADM . '/payment-sent.txt');
                  $sbj = str_replace('{website}', $SETTINGS->website, $msw_emails11);
                  $MAILR->addTag('{NAME}', $AFF->fname . ' ' . $AFF->lname);
                  $MAILR->addTag('{FNAME}', $AFF->fname);
                  $MAILR->addTag('{LNAME}', $AFF->lname);
                  $MAILR->addTag('{SALE}', mswFPCE($fm['payamount'], true));
                  $MAILR->addTag('{METHOD}', $fm['paymethod']);
                  $MAILR->addTag('{CURRENCY}', $SETTINGS->currency);
                  $MAILR->addTag('{CURRENCY_NAME}', ($SETTINGS->currency && isset($currencies[$SETTINGS->currency]) ? $currencies[$SETTINGS->currency] : '-'));
                  $MAILR->sendMail(array(
                    'from_email' => ($SETTINGS->smtp_email ? $SETTINGS->smtp_email : $SETTINGS->email),
                    'from_name' => ($SETTINGS->smtp_from ? $SETTINGS->smtp_from : $SETTINGS->website),
                    'to_email' => $AFF->email,
                    'to_name' => $AFF->fname . ' ' . $AFF->lname,
                    'subject' => $sbj,
                    'replyto' => array(
                      'name' => ($SETTINGS->smtp_rfrom ? $SETTINGS->smtp_rfrom : $SETTINGS->website),
                      'email' => ($SETTINGS->smtp_remail ? $SETTINGS->smtp_remail : $SETTINGS->email)
                    ),
                    'template' => $msg,
                    'language' => $SETTINGS->language
                  ));
                  $MAILR->smtpClose();
                }
              }
              $controlButtons = array(
                '<a href="?p=payments-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav30 . '</span></a>',
                '<a href="?p=payments-manage"><i class="fa fa-money fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav31 . '</span></a>'
              );
              include(PATH . 'content/control-btns.php');
              $arr['status'] = 'ok';
              $arr['txt'][1] = str_replace('{id}', $id, $msw_adm_payments_add10) . $c_b;
            }
          } else {
            if (in_array($fm['payamount'], array('0','0.00')) || $fm['paymethod'] == '' || $fm['aff_id'] == '0') {
              $arr['txt'][1] = $msw_adm_payments_add_err;
            } else {
              $PAY->update($fm);
              $controlButtons = array(
                '<a href="?p=payments-add&amp;id=' . $fm['id'] . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_payments_add9 . '</span></a>',
                '<a href="?p=payments-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav30 . '</span></a>',
                '<a href="?p=payments-manage"><i class="fa fa-money fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav31 . '</span></a>'
              );
              include(PATH . 'content/control-btns.php');
              $arr['status'] = 'ok';
              $arr['txt'][1] = $msw_adm_payments_add11 . $c_b;
            }
          }
          break;
        case 'payments-manage-del':
          include(MSW_ADM_LANG . 'payments-manage.php');
          $fm = array(
            'ids' => (!empty($_POST['fm']['id']) ? $_POST['fm']['id'] : [])
          );
          if (!empty($fm['ids'])) {
            $PAY->delete($fm);
            $controlButtons = array(
              '<a href="?p=payments-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav30 . '</span></a>',
              '<a href="?p=payments-manage"><i class="fa fa-money fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav31 . '</span></a>'
            );
            include(PATH . 'content/control-btns.php');
            $arr['tr-ds'] = $fm['ids'];
            $arr['status'] = 'ok';
            $arr['txt'][1] = str_replace('{count}', mswNFM(count($fm['ids'])), $msw_adm_payments_manage11) . $c_b;
          } else {
            $arr['txt'][1] = $msw_adm_payments_manage_err;
          }
          break;
        case 'payments-export':
        case 'payments-export-dl':
          include(MSW_ADM_LANG . 'payments-manage.php');
          include(MSW_ADM_LANG . 'payments-search.php');
          switch($_GET['ajax-ops']) {
            case 'payments-export':
              $fm = array(
                'q' => (isset($_GET['q']) ? urldecode($_GET['q']) : ''),
                'id' => (isset($_GET['id']) ? (int) $_GET['id'] : ''),
                'sort' => array(
                  'def' => $msw_adm_payments_search_default_sort,
                  'options' => $msw_adm_payments_search_sort
                ),
                'header' => $msw_adm_payments_manage14
              );
              $data = $PAY->export($fm);
              if ($data[0] && file_exists($data[1])) {
                $arr['status'] = 'ok';
                $arr['loc'] = 'index.php?ajax-ops=payments-export-dl&f=' . basename($data[1]) . '&csrf=' . $SSN->get('csrf_token');
              } else {
                $arr['txt'][1] = $msw_adm_payments_manage13;
              }
              break;
            case 'payments-export-dl':
              if (isset($_GET['f']) && file_exists(SYS_TEMP_DIR . $_GET['f'])) {
                include(GPATH . 'control/classes/system/class.download.php');
                $DL = new download();
                $DL->dl(
                  SYS_TEMP_DIR . $_GET['f'],
                  'text/csv',
                  'yes'
                );
              }
              break;
          }
          break;
      }
      break;

    // Commissions
    case 'commissions-add':
    case 'commissions-approve':
    case 'commissions-add-edit':
    case 'commissions-manage-del':
    case 'commissions-approve-del':
    case 'commissions-export':
    case 'commissions-export-dl':
      include(MSW_ADM_LANG . 'commissions-add.php');
      include(PATH . 'control/classes/class.commissions.php');
      $COM           = new commissions();
      $COM->dt       = $DT;
      $COM->settings = $SETTINGS;
      $COM->sys      = $SYS;
      switch($_GET['ajax-ops']) {
        case 'commissions-add':
        case 'commissions-add-edit':
          $fm = array(
            'ts' => (isset($_POST['fm']['ts']) ? $DT->jstots($_POST['fm']['ts']) : $DT->ts()),
            'saletotal' => (isset($_POST['fm']['saletotal']) ? $_POST['fm']['saletotal'] : ''),
            'commtotal' => (isset($_POST['fm']['commtotal']) ? $_POST['fm']['commtotal'] : ''),
            'product' => (isset($_POST['fm']['product']) ? (int) $_POST['fm']['product'] : '0'),
            'notes' => (isset($_POST['fm']['notes']) ? $_POST['fm']['notes'] : ''),
            'aff_id' => (isset($_POST['fm']['aff_id']) ? (int) $_POST['fm']['aff_id'] : '0'),
            'id' => (isset($_POST['fm']['id']) ? (int) $_POST['fm']['id'] : '0')
          );
          if ($_GET['ajax-ops'] == 'commissions-add') {
            if (in_array($fm['saletotal'], array('0','0.00')) || in_array($fm['commtotal'], array('0','0.00')) || $fm['product'] == '0' || $fm['aff_id'] == '0') {
              $arr['txt'][1] = $msw_adm_commissions_add_err;
            } else {
              $id = $COM->add($fm);
              $controlButtons = array(
                '<a href="?p=commissions-add&amp;id=' . $id . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_commissions_add9 . '</span></a>',
                '<a href="?p=commissions-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_commissions_add8 . '</span></a>',
                '<a href="?p=commissions-manage"><i class="fa fa-line-chart fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav25 . '</span></a>'
              );
              include(PATH . 'content/control-btns.php');
              $arr['status'] = 'ok';
              $arr['txt'][1] = str_replace('{id}', $id, $msw_adm_commissions_add10) . $c_b;
            }
          } else {
            if (in_array($fm['saletotal'], array('0','0.00')) || in_array($fm['commtotal'], array('0','0.00')) || $fm['product'] == '0' || $fm['aff_id'] == '0') {
              $arr['txt'][1] = $msw_adm_commissions_add_err;
            } else {
              $COM->update($fm);
              $controlButtons = array(
                '<a href="?p=commissions-add&amp;id=' . $fm['id'] . '&amp;aid=' . $fm['aff_id'] . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_commissions_add9 . '</span></a>',
                '<a href="?p=commissions-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_commissions_add8 . '</span></a>',
                '<a href="?p=commissions-manage"><i class="fa fa-line-chart fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav25 . '</span></a>'
              );
              include(PATH . 'content/control-btns.php');
              $arr['status'] = 'ok';
              $arr['txt'][1] = $msw_adm_commissions_add11 . $c_b;
            }
          }
          break;
        case 'commissions-approve':
          include(MSW_ADM_LANG . 'commissions-manage.php');
          include(MSW_ADM_LANG . 'commissions-approve.php');
          $fm = array(
            'ids' => (!empty($_POST['fm']['id']) ? $_POST['fm']['id'] : [])
          );
          if (!empty($fm['ids'])) {
            include(GPATH . 'control/mail-init.php');
            $msg = mswTmp(MSW_EM_LANG . LANG_FLDR_ADM . '/commission-approved.txt');
            $sbj = str_replace('{website}', $SETTINGS->website, $msw_emails7);
            $sent = 0;
            $COM->activate($fm);
            foreach ($fm['ids'] AS $aID) {
              $Q  = $DB->db_query("SELECT *,
                    (SELECT CONCAT(`fname`,'###',`lname`,'###',`email`) FROM `" . DB_PREFIX . "affiliates` WHERE `" . DB_PREFIX . "affiliates`.`id` = `" . DB_PREFIX . "affiliates_commissions`.`affid`) AS `affData`,
                    (SELECT `name` FROM `" . DB_PREFIX . "products` WHERE `" . DB_PREFIX . "products`.`id` = `" . DB_PREFIX . "affiliates_commissions`.`product`) AS `productName`,
                    (SELECT `url` FROM `" . DB_PREFIX . "products` WHERE `" . DB_PREFIX . "products`.`id` = `" . DB_PREFIX . "affiliates_commissions`.`product`) AS `productUrl`
                    FROM `" . DB_PREFIX . "affiliates_commissions`
                    WHERE `id` = '{$aID}'
                    ");
              $CM = $DB->db_object($Q);
              if (isset($CM->id)) {
                $chop = explode('###', $CM->affData);
                if (isset($chop[0],$chop[1],$chop[2]) && $CM->productName) {
                  $MAILR->addTag('{NAME}', $chop[0] . ' ' . $chop[1]);
                  $MAILR->addTag('{FNAME}', $chop[0]);
                  $MAILR->addTag('{LNAME}', $chop[1]);
                  $MAILR->addTag('{SALE}', mswFPCE($CM->saletotal, true));
                  $MAILR->addTag('{COMMISSION}', mswFPCE($CM->commtotal, true));
                  $MAILR->addTag('{PRODUCT}', $CM->productName);
                  $MAILR->addTag('{PRODUCT_URL}', $CM->productUrl);
                  $MAILR->addTag('{CURRENCY}', $SETTINGS->currency);
                  $MAILR->addTag('{CURRENCY_NAME}', ($SETTINGS->currency && isset($currencies[$SETTINGS->currency]) ? $currencies[$SETTINGS->currency] : '-'));
                  $MAILR->sendMail(array(
                    'from_email' => ($SETTINGS->smtp_email ? $SETTINGS->smtp_email : $SETTINGS->email),
                    'from_name' => ($SETTINGS->smtp_from ? $SETTINGS->smtp_from : $SETTINGS->website),
                    'to_email' => $chop[2],
                    'to_name' => $chop[0] . ' ' . $chop[1],
                    'subject' => $sbj,
                    'replyto' => array(
                      'name' => ($SETTINGS->smtp_rfrom ? $SETTINGS->smtp_rfrom : $SETTINGS->website),
                      'email' => ($SETTINGS->smtp_remail ? $SETTINGS->smtp_remail : $SETTINGS->email)
                    ),
                    'template' => $msg,
                    'alive' => 'yes',
                    'language' => $SETTINGS->language
                  ));
                  ++$sent;
                }
              }
            }
            if ($sent > 0) {
              $MAILR->smtpClose();
            }
            $arr['tr-ds'] = $fm['ids'];
            $controlButtons = array(
              '<a href="?p=commissions-approve"><i class="fa fa-check fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav13 . '</span></a>',
              '<a href="?p=commissions-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav24 . '</span></a>',
              '<a href="?p=commissions-manage"><i class="fa fa-line-chart fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav25 . '</span></a>'
            );
            include(PATH . 'content/control-btns.php');
            $arr['status'] = 'ok';
            $arr['txt'][1] = str_replace('{count}', mswNFM(count($fm['ids'])), $msw_adm_commissions_approve3) . $c_b;
          } else {
            $arr['txt'][1] = $msw_adm_commissions_manage_err;
          }
          break;
        case 'commissions-approve-del':
        case 'commissions-manage-del':
          include(MSW_ADM_LANG . 'commissions-manage.php');
          $fm = array(
            'ids' => (!empty($_POST['fm']['id']) ? $_POST['fm']['id'] : [])
          );
          if (!empty($fm['ids'])) {
            $COM->delete($fm);
            $controlButtons = array(
              '<a href="?p=aff-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_commissions_add8 . '</span></a>'
            );
            if ($_GET['ajax-ops'] == 'commissions-manage-del') {
              $controlButtons[] = '<a href="?p=commissions-manage"><i class="fa fa-line-chart fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav25 . '</span></a>';
            } else {
              $controlButtons[] = '<a href="?p=commissions-approve"><i class="fa fa-line-chart fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav13 . '</span></a>';
            }
            include(PATH . 'content/control-btns.php');
            $arr['tr-ds'] = $fm['ids'];
            $arr['status'] = 'ok';
            $arr['txt'][1] = str_replace('{count}', mswNFM(count($fm['ids'])), $msw_adm_commissions_manage11) . $c_b;
          } else {
            $arr['txt'][1] = $msw_adm_commissions_manage_err;
          }
          break;
        case 'commissions-export':
        case 'commissions-export-dl':
          include(MSW_ADM_LANG . 'commissions-manage.php');
          include(MSW_ADM_LANG . 'commissions-search.php');
          switch($_GET['ajax-ops']) {
            case 'commissions-export':
              $fm = array(
                'q' => (isset($_GET['q']) ? urldecode($_GET['q']) : ''),
                'id' => (isset($_GET['id']) ? (int) $_GET['id'] : ''),
                'sort' => array(
                  'def' => $msw_adm_commissions_search_default_sort,
                  'options' => $msw_adm_commissions_search_sort
                ),
                'header' => $msw_adm_commissions_manage14
              );
              $data = $COM->export($fm);
              if ($data[0] && file_exists($data[1])) {
                $arr['status'] = 'ok';
                $arr['loc'] = 'index.php?ajax-ops=commissions-export-dl&f=' . basename($data[1]) . '&csrf=' . $SSN->get('csrf_token');
              } else {
                $arr['txt'][1] = $msw_adm_commissions_manage13;
              }
              break;
            case 'commissions-export-dl':
              if (isset($_GET['f']) && file_exists(SYS_TEMP_DIR . $_GET['f'])) {
                include(GPATH . 'control/classes/system/class.download.php');
                $DL = new download();
                $DL->dl(
                  SYS_TEMP_DIR . $_GET['f'],
                  'text/csv',
                  'yes'
                );
              }
              break;
          }
          break;
      }
      break;

    // Affiliates
    case 'aff-add':
    case 'aff-add-edit':
    case 'aff-import':
    case 'aff-approve':
    case 'aff-commissions-del':
    case 'aff-approve-del':
    case 'aff-manage-del':
    case 'aff-export':
    case 'aff-export-dl':
    case 'aff-referrals-del':
    case 'referrals-export':
    case 'referrals-export-dl':
    case 'referral-edit':
      include(MSW_ADM_LANG . 'aff-add.php');
      include(PATH . 'control/classes/class.affiliates.php');
      $AFF           = new affiliates();
      $AFF->dt       = $DT;
      $AFF->settings = $SETTINGS;
      $AFF->sys      = $SYS;
      switch($_GET['ajax-ops']) {
        case 'aff-add':
        case 'aff-add-edit':
          $fm = array(
            'fname' => (isset($_POST['fm']['fname']) ? $_POST['fm']['fname'] : ''),
            'lname' => (isset($_POST['fm']['lname']) ? $_POST['fm']['lname'] : ''),
            'email' => (isset($_POST['fm']['email']) && mswEmVal($_POST['fm']['email']) == 'yes' ? $_POST['fm']['email'] : ''),
            'pass' => (isset($_POST['fm']['pass']) ? $_POST['fm']['pass'] : ''),
            'opass' => (isset($_POST['fm']['opass']) ? $_POST['fm']['opass'] : ''),
            'rdr' => (isset($_POST['fm']['rdr']) ? $_POST['fm']['rdr'] : ''),
            'en' => (isset($_POST['fm']['en']) && in_array($_POST['fm']['en'], array('yes','no')) ? $_POST['fm']['en'] : 'yes'),
            'code' => (isset($_POST['fm']['code']) ? preg_replace('/[^0-9a-zA-Z_\-]/', '', $_POST['fm']['code']) : ''),
            'commtier' => (isset($_POST['fm']['commtier']) ? $_POST['fm']['commtier'] : ''),
            'ts' => (isset($_POST['fm']['ts']) ? $DT->jstots($_POST['fm']['ts']) : $DT->ts()),
            'ip' => (isset($_POST['fm']['ip']) ? $_POST['fm']['ip'] : ''),
            'payinfo' => (isset($_POST['fm']['payinfo']) ? $_POST['fm']['payinfo'] : ''),
            'notes' => (isset($_POST['fm']['notes']) ? $_POST['fm']['notes'] : ''),
            'id' => (isset($_POST['fm']['id']) ? (int) $_POST['fm']['id'] : '0')
          );
          if ($_GET['ajax-ops'] == 'aff-add') {
            if ($fm['fname'] == '' || $fm['lname'] == '' || $fm['pass'] == '' || $fm['email'] == '') {
              $arr['txt'][1] = $msw_adm_aff_add_err;
            } else {
              if ($DB->db_rowcount('affiliates', 'WHERE `email` = \'' . $DB->db_escape($fm['email']) . '\'') > 0) {
                $arr['txt'][1] = $msw_adm_aff_add_err2;
              } else {
                if (LICENCE_VER == 'locked' && !defined('LIC_DEV') && ($DB->db_rowcount('affiliates') + 1) > RESTR_AFFILIATES) {
                  $arr['txt'][1] = str_replace('{affiliates}', RESTR_AFFILIATES, $msw_adm_add_err3);
                } else {
                  $id = $AFF->add($fm);
                  $controlButtons = array(
                    '<a href="?p=aff-add&amp;id=' . $id . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_aff_add15 . '</span></a>',
                    '<a href="?p=aff-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_aff_add14 . '</span></a>',
                    '<a href="?p=aff-manage"><i class="fa fa-users fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav23 . '</span></a>'
                  );
                  include(PATH . 'content/control-btns.php');
                  $arr['status'] = 'ok';
                  $arr['txt'][1] = str_replace('{id}', $id, $msw_adm_aff_add16) . $c_b;
                }
              }
            }
          } else {
            if ($fm['fname'] == '' || $fm['lname'] == '' || $fm['email'] == '') {
              $arr['txt'][1] = $msw_adm_aff_add_err4;
            } else {
              if ($DB->db_rowcount('affiliates', 'WHERE `email` = \'' . $DB->db_escape($fm['email']) . '\' AND `id` != \'' . $fm['id'] . '\'') > 0) {
                $arr['txt'][1] = $msw_adm_aff_add_err2;
              } else {
                $AFF->update($fm);
                $controlButtons = array(
                  '<a href="?p=aff-add&amp;id=' . $fm['id'] . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_aff_add15 . '</span></a>',
                  '<a href="?p=aff-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_aff_add14 . '</span></a>'
                );
                switch($fm['rdr']) {
                  case 'aff-approve':
                    $controlButtons[] = '<a href="?p=aff-approve"><i class="fa fa-check fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav28 . '</span></a>';
                    break;
                  default:
                    $controlButtons[] = '<a href="?p=aff-manage"><i class="fa fa-users fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav23 . '</span></a>';
                    break;
                }
                include(PATH . 'content/control-btns.php');
                $arr['status'] = 'ok';
                $arr['txt'][1] = $msw_adm_aff_add17 . $c_b;
              }
            }
          }
          break;
        case 'aff-import':
          include(MSW_ADM_LANG . 'aff-import.php');
          $fm = array(
            'file' => (isset($_FILES['fm']['tmp_name']['file'][0]) ? $_FILES['fm']['tmp_name']['file'][0] : '')
          );
          if ($fm['file'] == '') {
            $arr['txt'][1] = $msw_adm_aff_import5;
          } else {
            $c = $AFF->import($fm);
            $controlButtons = array(
              '<a href="?p=aff-manage"><i class="fa fa-users fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav23 . '</span></a>'
            );
            include(PATH . 'content/control-btns.php');
            $arr['status'] = 'ok';
            if ($c[0] > 0) {
              $arr['txt'][1] = str_replace(array('{count}','{pass}','{dupes}'), array(mswNFM($c[0]),$c[1],mswNFM($c[2])), $msw_adm_aff_import4) . $c_b;
            } else {
              $arr['txt'][1] = $msw_adm_aff_import6;
            }
          }
          break;
        case 'aff-approve':
          include(MSW_ADM_LANG . 'aff-manage.php');
          include(MSW_ADM_LANG . 'aff-approve.php');
          $fm = array(
            'ids' => (!empty($_POST['fm']['id']) ? $_POST['fm']['id'] : [])
          );
          if (!empty($fm['ids'])) {
            include(GPATH . 'control/mail-init.php');
            $msg = mswTmp(MSW_EM_LANG . LANG_FLDR_ADM . '/affiliate-approved.txt');
            $sbj = str_replace('{website}', $SETTINGS->website, $msw_emails4);
            foreach ($fm['ids'] AS $aID) {
              $Q   = $DB->db_query("SELECT * FROM `" . DB_PREFIX . "affiliates` WHERE `id` = '{$aID}'");
              $AFA = $DB->db_object($Q);
              if (isset($AFA->id)) {
                $pass = $AFF->password();
                $AFF->activate($AFA->id, $pass);
                $MAILR->addTag('{NAME}', $AFA->fname . ' ' . $AFA->lname);
                $MAILR->addTag('{FNAME}', $AFA->fname);
                $MAILR->addTag('{LNAME}', $AFA->lname);
                $MAILR->addTag('{EMAIL}', $AFA->email);
                $MAILR->addTag('{PASS}', $pass);
                $MAILR->sendMail(array(
                  'from_email' => ($SETTINGS->smtp_email ? $SETTINGS->smtp_email : $SETTINGS->email),
                  'from_name' => ($SETTINGS->smtp_from ? $SETTINGS->smtp_from : $SETTINGS->website),
                  'to_email' => $AFA->email,
                  'to_name' => $AFA->fname . ' ' . $AFA->lname,
                  'subject' => $sbj,
                  'replyto' => array(
                    'name' => ($SETTINGS->smtp_rfrom ? $SETTINGS->smtp_rfrom : $SETTINGS->website),
                    'email' => ($SETTINGS->smtp_remail ? $SETTINGS->smtp_remail : $SETTINGS->email)
                  ),
                  'template' => $msg,
                  'alive' => 'yes',
                  'language' => $SETTINGS->language
                ));
              }
            }
            $MAILR->smtpClose();
            $arr['tr-ds'] = $fm['ids'];
            $controlButtons = array(
              '<a href="?p=aff-approve"><i class="fa fa-check fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav28 . '</span></a>',
              '<a href="?p=aff-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_aff_add14 . '</span></a>',
              '<a href="?p=aff-manage"><i class="fa fa-users fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav23 . '</span></a>'
            );
            include(PATH . 'content/control-btns.php');
            $arr['status'] = 'ok';
            $arr['txt'][1] = str_replace('{count}', mswNFM(count($fm['ids'])), $msw_adm_aff_approve6) . $c_b;
          } else {
            $arr['txt'][1] = $msw_adm_aff_manage_err;
          }
          break;
        case 'aff-commissions-del':
          include(MSW_ADM_LANG . 'commissions-manage.php');
          $fm = array(
            'ids' => (!empty($_POST['fm']['id']) ? $_POST['fm']['id'] : [])
          );
          if (!empty($fm['ids'])) {
            $arr['tr-ds'] = $fm['ids'];
            $controlButtons = array(
              '<a href="?p=aff-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_aff_add14 . '</span></a>',
              '<a href="?p=aff-manage"><i class="fa fa-users fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav23 . '</span></a>'
            );
            include(PATH . 'content/control-btns.php');
            $arr['status'] = 'ok';
            $arr['txt'][1] = str_replace('{count}', mswNFM(count($fm['ids'])), $msw_adm_commissions_manage11) . $c_b;
          } else {
            $arr['txt'][1] = $msw_adm_commissions_manage_err;
          }
          break;
        case 'aff-approve-del':
        case 'aff-manage-del':
          include(MSW_ADM_LANG . 'aff-manage.php');
          $fm = array(
            'ids' => (!empty($_POST['fm']['id']) ? $_POST['fm']['id'] : [])
          );
          if (!empty($fm['ids'])) {
            $AFF->delete($fm);
            $controlButtons = array(
              '<a href="?p=aff-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_aff_add14 . '</span></a>'
            );
            if ($_GET['ajax-ops'] == 'aff-manage-del') {
              $controlButtons[] = '<a href="?p=aff-manage"><i class="fa fa-users fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav23 . '</span></a>';
            } else {
              $controlButtons[] = '<a href="?p=aff-approve"><i class="fa fa-users fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav28 . '</span></a>';
            }
            include(PATH . 'content/control-btns.php');
            $arr['tr-ds'] = $fm['ids'];
            $arr['status'] = 'ok';
            $arr['txt'][1] = str_replace('{count}', mswNFM(count($fm['ids'])), $msw_adm_aff_manage16) . $c_b;
          } else {
            $arr['txt'][1] = $msw_adm_aff_manage_err;
          }
          break;
        case 'aff-export':
        case 'aff-export-dl':
          include(MSW_ADM_LANG . 'aff-manage.php');
          include(MSW_ADM_LANG . 'aff-search.php');
          switch($_GET['ajax-ops']) {
            case 'aff-export':
              $fm = array(
                'q' => (isset($_GET['q']) ? urldecode($_GET['q']) : ''),
                'sort' => array(
                  'def' => $msw_adm_aff_search_default_sort,
                  'options' => $msw_adm_aff_search_sort
                ),
                'header' => $msw_adm_aff_manage19,
                'lang' => array($msw_common17)
              );
              $data = $AFF->export($fm);
              if ($data[0] && file_exists($data[1])) {
                $arr['status'] = 'ok';
                $arr['loc'] = 'index.php?ajax-ops=aff-export-dl&f=' . basename($data[1]) . '&csrf=' . $SSN->get('csrf_token');
              } else {
                $arr['txt'][1] = $msw_adm_aff_manage17;
              }
              break;
            case 'aff-export-dl':
              if (isset($_GET['f']) && file_exists(SYS_TEMP_DIR . $_GET['f'])) {
                include(GPATH . 'control/classes/system/class.download.php');
                $DL = new download();
                $DL->dl(
                  SYS_TEMP_DIR . $_GET['f'],
                  'text/csv',
                  'yes'
                );
              }
              break;
          }
          break;
        case 'aff-referrals-del':
          include(MSW_ADM_LANG . 'aff-referrals.php');
          $fm = array(
            'ids' => (!empty($_POST['fm']['id']) ? $_POST['fm']['id'] : [])
          );
          if (!empty($fm['ids'])) {
            $AFF->deleteref($fm);
            $arr['tr-ds']  = $fm['ids'];
            $controlButtons = array(
              '<a href="?p=aff-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_aff_add14 . '</span></a>',
              '<a href="#" onclick="window.location.reload();return false"><i class="fa fa-globe fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_aff_referral9 . '</span></a>',
              '<a href="?p=aff-manage"><i class="fa fa-users fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav23 . '</span></a>'
            );
            include(PATH . 'content/control-btns.php');
            $arr['status'] = 'ok';
            if (isset($msw_adm_aff_referral13)) {
              $arr['txt'][1] = str_replace('{count}', mswNFM(count($fm['ids'])), $msw_adm_aff_referral13) . $c_b;
            } else {
              $arr['txt'][1] = 'OK';
            }
          } else {
            $arr['txt'][1] = (isset($msw_adm_aff_referral_edit8) ? $msw_adm_aff_referral_edit8 : 'OK');
          }
          break;
        case 'referrals-export':
        case 'referrals-export-dl':
          include(MSW_ADM_LANG . 'aff-referrals.php');
          switch($_GET['ajax-ops']) {
            case 'referrals-export':
              $fm = array(
                'q' => (isset($_GET['q']) ? urldecode($_GET['q']) : ''),
                'id' => (isset($_GET['id']) ? (int) $_GET['id'] : '0'),
                'header' => $msw_adm_aff_referral12,
                'lang' => array($msw_common17)
              );
              $data = $AFF->exportReferrals($fm);
              if ($data[0] && file_exists($data[1])) {
                $arr['status'] = 'ok';
                $arr['loc'] = 'index.php?ajax-ops=referrals-export-dl&f=' . basename($data[1]) . '&csrf=' . $SSN->get('csrf_token');
              } else {
                $arr['txt'][1] = $msw_adm_aff_referral10;
              }
              break;
            case 'referrals-export-dl':
              if (isset($_GET['f']) && file_exists(SYS_TEMP_DIR . $_GET['f'])) {
                include(GPATH . 'control/classes/system/class.download.php');
                $DL = new download();
                $DL->dl(
                  SYS_TEMP_DIR . $_GET['f'],
                  'text/csv',
                  'yes'
                );
              }
              break;
          }
          break;
        case 'referral-edit':
          include(MSW_ADM_LANG . 'aff-referrals.php');
          $fm = array(
            'product' => (isset($_POST['fm']['product']) ? (int) $_POST['fm']['product'] : '0'),
            'referrer' => (isset($_POST['fm']['referrer']) ? $_POST['fm']['referrer'] : ''),
            'count' => (isset($_POST['fm']['count']) ? (int) $_POST['fm']['count'] : '0'),
            'aff' => (isset($_POST['fm']['aff']) ? (int) $_POST['fm']['aff'] : '0'),
            'id' => (isset($_POST['fm']['id']) ? (int) $_POST['fm']['id'] : '0')
          );
          if ($fm['product'] == '0' || $fm['referrer'] == '') {
            $arr['txt'][1] = $msw_adm_aff_referral_edit7;
          } else {
            $AFF->updatereferral($fm);
            $controlButtons = array(
              '<a href="?p=referral&amp;id=' . $fm['id'] . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_aff_referral_edit2 . '</span></a>',
              '<a href="?p=aff-referrals&amp;id=' . $fm['aff'] . '"><i class="fa fa-globe fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_aff_referral9 . '</span></a>'
            );
            include(PATH . 'content/control-btns.php');
            $arr['status'] = 'ok';
            $arr['txt'][1] = $msw_adm_aff_referral_edit6 . $c_b;
          }
          break;
      }
      break;

    // Products
    case 'product-add':
    case 'product-add-edit':
    case 'product-manage-del':
    case 'rem_promo':
      include(MSW_ADM_LANG . 'product-add.php');
      include(PATH . 'control/classes/class.products.php');
      $PRD           = new products();
      $PRD->dt       = $DT;
      $PRD->settings = $SETTINGS;
      $PRD->sys      = $SYS;
      switch($_GET['ajax-ops']) {
        case 'product-add':
        case 'product-add-edit':
          $fm = array(
            'name' => (isset($_POST['fm']['name']) ? $_POST['fm']['name'] : ''),
            'url' => (isset($_POST['fm']['url']) ? $_POST['fm']['url'] : ''),
            'uprotocol' => (isset($_POST['fm']['url_protocol']) && in_array($_POST['fm']['url_protocol'], array('http','https')) ? $_POST['fm']['url_protocol'] : 'http'),
            'info' => (isset($_POST['fm']['info']) ? $_POST['fm']['info'] : ''),
            'en' => (isset($_POST['fm']['en']) && in_array($_POST['fm']['en'], array('yes','no')) ? $_POST['fm']['en'] : 'yes'),
            'p_info' => (isset($_POST['fm']['pinfo']) ? $_POST['fm']['pinfo'] : ''),
            'p_file' => (isset($_FILES['fm']['tmp_name']['file'][0]) ? $_FILES['fm']['tmp_name']['file'][0] : ''),
            'p_name' => (isset($_FILES['fm']['name']['file'][0]) ? $PRD->cleanFileName($_FILES['fm']['name']['file'][0]) : ''),
            'p_size' => (isset($_FILES['fm']['size']['file'][0]) ? $_FILES['fm']['size']['file'][0] : ''),
            'p_mime' => (isset($_FILES['fm']['mime']['file'][0]) ? $_FILES['fm']['mime']['file'][0] : ''),
            'link' => (isset($_POST['fm']['link']) ? $_POST['fm']['link'] : ''),
            'lprotocol' => (isset($_POST['fm']['link_protocol']) && in_array($_POST['fm']['link_protocol'], array('http','https')) ? $_POST['fm']['link_protocol'] : 'http'),
            'zip' => (isset($_POST['fm']['zip']) ? $_POST['fm']['zip'] : ''),
            'id' => (isset($_POST['fm']['id']) ? (int) $_POST['fm']['id'] : '0')
          );
          if ($_GET['ajax-ops'] == 'product-add') {
            if ($fm['name'] == '' || $fm['url'] == '') {
              $arr['txt'][1] = $msw_adm_add_err;
            } else if ($fm['p_file'] && $fm['p_size'] > 0 && $PRD->extCheck($fm['p_name']) == 'no') {
              $arr['txt'][1] = str_replace('{allowed}', str_replace(',', ', ', ALLOWED_PRODUCT_PROMO_EXTENSIONS), $msw_11_admin[15]);
            } else {
              if ($DB->db_rowcount('products', 'WHERE `name` = \'' . $DB->db_escape($fm['name']) . '\'') > 0) {
                $arr['txt'][1] = $msw_adm_add_err2;
              } else {
                if (LICENCE_VER == 'locked' && !defined('LIC_DEV') && ($DB->db_rowcount('products') + 1) > RESTR_PRODUCTS) {
                  $arr['txt'][1] = str_replace('{products}', RESTR_PRODUCTS, $msw_adm_add_err3);
                } else {
                  $id = $PRD->add($fm);
                  $controlButtons = array(
                    '<a href="?p=product-add&amp;id=' . $id . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_add11 . '</span></a>',
                    '<a href="?p=product-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_add4 . '</span></a>',
                    '<a href="?p=product-manage"><i class="fa fa-cubes fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav4 . '</span></a>'
                  );
                  include(PATH . 'content/control-btns.php');
                  $arr['status'] = 'ok';
                  $arr['txt'][1] = str_replace('{id}', $id, $msw_adm_add9) . $c_b;
                }
              }
            }
          } else {
            if ($fm['name'] == '' || $fm['url'] == '') {
              $arr['txt'][1] = $msw_adm_add_err;
            } else if ($fm['p_file'] && $fm['p_size'] > 0 && $PRD->extCheck($fm['p_name']) == 'no') {
              $arr['txt'][1] = str_replace('{allowed}', str_replace(',', ', ', ALLOWED_PRODUCT_PROMO_EXTENSIONS), $msw_11_admin[15]);
            } else {
              if ($DB->db_rowcount('products', 'WHERE `name` = \'' . $DB->db_escape($fm['name']) . '\' AND `id` != \'' . $fm['id'] . '\'') > 0) {
                $arr['txt'][1] = $msw_adm_add_err2;
              } else {
                $PRD->update($fm);
                $controlButtons = array(
                  '<a href="?p=product-add&amp;id=' . $fm['id'] . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_add11 . '</span></a>',
                  '<a href="?p=product-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_add4 . '</span></a>',
                  '<a href="?p=product-manage"><i class="fa fa-cubes fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav4 . '</span></a>'
                );
                include(PATH . 'content/control-btns.php');
                $arr['status'] = 'ok';
                $arr['txt'][1] = $msw_adm_add10 . $c_b;
              }
            }
          }
          break;
        case 'product-manage-del':
          include(MSW_ADM_LANG . 'product-manage.php');
          $fm = array(
            'ids' => (!empty($_POST['fm']['id']) ? $_POST['fm']['id'] : [])
          );
          if (!empty($fm['ids'])) {
            $PRD->delete($fm);
            $controlButtons = array(
              '<a href="?p=product-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_add4 . '</span></a>',
              '<a href="?p=product-manage"><i class="fa fa-cubes fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav4 . '</span></a>'
            );
            include(PATH . 'content/control-btns.php');
            $arr['tr-ds']  = $fm['ids'];
            $arr['status'] = 'ok';
            $arr['txt'][1] = str_replace('{count}', mswNFM(count($fm['ids'])), $msw_adm_manage10) . $c_b;
          } else {
            $arr['txt'][1] = $msw_adm_manage11;
          }
          break;
        case 'rem_promo':
          include(MSW_ADM_LANG . 'product-manage.php');
          $fm = array(
            'id' => (isset($_GET['id']) ? (int) $_GET['id'] : '0')
          );
          if ($fm['id'] > 0) {
            $PRD->delPromo($fm);
            $controlButtons = array(
              '<a href="?p=product-add&amp;id=' . $fm['id'] . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_add11 . '</span></a>',
              '<a href="?p=product-add"><i class="fa fa-plus fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_add4 . '</span></a>',
              '<a href="?p=product-manage"><i class="fa fa-cubes fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav4 . '</span></a>'
            );
            $arr['status'] = 'ok';
            include(PATH . 'content/control-btns.php');
            $arr['txt'][1] = $msw_11_admin[16] . $c_b;
          }
          break;
      }
      break;

    // Themes
    case 'themes':
      include(MSW_ADM_LANG . 'themes.php');
      $fm = array(
        'theme' => (isset($_POST['fm']['theme']) ? $_POST['fm']['theme'] : '_theme_default'),
        'entheme' => (isset($_POST['fm']['entheme']) && in_array($_POST['fm']['entheme'], array('yes','no')) ? $_POST['fm']['entheme'] : 'no'),
        'thm' => (!empty($_POST['fm']['thm']) ? $_POST['fm']['thm'] : []),
        'from' => (!empty($_POST['fm']['from']) ? $_POST['fm']['from'] : []),
        'to' => (!empty($_POST['fm']['to']) ? $_POST['fm']['to'] : [])
      );
      $SYS->themes($fm);
      $controlButtons = array(
        '<a href="?p=themes"><i class="fa fa-html5 fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav20 . '</span></a>'
      );
      include(PATH . 'content/control-btns.php');
      $arr['status'] = 'ok';
      $arr['txt'][1] = $msw_adm_themes7 . $c_b;
      break;

    // Boxes
    case 'boxes':
    case 'boxes-edit':
    case 'boxes-del':
    case 'boxes-sort':
      include(MSW_ADM_LANG . 'boxes.php');
      include(PATH . 'control/classes/class.boxes.php');
      $BOX           = new box();
      $BOX->dt       = $DT;
      $BOX->settings = $SETTINGS;
      switch($_GET['ajax-ops']) {
        case 'boxes':
        case 'boxes-edit':
          $fm = array(
            'title' => (isset($_POST['fm']['title']) ? $_POST['fm']['title'] : ''),
            'info' => (isset($_POST['fm']['info']) ? $_POST['fm']['info'] : ''),
            'tmp' => (isset($_POST['fm']['tmp']) ? $_POST['fm']['tmp'] : ''),
            'icon' => (isset($_POST['fm']['icon']) ? $_POST['fm']['icon'] : 'th'),
            'en' => (isset($_POST['fm']['en']) && in_array($_POST['fm']['en'], array('yes','no')) ? $_POST['fm']['en'] : 'yes'),
            'id' => (isset($_POST['fm']['id']) ? (int) $_POST['fm']['id'] : '0')
          );
          if ($fm['title'] == '' || ($fm['info'] == '' && $fm['tmp'] == '')) {
            $arr['txt'][1] = $msw_adm_boxes14;
          } else {
            if ($_GET['ajax-ops'] == 'boxes') {
              if ($DB->db_rowcount('boxes', 'WHERE `title` = \'' . $DB->db_escape($fm['title']) . '\'') > 0) {
                $arr['txt'][1] = $msw_adm_boxes13;
              } else {
                if (LICENCE_VER == 'locked' && !defined('LIC_DEV') && ($DB->db_rowcount('boxes') + 1) > RESTR_BOXES) {
                  $arr['txt'][1] = str_replace('{boxes}', RESTR_BOXES, $msw_adm_boxes16);
                } else {
                  $ID = $BOX->add($fm);
                  $controlButtons = array(
                    '<a href="?p=boxes&amp;id=' . $ID . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_boxes8 . '</span></a>',
                    '<a href="?p=boxes"><i class="fa fa-cubes fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav8 . '</span></a>'
                  );
                  include(PATH . 'content/control-btns.php');
                  $arr['status'] = 'ok';
                  $arr['txt'][1] = $msw_adm_boxes10 . $c_b;
                }
              }
            } else {
              if ($DB->db_rowcount('boxes', 'WHERE `title` = \'' . $DB->db_escape($fm['title']) . '\' AND `id` != \'' . $fm['id'] . '\'') > 0) {
                $arr['txt'][1] = $msw_adm_boxes13;
              } else {
                $BOX->update($fm);
                $controlButtons = array(
                  '<a href="?p=boxes&amp;id=' . $fm['id'] . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_boxes8 . '</span></a>',
                  '<a href="?p=boxes"><i class="fa fa-cubes fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav8 . '</span></a>'
                );
                include(PATH . 'content/control-btns.php');
                $arr['status'] = 'ok';
                $arr['txt'][1] = $msw_adm_boxes11 . $c_b;
              }
            }
          }
          break;
        case 'boxes-del':
          $fm = array(
            'ids' => (!empty($_POST['fm']['id']) ? $_POST['fm']['id'] : [])
          );
          if (!empty($fm['ids'])) {
            $BOX->delete($fm);
            $controlButtons = array(
              '<a href="?p=boxes"><i class="fa fa-cubes fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav8 . '</span></a>'
            );
            include(PATH . 'content/control-btns.php');
            $arr['status'] = 'ok';
            $arr['tr-ds'] = $fm['ids'];
            $arr['txt'][1] = $msw_adm_boxes12 . $c_b;
          } else {
            $arr['txt'][1] = $msw_common8;
          }
          break;
        case 'boxes-sort':
          $fm = array(
            'sort' => (!empty($_POST['fm']['sort']) ? $_POST['fm']['sort'] : [])
          );
          if (!empty($fm['sort'])) {
            $BOX->order($fm);
            $arr['status'] = 'ok';
          }
          break;
      }
      break;
      
    // Admin Pages
    case 'apages':
    case 'apages-edit':
    case 'apages-del':
    case 'apages-sort':
      include(MSW_ADM_LANG . 'apages.php');
      include(PATH . 'control/classes/class.pages.php');
      $PAGS           = new page();
      $PAGS->dt       = $DT;
      $PAGS->settings = $SETTINGS;
      switch($_GET['ajax-ops']) {
        case 'apages':
        case 'apages-edit':
          $fm = array(
            'name' => (isset($_POST['fm']['name']) ? $_POST['fm']['name'] : ''),
            'info' => (isset($_POST['fm']['info']) ? $_POST['fm']['info'] : ''),
            'tmp' => (isset($_POST['fm']['tmp']) ? $_POST['fm']['tmp'] : ''),
            'en' => (isset($_POST['fm']['en']) && in_array($_POST['fm']['en'], array('yes','no')) ? $_POST['fm']['en'] : 'yes'),
            'id' => (isset($_POST['fm']['id']) ? (int) $_POST['fm']['id'] : '0')
          );
          if ($fm['name'] == '' || ($fm['info'] == '' && $fm['tmp'] == '')) {
            $arr['txt'][1] = $msw_adm_apages18;
          } else {
            if ($_GET['ajax-ops'] == 'apages') {
              if ($DB->db_rowcount('admin_pages', 'WHERE `name` = \'' . $DB->db_escape($fm['name']) . '\'') > 0) {
                $arr['txt'][1] = $msw_adm_apages17;
              } else {
                if (LICENCE_VER == 'locked' && !defined('LIC_DEV') && ($DB->db_rowcount('admin_pages') + 1) > RESTR_ADM_PAGES) {
                  $arr['txt'][1] = str_replace('{pages}', RESTR_ADM_PAGES, $msw_adm_apages20);
                } else {
                  $ID = $PAGS->admin_add($fm);
                  $controlButtons = array(
                    '<a href="?p=apages&amp;id=' . $ID . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_apages13 . '</span></a>',
                    '<a href="?p=apages"><i class="fa fa-file-text-o fa-fw"></i><span class="hidden-xs"> ' . $msw_11_admin_pages[0] . '</span></a>'
                  );
                  include(PATH . 'content/control-btns.php');
                  $arr['status'] = 'ok';
                  $arr['txt'][1] = $msw_adm_apages14 . $c_b;
                }
              }
            } else {
              if ($DB->db_rowcount('admin_pages', 'WHERE `name` = \'' . $DB->db_escape($fm['name']) . '\' AND `id` != \'' . $fm['id'] . '\'') > 0) {
                $arr['txt'][1] = $msw_adm_apages17;
              } else {
                $ID = $PAGS->admin_update($fm);
                $controlButtons = array(
                  '<a href="?p=apages&amp;id=' . $fm['id'] . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_apages13 . '</span></a>',
                  '<a href="?p=apages"><i class="fa fa-file-text-o fa-fw"></i><span class="hidden-xs"> ' . $msw_11_admin_pages[0] . '</span></a>'
                );
                include(PATH . 'content/control-btns.php');
                $arr['status'] = 'ok';
                $arr['txt'][1] = $msw_adm_apages15 . $c_b;
              }
            }
          }
          break;
        case 'apages-del':
          $fm = array(
            'ids' => (!empty($_POST['fm']['id']) ? $_POST['fm']['id'] : [])
          );
          if (!empty($fm['ids'])) {
            $PAGS->admin_delete($fm);
            $controlButtons = array(
              '<a href="?p=apages"><i class="fa fa-file-text-o fa-fw"></i><span class="hidden-xs"> ' . $msw_11_admin_pages[0] . '</span></a>'
            );
            include(PATH . 'content/control-btns.php');
            $arr['status'] = 'ok';
            $arr['tr-ds'] = $fm['ids'];
            $arr['txt'][1] = $msw_adm_apages16 . $c_b;
          } else {
            $arr['txt'][1] = $msw_common8;
          }
          break;
        case 'apages-sort':
          $fm = array(
            'sort' => (!empty($_POST['fm']['sort']) ? $_POST['fm']['sort'] : [])
          );
          if (!empty($fm['sort'])) {
            $PAGS->order($fm, 'admin_pages');
            $arr['status'] = 'ok';
          }
          break;
      }
      break;

    // Pages
    case 'pages':
    case 'pages-edit':
    case 'pages-del':
    case 'pages-sort':
      include(MSW_ADM_LANG . 'pages.php');
      include(PATH . 'control/classes/class.pages.php');
      $PAGS           = new page();
      $PAGS->dt       = $DT;
      $PAGS->settings = $SETTINGS;
      switch($_GET['ajax-ops']) {
        case 'pages':
        case 'pages-edit':
          $fm = array(
            'name' => (isset($_POST['fm']['name']) ? $_POST['fm']['name'] : ''),
            'info' => (isset($_POST['fm']['info']) ? $_POST['fm']['info'] : ''),
            'metat' => (isset($_POST['fm']['metat']) ? $_POST['fm']['metat'] : ''),
            'tmp' => (isset($_POST['fm']['tmp']) ? $_POST['fm']['tmp'] : ''),
            'en' => (isset($_POST['fm']['en']) && in_array($_POST['fm']['en'], array('yes','no')) ? $_POST['fm']['en'] : 'yes'),
            'id' => (isset($_POST['fm']['id']) ? (int) $_POST['fm']['id'] : '0')
          );
          if ($fm['name'] == '' || ($fm['info'] == '' && $fm['tmp'] == '')) {
            $arr['txt'][1] = $msw_adm_pages18;
          } else {
            if ($_GET['ajax-ops'] == 'pages') {
              if ($DB->db_rowcount('pages', 'WHERE `name` = \'' . $DB->db_escape($fm['name']) . '\'') > 0) {
                $arr['txt'][1] = $msw_adm_pages17;
              } else {
                if (LICENCE_VER == 'locked' && !defined('LIC_DEV') && ($DB->db_rowcount('pages') + 1) > RESTR_PAGES) {
                  $arr['txt'][1] = str_replace('{pages}', RESTR_PAGES, $msw_adm_pages20);
                } else {
                  $ID = $PAGS->add($fm);
                  $controlButtons = array(
                    '<a href="?p=pages&amp;id=' . $ID . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_pages13 . '</span></a>',
                    '<a href="?p=pages"><i class="fa fa-file-text-o fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav7 . '</span></a>'
                  );
                  include(PATH . 'content/control-btns.php');
                  $arr['status'] = 'ok';
                  $arr['txt'][1] = $msw_adm_pages14 . $c_b;
                }
              }
            } else {
              if ($DB->db_rowcount('pages', 'WHERE `name` = \'' . $DB->db_escape($fm['name']) . '\' AND `id` != \'' . $fm['id'] . '\'') > 0) {
                $arr['txt'][1] = $msw_adm_pages17;
              } else {
                $PAGS->update($fm);
                $controlButtons = array(
                  '<a href="?p=pages&amp;id=' . $fm['id'] . '"><i class="fa fa-edit fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_pages13 . '</span></a>',
                  '<a href="?p=pages"><i class="fa fa-file-text-o fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav7 . '</span></a>'
                );
                include(PATH . 'content/control-btns.php');
                $arr['status'] = 'ok';
                $arr['txt'][1] = $msw_adm_pages15 . $c_b;
              }
            }
          }
          break;
        case 'pages-del':
          $fm = array(
            'ids' => (!empty($_POST['fm']['id']) ? $_POST['fm']['id'] : [])
          );
          if (!empty($fm['ids'])) {
            $PAGS->delete($fm);
            $controlButtons = array(
              '<a href="?p=pages"><i class="fa fa-file-text-o fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav7 . '</span></a>'
            );
            include(PATH . 'content/control-btns.php');
            $arr['status'] = 'ok';
            $arr['tr-ds'] = $fm['ids'];
            $arr['txt'][1] = $msw_adm_pages16 . $c_b;
          } else {
            $arr['txt'][1] = $msw_common8;
          }
          break;
        case 'pages-sort':
          $fm = array(
            'sort' => (!empty($_POST['fm']['sort']) ? $_POST['fm']['sort'] : [])
          );
          if (!empty($fm['sort'])) {
            $PAGS->order($fm);
            $arr['status'] = 'ok';
          }
          break;
      }
      break;

    // Logout
    case 'logout':
      $arr['rdr'] = 'index.php?p=login';
      $SSN->delete(array('adm_maff_global'));
      // Clear cookies
      if ($SSN->active_c('adm_maff_cookie') == 'yes') {
        $SSN->delete_c(array('adm_maff_cookie'));
      }
      break;

    // Login
    case 'login':
      if (isset($_POST['usr'], $_POST['pw'])) {
        include(MSW_ADM_LANG . 'login.php');
        $arr['txt'][1] = $msw_adm_login5;
        if ($_POST['usr'] && $_POST['pw']) {
          if (defined('LG_RESTRICT_BY_IP') && LG_RESTRICT_BY_IP) {
            $allowed = array_map('trim', explode(',', LG_RESTRICT_BY_IP));
            $current = mswIP(true);
            if (isset($current[0]) && !in_array($current[0], $allowed)) {
              $ipBlock = true;
              $arr['txt'][1] = $msw_adm_login6;
            }
          }
          if (!isset($ipBlock)) {
            // Check global login first
            if (defined('LG_USER') && defined('LG_PASS') && $_POST['usr'] == LG_USER
                && mswEnc(SECRET_KEY . $_POST['pw']) == mswEnc(SECRET_KEY . LG_PASS)) {
              $SSN->set(array('adm_maff_user' => LG_USER));
              $SSN->set(array('adm_maff_global' => mswEnc(SECRET_KEY . mswEnc('gl0bal'))));
              // Set cookie
              if (ENABLE_LOGIN_COOKIE && isset($_POST['rm'])) {
                $SSN->set_c(array(
                  array(
                    'adm_maff_cookie',
                    mswEnc(SECRET_KEY . mswEnc('gl0bal')),
                    time() + 60 * 60 * 24 * LOGIN_COOKIE_DURATION
                  )
                ));
              }
              $arr['status'] = 'ok';
              $arr['rdr'] = 'index.php';
            }
          }
        }
      }
      break;

    // Database backup
    case 'backup':
    case 'backup-dl':
      include(MSW_ADM_LANG . 'backup.php');
      include(MSW_LANG . 'emails.php');
      include(PATH . 'control/classes/class.backup.php');
      switch($_GET['ajax-ops']) {
        case 'backup':
          mswTO();
          $fm = array(
            'time' => date('H:i:s'),
            'download' => (isset($_POST['fm']['download']) && in_array($_POST['fm']['download'], array('yes','no')) ? $_POST['fm']['download'] : 'yes'),
            'compress' => (isset($_POST['fm']['compress']) && in_array($_POST['fm']['compress'], array('yes','no')) ? $_POST['fm']['compress'] : 'no'),
            'emails' => (isset($_POST['fm']['emails']) ? $_POST['fm']['emails'] : '')
          );
          // File path..
          $fpath = SYS_TEMP_DIR . 'db-backup-' . $DT->ts() . ($fm['compress'] == 'yes' ? '.gz' : '.sql');
          // We don't need a temp directory if just saving to backup folder
          if ($fm['download'] == 'no') {
            if ($SETTINGS->bfolder && is_dir($SETTINGS->bfolder) && is_writeable($SETTINGS->bfolder)) {
              $fpath = $SETTINGS->bfolder . DIRECTORY_SEPARATOR . 'db-backup-' . $DT->ts() . ($fm['compress'] == 'yes' ? '.gz' : '.sql');
            } else {
              $arr['txt'][1] = $msw_11_admin[14];
              echo $JSON->encode($arr);
              exit;
            }
          }
          $DBB = new backup(array(
            'file' => $fpath,
            'cmp' => $fm['compress'],
            'db' => $DB
          ));
          $DBB->settings = $SETTINGS;
          $DBB->db       = $DB;
          $DBB->doDump();
          // Copy email addresses if set
          if ($fm['emails'] && file_exists($fpath)) {
            $s_emails = mswMlr('', $fm['emails']);
            include(GPATH . 'control/mail-init.php');
            $msg = mswTmp(MSW_EM_LANG . 'backup.txt');
            $sbj = str_replace('{website}', $SETTINGS->website, $msw_emails2);
            $MAILR->addTag('{DATE_TIME}', date($SETTINGS->dateformat) . ' @ ' . date($SETTINGS->timeformat));
            $MAILR->addTag('{VERSION}', SCRIPT_VERSION);
            $MAILR->addTag('{FILE}', basename($fpath));
            // Include attachment
            $MAILR->attachments[$fpath] = basename($fpath);
            // Send
            $MAILR->sendMail(array(
              'from_email' => ($SETTINGS->smtp_email ? $SETTINGS->smtp_email : $SETTINGS->email),
              'from_name' => ($SETTINGS->smtp_from ? $SETTINGS->smtp_from : $SETTINGS->website),
              'to_email' => $s_emails[0],
              'to_name' => $SETTINGS->website,
              'subject' => $sbj,
              'replyto' => array(
                'name' => ($SETTINGS->smtp_rfrom ? $SETTINGS->smtp_rfrom : $SETTINGS->website),
                'email' => ($SETTINGS->smtp_remail ? $SETTINGS->smtp_remail : $SETTINGS->email)
              ),
              'template' => $msg,
              'add-emails' => $s_emails[1],
              'language' => $SETTINGS->language
            ));
            $MAILR->smtpClose();
          }
          if ($fm['download'] == 'yes') {
            $arr['status'] = 'rdr';
            $arr['loc'] = 'index.php?ajax-ops=backup-dl&f=' . basename($fpath) . '&cp=' . $fm['compress'] . '&csrf=' . $SSN->get('csrf_token');
          } else {
            $controlButtons = array(
              '<a href="?p=backup"><i class="fa fa-database fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav16 . '</span></a>'
            );
            include(PATH . 'content/control-btns.php');
            if ($fm['emails']) {
              if (file_exists($fpath)) {
                try {
                  unlink($fpath);
                } catch(ErrorException $e) {
                  Debugger::log($e->getMessage());
                }
              }
              $arr['status'] = 'ok';
              $arr['txt'][1] = str_replace('{emails}',implode('<br>', $em),$msw_adm_backup14) . $c_b;
            } else {
              $arr['status'] = 'ok';
              $arr['txt'][1] = str_replace('{file}', basename($fpath),$msw_adm_backup15) . $c_b;
            }
          }
          break;
        case 'backup-dl':
          $_GET['f'] = (isset($_GET['f']) ? preg_replace('/[^0-9a-zA-Z.\-\s]/', '', $_GET['f']) : '');
          if ($_GET['f'] && file_exists(SYS_TEMP_DIR . $_GET['f'])) {
            include(GPATH . 'control/classes/system/class.download.php');
            $DL = new download();
            $DL->dl(
              SYS_TEMP_DIR . $_GET['f'],
              (isset($_GET['cp']) && $_GET['cp'] == 'yes' ? 'application/x-compressed' : 'text/plain'),
              'yes'
            );
          }
          break;
      }
      break;

    case 'settings':
    case 'settings-sort-menu-reset':
      include(MSW_ADM_LANG . 'settings.php');
      switch($_GET['ajax-ops']) {
        case 'settings':
          $fm = array(
            'website' => (isset($_POST['fm']['website']) ? $_POST['fm']['website'] : ''),
            'email' => (isset($_POST['fm']['email']) ? $_POST['fm']['email'] : ''),
            'ifolder' => (isset($_POST['fm']['ifolder']) ? $_POST['fm']['ifolder'] : ''),
            'protocol' => (isset($_POST['fm']['protocol']) && in_array($_POST['fm']['protocol'], array('http','https')) ? $_POST['fm']['protocol'] : 'http'),
            'dfolder' => (isset($_POST['fm']['dfolder']) ? $_POST['fm']['dfolder'] : ''),
            'language' => (isset($_POST['fm']['language']) ? $_POST['fm']['language'] : 'english'),
            'bfolder' => (isset($_POST['fm']['bfolder']) ? $_POST['fm']['bfolder'] : ''),
            'currency' => (isset($_POST['fm']['currency']) && in_array($_POST['fm']['currency'], array_keys($currencies)) ? $_POST['fm']['currency'] : ''),
            'createaff' => (isset($_POST['fm']['createaff']) && in_array($_POST['fm']['createaff'], array('yes','no')) ? $_POST['fm']['createaff'] : 'yes'),
            'approve' => (isset($_POST['fm']['approve']) && in_array($_POST['fm']['approve'], array('yes','no')) ? $_POST['fm']['approve'] : 'no'),
            'approveaff' => (isset($_POST['fm']['approveaff']) && in_array($_POST['fm']['approveaff'], array('yes','no')) ? $_POST['fm']['approveaff'] : 'no'),
            'affdel' => (isset($_POST['fm']['affdel']) && in_array($_POST['fm']['affdel'], array('yes','no')) ? $_POST['fm']['affdel'] : 'yes'),
            'ipmatch' => (isset($_POST['fm']['ipmatch']) && in_array($_POST['fm']['ipmatch'], array('yes','no')) ? $_POST['fm']['ipmatch'] : 'yes'),
            'affcode' => (isset($_POST['fm']['affcode']) ? preg_replace('/[^0-9a-zA-Z_\-]/', '', $_POST['fm']['affcode']) : ''),
            'priceformat' => (isset($_POST['fm']['priceformat']) ? $_POST['fm']['priceformat'] . (stripos($_POST['fm']['priceformat'], '{PRICE}') !==false ? '' : '{PRICE}') : ''),
            'dateformat' => (isset($_POST['fm']['dateformat']) ? $_POST['fm']['dateformat'] : 'j M Y'),
            'timeformat' => (isset($_POST['fm']['timeformat']) ? $_POST['fm']['timeformat'] : 'H:iA'),
            'timezone' => (isset($_POST['fm']['timezone']) && in_array($_POST['fm']['timezone'], array_keys($timezones)) ? $_POST['fm']['timezone'] : 'Europe/London'),
            'calformat' => (isset($_POST['fm']['calformat']) && in_array($_POST['fm']['calformat'], $jsCalFormat) ? $_POST['fm']['calformat'] : 'DD/MM/YYYY'),
            'weekstart' => (isset($_POST['fm']['weekstart']) && in_array($_POST['fm']['weekstart'], array('sun','mon')) ? $_POST['fm']['weekstart'] : 'sun'),
            'apikey' => (isset($_POST['fm']['apikey']) ? $_POST['fm']['apikey'] : ''),
            'apilog' => (isset($_POST['fm']['apilog']) && in_array($_POST['fm']['apilog'], array('yes','no')) ? $_POST['fm']['apilog'] : 'yes'),
            'pfoot' => (LICENCE_VER == 'unlocked' && isset($_POST['fm']['pfoot']) ? $_POST['fm']['pfoot'] : ''),
            'afoot' => (LICENCE_VER == 'unlocked' && isset($_POST['fm']['afoot']) ? $_POST['fm']['afoot'] : ''),
            'pr' => (isset($_POST['fm']['pr']) ? $_POST['fm']['pr'] : ''),
            'pr_areas' => (isset($_POST['fm']['pr_areas']) ? $_POST['fm']['pr_areas'] : []),
            'am_sections' => (!empty($_POST['am_sections']) ? $_POST['am_sections'] : []),
            'am_links' => (!empty($_POST['am_links']) ? $_POST['am_links'] : []),
            'delem' => (isset($_POST['fm']['delem']) && in_array($_POST['fm']['delem'], array('yes','no')) ? $_POST['fm']['delem'] : 'no')
          );
          if ($fm['website'] == '' || mswEmVal($fm['email']) == 'no' || $fm['ifolder'] == '') {
            $arr['txt'][1] = $msw_adm_settings29;
          } else {
            $SYS->settings($fm);
            $SYS->links($fm);
            $controlButtons = array(
              '<a href="?p=settings"><i class="fa fa-cog fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav9 . '</span></a>'
            );
            include(PATH . 'content/control-btns.php');
            $arr['status'] = 'ok';
            $arr['txt'][1] = $msw_adm_settings28 . $c_b;
          }
          break;
        case 'settings-sort-menu-reset':
          $SYS->links([], 'yes');
          $arr['status'] = 'rdr';
          $arr['loc'] = 'index.php?p=settings';
          break;
      }
      break;

    // Email settings
    case 'email':
    case 'mail':
      include(MSW_ADM_LANG . 'email.php');
      $controlButtons = array(
        '<a href="?p=email"><i class="fa fa-envelope fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav10 . '</span></a>'
      );
      include(PATH . 'content/control-btns.php');
      switch($_GET['ajax-ops']) {
        case 'email':
          $fm = array(
            'smtp_host' => (isset($_POST['fm']['smtp_host']) ? $_POST['fm']['smtp_host'] : ''),
            'smtp_port' => (isset($_POST['fm']['smtp_port']) ? (int) $_POST['fm']['smtp_port'] : '0'),
            'smtp_user' => (isset($_POST['fm']['smtp_user']) ? $_POST['fm']['smtp_user'] : ''),
            'smtp_pass' => (isset($_POST['fm']['smtp_pass']) ? $_POST['fm']['smtp_pass'] : ''),
            'smtp_from' => (isset($_POST['fm']['smtp_from']) ? $_POST['fm']['smtp_from'] : ''),
            'smtp_email' => (isset($_POST['fm']['smtp_email']) && mswEmVal($_POST['fm']['smtp_email']) == 'yes' ? $_POST['fm']['smtp_email'] : ''),
            'smtp_rfrom' => (isset($_POST['fm']['smtp_rfrom']) ? $_POST['fm']['smtp_rfrom'] : ''),
            'smtp_remail' => (isset($_POST['fm']['smtp_remail']) && mswEmVal($_POST['fm']['smtp_remail']) == 'yes' ? $_POST['fm']['smtp_remail'] : ''),
            'smtp_security' => (isset($_POST['fm']['smtp_security']) && in_array($_POST['fm']['smtp_security'], array('','tls','ssl')) ? $_POST['fm']['smtp_security'] : ''),
            'mailpref' => (isset($_POST['fm']['mailpref']) && in_array($_POST['fm']['mailpref'], array('smtp','php')) ? $_POST['fm']['mailpref'] : 'smtp'),
            'smtp_insec' => (isset($_POST['fm']['smtp_insec']) && in_array($_POST['fm']['smtp_insec'], array('yes','no')) ? $_POST['fm']['smtp_insec'] : 'no'),
            'addemails' => (isset($_POST['fm']['addemails']) ? str_replace(' ', '', $_POST['fm']['addemails']) : ''),
            'smtp_debug' => (isset($_POST['fm']['smtp_debug']) && in_array($_POST['fm']['smtp_debug'], array('yes','no')) ? $_POST['fm']['smtp_debug'] : 'no'),
            'captcha' => (isset($_POST['fm']['captcha']) && in_array($_POST['fm']['captcha'], array('yes','no')) ? $_POST['fm']['captcha'] : 'no'),
            'api' => (!empty($_POST['api']) ? $_POST['api'] : [])
          );
          if ($fm['smtp_host'] == '' || $fm['smtp_port'] == '0') {
            $arr['txt'][1] = $msw_adm_email17;
          } else {
            $SYS->email($fm);
            $arr['status'] = 'ok';
            $arr['txt'][1] = $msw_adm_email16 . $c_b;
          }
          break;
        case 'mail':
          if ($SETTINGS->smtp_host && $SETTINGS->smtp_port) {
            $s_emails = mswMlr($SETTINGS->email, $SETTINGS->addemails);
            include(GPATH . 'control/mail-init.php');
            $msg = mswTmp(MSW_EM_LANG . LANG_FLDR_ADM . '/mail-test.txt');
            $sbj = str_replace('{website}', $SETTINGS->website, $msw_emails);
            $MAILR->sendMail(array(
              'from_email' => ($SETTINGS->smtp_email ? $SETTINGS->smtp_email : $SETTINGS->email),
              'from_name' => ($SETTINGS->smtp_from ? $SETTINGS->smtp_from : $SETTINGS->website),
              'to_email' => $s_emails[0],
              'to_name' => $SETTINGS->website,
              'subject' => $sbj,
              'replyto' => array(
                'name' => ($SETTINGS->smtp_rfrom ? $SETTINGS->smtp_rfrom : $SETTINGS->website),
                'email' => ($SETTINGS->smtp_remail ? $SETTINGS->smtp_remail : $SETTINGS->email)
              ),
              'template' => $msg,
              'add-emails' => $s_emails[1],
              'language' => $SETTINGS->language
            ));
            $MAILR->smtpClose();
            $arr['status'] = 'ok';
            $arr['txt'][1] = str_replace(',', '<br>', str_replace('{emails}', implode('<br>', $s_emails),$msw_ajax3)) . $c_b;
          } else {
            $arr['txt'][1] = $msw_adm_email17;
          }
          break;
      }
      break;

    // System Status
    case 'offline':
      include(MSW_ADM_LANG . 'offline.php');
      $fm = array(
        'sysstatus' => (isset($_POST['fm']['sysstatus']) && in_array($_POST['fm']['sysstatus'], array('yes','no')) ? $_POST['fm']['sysstatus'] : 'yes'),
        'reason' => (isset($_POST['fm']['reason']) ? $_POST['fm']['reason'] : ''),
        'autoenable' => (isset($_POST['fm']['autoenable']) ? $_POST['fm']['autoenable'] : '')
      );
      $SYS->offline($fm);
      $controlButtons = array(
        '<a href="?p=offline"><i class="fa fa-calendar-times-o fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav14 . '</span></a>'
      );
      include(PATH . 'content/control-btns.php');
      $arr['status'] = 'ok';
      $arr['txt'][1] = $msw_adm_offline6 . $c_b;
      break;

    // Social settings
    case 'social':
    case 'social_link_box':
      include(MSW_ADM_LANG . 'social.php');
      switch($_GET['ajax-ops']) {
        case 'social':
          $SYS->social();
          $controlButtons = array(
            '<a href="?p=social"><i class="fa fa-facebook fa-fw"></i><span class="hidden-xs"> ' . $msw_adm_nav11 . '</span></a>'
          );
          include(PATH . 'content/control-btns.php');
          $arr['status'] = 'ok';
          $arr['txt'][1] = $msw_adm_social16 . $c_b;
          break;
        case 'social_link_box':
          if (isset($_POST['name'],$_POST['link'])) {
            $protocol = 'http';
            if (substr($_POST['link'], 0, 7) == 'http://') {
              $protocol = 'http';
            }
            if (substr($_POST['link'], 0, 8) == 'https://') {
              $protocol = 'https';
            }
            $name = preg_replace('/[^0-9a-zA-Z]/', '', $_POST['name']);
            $link = str_replace(array('http://','https://'),[],$_POST['link']);
            $tmp  = mswTmp(PATH . 'content/html/social-link.htm','yes');
            if ($name && $link && $tmp) {
              $arr['status'] = 'ok';
              $arr['html'] = str_replace(array(
                '{key}','{text}',
                '{protocol}','{val}',
                '{active1}','{active2}'
              ), array(
                mswSH($name),
                $msw_11_admin[8],
                $protocol,
                $link,
                ($protocol == 'http' ? ' class="active"' : ''),
                ($protocol == 'https' ? ' class="active"' : '')
              ), $tmp);
            }
          }
          break;
      }
      break;

    case 'affiliate-search':
      switch($_GET['ajax-ops']) {
        case 'affiliate-search':
          $s = [];
          if (isset($_GET['term'])) {
            include(PATH . 'control/classes/class.affiliates.php');
            $AFF = new affiliates();
            echo $JSON->encode($AFF->search());
            exit;
          }
          break;
      }
      break;

    case 'menu-panel':
      if (in_array($_GET['pnl'], array_keys($mswSysLinks))) {
        $SSN->set(array('adm_menu_panel' => $_GET['pnl']));
        $arr['status'] = 'ok';
      }
      break;

    case 'apikey':
      $arr['status'] = 'ok';
      $key = substr(md5(uniqid(rand(),1)), 3, rand(6,15)) . '-' . substr(md5(uniqid(rand(),1)), 3, rand(4,15)) . '-' . substr(md5(uniqid(rand(),1)), 3, rand(5,18));
      $arr['key'] = strtoupper($key);
      break;

    case 'pass-gen':
      include(PATH . 'control/classes/class.affiliates.php');
      $AFF         = new affiliates();
      $arr['pass'] = $AFF->password();
      break;

    case 'vc':
      $arr['status'] = 'ok';
      $arr['txt'][1] = mswL2BR($SYS->version());
      break;

  }
}

if ($arr['status'] == 'ok') {
  $arr['txt'][0] = $msw_ajax4;
}
echo $JSON->encode($arr);
exit;

?>