<?php
/*
CMS Realty - Open-Realty 2 Component for Joomla.
Author: Philip Vickers - www.codenza.co.nz
Copyright (C) 2005 Codenza Limited

This file is part of CMS Realty.

CMS Realty 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.

CMS Realty is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with CMS Realty; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

include_once COM_PATH."/gateway/AbstractURLFactory.class.php";
include_once COM_PATH."/rule/RuleManager.class.php";


/**
 * Base class for Joomla URLFactory classes.
 */
class JoomlaURLFactory extends AbstractURLFactory {
	var $indexFile;
	var $cmsParameterKeys;

	function JoomlaURLFactory() {
		
	}

	/**
	 * Inserts some javascript to redirect forms through the CMS
	 */
	function formToCMS($matches) {

		// OR is a bit inconsistent with the form actions. There are 3 different syntaxes used:
		// action=""
		// action="?action=???"
		// action="index.php?action=???"
		// The empty form actions do not work with CMS Realty so replace them with something more useable
		// i.e. replace action="" with action="index.php?action=???"
		$matches[0] = preg_replace_callback("/action=\"\"/", array(&$this, 'addAction'), $matches[0]);
		$matches[0] = preg_replace_callback("/action=\"([?][%u015E0-9a-zA-Z_&=;\-\[\]%+]*)\"/", array(&$this, 'addAction'), $matches[0]);

		// The following code is now only relevant to the search page's GET form...
		$sliced = split(">", $matches[0]);
		// the subsequent call to cmsFriendlyURL will add the CMS parameters...
		$cmsAction = $this->indexFile."?";

		return $sliced[0].' onSubmit="return sendFormToCMS(this, \''.$cmsAction.'\');">';
	}

	function replaceOnClick($matches) {
		return "sendFormToCMS(document.getElementById('$matches[1]'), '$this->indexFile?');";
	}

	function addAction($matches) {
		return 'action="'.$this->indexFile.'?'.$this->getORQuery().'"';
	}

	function setQueryParameters($params) {
		$this->cmsParameters = array();
		$this->orParameters = array();
		foreach ( $params as $k=>$v ) {
			$key = urldecode($k);
			if ( is_string($v) ) {
				$v = urldecode($v);
			}

			if ( in_array($key, $this->cmsParameterKeys) ) {
				$this->cmsParameters[$key] = $v;
			}
			else if ( $key == 'cmsrealty' ) {
				$this->setAdmin($v=='admin');
			}
			else if ( $key == 'openrealty' ) { // the old encoding scheme
				$orQuery = $this->decodeOpenRealtyQuery($v);
				parse_str($orQuery, $this->orParameters);
			}
			else {
				// When Joomla SEF is enabled, array parameters are not being converted
				// to PHP arrays, so do this manually...
				if ( substr($key, strlen($key)-2) == '[]' ) {
					$key = substr($key, 0, strlen($key)-2);
					if ( !isset($this->orParameters[$key]) ) {
						$this->orParameters[$key] = array();
					}
					array_push($this->orParameters[$key], $v);
				} 
				else {
					$this->orParameters[$key] = $v;
				}
			}
		}
	}

	function getCMSQuery() {
		return getQuery($this->cmsParameters);
	}

	function getORQuery() {
		return $this->getQuery($this->orParameters);
	}

	function getQuery($parameters) {
		$query = "";
		foreach ( $parameters as $k=>$v ) {
			if ( is_array($k) ) {
				foreach ( $k as $key=>$value ) {
					if ( strlen($query)>0 ) {
						$query .= "&";
					}
					$query.="$key=$value";
				}
			}
			else {
				if ( strlen($query)>0 ) {
					$query .= "&";
				}
				$query.="$k=$v";
			}
		}
		return $query;
	}


	function encodeOpenRealtyQuery($queryString) {
		$len = strlen($queryString);
		$data = '';
		for ($i=0; $i<$len; $i++) $data.=sprintf("%02x",ord(substr($queryString,$i,1)));
		return $data;
	}
	function decodeOpenRealtyQuery($queryString) {
		$len = strlen($queryString);
		$data = '';
		for ($i=0;$i<$len;$i+=2) $data.=chr(hexdec(substr($queryString,$i,2)));
		return $data;
	}
}

?>