<?php

/*---------------------------------------------
  MAIAN EVENTS v2.0
  Written by David Ian Bennett
  E-Mail: support@maianscriptworld.co.uk
  Website: www.maianscriptworld.co.uk
  This File: Administration Parsing File
----------------------------------------------*/

session_start();

// Set error reporting level..E_ALL for development..
error_reporting (0);
set_magic_quotes_runtime(0);

// Set paths..
define ('FOLDER_PATH', dirname(__FILE__).'/');
define ('RELATIVE_PATH', '../');
define ('INCLUDE_FILES', 1);
define ('PARENT',1);

// Database connection..
include(RELATIVE_PATH.'inc/db_connection.inc.php');

$SETTINGS = mysql_fetch_object(mysql_query("SELECT * FROM ".$database['prefix']."settings LIMIT 1")) or die(mysql_error());

include(RELATIVE_PATH.'lang/'.$SETTINGS->language);
include(FOLDER_PATH.'inc/defined.inc.php');
include(FOLDER_PATH.'inc/functions.php');
include(RELATIVE_PATH.'inc/functions.php');
include(RELATIVE_PATH.'classes/class_generic.inc.php');
include(FOLDER_PATH.'classes/PaginateIt.php');
include(FOLDER_PATH.'classes/class_mail.inc.php');
include(FOLDER_PATH.'classes/class_events.inc.php');
include(FOLDER_PATH.'classes/class_comments.inc.php');

// Assign vars..
$cmd           = (isset($_GET['cmd']) ? strip_tags($_GET['cmd']) : 'home');
$page          = (isset($_GET['page']) ? strip_tags($_GET['page']) : '1');
$count         = 0;
$limit         = PER_PAGE;
$limitvalue    = $page * $limit - ($limit);
$error_string  = array();
$pageTitle     = '';

// Create objects of class files..
$MEV_EVENT             = new calEvent();
$MEV_MAIL              = new mailClass();
$MEV_COMMENTS          = new comments();
$MEV_EVENT->prefix     = $database['prefix'];
$MEV_COMMENTS->prefix  = $database['prefix'];
$MEV_MAIL->smtp        = $SETTINGS->smtp;
$MEV_MAIL->smtp_host   = $SETTINGS->smtp_host;
$MEV_MAIL->smtp_user   = $SETTINGS->smtp_user;
$MEV_MAIL->smtp_pass   = $SETTINGS->smtp_pass;
$MEV_MAIL->smtp_port   = $SETTINGS->smtp_port;
$MEV_MAIL->addTag('{WEBSITE_NAME}',$SETTINGS->website);
$MEV_MAIL->addTag('{WEBSITE_URL}',$SETTINGS->folder);
$MEV_MAIL->addTag('{WEBSITE_EMAIL}',$SETTINGS->email);

//-------------------------------
//Switch Statement
//Displays data based on $cmd
//-------------------------------

