<?php
/**
 * This file defines a helper {@link MobileOKPowderFactory} class.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package POWDER
 * @version $Revision: 1.11 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 */

/**
 * The mobileOK POWDER factory can be used to generate a machine-readable POWDER
 * document in XML that claims that a set of URIs was checked by the W3C mobileOK
 * Checker and conforms to the mobileOK specification.
 * 
 * @author Sylvain Lequeux
 * @author Francois Daoust <fd@w3.org>
 * @package POWDER
 * @link http://www.w3.org/TR/powder-dr/ POWDER
 * @link http://validator.w3.org/mobile/ The W3C mobileOK Checker
 * @link http://www.w3.org/TR/mobileOK-basic10-tests/ W3C mobileOK Basic Tests 1.0
 * @version $Revision: 1.11 $
 * @license http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231.html W3C Software Notice and License
 * @copyright Copyright (c) 2009, W3C (MIT, ERCIM, Keio)
 */
class MobileOKPowderFactory {
	
	static private $filename = "powder.xml";
	
	/**
	 * Creates the POWDER file that may be used to claim that the given
	 * set of resources were checked by the W3C mobileOK Checker and are
	 * mobileOK.
	 * 
	 * The POWDER file is not overwritten if it already exists.
	 * 
	 * @param array(string) $resources The list of resources for which mobileOK
	 *                                 conformance should be claimed.
	 * @param string $powderFile The full path to the file to generate
	 * @return string The path to the resulting POWDER XML file,
	 * 	              NULL when the file could not generated.
	 */
	public function generatePowderFile($resources, $powderFile){
		if (file_exists($powderFile)) {
			return $powderFile;
		}
		
		$powderContent = self::generatePowderDocument($resources);
		
		try {
			$handle = fopen($powderFile, 'w');
			fwrite($handle, $powderContent);
			fclose($handle);
		}
		catch (Exception $e) {
			return NULL;
		}
		
		return $powderFile;
	}
	
	
	/**
	 * Returns the POWDER document that may be used to claim that the given set
	 * of resources were checked by the W3C mobileOK Checker and are mobileOK.
	 * 
	 * @param string $uri Address of the Web site for which mobileOK-ness should
	 *   be claimed. The address must be a URI.
	 * @return string The resulting POWDER XML document as a string.
	 */
	public function generatePowderDocument($uri) {
		$uriParts = parse_url($uri);
		
		$port = ($uriParts['port'] != '') ? $uriParts['port'] : 80;
		
		$contents = '<?xml version="1.0"?>' . chr(13)
			. '<powder>' . chr(13)
			. chr(9) . '<attribution>' . chr(13)
			. chr(9) . chr(9) . '<issuedby src="http://www.w3.org/data#W3C"/>' . chr(13)
			. chr(9) . chr(9) . '<issued>'
			. date('c')
			. '</issued>' . chr(13)
			. chr(9) . chr(9) . '<supportedby src="http://validator.w3.org/mobile/"/>' . chr(13)
			. chr(9) . '</attribution>' . chr(13)
			. chr(9) . '<dr>' . chr(13)
			. chr(9) . chr(9) . '<iriset>' . chr(13)
			. chr(9) . chr(9) . chr(9) . '<includehost>'
			. $uriParts['host']
			. '</includehost>' . chr(13)
			. chr(9) . chr(9) . chr(9) . '<includeports>'
			. $port
			. '</includeports>' . chr(13);
			
		if (($uriParts['path'] != '')
		&& ($uriParts['path'] != '/')) {
			$contents .= chr(9) . chr(9) . chr(9) . '<includepathstartswith>'
				. $uriParts['path'] 
				. '</includepathstartswith>' . chr(13);
		}
		$contents .= chr(9) . chr(9) . '</iriset>' . chr(13)
			. chr(9) . chr(9) . '<descriptorset>' . chr(13)
			. chr(9) . chr(9) . chr(9) . '<typeof src="http://www.w3.org/2008/06/mobileOK#Conformant"/>' . chr(13)
			. chr(9) . chr(9) . chr(9) . '<displayicon src="http://www.w3.org/2005/11/MWI-Icons/mobileOK.png"/>' . chr(13)
			. chr(9) . chr(9) . chr(9) . '<displaytext>This page is mobileOK!</displaytext>' . chr(13)
			. chr(9) . chr(9) . '</descriptorset>' . chr(13)
			. chr(9) . '</dr>' . chr(13)
			. '</powder>' . chr(13);
		
		return $contents;
	}
	
}

?>