aboutsummaryrefslogtreecommitdiffstats
path: root/admin/include/configuration_watermark_process.inc.php
blob: 0e07380a78231cd7baa042c05b8eb9f68de949f4 (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
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery                                    |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008-2016 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.                                                                  |
// +-----------------------------------------------------------------------+

if( !defined("PHPWG_ROOT_PATH") )
{
  die ("Hacking attempt!");
}

function get_watermark_filename($list, $candidate, $step = 0)
{
  global $change_name;
  $change_name = $candidate;
  if ($step != 0)
  {
    $change_name .= '-'.$step;
  }
  if (in_array($change_name, $list))
  {
    return get_watermark_filename($list, $candidate, $step+1);
  }
  return $change_name.'.png';
}

$errors = array();
$pwatermark = $_POST['w'];

// step 0 - manage upload if any
if (isset($_FILES['watermarkImage']) and !empty($_FILES['watermarkImage']['tmp_name']))
{
  list($width, $height, $type) = getimagesize($_FILES['watermarkImage']['tmp_name']);
  if (IMAGETYPE_PNG != $type)
  {
    $errors['watermarkImage'] = sprintf(
      l10n('Allowed file types: %s.'),
      'PNG'
      );
  }
  else
  {
    $upload_dir = PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'watermarks';
    if (mkgetdir($upload_dir, MKGETDIR_DEFAULT&~MKGETDIR_DIE_ON_ERROR))
    {
      // file name may include exotic chars like single quote, we need a safe name 
      $new_name = str2url(get_filename_wo_extension($_FILES['watermarkImage']['name']));

      // we need existing watermarks to avoid overwritting one 
      $watermark_files = array();
      if ( ($glob=glob(PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'watermarks/*.png')) !== false)
      {
        foreach ($glob as $file)
        {
          $watermark_files[] = get_filename_wo_extension(
            substr($file, strlen(PHPWG_ROOT_PATH.PWG_LOCAL_DIR.'watermarks/'))
          );
        }
      }

      $file_path = $upload_dir.'/'.get_watermark_filename($watermark_files, $new_name);

      if (move_uploaded_file($_FILES['watermarkImage']['tmp_name'], $file_path))
      {
        $pwatermark['file'] = substr($file_path, strlen(PHPWG_ROOT_PATH));
      }
      else
      {
        $page['errors'][] = $errors['watermarkImage'] = "$file_path " .l10n('no write access');
      }
    }
    else
    {
      $page['errors'][] = $errors['watermarkImage'] = sprintf( l10n('Add write access to the "%s" directory'), $upload_dir);
    }
  }
}

// step 1 - sanitize HTML input
switch ($pwatermark['position'])
{
  case 'topleft':
  {
    $pwatermark['xpos'] = 0;
    $pwatermark['ypos'] = 0;
    break;
  }
  case 'topright':
  {
    $pwatermark['xpos'] = 100;
    $pwatermark['ypos'] = 0;
    break;
  }
  case 'middle':
  {
    $pwatermark['xpos'] = 50;
    $pwatermark['ypos'] = 50;
    break;
  }
  case 'bottomleft':
  {
    $pwatermark['xpos'] = 0;
    $pwatermark['ypos'] = 100;
    break;
  }
  case 'bottomright':
  {
    $pwatermark['xpos'] = 100;
    $pwatermark['ypos'] = 100;
    break;
  }
}

// step 2 - check validity
$v = intval($pwatermark['xpos']);
if ($v < 0 or $v > 100)
{
  $errors['watermark']['xpos'] = '[0..100]';
}

$v = intval($pwatermark['ypos']);
if ($v < 0 or $v > 100)
{
  $errors['watermark']['ypos'] = '[0..100]';
}

$v = intval($pwatermark['opacity']);
if ($v <= 0 or $v > 100)
{
  $errors['watermark']['opacity'] = '(0..100]';
}

// step 3 - save data
if (count($errors) == 0)
{
  $watermark = new WatermarkParams();
  $watermark->file = $pwatermark['file'];
  $watermark->xpos = intval($pwatermark['xpos']);
  $watermark->ypos = intval($pwatermark['ypos']);
  $watermark->xrepeat = intval($pwatermark['xrepeat']);
  $watermark->yrepeat = intval($pwatermark['yrepeat']);
  $watermark->opacity = intval($pwatermark['opacity']);
  $watermark->min_size = array(intval($pwatermark['minw']),intval($pwatermark['minh']));

  $old_watermark = ImageStdParams::get_watermark();
  $watermark_changed =
    $watermark->file != $old_watermark->file
    || $watermark->xpos != $old_watermark->xpos
    || $watermark->ypos != $old_watermark->ypos
    || $watermark->xrepeat != $old_watermark->xrepeat
    || $watermark->yrepeat != $old_watermark->yrepeat
    || $watermark->opacity != $old_watermark->opacity;

  // save the new watermark configuration
  ImageStdParams::set_watermark($watermark);

  // do we have to regenerate the derivatives (and which types)?
  $changed_types = array();

  foreach (ImageStdParams::get_defined_type_map() as $type => $params)
  {
    $old_use_watermark = $params->use_watermark;
    ImageStdParams::apply_global($params);

    $changed = $params->use_watermark != $old_use_watermark;
    if (!$changed and $params->use_watermark)
    {
      $changed = $watermark_changed;
    }
    if (!$changed and $params->use_watermark)
    {
      // if thresholds change and before/after the threshold is lower than the corresponding derivative side -> some derivatives might switch the watermark
      $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());
      $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());
    }

    if ($changed)
    {
      $params->last_mod_time = time();
      $changed_types[] = $type;
    }
  }

  ImageStdParams::save();

  if (count($changed_types))
  {
    clear_derivative_cache($changed_types);
  }

  $page['infos'][] = l10n('Your configuration settings are saved');
}
else
{
  $template->assign('watermark', $pwatermark);
  $template->assign('ferrors', $errors);
}
?>