<?php
include 'includes/h3.php'
?>


<?php
/**
* help-viewer.php
* EmpireMUD 2.0 web-based help files
* by Paul S. Clarke
*
* This program searches your help files from a web browser, which can be
* helpful both for players and for content builders.
*
* Using this file:
*  1. update the configuration, near the top
*  2. you may want to symlink this file into your web directory; it's also
*     very plain looking so you may want to put it into an iframe or style it
*     yourself
*/


// CONFIGURE: path to help index, ending in /
$indexPath = '/home/ubuntu/mud/lib/text/help/';


// input
$q = isset($_GET['q']) ? $_GET['q'] : '';
$allowimm = isset($_GET['imm']) && $_GET['imm'];

// data sotrage
global $helpFiles;
$helpFiles = array();

// import help files
$list = file_get_contents($indexPath . 'index');
foreach (explode("\n", $list) as $fname) {
	if (preg_match('/\.hlp$/', $fname)) {
		// echo "importing $indexPath$fname...\n";
		import_help(file_get_contents($indexPath . $fname));
	}
}


 //////////////////////////////////////////////////////////////////////////////
//// BASIC DISPLAY ///////////////////////////////////////////////////////////
?>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style> 
input[type=text] {
  width: 130px;
  max-width: 350px;
  box-sizing: border-box;
  border: 2px solid #ccc;
  border-radius: 4px;
  font-size: 16px;
  background-color: white;
  background-image: url('searchicon.png');
  background-position: 10px 10px; 
  background-repeat: no-repeat;0
  padding: 50px 20px 12px 40px;
  -webkit-transition: width 7s ease-in-out, margin-left 3s ease-in-out;
  transition: width 7s ease-in-out, margin-left 3s ease-in-out;
}

input[type=text]:focus {
  width: 100%;
  margin-left: -130px;
}
.title { 
	padding: 5px;
	margin-left: 0px;
}
.search-form{
	margin-left: 40vw;
}
body {
	background-image: url('images/village1.jpeg');
	background-size: cover;
	background-position: center;
	background-attachment: fixed;
	margin: 0;
	padding: 0;
}
.w3-content {
  position: relative;
  color: white;
}

.w3-opacity-box {
  position: relative;
  background-color: rgba(0, 0, 0, 0.5); /* Adjust opacity and color as needed */
  padding: 20px;
  border-radius: 10px;
}

.w3-opacity {
  opacity: 1; /* Full opacity for text */
}

</style>
</head>
<body>
<style>
	div.result {
	    font-family: "Lucida Console", Monaco, monospace;
	    font-size: 10pt;
	    margin: 5px;
	    padding: 5px;
	    border: 1px solid gray;
	    overflow: auto; /* Enable vertical scrolling if needed */
	    overflow-wrap: normal; /* Allow long words to break */
	    display: flex; /* Use flexbox */
	    flex-direction: column; /* Arrange children in a column */
	    flex: 1; /* Allow the box to grow to fill available space */
	    max-width: 700px;
	    margin-left: 15vw;
	}

</style>
<style>

@media only screen and (max-width:800px) {
  /* For tablets: */
  .title {
    width: 80%;
    padding: 0;
  }
  .search-form {
    margin-left: 40vw;
  }
  .result{
    margin-left: 10vw;
}
@media only screen and (max-width:500px) {
  /* For mobile phones: */
  .search-form{
  margin-left: 5vw;
  width: auto;
  }
 .title {
    margin-left: 5px;
    font-size: 2px;
    padding-top: 12px;
} 

.result {
    margin-left: 5px;
  }
input[type=text]:focus {
                /* Override animation properties for mobile */
                -webkit-transition: none;
                transition: none;
                width: auto;
                margin-left: 0;
            }
input[type=text] {
  width: auto;
  max-width: 350px;
  box-sizing: border-box;
  border: 2px solid #ccc;
  border-radius: 4px;
  font-size: 16px;
  background-color: white;
  background-image: url('searchicon.png');
  background-position: 10px 10px; 
  background-repeat: no-repeat;0
  padding: 50px 20px 12px 40px;
}
div.result {
	margin-left: auto;
	}
}
</style>
<div class="w3-opacity-box">
<div class="title w3-opacity w3-content"><h1>Search in-game help files:</h1></div></div>
<form class="search-form w3-content" action="<?= $_SERVER['SCRIPT_NAME'] ?>" method="GET">
	<input type="text" name="q" placeholder="search" value="<?= htmlentities($q) ?>" size="40" tabindex="1" />
	<input type="submit" value="search" tabindex="2" />
	<input type="checkbox" id="allowimm" name="imm" value="1" tabindex="3" <?= ($allowimm ? 'checked="checked"' : '') ?> /> <label for="allowimm">Search immortal helps too!</label>
</form>
<!--</div>-->
</body>
<?php

 //////////////////////////////////////////////////////////////////////////////
//// SHOW RESULTS ////////////////////////////////////////////////////////////

if ($q) {
	foreach ($helpFiles as $help) {
		if (($allowimm || !$help->isImmortal()) && $help->isKeyword($q)) {
			echo "<div class=\"result w3-content w3-opacity w3-opacity-box\">", $help->showBody(), "</div>\n";
		}
	}
}


 //////////////////////////////////////////////////////////////////////////////
//// HELPER STUFF ////////////////////////////////////////////////////////////


// helper for importing
function import_help($raw) {
	global $helpFiles;
	
	$working = FALSE;
	$body = '';
	foreach (explode("\n", $raw) as $line) {
		if (!$working) {
			$working = TRUE;
			$body = '';
		}
		if ($line[0] == '#') {
			$helpFiles[] = new HelpFile($body, substr($line, 1));
			$working = FALSE;
		}
		else if ($working) {
			$body .= $line . "\n";
		}
		
		// done
		if (!$working && $line[0] == '$') {
			return;
		}
	}
}

class HelpFile {
	/**
	* constructor -- pass a raw help file from keyword line to BEFORE the #
	*
	* @param string $raw The raw help file.
	* @param 
	*/
	public function __construct($raw, $level = '') {
		// process keywords
		$first = strtok($raw, "\n");
		preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $first, $matches);
		foreach ($matches[0] as $str) {
			$this->keywords[] = str_replace('"', '', $str);
		}
		
		// store data
		$this->body = $raw;
		$this->level = $level;
	}
	
	/**
	* @return bool TRUE if there is no access restriction, FALSE if there is
	*/
	public function isImmortal() {
		return $this->level != '';
	}
	
	/**
	* @return string The body of the help file, for html.
	*/
	public function showBody() {
		$str = $this->body;
		
		// strip color codes
		$str = preg_replace('/([^&])&[rbgywpalomcjtvun0RBGYWPALOMCJTVU]/', '$1', $str);
		
		$replacements = array(
			"&&" => "&amp;",
			"<" => "&lt;",	">" => "&gt;",
			" " => "&nbsp;",
			"\n" => "<br/>",
		);
		
		$str = str_replace(array_keys($replacements), array_values($replacements), $str);
		
		return $str;
	}
	
	/**
	* @param string $str A keyword/phrase.
	* @return bool TRUE if the keyword matches.
	*/
	public function isKeyword($str) {
		foreach ($this->keywords as $word) {
			if (!strncmp(strtoupper($str), $word, strlen($str))) {
				return TRUE;
			}
		}
		return FALSE;
	}

	// data
	private $keywords;	// array of strings
	private $level;	// level code, if any
	private $body;	// text of help including keyword header
}
