4 +-----------------------------------------------------------------------+
5 | program/steps/addressbook/upload_photo.inc |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2005-2011, The Roundcube Dev Team |
9 | Licensed under the GNU GPL |
12 | Handles contact photo uploads |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: upload_photo.inc 5110 2011-08-22 14:33:02Z alec $
22 // Supported image format types
23 // ImageMagick works with other non-image types (e.g.pdf) we don't want here
24 $IMAGE_TYPES = explode(',', 'jpeg,jpg,jp2,tiff,tif,bmp,eps,gif,png,png8,png24,png32,svg,ico');
26 // clear all stored output properties (like scripts and env vars)
29 if ($filepath = $_FILES['_photo']['tmp_name']) {
30 // check file type and resize image
31 $imageprop = rcmail::imageprops($_FILES['_photo']['tmp_name']);
33 if (in_array(strtolower($imageprop['type']), $IMAGE_TYPES)
34 && $imageprop['width'] && $imageprop['height']
36 $maxsize = intval($RCMAIL->config->get('contact_photo_size', 160));
37 $tmpfname = tempnam($RCMAIL->config->get('temp_dir'), 'rcmImgConvert');
38 $save_hook = 'attachment_upload';
40 // scale image to a maximum size
41 if (($imageprop['width'] > $maxsize || $imageprop['height'] > $maxsize) &&
42 (rcmail::imageconvert(array('in' => $filepath, 'out' => $tmpfname,
43 'size' => $maxsize.'x'.$maxsize, 'type' => $imageprop['type'])) !== false)) {
44 $filepath = $tmpfname;
45 $save_hook = 'attachment_save';
48 // save uploaded file in storage backend
49 $attachment = $RCMAIL->plugins->exec_hook($save_hook, array(
51 'size' => $_FILES['_photo']['size'],
52 'name' => $_FILES['_photo']['name'],
53 'mimetype' => 'image/' . $imageprop['type'],
58 $attachment['error'] = rcube_label('invalidimageformat');
60 if ($attachment['status'] && !$attachment['abort']) {
61 $file_id = $attachment['id'];
62 $_SESSION['contacts']['files'][$file_id] = $attachment;
63 $OUTPUT->command('replace_contact_photo', $file_id);
65 else { // upload failed
66 $err = $_FILES['_photo']['error'];
67 if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE)
68 $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
69 else if ($attachment['error'])
70 $msg = $attachment['error'];
72 $msg = rcube_label('fileuploaderror');
74 $OUTPUT->command('display_message', $msg, 'error');
77 else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
78 // if filesize exceeds post_max_size then $_FILES array is empty,
79 // show filesizeerror instead of fileuploaderror
80 if ($maxsize = ini_get('post_max_size'))
81 $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes($maxsize)))));
83 $msg = rcube_label('fileuploaderror');
85 $OUTPUT->command('display_message', $msg, 'error');
88 $OUTPUT->command('photo_upload_end');
89 $OUTPUT->send('iframe');