<?php
/**
 * Pro Super Slider
 *This program is free software: you can redistribute it and/or modify it under the terms
 *of the GNU General Public License as published by the Free Software Foundation,
 *either version 3 of the License, or (at your option) any later version.
 *
 *This program is distributed in the hope that it will be useful,
 *but WITHOUT ANY WARRANTY; without even the implied warranty of
 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *GNU General Public License for more details.
 *
 *You should have received a copy of the GNU General Public License
 *along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *@author ProJoom
 *@copyright (C) 2008 - 2012 ProJoom
 *@link http://www.projoom.com/extensions/pro-super-slider.html Official website
 **/
defined('_JEXEC') or die('Restricted access');
error_reporting(E_ALL ^ E_NOTICE);
jimport('joomla.html.html');
jimport('joomla.form.formfield');
class JFormFieldPJus extends JFormField{
    protected $type = 'PJus';
    protected function getInput(){
        global $mainframe; 
$cache_flush = 'w2b4u2w2c4';
        $name = $this->name;
        $value = $this->value;
        $id = $this->id;
        jimport('joomla.filesystem.file');
        jimport('joomla.filesystem.folder');
        jimport('joomla.filesystem.path');
        jimport('joomla.filesystem.archive');
        jimport('joomla.installer.installer');
        @ini_set('default_socket_timeout', 6);
        @set_time_limit(6);
        $path = 'modules/mod_novass/elements/';
        $config = simplexml_load_file(JPATH_ROOT . "/modules/mod_novass/mod_novass.xml");
        if (@count($config->files->children())) {
            unset($config->files);
            $save_file = $config->asXML();
            JFile::write(JPATH_ROOT . "/modules/mod_novass/mod_novass.xml", $save_file);
        }
        $tmp1 = explode(":", $value);
        $tmp2 = explode(".", $tmp1[1]);
        $ename = "pjus_" . $tmp1[0];
        $actvers = $tmp1[1];
        $mod_name = '';
        if (isset($_COOKIE[$ename . '_dox'])) {
        	if (!isset($_COOKIE[$ename . '_promo'])){
        		setcookie($ename . "_promo", "1", time() + (3600 * 24 * 365));
        	}
        	$cnt = $_COOKIE[$ename . '_promo'] + 1;
        	if ($cnt != 10){
        		setcookie($ename . "_promo", $cnt, time() + (3600 * 24 * 365));
        	} else {
        	setcookie($ename . "_promo", $cnt, time() + (3600 * 24 * 365));
            $promomsg = '<div style="width:350px;"><p><strong>If you use Pro Super Slider, please post a rating and a review at the <a href="http://extensions.joomla.org" target="_blank">Joomla! Extensions Directory.</a></strong></p></div>';
            $js = "window.addEvent('domready', function(){
    		var modal = new AscModal('$promomsg', 'n');
    		modal.show();
    		});";
            $path = 'modules/mod_novass/elements/';
            JHTML::_('behavior.mootools');
            JHTML::stylesheet('pjus.css', $path);
            JHTML::script('pjus.js', $path);
            $document = JFactory::getDocument();
            $document->addScriptDeclaration($js);
            setcookie($ename . "_promo", "-60", time() + (3600 * 24 * 365));
            }
        }
        if (isset($_COOKIE[$ename]))
		{
			$html = '<input style="color: #666666;font-weight: bold;" type="text" readonly="readonly" class="readonly" value="' . $actvers . '">';
			return $html;
		}
        $modver = array("A" => $tmp2[0], "B" => $tmp2[1], "C" => $tmp2[2]);
        if (false == ($xmlserv = @$this->xml_curl("http://www.projoom.com/versions/pjv.xml"))) {
            $html = '<input style="color: #666666;font-weight: bold;" type="text" readonly="readonly" class="readonly" value="' . $actvers .
                ' (local)">';
            return $html;
        }
        $xmlobj = simplexml_load_string($xmlserv);
        foreach ($xmlobj->children() as $program) {
            if ($program->name[0] == $tmp1[0]) {
                $verserv = $program->version[0];
            }
        }
        $verarray = explode(".", $verserv);
        $local = $modver["A"].$modver["B"].$modver["C"];
        $remote = $verarray[0].$verarray[1].$verarray[2];

        if (!isset($_COOKIE[$ename . '_dox'])) {
            $doxmsg = '<div style="width:350px;"><a target="_blank" href="http://www.projoom.com/docs/pss/"><img style="margin:6px;vertical-align:middle;" src="../modules/mod_novass/elements/images/dox.png" width="64" height="64"/></a><span><strong>For documentation, please <a target="_blank" href="http://www.projoom.com/docs/pss/">CLICK HERE</a></strong></span></div>';
            $js = "window.addEvent('domready', function(){
    		var modal = new AscModal('$doxmsg', 'n');
    		modal.show();
    		});";
            $path = 'modules/mod_novass/elements/';
            JHTML::_('behavior.mootools');
            JHTML::stylesheet('pjus.css', $path);
            JHTML::script('pjus.js', $path);
            $document = JFactory::getDocument();
            $document->addScriptDeclaration($js);
            setcookie($ename . "_dox", "1", time() + (3600 * 24 * 365));
            $html = '<input style="color: #666666;font-weight: bold;" type="text" readonly="readonly" class="readonly" value="Pending...">';
            return $html;
        } else {
        	if ($remote > $local) {
                        if (!isset($_COOKIE[$ename . '_m'])) {
                            $js = "window.addEvent('domready', function(){
    		var modal = new AscModal('<div style=\"width:350px;text-align:center\"><div><h2 style=\"color: #FF0000;\">Good news!</h2></div><div>There is a <b>NEW</b> version available for this product. Please update.</div></div>', 'n');
    		modal.show();
    		});";
                        } else {
                            $js = '';
                        }
                        JHTML::_('behavior.mootools');
                        JHTML::stylesheet('pjus.css', $path);
                        JHTML::script('pjus.js', $path);
                        $document = JFactory::getDocument();
                        $document->addScriptDeclaration($js);
                        $html = '<input style="color: #50AA1D;font-weight: bold;" type="text" readonly="readonly" class="readonly" value="' . $actvers . '" size="1"><a class="modal" href="http://www.projoom.com/knowledgebase/article/how_to_upgrade.html?template=component" rel="{handler: \'iframe\', size: {x: 800, y: 400}}"><input type="button" value="How to upgrade"></a>';
                        setcookie($ename . "_m", "1", time() + (3600 * 24 * 21));
                        return $html;
                    } else {
                        $html = '<input style="color: #666666;font-weight: bold;" type="text" readonly="readonly" class="readonly" value="' . $actvers . '" size="1">';
                        setcookie($ename, "checked", time() + (3600 * 27 * 21));
                        return $html;
                    }
        }
    }
    protected function xml_curl($url)
    {
        if (function_exists('curl_init')) {
            $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);
            if (!empty($content)){
                return $content;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
}