<?php

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

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

// Set session var for CleanTalk..
$_SESSION[mswEnc(SECRET_KEY) . '_stime'] = time();

$arr = array('status' => 'err', 'txt' => array($msw_ajax, $msw_ajax2));
$reloadBtn = '<hr><a href="#" onclick="window.location.reload();return false"><i class="fa fa-refresh fa-fw"></i><span class="hidden-xs"> ' . (isset($msw_common20) ? $msw_common20 : '') . '</span></a>';

// 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-ops'], $_SERVER['HTTP_X_CSRF_TOKEN']) && $_SERVER['HTTP_X_CSRF_TOKEN'] == $SSN->get('csrf_token')) {
  switch($_GET['ajax-ops']) {
    case 'profile':
      if (isset($loggedInUser['id'])) {
        include(LANG_FLDR . 'profile.php');
        $fm = array(
          'first_name' => (isset($_POST['fm']['fn']) ? $_POST['fm']['fn'] : ''),
          'last_name' => (isset($_POST['fm']['ln']) ? $_POST['fm']['ln'] : ''),
          'email' => (isset($_POST['fm']['em']) && mswEmVal($_POST['fm']['em']) == 'yes' ? $_POST['fm']['em'] : ''),
          'curr_pass' => (isset($_POST['fm']['cp']) ? $_POST['fm']['cp'] : ''),
          'pass' => (isset($_POST['fm']['pw']) ? $_POST['fm']['pw'] : ''),
          'pass2' => (isset($_POST['fm']['pw2']) ? $_POST['fm']['pw2'] : ''),
          'pay_info' => (isset($_POST['fm']['pay']) ? $_POST['fm']['pay'] : '')
        );
        if ($fm['first_name'] == '' || $fm['last_name'] == '' || $fm['email'] == '' || $fm['pay_info'] == '') {
          $arr['txt'][1] = $msw_profile_err;
        } else {
          $accnt = $ACC->account(array(
            '`email` = \'' . $DB->db_escape($fm['email']) . '\'',
            '`id` != \'' . (int) $loggedInUser['id'] . '\''
          ));
          if (isset($accnt->id)) {
            $arr['txt'][1] = $msw_profile_err4;
          } else {
            if ($fm['curr_pass'] && ($fm['pass'] == '' || $fm['pass2'] == '') || ($fm['curr_pass'] == '' && $loggedInUser['sys3'] == 'passchange')) {
              $arr['txt'][1] = ($fm['curr_pass'] == '' && $loggedInUser['sys3'] == 'passchange' ? $msw_profile_err7 : $msw_profile_err2);
            } else {
              if ($fm['curr_pass'] && mswPassHash(array('type' => 'add', 'pass' => $fm['curr_pass'])) != mswPassHash(array('type' => 'calc', 'val' => $fm['curr_pass'], 'hash' => $loggedInUser['pass']))) {
                $arr['txt'][1] = $msw_profile_err5;
              } else {
                if ($fm['pass'] && ($fm['curr_pass'] == $fm['pass'])) {
                  $arr['txt'][1] = $msw_profile_err6;
                } else {
                  if ($fm['pass'] && $fm['pass'] != $fm['pass2']) {
                    $arr['txt'][1] = $msw_profile_err3;
                  } else {
                    $update = $ACC->update(array(
                      '`fname` = \'' . $DB->db_escape($fm['first_name']) . '\'',
                      '`lname` = \'' . $DB->db_escape($fm['last_name']) . '\'',
                      '`email` = \'' . $DB->db_escape($fm['email']) . '\'',
                      '`payinfo` = \'' . $DB->db_escape($fm['pay_info']) . '\'',
                      '`pass` = \'' . ($fm['pass'] ? mswPassHash(array('type' => 'add', 'pass' => $fm['pass'])) : $loggedInUser['pass']) . '\'',
                      '`sys3` = \'\''
                    ), '`id` = \'' . (int) $loggedInUser['id'] . '\'');
                    $arr['status'] = 'ok';
                    $arr['txt'][1] = str_replace('{name}', mswSH($loggedInUser['fname']), $msw_profile[10]) . $reloadBtn;
                  }
                }
              }
            }
          }
        }
      }
      break;
    case 'create':
      include(LANG_FLDR . 'create.php');
      $ok_captcha = 'yes';
      $fm = array(
        'first_name' => (isset($_POST['fm']['fn']) ? $_POST['fm']['fn'] : ''),
        'last_name' => (isset($_POST['fm']['ln']) ? $_POST['fm']['ln'] : ''),
        'email' => (isset($_POST['fm']['em']) && mswEmVal($_POST['fm']['em']) == 'yes' ? $_POST['fm']['em'] : ''),
        'ct_ts' => (isset($_POST['fm']['ct_ts']) ? $_POST['fm']['ct_ts'] : 'fail'),
        'privacy' => (isset($_POST['fm']['privacy']) ? 'yes' : 'no'),
        'captcha' => (isset($_POST['fm']['cpt']) ? $_POST['fm']['cpt'] : '')
      );
      // Use basic captcha?
      if ($SETTINGS->captcha == 'yes') {
        $CPHA->c_var = 'cpt_create';
        $arr['cp_area'] = $CPHA->c_var;
        if ($fm['captcha'] == '' || $fm['captcha'] != $CPHA->check()) {
          $arr['txt'][1] = $msw_public_11_errs[0];
          if (EN_RELOAD_CAPTCHA) {
            $arr['rcap'] = 'yes';
          }
          $ok_captcha = 'no';
        }
      }
      if ($ok_captcha == 'yes') {
        if ($fm['first_name'] == '' || $fm['last_name'] == '' || $fm['email'] == '') {
          $arr['txt'][1] = $msw_create_err;
        } elseif (in_array('log', $pr_areas) && $fm['privacy'] == 'no') {
          $arr['txt'][1] = $msw_public_11_errs[1];
        } else {
          $accnt = $ACC->account(array(
            '`email` = \'' . $DB->db_escape($fm['email']) . '\''
          ));
          if (isset($accnt->id)) {
            $arr['txt'][1] = $msw_create_err2;
          } else {
            // Is cleanTalk enabled?
            $api = $SCL->params('ctalk');
            $ok_to_process = 'yes';
            if (isset($api['ctalk']['enable'],$api['ctalk']['key']) && $api['ctalk']['enable'] == 'yes' && $api['ctalk']['key']) {
              include(PATH . 'control/classes/system/class.cleantalk.php');
              $CTK           = new cleanTalk();
              $CTK->settings = $SETTINGS;
              $CTK->social   = $api;
              // Check message submission..
              $ctk_user = $CTK->check(array(
                'email' => $fm['email'],
                'name' => $fm['first_name'] . ' ' . $fm['last_name'],
                'ct_ts' => $fm['ct_ts']
              ));
              if (!isset($ctk_user['allow']) || $ctk_user['allow'] == 0) {
                $ok_to_process = 'no';
                $cleanTalkBlock = 'yes';
              }
            }
            if ($ok_to_process == 'yes') {
              // Create pass, add account and email..
              $npass = $ACC->pass();
              $code  = $ACC->pass('no', 20);
              $ACC->add($fm, $npass, $code);
              include(PATH . 'control/mail-init.php');
              $MAILR->addTag('{NAME}', $fm['first_name'] . ' ' . $fm['last_name']);
              $MAILR->addTag('{FNAME}', $fm['first_name']);
              $MAILR->addTag('{LNAME}', $fm['last_name']);
              $MAILR->addTag('{EMAIL}', $fm['email']);
              $MAILR->addTag('{PASS}', $npass);
              $MAILR->addTag('{CODE}', $code);
              // Send based on approval option..
              switch($SETTINGS->approveaff) {
                case 'yes':
                  $msg = mswTmp(MSW_EM_LANG . 'affiliate-approval.txt', 'yes');
                  $sbj = str_replace('{website}', $SETTINGS->website, $msw_emails5);
                  $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' => $SETTINGS->email,
                    '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,
                    'language' => $SETTINGS->language
                  ));
                  $MAILR->smtpClose();
                  $arr['status'] = 'ok';
                  $arr['txt'][1] = str_replace(array('{name}','{email}'), array(mswSH($fm['first_name']),mswSH($fm['email'])), $msw_create[5]);
                  break;
                default:
                  $msg = mswTmp(MSW_EM_LANG . 'affiliate-new-account.txt', 'yes');
                  $sbj = str_replace('{website}', $SETTINGS->website, $msw_emails4);
                  $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' => $fm['email'],
                    'to_name' => $fm['first_name'] . ' ' . $fm['last_name'],
                    '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();
                  $arr['status'] = 'ok';
                  $arr['txt'][1] = str_replace(array('{name}','{email}'), array(mswSH($fm['first_name']),mswSH($fm['email'])), $msw_create[4]);
                  break;
              }
            } else {
              $arr['txt'][1] = $msw_create_err4;
            }
          }
        }
      }
      break;
    case 'login':
    case 'forgot-load':
    case 'pass-rem':
      include(LANG_FLDR . 'login.php');
      switch($_GET['ajax-ops']) {
        case 'login':
          $ok_captcha = 'yes';
          $fm = array(
            'email' => (isset($_POST['fm']['em']) && mswEmVal($_POST['fm']['em']) == 'yes' ? $_POST['fm']['em'] : ''),
            'pass' => (isset($_POST['fm']['pw']) ? $_POST['fm']['pw'] : ''),
            'captcha' => (isset($_POST['fm']['cpt']) ? $_POST['fm']['cpt'] : '')
          );
          // Use basic captcha?
          if ($SETTINGS->captcha == 'yes') {
            $CPHA->c_var = 'cpt_login';
            $arr['cp_area'] = $CPHA->c_var;
            if ($fm['captcha'] == '' || $fm['captcha'] != $CPHA->check()) {
              $arr['txt'][1] = $msw_public_11_errs[0];
              if (EN_RELOAD_CAPTCHA) {
                $arr['rcap'] = 'yes';
              }
              $ok_captcha = 'no';
            }
          }
          if ($ok_captcha == 'yes') {
            if ($fm['email'] == '' || $fm['pass'] == '') {
              $arr['txt'][1] = $msw_login_err;
            } else {
              $accnt = $ACC->account(array(
                '`email` = \'' . $DB->db_escape($fm['email']) . '\'',
                '`en` = \'yes\'',
                '`approved` = \'yes\''
              ));
              if (isset($accnt->id) && mswPassHash(array('type' => 'calc', 'val' => $fm['pass'], 'hash' => $accnt->pass))) {
                $SSN->set(array('aff-' . mswEnc($SETTINGS->email . SECRET_KEY) . '-key' => $accnt->email . ',' . $accnt->id));
                $arr['status'] = 'rdr';
                $arr['loc'] = $SETTINGS->ifolder . '?p=home';
              } else {
                $arr['txt'][1] = $msw_login_err3;
              }
            }
          }
          break;
        case 'pass-rem':
          $ok_captcha = 'yes';
          $fm = array(
            'email' => (isset($_POST['fm']['em']) && mswEmVal($_POST['fm']['em']) == 'yes' ? $_POST['fm']['em'] : ''),
            'captcha' => (isset($_POST['fm']['cpt']) ? $_POST['fm']['cpt'] : '')
          );
          // Use basic captcha?
          if ($SETTINGS->captcha == 'yes') {
            $CPHA->c_var = 'cpt_login';
            $arr['cp_area'] = $CPHA->c_var;
            if ($fm['captcha'] == '' || $fm['captcha'] != $CPHA->check()) {
              $arr['txt'][1] = $msw_public_11_errs[0];
              if (EN_RELOAD_CAPTCHA) {
                $arr['rcap'] = 'yes';
              }
              $ok_captcha = 'no';
            }
          }
          if ($ok_captcha == 'yes') {
            if ($fm['email'] == '') {
              $arr['txt'][1] = $msw_login_err2;
            } else {
              $accnt = $ACC->account(array(
                '`email` = \'' . $DB->db_escape($fm['email']) . '\'',
                '`en` = \'yes\'',
                '`approved` = \'yes\''
              ));
              // If account found, reset password
              if (isset($accnt->id)) {
                $npass = $ACC->pass();
                $update = $ACC->update(array(
                  '`pass` = \'' . mswPassHash(array('type' => 'add', 'pass' => $npass)) . '\'',
                  '`sys3` = \'passchange\''
                ), '`id` = \'' . $accnt->id . '\'');
                // We`ll send email if something got updated..
                if ($update > 0) {
                  include(PATH . 'control/mail-init.php');
                  $msg = mswTmp(MSW_EM_LANG . 'affiliate-pass-reset.txt', 'yes');
                  $sbj = str_replace('{website}', $SETTINGS->website, $msw_emails3);
                  $MAILR->addTag('{NAME}', $accnt->fname . ' ' . $accnt->lname);
                  $MAILR->addTag('{FNAME}', $accnt->fname);
                  $MAILR->addTag('{LNAME}', $accnt->lname);
                  $MAILR->addTag('{EMAIL}', $accnt->email);
                  $MAILR->addTag('{PASS}', $npass);
                  $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' => $accnt->email,
                    'to_name' => $accnt->fname . ' ' . $accnt->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();
                }
              }
              $arr['status'] = 'ok';
              $arr['txt'][1] = str_replace('{email}', mswSH($fm['email']), $msw_login[6]) . $reloadBtn;
            }
          }
          break;
        case 'forgot-load':
          $arr['lang'] = array($msw_common6, $msw_login[5]);
          break;
      }
      break;
    case 'dl':
      if (isset($loggedInUser['id'])) {
        include(LANG_FLDR . 'products.php');
        if (isset($_GET['id'])) {
          $PRD = $SYS->load(array(
            'section' => 'product',
            'id' => (int) $_GET['id']
          ));
          if (isset($PRD->id)) {
            // Direct link?
            if ($PRD->link && (substr(strtolower($PRD->link), 0, 5) == 'http:' || substr(strtolower($PRD->link), 0, 6) == 'https:')) {
              $arr['status'] = 'rdr';
              $arr['loc'] = $PRD->link;
            } else {
              $PROMO = $SYS->load(array(
                'section' => 'promo',
                'product' => (int) $_GET['id']
              ));
              if (isset($PROMO->id) && $PROMO->file_path) {
                if (file_exists(PATH . 'content/promo/' . $PROMO->file_path)) {
                  $arr['status'] = 'rdr';
                  $arr['loc'] = $SETTINGS->ifolder . 'content/promo/' . $PROMO->file_path;
                } else {
                  $arr['txt'][1] = $msw_products[10];
                }
              } else {
                $arr['txt'][1] = $msw_products[9];
              }
            }
          }
        }
      }
      break;
    case 'export-csv':
    case 'export-csv-dl':
      if (isset($loggedInUser['id'])) {
        include(PATH . 'control/classes/system/class.download.php');
        $DL = new download();
        switch($_GET['ajax-ops']) {
          case 'export-csv':
            if (isset($_GET['qs'])) {
              switch(substr($_GET['qs'], 2)) {
                case 'commissions':
                  include(LANG_FLDR . 'commissions.php');
                  $data = $HTML->commissions(array(
                    'id' => $loggedInUser['id'],
                    'csv' => 'yes',
                    'lang' => array($msw_common, $msw_common2)
                  ));
                  if ($data) {
                    $file = SYS_TEMP_DIR . time() . '-' . $loggedInUser['id'] . '-' . (substr(strtolower($msw_csv_file),-4) == '.csv' ? $msw_csv_file : $msw_csv_file . '.csv');
                    mswPUT($file, $msw_csv_header . mswNL() . $data);
                    $arr['status'] = 'rdr';
                    $arr['loc'] = $SETTINGS->ifolder . '?ajax-ops=export-csv-dl&csrf=' . $SSN->get('csrf_token') . '&f=' . basename($file);
                  } else {
                    $arr['txt'][1] = $msw_commissions[16];
                  }
                  break;
                case 'payments':
                  include(LANG_FLDR . 'payments.php');
                  $data = $HTML->payments(array(
                    'id' => $loggedInUser['id'],
                    'csv' => 'yes',
                    'lang' => array($msw_common, $msw_common2)
                  ));
                  if ($data) {
                    $file = SYS_TEMP_DIR . time() . '-' . $loggedInUser['id'] . '-' . (substr(strtolower($msw_csv_file),-4) == '.csv' ? $msw_csv_file : $msw_csv_file . '.csv');
                    mswPUT($file, $msw_csv_header . mswNL() . $data);
                    $arr['status'] = 'rdr';
                    $arr['loc'] = $SETTINGS->ifolder . '?ajax-ops=export-csv-dl&csrf=' . $SSN->get('csrf_token') . '&f=' . basename($file);
                  } else {
                    $arr['txt'][1] = $msw_payments[9];
                  }
                  break;
              }
            }
            break;
          case 'export-csv-dl':
            if (isset($_GET['f']) && $_GET['f'] && file_exists(SYS_TEMP_DIR . $_GET['f'])) {
              $DL->dl(
                SYS_TEMP_DIR . $_GET['f'],
                'text/csv',
                'yes'
              );
            }
            break;
        }
      }
      break;
    case 'close':
      if (isset($loggedInUser['id']) && $SETTINGS->affdel == 'yes') {
        // Email notification
        if ($SETTINGS->delem == 'yes') {
          $accnt = $ACC->account(array(
            '`id` = \'' . $loggedInUser['id'] . '\''
          ));
          if (isset($accnt->id)) {
            include(PATH . 'control/mail-init.php');
            $MAILR->addTag('{NAME}', $accnt->fname . ' ' . $accnt->lname);
            $MAILR->addTag('{EMAIL}', $accnt->email);
            $msg = mswTmp(MSW_EM_LANG . 'account-deleted.txt', 'yes');
            $sbj = str_replace('{website}', $SETTINGS->website, $msw_emails12);
            $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' => $SETTINGS->email,
              '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,
              'language' => $SETTINGS->language
            ));
            $MAILR->smtpClose();
          }
        }
        // Remove
        $ACC->close((int) $loggedInUser['id']);
        $arr['status'] = 'rdr';
        $arr['loc'] = $SETTINGS->ifolder . '?acc_del_confirm=yes';
      }
      break;
    case 'menu-state':
      if (isset($_GET['state'])) {
        $SSN->set(array('panel-state' => (int) $_GET['state']));
      }
      break;
  }
}

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

?>