<?php
// File: index.php
// ----------------------------------------------------------------------
// Based on:
// PHP-NUKE Web Portal System - http://phpnuke.org/
// Thatware - http://thatware.org/
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Version For PHPNUKE by Rob wolf visit http://www.tremorsfan.com for the latest version.
// Original Author of file: Rebecca Smallwood
// Fictioneer Updrades to file by Theresa Sanchez and Joyce Melton
// ----------------------------------------------------------------------

if (!defined('MODULE_FILE')) {die('You can\'t access this file directly.. . ');}
if (!defined('PHP_EOL')) define('PHP_EOL', strtoupper(substr(PHP_OS,0,3) == 'WIN') ? "\r\n" : "\n"); 
$module_name = basename(dirname(__FILE__));
get_lang($module_name);
$pagetitle = '- Fictioneer';
#$ModName = Fictioneer;
$ModName = $module_name;
$dontcare = -1;
include_once 'modules/' . $module_name . '/config.php';

//Intro page

function FictioneerMain() {
   global $bgcolor1, $bgcolor2, $prefix, $db;
   $result = $db->sql_query('SELECT * FROM `' . $prefix . '_fictioneer_settings`');
   if($settings = $db->sql_fetchrow($result)) {
      FictioneerHeader();
      echo '<br />' , PHP_EOL
         , '<table width="100%">' , PHP_EOL
         , '<tr valign="top">' , PHP_EOL
         , '<td width="60%">' , $settings['welcome'], '</td>' , PHP_EOL
         , '<td width="40%">' , PHP_EOL
         , '<span class="thick">Categories</span>' , PHP_EOL
         , '<br /><br />' , PHP_EOL;
      ListCategories();
      echo '<br />' , PHP_EOL
         , '</td>' , PHP_EOL
         , '</tr>' , PHP_EOL
         , '</table>' , PHP_EOL;
      FictioneerFooter();
   } else {
      echo 'This is an error message.' , PHP_EOL;
   }
}

//Header

function FictioneerHeader() {
	global $bgcolor1, $bgcolor2, $module_name, $sitename, $user, $cookie, $subsoff;
	include_once 'header.php';
	OpenTable();
	if (is_user($user)) {
		$userinfo = getusrinfo($user);
	} else {
		$userinfo = false;
	}
	echo '<br />' , PHP_EOL
	   , '<table width="100%">' , PHP_EOL
	   , '<tr>' , PHP_EOL
	   , '<td colspan="2" class="text-cneter thick">' , $sitename , ' -- ' . _FNNAME . '</td>' , PHP_EOL
	   , '</tr>' , PHP_EOL
	   , '<tr>' , PHP_EOL
	   , '<td>' , PHP_EOL
	   , '<form enctype="multipart/form-data" method="post" action="modules.php?name=' , $module_name , '&amp;file=index&amp;options=SearchResults&amp;pagenumber=1&amp;searchterm=' , $searchterm , '&amp;searchkind=' , $searchkind , '&amp;totalpages=-1">' , PHP_EOL
	   , '<a href="modules.php?name=' , $module_name , '&amp;file=index">Main</a> | ';
	if($subsoff == '1') {
		echo '<a href="modules.php?name=' , $module_name , '&amp;file=index&amp;options=ViewRules">Add Story</a> | '
		   , '<a href=\"modules.php?name=' , $module_name , '&amp;file=index&amp;options=DisplayStories&amp;pagenumber=1&amp;categoryid=-1&amp;totalpages=-1">Recent Stories</a> | ';
	}
	if(($subsoff == '1') && $userinfo != false) {
		echo '<a href="modules.php?name=' , $module_name , '&amp;file=index&amp;options=SearchResults&amp;searchterm=' , $userinfo['username'] , '&amp;pagenumber=1&amp;searchkind=author&amp;totalpages=-1">Your Stories</a> | '
		   , '<a href="modules.php?name=' , $module_name , '&amp;file=index&amp;options=Help">Help</a> | ';
	}

	echo '<select name="searchkind">' , PHP_EOL
	   , '	<option value="author">Author</option>' , PHP_EOL
	   , '	<option value="title">Title</option>' , PHP_EOL
	   , '	<option value="keywords">Keywords</option>' , PHP_EOL
	   , '	<option value="summary">Summary</option>' , PHP_EOL
	   , '</select>' , PHP_EOL
	   , '<input type="text" name="searchterm" size="10" /> ' , PHP_EOL
	   , '<input type="submit" name="search" value="Search" /> ' , PHP_EOL
	   , '</form>' , PHP_EOL
	   , '</td>' , PHP_EOL
	   , '</tr>' , PHP_EOL
	   , '</table>' , PHP_EOL;
   CloseTable();
   OpenTable();

}

