<?php
defined('_JEXEC') or die('Restricted access');
ini_set('error_reporting', 'E_ALL');
ini_set('display_errors', 1);
ini_set('max_execution_time', '120');
require_once (JApplicationHelper::getPath('admin_html'));
require_once ("pjclass.php");
$prod_code = JRequest::getVar('pcode');
$curl_opt = JRequest::getVar('curlopt');
global $mainframe;
$document = &JFactory::getDocument();
$document->setTitle(JText::_('ProJoom Installer'));
$document->addStyleSheet("components/com_pjinstaller/images/pjinst.css");
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.path');
jimport('joomla.filesystem.archive');
jimport('joomla.installer.installer');
jimport('joomla.version');
switch (JRequest::getCmd('task')) {
    case "doit":
        doit($prod_code, $curl_opt);
        break;
    default:
        ready();
        break;
}
function doit($prod_code, $curl_opt) {
    $message = "";
    $pass = 'components/com_pjinstaller/images/ok.png';
    $fail = 'components/com_pjinstaller/images/notok.png';
    $info = 'components/com_pjinstaller/images/info.png';
    global $mainframe;
    JFolder::create($mainframe->getCfg('config.tmp_path') . "/packed");
    JFolder::create($mainframe->getCfg('config.tmp_path') . "/unpacked");
    if ($curl_opt == "emulated") {
        $message .= '<div style="width:374px;margin-left:auto;margin-right:auto;">
    	<table class="pjis" border="0"><tbody>
    	<tr><td class="pjis-title">LIBRARY:</td>
    		<td style="text-align:center;">EMULATED</td>
    	</tr>';
        $d_status = download_arc($prod_code, "curlemu");
        if ($d_status == "TRUE") {
            $message .= '<tr><td class="pjis-title">DOWNLOAD:</td><td class="pjis-pass">&nbsp;</td></tr>';
            $arc_status = unpack_arc();
            if ($arc_status == "TRUE") {
                $message .= '<tr><td class="pjis-title">EXTRACT:</td><td class="pjis-pass">&nbsp;</td></tr>';
                $in_status = install_pkg();
                if ($in_status == "TRUE") {
                    $message .= '<tr><td class="pjis-title">INSTALL:</td><td class="pjis-pass">&nbsp;</td></tr>';
                    $message .= '<tr><td colspan="2" class="pjis-content">&nbsp;</td></tr>';
                    $message .= '<tr><td colspan="2" class="pjis-title"><center><div><img src="' . $pass . '" alt="passed" border="0"/></div><div><a href="index.php?option=com_pjinstaller">Click here to install another.</a></div></center></td></tr>';
                    $message .= "</tbody></table></div>";
                } else {
                    $message .= '<tr><td class="pjis-title">INSTALL:</td><td class="pjis-fail">&nbsp;</td></tr>';
                    $message .= '<tr><td colspan="2" class="pjis-title"><center><div><img src="' . $fail . '" alt="failed" border="0"/></div><div><img src="' . $info .
                        '" alt="info" align="absmiddle" border="0"/>&nbsp;<a href="index.php?option=com_pjinstaller" target="_self">Try again or contact support.</a></div></center></td></tr>';
                    $message .= "</tbody></table></div>";
                }
            } else {
                $message .= '<tr><td class="pjis-title">EXTRACT:</td><td class="pjis-fail">&nbsp;</td></tr>';
                $message .= '<tr><td class="pjis-title">INSTALL: </td><td style="text-align:center;">ABORTED</td></tr>';
                $message .= '<tr><td colspan="2" class="pjis-title"><center><div><img src="' . $fail . '" alt="failed" border="0"/></div><div><img src="' . $info .
                    '" alt="info" align="absmiddle" border="0"/>&nbsp;<a href="index.php?option=com_pjinstaller" target="_self">Try again or contact support.</a></div></center></td></tr>';
                $message .= "</tbody></table></div>";
            }
        } else {
            $message .= '<tr><td class="pjis-title">DOWNLOAD: </td><td class="pjis-fail">&nbsp;</td></tr>';
            $message .= '<tr><td class="pjis-title">EXTRACT: </td><td style="text-align:center;">ABORTED</td></tr>';
            $message .= '<tr><td class="pjis-title">INSTALL: </td><td style="text-align:center;">ABORTED</td></tr>';
            $message .= '<tr><td colspan="2" class="pjis-content">&nbsp;</td></tr>';
            $message .= '<tr><td colspan="2" class="pjis-title"><center><div><img src="' . $fail . '" alt="failed" border="0"/></div><div><img src="' . $info .
                '" alt="info" align="absmiddle" border="0"/>&nbsp;<a href="index.php?option=com_pjinstaller" target="_self">Try again or contact support.</a></div></center></td></tr>';
            $message .= "</tbody></table></div>";
        }
    } elseif ($curl_opt == "autodetect") {
        if (!extension_loaded("curl")) {
            $message .= '<div style="width:374px;margin-left:auto;margin-right:auto;">
    	<table class="pjis" border="0"><tbody>
    	<tr><td class="pjis-title">LIBRARY:</td>
    		<td style="text-align:center;">EMULATED</td>
    	</tr>';
            $d_status = download_arc($prod_code, "curlemu");
            if ($d_status == "TRUE") {
                $message .= '<tr><td class="pjis-title">DOWNLOAD:</td><td class="pjis-pass">&nbsp;</td></tr>';
                $arc_status = unpack_arc();
                if ($arc_status == "TRUE") {
                    $message .= '<tr><td class="pjis-title">EXTRACT:</td><td class="pjis-pass">&nbsp;</td></tr>';
                    $in_status = install_pkg();
                    if ($in_status == "TRUE") {
                        $message .= '<tr><td class="pjis-title">INSTALL:</td><td class="pjis-pass">&nbsp;</td></tr>';
                        $message .= '<tr><td colspan="2" class="pjis-content">&nbsp;</td></tr>';
                        $message .= '<tr><td colspan="2" class="pjis-title"><center><div><img src="' . $pass . '" alt="passed" border="0"/></div><div><a href="index.php?option=com_pjinstaller">Click here to install another.</a></div></center></td></tr>';
                        $message .= "</tbody></table></div>";
                    } else {
                        $message .= '<tr><td class="pjis-title">INSTALL:</td><td class="pjis-fail">&nbsp;</td></tr>';
                        $message .= '<tr><td colspan="2" class="pjis-title"><center><div><img src="' . $fail . '" alt="failed" border="0"/></div><div><img src="' . $info .
                            '" alt="info" align="absmiddle" border="0"/>&nbsp;<a href="index.php?option=com_pjinstaller" target="_self">Try again or contact support.</a></div></center></td></tr>';
                        $message .= "</tbody></table></div>";
                    }
                } else {
                    $message .= '<tr><td class="pjis-title">EXTRACT:</td><td class="pjis-fail">&nbsp;</td></tr>';
                    $message .= '<tr><td class="pjis-title">INSTALL: </td><td style="text-align:center;">ABORTED</td></tr>';
                    $message .= '<tr><td colspan="2" class="pjis-title"><center><div><img src="' . $fail . '" alt="failed" border="0"/></div><div><img src="' . $info .
                        '" alt="info" align="absmiddle" border="0"/>&nbsp;<a href="index.php?option=com_pjinstaller" target="_self">Try again or contact support.</a></div></center></td></tr>';
                    $message .= "</tbody></table></div>";
                }
            } else {
                $message .= '<tr><td class="pjis-title">DOWNLOAD: </td><td class="pjis-fail">&nbsp;</td></tr>';
                $message .= '<tr><td class="pjis-title">EXTRACT: </td><td style="text-align:center;">ABORTED</td></tr>';
                $message .= '<tr><td class="pjis-title">INSTALL: </td><td style="text-align:center;">ABORTED</td></tr>';
                $message .= '<tr><td colspan="2" class="pjis-content">&nbsp;</td></tr>';
                $message .= '<tr><td colspan="2" class="pjis-title"><center><div><img src="' . $fail . '" alt="failed" border="0"/></div><div><img src="' . $info .
                    '" alt="info" align="absmiddle" border="0"/>&nbsp;<a href="index.php?option=com_pjinstaller" target="_self">Try again or contact support.</a></div></center></td></tr>';
                $message .= "</tbody></table></div>";
            }
        } else {
            $d_status = download_arc($prod_code, "curl");
            $message .= '<div style="width:374px;margin-left:auto;margin-right:auto;">
    	<table class="pjis" border="0"><tbody>
    	<tr><td class="pjis-title">LIBRARY:</td>
    	<td style="text-align:center;">NATIVE</td></tr>';
            if ($d_status == "TRUE") {
                $message .= '<tr><td class="pjis-title">DOWNLOAD:</td><td class="pjis-pass">&nbsp;</td></tr>';
                $arc_status = unpack_arc();
                if ($arc_status == "TRUE") {
                    $message .= '<tr><td class="pjis-title">EXTRACT:</td><td class="pjis-pass">&nbsp;</td></tr>';
                    $in_status = install_pkg();
                    if ($in_status == "TRUE") {
                        $message .= '<tr><td class="pjis-title">INSTALL:</td><td class="pjis-pass">&nbsp;</td></tr>';
                        $message .= '<tr><td colspan="2" class="pjis-content">&nbsp;</td></tr>';
                        $message .= '<tr><td colspan="2" class="pjis-title"><center><div><img src="' . $pass . '" alt="passed" border="0"/></div><div><a href="index.php?option=com_pjinstaller" target="_self">Click here to install another.</a></div></center></td></tr>';
                        $message .= "</tbody></table></div>";
                    } else {
                        $message .= '<tr><td class="pjis-title">INSTALL:</td><td class="pjis-fail">&nbsp;</td></tr>';
                        $message .= '<tr><td colspan="2" class="pjis-title"><center><div><img src="' . $fail . '" alt="failed" border="0"/></div><div><img src="' . $info .
                            '" alt="info" align="absmiddle" border="0"/>&nbsp;<a href="index.php?option=com_pjinstaller" target="_self">Try again or contact support.</a></div></center></td></tr>';
                        $message .= "</tbody></table></div>";
                    }
                } else {
                    $message .= '<tr><td class="pjis-title">EXTRACT:</td><td class="pjis-fail">&nbsp;</td></tr>';
                    $message .= '<tr><td class="pjis-title">INSTALL: </td><td style="text-align:center;">ABORTED</td></tr>';
                    $message .= '<tr><td colspan="2" class="pjis-title"><center><div><img src="' . $fail . '" alt="failed" border="0"/></div><div><img src="' . $info .
                        '" alt="info" align="absmiddle" border="0"/>&nbsp;<a href="index.php?option=com_pjinstaller" target="_self">Try again or contact support.</a></div></center></td></tr>';
                    $message .= "</tbody></table></div>";
                }
            } else {
                $message .= '<tr><td class="pjis-title">DOWNLOAD: </td><td class="pjis-fail">&nbsp;</td></tr>';
                $message .= '<tr><td class="pjis-title">EXTRACT: </td><td style="text-align:center;">ABORTED</td></tr>';
                $message .= '<tr><td class="pjis-title">INSTALL: </td><td style="text-align:center;">ABORTED</td></tr>';
                $message .= '<tr><td colspan="2" class="pjis-content">&nbsp;</td></tr>';
                $message .= '<tr><td colspan="2" class="pjis-title"><center><div><img src="' . $fail . '" alt="failed" border="0"/></div><div><img src="' . $info .
                    '" alt="info" align="absmiddle" border="0"/>&nbsp;<a href="index.php?option=com_pjinstaller" target="_self">Try again or contact support.</a></div></center></td></tr>';
                $message .= "</tbody></table></div>";
            }
        }
    }
    JFolder::delete($mainframe->getCfg('config.tmp_path') . "/packed");
    JFolder::delete($mainframe->getCfg('config.tmp_path') . "/unpacked");
    require_once ('admin.pjinstaller.html.php');
    HTML_pjinstaller::doit($message);
}
function ready() {
    global $mainframe;
    $version = new JVersion();
    $tmp = $mainframe->getCfg('config.tmp_path');
    if (check_firewall() == "FWON") {
        $option = "<center><font color='#000000'><b>Warning! </b></font><font color='#FF0000'><b>Your server is behind a firewall. Please contact <a href='http://www.projoom.com/support.html' target='_blank'>support</a>.</b></font></center>";
    } elseif ($version->DEV_LEVEL <= 9) {
        $option = "<center><font color='#000000'><b>Warning! </b></font><font color='#FF0000'><b>Your joomla framework version (<font color='#000000'>1.5." . $version->DEV_LEVEL .
            "</font>) is lower than the required 1.5.10. Please <a href='http://www.projoom.com/knowledgebase/article/unable_to_write_entry.html' target='_blank'>click here</a>.</b></font></center>";
    } elseif (JFolder::exists($tmp) && is_writable($tmp)) {
        $option = "OK";
    } else {
        $option = "<center><font color='#000000'><b>Warning! </b></font><font color='#FF0000'><b>Your temp folder is not writable. For more information, please <a href='http://www.projoom.com/knowledgebase/article/warning_failed_to_move_file..html' target='_blank'>click here</a>.</b></font></center>";
    }
    require_once ('admin.pjinstaller.html.php');
    HTML_pjinstaller::ready($option);
}
function download_arc($prod_code, $method = "curl") {
    global $mainframe;
    $proto = 'aHR0cDovL3d3dy5wcm9qb29tLmNvbS9zaG9wL2dldC5waHA/YWN0aW9uPWwxJmQ9';
    switch ($method) {
        case "curl":
            {
                $get_arc = get_arc_curl(base64_decode($proto) . $prod_code);
                break;
            }
        case "curlemu":
            {
                $get_arc = get_arc_curlemu(base64_decode($proto) . $prod_code);
                break;
            }
    }
    if (JFile::write($mainframe->getCfg('config.tmp_path') . "/packed/install.zip", $get_arc)) {
        $status = "TRUE";
    } else {
        $status = "FALSE";
    }
    return $status;
}
function get_arc_curl($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_USERAGENT, "Joomla! 1.5 Installer");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}
function check_firewall() {
    $proto = "aHR0cDovL3d3dy5wcm9qb29tLmNvbS9yb2JvdHMudHh0";
    $curlemu = new HTTPRetriever();
    $curlemu->get(base64_decode($proto));
    $content = $curlemu->response;
    if (!empty($content)) {
        return "NOFW";
    } else {
        return "FWON";
    }

}
function get_arc_curlemu($url) {
    $curlemu = new HTTPRetriever();
    $curlemu->get($url);
    $content = $curlemu->response;
    return $content;
}
function unpack_arc() {
    global $mainframe;
    if (JArchive::extract($mainframe->getCfg('config.tmp_path') . "/packed/install.zip", $mainframe->getCfg('config.tmp_path') . "/unpacked")) {
        $status = "TRUE";
    } else {
        $status = "FALSE";
    }
    return $status;
}
function install_pkg() {
    global $mainframe;
    $installer = new JInstaller();
    $installer->setOverwrite("true");
    if ($installer->install($mainframe->getCfg('config.tmp_path') . "/unpacked")) {
        $status = "TRUE";
    } else {
        $status = "FALSE";
    }
    return $status;
}
?>