You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

upload_photo.inc 3.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | program/steps/addressbook/upload_photo.inc |
  5. | |
  6. | This file is part of the Roundcube Webmail client |
  7. | Copyright (C) 2005-2011, The Roundcube Dev Team |
  8. | |
  9. | Licensed under the GNU General Public License version 3 or |
  10. | any later version with exceptions for skins & plugins. |
  11. | See the README file for a full license statement. |
  12. | |
  13. | PURPOSE: |
  14. | Handles contact photo uploads |
  15. | |
  16. +-----------------------------------------------------------------------+
  17. | Author: Thomas Bruederli <roundcube@gmail.com> |
  18. +-----------------------------------------------------------------------+
  19. */
  20. // Supported image format types
  21. // ImageMagick works with other non-image types (e.g.pdf) we don't want here
  22. $IMAGE_TYPES = explode(',', 'jpeg,jpg,jp2,tiff,tif,bmp,eps,gif,png,png8,png24,png32,svg,ico');
  23. // clear all stored output properties (like scripts and env vars)
  24. $OUTPUT->reset();
  25. if ($filepath = $_FILES['_photo']['tmp_name']) {
  26. // check file type and resize image
  27. $image = new rcube_image($_FILES['_photo']['tmp_name']);
  28. $imageprop = $image->props();
  29. if (in_array(strtolower($imageprop['type']), $IMAGE_TYPES)
  30. && $imageprop['width'] && $imageprop['height']
  31. ) {
  32. $maxsize = intval($RCMAIL->config->get('contact_photo_size', 160));
  33. $tmpfname = tempnam($RCMAIL->config->get('temp_dir'), 'rcmImgConvert');
  34. $save_hook = 'attachment_upload';
  35. // scale image to a maximum size
  36. if (($imageprop['width'] > $maxsize || $imageprop['height'] > $maxsize) && $image->resize($maxsize, $tmpfname)) {
  37. $filepath = $tmpfname;
  38. $save_hook = 'attachment_save';
  39. }
  40. // save uploaded file in storage backend
  41. $attachment = $RCMAIL->plugins->exec_hook($save_hook, array(
  42. 'path' => $filepath,
  43. 'size' => $_FILES['_photo']['size'],
  44. 'name' => $_FILES['_photo']['name'],
  45. 'mimetype' => 'image/' . $imageprop['type'],
  46. 'group' => 'contact',
  47. ));
  48. }
  49. else {
  50. $attachment['error'] = $RCMAIL->gettext('invalidimageformat');
  51. }
  52. if ($attachment['status'] && !$attachment['abort']) {
  53. $file_id = $attachment['id'];
  54. $_SESSION['contacts']['files'][$file_id] = $attachment;
  55. $OUTPUT->command('replace_contact_photo', $file_id);
  56. }
  57. else { // upload failed
  58. $err = $_FILES['_photo']['error'];
  59. $size = $RCMAIL->show_bytes(parse_bytes(ini_get('upload_max_filesize')));
  60. if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE)
  61. $msg = $RCMAIL->gettext(array('name' => 'filesizeerror', 'vars' => array('size' => $size)));
  62. else if ($attachment['error'])
  63. $msg = $attachment['error'];
  64. else
  65. $msg = $RCMAIL->gettext('fileuploaderror');
  66. $OUTPUT->command('display_message', $msg, 'error');
  67. }
  68. }
  69. else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  70. // if filesize exceeds post_max_size then $_FILES array is empty,
  71. // show filesizeerror instead of fileuploaderror
  72. if ($maxsize = ini_get('post_max_size'))
  73. $msg = $RCMAIL->gettext(array('name' => 'filesizeerror', 'vars' => array('size' => $RCMAIL->show_bytes(parse_bytes($maxsize)))));
  74. else
  75. $msg = $RCMAIL->gettext('fileuploaderror');
  76. $OUTPUT->command('display_message', $msg, 'error');
  77. }
  78. $OUTPUT->command('photo_upload_end');
  79. $OUTPUT->send('iframe');