<?php
ini_set("display_errors", "on");
error_reporting(E_ALL ^ E_NOTICE);
date_default_timezone_set('America/New_York');

$curFile = $_GET['curFile'];
$action = $_GET['action'];
$questNum = $_GET['questNum'];
$ind = $_GET['ind'];
$print = $_GET['print'];
$newIndList = array();
$imTarg = 'Image-O-Matic2.php';
$renameIn = array(':', '#');
$renameOut = array('_', '');
$pageRenameIn = array('#', '&');
$pageRenameOut = array('', 'and');

function fileWrite($x){
     $x .= "\r\n";
     global $file;
     global $nameList;
     global $curFile;
     if (!$file) {
         $file = fopen($nameList[$curFile].'Parse.txt', 'w+');
     }
     if (!fwrite($file, $x)){
         print "&errMsg=Unable to print line $x/r<br>";
     }
}

function breakLine($y) {
    $y = str_replace('[br]', ' ', $y);
    $arr = explode('|', $y);
    $arr[4] = explode(' (', $arr[4], 2);
    $arr[4][0]  = str_replace('Client: ', '', $arr[4][0]);
    $arr[4] = implode('|', $arr[4]);
    $arr = implode('|', $arr);
    $arr = explode('|', $arr);
    $arr[5] = explode(')', $arr[5], 2);
    $arr[5][0] = explode(", ", $arr[5][0]);
    if (count($arr[5][0]) == 1) {
        array_splice($arr[5][0], 1, 0, 'x');
    }
    $arr[5][0] = implode('|', $arr[5][0]);
    $arr[5][1] = str_replace('Summary:', '', $arr[5][1]);
    $arr[5] = implode('|', $arr[5]);
    $arr = implode('|', $arr);
    return (explode('|', $arr));
}

$nameList = array('', 'BastokQuests', 'SanDorianQuests', 'WindurstQuests', 'JeunoQuests', 'OtherQuests', 'ZilartQuests', 'AhtUrhganQuests', 'GoddessQuests', 'AbysseaQuests');
$catList = array('', 'Bastok', "San d'Oria", 'Windurst', 'Jeuno', 'Other', 'Outlands', 'Aht Urhgan', 'Crystal War', 'Abyssea');
$fameList = array('', 'Bastok', "San d'Oria", 'Windurst', 'Jeuno', 'Other', 'Outlands', 'Treasures of Aht Urhgan', 'Wings of the Goddess', 'Abyssea');
$expList = array('', '', '', '', '', '', 'Rise of the Zilart', 'Treasues of Aht Urhgan', 'Wings of the Goddess', 'Abyssea');

$newData = array();

//print "curFile = ".$curFile."<br />\r\n";
$printString = ($print == true) ? '&print=true' : '';

