flushing installation script

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
Post Reply
zemaj
Registered User
Posts: 267
Joined: Thu Mar 07, 2002 3:58 am
Location: Australia
Contact:

flushing installation script

Post by zemaj » Sun Sep 29, 2002 11:38 am

Ok, if anyone is familar with my AI mod, they'll have found how annoying the AIML file installation procedure is. I've made a new system to handle this that should work much better. While I've nearly finished it (andit's the last thing I need to do for the mod), I can not get the script to flush it's output as it installs each file.

It used to work in the old version, but it didn't check if the person using it was an admin which is an obvious security hole, so I have to check that first by doing the usual checking for admin pages.

I've tried using flush() and ob_implicit_flush(), but it doesn't seem to work.

I'm placing a copy of my code here to see if anyone can help.

Please note that some of the code will not work. Don't comment on that, this page is still in development. I just need to know why it won't flush the output:

Code: Select all

<?php
/***************************************************************************
 *			     admin_iai_aiml_install.php
 *			     --------------------------
 *   copyright	  : (C) 2002 James Gay [zemaj]
 *   email		  : zemaj@palgn.com
 *
 *  This file is part of project IAI and uses code from Program E as well as
 *  the phpBB forum system. IP and copyright is retained by the authors of
 *  all three systems in their respective contributions.
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/
//
// The new, cool AIML system. Many thanks to r_mcm for the idea! It works well, huh?
// Also thanks to Sam (sj26) for being my talking php dictionary again :|
//

if( !empty($setmodules) )
{
	$file = basename(__FILE__);
	$module['Forum_AI']['AIML_files'] = $file;
	return;
}

define('IN_PHPBB', true);

$phpbb_root_path = './../';
$iai_root_path = "./../mods/iai/";
// $aimldir = $iai_root_path . "aiml/";
$aimldir = "./../iai/aiml/";
require($phpbb_root_path . 'extension.inc');
require("pagestart.$phpEx");
include($iai_root_path.'includes/constants.'.$phpEx);
include($iai_root_path.'includes/load_functions.'.$phpEx);
include($phpbb_root_path . 'language/lang_' . $board_config['default_lang'] . '/lang_admin_iai.' . $phpEx);

// allow script flushing
ob_implicit_flush();

if( $HTTP_POST_VARS['mode'] == 'update' )
{
	// now using update mode where we write directly to the page without using a template so the file loading can be seen.
	if($HTTP_POST_VARS['wipe_all'])
	{
		echo "wipe all on<br />";
	}
	if($HTTP_POST_VARS['install_all'])
	{
		echo "install all on<br />";
	}
	$sql = array();
	$vars = array();
	echo "<font size='3'><b>Welcome to the IAI AIML loading system. This may take a while depending on how many AIML files you have choosen to load into the bot's brain.<br />";
	if($HTTP_POST_VARS['safemode'])
	{
		echo "You are loading the files in <i>safe mode</i>. This loads files one at a time and should only be used if your php is in safe mode.";
	}
	else
	{
		echo "If the script times out it is probably because your PHP is running in safe mode. Choose the <i>safe mode</i> option if this is the case.";
	}
	echo "<br /><br /></b></font>\n";
	flush();

	// load installation vars
	ss_timing_start("all");
	$rootdir = $aimldir;
	if(empty($templatesinserted))
	{
		$templatesinserted = 0;
	}
	$fp = "";
	$whaton = "";
	$pattern = "";
	$topic = "";
	$that = "";
	$template = "";
	$startupwhich = "";
	$depth = array();
	$splitterarray = array();
	$inputarray = array();
	$genderarray = array();
	$personarray = array();
	$person2array = array();

	// start our functions
	lockdatabase();
	flushcache();
	$wordidcounter = getmaxwordid();

	if(empty($HTTP_POST_VARS['safethrough']))
	{
		// get list of AIML files in AIML dir
		$dir = opendir ($aimldir); 
		while ($file = readdir($dir))
		{ 
			if (preg_match('#^(.+).aiml$#', $file, $match))
			{
				$vars[] = $match[1];
			}
		}
		closedir($dir);
	}
	if(!$HTTP_POST_VARS['safemode'])
	{
		// this next for() replaces the loadstartup() function for the new system
		// NOTE: IAI does NOT use any startup.xml files
		for ($i = 0; $i < count($vars); $i++)
		{
			if (str_replace("\\'", "''", $HTTP_POST_VARS[$vars[$i]]))
			{
				// User wants to load current file, with filename stored under $vars[$i]
				$filename = $vars[$i] . ".aiml";
				learn($filename);
			}
		}
	}
	if($HTTP_POST_VARS['safemode'])
	{
		if(empty($HTTP_POST_VARS['safethrough']))
		{
			$safefiles = count($vars);
			$safethrough = 0;
			$safenames = array();
			for ($i = 0; $i < count($vars); $i++)
			{
				if (str_replace("\\'", "''", $HTTP_POST_VARS[$vars[$i]]))
				{
					$safenames[] = $vars[$i] . ".aiml";
					learn($filename);
				}
			}
		}
	}

	unlocktables();

	echo "<font size='3' color='GREEN'><b>ALL FILES LOADED SUCCESSFULLY</B><BR></font>\n";
	echo "<font size='3' color='BLUE'>[" . $templatesinserted . " AIML categories inserted]</font><br><BR>\n";
	echo "<BR>";
	flush();

	ss_timing_stop("all");
	echo "<BR><BR><font size='3' color='BLACK'>Total time was " . ss_timing_current("all") . " secs [" . ((ss_timing_current("all"))/60) . " mins].";
	echo "<BR><font size='3' color='BLACK'>Number of templates per second was " . $templatesinserted/ss_timing_current("all") ."<BR>";
	echo "<font size='3' color='BLACK'>Number of templates per minute was " . $templatesinserted/((ss_timing_current("all"))/60) . "<BR>";
	flush();
	
}
else
{
	// not updating, so use templates

	$template->set_filenames(array(
		'body' => 'admin/iai_aiml_install_body.tpl')
	);

	$s_hidden_fields = '<input type="hidden" name="mode" value="update" />';

	$template->assign_vars(array(
		'L_IAI_AIML_INSTALL' => $lang['IAI_aiml_install'],
		'L_IAI_AIML_INSTALL_EXPLAIN' => $lang['IAI_aiml_install_explain'],

		'L_AIML_FILELIST' => $lang['IAI_aiml_filelist'],
		'L_IN_SAFE_MODE' => $lang['IAI_in_safe_mode'],

		'L_INSTALL_ALL' => $lang['IAI_install_all'],
		'L_WIPE_ALL' => $lang['IAI_wipe_all'],
		'L_SUBMIT' => $lang['Submit'],
		'L_RESET' => $lang['Reset'],

		'S_HIDDEN_FIELDS' => $s_hidden_fields,
		'S_IAI_ACTION' => append_sid(basename(__FILE__)))
	);

	// send list of AIML files in AIML dir
	$dir = opendir ($aimldir); 
	while ($file = readdir($dir))
	{ 
		if (preg_match('#^(.+).aiml$#', $file, $match))
		{
			$template->assign_block_vars('filelist', array(
				'AIML_FILE' => $match[1])
			);
		}
	}
	closedir($dir);


	$template->pparse('body');
}

?>
Many thanks to anyone can figure this out.

-zemaj

Post Reply

Return to “[2.0.x] MOD Writers Discussion”