aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/SwiftThemeCreator/theme_creator.php
blob: 1c9f0d9dfc1c25c381f1bda7cface3840538b4b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based picture gallery                                  |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008      Piwigo Team                  http://piwigo.org |
// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+

/* Ajouter le lien au menu de l'admin */
if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
if (!defined('IN_ADMIN') or !IN_ADMIN) die('Hacking attempt!');

/*
 * stc_hex2rgb convert any string to array of RGB values
 */
function stc_hex2rgb($color)
{
  if ($color[0] == '#') $color = substr($color, 1);
  if (strlen($color) == 6)
    list($r, $g, $b) = array($color[0].$color[1],
                             $color[2].$color[3],
                             $color[4].$color[5]);
  else {
    $color .= $color . $color . '000';
    list($r, $g, $b) = array($color[0].$color[0],
                             $color[1].$color[1], 
                             $color[2].$color[2]);
  }
  return array(hexdec($r), hexdec($g), hexdec($b));
}
/*
 * lighten returns array of x% of lighter RGB values
 */
function lighten( $r, $g, $b, $percent)
{
  $r = min(round($r+(($percent*(255-$r))/100)),255);
  $g = min(round($g+(($percent*(255-$g))/100)),255);
  $b = min(round($b+(($percent*(255-$b))/100)),255);
  return sprintf('#%02X%02X%02X', $r, $g, $b);
}
/*
 * darken returns array of x% of darker RGB values
 */
function darken( $r, $g, $b, $percent)
{
  $r = max(round($r-(($percent*$r)/100)),0);
  $g = max(round($g-(($percent*$g)/100)),0);
  $b = max(round($b-(($percent*$b)/100)),0);
  return sprintf('#%02X%02X%02X', $r, $g, $b);
}


$errors = array();
$infos = array();
$available_templates = array();
$template_dir = PHPWG_ROOT_PATH.'template';
foreach (get_dirs($template_dir) as $dir)
{
  array_push($available_templates, $dir);
}

// +-----------------------------------------------------------------------+
// |                            selected templates                         |
// +-----------------------------------------------------------------------+
if (!isset($swift_theme_creator)) $swift_theme_creator = new ThemeCreator();
$swift_theme_creator->reload();
$main = $swift_theme_creator->theme_config;