//Footer

function FictioneerFooter() {
	global $bgcolor1, $bgcolor2;
	CloseTable();
	echo '<br />' , PHP_EOL
	   , '<div class="text-center">' , PHP_EOL
	   , 'Fictioneer Module 0.5 by <a href="http://www.fictioneer.net">Theresa Sanchez</a>, and <a href="http://bigcloset.ateros.com">Joyce Melton</a><br />' , PHP_EOL
	   , 'Original FanFiction Module by <a href="http://orodruin.sourceforge.net">Rebecca Smallwood</a><br />' , PHP_EOL
	   , 'Version for phpnuke by <a href="http://www.tremorsfan.com">Rob Wolf</a> Dev v0.2' , PHP_EOL
	   , '</div>' , PHP_EOL;
   include_once 'footer.php';
}

//Helpfile for clueless people

function Help() {
	global $bgcolor1, $bgcolor2, $module_name;
	FictioneerHeader();
	include_once 'modules/' . $module_name . '/help.txt';
	FictioneerFooter();
}

//listing of all the top level categories

function ListCategories() {
	global $bgcolor1, $bgcolor2, $prefix, $module_name, $prefix, $db;
	$result = $db->sql_query('SELECT * SELECT `' . $prefix . '_fictioneer_categories` WHERE `parent` = \'-1\'');
	while($catlist = $db->sql_fetchrow($result)) {
		echo '<a href="modules.php?name=' , $module_name , '&amp;file=index&amp;options=DisplayStories&amp;pagenumber=1&amp;categoryid=' , $catlist['categoryid'] . '&amp;totalpages=-1">' , htmlspecialchars($catlist['category'], ENT_QUOTES, _CHARSET) , '</a>' , PHP_EOL
		   , '<div>' , PHP_EOL
		   , $catlist['description'] , PHP_EOL
		   , '</div>' , PHP_EOL
		   , '<br />' , PHP_EOL;
   }
}


//this is here so that the admin can view the story without having the header junk and all that

