source: subversion/trunk/roundcubemail/program/steps/addressbook/upload_photo.inc @ 4999

Last change on this file since 4999 was 4999, checked in by alec, 22 months ago
  • Fixed contact photos upload, moved code to separate file for better performance

(to handle upload we don't need to initialize addressbook source)

  • Property svn:keywords set to Id Date Author
File size: 3.5 KB
Line 
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$
19
20*/
21
22// clear all stored output properties (like scripts and env vars)
23$OUTPUT->reset();
24console($_FILES);
25if ($filepath = $_FILES['_photo']['tmp_name']) {
26    // check file type and resize image
27    $imageprop = rcmail::imageprops($_FILES['_photo']['tmp_name']);
28
29    if ($imageprop['width'] && $imageprop['height']) {
30        $maxsize = intval($RCMAIL->config->get('contact_photo_size', 160));
31        $tmpfname = tempnam($RCMAIL->config->get('temp_dir'), 'rcmImgConvert');
32        $save_hook = 'attachment_upload';
33
34        // scale image to a maximum size
35        if (($imageprop['width'] > $maxsize || $imageprop['height'] > $maxsize) &&
36            (rcmail::imageconvert(array('in' => $filepath, 'out' => $tmpfname,
37                'size' => $maxsize.'x'.$maxsize, 'type' => $imageprop['type'])) !== false)) {
38            $filepath = $tmpfname;
39            $save_hook = 'attachment_save';
40        }
41
42        // save uploaded file in storage backend
43        $attachment = $RCMAIL->plugins->exec_hook($save_hook, array(
44            'path' => $filepath,
45            'size' => $_FILES['_photo']['size'],
46            'name' => $_FILES['_photo']['name'],
47            'mimetype' => 'image/' . $imageprop['type'],
48            'group' => 'contact',
49        ));
50    }
51    else
52        $attachment['error'] = rcube_label('invalidimageformat');
53
54    if ($attachment['status'] && !$attachment['abort']) {
55        $file_id = $attachment['id'];
56        $_SESSION['contacts']['files'][$file_id] = $attachment;
57        $OUTPUT->command('replace_contact_photo', $file_id);
58    }
59    else {  // upload failed
60        $err = $_FILES['_photo']['error'];
61        if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE)
62            $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))));
63        else if ($attachment['error'])
64            $msg = $attachment['error'];
65        else
66            $msg = rcube_label('fileuploaderror');
67           
68        $OUTPUT->command('display_message', $msg, 'error');
69    }
70}
71else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
72    // if filesize exceeds post_max_size then $_FILES array is empty,
73    // show filesizeerror instead of fileuploaderror
74    if ($maxsize = ini_get('post_max_size'))
75        $msg = rcube_label(array('name' => 'filesizeerror', 'vars' => array('size' => show_bytes(parse_bytes($maxsize)))));
76    else
77        $msg = rcube_label('fileuploaderror');
78
79    $OUTPUT->command('display_message', $msg, 'error');
80}
81
82$OUTPUT->command('photo_upload_end');
83$OUTPUT->send('iframe');
Note: See TracBrowser for help on using the repository browser.