<?php


	class Plugin
	{
		var $title = '';
		var $desc = '';
		var $links = Array();
		var $savefullcontent = false;

		var $masks;
		var $extensions = null;
		var $ignorestrings = null;

		var $debug = false;

		/**
		* A stub for the parse function
		* The parse function is responsible for
		* Parsing a page content
		* Setting $this->title to the page title for the page
		* Setting $this->desc to the description of the page
		* Setting $this->links to any links to follow on the page
		* Returning the content to be stored in the index
		*
		* @return string
		*/
		function Parse()
		{
		}

		/**
		* Check if this plugin can be enabled or not
		*
		* @return boolean true if it's ok to enable otherwise false
		*/
		function Check()
		{
			return true;
		}

		function BuildExtensionList()
		{
			if ($this->extensions === null) {
				$exts = str_replace(" ", "", trim($GLOBALS["FF_CFG"]["site"]["indexExtensions"]));
				$arrExts = explode(",", $exts);
				$this->extensions = $arrExts;
			}

			return $this->extensions;
		}

		function RemoveExtraQueryStrings($url=false)
		{
			if (!$url) {
				return false;
			}

			$ignore_strings = $this->GetIgnoreStrings();

			if (!$ignore_strings) {
				return $url;
			}

			$url = str_replace('&amp;', '&', $url);

			$chars_to_chop = array('?', '&');  // these get taken off the end of the string. If it's going to be an empty query string, totally remove them.

			$urlparts = parse_url($url);

			if (!isset($urlparts['query']) || $urlparts['query'] == '') {
				// clean up the last char if we need to...
				$lastchar = substr($url, -1);
				if (in_array($lastchar, $chars_to_chop)) {
					$url = substr($url, 0, -1);
				}
				return $url;
			}
			$qstring = $urlparts['query'];

			$partstoreplace = array();
			// if there are multiple query strings on this url, then split them up. If there aren't, turn it into an array for the next step.
			if (strpos($qstring, '&') !== false) {
				$stringparts = explode('&', $qstring);
			} else {
				$stringparts = array($qstring);
			}

			// go through each section and see if the qry part is in the list of things to remove. If it is, remember it.
			foreach ($stringparts as $pos => $string) {
				// if we're ignoring a whole section (eg act=calendar), just check the whole string.
				if (in_array($string, $ignore_strings)) {
					$partstoreplace[] = $string;
					continue;
				}

				$qparts = explode('=', $string);
				$qry = $qparts[0];
				if (in_array($qry, $ignore_strings)) {
					$partstoreplace[] = $string;
				}
			}

			$stringtoreplace = $partstoreplace;

			// get rid of the parts we need to. Doesn't matter if this is an array, str_replace handles that ok.
			$url = str_replace($stringtoreplace, '', $url);
			$url = preg_replace('/(&)+/', '&', $url); // clean up any extra '&' chars.
			$url = str_replace(array('?&'), array('?'), $url); // clean up the query string a little more - just leave what we need.

			// clean up the end of the query string if we need to.
			$lastchar = substr($url, -1);
			if (in_array($lastchar, $chars_to_chop)) {
				$url = substr($url, 0, -1);
			}

			return $url;
		}

		function GetIgnoreStrings()
		{
			if (is_null($this->ignorestrings)) {
				$strings = trim($GLOBALS['FF_CFG']['site']['ignoreQueryStrings']);
				if ($strings != '') {
					$this->ignorestrings = explode(',', str_replace(' ', '', $strings));
				} else {
					$this->ignorestrings = false;
				}
			}
			return $this->ignorestrings;
		}

		function IsValidExtension($url='')
		{
			$arrURL = parse_url($url);

			// if it's just a url, it's a valid "extension".
			if (!isset($arrURL['path'])) {
				unset($arrURL);
				return true;
			}

			if ($arrURL['path'] == '') {
				unset($arrURL);
				return true;
			}

			$arrExt = explode(".", $arrURL["path"]);
			$ext = strtolower($arrExt[count($arrExt)-1]);

			unset($arrURL);
			unset($arrExt);

			// if the extension starts off like "/blah", it's fine.
			if (strpos($ext, '/') !== false) {
				return true;
			}

			static $indexExtensions = null;
			if ($indexExtensions === null) {
				$indexExtensions = $this->BuildExtensionList();
			}

			if (in_array($ext, $indexExtensions)) {
				return true;
			}

			return false;
		}

		/**
		 * Log a message of status Debug to the Shopping Cart system log
		 *
		 * @param mixed The var to log.
		 * @param boolean Should we escape the html in the var
		 *
		 * @return void
		 **/
		function DebugLog($filename, $var='', $escape=true)
		{
			// If debug is off then exit
			if (!$this->debug) {
				return;
			}

			$output = print_r($var, true);

			if ($escape) {
				$output = htmlspecialchars($output, ENT_QUOTES, 'utf-8');
			}

			$trace = debug_backtrace();
			$last_call = array_shift($trace);

			$called_from = 'Called from file '.str_replace(FF_BASE_PATH, '', $last_call['file']).' at line '.$last_call['line']."<br />\n";

			$logtext = 'DEBUG: '.$called_from.'<pre>'."\n".$output."\n</pre>\n";
			error_log($logtext, 3, TEMP_DIRECTORY.'/'.$filename);
		}

	}

?>
