<?php
ini_set("display_errors", "on");
error_reporting(E_ALL ^ E_NOTICE);
require ('commonFunctions.php');

$page = $_GET['page'];
$action = $_GET['action'];
$textListing = $_POST['textListing'];

$ampIn = array(' + ', ' +', '+ ', '+', ' & ', ' &', '& ', '&');
$ampOut = array(' +', ' +', ' +', ' +', ' and ', ' and ', ' and ', ' and ');
$plusIn = array(' +');
$plusOut = array(' Plus ');
$cutList = array('Square of', 'Jug of', 'Vial of', 'Piece of', 'Jar of', 'Pot of', 'Vase of', 'Sprig of', 'Bag of', 'Clump of', 'Set of', 'Ingot of', 'Handful of', 'Spool of', 'Ponze of', 'Bowl of', 'Bundle of', 'Flask of', 'Flasque of', 'Lump of', 'Chunk of', 'Slice of', 'Serving of', 'Bunch of', 'Plate of', 'Bottle of', 'Sprig of', 'Dish of', 'Can of', 'Pair of', 'Loaf of', 'Cone of', 'Pitcher of', 'Pair of', 'Dose of');
$cutListPl = array('Squares of', 'Jugs of', 'Vials of', 'Pieces of', 'Jars of', 'Pots of', 'Vases of', 'Sprigs of', 'Bags of', 'Clumps of', 'Sets of', 'Ingots of', 'Handfuls of', 'Spools of', 'Ponzes of', 'Bowls of', 'Bundles of', 'Flasks of', 'Flasques of', 'Lumps of', 'Chunks of', 'Slices of', 'Servings of', 'Bunches of', 'Plates of', 'Bottles of', 'Sprigs of', 'Dishes of', 'Cans of', 'Pairs of', 'Loaves of', 'Cones of', 'Pitchers of', 'Pairs of', 'Doses of');
$repList = array('Square', 'Jug', 'Vial', 'Piece', 'Jar', 'Pot', 'Vase', 'Sprig', 'Bag', 'Clump', 'Set', 'Ingot', 'Handful', 'Spool', 'Ponze', 'Bowl', 'Bundle', 'Flask', 'Flasque', 'Lump', 'Chunk', 'Slice', 'Serving', 'Bunch', 'Plate', 'Bottle', 'Sprig', 'Dish', 'Can', 'Pair', 'Loaf', 'Cone', 'Pitcher', 'Pair', 'Dose');
$repListPl = array('Squares', 'Jugs', 'Vials', 'Pieces', 'Jars', 'Pots', 'Vases', 'Sprigs', 'Bags', 'Clumps', 'Sets', 'Ingots', 'Handfuls', 'Spools', 'Ponzes', 'Bowls', 'Bundles', 'Flasks', 'Flasques', 'Lumps', 'Chunks', 'Slices', 'Servings', 'Bunches', 'Plates', 'Bottles', 'Sprigs', 'Dishes', 'Cans', 'Pairs', 'Loaves', 'Cones', 'Pitchers', 'Pairs', 'Doses');

$specialWeaponIn = array("Ochain", "Daurdabla", "Verethragna", "Twashtar", "Almace", "Caladbolg", "Farsha", "Ukonvasara", "Redemption", "Rhongomiant", "Kannagi", "Masamune", "Gambanteinn", "Hvergelmir", "Gandiva", "Armageddon", "Aegis", "Gjallarhorn", "Spharai", "Mandau", "Excalibur", "Ragnarok", "Guttler", "Bravura", "Gungnir", "Apocalypse", "Kikoku", "Amanomurakumo", "Mjollnir", "Claustrum", "Annihilator", "Yoichinoyumi", "Conqueror", "Glanzfaust", "Yagrush", "Laevateinn", "Murgleis", "Vajra", "Burtgang", "Liberator", "Aymur", "Carnwenhan", "Gastraphetes", "Kogarasumaru", "Nagi", "Ryunohige", "Nirvana", "Tizona", "Death Penalty", "Kenkonken", "Terpsichore", "Tupsimati");
$numperpage = 250;