function AdminViewStory($storyid, $author, $title) {
	global $bgcolor1, $bgcolor2, $prefix, $module_name, $nukeurl, $user, $cookie, $admin, $db;
	$result = $db->sql_query('SELECT * FROM `' . $prefix . '_fictioneer_stories` WHERE `storyid` = \'' . $storyid . '\'');
	$storyresults = $db->sql_fetchrow($result);
	if (is_admin($admin)) {
		echo '<table width="100%">' , PHP_EOL
		   , '<tr>' , PHP_EOL
		   , '<td>' , PHP_EOL
		   , htmlspecialchars($storyresults['title'], ENT_QUOTES, _CHARSET) , ' by <a href="user.php?op=userinfo&uname=' , $storyresults['author'] , '">' , htmlspecialchars($storyresults['author'], ENT_QUOTES, _CHARSET) , '</a>' , PHP_EOL
		   , '</td>' , PHP_EOL
		   , '<td>' , PHP_EOL
		   , '</td>' , PHP_EOL
		   , '<td align="right">' , PHP_EOL;
      $result = $db->sql_query('SELECT * FROM `' . $prefix . '_fictioneer_stories` WHERE `parentstoryid` = \'' . $storyresults['parentstoryid'] . '\' ORDER BY `storyid` ASC');
      echo '<form name="jump">' , PHP_EOL
         , '<select name="jumpmenu" onchange="location.href=jump.jumpmenu.options[selectedIndex].value">' , PHP_EOL
         , '<option>Chapters</option>' , PHP_EOL;
      while ($chapterdisplay = $db->sql_fetchrow($result)) {
         echo '<option value="modules.php?name=' , $module_name , '&amp;file=index&amp;options=DisplayStory&amp;storyid=' , $chapterdisplay['storyid'] , '">' ,  htmlspecialchars($chapterdisplay['title'], ENT_QUOTES, _CHARSET) , '</option>' , PHP_EOL;
      }
      echo '</select>' , PHP_EOL
         , '</form>' , PHP_EOL
         , '</td>' , PHP_EOL
         , '</tr>' , PHP_EOL
         , '</table>' , PHP_EOL;
      //writes out the story, and replaces the carriage returns with <br /> tags

      $file = 'modules/' . $module_name . '/stories/' . $storyresults['author'] . '/' . $storyresults['storyid'] . '.txt';
      $log_file = fopen($file, 'r');
      $file_contents = fread($log_file, filesize($file));
      echo(nl2br(htmlspecialchars($file_contents)));
      fclose($log_file);
      echo '<hr noshade="noshade">' , PHP_EOL
         , '<form method="post" enctype="multipart/form-data" action="modules.php?name=' , $module_name , '&amp;file=index&amp;options=SubmitVauthorStory&amp;storyid=' , $storyresults['storyid'] , '">' , PHP_EOL
         , _FNTITLE , ': <input type="text" name="title" value="' , htmlspecialchars($chapterdisplay['title'], ENT_QUOTES, _CHARSET) , '" /><br /><br />' , PHP_EOL
         , _FNKEYWORDS , ': <textarea name="keywords" cols="40" rows="3">"' , htmlspecialchars($storyresults['keywords'], ENT_QUOTES, _CHARSET) , '</textarea><br /><br />"' , PHP_EOL
         , _FNSUMMARY , ': <textarea name="summary" cols="40" rows="3">"' , htmlspecialchars($storyresults['summary'], ENT_QUOTES, _CHARSET) , '</textarea><br /><br />' , PHP_EOL
         , '<input type="hidden" name="storyid" value="' , $storyresults['storyid'] , '" />' , PHP_EOL
         , '<input type="hidden" name="author" value="' , htmlspecialchars($storyresults['author'], ENT_QUOTES, _CHARSET) , '" />' , PHP_EOL
         , '<input type="hidden" name="categoryid" value="' , $storyresults['categoryid'] , '" />' , PHP_EOL
         , '<input type="hidden" name="parentstoryid" value="' , $storyresults['parentstoryid'] , '" />' , PHP_EOL
         , '<input type="hidden" name="ratingid" value="' , $storyresults['ratingid'] , '" />' , PHP_EOL
         , '<input type="hidden" name="char1" value="' , htmlspecialchars($storyresults['char1'], ENT_QUOTES, _CHARSET) , '" />' , PHP_EOL
         , '<input type="hidden" name="char2" value="' , htmlspecialchars($storyresults['char2'], ENT_QUOTES, _CHARSET) , '" />' , PHP_EOL
         , '<input type="hidden" name="genre" value="' , htmlspecialchars($storyresults['genre'], ENT_QUOTES, _CHARSET) , '" />' , PHP_EOL
         , '<input type="hidden" name="roundrobin" value="' , $storyresults['roundrobin'] , '" />' , PHP_EOL
         , _FNTEXT , ': <textarea wrap="virtual" name="story" cols="40" rows="6">';
      include_once 'modules/' . $module_name . '/stories/' . $storyresults['author'] . '/' . $storyresults['storyid'] . '.txt';
      echo '</textarea>' , PHP_EOL
         , '<br /><br />' , PHP_EOL
         , '<input type="submit" name="buttonaction" value="Update Story" />' , PHP_EOL;
	}
}

