<?php /*~ theme.inc.php
.---------------------------------------------------------------------------.
|  Software: Theme Control Library                                          |
|   Version: 0.1                                                            |
|   Contact: via http://www.uniwits.com/contact                             |
|      Info: http://www.uniwits.com/                                        |
|   Support: http://www.uniwits.com/support                                 |
| ------------------------------------------------------------------------- |
|     Admin: Yuxiang Mao (project admininistrator)                          |
|   Authors: Yuxiang Mao (Uniwits) contact@uniwits.com                      |
|   Founder: Yuxiang Mao                                                    |
| Copyright (c) 2010-2012, Yuxiang Mao. All Rights Reserved.                |
| ------------------------------------------------------------------------- |
|   License: Distributed under Uniwits Shareware License (USL)              |
|            http://www.uniwits.com/licenses/shareware/                     |
| This program is distributed as a part of software provided by Uniwits     |
| System WITHOUT ANY WARRANTY; without even the implied warranty of         |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                      |
| ------------------------------------------------------------------------- |
*/ $USE_THEME_BY_USER_ID=null; function LanguageFiles($name=false,$lang=false,$forcetextreplace=false) { global $Config,$TMPL_PARAM,$USE_THEME_BY_USER_ID,$NO_TEXT_REPLACE; if (!$lang) $lang=language_requested(); $language_files=array(); $language_file=$Config->Paths['lang.system']."/".$lang; if ($name) $language_file.='.'.$name; $language_file.=$Config->LangFileSuffix; if (is_file($language_file)) array_push($language_files,$language_file); if (!$NO_TEXT_REPLACE && (!$name || $forcetextreplace)) { if ($USE_THEME_BY_USER_ID) $cp_id=$USE_THEME_BY_USER_ID; else $cp_id=compere_id_requested(); if ($cp_id && is_file($language_file)) { $cp_trt_file=compute_user_path($cp_id,$Config->Paths['user.base'].'/','/trt.xml'); $user_lang_file=compute_user_path($cp_id,$Config->CachePrefix.'lang/','/'.fileinode($language_file).'.txt'); textreplace_transform_files( $cp_trt_file, array( array( 'src'=>$language_file, 'dest'=>$user_lang_file ) ) ); array_push($language_files,$user_lang_file); $tmplparam='.CPID_'.$cp_id; if (strpos($TMPL_PARAM,$tmplparam)===false) $TMPL_PARAM.=$tmplparam; } } return $language_files; } function compute_theme_name($userid,$themelocalid) { return base_convert($userid,10,36).'-'.base_convert($themelocalid,10,36); } function is_current_theme_updatable_by_user($mb_id) { $themereq=deduce_theme_requested(); $themenameparts=explode('-',$themereq); $nullthemename=compute_theme_name($mb_id,0); return $themenameparts[0].'-0'==$nullthemename; } function compute_theme_path($themename=false,$prefix=false,$suffix=false) { return $prefix.implode('/',str_split(base_convert(crc32($themename),10,36),2)).'_/'.$themename.$suffix; } function retrieve_user_theme_setting($userid) { global $Config; $userthemenamefile=compute_user_path($userid,$Config->Paths['user.base'].'/','/themename'); $usertheme=trim(@file_get_contents($userthemenamefile)); if ($usertheme) { $parts=explode("\n",$usertheme); return array('name'=>$parts[0],'nickname'=>$parts[1]); } return array(); } function store_user_theme_setting($userid,$themeinfo) { global $Config; $userthemenamefile=compute_user_path($userid,$Config->Paths['user.base'].'/','/themename'); $dir=dirname($userthemenamefile); @mkdir($dir,0744,true); file_put_contents($userthemenamefile,$themeinfo['name']."\n".$themeinfo['nickname']); $_SESSION['uniwits.theme.cache'][$userid]=$themeinfo; } function deduce_theme_requested($themereq=false) { global $Config,$USE_THEME_BY_USER_ID; if (!$themereq) { $themereq=TGET('THEME'); if (!$themereq) { if ($USE_THEME_BY_USER_ID) $userid=$USE_THEME_BY_USER_ID; else $userid=compere_id_requested(); if (isset($_SESSION['uniwits.theme.cache'][$userid])) { $themereq=$_SESSION['uniwits.theme.cache'][$userid]['name']; } else { if ($userid) { $_SESSION['uniwits.theme.cache'][$userid]=retrieve_user_theme_setting($userid); $themereq=$_SESSION['uniwits.theme.cache'][$userid]['name']; } } } } if (!$themereq) { if ($_SESSION['uniwits.theme.default']) $themereq=$_SESSION['uniwits.theme.default']; else { $defaultset=@file_get_contents($Config->Paths['theme.system'].'/theme.default.txt'); if ($defaultset && file_exists($Config->Paths['theme.system'].'/'.$defaultset)) $_SESSION['uniwits.theme.default']=$themereq=$defaultset; else { $filelists=glob($Config->Paths['theme.system'].'/*-*'); $themedirs=array(); foreach ($filelists as $f) if (is_dir($f)) array_push($themedirs,$f); $themereq=basename($themedirs[mt_rand(0,sizeof($themedirs)-1)]); $_SESSION['uniwits.theme.default']=$themereq; } } } return $themereq; } function deduce_theme_path($filename=false,$themereq=false) { global $Config; $themereq=deduce_theme_requested($themereq); $themepath=$Config->Paths['theme.system'].'/'.$themereq; if (!is_dir($themepath)) { $cache_full_path=compute_theme_path($themereq,$Config->CachePrefix); if (!is_dir($cache_full_path)) { $src_theme_path=compute_theme_path($themereq,$Config->Paths['theme.shared'].'/'); @mkdir($src_theme_path,0755,true); @copy_dir($src_theme_path,$cache_full_path); } $themepath=$cache_full_path; } $fullpath=$themepath; if ($filename) { $fullpath=$fullpath.'/'.$filename; if (!file_exists($fullpath)) $fullpath=$Config->Paths['theme.system'].'/'.$Config->DefaultTheme."/".$filename; } return $fullpath; } function set_user_theme($themename) { global $Config; $themenamefile=compute_user_path($userid,$Config->Paths['user.base'].'/','/themename'); file_put_contents($themenamefile,$themename); } function report_theme_change($themename,$nickname,$action) { global $Config; $dir=$Config->Paths['theme.shared.report'].'/'.$action.'/'; @mkdir($dir,0755,true); $reportfilename=tempnam($dir,''); $fp=fopen($reportfilename,"w"); if ($fp) { fwrite($fp,$themename."\n".$nickname."\n\n"); fclose($fp); } chmod($reportfilename,0666); } function count_shared_themes() { global $Config; $size=0+@filesize($Config->Paths['theme.shared'].'/grandlist.tbl'); return $size/17; } function load_theme_info($fromindex,$count,$backward=false,$fptbl=null,$fplist=null) { global $Config; $themelist=array(); $owntbl=false; $ownlist=false; if (!$fptbl) { $fptbl=@fopen($Config->Paths['theme.shared'].'/grandlist.tbl','r'); $owntbl=true; } if (!$fplist) { $fplist=@fopen($Config->Paths['theme.shared'].'/grandlist.txt','r'); $ownlist=true; } if ($fptbl && $fplist) { $postbl=ftell($fptbl); $poslist=ftell($fplist); $stattbl=fstat($fptbl); $statlist=fstat($fplist); $totalnumber=$stattbl['size']/17; if ($backward) { if ($fromindex+$count>=$totalnumber) $count=$totalnumber-$fromindex; $postblfrom=($totalnumber-$fromindex-$count)*17; $postblto=($totalnumber-$fromindex)*17; } else { $postblfrom=$fromindex*17; $postblto=($fromindex+$count)*17; } fseek($fptbl,$postblfrom); $tmp=fgets($fptbl); $poslistfrom=$tmp?base_convert($tmp,16,10):$statlist['size']; fseek($fplist,$poslistfrom); for ($ci=0;$ci<$count && !feof($fplist);$ci++) { $info=array(); while ($line=trim(fgets($fplist))) array_push($info,$line); if ($info) array_push($themelist,$info); } if ($backward) $themelist=array_reverse($themelist); } if ($owntbl) { if ($fptbl) fclose($fptbl); } else fseek($fp,$postbl); if ($ownlist) { if ($fplist) fclose($fplist); } else fseek($fp,$poslist); return $themelist; } function list_default_themes() { global $Config; return array_map('basename',glob($Config->Paths['theme.system'].'/*-*')); } ?>