if ($action == 'list') {
    $array = @file('ultimateParse.txt');
    $linkList = array();
    foreach ($array as $currow) {
        $row = explode('|', $currow);
        $row[2] = trim($row[2]);
        $row[3] = trim($row[3]);
        $row[4] = trim($row[4]);
        $row[2] = str_replace('  ', ' ', $row[2]);
        $row[3] = str_replace('  ', ' ', $row[3]);
        $row[4] = str_replace('  ', ' ', $row[4]);
        $testname = ucitem($row[4]);
        $testname = str_replace($ampIn, $ampOut, $testname);
        $spcheck = str_replace($specialWeaponIn, "Special Weapon", $row[3]);
        $cutPhrase = str_replace($cutList, "", $testname);
        $cutPhrase = trim($cutPhrase);
        $checkPhrase = "False";
        $testshort = str_replace($ampIn, $ampOut, $row[3]);
        if ($row[2] != $testname) {
            $doLink = false;
            $doCheck = false;
            if (strpos($row[2], 'Mannequin') !== false) {
                if (strpos($row[7], 'female') !== false) {
                    $testname = str_replace('?', 'F', $testname);
                } else {
                    $testname = str_replace('?', 'M', $testname);
                }
                $doLink = true;
            } elseif (strpos($testname, 'Scroll of') !== false) {
                $doLink = true;
            } elseif ($spcheck == "Special Weapon" || ($testshort != $row[2] && strpos($row[2], '(Level ') !== false )) {
                $doLink = false;
                if ($checkPhrase == "True") {
                    //$doCheck = true;
                }
            } elseif (strpos($row[3], 'Bonanza Kupon') !== false) {
            } elseif ($testshort != $testname) {
                $doLink = true;
                if ($checkPhrase == "True") {
                    $doCheck = true;
                }
            }
            if ($doLink == true) {
                if ($linkList[$testname] != true) {
                    print $testname."|".$row[2]."<br />\r\n";
                    $linkList[$testname] = true;
                }
            }
            if ($doCheck == true) {
                if ($linkList[$cutPhrase] != true) {
                    print $cutPhrase."|".$row[2]."<br />\r\n";
                    $linkList[$cutPhrase] = true;
                }
            }
        }
    }
} elseif ($action == 'pluslist') {
    $array = @file('ultimateParse.txt');
    $linkList = array();
    foreach ($array as $currow) {
        $row = explode('|', $currow);
        $row[2] = trim($row[2]);
        $row[3] = trim($row[3]);
        $row[4] = trim($row[4]);
        $row[2] = str_replace('  ', ' ', $row[2]);
        $row[3] = str_replace('  ', ' ', $row[3]);
        $row[4] = str_replace('  ', ' ', $row[4]);
        $testname = ucitem($row[4]);
        $testname = str_replace($ampIn, $ampOut, $testname);
        $testLongPlus = str_replace($plusOut, $plusIn, $testname);
        $spcheck = str_replace($specialWeaponIn, "Special Weapon", $row[3]);
        $cutPhrase = str_replace($cutList, "", $testname);
        $cutPhrase = trim($cutPhrase);
        $cutPhrasePlus = str_replace($plusOut, $plusIn, $cutPhrase);
        $checkPhrase = "False";
        $checkPhrasePlus = "False";
        $testshort = str_replace($ampIn, $ampOut, $row[3]);
        $testShortPlus = str_replace($plusOut, $plusIn, $testshort);
        $checkPlus = (strpos($row[3], '+') === false) ? "False" : "True";
        if ($cutPhrase != $testname && $cutPhrase != $row[2]) {
            $checkPhrase = "True";
            //similar_text($cutPhrase, $row[2], $p);
            //print "<b>Check cut: \"".$row[2] . "\" | \"$testname\" | \"$cutPhrase\" $p</b><br />\r\n";
        }
        if ($checkPlus == "True") {
            if ($linkList[$testShortPlus] != true) {
                print $testShortPlus."|".$row[2]."<br />\r\n";
                $linkList[$testShortPlus] = true;
            }
        }
    }
} elseif ($action == 'directory') {
    print "Another form for Septimus.  This makes redirect pages in an XML
    format <br />
  <form id=\"form1\" name=\"form1\" method=\"post\" action=\"Redirect-O-Rama.php\">
  <label>Text Listing<br />
  <textarea name=\"textListing\" id=\"textListing\" cols=\"45\" rows=\"5\"></textarea>
  </label>
  </p>
  <p>
    <label>
    <input type=\"submit\" name=\"Submit\" id=\"Submit\" value=\"Submit\" />
    </label>
    <label>
    <input type=\"reset\" name=\"Reset\" id=\"Reset\" value=\"Reset\" />
    </label>
  </p>
</form>";
} elseif (strlen($textListing) > 0) {
    $textListing = explode("\r", $textListing);
    date_default_timezone_set('America/New_York');
    $day = date('Y-m-d');
    $time = date('H:i:s');
    $now = $day."T".$time."Z";
    $fIndex = floor (microtime(true));
    foreach ($textListing as $currow) {
        $row = explode('|', $currow);
        $fIndex++;
        print "&nbsp;&nbsp;&lt;page&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;".trim($row[0])."&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;#REDIRECT [[".trim($row[1])."]]&lt;/text&gt;
<br />&nbsp;&nbsp;&nbsp;&nbsp;&lt;/revision&gt;
<br />&nbsp;&nbsp;&lt;/page&gt;<br />";
    }
} elseif (strlen($page) == 0) {
    $array = @file('redirectList.txt');
    $count = count($array);
    $pages = ceil($count/$numperpage);
    for ($i=0; $i<$pages; $i++) {
        $start = $i * $numperpage;
        $end = $start + $numperpage - 1;
        if ($end > $count) {
            $end = $count;
        }
        print "<a href=\"Redirect-O-Rama.php?&page=$i\">Redirect $start through $end</a> <a href=\"Redirect-O-Rama.php?&page=$i&action=links\">Redirect $start through $end</a><br />\r\n";
    }
    print "<a href=\"Redirect-O-Rama.php?&action=directory\">Make a redirect directory</a> (For Septimus)<br />\r\n";
    //print "<a href=\"Redirect-O-Rama.php?&action=list\">Make redirect datafile</a> (For Septimus)<br />\r\n";
} else {
    $array = @file('redirectList.txt');
    $start = $page * $numperpage;
    $end = $start + $numperpage;
    for ($i = $start; $i < $end; $i++) {
        $row = explode('|', $array[$i]);
        print "[[".trim($row[0])."]]|[[".trim($row[1])."]]&lt;br /&gt;<br />\r\n";
    }
}
?>
