source: trunk/plugins/SwiftThemeCreator/theme_creator.php @ 2454

Last change on this file since 2454 was 2454, checked in by vdigital, 16 years ago

Swift Theme Creator (minor changes): Still incomplete but first functional version (5).

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24/* Ajouter le lien au menu de l'admin */
25if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
26if (!defined('IN_ADMIN') or !IN_ADMIN) die('Hacking attempt!');
27
28/*
29 * stc_hex2rgb convert any string to array of RGB values
30 */
31function stc_hex2rgb($color)
32{
33  if ($color[0] == '#') $color = substr($color, 1);
34  if (strlen($color) == 6)
35    list($r, $g, $b) = array($color[0].$color[1],
36                             $color[2].$color[3],
37                             $color[4].$color[5]);
38  else {
39    $color .= $color . $color . '000';
40    list($r, $g, $b) = array($color[0].$color[0],
41                             $color[1].$color[1], 
42                             $color[2].$color[2]);
43  }
44  return array(hexdec($r), hexdec($g), hexdec($b));
45}
46/*
47 * lighten returns array of x% of lighter RGB values
48 */
49function lighten( $r, $g, $b, $percent)
50{
51  $r = min(round($r+(($percent*(255-$r))/100)),255);
52  $g = min(round($g+(($percent*(255-$g))/100)),255);
53  $b = min(round($b+(($percent*(255-$b))/100)),255);
54  return sprintf('#%02X%02X%02X', $r, $g, $b);
55}
56/*
57 * darken returns array of x% of darker RGB values
58 */
59function darken( $r, $g, $b, $percent)
60{
61  $r = max(round($r-(($percent*$r)/100)),0);
62  $g = max(round($g-(($percent*$g)/100)),0);
63  $b = max(round($b-(($percent*$b)/100)),0);
64  return sprintf('#%02X%02X%02X', $r, $g, $b);
65}
66/*
67 * stc_newfile create a new file
68 */
69function stc_newfile( $filename, $data )
70{
71  $fp = fopen($filename, 'w');
72  if ($fp)
73  {
74    $ret = fwrite($fp, $data); 
75    fclose($fp);
76    return $ret;
77  }
78  return false;
79}
80
81$errors = array();
82$infos = array();
83$available_templates = array();
84$template_dir = PHPWG_ROOT_PATH.'template';
85foreach (get_dirs($template_dir) as $dir)
86{
87  array_push($available_templates, $dir);
88}
89
90// +-----------------------------------------------------------------------+
91// |                            selected templates                         |
92// +-----------------------------------------------------------------------+
93if (!isset($swift_theme_creator)) $swift_theme_creator = new ThemeCreator();
94$swift_theme_creator->reload();
95$main = $swift_theme_creator->theme_config;
96
97if (isset($_POST['submit']) and (!is_adviser()))
98{
99  // 1 - Theme name control
100  $main['newtheme'] = strip_tags($_POST['new_theme']);
101  if ( !preg_match('/^[a-z0-9-_]{1,8}$/', $main['newtheme']) ) 
102      array_push($errors,
103         l10n('Invalid theme name: 1 to 8 lowercase alphanumeric characters'
104         . ' including "-" and "_".')); 
105
106  // 2 - Colours control
107  $main['color'] = array($_POST['color1'], $_POST['color2'], 
108                         $_POST['color3'], $_POST['color4'], 
109                         $_POST['color5']);
110  $colors = $main['color'][0] . $main['color'][1] . $main['color'][2] 
111          . $main['color'][3] . $main['color'][4];
112  if ( !preg_match('/^(#?([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})){5}$/', $colors) ) 
113      array_push($errors,
114         l10n('Invalid color code: 3 or 6 hexadecimal characters, preceded' 
115         . ' or not by "#"')); 
116
117  // 2.1 - Background and text control
118  list($r1,$g1,$b1) = stc_hex2rgb($main['color'][0]);
119  list($r2,$g2,$b2) = stc_hex2rgb($main['color'][1]);
120  // Formula for converting RGB values to YIQ values
121  // as perceived brightness difference.
122  // Background and text "brightness" difference control:
123  $dif = abs( ( (($r1*299)+($g1*587)+($b1*114)) / 1000 )
124          - ( (($r2*299)+($g2*587)+($b2*114)) / 1000 ));
125  if ( $dif < 65 )
126      array_push($errors,
127       l10n('Insufficient brightness difference between ' 
128            . 'text and background. dif=') . $dif); 
129  // Background and text "colour" difference control:
130  $dif = (max($r1, $r2) - min($r1, $r2)) 
131     + (max($g1, $g2) - min($g1, $g2)) 
132     + (max($b1, $b2) - min($b1, $b2));
133  if ( $dif < 200 )
134    array_push($errors,
135       l10n('Insufficient colour difference between ' 
136            . 'text and background. dif=') . $dif); 
137
138  // 2.2 - Background and Internal links control
139  list($r1,$g1,$b1) = stc_hex2rgb($main['color'][0]);
140  list($r2,$g2,$b2) = stc_hex2rgb($main['color'][2]);
141  // Background and Internal links "brightness" difference control:
142  $dif = abs( ( (($r1*299)+($g1*587)+($b1*114)) / 1000 )
143          - ( (($r2*299)+($g2*587)+($b2*114)) / 1000 ));
144  if ( $dif < 65 )
145      array_push($errors,
146       l10n('Insufficient brightness difference between ' 
147            . 'Internal links and background. dif=') . $dif); 
148  // Background and Internal links "colour" difference control:
149  $dif = (max($r1, $r2) - min($r1, $r2)) 
150     + (max($g1, $g2) - min($g1, $g2)) 
151     + (max($b1, $b2) - min($b1, $b2));
152  if ( $dif < 200 )
153    array_push($errors,
154       l10n('Insufficient colour difference between ' 
155            . 'Internal links and background. dif=') . $dif); 
156
157  // 3 - Directory control
158  $main['templatedir'] = PHPWG_ROOT_PATH . 'template/' 
159               . $available_templates[$_POST['template']];
160  $main['newtpl'] = $available_templates[$_POST['template']];
161  $themedir = $main['templatedir'] . '/theme/' . $main['newtheme'];
162  if (is_dir(  $themedir )) 
163    array_push($errors,
164       '['.$themedir.'] : '.l10n('Invalid theme: This theme exists ' 
165                           . 'already (no override available).')); 
166  elseif (!is_writable($main['templatedir']))
167    array_push($errors,
168       '['.$main['templatedir'].'] : '.l10n('no_write_access'));
169
170  // 4 - Picture URL control
171  if ( $_POST['background'] == 'fixed' and (is_dir($_POST['picture_url'])
172      or !is_file($_POST['picture_url'])) )
173    array_push($errors,
174       l10n('Header picture is not found, check its path and name.')); 
175 
176  // 5 - Expected Width and Height limits control
177  if ( !(ctype_digit($_POST['picture_width']) and $_POST['picture_width'] > 11 
178       and $_POST['picture_width'] < 4097 ) )
179    array_push($errors,
180       '['.$_POST['picture_width'].'] : ' 
181       . l10n('incorrect width value [12-4096].')); 
182  if ( !(ctype_digit($_POST['picture_height']) and $_POST['picture_height'] > 11 
183       and $_POST['picture_height'] < 201 ) )
184    array_push($errors,
185       '['.$_POST['picture_height'].'] : '
186       . l10n('incorrect width value [12-200].')); 
187       
188  // 6 - Generate missing colors values
189  list($r1,$g1,$b1) = stc_hex2rgb($main['color'][0]);
190  if ((( (($r1+1)/256)*(($g1+1)/256)*(($b1+1)/256) ) * 1000 ) < 125 )
191       $main['color6'] = lighten( $r1, $g1, $b1, 10);
192  else $main['color6'] = darken( $r1, $g1, $b1, 10);
193  list($r1,$g1,$b1) = stc_hex2rgb($main['color'][4]);
194  if ((( (($r1+1)/256)*(($g1+1)/256)*(($b1+1)/256) ) * 1000 ) < 125 )
195       $main['color7'] = lighten( $r1, $g1, $b1, 10);
196  else $main['color7'] = darken( $r1, $g1, $b1, 10);
197
198  // Go ahead
199  if (count($errors) == 0) {
200    umask(0000);
201    mkdir($themedir, 0777);
202    if (!is_dir(  $themedir ))
203        array_push($errors,
204          l10n('Theme directory creation failure: ' 
205          . 'it can\'t be created (for now en attendant la suite 8-) ).'));
206    else {
207      $main['ldelim'] = '{ldelim}';
208      /*
209       * Build themeconf.inc.php
210       **/
211      $plugin_tpl = new Template();
212      $plugin_tpl->set_filenames(array('themeconf'=>
213      dirname(__FILE__) . '/themeconf.inc.tpl'));
214      $plugin_tpl->assign('main',$main);
215      $main['themeconf_inc_php'] = $plugin_tpl->parse('themeconf', true);
216      $r = stc_newfile( $themedir . '/themeconf.inc.php', 
217        $main['themeconf_inc_php'] );
218      /*
219       * Build mail-css.tpl
220       **/ 
221      $plugin_tpl->set_filenames(array('mailcss'=>
222      dirname(__FILE__) . '/mail-css.tpl2'));
223      $plugin_tpl->assign('main',$main);
224      $main['mail-css.tpl'] = $plugin_tpl->parse('mailcss', true); 
225      $r = $r && stc_newfile( $themedir . '/mail-css.tpl', 
226        $main['mail-css.tpl'] );
227      /*
228       * Build theme.css
229       **/ 
230      $plugin_tpl->set_filenames(array('theme'=>
231      dirname(__FILE__) . '/theme.tpl'));
232      $plugin_tpl->assign('main',$main);
233      $main['theme.css'] = $plugin_tpl->parse('theme', true); 
234      $r = $r && stc_newfile( $themedir . '/theme.css', 
235        $main['theme.css'] );
236      /*
237       * Build background image for titrePage or definition list (in #menubar)
238       **/
239      if (function_exists('imagecreatefrompng'))
240      {
241        $img = imagecreatefrompng(dirname(__FILE__) . '/titrePage-bg.png');
242        $dest = imagecreate(1, 64);
243        for ($i=0; $i<256; $i++) {
244          imagecolorallocate($dest, $i, $i, $i); 
245        }
246        imagecopy($dest, $img, 0, 0, 0, 0, 1, 64);
247        list($r1,$g1,$b1) = stc_hex2rgb($main['color'][4]);
248        for ($i = 0; $i < 256; $i++) {
249          imagecolorset($dest, $i, min(floor($i * $r1 / 255), 255), 
250                         min(floor($i * $g1 / 255), 255), 
251                         min(floor($i * $b1 / 255), 255));
252        }
253        // to be tested imagecopymerge($dest,$img,0,0,0,0,1,64,33);
254        imagepng( $dest, $themedir . '/stc.png', 9 );
255        imagedestroy ($img);
256        imagedestroy ($dest);
257      }
258      else @copy( dirname(__FILE__) 
259                 . '/titrePage-bg.png', $themedir . '/stc.png');     
260      if ($r == false) {
261        array_push($errors,
262          l10n('Theme files creation failure: theme should be deleted.'));
263        @unlink( $themedir . '/stc.png' );
264        @unlink( $themedir . '/themeconf.inc.php' );
265        @unlink( $themedir . '/mail-css.tpl' );
266        @unlink( $themedir . '/theme.css' );
267        @rmdir( $themedir );
268      }
269      else {
270        array_push($infos,
271       '['.$main['newtpl'] . '/' . $main['newtheme'].'] : '
272       .l10n('Congratulation! You have got(/ten) a new available theme.')); 
273      }
274    }
275  }
276 
277  // Interesting Graphic Charter
278  // http://accessites.org/site/2006/08/visual-vs-structural/
279
280  $swift_theme_creator->save_theme_config(); 
281}
282
283// +-----------------------------------------------------------------------+
284// |                            reset values
285// +-----------------------------------------------------------------------+
286
287// To be implemented delete $main save and redirect
288
289// Don't forget to re-read because some statements are superfluous
290 
291// +-----------------------------------------------------------------------+
292// |                            template initialization
293// +-----------------------------------------------------------------------+
294$template->set_filenames(array(
295    'plugin_admin_content' => dirname(__FILE__) . '/theme_creator.tpl'));
296$template->append('head_elements',
297   '<script type="text/javascript"
298        src="./plugins/SwiftThemeCreator/farbtastic/farbtastic.js"></script>
299<link rel="stylesheet" type="text/css"
300      href="./plugins/SwiftThemeCreator/farbtastic/farbtastic.css" />
301<style type="text/css" media="screen">
302.colorwell { border: 3px double #F30; width: 6em;
303  text-align: center; cursor: pointer; }
304body .colorwell-selected { border: 3px double #F36; font-weight: bold; }
305.radio { margin: 0 10px 0 50px; }
306</style>'
307    );
308
309/* Templates */
310$template->assign('template_options', $available_templates);
311if (!isset($main['template_options'])) $main['template_options'] = 0; 
312
313/* New theme */
314if (isset($_POST['new_theme'])) $main['new_theme'] = $_POST['new_theme'];
315
316/* Colors */
317if (isset($_POST['color1'])) $main['color1'] = $_POST['color1'];
318if (isset($_POST['color2'])) $main['color2'] = $_POST['color2'];
319if (isset($_POST['color3'])) $main['color3'] = $_POST['color3'];
320if (isset($_POST['color4'])) $main['color4'] = $_POST['color4'];
321if (isset($_POST['color5'])) $main['color5'] = $_POST['color5'];
322if (!isset($main['color1'])) $main['color1'] = '#111111'; 
323if (!isset($main['color2'])) $main['color2'] = '#EEEEEE'; 
324if (!isset($main['color3'])) $main['color3'] = '#FF7700'; 
325if (!isset($main['color4'])) $main['color4'] = '#FF3333'; 
326if (!isset($main['color5'])) $main['color5'] = '#FF3363'; 
327
328/* header */
329if (isset($_POST['background'])) $main['background'] = $_POST['background'];
330if (!isset($main['background'])) $main['background'] = 'off'; 
331$template->assign('background_options',
332  array(
333    'off' => l10n('No'),
334    'random' => l10n('24H Random'),
335    'fixed' => l10n('Fixed URL'),
336  ));
337
338$query = '
339SELECT id,name,uppercats,global_rank
340  FROM ' . CATEGORIES_TABLE . ';';
341display_select_cat_wrapper($query,array(),'src_category');
342if (isset($_POST['src_category'])) $main['src_category'] = 
343    $_POST['src_category'];
344
345$main['picture_url'] = PHPWG_ROOT_PATH . 'plugins/SwiftThemeCreator/sample.jpg';
346if (isset($swift_theme_creator->picture_url)) 
347    $main['picture_url'] = $swift_theme_creator->picture_url;
348if (isset($_POST['picture_url'])) $main['picture_url'] = $_POST['picture_url'];
349
350if (isset($_POST['picture_width'])) 
351    $main['picture_width'] = $_POST['picture_width'];
352if (!isset($main['picture_width'])) $main['picture_width'] = 2048; 
353if (isset($_POST['picture_height'])) 
354    $main['picture_height'] = $_POST['picture_height'];
355if (!isset($main['picture_height'])) $main['picture_height'] = 100; 
356
357if (isset($_POST['background_mode'])) 
358      $main['background_mode'] = $_POST['background_mode'];
359if (!isset($main['background_mode'])) $main['background_mode'] = 'as'; 
360$template->assign('background_mode_options',
361  array(
362    'as' => l10n('As is'),
363    'crop' => l10n('Truncated'),
364    'sized' => l10n('Resized'),
365  ));
366if (count($errors) != 0) $template->assign('errors', $errors);
367if (count($infos) != 0) $template->assign('infos', $infos);
368/* Restore Main values */
369$template->assign('main', $main);
370$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
371$swift_theme_creator->theme_config = $main;
372$swift_theme_creator->save_theme_config();
373?>
Note: See TracBrowser for help on using the repository browser.