Source for file GBFilePicker.module.php

Documentation is available at GBFilePicker.module.php

  1. <?php
  2. #-------------------------------------------------------------------------------
  3. #
  4. # Module : GBFilePicker (c) 2010 by Georg Busch (georg.busch@gmx.net) is a file
  5. #          picker tool for CMS Made Simple
  6. #          The projects homepage is: dev.cmsmadesimple.org/projects/gbfilepicker
  7. #          CMS Made Simple is (c) 2004-2010 by Ted Kulp
  8. #          The projects homepage is: cmsmadesimple.org
  9. # Version: 1.1
  10. # File   : GBFilePicker.module.php
  11. # Purpose: main module class
  12. # License: GPL
  13. #
  14. #-------------------------------------------------------------------------------
  15.  
  16. /**
  17.  * Class definition and methods for GBFilePicker
  18.  *
  19.  * @package CMSModule
  20.  * @license GPL
  21.  */
  22.  
  23. /**
  24.  * Main class of the GBFilePicker Module
  25.  *
  26.  * @author Georg Busch (NaN)
  27.  * @copyright 2010 Georg Busch (NaN)
  28.  * @version 1.1
  29.  *
  30.  * @package CMSModule
  31.  * @license GPL
  32.  */
  33. class GBFilePicker extends CMSModule 
  34. {
  35.     /**
  36.      * @access private
  37.      * @var boolean 
  38.      * @ignore
  39.      */
  40.     var $_isAdmin;
  41.     
  42.     /**
  43.      * @access private
  44.      * @var boolean 
  45.      * @ignore
  46.      */
  47.     var $_loaded;
  48.     
  49.     /**
  50.      * @access private
  51.      * @var string 
  52.      * @ignore
  53.      */
  54.     var $_username;
  55.     
  56.     /**
  57.      * Constructor
  58.      * @ignore
  59.      */
  60.     function GBFilePicker() 
  61.     {
  62.         parent::CMSModule();
  63.         $this->_loaded = false;
  64.         global $CMS_ADMIN_PAGE;
  65.         if(isset($CMS_ADMIN_PAGE) && $CMS_ADMIN_PAGE == 1) 
  66.         {
  67.             $gCms            = cmsms();
  68.             $userops         =& $gCms->GetUserOperations();
  69.             $this->_isAdmin  = $userops->UserInGroup(get_userid(false),1);
  70.             $this->_username = $userops->LoadUserById(get_userid(false))->username;
  71.         }
  72.     }
  73.     
  74.     /**
  75.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetName
  76.      * @ignore
  77.      */
  78.     function GetName() 
  79.     {
  80.         return 'GBFilePicker';
  81.     }
  82.     
  83.     /**
  84.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetFriendlyName
  85.      * @ignore
  86.      */
  87.     function GetFriendlyName() 
  88.     {
  89.         return $this->Lang('GBFilePicker');
  90.     }
  91.     
  92.     /**
  93.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetVersion
  94.      * @ignore
  95.      */
  96.     function GetVersion() 
  97.     {
  98.         return '1.1';
  99.     }
  100.     
  101.     /**
  102.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetHelp
  103.      * @ignore
  104.      */
  105.     function GetHelp() 
  106.     {
  107.         return $this->lang('help');
  108.     }
  109.     
  110.     /**
  111.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetAuthor
  112.      * @ignore
  113.      */
  114.     function GetAuthor() 
  115.     {
  116.         return 'Georg Busch (NaN)';
  117.     }
  118.     
  119.     /**
  120.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetChangeLog
  121.      * @ignore
  122.      */
  123.     function GetChangeLog() 
  124.     {
  125.         return $this->lang('changelog');
  126.     }
  127.     
  128.     /**
  129.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#HasAdmin
  130.      * @ignore
  131.      */
  132.     function HasAdmin() 
  133.     {
  134.         return true;
  135.     }
  136.     
  137.     /**
  138.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetAdminSection
  139.      * @ignore
  140.      */
  141.     function GetAdminSection() 
  142.     {
  143.         return 'extensions';
  144.     }
  145.     
  146.     /**
  147.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetAdminDescription
  148.      * @ignore
  149.      */
  150.     function GetAdminDescription() 
  151.     {
  152.         return $this->lang('admindescription');
  153.     }
  154.     
  155.     /**
  156.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#VisibleToAdminUser
  157.      * @ignore
  158.      */
  159.     function VisibleToAdminUser() 
  160.     {
  161.         return $this->Checkpermission('Manage GBFilePicker');
  162.     }
  163.     
  164.     /**
  165.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#MinimumCMSVersion
  166.      * @ignore
  167.      */
  168.     function MinimumCMSVersion() 
  169.     {
  170.         return "1.8.2";
  171.     }
  172.     
  173.     /**
  174.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#MaximumCMSVersion
  175.      * @ignore
  176.      */
  177.     function MaximumCMSVersion() 
  178.     {
  179.         return "1.9.9";
  180.     }
  181.     
  182.     /**
  183.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#InstallPostMessage
  184.      * @ignore
  185.      */
  186.     function InstallPostMessage() 
  187.     {
  188.         return $this->Lang('post_install', $this->GetVersion());
  189.     }
  190.     
  191.     /**
  192.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#UninstallPostMessage
  193.      * @ignore
  194.      */
  195.     function UninstallPostMessage() 
  196.     {
  197.         return $this->Lang('post_uninstall', $this->GetVersion());
  198.     }
  199.     
  200.     /**
  201.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#UninstallPreMessage
  202.      * @ignore
  203.      */
  204.     function UninstallPreMessage() 
  205.     {
  206.         return $this->Lang('confirm_uninstall');
  207.     }
  208.     
  209.     /**
  210.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#HandlesEvents
  211.      * @ignore
  212.      */
  213.     function HandlesEvents() 
  214.     {
  215.         return false;
  216.     }
  217.     
  218.     /**
  219.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#DoAction
  220.      * @ignore
  221.      */
  222.     function DoAction($action, $id, $params, $returnid = '') 
  223.     {
  224.         switch($action) 
  225.         {
  226.             case 'defaultadmin':
  227.             case 'savePrefs':
  228.             case 'fileBrowser':
  229.                 parent::DoAction($action, $id, $params, $returnid);
  230.                 break;
  231.                 
  232.             default: break;
  233.         }
  234.     }
  235.     
  236.     /**
  237.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#SetParameters
  238.      * @todo create list of allowed parameters!
  239.      */
  240.     function SetParameters() 
  241.     {
  242.         #$this->RestrictUnknownParams();
  243.     }
  244.     
  245.     /**
  246.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#HasCapability
  247.      * @ignore
  248.      */
  249.     function HasCapability($capability,$params = array()) 
  250.     {
  251.         switch( $capability ) 
  252.         {
  253.             case 'contentblocks':
  254.             case 'content_attributes':
  255.                 return TRUE;
  256.             default:
  257.                 return FALSE;
  258.         }
  259.     }
  260.     
  261.     /**
  262.      * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetContentBlockInput
  263.      * @ignore
  264.      */
  265.     function GetContentBlockInput($blockname,$value,$params,$adding = false) 
  266.     {
  267.         if($this->CheckPermission('Use GBFilePicker'))
  268.         {
  269.             return $this->CreateFilePickerInput(&$this, '', str_replace(' ','_',$blockname), $value, $params);
  270.         }
  271.         return;
  272.     }
  273.     
  274. #-------------------------------------------------------------------------------
  275. # Not part of the CMSms module API
  276. #-------------------------------------------------------------------------------
  277.     
  278.     /**
  279.      * Creates the filepicker input.
  280.      *
  281.      * @since 1.0
  282.      * @access public
  283.      *
  284.      * @param object &$module - the module instance that calls the filepicker (this is only needed if you want to process own templates; pass the filepicker instance itself or leave it empty if you don't need this)
  285.      * @param string $id - the id of the moduleinstance that creates the filepicker input
  286.      * @param string $name - the name of the input field
  287.      * @param string $value - the preselected value
  288.      * @param array $params - the input params (param_name => param_value):<br /><ul>
  289.      *         <li>(string)  'feu_access'               => a csv of group ids of the frontend users module,</li>
  290.      *         <li>(boolean) 'restrict_users_diraccess' => set to true if user may only access a dir of his username (notice: this has no effect if you are admin),</li>
  291.      *         <li>(string)  'input_template'           => specify wich template should be used to create the input elements (default is GBFilePickers input.tpl),</li>
  292.      *         <li>(string)  'filebrowser_template'     => specify wich template should be used to create the input elements (default is GBFilePickers fileBrowser.tpl),</li>
  293.      *         <li>(string)  'prompt'                   => the prompt of the input field,</li>
  294.      *         <li>(string)  'media_type'               => the media type (at the moment only image / file are supported),</li>
  295.      *         <li>(mixed)   'file_extensions'          => an array or a csv of allowed file extensions,</li>
  296.      *         <li>(mixed)   'exclude_prefix'           => an array or a csv of file prefixes that will be excluded,</li>
  297.      *         <li>(mixed)   'exclude_sufix'            => an array or a csv of file suffixes that will be excluded,</li>
  298.      *         <li>(mixed)   'include_prefix'           => an array or a csv of file prefixes that will be included,</li>
  299.      *         <li>(mixed)   'include_sufix'            => an array or a csv of file suffixes that will be included,</li>
  300.      *         <li>(boolean) 'show_subdirs'             => set to true if user may browse subdirs (notice: this has no effect if you are admin)</li>
  301.      *         <li>(string)  'mode'                     => dropdown/browser,</li>
  302.      *         <li>(boolean) 'allow_none'               => set to false if the option "none" should not be shown (notice: this has no effect if you are admin),</li>
  303.      *         <li>(boolean) 'lock_input'               => set to false if user may enter the path in the inputfield ('mode'=>'browser', 'media_type' => 'file') (notice: this has no effect if you are admin),</li>
  304.      *         <li>(boolean) 'upload'                   => set to true if user may upload files (notice: this has no effect if you are admin or have permission to modify files),</li>
  305.      *         <li>(boolean) 'delete'                   => set to true if user may delete files/dirs (notice: this has no effect if you are admin or have permission to modify files),</li>
  306.      *         <li>(boolean) 'create_dirs'              => set to true if user may create dirs (notice: this has no effect if you are admin or have permission to modify files),</li>
  307.      *         <li>(string)  'add_txt'                  => any additional text that will be added to the html input when tag is rendered,</li>
  308.      *         <li>(integer) 'size'                     => the size of the textinput ('mode' => 'browser', 'media_type' => 'file' only),</li>
  309.      *         <li>(integer) 'maxlength'                => the max length of the textinput ('mode' => 'browser', 'media_type' => 'file' only),</li>
  310.      *         <li>(integer) 'scaling_width'            => the default width of images when allow_scaling is true,</li>
  311.      *         <li>(integer) 'scaling_height'           => the default height of images when allow_scaling is true,</li>
  312.      *         <li>(boolean) 'show_thumbfiles'          => set to true if thumbs should also be shown as regular files,</li>
  313.      *         <li>(boolean) 'allow_scaling'            => set to false if user may not scale the images on upload,</li>
  314.      *         <li>(boolean) 'create_thumbs'            => set to false if the module may not create thumbs for the input,</li>
  315.      *         <li>(boolean) 'allow_upscaling'          => set to true if user may enlarge the images</li></ul>
  316.      *
  317.      * @return string - the HTML output of the filepicker
  318.      */
  319.     function CreateFilePickerInput(&$module, $id, $name, $value = '', $params = array()) 
  320.     {
  321.         $name = str_replace(' ','_',$name);
  322.         if($module == '')
  323.         {
  324.             $module =& $this;
  325.         }
  326.         if(!$params =& $this->SetInputParams(&$module, $id, $name, $params))
  327.         {
  328.             return;
  329.         }
  330.         
  331.         $value  = $this->CleanUrl($value, false);
  332.         $gCms   = cmsms();
  333.         $config =& $gCms->GetConfig();
  334.         
  335.         $module->smarty->assign('thumb_width', get_site_preference('thumbnail_width',96));
  336.         $module->smarty->assign('thumb_height', get_site_preference('thumbnail_height','auto'));
  337.         $module->smarty->assign('gbfp_thumburl', $this->GetThumbnail($value, $params['create_thumbs'],true));
  338.         $module->smarty->assign('gbfp_value', $value);
  339.         $module->smarty->assign('gbfp_loaded', $this->_loaded);
  340.         $this->_loaded = true;
  341.         
  342.         $input      = '';
  343.         $upload_btn = '';
  344.         $browse_btn = '';
  345.         $clear_btn  = '';
  346.         $reload_btn = '';
  347.         $upload_url = '';
  348.         $browse_url = '';
  349.         $reload_url = '';
  350.         
  351.         if($params['mode'] == 'dropdown')
  352.         {
  353.             #if($params['add_txt'] == '')
  354.             #{
  355.             #    $params['add_txt'] = 'onchange="GBFP.ToggleThumbnail(\'#' . $id.$name . '\', jQuery(\'#' . $id.$name . ' option:selected:first\').attr(\'thumbnail\'),this.value,this.value)"';
  356.             #}
  357.             $input = $this->CreateFileDropdown(
  358.                 $id,
  359.                 $name, 
  360.                 $params['dir'],
  361.                 $value,
  362.                 $params['exclude_prefix'], 
  363.                 $params['include_prefix'], 
  364.                 $params['exclude_sufix'], 
  365.                 $params['include_sufix'], 
  366.                 $params['file_extensions'], 
  367.                 $params['media_type'], 
  368.                 $params['allow_none'],
  369.                 $params['add_txt']);
  370.             
  371.             if($params['upload'])
  372.             {
  373.                 $upload_url = str_replace('&amp;','&',$this->CreateLink(($id == '' ? 'm1_' : $id), 'fileBrowser', '', $this->lang('upload'),array('name' =>$name,'disable_theme'=>true,'dir'=>($this->_isAdmin?$params['dir']:'')),'', true));
  374.                 $upload_btn = '<input disabled="disabled" type="button" value="'.$this->lang('upload').'" 
  375.                     id="'.$id.$name.'_GBFP_browse" class="GBFP_browse GBFP_button GBFP_disabled" onclick="GBFP.LoadDir(\''.$upload_url.'\',\''.munge_string_to_url($id.$name.'_'.$params['dir']).'\'); return false;" />';
  376.             }
  377.             $reload_url = str_replace('&amp;','&',
  378.                 $this->CreateLink(($id == '' ? 'm1_' : $id), 
  379.                     'fileBrowser', 
  380.                     '', 
  381.                     $this->lang('reload'),
  382.                     array('name' =>$name,
  383.                         'disable_theme'=>true,
  384.                         'reload'=>true),
  385.                     '', 
  386.                     true));
  387.             $reload_btn = '<input disabled="disabled" type="button" value="'.$this->lang('reload_dropdown').'" 
  388.                 id="'.$id.$name.'_GBFP_reload" class="GBFP_reload GBFP_button GBFP_disabled" onclick="GBFP.ReloadDropdown(\''.$reload_url.'\',\''.$id.$name.'\'); return false;" />';
  389.         }
  390.         else if($params['mode'] == 'browser') 
  391.         {
  392.             if($params['media_type'] == 'image') 
  393.             {
  394.                 $input = '<input id="'.$id.$name.'" type="hidden" name="'.$id.$name.'" value="' . $value . '" />';
  395.             }
  396.             else if($params['media_type'] == 'file') 
  397.             {
  398.                 $input = '<input id="'.$id.$name.'" class="GBFP_textinput" type="text"' . ($params['lock_input']?' disabled="disabled"':'') . ($params['add_txt'] != ''? ' '.$params['add_txt'].' ':'') . ($params['size'] != ''?' size="' . $params['size'] . '"':'') . ($params['maxlength'] != ''?' maxlength="' . $params['maxlength'] . '"':'') . ' name="' . $id.$name . '" value="' . $value . '" />';
  399.             }
  400.             
  401.             $browse_url = str_replace('&amp;','&',$this->CreateLink(($id == '' ? 'm1_' : $id), 'fileBrowser', '', $this->lang('browse_'.$params['media_type']),array('name' =>$name,'disable_theme'=>true,'dir'=>($this->_isAdmin?$params['dir']:'')),'', true));
  402.             $browse_btn = '<input disabled="disabled" type="button" value="'.$this->lang('browse_'.$params['media_type']).'" id="'.$id.$name.'_GBFP_browse" class="GBFP_browse GBFP_disabled GBFP_button" onclick="GBFP.LoadDir(\''.$browse_url.'\',\''.munge_string_to_url($id.$name.'_'.$params['dir']).'\'); return false;" />';
  403.             if($params['allow_none']) 
  404.             {
  405.                 $clear_btn = '<input disabled="disabled" class="GBFP_disabled GBFP_button" type="button" value="'.lang('none').'" onclick="jQuery(\'#'.$id.$name.'\').val(\'\'); GBFP.ToggleThumbnail(\'#'.$id.$name.'\',\'\'); return false;" />';
  406.             }
  407.         }
  408.         
  409.         if($config['debug'])
  410.         {
  411.             debug_buffer($params,'GBFilePicker input params; input = "'.$id.$name.'"; function CreateFilePickerInput() ');
  412.         }
  413.         $module->smarty->assign('gbfp_input', $input);
  414.         $module->smarty->assign('gbfp_browse_button', $browse_btn);
  415.         $module->smarty->assign('gbfp_reload_button', $reload_btn);
  416.         $module->smarty->assign('gbfp_reload_url', $reload_url);
  417.         $module->smarty->assign('gbfp_upload_button', $upload_btn);
  418.         $module->smarty->assign('gbfp_clear_button', $clear_btn);
  419.         $module->smarty->assign('gbfp_id', $id);
  420.         $module->smarty->assign('gbfp_name', $name);
  421.         $module->smarty->assign('gbfp_inputid', $id.$name);
  422.         $module->smarty->assign('gbfp_inputname', $name);
  423.         $module->smarty->assign('gbfp_upload_url', $upload_url);
  424.         $module->smarty->assign('gbfp_browse_url', $browse_url);
  425.         $module->smarty->assign('gbfp_title', $this->Lang('filebrowser'));
  426.         $module->smarty->assign('gbfp_browse_url', $browse_url);
  427.         foreach($params as $k => $v)
  428.         {
  429.             $module->smarty->assign('gbfp_'.$k,$v);
  430.         }
  431.         $module->smarty->assign('gbfp_reload_dir_text', $this->lang('reload_dir'));
  432.         $module->smarty->assign('gbfp_clear_cache_text', $this->lang('clear_cache'));
  433.         $module->smarty->assign('gbfp_close_text', $this->lang('close'));
  434.         if(endswith($params['input_template'],'.tpl'))
  435.         {
  436.             return $module->ProcessTemplate($params['input_template']);
  437.         }
  438.         return $module->ProcessTemplateFromDatabase($params['input_template']);
  439.     }
  440.     
  441.     
  442.     /**
  443.      * Sets the params of a filepicker input and save them in a session var
  444.      * 
  445.      * @since 1.1
  446.      * @access private
  447.      *
  448.      * @param string $id - the id of the moduleinstance that creates the filepicker input
  449.      * @param string $name - the name of the input field
  450.      * @param array $params - the params
  451.      *
  452.      * @return array|false
  453.      *
  454.      * @ignore
  455.      */
  456.     function &SetInputParams(&$module, $id, $name, $params = array()) 
  457.     {
  458.         global $CMS_ADMIN_PAGE;
  459.         $gCms = cmsms();
  460.         $config =& $gCms->GetConfig();
  461.         @session_start();
  462.         $_SESSION['GPFP_' . ($id == '' ? 'm1_' : $id).$name] = array();
  463.         
  464.         $name = str_replace(' ','_',$name);
  465.         if(!$session_params =& $this->GetInputParams($id, $name))
  466.         {
  467.             return false;
  468.         }
  469.         
  470.         if(isset($params['prompt']))
  471.         {
  472.             $session_params['prompt'] = $params['prompt'];
  473.         }
  474.         
  475.         if(isset($params['input_template']))
  476.         {
  477.             $session_params['input_template'] = $params['input_template'];
  478.         }
  479.         if(isset($params['filepicker_template']))
  480.         {
  481.             $session_params['filebrowser_template'] = $params['filepicker_template'];
  482.         }
  483.         
  484.         $session_params['module'] = $module->GetName();
  485.         if(isset($params['feu_access'])) 
  486.         {
  487.             if(!is_array($params['feu_access']))
  488.             {
  489.                 $session_params['feu_access'] = $this->CleanArray(explode(',',$params['feu_access']));
  490.             }
  491.             else
  492.                 $session_params['feu_access'] = $this->CleanArray($params['feu_access']);
  493.         }
  494.         if(isset($params['restrict_users_diraccess']) && $this->IsFalse($params['restrict_users_diraccess'])) 
  495.         {
  496.             $session_params['restrict_users_diraccess'] = false;
  497.         }
  498.         
  499.         $dir = $session_params['start_dir'];
  500.         if(isset($params['dir']) && $params['dir'] != '')
  501.         {
  502.             $dir = $this->CleanUrl($params['dir'],false);
  503.         }
  504.         
  505.         if(isset($CMS_ADMIN_PAGE) && $CMS_ADMIN_PAGE == 1)
  506.         {
  507.             check_login();
  508.             if(!$this->CheckPermission('Use GBFilePicker'))
  509.             {
  510.                 return false;
  511.             }
  512.             $username = $this->_username;
  513.         }
  514.         else 
  515.         {
  516.             if( !$feusers =& $this->GetModuleInstance('FrontEndUsers' ) )
  517.             {
  518.                 return false;
  519.             }
  520.             if(!$userid = $feusers->LoggedInId())
  521.             {
  522.                 return false;
  523.             }
  524.             if(!$groups = $feusers->GetMemberGroupsArray($userid))
  525.             {
  526.                 return false;
  527.             }
  528.             $access = false;
  529.             foreach($groups as $_group) 
  530.             {
  531.                 if(in_array($_group['groupid'],$session_params['feu_access']))
  532.                 {
  533.                     $access = true;
  534.                     break;
  535.                 }
  536.             }
  537.             if(!$access)
  538.             {
  539.                 return false;
  540.             }
  541.             
  542.             $username = $feusers->GetUserName($userid);
  543.             $params['media_type'] = 'image';
  544.             $params['restrict_users_diraccess'] = true;
  545.         }
  546.         
  547.         if(!$this->_isAdmin && $session_params['restrict_users_diraccess'])
  548.         {
  549.             if(!in_array($username, $this->CleanArray(explode('/',$dir))))
  550.             {
  551.                 $session_params['start_dir'] .= '/' . $dir;
  552.             }
  553.             else 
  554.             {
  555.                 $session_params['start_dir'] = $dir;
  556.             }
  557.             
  558.             $_dir = $this->CleanPath($session_params['start_dir']);
  559.             if(!@mkdir($_dir, 0755, true) && !file_exists($_dir) && !is_dir($_dir)) 
  560.             {
  561.                 return false;
  562.             }
  563.         }
  564.         
  565.         if(isset($params['media_type']))
  566.         {
  567.             $session_params['media_type'] = strtolower($params['media_type']);
  568.         }
  569.         
  570.         if($session_params['media_type'] == 'file')
  571.         {
  572.             $session_params['file_extensions'] = array();
  573.         }
  574.         if(isset($params['file_extensions']) && $params['file_extensions'] != '')
  575.         {
  576.             $_file_ext = $this->CleanArray(explode(',',strtolower($params['file_extensions'])));
  577.             if($session_params['media_type'] == 'image')
  578.             {
  579.                 $_file_ext = $this->CleanArray(array_intersect($_file_ext, array('jpg','jpeg','gif','png')));
  580.             }
  581.             if(empty($_file_ext))
  582.             {
  583.                 $_file_ext = $session_params['file_extensions'];
  584.             }
  585.             $session_params['file_extensions'] = $_file_ext;
  586.         }
  587.         if(isset($params['exclude_prefix']))
  588.         {
  589.             $session_params['exclude_prefix'] = trim($params['exclude_prefix']);
  590.         }
  591.         if(isset($params['exclude_sufix']))
  592.         {
  593.             $session_params['exclude_sufix'] = trim($params['exclude_sufix']);
  594.         }
  595.         if(isset($params['include_prefix']))
  596.         {
  597.             $session_params['include_prefix'] = trim($params['include_prefix']);
  598.         }
  599.         if(isset($params['include_sufix']))
  600.         {
  601.             $session_params['include_sufix'] = trim($params['include_sufix']);
  602.         }
  603.         if(isset($params['show_subdirs']) && $this->IsTrue($params['show_subdirs']))
  604.         {
  605.             $session_params['show_subdirs'] = true;
  606.         }
  607.         if(isset($params['mode']))
  608.         {
  609.             $session_params['mode'] = strtolower($params['mode']);
  610.         }
  611.         if(isset($params['allow_none']) && $this->IsFalse($params['allow_none']))
  612.         {
  613.             $session_params['allow_none'] = intval(false);
  614.         }
  615.         if(isset($params['lock_input']) && $this->IsFalse($params['lock_input']))
  616.         {
  617.             $session_params['lock_input'] = intval(false);
  618.         }
  619.         if(isset($params['upload']) && $this->IsTrue($params['upload']) && $this->GetPreference('show_filemanagement')) 
  620.         {
  621.             $session_params['upload'] = true;
  622.         }
  623.         if(isset($params['delete']) && $this->IsTrue($params['delete']) && $this->GetPreference('show_filemanagement')) 
  624.         {
  625.             $session_params['delete'] = true;
  626.         }
  627.         if(isset($params['create_dirs']) && $this->IsTrue($params['create_dirs']) && $this->GetPreference('show_filemanagement')) 
  628.         {
  629.             $session_params['create_dirs'] = true;
  630.         }
  631.         if(isset($params['add_txt']))
  632.         {
  633.             $session_params['add_txt'] = trim($params['add_txt']);
  634.         }
  635.         if(isset($params['size']))
  636.         {
  637.             $session_params['size'] = intval($params['size']);
  638.         }
  639.         if(isset($params['maxlength']))
  640.         {
  641.             $session_params['maxlength'] = intval($params['maxlength']);
  642.         }
  643.         if(isset($params['scaling_width']))
  644.         {
  645.             $session_params['scaling_width'] = intval($params['scaling_width']);
  646.         }
  647.         if(isset($params['scaling_height']))
  648.         {
  649.             $session_params['scaling_height'] = intval($params['scaling_height']);
  650.         }
  651.         if(isset($params['show_thumbfiles']))
  652.         {
  653.             $session_params['show_thumbfiles'] = intval($params['show_thumbfiles']);
  654.         }
  655.         if(isset($params['allow_scaling']))
  656.         {
  657.             $session_params['allow_scaling'] = intval($params['allow_scaling']);
  658.         }
  659.         if(isset($params['create_thumbs']))
  660.         {
  661.             $session_params['create_thumbs'] = intval($params['create_thumbs']);
  662.         }
  663.         if(isset($params['allow_upscaling']))
  664.         {
  665.             $session_params['allow_upscaling'] = intval($params['allow_upscaling']);
  666.         }
  667.         
  668.         if($session_params['mode'] == 'dropdown') {
  669.             $session_params['dir'] = $dir;
  670.             $session_params['start_dir'] = $dir;
  671.         }
  672.         $_SESSION['GPFP_' . ($id == '' ? 'm1_' : $id).$name] = $session_params;
  673.         if($config['debug'])
  674.         {
  675.             debug_buffer($session_params,'GBFilePicker session vars; input = "'.$id.$name.'"; function SetInputParams()');
  676.         }
  677.         $session_params['dir'] = $dir;
  678.         return $session_params;
  679.     }
  680.     
  681.     
  682.     /**
  683.      * Gets the params of a filepicker input
  684.      * 
  685.      * @since 1.1
  686.      * @access private
  687.      *
  688.      * @param string $id - the id of the moduleinstance that creates the filepicker input
  689.      * @param string $name - the name of the input field
  690.      *
  691.      * @return array 
  692.      * @ignore
  693.      */
  694.     function &GetInputParams($id, $name)
  695.     {
  696.         $name   = str_replace(' ','_',$name);
  697.         $params = array(
  698.             'id'                       => $id, 
  699.             'name'                     => $name,
  700.             'module'                   => $this->GetName(),
  701.             'start_dir'                => '', 
  702.             'feu_access'               => $this->CleanArray(explode(',',$this->GetPreference('feu_access'))),
  703.             'restrict_users_diraccess' => intval($this->GetPreference('restrict_users_diraccess', false)),
  704.             'input_template'           => 'input.tpl',
  705.             'filebrowser_template'     => 'fileBrowser.tpl',
  706.             'prompt'                   => '',
  707.             'media_type'               => 'image',
  708.             'file_extensions'          => array('jpg','jpeg','gif','png'),
  709.             'exclude_prefix'           => '',
  710.             'exclude_sufix'            => '',
  711.             'include_prefix'           => '',
  712.             'include_sufix'            => '',
  713.             'show_subdirs'             => intval($this->_isAdmin),
  714.             'mode'                     => 'dropdown',
  715.             'allow_none'               => true,
  716.             'lock_input'               => intval(!$this->_isAdmin),
  717.             'upload'                   => intval($this->_isAdmin || $this->GetPreference('show_filemanagement', false)),
  718.             'delete'                   => intval($this->_isAdmin || $this->GetPreference('show_filemanagement', false)),
  719.             'create_dirs'              => intval($this->_isAdmin || $this->GetPreference('show_filemanagement', false)),
  720.             'add_txt'                  => 'onchange="GBFP.ToggleThumbnail(\'#' . $id.$name . '\', jQuery(\'#' . $id.$name . ' option:selected:first\').attr(\'thumbnail\'),this.value,this.value)"',
  721.             'size'                     => '',
  722.             'maxlength'                => '',
  723.             'scaling_width'            => intval($this->GetPreference('scaling_width', '')),
  724.             'scaling_height'           => intval($this->GetPreference('scaling_height', '')),
  725.             'show_thumbfiles'          => intval($this->GetPreference('show_thumbfiles', false)),
  726.             'allow_scaling'            => intval($this->GetPreference('allow_scaling', false) || $this->_isAdmin),
  727.             'create_thumbs'            => intval($this->GetPreference('create_thumbs', true)),
  728.             'allow_upscaling'          => intval($this->GetPreference('allow_upscaling', false) || $this->_isAdmin));
  729.         
  730.         global $CMS_ADMIN_PAGE;
  731.         $gCms   = cmsms();
  732.         $config =& $gCms->GetConfig();
  733.         if(isset($CMS_ADMIN_PAGE) && $CMS_ADMIN_PAGE == 1) 
  734.         {
  735.             check_login();
  736.             if(!$this->CheckPermission('Use GBFilePicker'))
  737.             {
  738.                 return false;
  739.             }
  740.             if(!$this->_isAdmin && $params['restrict_users_diraccess'])
  741.             {
  742.                 $params['start_dir'] = $this->_username;
  743.             }
  744.         }
  745.         else
  746.         {
  747.             if( !$feusers =& $this->GetModuleInstance('FrontEndUsers' ) )
  748.             {
  749.                 return false;
  750.             }
  751.             if(!$userid = $feusers->LoggedInId())
  752.             {
  753.                 return false;
  754.             }
  755.             if(!$groups = $feusers->GetMemberGroupsArray($userid))
  756.             {
  757.                 return false;
  758.             }
  759.             $access = false;
  760.             foreach($groups as $_group) 
  761.             {
  762.                 if(in_array($_group['groupid'],$params['feu_access'])) 
  763.                 {
  764.                     $access = true;
  765.                     break;
  766.                 }
  767.             }
  768.             if(!$access)
  769.             {
  770.                 return false;
  771.             }
  772.             
  773.             $params['restrict_users_diraccess'] = true;
  774.             $params['start_dir'] = $feusers->GetPreference('image_destination_path', 'feusers') . '/' . $feusers->GetUserName($userid);
  775.         }
  776.         
  777.         
  778.         if($config['debug'])
  779.         {
  780.             debug_buffer($params,'GBFilePicker input params; input = "'.$id.$name.'"; function GetInputParams()');
  781.         }
  782.         
  783.         if(isset($_SESSION['GPFP_' . ($id == '' ? 'm1_' : $id).$name]))
  784.         {
  785.             $params = array_merge($params, $_SESSION['GPFP_' . ($id == '' ? 'm1_' : $id).$name]);
  786.             return $params;
  787.         }
  788.         
  789.         return $params;
  790.     }
  791.     
  792.     /**
  793.      * Gets the file type of a file.
  794.      * 
  795.      * @since 1.0
  796.      * @access public
  797.      *
  798.      * @param string $file - the path to a file
  799.      * @param string $use_mime - set to true to detect file type by mime type
  800.      *
  801.      * @return string - the mime-type or file extension
  802.      */
  803.     function GetFileType($file, $use_mime = false) 
  804.     {
  805.         $filetype = '';
  806.         if($use_mime)
  807.         {
  808.             if (version_compare(PHP_VERSION, '5.3.0') >= 0 && function_exists('finfo_open')) 
  809.             {
  810.                 $finfo    = finfo_open(FILEINFO_MIME_TYPE); 
  811.                 $filetype = strtolower(finfo_file($finfo, $file));
  812.                 finfo_close($finfo);
  813.             }
  814.             else if(function_exists('mime_content_type')) 
  815.             {
  816.                 $filetype = strtolower(mime_content_type($file));
  817.             }
  818.         }
  819.         else
  820.         {
  821.             $fileinfo = pathinfo($file);
  822.             if(isset($fileinfo['extension']))
  823.             {
  824.                 $filetype = strtolower($fileinfo['extension']);
  825.             }
  826.         }
  827.         return $filetype;
  828.     }
  829.     
  830.     
  831.     /**
  832.      * Create a dropdown form element containing a list of files that match certain conditions
  833.      *
  834.      * @since 1.0
  835.      * @access public
  836.      *
  837.      * @param string $id - the id of the moduleinstance that creates the filepicker input
  838.      * @param string $name - the name of the input field
  839.      * @param string $dir - the directory to list the files of (may be relative to uploads dir or absolute path)
  840.      * @param string $selected - the preselected value
  841.      * @param array|string$excl_prefix - fileprefixes to exclude (array('foo','bar',...) or csv)
  842.      * @param array|string$incl_prefix - fileprefixes to include (array('foo','bar',...) or csv)
  843.      * @param array|string$excl_sufix - filesufixes to exclude (array('foo','bar',...) or csv)
  844.      * @param array|string$incl_sufix - filesufixes to include (array('foo','bar',...) or csv)
  845.      * @param array|string$file_ext - filesufixes to include (array('foo','bar',...) or csv)
  846.      * @param string $media_type - file or image
  847.      * @param boolean $allow_none - set to false to hide the option 'none'
  848.      * @param string $add_txt - any additional text that will be added to the html input when tag is rendered
  849.      *
  850.      * @return string - the HTML output of a select element with options
  851.      */
  852.     function CreateFileDropdown($id, $name, $dir = '', $selected = '', $excl_prefix = '', $incl_prefix = '', 
  853.         $excl_sufix = '', $incl_sufix = '', $file_ext = '', $media_type = '', $allow_none = true, $add_txt = '')
  854.     {
  855.         $files =& $this->GetInputFiles($id, $name, $dir, true);
  856.         if(empty($files) && !$allow_none)
  857.         {
  858.             return $this->lang('dir_empty');
  859.         }
  860.         $dropdown = '<select class="GBFP_dropdown GBFP_'.$media_type.'" name="'.$id.$name.'" id="'.$id.$name.'"';
  861.         if($add_txt != '')
  862.         {
  863.             $dropdown .= ' '.$add_txt;
  864.         }
  865.         $dropdown .= '>';
  866.         if( $allow_none )
  867.         {
  868.             $dropdown .= '<option value=""';
  869.             if($selected == '')
  870.             {
  871.                 $dropdown .= ' selected="selected"';
  872.             }
  873.             $dropdown .= ' thumbnail="">--- '.lang('none').' ---</option>';
  874.         }
  875.         foreach( $files as $file )
  876.         {
  877.             $dropdown .= '<option value="'.$file->relurl.'"';
  878.             if($file->relurl == $selected)
  879.             {
  880.                 $dropdown .= ' selected="selected"';
  881.             }
  882.             if($file->is_image && $media_type == 'image')
  883.             {
  884.                 $dropdown .= ' thumbnail="'.(isset($file->thumburl) && $file->thumburl!=''?$file->thumburl:$file->fullurl).'"';
  885.             }
  886.             $dropdown .= '>'.$file->basename.'</option>';
  887.         }
  888.         $dropdown .= "</select>";
  889.         return $dropdown;
  890.     }
  891.     
  892.     
  893.     /**
  894.      * Return an array containing a list of files in a directory related to a certain input.<br />
  895.      * Performs a non recursive search.
  896.      *
  897.      * @since 1.1
  898.      * @access public
  899.      *
  900.      * @param string $id - the id of the moduleinstance that creates the filepicker input
  901.      * @param string $name - the name of the input field
  902.      * @param string $dir - the directory to list the files of (may be relative to uploads dir or absolute path)
  903.      * @param bool $excl_dirs 
  904.      *
  905.      * @return array 
  906.      */
  907.     function &GetInputFiles($id, $name, $dir)
  908.     {
  909.         $params =& $this->GetInputParams($id, $name);
  910.         return $this->GetFiles($dir, $params['exclude_prefix'], 
  911.             $params['include_prefix'], $params['exclude_sufix'], 
  912.             $params['include_sufix'], $params['file_extensions'], 
  913.             $params['media_type'], ($params['mode'] == 'dropdown' ? true : !$params['show_subdirs']), $params['show_thumbfiles'],
  914.             $params['create_thumbs']);
  915.     }
  916.     
  917.     
  918.     /**
  919.      * Return an array containing a list of files in a directory.<br />
  920.      * Performs a non recursive search.
  921.      *
  922.      * @since 1.0
  923.      * @access public
  924.      *
  925.      * @param string $dir - the directory to list the files of (may be relative to uploads dir or absolute path)
  926.      * @param array|string$excl_prefix - fileprefixes to exclude (array('foo','bar',...) or csv)
  927.      * @param array|string$incl_prefix - fileprefixes to include (array('foo','bar',...) or csv)
  928.      * @param array|string$excl_sufix - filesufixes to exclude (array('foo','bar',...) or csv)
  929.      * @param array|string$incl_sufix - filesufixes to include (array('foo','bar',...) or csv)
  930.      * @param array|string$file_ext - filesufixes to include (array('foo','bar',...) or csv)
  931.      * @param string $media_type - file or image
  932.      * @param boolean $excl_dirs - set to true to exclude directories
  933.      * @param boolean $show_thumbfiles 
  934.      * @param boolean $create_thumbs = true
  935.      *
  936.      * @return array 
  937.      */
  938.     function &GetFiles($dir = '', $excl_prefix = '', $incl_prefix = '', $excl_sufix = '', 
  939.         $incl_sufix = '', $file_ext = '', $media_type = 'image', $excl_dirs = false, $show_thumbfiles = false, $create_thumbs = true)
  940.     {
  941.         $gCms   = cmsms();
  942.         $config =& $gCms->GetConfig();
  943.         $files  = Array();
  944.         $dir    = $this->CleanPath($dir);
  945.         $url    = $this->CleanURL($dir);
  946.         
  947.         if(!file_exists($dir))
  948.         {
  949.             return $files;
  950.         }
  951.         
  952.         if(!is_array($excl_prefix))
  953.         {
  954.             $excl_prefix = $this->CleanArray(explode(',',$excl_prefix));
  955.         }
  956.         
  957.         if(!is_array($incl_prefix))
  958.         {
  959.             $incl_prefix = $this->CleanArray(explode(',',$incl_prefix));
  960.         }
  961.         
  962.         if(!is_array($excl_sufix))
  963.         {
  964.             $excl_sufix = $this->CleanArray(explode(',',$excl_sufix));
  965.         }
  966.         
  967.         if(!is_array($incl_sufix))
  968.         {
  969.             $incl_sufix = $this->CleanArray(explode(',',$incl_sufix));
  970.         }
  971.         
  972.         if(!is_array($file_ext))
  973.         {
  974.             $file_ext = $this->CleanArray(explode(',',$file_ext));
  975.         }
  976.         
  977.         $_file_ext = array('jpg','jpeg','gif','png');
  978.         if($media_type == 'image') 
  979.         {
  980.             $file_ext = $this->CleanArray(array_intersect($_file_ext, $file_ext));
  981.             if(empty($file_ext))
  982.             {
  983.                 $file_ext = $_file_ext;
  984.             }
  985.         }
  986.         
  987.         $excl_prefix = $this->CleanArray(array_diff($excl_prefix, $incl_prefix));
  988.         $excl_sufix  = $this->CleanArray(array_diff($excl_sufix, $incl_sufix));
  989.         
  990.         $d = dir($dir);
  991.         while ($entry = $d->read()) 
  992.         {
  993.             if ($entry[0] == '.' || (is_dir($dir.$entry) && $excl_dirs))
  994.             {
  995.                 continue;
  996.             }
  997.             
  998.             $skip        = false;
  999.             $incl_thumbs = false;
  1000.             
  1001.             $file = new stdClass();
  1002.             $file->is_dir = is_dir($dir.$entry);
  1003.             $fileinfo = pathinfo($dir.$entry);
  1004.             foreach($fileinfo as $k=>$v) 
  1005.             {
  1006.                 $file->$k = $v;
  1007.             }
  1008.             if(!$file->is_dir) 
  1009.             {
  1010.                 foreach($excl_prefix as $str) 
  1011.                 {
  1012.                     if(startswith($file->filename,$str)) 
  1013.                     {
  1014.                         $skip = true;
  1015.                         break;
  1016.                     }
  1017.                 }
  1018.                 if($skip) continue;
  1019.                 
  1020.                 foreach($incl_prefix as $str)
  1021.                 {
  1022.                     if(startswith($str,'thumb_')) 
  1023.                     {
  1024.                         $incl_thumbs = true;
  1025.                     }
  1026.                     if(!startswith($file->filename,$str)) 
  1027.                     {
  1028.                         $skip = true;
  1029.                         break;
  1030.                     }
  1031.                 }
  1032.                 if($skip) continue;
  1033.                 
  1034.                 foreach($excl_sufix as $str)
  1035.                 {
  1036.                     if(endswith($file->filename,$str)) 
  1037.                     {
  1038.                         $skip = true;
  1039.                         break;
  1040.                     }
  1041.                 }
  1042.                 if($skip) continue;
  1043.                 
  1044.                 foreach($incl_sufix as $str)
  1045.                 {
  1046.                     if(!endswith($file->filename,$str)) 
  1047.                     {
  1048.                         $skip = true;
  1049.                         break;
  1050.                     }
  1051.                 }
  1052.                 if($skip) 
  1053.                     continue;
  1054.             }
  1055.             
  1056.             if(startswith($entry,'thumb_') && !$show_thumbfiles && !$incl_thumbs)
  1057.             {
  1058.                 continue;
  1059.             }
  1060.             
  1061.             $filetype = $this->GetFileType($dir.$entry,$this->GetPreference('use_mimetype',false));
  1062.             if (!$file->is_dir && $media_type == 'image' && !in_array(str_replace('image/','',$filetype),$file_ext)) 
  1063.             {
  1064.                 continue;
  1065.             }
  1066.             else if(!$file->is_dir && $media_type != 'image' && !in_array($file->extension,$file_ext) && !empty($file_ext)) 
  1067.             {
  1068.                 continue;
  1069.             }
  1070.             
  1071.             $file->fullurl      = $url.$entry;
  1072.             $file->relurl       = trim(str_replace($config['uploads_url'],'',$file->fullurl),'/');
  1073.             $file->id           = munge_string_to_url($file->relurl);
  1074.             $file->last_modifed = filemtime($dir.$entry);
  1075.             
  1076.             if (!$file->is_dir)
  1077.             {
  1078.                 $file->is_image = false;
  1079.                 if (!$file->is_dir && in_array(str_replace('image/','',$filetype),array('jpg','jpeg','gif','png'))) 
  1080.                 {
  1081.                     $file->is_image = true;
  1082.                 }
  1083.                 $file->filetype = $filetype;
  1084.                 if($file->is_image) 
  1085.                 {
  1086.                     $file->thumbnail = '';
  1087.                     $file->thumburl  = '';
  1088.                     if ($show_thumbfiles || $incl_thumbs) 
  1089.                     {
  1090.                         $file->thumbnail = $this->GetThumbnail($dir.str_replace('thumb_', '', $entry), $create_thumbs);
  1091.                         $file->thumburl  = $this->GetThumbnail($dir.str_replace('thumb_', '', $entry), $create_thumbs,true);
  1092.                     } 
  1093.                     else 
  1094.                     {
  1095.                         $file->thumbnail = $this->GetThumbnail($dir.$entry, $create_thumbs);
  1096.                         $file->thumburl  = $this->GetThumbnail($dir.$entry, $create_thumbs,true);
  1097.                     }
  1098.                     
  1099.                     $file->imgsize = '';
  1100.                     $imgsize = @getimagesize($dir.$entry);
  1101.                     if ($imgsize) 
  1102.                     {
  1103.                         $file->imgsize = $imgsize[0] . ' x ' . $imgsize[1];
  1104.                     } 
  1105.                     else 
  1106.                     {
  1107.                         $file->imgsize = '&nbsp;';
  1108.                     }
  1109.                 }
  1110.                 $file->filesize = '';
  1111.                 $info = @stat($dir.$entry);
  1112.                 if ($info) 
  1113.                 {
  1114.                     $file->filesize = $info['size'];
  1115.                 }
  1116.                 $file->fileicon = $this->GetFileIcon($file->extension,false,true);
  1117.             }
  1118.             else 
  1119.             {
  1120.                 $file->fileicon = $this->GetFileIcon('',true);
  1121.             }
  1122.             $files[] = $file;
  1123.         }
  1124.         $d->close();
  1125.         usort($files, array($this, 'SortFiles'));
  1126.         return $files;
  1127.     }
  1128.     
  1129.     
  1130.     /**
  1131.      * Replaces all multiple DIRECTORY_SEPARATOR, dots and (multiple) slashes with one single DIRECTORY_SEPARATOR to make a clean save url parameter
  1132.      *
  1133.      * @since 1.0
  1134.      * @access public
  1135.      * 
  1136.      * @param string $path - the path to clean
  1137.      * @param string $full_path - true to prepend uploads path on return, false to return only the cleaned $path
  1138.      *
  1139.      * @return string - the clean path
  1140.      */
  1141.     function CleanPath($path, $full_path = true)
  1142.     {
  1143.         $gCms   = cmsms();
  1144.         $config =& $gCms->GetConfig();
  1145.         $path   = trim(str_replace(array($config['uploads_path'],$config['uploads_url']), '', $path),'/'.DIRECTORY_SEPARATOR);
  1146.         $path   = str_replace(DIRECTORY_SEPARATOR,'/',$path);
  1147.         $path   = trim(preg_replace('/(\/\.)|(\.\/)|(\/?\.\.\/?)/','/',$path),'./');
  1148.         if($full_path)
  1149.         {
  1150.             $path = preg_replace('/[\\/]+/',DIRECTORY_SEPARATOR, '/' . $path . '/');
  1151.             $path = $config['uploads_path'] . $path;
  1152.             return $path;
  1153.         }
  1154.         return trim(preg_replace('/[\\/]+/',DIRECTORY_SEPARATOR, $path) , DIRECTORY_SEPARATOR);
  1155.     }
  1156.     
  1157.     
  1158.     /**
  1159.      * Replaces all multiple slashes, dots and (multiple) backslashes with one single slash to make a clean save url parameter
  1160.      * 
  1161.      * @since 1.0
  1162.      * @access public
  1163.      *
  1164.      * @param string $url - the url to clean
  1165.      * @param string $full_url - true to prepend uploads url on return, false to return only the cleaned $url
  1166.      *
  1167.      * @return string - the clean url
  1168.      */
  1169.     function CleanURL($url, $full_url = true)
  1170.     {
  1171.         $gCms   = cmsms();
  1172.         $config =& $gCms->GetConfig();
  1173.         $url    = trim(str_replace(array($config['uploads_path'],$config['uploads_url']), '', $url),'/'.DIRECTORY_SEPARATOR);
  1174.         $url    = str_replace(DIRECTORY_SEPARATOR,'/',$url);
  1175.         $url    = trim(preg_replace('/(\/\.)|(\.\/)|(\/?\.\.\/?)/','/',$url),'./');
  1176.         if($full_url)
  1177.         {
  1178.             $url = preg_replace('/\/+/','/', '/' . str_replace('http://','',$url) . '/');
  1179.             $url = $config['uploads_url'] . $url;
  1180.             return $url;
  1181.         }
  1182.         return trim(preg_replace('/\/+/','/',$url),'/');
  1183.     }
  1184.     
  1185.     
  1186.     /**
  1187.      * Returns the thumbnail of a given image file.
  1188.      *
  1189.      * @since 1.0
  1190.      * @access public
  1191.      *
  1192.      * @param string $path - the path of the file to create a thumbnail of (can be relative to the uploads dir or absolute)
  1193.      * @param string $create_thumb - set to false if thumbnail may not be created if not exists
  1194.      * @param string $urlonly - set to true to get only the url to the thumbnail
  1195.      *
  1196.      * @return string - HTML img element or url
  1197.      */
  1198.     function GetThumbnail($path, $create_thumb = true, $urlonly = false) 
  1199.     {
  1200.         if($path == '')
  1201.         {
  1202.             return false;
  1203.         }
  1204.         $gCms     = cmsms();
  1205.         $config   =& $gCms->GetConfig();
  1206.         $filename = basename($path);
  1207.         $image    = '';
  1208.         $path     = $this->CleanPath(str_replace($filename,'',$path));
  1209.         $url      = $this->CleanUrl(str_replace($filename,'',$path));
  1210.         
  1211.         if (!file_exists($path . 'thumb_' . $filename) || filemtime($path . $filename) > filemtime($path . 'thumb_' . $filename))
  1212.         {
  1213.             if($create_thumb && !$this->HandleFileResizing($path . $filename, $path . 'thumb_' . $filename, get_site_preference('thumbnail_width', 96), get_site_preference('thumbnail_height',96),true,false,60,false))
  1214.             {
  1215.                 return;
  1216.             }
  1217.         }
  1218.         if($urlonly)
  1219.         {
  1220.             return $url . 'thumb_' . $filename;
  1221.         }
  1222.         return '<img class="GBFP_thumbnail" id="'.munge_string_to_url($filename).'_GBFP_thumbnail" src="' . $url . 'thumb_' . $filename . '" alt="' . str_replace($config['uploads_url'],'',$url) . $filename . '" title="' . str_replace($config['uploads_url'],'',$url) . $filename . '" />';
  1223.     }
  1224.     
  1225.     
  1226.     /**
  1227.      * Gets the icon for a file.<br />
  1228.      * If FileManager is installed it uses FileManager icons.<br />
  1229.      * Otherwise it uses own icons.
  1230.      *
  1231.      * @since 1.0
  1232.      * @access public
  1233.      *
  1234.      * @param string $ext - the file extension
  1235.      * @param bool $is_dir 
  1236.      * @param bool $is_image 
  1237.      * @return string - the HTML ouput of an image
  1238.      */
  1239.     function GetFileIcon($ext = '', $is_dir = false, $is_image = false) 
  1240.     {
  1241.         $gCms   = cmsms();
  1242.         $config =& $gCms->GetConfig();
  1243.         if($fm =& $this->GetModuleInstance('FileManager')) 
  1244.         {
  1245.             return $fm->GetFileIcon($ext, $is_dir);
  1246.         }
  1247.         if($is_dir)
  1248.         {
  1249.             return '<img class="GBFP_fileicon" src="'.$config['root_url'].'/modules/GBFilePicker/images/dir.gif" alt="" />';
  1250.         }
  1251.         if ($is_image) 
  1252.         {
  1253.             return '<img class="GBFP_fileicon" src="'.$config['root_url'].'/modules/GBFilePicker/images/images.gif" alt="" />';
  1254.         }
  1255.         return '<img class="GBFP_fileicon" src="'.$config['root_url'].'/modules/GBFilePicker/images/fileicon.gif" alt="" />';
  1256.     }
  1257.     
  1258.     
  1259.     /**
  1260.      * Gets the icon for the file operation. <br />
  1261.      * If FileManager is installed it uses FileManager icons. <br />
  1262.      * Otherwise it uses own icons<br />
  1263.      *
  1264.      * @since 1.0
  1265.      * @access public
  1266.      *
  1267.      * @param string $action - the file operation (e.g. 'delete')
  1268.      * @return string - the HTML ouput of an image
  1269.      */
  1270.     function GetActionIcon($action) 
  1271.     {
  1272.         $gCms   = cmsms();
  1273.         $config =& $gCms->GetConfig();
  1274.         if($fm =& $this->GetModuleInstance('FileManager')) 
  1275.         {
  1276.             return $fm->GetActionIcon($action);
  1277.         }
  1278.         else
  1279.         {
  1280.             return '<img class="GBFP_actionicon" src="'.$config['root_url'].'/modules/GBFilePicker/images/'.$action.'.gif" title="'.$action.'" alt="'.$action.'" />';
  1281.         }
  1282.     }
  1283.     
  1284.     
  1285.     /**
  1286.      * Checks if a directory is empty<br />
  1287.      * Taken from TinyMCE Module (extracted from filepicker).
  1288.      * @since 1.0
  1289.      * @access public
  1290.      * @param string $dir - the path to a directory
  1291.      * @return boolean 
  1292.      */
  1293.     function IsDirEmpty($dir) 
  1294.     {
  1295.         $d = dir($this->CleanPath($dir));
  1296.         while ($entry = $d->read())
  1297.         {
  1298.             if ($entry == '.') 
  1299.                 continue;
  1300.             if ($entry == '..') 
  1301.                 continue;
  1302.             return false;
  1303.         }
  1304.         return true;
  1305.     }
  1306.     
  1307.     
  1308.     /**
  1309.      * Sorts the files by filename; shows directories first<br />
  1310.      * this function is meant to be called from the php usort() function: usort($array,array($this,'SortFiles')<br />
  1311.      * Taken from TinyMCE filepicker.
  1312.      * @since 1.0
  1313.      * @access public
  1314.      * @param object $file1 - one file to compare <br />a php stdClass with properties (boolean) $is_dir - a flag that specifys if it is a dir or a file;<br />(string) $basename - the basename of the file;
  1315.      * @param object $file2 - the other file to compare <br />a php stdClass with properties (boolean) $is_dir - a flag that specifys if it is a dir or a file;<br />(string) $basename - the basename of the file;
  1316.      * @return integer -1 or 1
  1317.      */
  1318.     function SortFiles($file1, $file2) 
  1319.     {
  1320.         if ($file1->is_dir && !$file2->is_dir) return -1;
  1321.         if (!$file1->is_dir && $file2->is_dir) return 1;
  1322.         return strnatcasecmp($file1->basename, $file2->basename);
  1323.     }
  1324.     
  1325.     
  1326.     /**
  1327.      * resizes images<br />
  1328.      * Adapted from TinyMCE filepicker.
  1329.      * @since 1.0
  1330.      * @access public
  1331.      * 
  1332.      * @param string $source - the source path of the image
  1333.      * @param string $output - the destination path of the resized image
  1334.      * @param integer $new_width - the new wdth to scale the image to (if $keep_aspectratio is set to true this will be just some kind of max value)
  1335.      * @param integer $new_height - the new height to scale the image to (if $keep_aspectratio is set to true this will be just some kind of max value)
  1336.      * @param boolean $keep_aspectratio - set to false if image aspect ratio may be changed
  1337.      * @param boolean $allow_upscaling - set to true if user may enlarge the image
  1338.      * @param integer $quality - the quality of the new image (jpg only)
  1339.      * @param boolean $clean_path - set to false if source is the upload_tmp_dir
  1340.      *
  1341.      * @todo enable format change?
  1342.      */
  1343.     function HandleFileResizing($source, $output, $new_width, $new_height, $keep_aspectratio = true, $allow_upscaling = false, $quality = 100, $clean_path = true) 
  1344.     {
  1345.         if(!startswith($source,ini_get("upload_tmp_dir")) && $clean_path) {
  1346.             $source = $this->CleanPath(str_replace(basename($source),'',$source)).basename($source);
  1347.         }
  1348.         if($clean_path) {
  1349.             $output = $this->CleanPath(str_replace(basename($output),'',$output)).basename($output);
  1350.         }
  1351.         $img_data = @getimagesize($source);
  1352.         
  1353.         if (!$img_data)
  1354.         {
  1355.             return false;
  1356.         }
  1357.         
  1358.         $orig_image = '';
  1359.         switch($img_data['mime']) 
  1360.         {
  1361.             case 'image/jpeg': 
  1362.                 $orig_image = @imagecreatefromjpeg($source); 
  1363.                 break;
  1364.             case 'image/gif' : 
  1365.                 $orig_image = @imagecreatefromgif($source); 
  1366.                 break;
  1367.             case 'image/png' : 
  1368.                 $orig_image = @imagecreatefrompng($source); 
  1369.                 break;
  1370.             default: 
  1371.                 return false;
  1372.         }
  1373.         $orig_width   = @imagesx($orig_image);
  1374.         $orig_height  = @imagesy($orig_image);
  1375.         $aspectratio = $orig_width / $orig_height;
  1376.         
  1377.         $new_width  = floor($new_width);
  1378.         $new_height = floor($new_height);
  1379.         
  1380.         if($new_width <= 0 && $new_height > 0) { // force height
  1381.             $new_width = $orig_width;
  1382.             if($new_height > $orig_height && !$allow_upscaling) 
  1383.             {
  1384.                 $new_height = $orig_height;
  1385.             }
  1386.             if($keep_aspectratio) 
  1387.             {
  1388.                 $new_width = floor($new_height * $aspectratio);
  1389.             }
  1390.         }
  1391.         else if($new_height <= 0 && $new_width > 0) { // force width
  1392.             $new_height = $orig_height;
  1393.             if($new_width > $orig_width && !$allow_upscaling) 
  1394.             {
  1395.                 $new_width = $orig_width;
  1396.             }
  1397.             if($keep_aspectratio) 
  1398.             {
  1399.                 $new_height = floor($new_width / $aspectratio);
  1400.             }
  1401.         }
  1402.         else if($new_height > 0 && $new_width > 0) { // both
  1403.             if($new_width > $orig_width && !$allow_upscaling) 
  1404.             {
  1405.                 $new_width = $orig_width;
  1406.             }
  1407.             if($new_height > $orig_height && !$allow_upscaling) 
  1408.             {
  1409.                 $new_height = $orig_height;
  1410.             }
  1411.             $new_aspectratio = $new_width / $new_height;
  1412.             if($keep_aspectratio) 
  1413.             {
  1414.                 if($aspectratio > 1 && $new_aspectratio < 1) { // landscape to portrait
  1415.                     $_tmp = floor($new_width / $aspectratio);
  1416.                     if($_tmp > 0 && $_tmp <= $new_height)
  1417.                     {
  1418.                         $new_height = $_tmp;
  1419.                     }
  1420.                 }
  1421.                 else if($aspectratio < 1 && $new_aspectratio > 1) { // portrait to landscape
  1422.                     $_tmp = floor($new_height * $aspectratio);
  1423.                     if($_tmp > 0 && $_tmp <= $new_width)
  1424.                     {
  1425.                         $new_width = $_tmp;
  1426.                     }
  1427.                 }
  1428.                 else 
  1429.                 {
  1430.                     if($new_aspectratio < $aspectratio) 
  1431.                     {
  1432.                         $_tmp = floor($new_width / $aspectratio);
  1433.                         if($_tmp > 0 && $_tmp <= $new_height)
  1434.                         {
  1435.                             $new_height = $_tmp;
  1436.                         }
  1437.                     }
  1438.                     else if($new_aspectratio > $aspectratio) 
  1439.                     {
  1440.                         $_tmp = floor($new_height * $aspectratio);
  1441.                         if($_tmp > 0 && $_tmp <= $new_width)
  1442.                         {
  1443.                             $new_width = $_tmp;
  1444.                         }
  1445.                     }
  1446.                 }
  1447.             }
  1448.         }
  1449.         else 
  1450.         {
  1451.             $new_height = $orig_height;
  1452.             $new_width  = $orig_width;
  1453.         }
  1454.         
  1455.         if($new_width < 1)
  1456.         {
  1457.             $new_width = 1;
  1458.         }
  1459.         if($new_height < 1)
  1460.         {
  1461.             $new_height = 1;
  1462.         }
  1463.         
  1464.         $new_image = @imagecreatetruecolor(floor($new_width), floor($new_height));
  1465.         @imagecopyresampled($new_image, $orig_image, 0, 0, 0, 0, $new_width, $new_height, $orig_width, $orig_height);
  1466.         switch($img_data['mime']) 
  1467.         {
  1468.             case 'image/jpeg': return @imagejpeg($new_image, $output, $quality);
  1469.             case 'image/gif' : return @imagegif($new_image, $output);
  1470.             case 'image/png' : return @imagepng($new_image, $output);
  1471.             default          : return false;
  1472.         }
  1473.         
  1474.         /*
  1475.         $format = 'unchanged';
  1476.         if ($format=='unchanged') 
  1477.         {
  1478.             switch($img_data['mime']) 
  1479.             {
  1480.                 case 'image/jpeg': return @imagejpeg($newImage, $output, $quality);
  1481.                 case 'image/gif' : return @imagegif($newImage, $output);
  1482.                 case 'image/png' : return @imagepng($newImage, $output);
  1483.                 default          : return false;
  1484.             }
  1485.         } else 
  1486.         {
  1487.             switch ($format) 
  1488.             {
  1489.                 case 'jpg': return @imagejpeg($newImage, $output, $quality);
  1490.                 case 'gif': return @imagegif($newImage, $output);
  1491.                 case 'png': return @imagepng($newImage, $output);
  1492.                 default   : return;
  1493.             }
  1494.         }
  1495.         */
  1496.     }
  1497.     
  1498.     
  1499.     /**
  1500.      * Checks if a filename contains illegal chars<br />
  1501.      * Taken from TinyMCE Module.
  1502.      * @since 1.0
  1503.      * @access public
  1504.      * @param string $filename - the filename to check
  1505.      * @return boolean 
  1506.      */
  1507.     function ContainsIllegalChars($filename) 
  1508.     {
  1509.         if (strpos($filename, '\'') !== false) return true;
  1510.         if (strpos($filename, '"' ) !== false) return true;
  1511.         if (strpos($filename, '/' ) !== false) return true;
  1512.         if (strpos($filename, '\\') !== false) return true;
  1513.         if (strpos($filename, '&' ) !== false) return true;
  1514.         if (strpos($filename, '\$') !== false) return true;
  1515.         if (strpos($filename, '+' ) !== false) return true;
  1516.         return false;
  1517.     }
  1518.     
  1519.     
  1520.     /**
  1521.      * Checks if a var is empty. <br />
  1522.      * If $var is an array it recursivley checks all elements.
  1523.      *
  1524.      * @since 1.0
  1525.      * @access public
  1526.      *
  1527.      * @param mixed &$var - the var to check for empty value(s)
  1528.      * @param boolean $trim - true to trim off spaces
  1529.      * @param boolean $unset_empty_indexes - true to delete empty elements from array
  1530.      * @return boolean - true if empty, false if not
  1531.      */
  1532.     function IsVarEmpty(&$var, $trim = true, $unset_empty_indexes = false) 
  1533.     {
  1534.         if (is_array($var)) 
  1535.         {
  1536.             foreach ($var as $k=>$v) 
  1537.             {
  1538.                 if (!$this->IsVarEmpty($v)) 
  1539.                 {
  1540.                     return false;
  1541.                 }
  1542.                 
  1543.                 if($unset_empty_indexes)
  1544.                 {
  1545.                     unset($var[$k]);
  1546.                 }
  1547.                 return true;
  1548.             }
  1549.         }
  1550.         else if($trim && trim($var) == '') 
  1551.         {
  1552.             return true;
  1553.         }
  1554.         else if($var == '') 
  1555.         {
  1556.             return true;
  1557.         }
  1558.         return false;
  1559.     }
  1560.     
  1561.     
  1562.     /**
  1563.      * Removes empty elements from an array. <br />
  1564.      * (can be useful when using function explode to create the array from a csv)
  1565.      *
  1566.      * @since 1.0
  1567.      * @access public
  1568.      *
  1569.      * @param array $array - the array to clean up
  1570.      * @return array - an array without empty elements or an empty array
  1571.      */
  1572.     function CleanArray($array)
  1573.     {
  1574.         if (is_array($array))
  1575.         {
  1576.             foreach ($array as $k=>$v)
  1577.             {
  1578.                 if ($this->IsVarEmpty($v,true,true))
  1579.                 {
  1580.                     unset($array[$k]);
  1581.                 }
  1582.                 else 
  1583.                 {
  1584.                     if(is_array($v))
  1585.                     {
  1586.                         $v = $this->CleanArray($v);
  1587.                         if($this->IsVarEmpty($v,true,true))
  1588.                         {
  1589.                             unset($array[$k]);
  1590.                         }
  1591.                         else
  1592.                             $array[$k] = $v;
  1593.                     }
  1594.                 }
  1595.             }
  1596.             return $array;
  1597.         }
  1598.         return array();
  1599.     }
  1600.     
  1601.     
  1602.     /**
  1603.      * Checks if a value is really meant to be "true". <br />
  1604.      * Can be usefull when checking smarty params for the value true
  1605.      *
  1606.      * @since 1.0
  1607.      * @access public
  1608.      *
  1609.      * @param mixed $value - the value to check
  1610.      * @return bool 
  1611.      */
  1612.     function IsTrue($value)
  1613.     {
  1614.         return (strtolower($value) === 'true' || $value === 1 || $value === '1' || $value === true);
  1615.     }
  1616.     
  1617.     
  1618.     /**
  1619.      * Checks if a value is really meant to be "false". <br />
  1620.      * Can be usefull when checking smarty params for the value false
  1621.      *
  1622.      * @since 1.0
  1623.      * @access public
  1624.      *
  1625.      * @param mixed $value - the value to check
  1626.      * @return bool 
  1627.      */
  1628.     function IsFalse($value)
  1629.     {
  1630.         return (strtolower($value) === 'false' || $value === '0' || $value === 0 || $value === false || $value === '');
  1631.     }
  1632.     
  1633.     
  1634.     /**
  1635.      * Indicates if GBFilePicker output is already done<br />
  1636.      * can be useful to avoid ouput of javascript and css twice
  1637.      *
  1638.      * @since 1.1
  1639.      * @access public
  1640.      *
  1641.      * @return bool - true if output done; false if not
  1642.      */
  1643.     function Loaded()
  1644.     {
  1645.         return $this->_loaded;
  1646.     }
  1647.  
  1648. }
  1649. ?>

Documentation generated on Sat, 25 Sep 2010 22:35:10 +0200 by phpDocumentor 1.4.0a2