if (isset($_POST['submit']) and (!is_adviser()))
{
  // 1 - Theme name control
  $main['newtheme'] = strip_tags($_POST['new_theme']);
  if ( !preg_match('/^[a-z0-9-_]{1,8}$/', $main['newtheme']) ) 
      array_push($errors,
         l10n('Invalid theme name: 1 to 8 lowercase alphanumeric characters'
         . ' including "-" and "_".')); 

  // 2 - Colours control
  $main['color'] = array($_POST['color1'], $_POST['color2'], 
                         $_POST['color3'], $_POST['color4'], 
                         $_POST['color5']);
  $colors = $main['color'][0] . $main['color'][1] . $main['color'][2] 
          . $main['color'][3] . $main['color'][4];
  if ( !preg_match('/^(#?([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})){5}$/', $colors) ) 
      array_push($errors,
         l10n('Invalid color code: 3 or 6 hexadecimal characters, preceded' 
         . ' or not by "#"')); 

  // 2.1 - Background and text control
  list($r1,$g1,$b1) = stc_hex2rgb($main['color'][0]);
  list($r2,$g2,$b2) = stc_hex2rgb($main['color'][1]);
  // Formula for converting RGB values to YIQ values as perceived brightness difference.
  // Background and text "brightness" difference control:
  $dif = abs( ( (($r1*299)+($g1*587)+($b1*114)) / 1000 )
          - ( (($r2*299)+($g2*587)+($b2*114)) / 1000 ));
  if ( $dif < 125 )
      array_push($errors,
       l10n('Insufficient brightness difference between text and background. dif=') . $dif); 
  // Background and text "colour" difference control:
  $dif = (max($r1, $r2) - min($r1, $r2)) 
     + (max($g1, $g2) - min($g1, $g2)) 
     + (max($b1, $b2) - min($b1, $b2));
  if ( $dif < 200 )
    array_push($errors,
       l10n('Insufficient colour difference between text and background. dif=') . $dif); 

  // 2.2 - Background and Internal links control
  list($r1,$g1,$b1) = stc_hex2rgb($main['color'][0]);
  list($r2,$g2,$b2) = stc_hex2rgb($main['color'][2]);
  // Background and Internal links "brightness" difference control:
  $dif = abs( ( (($r1*299)+($g1*587)+($b1*114)) / 1000 )
          - ( (($r2*299)+($g2*587)+($b2*114)) / 1000 ));
  if ( $dif < 125 )
      array_push($errors,
       l10n('Insufficient brightness difference between Internal links and background. dif=') . $dif); 
  // Background and Internal links "colour" difference control:
  $dif = (max($r1, $r2) - min($r1, $r2)) 
     + (max($g1, $g2) - min($g1, $g2)) 
     + (max($b1, $b2) - min($b1, $b2));
  if ( $dif < 200 )
    array_push($errors,
       l10n('Insufficient colour difference between Internal links and background. dif=') . $dif); 

  // 3 - Directory control
  $main['templatedir'] = PHPWG_ROOT_PATH . 'template/' 
               . $available_templates[$_POST['template']];
  $main['newtpl'] = $available_templates[$_POST['template']];
  $themedir = $main['templatedir'] . '/' . $main['newtheme'];
  if (is_dir(  $themedir )) 
    array_push($errors,
       '['.$themedir.'] : '.l10n('Invalid theme: This theme exists already (no override available).')); 
  elseif (!is_writable($main['templatedir']))
    array_push($errors,
       '['.$main['templatedir'].'] : '.l10n('no_write_access'));

  // 4 - Picture URL control
  if ( $_POST['background'] == 'fixed' and (is_dir($_POST['picture_url'])
      or !is_file($_POST['picture_url'])) )
    array_push($errors,
       l10n('Header picture is not found, check its path and name.')); 
  
  // 5 - Expected Width and Height limits control
  if ( !(ctype_digit($_POST['picture_width']) and $_POST['picture_width'] > 11 
       and $_POST['picture_width'] < 4097 ) )
    array_push($errors,
       '['.$_POST['picture_width'].'] : ' 
       . l10n('incorrect width value [12-4096].')); 
  if ( !(ctype_digit($_POST['picture_height']) and $_POST['picture_height'] > 11 
       and $_POST['picture_height'] < 201 ) )
    array_push($errors,
       '['.$_POST['picture_height'].'] : '
       . l10n('incorrect width value [12-200].')); 
       
  // 6 - Generate missing colors values
  list($r1,$g1,$b1) = stc_hex2rgb($main['color'][0]);
  if ((( (($r1+1)/256)*(($g1+1)/256)*(($b1+1)/256) ) * 1000 ) < 125 )
       $main['color6'] = lighten( $r1, $g1, $b1, 10);
  else $main['color6'] = darken( $r1, $g1, $b1, 10);
  list($r1,$g1,$b1) = stc_hex2rgb($main['color'][4]);
  if ((( (($r1+1)/256)*(($g1+1)/256)*(($b1+1)/256) ) * 1000 ) < 125 )
       $main['color7'] = lighten( $r1, $g1, $b1, 10);
  else $main['color7'] = darken( $r1, $g1, $b1, 10);

  // Go ahead 
  if (count($errors) == 0) {
    umask(0000);
    // mkdir($themedir, 0777);
    if (!is_dir(  $themedir ))
        array_push($errors,
          l10n('Theme directory creation failure: it can\'t be created (for now en attendant la suite 8-) ).'));
  }

  $main['ldelim'] = '{ldelim}';
  /*
   * Build themeconf.inc.php
   **/
  $plugin_tpl = new Template();
  $plugin_tpl->set_filenames(array('themeconf'=>
  dirname(__FILE__) . '/themeconf.inc.tpl'));
  $plugin_tpl->assign('main',$main);
  $main['themeconf_inc_php'] = $plugin_tpl->parse('themeconf', true);
  /*
   * Build mail-css.tpl
   **/  
  $plugin_tpl->set_filenames(array('mailcss'=>
  dirname(__FILE__) . '/mail-css.tpl2'));
  $plugin_tpl->assign('main',$main);
  $main['mail-css.tpl'] = $plugin_tpl->parse('mailcss', true);  

  // Smarty trace
  $plugin_tpl->assign('main',$main);
  
  // Interesting Graphic Charter
  // http://accessites.org/site/2006/08/visual-vs-structural/

  /*
   * Build background image for titrePage or definition list (in #menubar)
   **/
  if (function_exists('imagecreatefrompng'))
  {
    $img = imagecreatefrompng(dirname(__FILE__) . '/titrePage-bg.png');
    $dest = imagecreate(1, 64);
    for ($i=0; $i<256; $i++) {
      imagecolorallocate($dest, $i, $i, $i); 
    }
    imagecopy($dest, $img, 0, 0, 0, 0, 1, 64);
    list($r1,$g1,$b1) = stc_hex2rgb($main['color'][4]);
    for ($i = 0; $i < 256; $i++) {
      imagecolorset($dest, $i, min($i * $r1 / 255, 255), 
                     min($i * $g1 / 255, 255), 
                     min($i * $b1 / 255, 255));
    }
    // to be tested imagecopymerge($dest,$img,0,0,0,0,1,64,33);
    
    // Uncomment to create the header stc.png
    // imagepng( $dest, dirname(__FILE__) . '/stc.png', 9 );
    imagedestroy ($img);
    imagedestroy ($dest);
  }

  
  /* All logic is there
   * On "todo" : Create files and uncomment some previous statements.
   */


  $swift_theme_creator->save_theme_config();  
}