//notes for public
//add previous and next 2 pages and have it show what page you're on
//if no results when sorting, have it say so.
//if no results on search, say so
//if no stories by author, say so

//notes for admin
//fix weird e-mail return address
//figure out fatal head error


switch($options) {
   case 'Help':
   Help();
   break;

   case 'ViewRules':
   include_once 'modules/' . $module_name . '/fn-submitstory.php';
   ViewRules();
   break;

   case 'ChooseCategory':
   include_once 'modules/' . $module_name . '/fn-submitstory.php';
   ChooseCategory();
   break;

   case 'SearchResults':
   include_once 'modules/' . $module_name . '/fn-displaystory.php';
   SearchResults($searchterm, $searchkind, $pagenumber, $totalpages);
   break;

   case 'SortResults':
   include_once 'modules/' . $module_name . '/fn-displaystory.php';
   SortResults($categoryid, $ratingid, $char1, $char2, $genre, $pagenumber, $totalpages);
   break;

   case 'SubmitVauthorStory':
   include_once 'modules/' . $module_name . '/fn-submitstory.php';
   SubmitVauthorStory($categoryid, $parentstoryid, $title, $keywords, $summary, $story, $ratingid, $char1, $char2, $genre, $author, $email, $roundrobin, $buttonaction, $imgfile, $imgfile_name, $storyid, $unvalidated);
   break;

   case 'SubmitUnvalidAuthorStory':
   include_once ("modules/' . $module_name . '/fn-submitstory.php");
   SubmitUnvalidAuthorStory($categoryid, $parentstoryid, $title, $keywords, $summary, $story, $ratingid, $char1, $char2, $genre, $author, $roundrobin, $buttonaction, $imgfile, $imgfile_name, $storyid, $unvalidated);
   break;

   case 'SubmitStory':
   include_once 'modules/' . $module_name . '/fn-submitstory.php';
   SubmitStory($author, $user, $categoryid, $parentstoryid, $roundrobin);
   break;

   case 'EditStory':
   include_once 'modules/' . $module_name . '/fn-submitstory.php';
   EditStory($storyid, $categoryid);
   break;

   case 'DeleteStory':
   include_once 'modules/' . $module_name . '/fn-submitstory.php';
   DeleteStory($deleteall, $storyid, $author, $submit, $transmit);
   break;

   case 'DisplayStories':
   include_once 'modules/' . $module_name . '/fn-displaystory.php';
   DisplayStories($summary, $pagenumber, $categoryid, $totalpages);
   break;

   case 'DisplayReviews':
   include_once 'modules/' . $module_name . '/fn-reviews.php';
   DisplayReviews($parentstoryid);
   break;

   case 'DeleteReviews':
   include_once 'modules/' . $module_name . '/fn-reviews.php';
   DeleteReviews($reviewid, $submit);
   break;

   case 'SubmitReview':
   include_once 'modules/' . $module_name . '/fn-reviews.php';
   SubmitReview($parentstoryid, $submit, $comment, $reviewer, $vote);
   break;

   case 'DisplayStory':
   include_once 'modules/' . $module_name . '/fn-displaystory.php';
   DisplayStory($storyid, $author, $title);
   break;

   case 'AdminViewStory':
   //include_once 'modules/' . $module_name . '/fn-submitstory.php';
   AdminViewStory($storyid, $author, $title);
   break;

   case 'SendAcceptance':
   include_once 'modules/' . $module_name . '/fn-letters.php';
   SendAcceptance($submit, $author, $email, $subject, $yesletter);
   break;

   case 'SendNoThankYou':
   include_once 'modules/' . $module_name . '/fn-letters.php';
   SendNoThankYou($submit, $author, $email, $subject, $noletter);
   break;

   default:
   FictioneerMain();
   break;
}