clearPrefCache('emote');
require_once("auth.php");
$mes = e107::getMessage();
// Change the active emote pack
if (isset($_POST['active']))
{
	if ($pref['smiley_activate'] != $_POST['smiley_activate'])
	{
		$pref['smiley_activate'] = (int) $_POST['smiley_activate'];
		e107::getLog()->add($pref['smiley_activate'] ? 'EMOTE_02' : 'EMOTE_03', $pref['emotepack'], E_LOG_INFORMATIVE, '');
		e107::getConfig()->set('smiley_activate', $pref['smiley_activate'])->save(true, true, true);
		$update = true;
	}
	else
	{
		$mes->addInfo(LAN_NO_CHANGE);
	}
}
//e107::getRender()->tablerender($caption, $mes->render() . $text);
/* get packs */
$fl = e107::getFile();
$emote = new emotec;
$one_pack = false;
$filtered = e107::getParser()->filter($_POST);
// Check for pack-related buttons pressed
foreach ($filtered as $key => $value)
{
	if (strpos($key, "subPack_") !== false)
	{
		$subpack = str_replace("subPack_", "", $key);
		$emote->emoteConf($subpack);
		break;
	}
	if (strpos($key, "XMLPack_") !== false)
	{
		$subpack = str_replace("XMLPack_", "", $key);
		$emote->emoteXML($subpack);
		break;
	}
	if (strpos($key, "defPack_") !== false)
	{
		e107::getConfig()->set('emotepack', str_replace("defPack_", "", $key))->save(true, true, true);
		e107::getLog()->add('EMOTE_01', $pref['emotepack'], E_LOG_INFORMATIVE, '');
		break;
	}
	if (strpos($key, "scanPack_") !== false)
	{
		$one_pack = str_replace("scanPack_", "", $key);
		break;
	}
}
// $ns->tablerender($caption, $mes->render() . $text);
$check = $emote->installCheck($one_pack);
if ($check !== false)
{
	$emote->listPacks();
}
class emotec
{
	var $packArray;            // Stores an array of all available emote packs (as subdirectory names)
	function __construct()
	{
		/* constructor */
		$fl = e107::getFile();
		$this->packArray = $fl->get_dirs(e_IMAGE . "emotes");
		if (isset($_POST['sub_conf']))
		{    // Update stored pack configuration
			$this->saveConf();
		}
	}
	// List available emote packs
	function listPacks()
	{
		$pref = e107::getPref();
		$frm = e107::getForm();
		$fl = e107::getFile();
		$ns = e107::getRender();
		$mes = e107::getMessage();
		$text = "
	
		
		
		
		";
		$ns->tablerender(EMOLAN_PAGE_TITLE, $mes->render() . $text);
	}
	// Configure an individual emote pack
	function emoteConf($packID)
	{
		global $e107, $sysprefs;
		$frm = e107::getForm();
		$tp = e107::getParser();
		$fl = e107::getFile();
		$ns = e107::getRender();
		$packID = $tp->filter($packID);
		$corea = "emote_" . $packID;
		$emotecode = $sysprefs->getArray($corea);
		$reject = '~^emoteconf|\.html$|\.php$|\.txt$|\.pak$|\.xml|\.phpBB';        // Files to exclude
		$emoteArray = $fl->get_files(e_IMAGE . "emotes/" . $packID, $reject);
		$eArray = array();
		foreach ($emoteArray as $value)
		{
			$eArray[] = array('path' => $value['path'], 'fname' => $value['fname']);
		}
		//XXX Not sure if we need to know rhe number of files found - count($eArray) - Total ".count($eArray)." files found
		$text = "
		";
		$ns->tablerender(EMOLAN_PAGE_TITLE . SEP . LAN_EDIT . ": '" . $packID . "'", $text);
	}
	// Generate an XML file - packname.xml in root emoticon directory
	function emoteXML($packID, $strip_xtn = true)
	{
		global $sysprefs;
		$mes = e107::getMessage();
		$fl = e107::getFile();
		$tp = e107::getParser();
		$packID = $tp->filter($packID);
		$fname = e_IMAGE . "emotes/" . $packID . "/emoticons.xml";
		$backname = e_IMAGE . "emotes/" . $packID . "/emoticons.bak";
		$corea = "emote_" . $packID;
		$emotecode = $sysprefs->getArray($corea);
		$reject = '~^emoteconf|\.html$|\.php$|\.txt$|\.pak$|\.xml|\.phpBB';        // Files to exclude
		$emoteArray = $fl->get_files(e_IMAGE . "emotes/" . $packID, $reject);
		$eArray = array();
		foreach ($emoteArray as $value)
		{
			$eArray[] = $value['fname'];
		}
		$f_string = "\n\n\n\n";
		foreach ($eArray as $emote)
		{
			// Optionally strip file extension
			$evalue = str_replace(".", "!", $emote);
			if ($strip_xtn)
			{
				$ename = substr($emote, 0, strrpos($emote, '.'));
			}
			$f_string .= "\n";
			foreach (explode(' ', $tp->toForm($emotecode[$evalue])) as $v)
			{
				if (trim($v))
				{
					$f_string .= "\t" . htmlentities($v) . "\n";
				}
			}
			$f_string .= "\n";
		}
		$f_string .= "\n\n";
		if (is_file($backname))
		{
			unlink($backname);
		}        // Delete any old backup
		if (is_file($fname))
		{
			rename($fname, $backname);
		}
		if (file_put_contents($fname, $f_string) === false)
		{
			$mes->addWarning('' . EMOLAN_30 . ':' . str_replace(e_IMAGE, e_IMAGE_ABS, $fname));
		}
		else
		{
			$mes->addSuccess('' . EMOLAN_29 . ':' . str_replace(e_IMAGE, e_IMAGE_ABS, $fname));
		}
	}
	// Save configuration for an emote pack that's been edited
	function saveConf()
	{
		$sql = e107::getDb();
		$tp = e107::getParser();
		$packID = $_POST['packID'];
		unset($_POST['sub_conf'], $_POST['packID']);
		$encoded_emotes = $tp->toDB($_POST);
		//	$tmp = addslashes(serialize($encoded_emotes));
		$tmp = e107::serialize($encoded_emotes);
		if ($sql->select("core", "*", "e107_name='emote_" . $packID . "'"))
		{
			e107::getMessage()->addAuto($sql->update("core", "`e107_value`='{$tmp}' WHERE `e107_name`='emote_" . $packID . "' "), 'update', LAN_SETSAVED, false, false);
		}
		else
		{
			e107::getMessage()->addAuto($sql->insert("core", "'emote_" . $packID . "', '$tmp' "), 'insert', LAN_SETSAVED, false, false);
		}
	}
	// Identify currently selected emote pack. Read in any new ones
	// Return false to disable listing of packs
	function installCheck($do_one = false)
	{
		global $e107;
		$sql = e107::getDb();
		$fl = e107::getFile();
		$ns = e107::getRender();
		$mes = e107::getMessage();
		// Pick up a list of emote packs from the database
		$pack_local = array();
		if ($sql->select("core", "*", "`e107_name` LIKE 'emote_%'"))
		{
			while ($row = $sql->fetch())
			{
				$pack_local[substr($row['e107_name'], 6)] = true;
			}
		}
		foreach ($this->packArray as $value)
		{
			if (strpos($value, ' ') !== false)
			{    // Highlight any directory names containing spaces - not allowed
				$msg = "
					" . EMOLAN_17 . " " . EMOLAN_18 . ":
					" . LAN_NAME . ": {$value}
					" . EMOLAN_20 . ": " . e_IMAGE_ABS . "emotes/
				";
				$mes->addError($msg);
				$ns->tablerender(EMOLAN_PAGE_TITLE . ' - ' . EMOLAN_21, $mes->render());
				return false;
			}
			if (array_key_exists($value, $pack_local))
			{
				unset($pack_local[$value]);
			}
			if (($do_one == $value) || !$do_one && (!$sql->select("core", "*", "e107_name='emote_" . $value . "' ")))
			{  // Pack info not in DB, or to be re-scanned
				$no_error = true;
				$File_type = EMOLAN_32 . ":";
				// Array of all files in the directory of the selected emote pack
				$fileArray = $fl->get_files(e_IMAGE . "emotes/" . $value);        // We actually want all the files in the directory
				$confFile = '';
				foreach ($fileArray as $k => $file)
				{
					if (strpos($file['fname'], ".xml") !== false)
					{
						$confFile = array('file' => $file['fname'], 'type' => "xml");
					}
					elseif (strpos($file['fname'], ".pak") !== false)
					{
						$confFile = array('file' => $file['fname'], 'type' => "pak");
					}
					elseif (strpos($file['fname'], ".php") !== false)
					{
						$confFile = array('file' => $file['fname'], 'type' => "php");
					}
					if ($confFile)
					{
						unset($fileArray[$k]);
						break;
					}
				}
				/* .pak file */
				if ($confFile['type'] == "pak")
				{
					$filename = e_IMAGE . "emotes/" . $value . "/" . $confFile['file'];
					$pakconf = file($filename);
					$contentArray = array();
					foreach ($pakconf as $line)
					{
						if (trim($line) && strpos($line, "=+") !== false && strpos($line, ".txt") === false && strpos($line, ".html") === false && strpos($line, "cvs") === false)
						{
							$contentArray[] = $line;
						}
					}
					$confArray = array();
					foreach ($contentArray as $pakline)
					{
						$tmp = explode("=+:", $pakline);
						$confIC = str_replace(".", "!", $tmp[0]);
						$confArray[$confIC] = trim($tmp[2]);
					}
					// $tmp = addslashes(serialize($confArray));
					$tmp = e107::serialize($confArray);
					$File_type = EMOLAN_22 . ":";
				}
				/* end  */
				/* .xml file  */
				if ($confFile['type'] == "xml")
				{
					$filename = e_IMAGE . "emotes/" . $value . "/" . $confFile['file'];
					$contents = file_get_contents($filename);
					$confArray = array();
					$xml_type = 0;
					if ((strpos($contents, "") !== false) && (strpos($contents, "") !== false))
					{    // xep-0038 format
						/* Example:
						  
							:-)
							:)
							
							
						  */
						preg_match_all("#\(.*?)\<\/icon\>#si", $contents, $match);
						$xml_type = 1;
						// $match[0] - complete emoticon entry
						// $match[1] - match string and object specification
						$item_index = 1;
					}
					elseif (strpos($contents, "";
						preg_match_all("#\(.*?)\<\/emoticon\>#si", $contents, $match);
						$xml_type = 2;
						// $match[0] - complete emoticon entry
						// $match[1] - filename (may or may not not have file extension/suffix)
						// $match[2] - match string(s) representing emote
						$item_index = 2;
					}
					if ($xml_type)
					{
						for ($a = 0, $aMax = count($match[0]); $a < $aMax; $a++)
						{
							$e_file = '';
							switch ($xml_type)
							{
								case 1 :        // xep-0038
									// Pull out a file name (only support first image type) - its in $fmatch[1]
									if (preg_match("#\