// +-----------------------------------------------------------------------+
// |                            reset values
// +-----------------------------------------------------------------------+

// To be implemented delete $main save and redirect

// Don't forget to re-read because some statements are superfluous
 
// +-----------------------------------------------------------------------+
// |                            template initialization
// +-----------------------------------------------------------------------+
$template->set_filenames(array(
    'plugin_admin_content' => dirname(__FILE__) . '/theme_creator.tpl'));
$template->append('head_elements',
  '<script type="text/javascript" src="./plugins/SwiftThemeCreator/farbtastic/farbtastic.js"></script>
<link rel="stylesheet" href="./plugins/SwiftThemeCreator/farbtastic/farbtastic.css" type="text/css" />
<style type="text/css" media="screen">
.colorwell { border: 3px double #F30; width: 6em; text-align: center; cursor: pointer; }
body .colorwell-selected { border: 3px double #F36; font-weight: bold; }
.radio { margin: 0 10px 0 50px; }
</style>'
    );

/* Templates */
$template->assign('template_options', $available_templates);
if (!isset($main['template_options'])) $main['template_options'] = 0; 

/* New theme */
if (isset($_POST['new_theme'])) $main['new_theme'] = $_POST['new_theme'];

/* Colors */
if (isset($_POST['color1'])) $main['color1'] = $_POST['color1'];
if (isset($_POST['color2'])) $main['color2'] = $_POST['color2'];
if (isset($_POST['color3'])) $main['color3'] = $_POST['color3'];
if (isset($_POST['color4'])) $main['color4'] = $_POST['color4'];
if (isset($_POST['color5'])) $main['color5'] = $_POST['color5'];
if (!isset($main['color1'])) $main['color1'] = '#111111'; 
if (!isset($main['color2'])) $main['color2'] = '#EEEEEE'; 
if (!isset($main['color3'])) $main['color3'] = '#FF7700'; 
if (!isset($main['color4'])) $main['color4'] = '#FF3333'; 
if (!isset($main['color5'])) $main['color5'] = '#FF3363'; 

/* header */
if (isset($_POST['background'])) $main['background'] = $_POST['background'];
if (!isset($main['background'])) $main['background'] = 'off'; 
$template->assign('background_options',
  array(
    'off' => l10n('No'),
    'random' => l10n('24H Random'),
    'fixed' => l10n('Fixed URL'),
  ));

$query = '
SELECT id,name,uppercats,global_rank
  FROM ' . CATEGORIES_TABLE . ';';
display_select_cat_wrapper($query,array(),'src_category');
if (isset($_POST['src_category'])) $main['src_category'] = 
    $_POST['src_category'];

$main['picture_url'] = PHPWG_ROOT_PATH . 'plugins/SwiftThemeCreator/sample.jpg';
if (isset($swift_theme_creator->picture_url)) 
    $main['picture_url'] = $swift_theme_creator->picture_url;
if (isset($_POST['picture_url'])) $main['picture_url'] = $_POST['picture_url'];

if (isset($_POST['picture_width'])) $main['picture_width'] = $_POST['picture_width'];
if (!isset($main['picture_width'])) $main['picture_width'] = 2048; 
if (isset($_POST['picture_height'])) $main['picture_height'] = $_POST['picture_height'];
if (!isset($main['picture_height'])) $main['picture_height'] = 100; 

if (isset($_POST['background_mode'])) 
      $main['background_mode'] = $_POST['background_mode'];
if (!isset($main['background_mode'])) $main['background_mode'] = 'as'; 
$template->assign('background_mode_options',
  array(
    'as' => l10n('As is'),
    'crop' => l10n('Truncated'),
    'sized' => l10n('Resized'),
  ));
if (count($errors) != 0) $template->assign('errors', $errors);
/* Restore Main values */
$template->assign('main', $main);
$template->assign_var_from_handle('ADMIN_CONTENT', 'plugin_admin_content');
$swift_theme_creator->theme_config = $main;
$swift_theme_creator->save_theme_config();
?>