FusionDirectory
 All Data Structures Files Functions Variables
class_FileAttribute.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4  Copyright (C) 2012-2016 FusionDirectory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
24 class FileAttribute extends Attribute
25 {
26  function loadPostValue()
27  {
28  $this->postValue = $this->value;
29  if (!empty($_FILES[$this->getHtmlId()]['name']) && $this->isVisible()) {
30  if ($_FILES[$this->getHtmlId()]['size'] <= 0) {
31  msg_dialog::display(_("Error"), sprintf(_("Cannot read uploaded file: %s"), _("file is empty")), ERROR_DIALOG);
32  } elseif (!file_exists($_FILES[$this->getHtmlId()]['tmp_name'])) {
33  // Is there a tmp file, which we can use ?
34  msg_dialog::display(_("Error"), sprintf(_("Cannot read uploaded file: %s"), _("file not found")), ERROR_DIALOG);
35  } elseif (!$handle = @fopen($_FILES[$this->getHtmlId()]['tmp_name'], "r")) {
36  // Can we open the tmp file, for reading
37  msg_dialog::display(_("Error"), sprintf(_("Cannot read uploaded file: %s"), _("file not readable")), ERROR_DIALOG);
38  } else {
39  // Everything just fine :)
40 
41  // Reading content
42  $this->readFile($handle);
43  }
44  // so that we only handle the file once
45  $_FILES[$this->getHtmlId()]['name'] = "";
46  }
47  }
48 
53  function readFile($handle)
54  {
55  $postValue = fread($handle, 1024);
56  while (!feof($handle)) {
57  $postValue .= fread($handle, 1024);
58  }
59  $this->setPostValue($postValue);
60  @fclose($handle);
61  }
62 
63  function renderFormInput ()
64  {
65  $id = $this->getHtmlId();
66  $display = $this->renderInputField('file', $id);
67  return $this->renderAcl($display);
68  }
69 
70  function displayValue($value)
71  {
72  return sprintf(_('%s (%d bytes)'), $this->getLabel(), mb_strlen($value, '8bit'));
73  }
74 }
75 
76 /* FileAttribue with download button */
78 {
79  protected $extension;
80  protected $upload;
81 
82  function __construct ($label, $description, $ldapName, $required = FALSE, $extension = '', $upload = FALSE, $defaultValue = "", $acl = "")
83  {
84  parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
85  $this->extension = $extension;
86  $this->upload = $upload;
87  }
88 
89  function computeFilename()
90  {
91  return $this->getLdapName().$this->extension;
92  }
93 
94  function loadPostValue ()
95  {
96  if ($this->isVisible()) {
97  $this->postValue = $this->value;
98  foreach (array_keys($_POST) as $name) {
99  if (preg_match('/^download'.$this->getHtmlId().'/', $name)) {
100  session::set('binary', $this->value);
101  session::set('binarytype', 'octet-stream');
102  session::set('binaryfile', $this->computeFilename());
103  header('location: getbin.php');
104  exit();
105  }
106  }
107  if ($this->upload && isset($_POST['upload'.$this->getHtmlId()])) {
108  parent::loadPostValue();
109  }
110  }
111  }
112 
113  function renderFormInput ()
114  {
115  $id = $this->getHtmlId();
116  $display = '';
117  if ($this->upload) {
118  $display .= $this->renderInputField('file', $id);
119  $display .= $this->renderInputField('submit', 'upload'.$id, array('value' => _('Upload')));
120  }
121  $display .= $this->renderInputField(
122  'image', 'download'.$id,
123  array(
124  'title' => _('Download'),
125  'alt' => _('Download'),
126  'class' => 'center',
127  'src' => 'geticon.php?context=actions&amp;icon=document-save&amp;size=16',
128  )
129  );
130  return $this->renderAcl($display);
131  }
132 
133  public function htmlIds()
134  {
135  $id = $this->getHtmlId();
136  $ids = array('download'.$id);
137  if ($this->upload) {
138  $ids[] = $id;
139  $ids[] = 'upload'.$id;
140  }
141  return $ids;
142  }
143 
144  function renderAttribute(&$attributes, $readOnly)
145  {
146  if ($this->upload == FALSE) {
147  parent::renderAttribute($attributes, FALSE);
148  } else {
149  parent::renderAttribute($attributes, $readOnly);
150  }
151  }
152 }
153 
155 {
156  /* Default values are not the same that for FileDownloadAttribute */
157  function __construct ($label, $description, $ldapName, $required = FALSE, $extension = '.txt', $upload = TRUE, $defaultValue = '', $acl = '')
158  {
159  parent::__construct(
160  $label, $description, $ldapName, $required,
161  $extension, $upload, $defaultValue, $acl
162  );
163  }
164 
167  function loadPostValue ()
168  {
169  if ($this->isVisible()) {
170  $this->postValue = $this->value;
171  foreach (array_keys($_POST) as $name) {
172  if (preg_match('/^download'.$this->getHtmlId().'/', $name)) {
173  session::set('binary', $this->value);
174  session::set('binarytype', 'octet-stream');
175  session::set('binaryfile', $this->computeFilename());
176  header('location: getbin.php');
177  exit();
178  }
179  }
180  if ($this->upload) {
181  if (isset($_POST['upload'.$this->getHtmlId()])) {
182  parent::loadPostValue();
183  } else {
184  $id = $this->getHtmlId().'_text';
185  if (isset($_POST[$id])) {
186  $this->setPostValue($_POST[$id]);
187  }
188  }
189  }
190  }
191  }
192 
193  function fixPostValue ($value)
194  {
195  /* Replace CRLF by LF, to avoid non-ASCII chars */
196  return str_replace(array("\r\n", "\r"), "\n", $value);
197  }
198 
199  function renderFormInput ()
200  {
201  $id = $this->getHtmlId();
202  $display = '<textarea name="'.$id.'_text" id="'.$id.'_text"'.
203  ($this->disabled? 'disabled="disabled"':'').'>'.
204  '{literal}'.htmlentities($this->getValue(), ENT_COMPAT, 'UTF-8').'{/literal}</textarea><br/>';
205  return $this->renderAcl($display).parent::renderFormInput();
206  }
207 
208  public function htmlIds()
209  {
210  $id = $this->getHtmlId();
211  $ids = parent::htmlIds();
212  $ids[] = $id.'_text';
213  return $ids;
214  }
215 }
216 
218 {
219  protected $width;
220  protected $height;
221  protected $format;
222  protected $forceSize;
223  protected $placeholder;
224 
225  function __construct ($label, $description, $ldapName, $required = FALSE, $width = 48, $height = 48, $format = 'png', $forceSize = FALSE, $defaultValue = "", $acl = "")
226  {
227  parent::__construct($label, $description, $ldapName, $required, $defaultValue, $acl);
228  $this->width = $width;
229  $this->height = $height;
230  $this->format = $format;
231  $this->forceSize = $forceSize;
232  }
233 
234  function setPlaceholder($placeholder)
235  {
236  $this->placeholder = $placeholder;
237  }
238 
241  function loadPostValue ()
242  {
243  $this->postValue = $this->value;
244  $id = $this->getHtmlId();
245  if (!$this->disabled && $this->isVisible()) {
246  foreach (array_keys($_POST) as $name) {
247  if (!$this->isRequired()) {
248  if (preg_match('/^'.$id.'_remove_/', $name)) {
249  $this->setPostValue('');
250  break;
251  }
252  }
253  if (preg_match('/^'.$id.'_upload_/', $name)) {
254  parent::loadPostValue();
255  break;
256  }
257  }
258  }
259  }
260 
261  function setValue ($value)
262  {
263  if ($value == "") {
264  $this->value = "";
265  return;
266  }
267  if (class_exists('Imagick')) {
268  $im = new Imagick();
269  $modify = FALSE;
270  $im->readImageBlob($value);
271 
272  $size = $im->getImageGeometry();
273 
274  if (
275  ($size['width'] > 0 && $size['height'] > 0) &&
276  (
277  ($size['width'] < $this->width && $size['height'] < $this->height) ||
278  ($size['width'] > $this->width) ||
279  ($size['height'] > $this->height)
280  )
281  ) {
282  $modify = TRUE;
283  $im->resizeImage($this->width, $this->height, Imagick::FILTER_GAUSSIAN, 1, !$this->forceSize);
284  }
285 
286  if ($modify || !preg_match('/^'.$this->format.'$/i', $im->getImageFormat())) {
287  if ($this->format == 'jpeg') {
288  $im->setImageCompression(Imagick::COMPRESSION_JPEG);
289  $im->setImageCompressionQuality(90);
290  }
291  $im->setImageFormat($this->format);
292 
293  /* Save attribute */
294  $this->value = $im->getImageBlob();
295  } else {
296  $this->value = $value;
297  }
298  } else {
300  _('Error'),
301  _('Cannot save user picture, FusionDirectory requires the PHP module "imagick" to be installed!'),
302  ERROR_DIALOG
303  );
304  }
305  }
306 
307  function renderFormInput ()
308  {
309  $this->setValue($this->inputValue($this->getValue()));
310  $id = $this->getHtmlId();
311  // Just to be sure the image is not cached
312  srand((double)microtime() * 1000000);
313  $key = $this->getLdapName().rand(0, 10000);
314  $display = '<img id="'.$id.'_img"'.
315  ($this->disabled ? 'disabled="disabled"' : '').
316  ' src="getbin.php?key='.$key.'"'.
317  ' style="border:1px solid black;"'.
318  ' alt="'.$this->getDescription().'"'.
319  ' title="'.$this->getDescription().'"'.
320  ' /><br/>';
321  $display .= $this->renderInputField('file', $id);
322  $display .= $this->renderInputField(
323  'image', $id.'_upload',
324  array(
325  'class' => 'center',
326  'src' => 'geticon.php?context=actions&amp;icon=upload&amp;size=16',
327  'title' => _('Upload'),
328  'alt' => _('Upload')
329  )
330  );
331  if (!$this->isRequired()) {
332  $display .= $this->renderInputField(
333  'image', $id.'_remove',
334  array(
335  'class' => 'center',
336  'src' => 'geticon.php?context=actions&amp;icon=remove&amp;size=16',
337  'title' => _('Remove'),
338  'alt' => _('Remove')
339  )
340  );
341  }
342  if (($this->getValue() == '') && ($this->placeholder != '')) {
343  session::set('binary'.$key, $this->placeholder);
344  } else {
345  session::set('binary'.$key, $this->getValue());
346  }
347  session::set('binarytype', 'image/'.$this->format);
348  return $this->renderAcl($display);
349  }
350 
351  public function htmlIds()
352  {
353  $id = $this->getHtmlId();
354  return array($id.'_img',$id,'upload'.$id);
355  }
356 
359  function fillLdapValue (&$attrs)
360  {
361  if ($this->isInLdap()) {
362  $value = $this->computeLdapValue();
363  if ($value !== '') {
364  if (isset($this->plugin) && $this->plugin->is_template) {
365  /* Add %% to provide template from parsing binary string */
366  $value = '%%'.$value;
367  }
368  $attrs[$this->getLdapName()] = $value;
369  } else {
370  $attrs[$this->getLdapName()] = array();
371  }
372  }
373  }
374 
375  function inputValue ($value)
376  {
377  /* Remove %% that might be there in case of templating */
378  return preg_replace('/^%%/', '', $value);
379  }
380 }
renderAcl($display)
Add ACL information around display.
setPostValue($value)
Set the postValue.
computeLdapValue()
Computes LDAP value.
This class allow to handle easily any kind of LDAP attribute.
loadPostValue()
Update this attributes postValue depending of the $_POST values.
readFile($handle)
This function read the file from the given handle and then closes it.
static set($name, $value)
Set a value in a session.
fillLdapValue(&$attrs)
Fill LDAP value in the attrs array.
static display($s_title, $s_message, $i_type=INFO_DIALOG)
Display a message dialog.
This class allow to handle easily an File LDAP attribute.
loadPostValue()
Update this attributes postValue depending of the $_POST values.