]> git.donarmstrong.com Git - roundcube.git/blob - program/steps/addressbook/upload_photo.inc
bd9b8399328c7a8bb7f64849771410373a6d9944
[roundcube.git] / program / steps / addressbook / upload_photo.inc
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/steps/addressbook/upload_photo.inc                            |
6  |                                                                       |
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                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |   Handles contact photo uploads                                       |
13  |                                                                       |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: upload_photo.inc 5130 2011-08-25 08:30:01Z alec $
19
20 */
21
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'); 
25
26 // clear all stored output properties (like scripts and env vars)
27 $OUTPUT->reset();
28
29 if ($filepath = $_FILES['_photo']['tmp_name']) {
30     // check file type and resize image
31     $imageprop = rcmail::imageprops($_FILES['_photo']['tmp_name']);
32
33     if (in_array(strtolower($imageprop['type']), $IMAGE_TYPES)
34         && $imageprop['width'] && $imageprop['height']
35     ) {
36         $maxsize = intval($RCMAIL->config->get('contact_photo_size', 160));
37         $tmpfname = tempnam($RCMAIL->config->get('temp_dir'), 'rcmImgConvert');
38         $save_hook = 'attachment_upload';
39
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';
46         }
47
48         // save uploaded file in storage backend
49         $attachment = $RCMAIL->plugins->exec_hook($save_hook, array(
50             'path' => $filepath,
51             'size' => $_FILES['_photo']['size'],
52             'name' => $_FILES['_photo']['name'],
53             'mimetype' => 'image/' . $imageprop['type'],
54             'group' => 'contact',
55         ));
56     }
57     else
58         $attachment['error'] = rcube_label('invalidimageformat');
59
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);
64     }
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'];
71         else
72             $msg = rcube_label('fileuploaderror');
73             
74         $OUTPUT->command('display_message', $msg, 'error');
75     }
76 }
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)))));
82     else
83         $msg = rcube_label('fileuploaderror');
84
85     $OUTPUT->command('display_message', $msg, 'error');
86 }
87
88 $OUTPUT->command('photo_upload_end');
89 $OUTPUT->send('iframe');