ocPortal Developer's Guide: Themes
» Return to Contents
sources/themes.php
Global_functions_themes.php
Function summary
|
void
|
init__themes ()
|
|
URLPATH
|
find_theme_image (ID_TEXT id, boolean silent_fail, boolean leave_local, ?ID_TEXT theme, ?LANGUAGE_NAME lang, ?object db, boolean pure_only)
|
|
URLPATH
|
cdn_filter (URLPATH path)
|
|
?string
|
_search_img_file (ID_TEXT theme, ?LANGUAGE_NAME lang, ID_TEXT id, ID_TEXT dir)
|
void init__themes()
Standard code module initialisation function.
Parameters…
(No return value)
function init__themes()
{
global $IMG_CODES,$CDN_CONSISTENCY_CHECK,$RECORD_IMG_CODES,$RECORDED_IMG_CODES;
$IMG_CODES=array();
$CDN_CONSISTENCY_CHECK=array();
$RECORD_IMG_CODES=false;
$RECORDED_IMG_CODES=array();
}
URLPATH find_theme_image(ID_TEXT id, boolean silent_fail, boolean leave_local, ?ID_TEXT theme, ?LANGUAGE_NAME lang, ?object db, boolean pure_only)
Find the URL to the theme image of the specified ID. It searches various priorities, including language and theme overrides.
Parameters…
| Name |
id |
| Description |
The theme image ID |
| Type |
ID_TEXT |
| Name |
silent_fail |
| Description |
Whether to silently fail (i.e. not give out an error message when a theme image cannot be found) |
| Default value |
boolean-false |
| Type |
boolean |
| Name |
leave_local |
| Description |
Whether to leave URLs as relative local URLs |
| Default value |
boolean-false |
| Type |
boolean |
| Name |
theme |
| Description |
The theme to search in (NULL: users current theme) |
| Default value |
|
| Type |
?ID_TEXT |
| Name |
lang |
| Description |
The language to search for (NULL: users current language) |
| Default value |
|
| Type |
?LANGUAGE_NAME |
| Name |
db |
| Description |
The database to use (NULL: site database) |
| Default value |
|
| Type |
?object |
| Name |
pure_only |
| Description |
Whether to only search the default 'images' filesystem |
| Default value |
boolean-false |
| Type |
boolean |
Returns…
| Description |
The URL found (blank: not found) |
| Type |
URLPATH |
function find_theme_image($id,$silent_fail=false,$leave_local=false,$theme=NULL,$lang=NULL,$db=NULL,$pure_only=false)
{
global $RECORD_IMG_CODES;
if ($RECORD_IMG_CODES)
{
global $RECORDED_IMG_CODES;
if ((isset($GLOBALS['FORUM_DB'])) && ($db->connection_write!==$GLOBALS['FORUM_DB']->connection_write))
$RECORDED_IMG_CODES[serialize(array($id,$theme,$lang))]=1;
}
if ((substr($id,0,4)=='ocf_') && (is_file(get_file_base().'/themes/default/images/avatars/index.html'))) // Allow debranding of theme img dirs
{
$id=substr($id,4);
}
if ((isset($_GET['keep_theme_seed'])) && (get_param('keep_theme_seed',NULL)!==NULL) && (function_exists('has_specific_permission')) && (has_specific_permission(get_member(),'view_profiling_modes')))
{
require_code('themewizard');
$test=find_theme_image_themewizard_preview($id);
if ($test!==NULL) return $test;
}
if ($db===NULL) $db=$GLOBALS['SITE_DB'];
$true_theme=$GLOBALS['FORUM_DRIVER']->get_theme();
if ($theme===NULL) $theme=$true_theme;
global $USER_LANG_CACHED;
$true_lang=($USER_LANG_CACHED===NULL)?user_lang():$USER_LANG_CACHED;
if ($lang===NULL) $lang=$true_lang;
$truism=($theme==$true_theme) && ($lang==$true_lang);
$site=($GLOBALS['SITE_DB']==$db)?'site':'forums';
global $IMG_CODES;
if (!isset($IMG_CODES[$site]))
{
$cache=NULL;
if ($site=='site')
{
$cache=persistant_cache_get('THEME_IMAGES');
}
if (!isset($cache[$true_theme][$true_lang]))
{
$IMG_CODES[$site]=$db->query_select('theme_images',array('id','path'),array('theme'=>$true_theme,'lang'=>$true_lang));
$IMG_CODES[$site]=collapse_2d_complexity('id','path',$IMG_CODES[$site]);
if ($site=='site')
{
if ($cache===NULL) $cache=array();
$cache[$theme][$lang]=$IMG_CODES[$site];
persistant_cache_set('THEME_IMAGES',$cache);
}
} else
{
$IMG_CODES[$site]=$cache[$true_theme][$true_lang];
}
}
if ((!$truism) && (!$pure_only)) // Separate lookup, cannot go through $IMG_CODES
{
$path=$db->query_value_null_ok('theme_images','path',array('theme'=>$theme,'lang'=>$lang,'id'=>$id));
if ($path!==NULL)
{
if ((url_is_local($path)) && (!$leave_local))
{
$path=(($db->connection_write!=$GLOBALS['SITE_DB']->connection_write)?get_forum_base_url():((substr($path,0,22)=='themes/default/images/')?get_base_url():get_custom_base_url())).'/'.$path;
}
return cdn_filter($path);
}
}
if (($pure_only) || (!isset($IMG_CODES[$site][$id])) || (!$truism)/* || (($IMG_CODES[$site][$id]=='') && (!$silent_fail))*/)
{
$path=NULL;
$priorities=array();
if (!$pure_only) // Should do "images_custom" first, as this will also do a DB search
{
$priorities=array_merge($priorities,array(
array($theme,$lang,'images_custom'),
array($theme,'','images_custom'),
($lang==fallback_lang())?NULL:array($theme,fallback_lang(),'images_custom'),
));
}
// This will not do a DB search, just a filesystem search. The Theme Wizard makes these though
$priorities=array_merge($priorities,array(
array($theme,$lang,'images'),
array($theme,'','images'),
($lang==fallback_lang())?NULL:array($theme,fallback_lang(),'images'),
));
if ($theme!='default')
{
if (!$pure_only)
{
$priorities=array_merge($priorities,array(
array('default',$lang,'images_custom'),
array('default','','images_custom'),
($lang==fallback_lang())?NULL:array('default',fallback_lang(),'images_custom'),
));
}
$priorities=array_merge($priorities,array(
array('default',$lang,'images'),
array('default','','images'),
($lang==fallback_lang())?NULL:array('default',fallback_lang(),'images'),
));
}
foreach ($priorities as $i=>$priority)
{
if ($priority===NULL) continue;
if (($priority[2]=='images_custom') && ($priority[1]!='')) // Likely won't auto find
{
$smap=array('id'=>$id,'theme'=>$priority[0],'lang'=>$priority[1]);
$nql_backup=$GLOBALS['NO_QUERY_LIMIT'];
$GLOBALS['NO_QUERY_LIMIT']=true;
$truism_b=($priority[0]==$true_theme) && ((!multi_lang()) || ($priority[1]=='') || ($priority[1]===$true_lang));
$path=$truism_b?NULL:$db->query_value_null_ok('theme_images','path',$smap);
$GLOBALS['NO_QUERY_LIMIT']=$nql_backup;
if ($path!==NULL) // Make sure this isn't just the result file we should find at a lower priority
{
if (strpos($path,'/images/'.$id.'.')!==false) continue;
if ((array_key_exists('lang',$smap)) && (strpos($path,'/images/'.$smap['lang'].'/'.$id.'.')!==false)) continue;
break;
}
}
$test=_search_img_file($priority[0],$priority[1],$id,$priority[2]);
if ($test!==NULL)
{
$path_bits=explode('/',$test);
$path='';
foreach ($path_bits as $bit)
{
if ($path!='') $path.='/';
$path.=rawurlencode($bit);
}
break;
}
}
if ($db->connection_write==$GLOBALS['SITE_DB']->connection_write) // If guard is here because a MSN site can't make assumptions about the file system of the central site
{
if ((($path!==NULL) && ($path!='')) || (($silent_fail) && ($GLOBALS['SEMI_DEBUG_MODE'])))
{
$nql_backup=$GLOBALS['NO_QUERY_LIMIT'];
$GLOBALS['NO_QUERY_LIMIT']=true;
$db->query_delete('theme_images',array('id'=>$id,'theme'=>$theme,'lang'=>$lang)); // Allow for race conditions
$db->query_insert('theme_images',array('id'=>$id,'theme'=>$theme,'path'=>($path===NULL)?'':$path,'lang'=>$lang),false,true); // Allow for race conditions
$GLOBALS['NO_QUERY_LIMIT']=$nql_backup;
persistant_cache_delete('THEME_IMAGES');
}
}
if ($path===NULL)
{
if (!$silent_fail)
{
require_code('site');
attach_message(do_lang_tempcode('NO_SUCH_IMAGE',escape_html($id)),'warn');
}
return '';
}
if ($truism) $IMG_CODES[$site][$id]=$path; // only cache if we are looking up for our own theme/lang
} else
{
$path=$IMG_CODES[$site][$id];
global $SITE_INFO;
if (($path!='') && ((!isset($SITE_INFO['disable_smart_decaching'])) || ($SITE_INFO['disable_smart_decaching']=='0')) && (url_is_local($path)) && (!is_file(get_custom_file_base().'/'.rawurldecode($path)))) // Missing image, so erase to re-search for it
{
unset($IMG_CODES[$site][$id]);
return find_theme_image($id,$silent_fail,$leave_local,$theme,$lang,$db,$pure_only);
}
}
if ((url_is_local($path)) && (!$leave_local) && ($path!=''))
{
if ($db->connection_write!=$GLOBALS['SITE_DB']->connection_write)
{
$base_url=get_forum_base_url();
} else
{
global $SITE_INFO;
$missing=(!$pure_only) && (((!isset($SITE_INFO['disable_smart_decaching'])) || ($SITE_INFO['disable_smart_decaching']=='0')) && (!is_file(get_file_base().'/'.rawurldecode($path)) && (!is_file(get_custom_file_base().'/'.rawurldecode($path)))));
if ((substr($path,0,22)=='themes/default/images/') || ($missing)) // Not found, so throw away custom theme image and look in default theme images to restore default
{
if ($missing)
{
return find_theme_image($id,$silent_fail,$leave_local,$theme,$lang,$db,true);
}
$base_url=get_base_url();
} else
{
$base_url=get_custom_base_url();
}
}
$path=$base_url.'/'.$path;
}
return cdn_filter($path);
}
URLPATH cdn_filter(URLPATH path)
Filter a path so it runs through a CDN.
Parameters…
| Name |
path |
| Description |
Input URL |
| Type |
URLPATH |
Returns…
| Description |
Output URL |
| Type |
URLPATH |
function cdn_filter($path)
{
$cdn=get_value('cdn');
if (isset($cdn))
{
global $CDN_CONSISTENCY_CHECK;
if (isset($CDN_CONSISTENCY_CHECK[$path])) return $CDN_CONSISTENCY_CHECK[$path];
$cdn_parts=explode(',',$cdn);
$sum_asc=0;
$path_len=strlen($path);
for ($i=0;$i<$path_len;$i++) $sum_asc+=ord($path[$i]);
$cdn_part=$cdn_parts[$sum_asc%count($cdn_parts)]; // To make a consistent but fairly even distribution we do some modular arithmetic against the total of the ascii values
$out=preg_replace('#(^https?://)'.str_replace('#','#',preg_quote(get_domain())).'(/)#','${1}'.$cdn_part.'${2}',$path);
$CDN_CONSISTENCY_CHECK[$path]=$out;
return $out;
}
return $path;
}
?string _search_img_file(ID_TEXT theme, ?LANGUAGE_NAME lang, ID_TEXT id, ID_TEXT dir)
Search for a specified image file within a theme for a specified language.
Parameters…
| Name |
theme |
| Description |
The theme |
| Type |
ID_TEXT |
| Name |
lang |
| Description |
The language (NULL: try generally, under no specific language) |
| Type |
?LANGUAGE_NAME |
| Name |
id |
| Description |
The theme image ID |
| Type |
ID_TEXT |
| Name |
dir |
| Description |
Directory to search |
| Default value |
images |
| Type |
ID_TEXT |
Returns…
| Description |
The path to the image (NULL: was not found) |
| Type |
?string |
function _search_img_file($theme,$lang,$id,$dir='images')
{
$extensions=array('png','jpg','jpeg','gif','ico');
$base=(($theme=='default')?get_file_base():get_custom_file_base()).'/themes/';
$url_base='themes/';
foreach (array(get_file_base(),get_custom_file_base()) as $_base)
{
$base=$_base.'/themes/';
foreach ($extensions as $extension)
{
$file_path=$base.$theme.'/';
if ($dir!='') $file_path.=$dir.'/';
if (($lang!==NULL) && ($lang!='')) $file_path.=$lang.'/';
$file_path.=$id.'.'.$extension;
if (is_file($file_path)) // Theme+Lang
{
$path=$url_base.rawurlencode($theme).'/'.$dir.'/';
if (($lang!==NULL) && ($lang!='')) $path.=rawurlencode($lang).'/';
$path.=$id.'.'.$extension;
return $path;
}
}
}
return NULL;
}
sources/themes2.php
Global_functions_themes2.php
Function summary
|
void
|
actual_edit_theme_image (SHORT_TEXT old_id, ID_TEXT theme, LANGUAGE_NAME lang, SHORT_TEXT id, URLPATH path, boolean quick)
|
|
string
|
css_preg (array matches)
|
|
void
|
actual_add_theme (ID_TEXT name)
|
|
void
|
actual_add_theme_image (ID_TEXT theme, LANGUAGE_NAME lang, SHORT_TEXT id, URLPATH path, boolean fail_ok)
|
|
ID_TEXT
|
get_theme_img_code (ID_TEXT type, boolean allow_skip, ID_TEXT field_file, ID_TEXT field_choose)
|
|
array
|
find_images_do_dir (ID_TEXT theme, string subdir, array langs)
|
|
array
|
get_all_image_ids_type (ID_TEXT type, boolean recurse, ?object db, ?ID_TEXT theme)
|
|
void
|
_get_all_image_ids_type (array ids, ID_TEXT dir, ID_TEXT type, boolean recurse)
|
|
tempcode
|
combo_get_image_paths (string selected_path, URLPATH base_url, PATH base_path)
|
|
array
|
get_image_paths (URLPATH base_url, PATH base_path)
|
|
array
|
get_all_image_codes (PATH base_path, PATH search_under, boolean recurse)
|
|
tempcode
|
nice_get_theme_images (?ID_TEXT it, ?string filter, boolean do_id, boolean include_all, string under)
|
|
tempcode
|
nice_get_themes (?ID_TEXT theme, boolean no_rely, boolean show_everything, ID_TEXT default_message_string)
|
|
array
|
find_all_themes (boolean full_details)
|
|
void
|
tidy_theme_img_code (?ID_TEXT new, ID_TEXT old, ID_TEXT table, ID_TEXT field, ?object db)
|
void actual_edit_theme_image(SHORT_TEXT old_id, ID_TEXT theme, LANGUAGE_NAME lang, SHORT_TEXT id, URLPATH path, boolean quick)
Edit a theme image.
Parameters…
| Name |
old_id |
| Description |
The current theme image ID |
| Type |
SHORT_TEXT |
| Name |
theme |
| Description |
The theme the theme image is in |
| Type |
ID_TEXT |
| Name |
lang |
| Description |
The language the theme image is for (blank: all languages) |
| Type |
LANGUAGE_NAME |
| Name |
id |
| Description |
The new theme image ID |
| Type |
SHORT_TEXT |
| Name |
path |
| Description |
The URL to the theme image |
| Type |
URLPATH |
| Name |
quick |
| Description |
Whether to avoid cleanup, etc |
| Default value |
boolean-false |
| Type |
boolean |
(No return value)
function actual_edit_theme_image($old_id,$theme,$lang,$id,$path,$quick=false)
{
if ($old_id!=$id)
{
$where_map=array('theme'=>$theme,'id'=>$id);
if (($lang!='') && (!is_null($lang))) $where_map['lang']=$lang;
$test=$GLOBALS['SITE_DB']->query_value_null_ok('theme_images','id',$where_map);
if (!is_null($test))
{
warn_exit(do_lang_tempcode('ALREADY_EXISTS',escape_html($id)));
}
}
if (!$quick)
{
$old_url=find_theme_image($id,true,true,$theme,($lang=='')?NULL:$lang);
$where_map=array('theme'=>$theme,'id'=>$id);
if (($lang!='') && (!is_null($lang))) $where_map['lang']=$lang;
$GLOBALS['SITE_DB']->query_delete('theme_images',$where_map);
if (($old_url!=$path) && ($old_url!=''))
{
require_code('themes3');
cleanup_theme_images($old_url);
}
} else
{
$where_map=array('theme'=>$theme,'id'=>$id);
if (($lang!='') && (!is_null($lang))) $where_map['lang']=$lang;
$GLOBALS['SITE_DB']->query_delete('theme_images',$where_map);
}
if ($lang=='')
{
$langs=array_keys(find_all_langs());
} else
{
$langs=array($lang);
}
foreach ($langs as $lang)
{
$GLOBALS['SITE_DB']->query_insert('theme_images',array('id'=>$id,'theme'=>$theme,'path'=>$path,'lang'=>$lang));
}
if (!$quick) log_it('EDIT_THEME_IMAGE',$id,$theme);
}
string css_preg(array matches)
Replace colour codes with references (helper callback function)
Parameters…
| Name |
matches |
| Description |
List of found regular expression matches (only index 0 relevant). |
| Type |
array |
Returns…
| Description |
Replacement. |
| Type |
string |
function css_preg($matches)
{
global $CSS_MATCHES;
$ret=count($CSS_MATCHES);
$CSS_MATCHES[]=$matches[0];
return '<color-'.strval($ret).'>';
}
void actual_add_theme(ID_TEXT name)
Add a theme.
Parameters…
| Name |
name |
| Description |
The theme name |
| Type |
ID_TEXT |
(No return value)
function actual_add_theme($name)
{
$GLOBALS['NO_QUERY_LIMIT']=true;
if ((file_exists(get_custom_file_base().'/themes/'.$name)) || ($name=='default'))
{
warn_exit(do_lang_tempcode('ALREADY_EXISTS',escape_html($name)));
}
require_code('abstract_file_manager');
force_have_afm_details();
// Create directories
$dir_list=array('','images','images/logo','images_custom','templates','templates_custom','templates_cached','css','css_custom');
$langs=find_all_langs(true);
foreach (array_keys($langs) as $lang)
$dir_list[]='templates_cached/'.$lang;
$dir_list_access=array('','images','images_custom','css');
foreach ($dir_list as $dir)
{
$path='themes/'.$name.'/'.$dir;
afm_make_directory($path,true);
if (!in_array($dir,$dir_list_access))
{
// $path='themes/'.$name.'/'.$dir.'/.htaccess';
// afm_copy('themes/default/templates_cached/.htaccess',$path,false);
}
$path='themes/'.$name.'/'.(($dir=='')?'':($dir.'/')).'index.html';
if (file_exists(get_file_base().'/themes/default/templates_cached/index.html'))
afm_copy('themes/default/templates_cached/index.html',$path,false);
}
afm_copy('themes/default/theme.ini','themes/'.$name.'/theme.ini',true);
/*$_dir=opendir(get_custom_file_base().'/themes/default/css');
while (false!==($file=readdir($_dir)))
{
if (strtolower(substr($file,-4,4))=='.css')
{
$path='themes/'.$name.'/css_custom/'.$file;
$new_css_file="@import url(../../../default/css/$file);\n\n".file_get_contents(get_custom_file_base().'/themes/default/css/'.$file,FILE_TEXT);
afm_make_file($path,$new_css_file,false);
}
}
closedir($_dir);*/
// Copy image references from default
$start=0;
do
{
$theme_images=$GLOBALS['SITE_DB']->query_select('theme_images',array('*'),array('theme'=>'default'),'',100,$start);
foreach ($theme_images as $theme_image)
{
$test=$GLOBALS['SITE_DB']->query_value_null_ok('theme_images','id',array('theme'=>$name,'id'=>$theme_image['id'],'lang'=>$theme_image['lang']));
if (is_null($test))
$GLOBALS['SITE_DB']->query_insert('theme_images',array('id'=>$theme_image['id'],'theme'=>$name,'path'=>$theme_image['path'],'lang'=>$theme_image['lang']));
}
$start+=100;
}
while (count($theme_images)==100);
log_it('ADD_THEME',$name);
}
void actual_add_theme_image(ID_TEXT theme, LANGUAGE_NAME lang, SHORT_TEXT id, URLPATH path, boolean fail_ok)
Add a theme image.
Parameters…
| Name |
theme |
| Description |
The theme the theme image is in |
| Type |
ID_TEXT |
| Name |
lang |
| Description |
The language the theme image is for |
| Type |
LANGUAGE_NAME |
| Name |
id |
| Description |
The theme image ID |
| Type |
SHORT_TEXT |
| Name |
path |
| Description |
The URL to the theme image |
| Type |
URLPATH |
| Name |
fail_ok |
| Description |
Whether to allow failure without bombing out |
| Default value |
boolean-false |
| Type |
boolean |
(No return value)
function actual_add_theme_image($theme,$lang,$id,$path,$fail_ok=false)
{
$test=$GLOBALS['SITE_DB']->query_value_null_ok('theme_images','id',array('id'=>$id,'theme'=>$theme,'lang'=>$lang));
if (!is_null($test))
{
if ($fail_ok) return;
warn_exit(do_lang_tempcode('ALREADY_EXISTS',escape_html($id)));
}
$GLOBALS['SITE_DB']->query_insert('theme_images',array('id'=>$id,'theme'=>$theme,'path'=>$path,'lang'=>$lang));
log_it('ADD_THEME_IMAGE',$id,$theme);
persistant_cache_delete('THEME_IMAGES');
}
ID_TEXT get_theme_img_code(ID_TEXT type, boolean allow_skip, ID_TEXT field_file, ID_TEXT field_choose)
A theme image has been passed through by POST, either as a file (a new theme image), or as a reference to an existing one. Get the image code from the POST data.
Parameters…
| Name |
type |
| Description |
The type of theme image |
| Default value |
ocf_emoticons |
| Type |
ID_TEXT |
| Name |
allow_skip |
| Description |
Allow no code to be given |
| Default value |
boolean-false |
| Type |
boolean |
| Name |
field_file |
| Description |
Form field for uploading |
| Default value |
file |
| Type |
ID_TEXT |
| Name |
field_choose |
| Description |
Form field for choosing |
| Default value |
theme_img_code |
| Type |
ID_TEXT |
Returns…
| Description |
The (possibly randomised) theme image code |
| Type |
ID_TEXT |
function get_theme_img_code($type='ocf_emoticons',$allow_skip=false,$field_file='file',$field_choose='theme_img_code')
{
if ((substr($type,0,4)=='ocf_') && (file_exists(get_file_base().'/themes/default/images/avatars/index.html'))) // Allow debranding of theme img dirs
{
$type=substr($type,4);
}
require_code('uploads');
if ((is_swf_upload()) || (((array_key_exists($field_file,$_FILES)) && (is_uploaded_file($_FILES[$field_file]['tmp_name'])))))
{
$urls=get_url('',$field_file,'themes/default/images_custom',0,OCP_UPLOAD_IMAGE,false);
$theme_img_code=$type.'/'.uniqid('');
$GLOBALS['SITE_DB']->query_insert('theme_images',array('id'=>$theme_img_code,'theme'=>'default','path'=>$urls[0],'lang'=>get_site_default_lang()));
persistant_cache_delete('THEME_IMAGES');
} else
{
$theme_img_code=post_param($field_choose,'');
if ($theme_img_code=='')
{
if ($allow_skip) return '';
warn_exit(do_lang_tempcode('IMPROPERLY_FILLED_IN_UPLOAD'));
}
}
return $theme_img_code;
}
array find_images_do_dir(ID_TEXT theme, string subdir, array langs)
Recursively find theme images under the specified details. Does not find custom theme images, as it doesn't check the DB.
Parameters…
| Name |
theme |
| Description |
The theme |
| Type |
ID_TEXT |
| Name |
subdir |
| Description |
The subdirectory to search under |
| Type |
string |
| Name |
langs |
| Description |
A map (lang=>1) of the languages in the system, so the codes may be filtered out of the image codes in our result list |
| Type |
array |
Returns…
| Description |
A map, theme-image-code=>URL |
| Type |
array |
function find_images_do_dir($theme,$subdir,$langs)
{
$full=(($theme=='default')?get_file_base():get_custom_file_base()).'/themes/'.filter_naughty($theme).'/'.filter_naughty($subdir);
$out=array();
$_dir=@opendir($full);
if ($_dir!==false)
{
while (false!==($file=readdir($_dir)))
{
if (($file!='.') && ($file!='..'))
{
if (is_dir($full.$file))
{
$out=array_merge($out,find_images_do_dir($theme,$subdir.$file.'/',$langs));
} else
{
$ext=substr($file,-4);
if (($ext=='.png') || ($ext=='.gif') || ($ext=='.jpg') || ($ext=='jpeg'))
{
$_file=explode('.',$file);
$_subdir=$subdir;
foreach (array_keys($langs) as $lang)
{
$_subdir=str_replace('/'.$lang.'/','/',$_subdir);
}
$_subdir=preg_replace('#(^|/)images(\_custom)?/#','',$_subdir);
$out[$_subdir.$_file[0]]='themes/'.rawurlencode($theme).'/'.$subdir.rawurlencode($file);
}
}
}
}
closedir($_dir);
}
return $out;
}
array get_all_image_ids_type(ID_TEXT type, boolean recurse, ?object db, ?ID_TEXT theme)
Get all the image IDs (both already known, and those uncached) of a certain type (i.e. under a subdirectory).
Parameters…
| Name |
type |
| Description |
The type of image (e.g. 'ocf_emoticons') |
| Type |
ID_TEXT |
| Name |
recurse |
| Description |
Whether to search recursively; i.e. in subdirectories of the type subdirectory |
| Default value |
boolean-false |
| Type |
boolean |
| Name |
db |
| Description |
The database connection to work over (NULL: site db) |
| Default value |
|
| Type |
?object |
| Name |
theme |
| Description |
The theme to search in, in addition to the default theme (NULL: current theme) |
| Default value |
|
| Type |
?ID_TEXT |
Returns…
| Description |
The list of image IDs |
| Type |
array |
function get_all_image_ids_type($type,$recurse=false,$db=NULL,$theme=NULL)
{
if (is_null($db)) $db=$GLOBALS['SITE_DB'];
if (is_null($theme)) $theme=$GLOBALS['FORUM_DRIVER']->get_theme();
if ((substr($type,0,4)=='ocf_') && (file_exists(get_file_base().'/themes/default/images/avatars/index.html'))) // Allow debranding of theme img dirs
{
$type=substr($type,4);
}
if (substr($type,-1)=='/') $type=substr($type,0,strlen($type)-1);
$ids=array();
if (($db->connection_write==$GLOBALS['SITE_DB']->connection_write) || (get_db_forums()==get_db_site()))
{
_get_all_image_ids_type($ids,get_file_base().'/themes/default/images/'.(($type=='')?'':($type.'/')),$type,$recurse);
_get_all_image_ids_type($ids,get_file_base().'/themes/default/images/'.get_site_default_lang().'/'.(($type=='')?'':($type.'/')),$type,$recurse);
if ($theme!='default')
{
_get_all_image_ids_type($ids,get_custom_file_base().'/themes/'.$theme.'/images/'.(($type=='')?'':($type.'/')),$type,$recurse);
_get_all_image_ids_type($ids,get_custom_file_base().'/themes/'.$theme.'/images/'.get_site_default_lang().'/'.(($type=='')?'':($type.'/')),$type,$recurse);
}
_get_all_image_ids_type($ids,get_file_base().'/themes/default/images_custom/'.(($type=='')?'':($type.'/')),$type,$recurse);
_get_all_image_ids_type($ids,get_file_base().'/themes/default/images_custom/'.get_site_default_lang().'/'.(($type=='')?'':($type.'/')),$type,$recurse);
if ($theme!='default')
{
_get_all_image_ids_type($ids,get_custom_file_base().'/themes/'.$theme.'/images_custom/'.(($type=='')?'':($type.'/')),$type,$recurse);
_get_all_image_ids_type($ids,get_custom_file_base().'/themes/'.$theme.'/images_custom/'.get_site_default_lang().'/'.(($type=='')?'':($type.'/')),$type,$recurse);
}
}
$rows=$db->query('SELECT DISTINCT id,path FROM '.$db->get_table_prefix().'theme_images WHERE path NOT LIKE \''.db_encode_like('themes/default/images/%').'\' AND '.db_string_not_equal_to('path','themes/default/images/blank.gif').' AND ('.db_string_equal_to('theme',$theme).' OR '.db_string_equal_to('theme','default').') AND id LIKE \''.db_encode_like($type.'%').'\' ORDER BY path');
foreach ($rows as $row)
{
if ($row['path']=='') continue;
if ((url_is_local($row['path'])) && (!file_exists(((substr($row['path'],0,15)=='themes/default/')?get_file_base():get_custom_file_base()).'/'.rawurldecode($row['path'])))) continue;
if ($row['path']!='themes/default/images/blank.gif') // We sometimes associate to blank.gif to essentially delete images so they can never be found again
{
$ids[]=$row['id'];
} else
{
$key=array_search($row['id'],$ids);
if (is_integer($key)) unset($ids[$key]);
}
}
sort($ids);
$ids=array_unique($ids);
return $ids;
}
void _get_all_image_ids_type(array ids, ID_TEXT dir, ID_TEXT type, boolean recurse)
Get all the image IDs (both already known, and those uncached) of a certain type (i.e. under a subdirectory).
Parameters…
| Name |
ids |
| Description |
The list of image IDs found so far. This list will be appended as we proceed |
| Type |
array |
| Name |
dir |
| Description |
The specific theme image subdirectory we are currently looking under |
| Type |
ID_TEXT |
| Name |
type |
| Description |
The type of image (e.g. 'ocf_emoticons') |
| Type |
ID_TEXT |
| Name |
recurse |
| Description |
Whether to search recursively; i.e. in subdirectories of the type subdirectory |
| Type |
boolean |
(No return value)
function _get_all_image_ids_type(&$ids,$dir,$type,$recurse)
{
require_code('images');
$_dir=@opendir($dir);
if ($_dir!==false)
{
while (false!==($file=readdir($_dir)))
{
if (!should_ignore_file($file,IGNORE_ACCESS_CONTROLLERS))
{
if (!is_dir($dir.'/'.$file))
{
$dot_pos=strrpos($file,'.');
if ($dot_pos===false) $dot_pos=strlen($file);
if (is_image($file)) $ids[]=$type.(($type!='')?'/':'').substr($file,0,$dot_pos);
}
elseif (($recurse) && ((strlen($file)!=2) || (strtoupper($file)!=$file)))
{
$ids=array_merge($ids,get_all_image_ids_type($type.(($type!='')?'/':'').$file,true));
}
}
}
closedir($_dir);
}
}
tempcode combo_get_image_paths(string selected_path, URLPATH base_url, PATH base_path)
Get tempcode for a radio list to choose an image from the image FILES in the theme.
Parameters…
| Name |
selected_path |
| Description |
The currently selected image path (blank for none) |
| Type |
string |
| Name |
base_url |
| Description |
The base-URL to where we are searching for images |
| Type |
URLPATH |
| Name |
base_path |
| Description |
The base-path to where we are searching for images |
| Type |
PATH |
Returns…
| Description |
The generated tempcode |
| Type |
tempcode |
function combo_get_image_paths($selected_path,$base_url,$base_path)
{
$out=new ocp_tempcode();
$paths=get_image_paths($base_url,$base_path);
$i=0;
foreach ($paths as $pretty=>$url)
{
$checked=(($url==$selected_path) || (($selected_path=='') && ($i==0)));
$out->attach(do_template('FORM_SCREEN_INPUT_RADIO_LIST_ENTRY_PICTURE',array('_GUID'=>'d2ff01291e5f0c0e4cf4ee5b6061593c','CHECKED'=>$checked,'NAME'=>'path','VALUE'=>$url,'URL'=>$url,'PRETTY'=>$pretty)));
$i++;
}
return $out;
}
array get_image_paths(URLPATH base_url, PATH base_path)
Search under a base path for image FILE URLs (not actually paths as function name would suggest).
Parameters…
| Name |
base_url |
| Description |
The base-URL to where we are searching for images |
| Type |
URLPATH |
| Name |
base_path |
| Description |
The base-path to where we are searching for images |
| Type |
PATH |
Returns…
| Description |
path->url map of found images |
| Type |
array |
function get_image_paths($base_url,$base_path)
{
$out=array();
require_code('images');
$handle=@opendir($base_path);
if ($handle!==false)
{
while (false!==($file=readdir($handle)))
{
if (!should_ignore_file($file,IGNORE_ACCESS_CONTROLLERS))
{
$this_path=$base_path.$file;
if (is_file($this_path))
{
if (is_image($file))
{
$this_url=$base_url.rawurlencode($file);
$out[$this_path]=$this_url;
}
}
elseif ((strlen($file)!=2) || (strtoupper($file)!=$file))
{
$out=array_merge($out,get_image_paths($base_url.$file.'/',$base_path.$file.'/'));
}
}
}
closedir($handle);
}
return $out;
}
array get_all_image_codes(PATH base_path, PATH search_under, boolean recurse)
Get all the themes image codes. THIS DOES NOT SEARCH THE DB - DO NOT USE UNLESS IT'S ON A PURE PACKAGED THEME
Parameters…
| Name |
base_path |
| Description |
The base-path to where we are searching for images |
| Type |
PATH |
| Name |
search_under |
| Description |
The path to search under, relative to the base-path. This is not the same as the base-path, as we are cropping paths to the base-path |
| Type |
PATH |
| Name |
recurse |
| Description |
Whether to search recursively from the given directory |
| Default value |
boolean-true |
| Type |
boolean |
Returns…
| Description |
A list of image codes |
| Type |
array |
function get_all_image_codes($base_path,$search_under,$recurse=true)
{
$out=array();
require_code('images');
if (!file_exists($base_path.'/'.$search_under)) return array();
$handle=@opendir($base_path.'/'.$search_under);
if ($handle!==false)
{
while (false!==($file=readdir($handle)))
{
if (!should_ignore_file($file,IGNORE_ACCESS_CONTROLLERS))
{
$full_path=$base_path.'/'.$search_under.'/'.$file;
if (is_file($full_path))
{
if (is_image($file))
{
$dot_pos=strrpos($file,'.');
if ($dot_pos===false) $dot_pos=strlen($file);
$_file=substr($file,0,$dot_pos);
$short_path=($search_under=='')?$_file:($search_under.'/'.$_file);
$out[$short_path]=1;
}
}
elseif ((strlen($file)!=2) || (strtoupper($file)!=$file))
{
if ($recurse) $out+=get_all_image_codes($base_path,$search_under.'/'.$file);
}
}
}
closedir($handle);
}
return $out;
}
tempcode nice_get_theme_images(?ID_TEXT it, ?string filter, boolean do_id, boolean include_all, string under)
Get tempcode for a dropdown to choose a theme from the themes present.
Parameters…
| Name |
it |
| Description |
The currently selected image ID (NULL: none selected) |
| Default value |
|
| Type |
?ID_TEXT |
| Name |
filter |
| Description |
An SQL where clause (including the WHERE), that filters the query somehow (NULL: none) |
| Default value |
|
| Type |
?string |
| Name |
do_id |
| Description |
Whether to show IDs as the list entry captions, rather than paths |
| Default value |
boolean-false |
| Type |
boolean |
| Name |
include_all |
| Description |
Whether to include images not yet used (i.e not in theme_images map yet) |
| Default value |
boolean-false |
| Type |
boolean |
| Name |
under |
| Description |
Only include images under this path. Including a trailing slash unless you specifically want to filter allowing filename stubs as well as paths (blank: no limitation) |
| Default value |
|
| Type |
string |
Returns…
| Description |
Tempcode for a list selection of theme images |
| Type |
tempcode |
function nice_get_theme_images($it=NULL,$filter=NULL,$do_id=false,$include_all=false,$under='')
{
$out=new ocp_tempcode();
if (!$include_all)
{
$rows=$GLOBALS['SITE_DB']->query('SELECT id,path FROM '.get_table_prefix().'theme_images WHERE '.db_string_equal_to('theme',$GLOBALS['FORUM_DRIVER']->get_theme()).' '.$filter.' ORDER BY path');
foreach ($rows as $myrow)
{
$id=$myrow['id'];
if (substr($id,0,strlen($under))!=$under) continue;
$selected=($id==$it);
$out->attach(form_input_list_entry($id,$selected,($do_id)?$id:$myrow['path']));
}
} else
{
$rows=get_all_image_ids_type($under,true);
foreach ($rows as $id)
{
if (substr($id,0,strlen($under))!=$under) continue;
$selected=($id==$it);
$out->attach(form_input_list_entry($id,$selected));
}
}
return $out;
}
tempcode nice_get_themes(?ID_TEXT theme, boolean no_rely, boolean show_everything, ID_TEXT default_message_string)
Get a UI list for choosing a theme.
Parameters…
| Name |
theme |
| Description |
The theme to select by default (NULL: no specific default) |
| Default value |
|
| Type |
?ID_TEXT |
| Name |
no_rely |
| Description |
Whether to skip the 'rely on forums' entry |
| Default value |
boolean-false |
| Type |
boolean |
| Name |
show_everything |
| Description |
Whether to forget about permissions for this list |
| Default value |
boolean-false |
| Type |
boolean |
| Name |
default_message_string |
| Description |
The language string to use for the default answer |
| Default value |
RELY_FORUMS |
| Type |
ID_TEXT |
Returns…
| Description |
The list |
| Type |
tempcode |
function nice_get_themes($theme=NULL,$no_rely=false,$show_everything=false,$default_message_string='RELY_FORUMS')
{
if (!$no_rely) $entries=form_input_list_entry('-1',false,do_lang_tempcode($default_message_string)); else $entries=new ocp_tempcode();
$themes=find_all_themes();
foreach ($themes as $_theme=>$title)
{
if (/*($_theme=='default') || */ ($show_everything) || (has_category_access(get_member(),'theme',$_theme)))
{
$selected=($theme==$_theme);
$entries->attach(form_input_list_entry($_theme,$selected,$title));
}
}
if ($entries->is_empty())
{
$entries->attach(form_input_list_entry('default',false,$themes['default']));
}
return $entries;
}
array find_all_themes(boolean full_details)
Get an array listing all the themes present.
Parameters…
| Name |
full_details |
| Description |
Whether to gather full details for each theme |
| Default value |
boolean-false |
| Type |
boolean |
Returns…
| Description |
A map of all themes (name=>title) OR if requested a map of theme name to full theme details |
| Type |
array |
function find_all_themes($full_details=false)
{
if ($GLOBALS['IN_MINIKERNEL_VERSION']==1) return $full_details?array('default'=>array()):array('default'=>do_lang('DEFAULT'));
require_code('files');
$themes=array();
$_dir=opendir(get_file_base().'/themes/');
while (false!==($file=readdir($_dir)))
{
$ini_file=get_file_base().'/themes/'.$file.'/theme.ini';
if ((strpos($file,'.')===false) && (is_dir(get_file_base().'/themes/'.$file)) && (file_exists($ini_file)))
{
$details=better_parse_ini_file($ini_file);
if (!array_key_exists('title',$details)) $details['title']='?';
if (!array_key_exists('description',$details)) $details['description']='?';
if (!array_key_exists('author',$details)) $details['author']='?';
$themes[$file]=$full_details?$details:$details['title'];
}
}
closedir($_dir);
if (get_custom_file_base()!=get_file_base())
{
$_dir=opendir(get_custom_file_base().'/themes/');
while (false!==($file=readdir($_dir)))
{
$ini_file=get_custom_file_base().'/themes/'.$file.'/theme.ini';
if ((strpos($file,'.')===false) && (is_dir(get_custom_file_base().'/themes/'.$file)) && (file_exists($ini_file)))
{
$details=better_parse_ini_file($ini_file);
if (!array_key_exists('title',$details)) $details['title']='?';
if (!array_key_exists('description',$details)) $details['description']='?';
if (!array_key_exists('author',$details)) $details['author']='?';
$themes[$file]=$full_details?$details:$details['title'];
}
}
closedir($_dir);
}
if (!array_key_exists('default',$themes))
{
$details=better_parse_ini_file(get_file_base().'/themes/default/theme.ini');
if (!array_key_exists('title',$details)) $details['title']='?';
if (!array_key_exists('description',$details)) $details['description']='?';
if (!array_key_exists('author',$details)) $details['author']='?';
$themes['default']=$full_details?$details:$details['title'];
}
return $themes;
}
void tidy_theme_img_code(?ID_TEXT new, ID_TEXT old, ID_TEXT table, ID_TEXT field, ?object db)
Delete a theme image used for a resource that was added, but only if the theme image is now unused.
Parameters…
| Name |
new |
| Description |
The new theme image (NULL: no new one) |
| Type |
?ID_TEXT |
| Name |
old |
| Description |
The old theme image we might be tidying up |
| Type |
ID_TEXT |
| Name |
table |
| Description |
Table to check against |
| Type |
ID_TEXT |
| Name |
field |
| Description |
Field in table |
| Type |
ID_TEXT |
| Name |
db |
| Description |
Database connection to check against (NULL: site database) |
| Default value |
|
| Type |
?object |
(No return value)
function tidy_theme_img_code($new,$old,$table,$field,$db=NULL)
{
if ($new===$old) return; // Still being used
$path=find_theme_image($old,true,true);
if ((is_null($path)) || ($path=='')) return;
if ((strpos($path,'/images_custom/')!==false) && ($GLOBALS['SITE_DB']->query_value('theme_images','COUNT(DISTINCT id)',array('path'=>$path))==1))
{
if (is_null($db)) $db=$GLOBALS['SITE_DB'];
$count=$db->query_value($table,'COUNT(*)',array($field=>$old));
if ($count==0)
{
@unlink(get_custom_file_base().'/'.$path);
$GLOBALS['SITE_DB']->query_delete('theme_images',array('id'=>$old));
}
}
}
0 reviews: Unrated (average)
There have been no comments yet