<?php
require_once(searchinc('htmlgen.inc'));

    class htmlimg extends _htmlblock
    {
        protected $_src;          // URL for image 
        protected $_alt;          // alternate text for image if not displayable
        protected $_longdesc;     // extended description of image
        protected $_ismap;        // TRUE or FALSE - is server side map
        protected $_usemap;       // ref to client side map
        
        protected $_hspace;       // whitespace on left and right side of an image - depreciated in HTML5
        protected $_vspace;       // whitespace on top and bottom of an image - depreciated in HTML5
        protected $_border;       // width of the border around an image - depreciated in HTML5
		
	var $_tag = 'img';
	var $_req_close_tag = false;
		
	function __construct($src=null, $height=null, $width=null, $alt=null, $attrs=null)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering('.$src.')', DEBUG_PROC_TRACE);

            if (!is_null($height))
            {
                if (!is_array($attrs)) { $attrs = array(); }
                $attrs['height'] = $height;
            }
                        
            if (!is_null($width))
            {
                if (!is_array($attrs)) { $attrs = array(); }
                $attrs['width'] = $width;
            }
			
            parent::__construct(null, $attrs);
            if ($this->is_ok())
            {
                $this->_src = $src;
                
                if (!is_null($alt))
                {
                    $this->_alt = $alt;
                } else if (is_array($attrs) && !is_null($attrs['alt'])) {
                    $this->_alt = $attrs['alt'];
                }
                        
                if (is_array($attrs) && !is_null($attrs['longdesc'])) {
                    $this->_longdesc = $attrs['longdesc'];
                }
                
                $this->_ismap = FALSE;
                if (is_array($attrs) && !is_null($attrs['ismap'])) {
                    $this->_ismap = TRUE;
                }
                        
                if (is_array($attrs) && !is_null($attrs['usemap'])) {
                    $this->_usemap = $attrs['usemap'];
                }
                        
                if (is_array($attrs) && !is_null($attrs['hspace'])) {
                    $this->_hspace = $attrs['hspace'];
                }
                        
                if (is_array($attrs) && !is_null($attrs['vspace'])) {
                    $this->_vspace = $attrs['vspace'];
                }
                        
                if (is_array($attrs) && !is_null($attrs['border'])) {
                    $this->_border = $attrs['border'];
                }
            }
            else {
		$this->trigger_error();
            }
        }
        
	// allows no contents
	//
	function set_contents(&$contents, $pos=null)
	{
            return FALSE;
	}
          
	function emit_str($tag_attrs='', $flags=0)
	{
		$proc = __CLASS__.'::'.__FUNCTION__.': ';
		$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
		$this->clear_error();
			
		$attrs = (isset($this->_src)?' src="'.$this->_src.'"':'').
			 (isset($this->_alt)?' alt="'.$this->_alt.'"':'').
			 (isset($this->_longdesc)?' longdesc="'.$this->_longdesc.'"':'').
			 ((isset($this->_ismap) && $this->_ismap)?' ismap':'').
			 (isset($this->_usemap)?' usemap="'.$this->_usemap.'"':'').
                         (isset($this->_vspace)?' vspace="'.$this->_vspace.'"':'').
			 (isset($this->_hspace)?' hspace="'.$this->_hspace.'"':'').
                         (isset($this->_border)?' border="'.$this->_border.'"':'');
			
			$str = parent::emit_str($attrs.$tag_attrs, $flags)."\n";
			
		$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
		return $this->is_ok($str);
	}
		
	function src($new=null)
	{
            if (isset($new))
            {
            	$this->_src = $new;
            }
            return $this->_src;
	}
		
	function alt($new=null)
	{
            if (isset($new))
            {
            	$this->_alt = $new;
            }
            return $this->_alt;
	}
		
	function longdesc($new=null)
	{
            if (isset($new))
            {
            	$this->_longdesc = $new;
            }
            return $this->_longdesc;
	}
		
	function ismap($new=null)
	{
            if (isset($new))
            {
            	$this->_ismap = (boolean)$new;
            }
            return $this->_ismap;
	}
		
	function usemap($new=null)
	{
            if (isset($new))
            {
            	$this->_usemap = $new;
            }
            return $this->_usemap;
	}
		
	function vspace($new=null)
	{
            if (isset($new))
            {
            	$this->_vspace = $new;
            }
            return $this->_vspace;
	}
		
	function hspace($new=null)
	{
            if (isset($new))
            {
            	$this->_hspace = $new;
            }
            return $this->_hspace;
	}
		
	function border($new=null)
	{
            if (isset($new))
            {
            	$this->_border = $new;
            }
            return $this->_border;
	}
        
    }
    
    class htmlsource extends htmlbase
    {
        protected $_src;
        protected $_type;
        protected $_srcset;
        protected $_media;
        protected $_sizes;

	var $_tag = 'source';
	var $_req_close_tag = false;
        
       	function __construct($src=null, $type=null, $attrs=null)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering('.$src.')', DEBUG_PROC_TRACE);

            parent::__construct($attrs);
            if ($this->is_ok())
            {
                if (!is_null($src)) { $this->_src = $src; }
                elseif (is_array($attrs) && !is_null($attrs['src'])) {
                    $this->_src = $attrs['src'];
                }
                
                if (!is_null($type)) { $this->_type = $type; }
                elseif (is_array($attrs) && !is_null($attrs['type'])) {
                    $this->_type = $attrs['type'];
                }
                
                if (is_array($attrs) && !is_null($attrs['srcset'])) {
                    $this->_srcset = $attrs['srcset'];
                }
                        
                if (is_array($attrs) && !is_null($attrs['media'])) {
                    $this->_media = $attrs['media'];
                }
                                        
                if (is_array($attrs) && !is_null($attrs['sizes'])) {
                    $this->_sizes = $attrs['sizes'];
                }
            }
            else {
		$this->trigger_error();
            }
            $this->dbgecho($proc.'leaving('.$src.')', DEBUG_PROC_TRACE);
        }

	function emit_str($tag_attrs='', $flags=0)
	{
		$proc = __CLASS__.'::'.__FUNCTION__.': ';
		$this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
		$this->clear_error();
			
		$attrs = (isset($this->_src)?' src="'.$this->_src.'"':'').
			 (isset($this->_type)?' type="'.$this->_type.'"':'').
			 (isset($this->_srcset)?' srcset="'.$this->_srcset.'"':'').
			 (isset($this->_media)?' media="'.$this->_media.'"':'').
                         (isset($this->_sizes)?' sizes="'.$this->_sizes.'"':'');
			
			$str = parent::emit_str($attrs.$tag_attrs, $flags)."\n";
			
		$this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
		return $this->is_ok($str);
	}
		
	function src($new=null)
	{
            if (isset($new))
            {
            	$this->_src = $new;
            }
            return $this->_src;
	}
		
	function type($new=null)
	{
            if (isset($new))
            {
            	$this->_type = $new;
            }
            return $this->_type;
	}
		
	function srcset($new=null)
	{
            if (isset($new))
            {
            	$this->_srcset = $new;
            }
            return $this->_srcset;
	}
		
	function media($new=null)
	{
            if (isset($new))
            {
            	$this->_media = $new;
            }
            return $this->_media;
	}
		
	function sizes($new=null)
	{
            if (isset($new))
            {
            	$this->_sizes = $new;
            }
            return $this->_sizes;
	}
    }

    class htmlaudio extends _htmlblock
    {
        
        protected $_src;                 // URL for audio file 
        protected $_controls = true;     // display controls
        protected $_autoplay = false;    // start playing immediatly
        protected $_loop = false;        // lopp continuously
        protected $_muted = false;       // audio output muted
        protected $_preload = true;      // load when page loads (auto, metadata, none)

	var $_tag = 'audio';
	var $_req_close_tag = true;
        
	function __construct($src=null, $altcontents=null, $attrs=null)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering('.$src.')', DEBUG_PROC_TRACE);
            
            if (is_array($attrs))
            {
                if (!is_empty($src)) {
                    $this->_src = $src;
                }
                elseif (isset($attrs['src']) && !is_empty($attrs['src'])) {
                    $this->_src = $attrs['src'];
                }

                if (isset($attrs['controls']) && !is_empty($attrs['controls'])) {
                    $this->_controls = $attrs['controls'];
                }
                if (isset($attrs['autoplay']) && !is_empty($attrs['autoplay'])) {
                    $this->_autoplay = $attrs['autoplay'];
                }
                if (isset($attrs['loop']) && !is_empty($attrs['loop'])) {
                    $this->_loop = $attrs['loop'];
                }
                if (isset($attrs['muted']) && !is_empty($attrs['muted'])) {
                    $this->_muted = $attrs['muted'];
                }
                if (isset($attrs['preload']) && !is_empty($attrs['preload'])) {
                    $this->_controls = $attrs['preload'];
                }
            }

            parent::__construct($altcontents, $attrs);
            if (!$this->is_ok())
            {
		$this->trigger_error();
            }
            $this->dbgecho($proc.'leaving('.$src.')', DEBUG_PROC_TRACE);
        }

	function emit_str($tag_attrs='', $flags=0)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
            $this->clear_error();
			
            $attrs = (isset($this->_src)?' src="'.$this->_src.'"':'').
		 (isset($this->_controls)?(($this->_controls)?' controls':''):'').
		 (isset($this->_autoplay)?(($this->_autoplay)?' autoplay':''):'').
		 (isset($this->_loop)?(($this->_loop)?' loop':''):'').
		 (isset($this->_muted)?(($this->_muted)?' muted':''):'').
                        (isset($this->_preload)?(($this->_preload)?' preload':''):'');
			
            $str = parent::emit_str($attrs.$tag_attrs, $flags)."\n";
			
            $this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($str);
	}
        
	function src($new=null)
	{
            if (isset($new))
            {
            	$this->_src = $new;
            }
            return $this->_src;
	}
		
	function controls($new=null)
	{
            if (isset($new))
            {
            	$this->_controls = ($new?true:false);
            }
            return $this->_controls;
	}
		
	function autoplay($new=null)
	{
            if (isset($new))
            {
            	$this->_autoplay = ($new?true:false);
            }
            return $this->_autoplay;
	}
		
	function loop($new=null)
	{
            if (isset($new))
            {
            	$this->_loop = ($new?true:false);
            }
            return $this->_loop;
	}
		
	function muted($new=null)
	{
            if (isset($new))
            {
            	$this->_muted = ($new?true:false);
            }
            return $this->_muted;
	}
		
	function preload($new=null)
	{
            if (isset($new))
            {
            	$this->_preload = ($new?true:false);
            }
            return $this->_preload;
	}
        
        function add_source($media_source)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
            $this->clear_error();
            
            // verify that $media_source is a htmlsource object
            if (!is_a($media_source, 'htmlsource', false))
            {
                $this->set_error('Attempt to add media source that is not of class htmlsource.', __FILE__, __LINE__);
            }
            else
            {
                // check for existing contents matching this media type and remove it if found
                $mediatype = $media_source->type();
                $this->push_mode(NO_TRIGGER_ON_ERROR);   // ensure we don't die on errors while searching the contents
                $idx = 0;
                while (($item = $this->get_contents($idx)) != false)
                {
                    if (is_a($item, 'htmlsource'))
                    {
                        if ($item->type() == $mediatype)
                        {
                            $this->clear_contents($idx);
                            break;
                        }
                    }
                    $idx++;
                }
                $this->clear_error();
                $this->pop_mode();
                
                // now add this media source to the contents
                $this->set_contentsx($media_source, $this->num_contents());
            }
			
            $this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok();
        }
        
        function get_source($type)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$type.')entering', DEBUG_PROC_TRACE);
            $this->clear_error();
            
            $ret = null;
            
            // check for existing contents matching this media type
            $this->push_mode(NO_TRIGGER_ON_ERROR);   // ensure we don't die on errors while searching the contents
            $idx = 0;
            while (($item = $this->get_contents($idx)) != false)
            {
                if (is_a($item, 'htmlsource'))
                {
                    if ($item->type() == $mediatype)
                    {
                        $ret = $item;
                        break;
                    }
                }
                $idx++;
            }
            $this->clear_error();
            $this->pop_mode();
			
            $this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($ret);
        }

        function remove_source($type)
        {
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'('.$type.')entering', DEBUG_PROC_TRACE);
            $this->clear_error();

            $removed = false;
            
            // check for existing contents matching this media type
            $this->push_mode(NO_TRIGGER_ON_ERROR);   // ensure we don't die on errors while searching the contents
            $idx = 0;
            while (($item = $this->get_contents($idx)) != false)
            {
                if (is_a($item, 'htmlsource'))
                {
                    if ($item->type() == $mediatype)
                    {
                        $removed = $this->clear_contents($idx);
                        break;
                    }
                }
                $idx++;
            }
            $this->clear_error();
            $this->pop_mode();
			
            $this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($removed);
        }
    }

    class htmlvideo extends htmlaudio
    {
        public $_width;
        public $_height;
        protected $_poster;     

	var $_tag = 'video';
        
	function __construct($src=null, $altcontents=null, $width=null, $height=null, $poster=null, $attrs=null)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering('.$src.')', DEBUG_PROC_TRACE);
            
            // check for member values in the attributes passed
            if (is_array($attrs))
            {
                if (!is_empty($attrs['width'])) {
                    $this->_width = $attrs['width'];
                }
                if (!is_empty($attrs['height'])) {
                    $this->_height = $attrs['height'];
                }
                if (!is_empty($attrs['poster'])) {
                    $this->_poster = $attrs['poster'];
                }
            }
            // give preference to member values passed as parameters
            if (!is_empty($width)) {
                $this->_width = $width;
            }
            if (!is_empty($height)) {
                $this->_height = $height;
            }
            if (!is_empty($poster)) {
                $this->_poster = $poster;
            }

            parent::__construct($altcontents, $attrs);
            if (!$this->is_ok())
            {
		$this->trigger_error();
            }
            $this->dbgecho($proc.'leaving('.$src.')', DEBUG_PROC_TRACE);
        }

	function emit_str($tag_attrs='', $flags=0)
	{
            $proc = __CLASS__.'::'.__FUNCTION__.': ';
            $this->dbgecho($proc.'entering', DEBUG_PROC_TRACE);
            $this->clear_error();
			
            $attrs = (isset($this->_width)?' width="'.$this->_width.'"':'').
		 (isset($this->_height)?' height="'.$this->_height.'"':'').
		 (isset($this->_poster)?' poster="'.$this->_poster.'"':'');
			
            $str = parent::emit_str($attrs.$tag_attrs, $flags)."\n";
			
            $this->dbgecho($proc.'leaving - '.($this->is_ok()?'succeeded':'failed'), DEBUG_PROC_TRACE);
            return $this->is_ok($str);
	}
        
	function width($new=null)
	{
            if (isset($new))
            {
            	$this->_width = $new;
            }
            return $this->_width;
	}
		
	function height($new=null)
	{
            if (isset($new))
            {
            	$this->_height = $new;
            }
            return $this->_height;
	}
		
	function poster($new=null)
	{
            if (isset($new))
            {
            	$this->_poster = $new;
            }
            return $this->_poster;
	}
    }

