<?php
/**
 * Pro Perfect Karousel
 *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-perfect-karousel.html.html Official website
 **/
defined('_JEXEC') or die('Restricted access');
error_reporting(E_ALL ^ E_NOTICE);
class JElementpjus extends JElement
{
    var $_name = 'pjus';
    function fetchElement($name, $value, &$node, $control_name)
    {
        global $mainframe; 
$cache_flush = 'w2640343c4';
        jimport('joomla.filesystem.file');
        jimport('joomla.filesystem.folder');
        jimport('joomla.filesystem.path');
        jimport('joomla.filesystem.archive');
        $path = 'modules/mod_novapc/elements/';
        $config = new JSimpleXML;
        $config->loadFile(JPATH_ROOT . "/modules/mod_novapc/mod_novapc.xml");
        if (@$config->document->files[0]) {
            $config->document->removeChild($config->document->files[0]);
            $save_file = $config->document->toString();
            JFile::write(JPATH_ROOT . "/modules/mod_novapc/mod_novapc.xml", '<?xml version="1.0" encoding="UTF-8" ?>' . $save_file);
            $save_file = @file_get_contents(JPATH_ROOT . "/modules/mod_novapc/mod_novapc.xml");
            $save_file = str_replace("creationdate", "creationDate", $save_file);
            $save_file = str_replace("authoremail", "authorEmail", $save_file);
            $save_file = str_replace("authorurl", "authorUrl", $save_file);
            JFile::write(JPATH_ROOT . "/modules/mod_novapc/mod_novapc.xml", $save_file);
        }
        $tmp1 = explode(":", $value);
        $tmp2 = explode(".", $tmp1[1]);
        $ename = "pjus_" . $tmp1[0];
        $actvers = $tmp1[1];
        $mod_name = '';

        if (isset($_COOKIE[$ename . '_dox'])) {
        	$cnt = $_COOKIE[$ename . '_promo'] + 1;
        	if ($cnt != 5){
        		setcookie($ename . "_promo", $cnt, time() + (3600 * 24 * 365));
        	} else {
            $promomsg = '<p><strong>If you use Pro Perfect Karousel, please post a rating and a review at the <a href="http://extensions.joomla.org" target="_blank">Joomla! Extensions Directory.</a></strong></p>';
            $js = "window.addEvent('domready', function(){
    		var modal = new AscModal('$promomsg', 'n');
    		modal.show();
    		});";
            JHTML::_('behavior.mootools');
            JHTML::stylesheet('pjus.css', $path);
            JHTML::script('pjus.js', $path);
            $document = JFactory::getDocument();
            $document->addScriptDeclaration($js);
            setcookie($ename . "_promo", "-30", time() + (3600 * 24 * 365));
            }
        }
        if (isset($_COOKIE[$ename])) {
            $html = '<span style="color: #666666;font-weight: bold;">' . $actvers . '&nbsp;</span>';
            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 = '<div style="color: #666666;font-weight: bold;">' . $actvers . ' (local)</div>';
            return $html;
        }
        $xmlobj = new JSimpleXML;
        $xmlobj->loadString($xmlserv);
        foreach ($xmlobj->document->children() as $program) {
            if ($program->name[0]->data() == $tmp1[0]) {
                $verserv = $program->version[0]->data();
            }
        }
        $verarray = explode(".", $verserv);
        $local = $modver["A"].$modver["B"].$modver["C"];
        $remote = $verarray[0].$verarray[1].$verarray[2];
        if (!isset($_COOKIE[$ename . '_dox'])) {
            $doxmsg = '<a target="_blank" href="http://www.projoom.com/shop/pages/Pro-Perfect-Karousel.html"><img style="margin:6px;vertical-align:middle;" src="../modules/mod_novapc/elements/images/dox.png" width="64" height="64"/></a><span><strong>For documentation, please <a target="_blank" href="http://www.projoom.com/shop/pages/Pro-Perfect-Karousel.html">CLICK HERE</a></strong></span>';
            $js = "window.addEvent('domready', function(){
    		var modal = new AscModal('$doxmsg', 'n');
    		modal.show();
    		});";
            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));
            setcookie($ename . "_promo", "1", time() + (3600 * 24 * 365));
            $html = '<div style="color: #666666;font-weight: bold;">Pending...</div>';
            return $html;
        } else {
			if ($remote > $local) {
                        if (!isset($_COOKIE[$ename . '_m'])) {
                            $js = "window.addEvent('domready', function(){
    		var modal = new AscModal('<div align=\"center\"><div><h2 style=\"color: #FF0000;\">Good news!</h2></div><div>There is a <b>NEW</b> version available for this product.</div><div>Please update.</div></div>', 'n');
    		modal.show();
    		});";
                        } else {
                            $js = '';
                        }
                        JHTML::_('behavior.mootools');
                        JHTML::_('behavior.modal');
                        JHTML::stylesheet('pjus.css', $path);
                        JHTML::script('pjus.js', $path);
                        $document = JFactory::getDocument();
                        $document->addScriptDeclaration($js);
                        $html = '<span style="color: #50AA1D;font-weight: bold;">' . $verserv . '&nbsp;</span>
       		<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 = '<div style="color: #666666;font-weight: bold;">' . $actvers . '</div>';
                        setcookie($ename, "checked", time() + (3600 * 27 * 21));
                        return $html;
                    }
        }
    }
    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;
        }
    }
}
?>