switch ($cmd)
{
  // Home..
  case 'home':
  
  // Check login..
  isWebmasterLoggedIn();
  
  $pageTitle = $header3;

  // Check to see if the install folder is present in installation directory
  if (is_dir(RELATIVE_PATH.'install/')) {
    $INSTALL_FILE = true;
  }

  include(FOLDER_PATH.'inc/header.php');
  include(FOLDER_PATH.'data_files/home.php');
  include(FOLDER_PATH.'inc/footer.php');
  break;
  
  // Add New Event
  case 'add':
  
  // Check login..
  isWebmasterLoggedIn();
  
  // Add event..
  if (isset($_POST['process'])) {
    // Use callback mechanism to trim post array vars..
    $_POST = array_map('trim',$_POST);
    
    if ($_POST['title'] && $_POST['fromDate'] && $_POST['toDate'] && $_POST['comments']) {
      $MEV_EVENT->add($_POST);
      $OK = true;
      unset($_POST);
    } else {
      $count = 1;
    }
  }
  
  $pageTitle = $header6;
  
  include(FOLDER_PATH.'inc/header.php');
  include(FOLDER_PATH.'data_files/add.php');
  include(FOLDER_PATH.'inc/footer.php');
  break;
    
  // Edit Events
  case 'edit':
  
  // Check login..
  isWebmasterLoggedIn();
  
  // Edit event..
  if (isset($_POST['process'])) {
    // Use callback mechanism to trim post array vars..
    $_POST = array_map('trim',$_POST);
    
    if ($_POST['title'] && $_POST['fromDate'] && $_POST['toDate'] && $_POST['comments']) {
      $MEV_EVENT->update($_POST);
      header("Location: index.php?cmd=edit&id=".$_POST['id']."&ok=1");
      exit;
    } else {
      $count = 1;
    }
  }
  
  $pageTitle = $edit;
  
  include(FOLDER_PATH.'inc/header.php');
  include(FOLDER_PATH.'data_files/edit.php');
  include(FOLDER_PATH.'inc/footer.php');
  break;
    
  // Program Settings
  case 'settings':
  
  // Check login..
  isWebmasterLoggedIn();
  
  // Update settings..
  if (isset($_POST['process'])) {
    // Use callback mechanism to trim post array vars..
    $_POST = array_map('trim',$_POST);
  
    include(FOLDER_PATH.'classes/class_settings.inc.php');
   
    $MEV_SETTINGS          = new settings();
    $MEV_SETTINGS->prefix  = $database['prefix'];
    $MEV_SETTINGS->update($_POST);
    header("Location: index.php?cmd=settings&ok=1");
    exit;
  }
  
  $pageTitle = $header4;
  
  include(FOLDER_PATH.'inc/header.php');
  include(FOLDER_PATH.'data_files/settings.php');
  include(FOLDER_PATH.'inc/footer.php');
  break;
    
  // Manage Events..
  case 'manage':
  
  // Check login..
  isWebmasterLoggedIn();
  
  // Load comments..
  if (isset($_GET['comments'])) {
    include(FOLDER_PATH.'data_files/popup/view_comments.php');
    exit;
  }
    
  // Delete Multiple Events
  if (isset($_POST['process'])) {
    if (!empty($_POST['id'])) {
      $MEV_EVENT->clear($_POST);
      $OK = true;
    } else {
      header("Location: index.php?cmd=manage");
      exit;
    }
  }

  $pageTitle = $header7;

  include(FOLDER_PATH.'inc/header.php');
  include(FOLDER_PATH.'data_files/manage.php');
  include(FOLDER_PATH.'inc/footer.php');
  break;
  
  // Approve comments..
  case 'comments':
  
  // Check login..
  isWebmasterLoggedIn();
  
  // Delete comment..
  if (isset($_GET['del'])) {
    $MEV_COMMENTS->delete($_GET['del']);
    header("Location: index.php?cmd=manage&comments=".$_GET['comments']."");
    exit;
  }
  
  // View/edit comments..
  if (isset($_GET['comment']) || isset($_POST['process'])) {
    // Update..
    if (isset($_POST['process'])) {
      $MEV_COMMENTS->update($_POST);
      if ($_POST['return']=='comments') {
        header("Location: index.php?cmd=comments&comment=".$_POST['id']."");
      } else {
        header("Location: index.php?cmd=manage&comments=".$_POST['eid']."");
      }
      exit;
    }
    include(FOLDER_PATH.'data_files/popup/edit_comment.php');
    exit;
  }
  
  // Process comments..
  if (isset($_POST['process2'])) {
    if (!empty($_POST['id'])) {
      for ($i=0; $i<count($_POST['id']); $i++) {
        $split  = explode("##", $_POST['id'][$i]);
        $EVENT  = getEvent($split[1]);
        $COM    = getComment($split[0]);
        
        // Process..approve/reject..
        if (isset($_POST['approve'])) {
          $MEV_EVENT->comment($split[0]);
        }
        if (isset($_POST['reject'])) {
          $MEV_EVENT->comment($split[0],true);
        }
                
        // Send mail..
        if (isset($_POST['mail'])) {
          
          $MEV_MAIL->addTag('{NAME}',$COM->name);
          $MEV_MAIL->addTag('{COMMENTS}',$COM->comments);
          $MEV_MAIL->addTag('{EVENT}',$EVENT->title);
          $MEV_MAIL->addTag('{EVENT_URL}',$SETTINGS->folder.'/'.($SETTINGS->modr ? 'event/'.$EVENT->id.'/'.cleanEventName(cleanData($EVENT->title)).'/index.html' : 'index.php?cmd=event&amp;event='.$EVENT->id));
          
          $MEV_MAIL->sendMail($COM->name,
                              $COM->email,
                              $SETTINGS->website,
                              $SETTINGS->email,
                              '['.$SETTINGS->website.'] '.(isset($_POST['approve']) ? $comments15 : $comments16),
                              $MEV_MAIL->template(RELATIVE_PATH.'themes/email/comments_'.(isset($_POST['approve']) ? 'approved' : 'rejected').'.txt')
                              );
        }
      }
    }
    
    header("Location: index.php?cmd=comments");
    exit;
  }
  
  $pageTitle = $header8;
  
  include(FOLDER_PATH.'inc/header.php');
  include(FOLDER_PATH.'data_files/comments.php');
  include(FOLDER_PATH.'inc/footer.php');
  break;
  
  // Search events..
  case 'search':
  
  // Check login..
  isWebmasterLoggedIn();
  
  $SQL = '';
  
  // Perform search query..
  if (isset($_GET['search'])) {
    // Use callback mechanism to trim get array vars..
    $_GET = array_map('trim',$_GET);
    
    if (($_GET['keywords']) || ($_GET['fromDate'] && $_GET['toDate'])) {
      // Explode keywords..
      if ($_GET['keywords']) {
        $words = explode(" ", $_GET['keywords']);
        // Loop through keywords..
        for ($i=0; $i<count($words); $i++) {
          $SQL .= ($i ? "OR title LIKE '%".mysql_real_escape_string($words[$i])."%' OR comments LIKE '%".mysql_real_escape_string($words[$i])."%' " : "WHERE title LIKE '%".mysql_real_escape_string($words[$i])."%' OR comments LIKE '%".mysql_real_escape_string($words[$i])."%' ");
        }
      }
      // Are dates set..
      if (($_GET['fromDate'] && checkCalendarDate($_GET['fromDate'])) && ($_GET['toDate'] && checkCalendarDate($_GET['toDate']))) {
        $SQL .= ($SQL ? "AND " : "WHERE ")."fromDate >= '{$_GET['fromDate']}' AND toDate <= '{$_GET['toDate']}'";
      }
      
      $q_search = mysql_query("SELECT *,DATE_FORMAT(fromDate,'".$SETTINGS->formatdate."') AS f_date,
                               DATE_FORMAT(toDate,'".$SETTINGS->formatdate."') AS t_date  
                               FROM ".$database['prefix']."events
                               $SQL
                               ORDER BY id DESC
                               LIMIT $limitvalue,$limit
                               ") or die(mysql_error());
                               
      if (mysql_num_rows($q_search)>0 && $SQL) {
        $SEARCH_RESULTS = true;
      } else {
        $NO_RESULTS = true;
      }                         
    } else {
      header("Location: index.php?cmd=search");
      exit;
    }
  }
  
  $pageTitle = $header9;
  
  include(FOLDER_PATH.'inc/header.php');
  include(FOLDER_PATH.'data_files/search.php');
  include(FOLDER_PATH.'inc/footer.php');
  break;

  // Login..
  case 'login':
  
  // If already logged in, direct to admin homepage..
  if (isset($_SESSION['mevents_admin_session']) || isset($_COOKIE['mevents_admin_cookie'])) {
    header("Location: index.php");
    exit;
  }
    
  // Process login..  
  if (isset($_POST['process'])) {
    // Use callback mechanism to trim post array vars..
    $_POST = array_map('trim',$_POST);
  
    if ($_POST['username'] && $_POST['password']) {
      include(FOLDER_PATH.'inc/password.inc.php');
      
      if (encrypt($admin_username)!=encrypt($_POST['username'])) {
        $N_ERROR = $login6;
        $count++;
      }
      if (encrypt($admin_password)!=encrypt($_POST['password'])) {
        $P_ERROR = $login7;
        $count++;
      }
      if ($count==0) {
        // Set session var..
        $_SESSION['mevents_admin_session'] = encrypt($_POST['username']);

        // Set cookie if requested..
        if (isset($_POST['cookie'])) {
          setcookie($database['cookieName'], encrypt($database['cookieKey']), time()+60*60*24*30);
        }

        header("Location: index.php");
        exit;
      }
    } else {
      header("Location: index.php?cmd=login");
      exit;
    }
  }
    
  $pageTitle = $login4;
  
  include(FOLDER_PATH.'inc/header.php');
  include(FOLDER_PATH.'data_files/login.php');
  include(FOLDER_PATH.'inc/footer.php');
  break;
    
  // Logout..
  case 'logout':
    
  session_unset();
  session_destroy();
    
  if (isset($_COOKIE[$database['cookieName']])) {
    setcookie($database['cookieName'], "");
  }
    
  unset ($_SESSION,$_COOKIE);
    
  header("Location: index.php?cmd=login");

  break;
}

?>
