<?php
// No direct access.
defined('_JEXEC') or die;
ini_set('max_execution_time', '120');
require_once (JPATH_COMPONENT_ADMINISTRATOR . "/helper/pjclass.php");
jimport('joomla.application.component.controlleradmin');
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.path');
jimport('joomla.filesystem.archive');
jimport('joomla.installer.installer');
jimport('joomla.version');

class PJInstallerControllerPrimary extends JControllerAdmin
{
    public function __construct($config = array())
    {
        parent::__construct($config);
        $this->registerTask('send_data', 'recieve_data');
    }
    public function recieve_data()
    {
        $prod_code = JRequest::getVar('pcode');
        $curl_opt = 'autodetect';//JRequest::getVar('curlopt');

        if (is_dir(JPATH_ROOT . "/tmp")) {
            if (is_writable(JPATH_ROOT . "/tmp")) {
                $GLOBALS['tmp_path'] = JPATH_ROOT . "/tmp";
            } else {
                if (JPath::canChmod(JPATH_ROOT . "/tmp")) {
                    JPath::setPermissions(JPATH_ROOT . "/tmp");
                    $GLOBALS['tmp_path'] = JPATH_ROOT . "/tmp";
                }
            }
        } else {
            JFolder::create(JPATH_ROOT . "/tmp");
            $GLOBALS['tmp_path'] = JPATH_ROOT . "/tmp";
        }
        $message = "";
        $pass = JURI::root() . 'administrator/components/com_pjinstaller/images/ok.png';
        $fail = JURI::root() . 'administrator/components/com_pjinstaller/images/notok.png';
        $info = JURI::root() . 'administrator/components/com_pjinstaller/images/info.png';
        JFolder::create($GLOBALS['tmp_path'] . "/packed");
        JFolder::create($GLOBALS['tmp_path'] . "/unpacked");
        $firewall = $this->firewall_check();
        if ($firewall == "OK") {
            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 = $this->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 = $this->unpack_arc();
                    if ($arc_status == "TRUE") {
                        $message .= '<tr><td class="pjis-title">EXTRACT:</td><td class="pjis-pass">&nbsp;</td></tr>';
                        $in_status = $this->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 = $this->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 = $this->unpack_arc();
                        if ($arc_status == "TRUE") {
                            $message .= '<tr><td class="pjis-title">EXTRACT:</td><td class="pjis-pass">&nbsp;</td></tr>';
                            $in_status = $this->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 = $this->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 = $this->unpack_arc();
                        if ($arc_status == "TRUE") {
                            $message .= '<tr><td class="pjis-title">EXTRACT:</td><td class="pjis-pass">&nbsp;</td></tr>';
                            $in_status = $this->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>";
                    }
                }
            }
        } else {
            $message = $firewall;
        }
        JFolder::delete($GLOBALS['tmp_path'] . "/packed");
        JFolder::delete($GLOBALS['tmp_path'] . "/unpacked");

        $view = $this->getView('primary', 'html');
        $view->message = $message;
        $view->display();


    }
    function firewall_check()
    {
        $tmp = $GLOBALS['tmp_path'];
        if ($this->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 (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>";
        }
        return $option;
    }
    function download_arc($prod_code, $method = "curl")
    {
        $proto = 'aHR0cDovL3d3dy5wcm9qb29tLmNvbS9zaG9wL2dldC5waHA/YWN0aW9uPWwxJmQ9';
        $r_dn = base64_encode(JURI::root());
        switch ($method) {
            case "curl":
                {
                    $get_arc = $this->get_arc_curl(base64_decode($proto) . $prod_code . "&r=" . $r_dn);
                    break;
                }
            case "curlemu":
                {
                    $get_arc = $this->get_arc_curlemu(base64_decode($proto) . $prod_code . "&r=" . $r_dn);
                    break;
                }
        }
        if (!empty($get_arc)) {
            if (JFile::write($GLOBALS['tmp_path'] . "/packed/install.zip", $get_arc)) {
                $status = "TRUE";
            } else {
                $status = "FALSE";
            }
        } 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 (is_file($GLOBALS['tmp_path'] . "/packed/install.zip")) {
            if (JArchive::extract($GLOBALS['tmp_path'] . "/packed/install.zip", $GLOBALS['tmp_path'] . "/unpacked")) {
                $status = "TRUE";
            } else {
                $status = "FALSE";
            }
        } else {
            $status = "FALSE";
        }
        return $status;
    }
    function install_pkg()
    {
        $installer = new JInstaller();
        $installer->setOverwrite("true");
        if ($installer->install($GLOBALS['tmp_path'] . "/unpacked")) {
            $status = "TRUE";
        } else {
            $status = "FALSE";
        }
        return $status;
    }
}