Source for file GBFilePicker.module.php
Documentation is available at GBFilePicker.module.php
#-------------------------------------------------------------------------------
# Module : GBFilePicker (c) 2010 by Georg Busch (georg.busch@gmx.net) is a file
# picker tool for CMS Made Simple
# The projects homepage is: dev.cmsmadesimple.org/projects/gbfilepicker
# CMS Made Simple is (c) 2004-2010 by Ted Kulp
# The projects homepage is: cmsmadesimple.org
# File : GBFilePicker.module.php
# Purpose: main module class
#-------------------------------------------------------------------------------
* Class definition and methods for GBFilePicker
* Main class of the GBFilePicker Module
* @author Georg Busch (NaN)
* @copyright 2010 Georg Busch (NaN)
if(isset
($CMS_ADMIN_PAGE) &&
$CMS_ADMIN_PAGE ==
1)
$userops =
& $gCms->GetUserOperations();
$this->_isAdmin =
$userops->UserInGroup(get_userid(false),1);
$this->_username =
$userops->LoadUserById(get_userid(false))->username;
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetName
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetFriendlyName
function GetFriendlyName()
return $this->Lang('GBFilePicker');
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetVersion
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetHelp
return $this->lang('help');
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetAuthor
return 'Georg Busch (NaN)';
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetChangeLog
return $this->lang('changelog');
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#HasAdmin
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetAdminSection
function GetAdminSection()
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetAdminDescription
function GetAdminDescription()
return $this->lang('admindescription');
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#VisibleToAdminUser
function VisibleToAdminUser()
return $this->Checkpermission('Manage GBFilePicker');
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#MinimumCMSVersion
function MinimumCMSVersion()
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#MaximumCMSVersion
function MaximumCMSVersion()
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#InstallPostMessage
function InstallPostMessage()
return $this->Lang('post_install', $this->GetVersion());
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#UninstallPostMessage
function UninstallPostMessage()
return $this->Lang('post_uninstall', $this->GetVersion());
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#UninstallPreMessage
function UninstallPreMessage()
return $this->Lang('confirm_uninstall');
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#HandlesEvents
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#DoAction
function DoAction($action, $id, $params, $returnid =
'')
parent::DoAction($action, $id, $params, $returnid);
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#SetParameters
* @todo create list of allowed parameters!
#$this->RestrictUnknownParams();
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#HasCapability
function HasCapability($capability,$params =
array())
case 'content_attributes':
* @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetContentBlockInput
function GetContentBlockInput($blockname,$value,$params,$adding =
false)
if($this->CheckPermission('Use GBFilePicker'))
#-------------------------------------------------------------------------------
# Not part of the CMSms module API
#-------------------------------------------------------------------------------
* Creates the filepicker input.
* @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)
* @param string $id - the id of the moduleinstance that creates the filepicker input
* @param string $name - the name of the input field
* @param string $value - the preselected value
* @param array $params - the input params (param_name => param_value):<br /><ul>
* <li>(string) 'feu_access' => a csv of group ids of the frontend users module,</li>
* <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>
* <li>(string) 'input_template' => specify wich template should be used to create the input elements (default is GBFilePickers input.tpl),</li>
* <li>(string) 'filebrowser_template' => specify wich template should be used to create the input elements (default is GBFilePickers fileBrowser.tpl),</li>
* <li>(string) 'prompt' => the prompt of the input field,</li>
* <li>(string) 'media_type' => the media type (at the moment only image / file are supported),</li>
* <li>(mixed) 'file_extensions' => an array or a csv of allowed file extensions,</li>
* <li>(mixed) 'exclude_prefix' => an array or a csv of file prefixes that will be excluded,</li>
* <li>(mixed) 'exclude_sufix' => an array or a csv of file suffixes that will be excluded,</li>
* <li>(mixed) 'include_prefix' => an array or a csv of file prefixes that will be included,</li>
* <li>(mixed) 'include_sufix' => an array or a csv of file suffixes that will be included,</li>
* <li>(boolean) 'show_subdirs' => set to true if user may browse subdirs (notice: this has no effect if you are admin)</li>
* <li>(string) 'mode' => dropdown/browser,</li>
* <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>
* <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>
* <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>
* <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>
* <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>
* <li>(string) 'add_txt' => any additional text that will be added to the html input when tag is rendered,</li>
* <li>(integer) 'size' => the size of the textinput ('mode' => 'browser', 'media_type' => 'file' only),</li>
* <li>(integer) 'maxlength' => the max length of the textinput ('mode' => 'browser', 'media_type' => 'file' only),</li>
* <li>(integer) 'scaling_width' => the default width of images when allow_scaling is true,</li>
* <li>(integer) 'scaling_height' => the default height of images when allow_scaling is true,</li>
* <li>(boolean) 'show_thumbfiles' => set to true if thumbs should also be shown as regular files,</li>
* <li>(boolean) 'allow_scaling' => set to false if user may not scale the images on upload,</li>
* <li>(boolean) 'create_thumbs' => set to false if the module may not create thumbs for the input,</li>
* <li>(boolean) 'allow_upscaling' => set to true if user may enlarge the images</li></ul>
* @return string - the HTML output of the filepicker
if(!$params =
& $this->SetInputParams(&$module, $id, $name, $params))
$value =
$this->CleanUrl($value, false);
$config =
& $gCms->GetConfig();
$module->smarty->assign('thumb_width', get_site_preference('thumbnail_width',96));
$module->smarty->assign('thumb_height', get_site_preference('thumbnail_height','auto'));
$module->smarty->assign('gbfp_thumburl', $this->GetThumbnail($value, $params['create_thumbs'],true));
$module->smarty->assign('gbfp_value', $value);
$module->smarty->assign('gbfp_loaded', $this->_loaded);
if($params['mode'] ==
'dropdown')
#if($params['add_txt'] == '')
# $params['add_txt'] = 'onchange="GBFP.ToggleThumbnail(\'#' . $id.$name . '\', jQuery(\'#' . $id.$name . ' option:selected:first\').attr(\'thumbnail\'),this.value,this.value)"';
$params['exclude_prefix'],
$params['include_prefix'],
$params['exclude_sufix'],
$params['include_sufix'],
$params['file_extensions'],
$upload_url =
str_replace('&','&',$this->CreateLink(($id ==
'' ?
'm1_' :
$id), 'fileBrowser', '', $this->lang('upload'),array('name' =>
$name,'disable_theme'=>
true,'dir'=>
($this->_isAdmin?
$params['dir']:
'')),'', true));
$upload_btn =
'<input disabled="disabled" type="button" value="'.
$this->lang('upload').
'"
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;" />';
$this->CreateLink(($id ==
'' ?
'm1_' :
$id),
$reload_btn =
'<input disabled="disabled" type="button" value="'.
$this->lang('reload_dropdown').
'"
id="'.
$id.
$name.
'_GBFP_reload" class="GBFP_reload GBFP_button GBFP_disabled" onclick="GBFP.ReloadDropdown(\''.
$reload_url.
'\',\''.
$id.
$name.
'\'); return false;" />';
else if($params['mode'] ==
'browser')
if($params['media_type'] ==
'image')
$input =
'<input id="'.
$id.
$name.
'" type="hidden" name="'.
$id.
$name.
'" value="' .
$value .
'" />';
else if($params['media_type'] ==
'file')
$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 .
'" />';
$browse_url =
str_replace('&','&',$this->CreateLink(($id ==
'' ?
'm1_' :
$id), 'fileBrowser', '', $this->lang('browse_'.
$params['media_type']),array('name' =>
$name,'disable_theme'=>
true,'dir'=>
($this->_isAdmin?
$params['dir']:
'')),'', true));
$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;" />';
if($params['allow_none'])
$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;" />';
debug_buffer($params,'GBFilePicker input params; input = "'.
$id.
$name.
'"; function CreateFilePickerInput() ');
$module->smarty->assign('gbfp_input', $input);
$module->smarty->assign('gbfp_browse_button', $browse_btn);
$module->smarty->assign('gbfp_reload_button', $reload_btn);
$module->smarty->assign('gbfp_reload_url', $reload_url);
$module->smarty->assign('gbfp_upload_button', $upload_btn);
$module->smarty->assign('gbfp_clear_button', $clear_btn);
$module->smarty->assign('gbfp_id', $id);
$module->smarty->assign('gbfp_name', $name);
$module->smarty->assign('gbfp_inputid', $id.
$name);
$module->smarty->assign('gbfp_inputname', $name);
$module->smarty->assign('gbfp_upload_url', $upload_url);
$module->smarty->assign('gbfp_browse_url', $browse_url);
$module->smarty->assign('gbfp_title', $this->Lang('filebrowser'));
$module->smarty->assign('gbfp_browse_url', $browse_url);
foreach($params as $k =>
$v)
$module->smarty->assign('gbfp_'.
$k,$v);
$module->smarty->assign('gbfp_reload_dir_text', $this->lang('reload_dir'));
$module->smarty->assign('gbfp_clear_cache_text', $this->lang('clear_cache'));
$module->smarty->assign('gbfp_close_text', $this->lang('close'));
if(endswith($params['input_template'],'.tpl'))
return $module->ProcessTemplate($params['input_template']);
return $module->ProcessTemplateFromDatabase($params['input_template']);
* Sets the params of a filepicker input and save them in a session var
* @param string $id - the id of the moduleinstance that creates the filepicker input
* @param string $name - the name of the input field
* @param array $params - the params
function &SetInputParams(&$module, $id, $name, $params =
array())
$config =
& $gCms->GetConfig();
$_SESSION['GPFP_' .
($id ==
'' ?
'm1_' :
$id).
$name] =
array();
if(!$session_params =
& $this->GetInputParams($id, $name))
if(isset
($params['prompt']))
$session_params['prompt'] =
$params['prompt'];
if(isset
($params['input_template']))
$session_params['input_template'] =
$params['input_template'];
if(isset
($params['filepicker_template']))
$session_params['filebrowser_template'] =
$params['filepicker_template'];
$session_params['module'] =
$module->GetName();
if(isset
($params['feu_access']))
$session_params['feu_access'] =
$this->CleanArray(explode(',',$params['feu_access']));
$session_params['feu_access'] =
$this->CleanArray($params['feu_access']);
if(isset
($params['restrict_users_diraccess']) &&
$this->IsFalse($params['restrict_users_diraccess']))
$session_params['restrict_users_diraccess'] =
false;
$dir =
$session_params['start_dir'];
if(isset
($params['dir']) &&
$params['dir'] !=
'')
$dir =
$this->CleanUrl($params['dir'],false);
if(isset
($CMS_ADMIN_PAGE) &&
$CMS_ADMIN_PAGE ==
1)
if(!$this->CheckPermission('Use GBFilePicker'))
$username =
$this->_username;
if( !$feusers =
& $this->GetModuleInstance('FrontEndUsers' ) )
if(!$userid =
$feusers->LoggedInId())
if(!$groups =
$feusers->GetMemberGroupsArray($userid))
foreach($groups as $_group)
if(in_array($_group['groupid'],$session_params['feu_access']))
$username =
$feusers->GetUserName($userid);
$params['media_type'] =
'image';
$params['restrict_users_diraccess'] =
true;
if(!$this->_isAdmin &&
$session_params['restrict_users_diraccess'])
$session_params['start_dir'] .=
'/' .
$dir;
$session_params['start_dir'] =
$dir;
$_dir =
$this->CleanPath($session_params['start_dir']);
if(isset
($params['media_type']))
$session_params['media_type'] =
strtolower($params['media_type']);
if($session_params['media_type'] ==
'file')
$session_params['file_extensions'] =
array();
if(isset
($params['file_extensions']) &&
$params['file_extensions'] !=
'')
if($session_params['media_type'] ==
'image')
$_file_ext =
$session_params['file_extensions'];
$session_params['file_extensions'] =
$_file_ext;
if(isset
($params['exclude_prefix']))
$session_params['exclude_prefix'] =
trim($params['exclude_prefix']);
if(isset
($params['exclude_sufix']))
$session_params['exclude_sufix'] =
trim($params['exclude_sufix']);
if(isset
($params['include_prefix']))
$session_params['include_prefix'] =
trim($params['include_prefix']);
if(isset
($params['include_sufix']))
$session_params['include_sufix'] =
trim($params['include_sufix']);
if(isset
($params['show_subdirs']) &&
$this->IsTrue($params['show_subdirs']))
$session_params['show_subdirs'] =
true;
if(isset
($params['mode']))
$session_params['mode'] =
strtolower($params['mode']);
if(isset
($params['allow_none']) &&
$this->IsFalse($params['allow_none']))
$session_params['allow_none'] =
intval(false);
if(isset
($params['lock_input']) &&
$this->IsFalse($params['lock_input']))
$session_params['lock_input'] =
intval(false);
if(isset
($params['upload']) &&
$this->IsTrue($params['upload']) &&
$this->GetPreference('show_filemanagement'))
$session_params['upload'] =
true;
if(isset
($params['delete']) &&
$this->IsTrue($params['delete']) &&
$this->GetPreference('show_filemanagement'))
$session_params['delete'] =
true;
if(isset
($params['create_dirs']) &&
$this->IsTrue($params['create_dirs']) &&
$this->GetPreference('show_filemanagement'))
$session_params['create_dirs'] =
true;
if(isset
($params['add_txt']))
$session_params['add_txt'] =
trim($params['add_txt']);
if(isset
($params['size']))
$session_params['size'] =
intval($params['size']);
if(isset
($params['maxlength']))
$session_params['maxlength'] =
intval($params['maxlength']);
if(isset
($params['scaling_width']))
$session_params['scaling_width'] =
intval($params['scaling_width']);
if(isset
($params['scaling_height']))
$session_params['scaling_height'] =
intval($params['scaling_height']);
if(isset
($params['show_thumbfiles']))
$session_params['show_thumbfiles'] =
intval($params['show_thumbfiles']);
if(isset
($params['allow_scaling']))
$session_params['allow_scaling'] =
intval($params['allow_scaling']);
if(isset
($params['create_thumbs']))
$session_params['create_thumbs'] =
intval($params['create_thumbs']);
if(isset
($params['allow_upscaling']))
$session_params['allow_upscaling'] =
intval($params['allow_upscaling']);
if($session_params['mode'] ==
'dropdown') {
$session_params['dir'] =
$dir;
$session_params['start_dir'] =
$dir;
$_SESSION['GPFP_' .
($id ==
'' ?
'm1_' :
$id).
$name] =
$session_params;
debug_buffer($session_params,'GBFilePicker session vars; input = "'.
$id.
$name.
'"; function SetInputParams()');
$session_params['dir'] =
$dir;
* Gets the params of a filepicker input
* @param string $id - the id of the moduleinstance that creates the filepicker input
* @param string $name - the name of the input field
function &GetInputParams($id, $name)
'module' =>
$this->GetName(),
'restrict_users_diraccess' =>
intval($this->GetPreference('restrict_users_diraccess', false)),
'input_template' =>
'input.tpl',
'filebrowser_template' =>
'fileBrowser.tpl',
'file_extensions' =>
array('jpg','jpeg','gif','png'),
'show_subdirs' =>
intval($this->_isAdmin),
'lock_input' =>
intval(!$this->_isAdmin),
'upload' =>
intval($this->_isAdmin ||
$this->GetPreference('show_filemanagement', false)),
'delete' =>
intval($this->_isAdmin ||
$this->GetPreference('show_filemanagement', false)),
'create_dirs' =>
intval($this->_isAdmin ||
$this->GetPreference('show_filemanagement', false)),
'add_txt' =>
'onchange="GBFP.ToggleThumbnail(\'#' .
$id.
$name .
'\', jQuery(\'#' .
$id.
$name .
' option:selected:first\').attr(\'thumbnail\'),this.value,this.value)"',
'scaling_width' =>
intval($this->GetPreference('scaling_width', '')),
'scaling_height' =>
intval($this->GetPreference('scaling_height', '')),
'show_thumbfiles' =>
intval($this->GetPreference('show_thumbfiles', false)),
'allow_scaling' =>
intval($this->GetPreference('allow_scaling', false) ||
$this->_isAdmin),
'create_thumbs' =>
intval($this->GetPreference('create_thumbs', true)),
'allow_upscaling' =>
intval($this->GetPreference('allow_upscaling', false) ||
$this->_isAdmin));
$config =
& $gCms->GetConfig();
if(isset
($CMS_ADMIN_PAGE) &&
$CMS_ADMIN_PAGE ==
1)
if(!$this->CheckPermission('Use GBFilePicker'))
if(!$this->_isAdmin &&
$params['restrict_users_diraccess'])
$params['start_dir'] =
$this->_username;
if( !$feusers =
& $this->GetModuleInstance('FrontEndUsers' ) )
if(!$userid =
$feusers->LoggedInId())
if(!$groups =
$feusers->GetMemberGroupsArray($userid))
foreach($groups as $_group)
if(in_array($_group['groupid'],$params['feu_access']))
$params['restrict_users_diraccess'] =
true;
$params['start_dir'] =
$feusers->GetPreference('image_destination_path', 'feusers') .
'/' .
$feusers->GetUserName($userid);
debug_buffer($params,'GBFilePicker input params; input = "'.
$id.
$name.
'"; function GetInputParams()');
if(isset
($_SESSION['GPFP_' .
($id ==
'' ?
'm1_' :
$id).
$name]))
$params =
array_merge($params, $_SESSION['GPFP_' .
($id ==
'' ?
'm1_' :
$id).
$name]);
* Gets the file type of a file.
* @param string $file - the path to a file
* @param string $use_mime - set to true to detect file type by mime type
* @return string - the mime-type or file extension
$finfo =
finfo_open(FILEINFO_MIME_TYPE);
$filetype =
strtolower(finfo_file($finfo, $file));
if(isset
($fileinfo['extension']))
* Create a dropdown form element containing a list of files that match certain conditions
* @param string $id - the id of the moduleinstance that creates the filepicker input
* @param string $name - the name of the input field
* @param string $dir - the directory to list the files of (may be relative to uploads dir or absolute path)
* @param string $selected - the preselected value
* @param array|string$excl_prefix - fileprefixes to exclude (array('foo','bar',...) or csv)
* @param array|string$incl_prefix - fileprefixes to include (array('foo','bar',...) or csv)
* @param array|string$excl_sufix - filesufixes to exclude (array('foo','bar',...) or csv)
* @param array|string$incl_sufix - filesufixes to include (array('foo','bar',...) or csv)
* @param array|string$file_ext - filesufixes to include (array('foo','bar',...) or csv)
* @param string $media_type - file or image
* @param boolean $allow_none - set to false to hide the option 'none'
* @param string $add_txt - any additional text that will be added to the html input when tag is rendered
* @return string - the HTML output of a select element with options
function CreateFileDropdown($id, $name, $dir =
'', $selected =
'', $excl_prefix =
'', $incl_prefix =
'',
$excl_sufix =
'', $incl_sufix =
'', $file_ext =
'', $media_type =
'', $allow_none =
true, $add_txt =
'')
if(empty($files) &&
!$allow_none)
return $this->lang('dir_empty');
$dropdown =
'<select class="GBFP_dropdown GBFP_'.
$media_type.
'" name="'.
$id.
$name.
'" id="'.
$id.
$name.
'"';
$dropdown .=
' '.
$add_txt;
$dropdown .=
'<option value=""';
$dropdown .=
' selected="selected"';
$dropdown .=
' thumbnail="">--- '.
lang('none').
' ---</option>';
foreach( $files as $file )
$dropdown .=
'<option value="'.
$file->relurl.
'"';
if($file->relurl ==
$selected)
$dropdown .=
' selected="selected"';
if($file->is_image &&
$media_type ==
'image')
$dropdown .=
' thumbnail="'.
(isset
($file->thumburl) &&
$file->thumburl!=
''?
$file->thumburl:
$file->fullurl).
'"';
$dropdown .=
'>'.
$file->basename.
'</option>';
$dropdown .=
"</select>";
* Return an array containing a list of files in a directory related to a certain input.<br />
* Performs a non recursive search.
* @param string $id - the id of the moduleinstance that creates the filepicker input
* @param string $name - the name of the input field
* @param string $dir - the directory to list the files of (may be relative to uploads dir or absolute path)
$params =
& $this->GetInputParams($id, $name);
return $this->GetFiles($dir, $params['exclude_prefix'],
$params['include_prefix'], $params['exclude_sufix'],
$params['include_sufix'], $params['file_extensions'],
$params['media_type'], ($params['mode'] ==
'dropdown' ?
true :
!$params['show_subdirs']), $params['show_thumbfiles'],
$params['create_thumbs']);
* Return an array containing a list of files in a directory.<br />
* Performs a non recursive search.
* @param string $dir - the directory to list the files of (may be relative to uploads dir or absolute path)
* @param array|string$excl_prefix - fileprefixes to exclude (array('foo','bar',...) or csv)
* @param array|string$incl_prefix - fileprefixes to include (array('foo','bar',...) or csv)
* @param array|string$excl_sufix - filesufixes to exclude (array('foo','bar',...) or csv)
* @param array|string$incl_sufix - filesufixes to include (array('foo','bar',...) or csv)
* @param array|string$file_ext - filesufixes to include (array('foo','bar',...) or csv)
* @param string $media_type - file or image
* @param boolean $excl_dirs - set to true to exclude directories
* @param boolean $show_thumbfiles
* @param boolean $create_thumbs = true
function &GetFiles($dir =
'', $excl_prefix =
'', $incl_prefix =
'', $excl_sufix =
'',
$incl_sufix =
'', $file_ext =
'', $media_type =
'image', $excl_dirs =
false, $show_thumbfiles =
false, $create_thumbs =
true)
$config =
& $gCms->GetConfig();
$_file_ext =
array('jpg','jpeg','gif','png');
if($media_type ==
'image')
while ($entry =
$d->read())
if ($entry[0] ==
'.' ||
(is_dir($dir.
$entry) &&
$excl_dirs))
$file->is_dir =
is_dir($dir.
$entry);
foreach($fileinfo as $k=>
$v)
foreach($excl_prefix as $str)
if(startswith($file->filename,$str))
foreach($incl_prefix as $str)
if(startswith($str,'thumb_'))
if(!startswith($file->filename,$str))
foreach($excl_sufix as $str)
if(endswith($file->filename,$str))
foreach($incl_sufix as $str)
if(!endswith($file->filename,$str))
if(startswith($entry,'thumb_') &&
!$show_thumbfiles &&
!$incl_thumbs)
$filetype =
$this->GetFileType($dir.
$entry,$this->GetPreference('use_mimetype',false));
if (!$file->is_dir &&
$media_type ==
'image' &&
!in_array(str_replace('image/','',$filetype),$file_ext))
else if(!$file->is_dir &&
$media_type !=
'image' &&
!in_array($file->extension,$file_ext) &&
!empty($file_ext))
$file->fullurl =
$url.
$entry;
$file->relurl =
trim(str_replace($config['uploads_url'],'',$file->fullurl),'/');
$file->id =
munge_string_to_url($file->relurl);
$file->last_modifed =
filemtime($dir.
$entry);
if (!$file->is_dir &&
in_array(str_replace('image/','',$filetype),array('jpg','jpeg','gif','png')))
$file->filetype =
$filetype;
if ($show_thumbfiles ||
$incl_thumbs)
$file->thumbnail =
$this->GetThumbnail($dir.
$entry, $create_thumbs);
$file->thumburl =
$this->GetThumbnail($dir.
$entry, $create_thumbs,true);
$file->imgsize =
$imgsize[0] .
' x ' .
$imgsize[1];
$file->imgsize =
' ';
$info =
@stat($dir.
$entry);
$file->filesize =
$info['size'];
$file->fileicon =
$this->GetFileIcon($file->extension,false,true);
usort($files, array($this, 'SortFiles'));
* Replaces all multiple DIRECTORY_SEPARATOR, dots and (multiple) slashes with one single DIRECTORY_SEPARATOR to make a clean save url parameter
* @param string $path - the path to clean
* @param string $full_path - true to prepend uploads path on return, false to return only the cleaned $path
* @return string - the clean path
$config =
& $gCms->GetConfig();
$path =
trim(str_replace(array($config['uploads_path'],$config['uploads_url']), '', $path),'/'.
DIRECTORY_SEPARATOR);
$path =
preg_replace('/[\\/]+/',DIRECTORY_SEPARATOR, '/' .
$path .
'/');
$path =
$config['uploads_path'] .
$path;
return trim(preg_replace('/[\\/]+/',DIRECTORY_SEPARATOR, $path) , DIRECTORY_SEPARATOR);
* Replaces all multiple slashes, dots and (multiple) backslashes with one single slash to make a clean save url parameter
* @param string $url - the url to clean
* @param string $full_url - true to prepend uploads url on return, false to return only the cleaned $url
* @return string - the clean url
function CleanURL($url, $full_url =
true)
$config =
& $gCms->GetConfig();
$url =
trim(str_replace(array($config['uploads_path'],$config['uploads_url']), '', $url),'/'.
DIRECTORY_SEPARATOR);
$url =
$config['uploads_url'] .
$url;
* Returns the thumbnail of a given image file.
* @param string $path - the path of the file to create a thumbnail of (can be relative to the uploads dir or absolute)
* @param string $create_thumb - set to false if thumbnail may not be created if not exists
* @param string $urlonly - set to true to get only the url to the thumbnail
* @return string - HTML img element or url
function GetThumbnail($path, $create_thumb =
true, $urlonly =
false)
$config =
& $gCms->GetConfig();
$url =
$this->CleanUrl(str_replace($filename,'',$path));
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))
return $url .
'thumb_' .
$filename;
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 .
'" />';
* Gets the icon for a file.<br />
* If FileManager is installed it uses FileManager icons.<br />
* Otherwise it uses own icons.
* @param string $ext - the file extension
* @return string - the HTML ouput of an image
function GetFileIcon($ext =
'', $is_dir =
false, $is_image =
false)
$config =
& $gCms->GetConfig();
if($fm =
& $this->GetModuleInstance('FileManager'))
return $fm->GetFileIcon($ext, $is_dir);
return '<img class="GBFP_fileicon" src="'.
$config['root_url'].
'/modules/GBFilePicker/images/dir.gif" alt="" />';
return '<img class="GBFP_fileicon" src="'.
$config['root_url'].
'/modules/GBFilePicker/images/images.gif" alt="" />';
return '<img class="GBFP_fileicon" src="'.
$config['root_url'].
'/modules/GBFilePicker/images/fileicon.gif" alt="" />';
* Gets the icon for the file operation. <br />
* If FileManager is installed it uses FileManager icons. <br />
* Otherwise it uses own icons<br />
* @param string $action - the file operation (e.g. 'delete')
* @return string - the HTML ouput of an image
$config =
& $gCms->GetConfig();
if($fm =
& $this->GetModuleInstance('FileManager'))
return $fm->GetActionIcon($action);
return '<img class="GBFP_actionicon" src="'.
$config['root_url'].
'/modules/GBFilePicker/images/'.
$action.
'.gif" title="'.
$action.
'" alt="'.
$action.
'" />';
* Checks if a directory is empty<br />
* Taken from TinyMCE Module (extracted from filepicker).
* @param string $dir - the path to a directory
while ($entry =
$d->read())
* Sorts the files by filename; shows directories first<br />
* this function is meant to be called from the php usort() function: usort($array,array($this,'SortFiles')<br />
* Taken from TinyMCE filepicker.
* @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;
* @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;
* @return integer -1 or 1
if ($file1->is_dir &&
!$file2->is_dir) return -
1;
if (!$file1->is_dir &&
$file2->is_dir) return 1;
* Adapted from TinyMCE filepicker.
* @param string $source - the source path of the image
* @param string $output - the destination path of the resized image
* @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)
* @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)
* @param boolean $keep_aspectratio - set to false if image aspect ratio may be changed
* @param boolean $allow_upscaling - set to true if user may enlarge the image
* @param integer $quality - the quality of the new image (jpg only)
* @param boolean $clean_path - set to false if source is the upload_tmp_dir
* @todo enable format change?
function HandleFileResizing($source, $output, $new_width, $new_height, $keep_aspectratio =
true, $allow_upscaling =
false, $quality =
100, $clean_path =
true)
if(!startswith($source,ini_get("upload_tmp_dir")) &&
$clean_path) {
switch($img_data['mime'])
$orig_width =
@imagesx($orig_image);
$orig_height =
@imagesy($orig_image);
$aspectratio =
$orig_width /
$orig_height;
$new_width =
floor($new_width);
$new_height =
floor($new_height);
if($new_width <=
0 &&
$new_height >
0) { // force height
$new_width =
$orig_width;
if($new_height >
$orig_height &&
!$allow_upscaling)
$new_height =
$orig_height;
$new_width =
floor($new_height *
$aspectratio);
else if($new_height <=
0 &&
$new_width >
0) { // force width
$new_height =
$orig_height;
if($new_width >
$orig_width &&
!$allow_upscaling)
$new_width =
$orig_width;
$new_height =
floor($new_width /
$aspectratio);
else if($new_height >
0 &&
$new_width >
0) { // both
if($new_width >
$orig_width &&
!$allow_upscaling)
$new_width =
$orig_width;
if($new_height >
$orig_height &&
!$allow_upscaling)
$new_height =
$orig_height;
$new_aspectratio =
$new_width /
$new_height;
if($aspectratio >
1 &&
$new_aspectratio <
1) { // landscape to portrait
$_tmp =
floor($new_width /
$aspectratio);
if($_tmp >
0 &&
$_tmp <=
$new_height)
else if($aspectratio <
1 &&
$new_aspectratio >
1) { // portrait to landscape
$_tmp =
floor($new_height *
$aspectratio);
if($_tmp >
0 &&
$_tmp <=
$new_width)
if($new_aspectratio <
$aspectratio)
$_tmp =
floor($new_width /
$aspectratio);
if($_tmp >
0 &&
$_tmp <=
$new_height)
else if($new_aspectratio >
$aspectratio)
$_tmp =
floor($new_height *
$aspectratio);
if($_tmp >
0 &&
$_tmp <=
$new_width)
$new_height =
$orig_height;
$new_width =
$orig_width;
@imagecopyresampled($new_image, $orig_image, 0, 0, 0, 0, $new_width, $new_height, $orig_width, $orig_height);
switch($img_data['mime'])
case 'image/jpeg':
return @imagejpeg($new_image, $output, $quality);
case 'image/gif' :
return @imagegif($new_image, $output);
case 'image/png' :
return @imagepng($new_image, $output);
if ($format=='unchanged')
switch($img_data['mime'])
case 'image/jpeg': return @imagejpeg($newImage, $output, $quality);
case 'image/gif' : return @imagegif($newImage, $output);
case 'image/png' : return @imagepng($newImage, $output);
case 'jpg': return @imagejpeg($newImage, $output, $quality);
case 'gif': return @imagegif($newImage, $output);
case 'png': return @imagepng($newImage, $output);
* Checks if a filename contains illegal chars<br />
* Taken from TinyMCE Module.
* @param string $filename - the filename to check
if (strpos($filename, '\'') !==
false) return true;
if (strpos($filename, '"' ) !==
false) return true;
if (strpos($filename, '/' ) !==
false) return true;
if (strpos($filename, '\\') !==
false) return true;
if (strpos($filename, '&' ) !==
false) return true;
if (strpos($filename, '\$') !==
false) return true;
if (strpos($filename, '+' ) !==
false) return true;
* Checks if a var is empty. <br />
* If $var is an array it recursivley checks all elements.
* @param mixed &$var - the var to check for empty value(s)
* @param boolean $trim - true to trim off spaces
* @param boolean $unset_empty_indexes - true to delete empty elements from array
* @return boolean - true if empty, false if not
function IsVarEmpty(&$var, $trim =
true, $unset_empty_indexes =
false)
else if($trim &&
trim($var) ==
'')
* Removes empty elements from an array. <br />
* (can be useful when using function explode to create the array from a csv)
* @param array $array - the array to clean up
* @return array - an array without empty elements or an empty array
foreach ($array as $k=>
$v)
* Checks if a value is really meant to be "true". <br />
* Can be usefull when checking smarty params for the value true
* @param mixed $value - the value to check
return (strtolower($value) ===
'true' ||
$value ===
1 ||
$value ===
'1' ||
$value ===
true);
* Checks if a value is really meant to be "false". <br />
* Can be usefull when checking smarty params for the value false
* @param mixed $value - the value to check
return (strtolower($value) ===
'false' ||
$value ===
'0' ||
$value ===
0 ||
$value ===
false ||
$value ===
'');
* Indicates if GBFilePicker output is already done<br />
* can be useful to avoid ouput of javascript and css twice
* @return bool - true if output done; false if not
Documentation generated on Sat, 25 Sep 2010 22:35:10 +0200 by phpDocumentor 1.4.0a2