source: trunk/admin/include/configuration_watermark_process.inc.php @ 22135

Last change on this file since 22135 was 19703, checked in by plg, 12 years ago

update Piwigo headers to 2013 (the end of the world didn't occur as expected on r12922)

File size: 5.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2013 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
24if( !defined("PHPWG_ROOT_PATH") )
25{
26  die ("Hacking attempt!");
27}
28
29$errors = array();
30$pwatermark = $_POST['w'];
31
32// step 0 - manage upload if any
33if (isset($_FILES['watermarkImage']) and !empty($_FILES['watermarkImage']['tmp_name']))
34{
35  list($width, $height, $type) = getimagesize($_FILES['watermarkImage']['tmp_name']);
36  if (IMAGETYPE_PNG != $type)
37  {
38    $errors['watermarkImage'] = sprintf(
39      l10n('Allowed file types: %s.'),
40      'PNG'
41      );
42  }
43  else
44  {
45    $upload_dir = PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'watermarks';
46    if (mkgetdir($upload_dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR))
47    {
48      $new_name = get_filename_wo_extension($_FILES['watermarkImage']['name']).'.png';
49      $file_path = $upload_dir.'/'.$new_name;
50
51      if (move_uploaded_file($_FILES['watermarkImage']['tmp_name'], $file_path))
52      {
53        $pwatermark['file'] = substr($file_path, strlen(PHPWG_ROOT_PATH));
54      }
55      else
56      {
57        $page['errors'][] = $errors['watermarkImage'] = "$file_path " .l10n('no write access');
58      }
59    }
60    else
61    {
62      $page['errors'][] = $errors['watermarkImage'] = sprintf( l10n('Add write access to the "%s" directory'), $upload_dir);
63    }
64  }
65}
66
67// step 1 - sanitize HTML input
68switch ($pwatermark['position'])
69{
70  case 'topleft':
71  {
72    $pwatermark['xpos'] = 0;
73    $pwatermark['ypos'] = 0;
74    break;
75  }
76  case 'topright':
77  {
78    $pwatermark['xpos'] = 100;
79    $pwatermark['ypos'] = 0;
80    break;
81  }
82  case 'middle':
83  {
84    $pwatermark['xpos'] = 50;
85    $pwatermark['ypos'] = 50;
86    break;
87  }
88  case 'bottomleft':
89  {
90    $pwatermark['xpos'] = 0;
91    $pwatermark['ypos'] = 100;
92    break;
93  }
94  case 'bottomright':
95  {
96    $pwatermark['xpos'] = 100;
97    $pwatermark['ypos'] = 100;
98    break;
99  }
100}
101
102// step 2 - check validity
103$v = intval($pwatermark['xpos']);
104if ($v < 0 or $v > 100)
105{
106  $errors['watermark']['xpos'] = '[0..100]';
107}
108
109$v = intval($pwatermark['ypos']);
110if ($v < 0 or $v > 100)
111{
112  $errors['watermark']['ypos'] = '[0..100]';
113}
114
115$v = intval($pwatermark['opacity']);
116if ($v <= 0 or $v > 100)
117{
118  $errors['watermark']['opacity'] = '(0..100]';
119}
120
121// step 3 - save data
122if (count($errors) == 0)
123{
124  $watermark = new WatermarkParams();
125  $watermark->file = $pwatermark['file'];
126  $watermark->xpos = intval($pwatermark['xpos']);
127  $watermark->ypos = intval($pwatermark['ypos']);
128  $watermark->xrepeat = intval($pwatermark['xrepeat']);
129  $watermark->opacity = intval($pwatermark['opacity']);
130  $watermark->min_size = array(intval($pwatermark['minw']),intval($pwatermark['minh']));
131
132  $old_watermark = ImageStdParams::get_watermark();
133  $watermark_changed =
134    $watermark->file != $old_watermark->file
135    || $watermark->xpos != $old_watermark->xpos
136    || $watermark->ypos != $old_watermark->ypos
137    || $watermark->xrepeat != $old_watermark->xrepeat
138    || $watermark->opacity != $old_watermark->opacity;
139
140  // save the new watermark configuration
141  ImageStdParams::set_watermark($watermark);
142
143  // do we have to regenerate the derivatives (and which types)?
144  $changed_types = array();
145
146  foreach (ImageStdParams::get_defined_type_map() as $type => $params)
147  {
148    $old_use_watermark = $params->use_watermark;
149    ImageStdParams::apply_global($params);
150
151    $changed = $params->use_watermark != $old_use_watermark;
152    if (!$changed and $params->use_watermark)
153    {
154      $changed = $watermark_changed;
155    }
156    if (!$changed and $params->use_watermark)
157    {
158      // if thresholds change and before/after the threshold is lower than the corresponding derivative side -> some derivatives might switch the watermark
159      $changed |= $watermark->min_size[0]!=$old_watermark->min_size[0] and ($watermark->min_size[0]<$params->max_width() or $old_watermark->min_size[0]<$params->max_width());
160      $changed |= $watermark->min_size[1]!=$old_watermark->min_size[1] and ($watermark->min_size[1]<$params->max_height() or $old_watermark->min_size[1]<$params->max_height());
161    }
162
163    if ($changed)
164    {
165      $params->last_mod_time = time();
166      $changed_types[] = $type;
167    }
168  }
169
170  ImageStdParams::save();
171
172  if (count($changed_types))
173  {
174    clear_derivative_cache($changed_types);
175  }
176
177  array_push(
178    $page['infos'],
179    l10n('Your configuration settings are saved')
180    );
181}
182else
183{
184  $template->assign('watermark', $pwatermark);
185  $template->assign('ferrors', $errors);
186}
187?>
Note: See TracBrowser for help on using the repository browser.