if ($curFile == null) {
    print "<b>Listing</b><br />\r\n";
    foreach ($nameList as $key => $curName) {
        if ($key > 0) {
            print "<a href=\"Quest-O-Matic.php?curFile=".$key."&action=list$printString\">".$curName."</a><br />\r\n";
        }
    }
    print "<br />\r\n<b>XML</b><br />\r\n";
    foreach ($nameList as $key => $curName) {
        if ($key > 0) {
            print "<a href=\"Quest-O-Matic.php?curFile=".$key."&action=XML\">".$curName."</a><br />\r\n";
        }
    }
} else {
    $array = file($nameList[$curFile].'Parse.txt');
    $data = array();
    $counter = 0;
    //print "action = ".$action."<br />\r\n";
    if (!$array) {
        print ('Unable to open file');
    } elseif ($action == "list") {
        if (strlen($questNum) > 0 ){
            $thisLine = breakLine($array[$questNum]);
            if (substr($thisLine[3], 0, 2) == "**") {
                $thisLine[3] = substr($thisLine[3], 2);
                $repeat = 'Yes';
            } else {
                $repeat = '';
            }
            if ($thisLine[6] == "x") {
                $loc = trim($thisLine[5]);
                $warning = "<b>The proper location for this file is missing, a substitute was chosen.  Please chect to see if this new location is correct.</b>\r\n<br />";
            } else {
                $loc = trim($thisLine[6]);
                $warning = '';
            }
            if ($curFile == 8) {
                $location = ", $loc (S)";
                $locCat = "[[Category:$loc (S) Quests]] ";
            } else {
                $location = ", $loc";
                $locCat = "[[Category:$loc Quests]] ";
            }
            $wikiName = str_replace($pageRenameIn, $pageRenameOut, $thisLine[3]);
            $pagename = str_replace($renameIn, $renameOut, $wikiName);
            if ($pagename == $thisLine[3]) {
                $pagename = '{{PAGENAME}}';
            }
            print "<html>
<head><title>".$thisLine[3]."</title>
</head>
<body>
{{Stub|Quest}}<br />
$warning{{Quest Header
<br />|Expansion=".$expList[$curFile]."
<br />|Start=".trim($thisLine[4])."$location
<br />|Description=".$thisLine[7]."
<br />|Fame=".$fameList[$curFile]."
<br />|FLevel=
<br />|Quest Reqs=
<br />|Item Reqs=
<br />|Level=
<br />|Repeatable=$repeat
<br />|Previous=
<br />|Next=
<br />|Title=
<br />|Reward=
<br />|Image=$pagename.jpg
<br />}}
<br />&lt;br /&gt;
<br />
<br />== Walkthrough ==
<br />
<br />[[Category:".$catList[$curFile]." Quests]]  $locCat
</body>
</html>";
        } else {
            $printTarg = ($print == true) ? "Image-O-Matic2.php?&fileSystem=Quests" : "Quest-O-Matic.php?";
            print "Listing ".$catList[$curFile]." quests<br /><br />\r\n";
            foreach ($array as $key => $curLine) {
                 $thisLine = breakLine($curLine);
                 if (substr($thisLine[3], 0, 2) == "**") {
                     $thisLine[3] = substr($thisLine[3], 2);
                 }
                 array_push($newIndList, $thisLine[1]);
                 print "* [[<a href=\"$printTarg&curFile=".$curFile."&action=list&questNum=".$key."\">".$thisLine[3]."</a>]]<br />\r\n";
             }
             $indList = implode(',', $newIndList);
             if ($print == true) {
                 print "\r\n<br /><br /><a href=\"$imTarg?&action=batch&fileSystem=".$nameList[$curFile]."&ind=$indList\">Quest batch image processing</a><br /><br />";
             }
        }
    } elseif ($action == "XML" && strlen($ind) > 0) {
        $array = file($nameList[$curFile].'Parse.txt');
        $data = array();
        $inList = explode(',', $ind);
        foreach ($array as $key => $curRow) {
            $curDat = explode('|', $curRow);
            $data[$curDat[1]] = $curRow;
        }
        $day = date('Y-m-d');
        $time = date('H:i:s');
        $now = $day."T".$time."Z";
        print "<html>
<head><title>".$catList[$curFile]." Quest listing</title>
</head>
<body>";
        foreach ($inList as $id) {
            if ($data[$id] != null) {
                $thisLine = breakLine($data[$id]);
                if (substr($thisLine[3], 0, 2) == "**") {
                    $thisLine[3] = substr($thisLine[3], 2);
                    $repeat = 'Yes';
                } else {
                     $repeat = '';
                }
                if ($thisLine[6] == "x") {
                     $loc = trim($thisLine[5]);
                     $warning = "<b>The proper location for this file is missing, a substitute was chosen.  Please chect to see if this new location is correct.</b>\r\n<br />";
                } else {
                    $loc = trim($thisLine[6]);
                    $warning = '';
                }
                if ($curFile == 8) {
                    $location = ", $loc (S)";
                    $locCat = "[[Category:$loc (S) Quests]] ";
                } else {
                    $location = ", $loc";
                    $locCat = "[[Category:$loc Quests]] ";
                }
                $wikiName = str_replace($pageRenameIn, $pageRenameOut, $thisLine[3]);
                $pagename = str_replace($renameIn, $renameOut, $wikiName);
                if ($pagename == $thisLine[3]) {
                    $pagename = '{{PAGENAME}}';
                }
                $fIndex = $curFile."00000000000000".$id;
                print "\r\n&nbsp;&nbsp;&lt;page&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;$wikiName&lt;/title&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;id&gt;$fIndex&lt;/id&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;revision&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;id&gt;$fIndex&lt;/id&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;timestamp&gt;$now&lt;/timestamp&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;contributor&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;username&gt;Septimus&lt;/username&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;id&gt;134&lt;/id&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/contributor&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;text xml:space=\"preserve\"&gt;{{Stub|Quest}}
<br />$warning{{Quest Header
<br />|Expansion=".$expList[$curFile]."
<br />|Start=".trim($thisLine[4])."$location
<br />|Description=".$thisLine[7]."
<br />|Fame=".$fameList[$curFile]."
<br />|FLevel=
<br />|Quest Reqs=
<br />|Item Reqs=
<br />|Level=
<br />|Repeatable=$repeat
<br />|Previous=
<br />|Next=
<br />|Title=
<br />|Reward=
<br />|Image=$pagename.jpg
<br />}}
<br />&lt;br /&gt;
<br />
<br />== Walkthrough ==
<br />
<br />[[Category:".$catList[$curFile]." Quests]]  $locCat &lt;/text&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/revision&gt;
<br />&nbsp;&nbsp;&lt;/page&gt;<br />";
            }
        }
        print "\r\n<br /></body>
</html>";
    } else {
        print "XML processing requires a batch listing.";
    }
}
