<?php
defined('_JEXEC') or die('Restricted access');
global $mainframe;
$document = &JFactory::getDocument();
$document->setTitle(JText::_('ProJoom Installer'));
$document->addStyleSheet("components/com_pjinstaller/images/pjinst.css");
$document->addStyleDeclaration(".toggler { cursor:pointer; font-weight:bold; font-size:10px; }
.element { padding:0; }");
$document->addScriptDeclaration("window.addEvent('domready', function() {
	var accordion = new Accordion($$('.toggler'),$$('.element'), {
		opacity: 0,
		onActive: function(toggler) { toggler.setStyle('color', '#ff0000'); },
		onBackground: function(toggler) { toggler.setStyle('color', '#000000'); }
	});
});");
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.path');
jimport('joomla.filesystem.archive');
class HTML_pjinstaller {
	
    function readXMLVER($xml) {
        $version = new JSimpleXML;
        $version->loadFile($xml);
        $ret_ver = $version->document->version[0]->data();
        return $ret_ver;

    }
    function readServVer($xmlobj, $module) {
        foreach ($xmlobj->document->children() as $program) {
            if ($program->name[0]->data() == $module) {
                $verserv = $program->version[0]->data();
            }
        }
        return $verserv;
    }
    function checkVer($internal, $external){
    	$orig = $internal;
    	$internal = explode(".", $internal);
    	$external = explode(".", $external);
        $critical = ($internal[0] == $external[0]) ? false : true;
        $higher = ($internal[1] == $external[1]) ? false : true;
        $normal = ($internal[2] == $external[2]) ? false : true;
        if($critical == true){
        	return '<span style="font-weight: bold; color: red;">'.$orig.'</span>';
        } elseif ($higher == true){
        	return '<span style="font-weight: bold; color: red;">'.$orig.'</span>';
        } elseif ($normal == true){
        	return '<span style="font-weight: bold; color: red;">'.$orig.'</span>';
        } else {
        	return '<span style="color: green;">'.$orig.'</span>';
        }
    }
    function ready($option) {
        if($option == "OK") {
			$error_dump = "";
        	@ini_set('default_socket_timeout', 20);
        	@set_time_limit(20);
            $xmlserv = @file_get_contents("http://www.projoom.com/versions/pjv.xml");
            if (!empty($xmlserv)){
        	$xmlobj = new JSimpleXML;
        	$xmlobj->loadString($xmlserv);
            
            $nova_psm = JPATH_ROOT . "/modules/mod_nova/mod_nova.xml";
            $nova_psm = (is_file($nova_psm)) ? HTML_pjinstaller::readXMLVER($nova_psm) : "";
            $nova_psm_serv = (!empty($nova_psm)) ? HTML_pjinstaller::readServVer($xmlobj, "psm") : "";
            
            $nova_pif = JPATH_ROOT . "/modules/mod_novaif/mod_novaif.xml";
            $nova_pif = (is_file($nova_pif)) ? HTML_pjinstaller::readXMLVER($nova_pif) : "";
            $nova_pif_serv = (!empty($nova_pif)) ? HTML_pjinstaller::readServVer($xmlobj, "pif") : "";

            $nova_pmr = JPATH_ROOT . "/modules/mod_novarp/mod_novarp.xml";
            $nova_pmr = (is_file($nova_pmr)) ? HTML_pjinstaller::readXMLVER($nova_pmr) : "";
            $nova_pmr_serv = (!empty($nova_pmr)) ? HTML_pjinstaller::readServVer($xmlobj, "mrp") : "";

            $nova_ppk = JPATH_ROOT . "/modules/mod_novapc/mod_novapc.xml";
            $nova_ppk = (is_file($nova_ppk)) ? HTML_pjinstaller::readXMLVER($nova_ppk) : "";
            $nova_ppk_serv = (!empty($nova_ppk)) ? HTML_pjinstaller::readServVer($xmlobj, "ppk") : "";

            $nova_ds = JPATH_ROOT . "/modules/mod_novashow/mod_novashow.xml";
            $nova_ds = (is_file($nova_ds)) ? HTML_pjinstaller::readXMLVER($nova_ds) : "";
            $nova_ds_serv = (!empty($nova_ds)) ? HTML_pjinstaller::readServVer($xmlobj, "pds") : "";

            $nova_ss = JPATH_ROOT . "/modules/mod_novass/mod_novass.xml";
            $nova_ss = (is_file($nova_ss)) ? HTML_pjinstaller::readXMLVER($nova_ss) : "";
            $nova_ss_serv = (!empty($nova_ss)) ? HTML_pjinstaller::readServVer($xmlobj, "pss") : "";

            $nova_sfh = JPATH_ROOT . "/modules/mod_novasfh/mod_novasfh.xml";
            $nova_sfh = (is_file($nova_sfh)) ? HTML_pjinstaller::readXMLVER($nova_sfh) : "";
            $nova_sfh_serv = (!empty($nova_sfh)) ? HTML_pjinstaller::readServVer($xmlobj, "sfh") : "";

            $nova_map = JPATH_ROOT . "/modules/mod_nmap/mod_nmap.xml";
            $nova_map = (is_file($nova_map)) ? HTML_pjinstaller::readXMLVER($nova_map) : "";
            $nova_map_serv = (!empty($nova_map)) ? HTML_pjinstaller::readServVer($xmlobj, "pmap") : "";

            $nova_us = JPATH_ROOT . "/modules/mod_novaus/mod_novaus.xml";
            $nova_us = (is_file($nova_us)) ? HTML_pjinstaller::readXMLVER($nova_us) : "";
            $nova_us_serv = (!empty($nova_us)) ? HTML_pjinstaller::readServVer($xmlobj, "pus") : "";
            
            if(!empty($nova_psm)){
            	$nova_psm = HTML_pjinstaller::checkVer($nova_psm, $nova_psm_serv);
            	$inst_modules[] = "<tr><td class='pjis'>Pro Sticky Message</td><td class='pjis-content'>".$nova_psm."</td><td class='pjis-content'>".$nova_psm_serv."</td></tr>";
            }
            if(!empty($nova_pif)){
            	$nova_pif = HTML_pjinstaller::checkVer($nova_pif, $nova_pif_serv);
            	$inst_modules[] = "<tr><td class='pjis'>Pro Image Flow</td><td class='pjis-content'>".$nova_pif."</td><td class='pjis-content'>".$nova_pif_serv."</td></tr>";
            }
            if(!empty($nova_pmr)){
            	$nova_pmr = HTML_pjinstaller::checkVer($nova_pmr, $nova_pmr_serv);
            	$inst_modules[] = "<tr><td class='pjis'>ProJoom Multi Rotator</td><td class='pjis-content'>".$nova_pmr."</td><td class='pjis-content'>".$nova_pmr_serv."</td></tr>";
            }
            if(!empty($nova_ppk)){
            	$nova_ppk = HTML_pjinstaller::checkVer($nova_ppk, $nova_ppk_serv);
            	$inst_modules[] = "<tr><td class='pjis'>Pro Perfect Karousel</td><td class='pjis-content'>".$nova_ppk."</td><td class='pjis-content'>".$nova_ppk_serv."</td></tr>";
            }
            if(!empty($nova_ds)){
            	$nova_ds = HTML_pjinstaller::checkVer($nova_ds, $nova_ds_serv);
            	$inst_modules[] = "<tr><td class='pjis'>ProJoom Deluxe Show</td><td class='pjis-content'>".$nova_ds."</td><td class='pjis-content'>".$nova_ds_serv."</td></tr>";
            }
            if(!empty($nova_ss)){
            	$nova_ss = HTML_pjinstaller::checkVer($nova_ss, $nova_ss_serv);
            	$inst_modules[] = "<tr><td class='pjis'>Pro Super Slider</td><td class='pjis-content'>".$nova_ss."</td><td class='pjis-content'>".$nova_ss_serv."</td></tr>";
            }
            if(!empty($nova_sfh)){
            	$nova_sfh = HTML_pjinstaller::checkVer($nova_sfh, $nova_sfh_serv);
            	$inst_modules[] = "<tr><td class='pjis'>Smart Flash Header</td><td class='pjis-content'>".$nova_sfh."</td><td class='pjis-content'>".$nova_sfh_serv."</td></tr>";
            }
            if(!empty($nova_map)){
            	$nova_map = HTML_pjinstaller::checkVer($nova_map, $nova_map_serv);
            	$inst_modules[] = "<tr><td class='pjis'>Pro Magic Audio Player</td><td class='pjis-content'>".$nova_map."</td><td class='pjis-content'>".$nova_map_serv."</td></tr>";
            }
            if(!empty($nova_us)){
            	$nova_us = HTML_pjinstaller::checkVer($nova_us, $nova_us_serv);
            	$inst_modules[] = "<tr><td class='pjis'>Pro Ultra Spinner</td><td class='pjis-content'>".$nova_us."</td><td class='pjis-content'>".$nova_us_serv."</td></tr>";
            }
    		} else {
    			$error_dump .= "Could not retrieve xml file.";
    		}

?>
<script language="javascript" type="text/javascript">
	function submitbutton(pressbutton) {
		var form = document.adminForm;
		if (form.pcode.value == ""){
			alert( "<?php echo JText::_('Insert a product code!', true); ?>" );
		} else {
			form.submit();
		}
	}
</script>
<form enctype="multipart/form-data" action="index.php" method="post" name="adminForm">
	<div style="width:420px;margin-left:auto;margin-right:auto;">
		<div id="accordion">
			<div class="element">
				<span style="display:none;" class="toggler">&nbsp;</span>
			</div>
		<fieldset class="adminform">
			<legend><?php echo JText::_('ProJoom Installer'); ?></legend>
			<table class="admintable">
				<tbody>
				<tr>
					<td>
						<input class="inputbox" type="text" name="pcode" id="pcode" size="72" maxlength="90" value="" />&nbsp<input class="button" type="button" value="<?php echo JText::_('Install'); ?>" onclick="submitbutton()" />
					</td>
				</tr>
				<tr>
					<td>
						<p style="text-align: center;">Library: <input type="radio" name="curlopt" value="autodetect" checked="checked"/>Auto-Detect <input type="radio" name="curlopt" value="emulated" />Emulated</p>
					</td>
				</tr>
				<tr>
					<td>
						<?php 
						  if(empty($error_dump)){
						  	echo '<center><span class="toggler">Click here to see your products</span></center>
								  <div class="element">
								  		<table class="pjis" border="1" cellspacing="0" align="center">
											<tr>
												<th class="pjis-content">Product</th>
												<th class="pjis-content">Local Version</th>
												<th class="pjis-content">Newest Version</th>
											</tr>';
							foreach($inst_modules as $i_m){
								echo $i_m;
							}
							echo '		</table>
								<center><span class="toggler">Close</span></center>
								<div class="element"></div>
								</div>';
						  }
						?>
									
					<td>
				</tr>
	</tbody>
	<tfoot>
			<tr>
					<td colspan="13"><br/>
						<div style="text-align:center;"><span style="color: #888888;"><strong><span style="font-size: 7pt;">Need help? <a href="http://www.projoom.com/docs/installation" target="_blank">Click here </a></span></strong></span></div>
						<hr align="center" width="30%" size="1" color="#888888" noshade>
						<center>ProJoom Installer v1.0.6 © <a href="http://www.projoom.com/" target="_blank" title="ProJoom Installer">ProJoom</a> 2009</center>
					</td>
				</tr>
			</tfoot>
			</table>
		</fieldset>
	</div>
	</div>
	<div class="clr"></div>
	<input type="hidden" name="task" value="doit" />
	<input type="hidden" name="option" value="com_pjinstaller" />
	<?php echo JHTML::_('form.token'); ?>
</form>
		<?php
        } else {
            echo $option;
        }
    }
    function doit($done) {
        echo $done;
    }
}
?>