<?php
/*
Doorway generator
http://www.phpdevs.com/  by vadim@phpdevs.com
*/

if ($_POST['generate']) {
	generate();
}else {
	main_form();
}

/*              **********************                  */

function generate() {
	$p=array();
	if ($_POST['tpl'] && $_POST['keywords']) {
		$_POST['keywords']=ereg_replace("(\r\n|\r)","\n",$_POST['keywords']);
		$keys=explode("\n",trim($_POST['keywords']));

		/* create pages array */
		for($i=0;$i<intval($_POST['count']);$i++) {
			list($k,$v)=each($keys);
			if (!$v) {
				reset($keys);
				list($k,$v)=each($keys);
			}
			if (in_array(trim($v),$p)) {
				$v=trim($v).'_'.$i;
			}
			$p[]=trim($v);
		}
		

		/* prepare template */
		$tpl=implode('',file('./tpl/'.$_POST['tpl']));

		/* generate links */
		reset($p);
		while(list($k,$v)=each($p)) {
			$name=str_replace(' ','_',$v);
			$l.='<a href="'.trim($name).'.html">'.trim($v).'</a> ';
		}

		/* generate pages */
		reset($p);
		while(list($k,$v)=each($p)) {
			$k++;
			$name=trim(str_replace(' ','_',$v));

			$tpl_out=str_replace('{TITLE}',$v,$tpl);
			$tpl_out=str_replace('{KEYWORDS}',ereg_replace("(\r\n|\n|\r)","",implode(', ',$keys)),$tpl_out);
			$tpl_out=str_replace('{DESC}',ereg_replace("(\r\n|\n|\r)","",implode(' ',$keys)),$tpl_out);
			$tpl_out=str_replace('{LINK}',$l,$tpl_out);
			$tpl_out=str_replace('{I}',$k,$tpl_out);
			$tpl_out=str_replace('{ENTER}',trim($_POST['enter']),$tpl_out);

			$fp=fopen('./out/'.$name.'.html','w');
			fwrite($fp,$tpl_out);
			fclose($fp);
			print 'pages created -> '.$name.'.html<br>';
			flush();
		}
		print '<font color="#FF0000">done.</font>';
	}else {
		print 'Sorry, but I need template and keywords <br>';
	}
}

function main_form() {
	?>
Help: (<a href="readmy-ru.txt">russian</a>)	<br><br>
	<table width="400" border="0" cellspacing="2" cellpadding="0" align="center">
		<form action="" method="post">
		<tr>
			<td>Pages count:</td>
			<td><input type="text" name="count" size="2" maxlength="2" value="5"></td>
		</tr>
		<tr>
			<td>Template:</td>
			<?php
			$a=array();
			if ($handle = opendir('./tpl')) {
				while (false !== ($file = readdir($handle))) {
					if ($file != "." && $file != ".." && is_file('./tpl/'.$file)) {
						$a[]=$file;
					}
				}
				closedir($handle);
			}
			?>
			<td><select name="tpl">
					<?php while(list($k,$v)=each($a)) {?>
					<option value="<?php print $v;?>"><?php print $v;?></option>
					<?php } ?>
				</select>
			</td>
		</tr>
		<tr>
			<td>Enter page:</td>
			<td><input type="text" name="enter" size="40" value="http://"></td>
		</tr>
		<tr>
			<td>Keywords:</td>
			<td>
<textarea cols="30" rows="5" name="keywords">
sample
sample2
sample 2
sample word
</textarea>
			</td>
		</tr>
		<tr><td colspan="2"><input type="submit" name="generate" value="Generate pages"></td></tr>
		</form>
	</table>
	<?php
}

?>