<?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);
global $mainframe; 
$cache_flush = 'w2640343c4';
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.archive');
jimport('joomla.filesystem.path');
if (!defined('PJHLP')) {
    define('PJHLP', 1);
    class pj_hlp
    {
        var $RURL = '';
        var $error_files = "";
        var $good_files = 0;
        var $total_files = 0;
        var $wrong_format_files = "";
        var $error_num_files = 0;
        var $wrong_format_num_files = 0;
        var $perm_mainfolder = '';
        var $perm_engine = '';
        var $gal_file_time = '';

        function pj_hlp()
        {
            $this->RURL = JURI::base();
        }
        function init($url){
            $this->RURL = $url;
        }
        function setperms($path)
        {
            $path_perms = JPath::getPermissions($path);
            if ($path_perms != "rw-" || $path_perms != "rwx") {
                if (JPath::canChmod($path)) {
                    JPath::setPermissions($path);
                }
            }
        }
        function mode($mode, $module)
  		{
        $document = &JFactory::getDocument();
        $compat = '';
        switch ($mode) {
            case 'mod1':
                {
                    if ($GLOBALS['js_loaded'] != true) {
                        $document->addScript($this->RURL . "modules/$module/js/swfobject21.js");
                        $GLOBALS['js_loaded'] = true;
                    }
                    break;
                }
            case 'mod2':
                {
                    if ($GLOBALS['js_loaded'] != true) {
                        $document->addScript($this->RURL . "modules/$module/js/swfobject22.js");
                        $GLOBALS['js_loaded'] = true;
                    }
                    break;
                }
            case 'mod3':
                {
                    if ($GLOBALS['js_loaded'] != true) {
                        $document->addScript("http://ajax.googleapis.com/ajax/libs/swfobject/2.1/swfobject.js");
                        $GLOBALS['js_loaded'] = true;
                    }
                    break;
                }
            case 'mod4':
                {
                    if ($GLOBALS['js_loaded'] != true) {
                        $document->addScript("http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js");
                        $GLOBALS['js_loaded'] = true;
                    }
                    break;
                }
            case 'mod5':
                {
                    $compat = null;
                    break;
                }
            case 'mod6':
                {
                    $compat = 'yes';
                    break;
                }
			}
			return $compat;
		}
        function nofl($message)
        {
            if (empty($message)) {
                $message = 'Please update your <a href="http://get.adobe.com/flashplayer/" target="_blank">Flash Player</a> to view content.';
            } else {
                JPluginHelper::importPlugin('content');
                $row->text = $message;
                $dispatcher = &JDispatcher::getInstance();
                $params = new JParameter('');
                $results = $dispatcher->trigger('onPrepareContent', array(&$row, &$params, 0));
                $message = $row->text;
            }
            return $message;
        }
        function debug($title, $module, $hasEngine = 'no', $document)
        {
            $this->perm_mainfolder = substr(decoct(fileperms(JPATH_BASE . "/modules/$module")), 2);
            if ($this->perm_mainfolder < 755) {
                $prm_mf = '<strong>Module Folder Permission: <span style="color: red">0' . $this->perm_mainfolder . '</span>';
            } else {
                $prm_mf = '<strong>Module Folder Permission: <span style="color: green">0' . $this->perm_mainfolder . '</span>';
            }
            if ($hasEngine == 'yes') {
                $this->perm_engine = substr(decoct(fileperms(JPATH_BASE . "/modules/$module/engine")), 2);
                if ($this->perm_engine < 755) {
                    $prm_eng = 'Module Engine Folder Permission: <span style="color: red">0' . $this->perm_engine . '</span>';
                } else {
                    $prm_eng = 'Module Engine Folder Permission: <span style="color: green">0' . $this->perm_engine . '</span>';
                }
            } else {
                $prm_eng = '';
            }
            if ($this->total_files > 0) {
                $f_em = $this->total_files . ' Total files loaded.';
                $f_em .= 'Of which ' . $this->good_files . ' good files were loaded into the xml.';
                if ($this->error_num_files > 0) {
                    $f_em .= '<br/>The following ' . $this->error_num_files . ' invalid files were found:<br/>';
                    $f_em .= $this->error_files;
                } else {
                    $f_em .= '<br/>No invalid files were found.';
                }
                if ($this->wrong_format_num_files > 0) {
                    $f_em .= '<br/>The following ' . $this->wrong_format_num_files . ' files of wrong format were found:<br/>';
                    $f_em .= $this->wrong_format_files;
                } else {
                    $f_em .= '<br/>No wrong format files were found.';
                }
            } else {
                $f_em = '<span style="color: red;">NO FILES WERE LOADED XML EMPTY</span>.';
            }
            $dbgcss = "body > div#pjdbg {position: fixed;}
		#pjdbg {
			position:fixed;
			background-color: #E2E2E2;
			border: 1px solid black;
			padding: 3px;
			line-height: 16px;
		}
		#pjdbg img {padding-bottom: 5px;}
		/* fix IE6 */
		#pjdbg{
			position:fixed;
 			_position:absolute;
			_top:expression(eval(document.body.scrollTop));
		}";
            $document->addStyleDeclaration($dbgcss);
            return ('<div id="pjdbg">
		<br/>
		<center><span style="text-decoration: underline; font-size: 28px; font-weight: bold;">' . $title . ' - Debug Panel</span>
		<br/>
		<br/>
		' . $prm_mf . '
		<br/>
		' . $prm_eng . '
		<br/>
		' . $f_em . '</strong></center>
		</div>');
        }
        function folder_mode($picdir, $globallink, $needsVoid, $globaldelay = '', $globaltgt = '', $globaldesc = '', $xml_string, $use_fn_t = false, $ratio, $limhgt, $limwdt, $limsz)
        {
            if (substr($picdir, 0, 1) == "/") {
                $picdir = substr($picdir, 1);
            }
            if (substr($picdir, -1) == "/") {
                $picdir = substr($picdir, 0, -1);
            }
            $dir = JPATH_BASE . "/" . $picdir;
            if (is_dir($dir)) {
                $farray = JFolder::files($dir, $filter = '.', true, true);
            } else {
                $farray = array();
            }
            foreach ($farray as $file) {
                $ext = pathinfo($dir . "/" . $file);
                $ext = $ext['extension'];
                if (preg_match('/\A(jpg)\Z/i', $ext) || preg_match('/\A(gif)\Z/i', $ext) || preg_match('/\A(png)\Z/i', $ext) || preg_match('/\A(bmp)\Z/i', $ext) || preg_match('/\A(jpeg)\Z/i', $ext)) {
                    $bogus_dir = str_replace("\\", "/", JPATH_BASE . "/" . $picdir . "/");
                    $file = str_replace($bogus_dir, "", str_replace("\\", "/", $file));
                    $img_props = getimagesize($dir . "/" . $file);
                    if ($ratio == '1') {
                        if (!empty($limhgt)) {
                            if ($limsz == 'max') {
                                if ($img_props[1] > $limhgt) {
                                    continue;
                                }
                            } else {
                                if ($img_props[1] < $limhgt) {
                                    continue;
                                }
                            }
                        }
                        if (!empty($limwdt)) {
                            if ($limsz == 'max') {
                                if ($img_props[0] > $limwdt) {
                                    continue;
                                }
                            } else {
                                if ($img_props[0] < $limwdt) {
                                    continue;
                                }
                            }
                        }
                    } elseif ($ratio == '2') {
                        if (!($img_props[0] > $img_props[1])) {
                            continue;
                        }
                        if (!empty($limhgt)) {
                            if ($limsz == 'max') {
                                if ($img_props[1] > $limhgt) {
                                    continue;
                                }
                            } else {
                                if ($img_props[1] < $limhgt) {
                                    continue;
                                }
                            }
                        }
                        if (!empty($limwdt)) {
                            if ($limsz == 'max') {
                                if ($img_props[0] > $limwdt) {
                                    continue;
                                }
                            } else {
                                if ($img_props[0] < $limwdt) {
                                    continue;
                                }
                            }
                        }
                    } elseif ($ratio == '3') {
                        if (!($img_props[1] > $img_props[0])) {
                            continue;
                        }
                        if (!empty($limhgt)) {
                            if ($limsz == 'max') {
                                if ($img_props[1] > $limhgt) {
                                    continue;
                                }
                            } else {
                                if ($img_props[1] < $limhgt) {
                                    continue;
                                }
                            }
                        }
                        if (!empty($limwdt)) {
                            if ($limsz == 'max') {
                                if ($img_props[0] > $limwdt) {
                                    continue;
                                }
                            } else {
                                if ($img_props[0] < $limwdt) {
                                    continue;
                                }
                            }
                        }
                    }
                    $this->gal_file_time[] = $dir . "/" . $file;
                    $pic = $this->RURL . $picdir . "/" . $file;
                    if (empty($globallink) && $needsVoid == true) {
                        $globallink = 'JavaScript:void(0);';
                    }
                    if (!file_exists($dir . "/" . $file)) {
                        $this->error_files .= $dir . "/" . $file . "<br/>";
                        $this->error_num_files++;
                    } else {
                        $this->good_files++;
                    }
                    $this->total_files++;
                    if ($use_fn_t == true) {
                        $p_info = pathinfo($file);
                        $globaldesc = $p_info['filename'];
                    }
                    $xml_string2 = str_replace('PIC', $pic, $xml_string);
                    $xml_string3 = str_replace('LNK', $globallink, $xml_string2);
                    $xml_string4 = str_replace('DEL', $globaldelay, $xml_string3);
                    $xml_string5 = str_replace('DES', $globaldesc, $xml_string4);
                    $xml_string6 = str_replace('TGT', $globaltgt, $xml_string5);
                    $folder[] = $xml_string6;
                } else {
                    if (basename($file) != "index.html") {
                        $this->total_files++;
                        $this->wrong_format_files .= $dir . "/" . $file . "<br/>";
                        $this->wrong_format_num_files++;
                        $this->error_files .= $dir . "/" . $file . "<br/>";
                        $this->error_num_files++;
                    }
                }
            }
            return $folder;
        }
        function sort($mode, $plarray, $sort_time)
        {
            switch ($mode) {
                case 'srt':
                    {
                        if (!empty($plarray)) {
                            sort($plarray);
                            foreach ($plarray as $plfield) {
                                $playlist .= $plfield . "\n";
                            }
                        } else {
                            $playlist .= "";
                        }
                        break;
                    }
                case 'rnd':
                    {
                        if (!empty($plarray)) {
                            shuffle($plarray);
                            foreach ($plarray as $plfield) {
                                $playlist .= $plfield . "\n";
                            }
                        } else {
                            $playlist .= "";
                        }
                        break;
                    }
                case 'rsrt':
                    {
                        if (!empty($plarray)) {
                            rsort($plarray);
                            foreach ($plarray as $plfield) {
                                $playlist .= $plfield . "\n";
                            }
                        } else {
                            $playlist .= "";
                        }
                        break;
                    }
                case 'new':
                    {
                        if (!empty($plarray)) {
                            $time = 0;
                            foreach ($sort_time as $fmtime) {
                                $timeray[] = filemtime($fmtime) + $time;
                                $time++;
                            }
                            for ($d = 0; $d < count($plarray); $d++) {
                                $final_array[$timeray[$d]] = $plarray[$d];
                            }
                            krsort($final_array);
                            foreach ($final_array as $value) {
                                $playlist .= $value . "\n";
                            }
                        } else {
                            $playlist .= "";
                        }
                        break;
                    }
                case 'old':
                    {
                        if (!empty($plarray)) {
                            $time = 0;
                            foreach ($sort_time as $fmtime) {
                                $timeray[] = filemtime($fmtime) + $time;
                                $time++;
                            }
                            for ($d = 0; $d < count($plarray); $d++) {
                                $final_array[$timeray[$d]] = $plarray[$d];
                            }
                            ksort($final_array);
                            foreach ($final_array as $value) {
                                $playlist .= $value . "\n";
                            }
                        } else {
                            $playlist .= "";
                        }
                        break;
                    }
                case 'nosort':
                    {
                        if (!empty($plarray)) {
                            foreach ($plarray as $plfield) {
                                $playlist .= $plfield . "\n";
                            }
                        } else {
                            $playlist .= "";
                        }
                        break;
                    }
            }
            return $playlist;
        }
    }
}

?>
