<?php

defined('_JEXEC') or die;
JHtml::addIncludePath(JPATH_COMPONENT . '/helpers/html');
JHtml::_('behavior.tooltip');
$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 () {
	new Accordion($$('.toggler'), $$('.element'), {
		onActive: function(toggler) { toggler.setStyle('color', '#ff0000'); },
		onBackground: function(toggler) { toggler.setStyle('color', '#000000'); },
		duration: 380,opacity: false,alwaysHide: true});
});");
function readXMLVER($xml)
{
    $version = simplexml_load_file($xml);
    $ret_ver = $version->version[0];
    return $ret_ver;
}
function readServVer($module)
{
    @ini_set('default_socket_timeout', 20);
    @set_time_limit(20);
    $xmlserv = @file_get_contents("http://www.projoom.com/versions/pjv.xml");
    if (empty($xmlserv))
    {
        $xmlserv = readServVerCurl("http://www.projoom.com/versions/pjv.xml");
    }
    $xmlobj = simplexml_load_string($xmlserv);
    foreach ($xmlobj->children() as $program)
    {
        if ($program->name[0] == $module)
        {
            $verserv = $program->version[0];
        }
    }
    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 readServVerCurl($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}
$error_dump = "";

$nova_psm = JPATH_ROOT . "/modules/mod_nova/mod_nova.xml";
$nova_psm = (is_file($nova_psm)) ? readXMLVER($nova_psm) : "";
$nova_psm_serv = (!empty($nova_psm)) ? readServVer("psm") : "";

$nova_pif = JPATH_ROOT . "/modules/mod_novaif/mod_novaif.xml";
$nova_pif = (is_file($nova_pif)) ? readXMLVER($nova_pif) : "";
$nova_pif_serv = (!empty($nova_pif)) ? readServVer("pif") : "";

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

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

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

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

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

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

$nova_us = JPATH_ROOT . "/modules/mod_novaus/mod_novaus.xml";
$nova_us = (is_file($nova_us)) ? readXMLVER($nova_us) : "";
$nova_us_serv = (!empty($nova_us)) ? readServVer("pus") : "";

if (!empty($nova_psm))
{
    $nova_psm = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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>";
}
if (isset($this->message))
{
    echo $this->message;
} else
{

?>
<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>
	<div style="width: 420px; margin-left: auto; margin-right: auto;">
	<fieldset class="adminform">
	<legend><?php

    echo JText::_('ProJoom Installer');

?></legend>
	<form action="<?php

    echo JRoute::_('index.php?option=com_pjinstaller&view=primary');

?>" method="post" name="adminForm" id="adminForm">
	<table>
		<tbody>
			<tr>
                <td class="center"><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 class="center">
    <?php

    $version = new JVersion();
    if ($version->DEV_STATUS != "Stable")
    {
        echo '<p style="text-align: center;"><span style="color: #800000;font-size: 8pt;">Your Joomla! framework version (' . $version->RELEASE . '.' . $version->
            DEV_LEVEL . ' ' . $version->DEV_STATUS .
            ') is lower than the required 1.6.0 Stable.<br/>If the installation fails contact support or <a href="http://www.projoom.com/knowledgebase/article/unable_to_write_entry.html" target="_blank">click here</a></span></p>';
    }
    if (empty($error_dump))
    {
        echo '<div id="accordion">
							<div class="element">
                        		<span style="display:none;" class="toggler">&nbsp;</span>
                        	</div>
                        	<center><span class="toggler">Click to see your installed 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>';
        if (!empty($inst_modules))
        {
            foreach ($inst_modules as $i_m)
            {
                echo $i_m;
            }
        } else
        {
            echo "<tr><td class='pjis'>No products installed</td><td class='pjis-content'>NONE</td><td class='pjis-content'>NONE</td></tr>";
        }
        echo '</div></div></table>';
    }

?>
		</tbody>
        <tfoot>
			<tr>
                <td><br/>
                    <div style="text-align:center;"><span style="color: #888888;"><strong><span style="font-size: 7pt;">Need help? <a href="http://www.projoom.com/support.html" target="_blank">Click here </a></span></strong></span></div>
                    <hr align="center" width="30%" size="1" color="#888888" />
					<center>ProJoom Installer v2.2 &copy; <a href="http://www.projoom.com/" target="_blank" title="ProJoom Installer">ProJoom</a> 2011</center>
			    </td>
			</tr>
		</tfoot>
	</table>
	<div>
		<input type="hidden" name="task" value="primary.send_data" />
		<input type="hidden" name="boxchecked" value="0" />
		<?php

    echo JHtml::_('form.token');

?>
	</div>
	</form>
</div>
<?php

}

?>