source: branches/2.2/workflow/inc/class.UserPictureProvider.inc.php @ 3167

Revision 3167, 4.9 KB checked in by viani, 14 years ago (diff)

Ticket #1135 - Merged r1990:3166 from /trunk/workflow into /branches/2.2/workflow

  • Property svn:executable set to *
Line 
1<?php
2/**************************************************************************\
3* eGroupWare                                                               *
4* http://www.egroupware.org                                                *
5* --------------------------------------------                             *
6*  This program is free software; you can redistribute it and/or modify it *
7*  under the terms of the GNU General Public License as published by the   *
8*  Free Software Foundation; either version 2 of the License, or (at your  *
9*  option) any later version.                                              *
10\**************************************************************************/
11
12require_once 'common.inc.php';
13
14/**
15 * Classe que provê as fotos dos usuários do LDAP
16 * @author Sidnei Augusto Drovetto Jr. - drovetto@gmail.com
17 * @version 1.0
18 * @package Workflow
19 * @license http://www.gnu.org/copyleft/gpl.html GPL
20 */
21class UserPictureProvider
22{
23        /**
24         * @var int $userID O ID do usuário cuja foto será exibida
25         * @access private
26         */
27        private $userID = false;
28
29        /**
30        * @var int $cacheDays O número de dias antes da imagem ser renovada
31        * @access private
32        */
33        private $cacheDays = 5;
34
35        /**
36         * Construtor da classe
37         * @return object
38         * @access public
39         */
40        public function UserPictureProvider()
41        {
42                if (isset($_GET['userID']))
43                        if (is_numeric($_GET['userID']))
44                                $this->userID = (int) $_GET['userID'];
45        }
46
47        /**
48         * Redimensiona uma imagem de acordo com o tamanho padrão (hardcoded 60x80)
49         * @param resource $image Um resource do tipo image (da imagem de entrada)
50         * @return resource Um resource do tipo image (da imagem redimensionada)
51         * @access private
52         */
53        private function resizeImage($image)
54        {
55                /* thumbnail dimensions */
56                $thumbWidth = 60;
57                $thumbHeight = 80;
58                $thumbRatio = $thumbWidth/$thumbHeight;
59
60                /* get the image dimensions */
61                $width = imagesx($image);
62                $height = imagesy($image);
63                $ratio = $width/$height;
64
65                /* resize the image according to the ratio */
66                if ($ratio > $thumbRatio)
67                {
68                        $newWidth = $thumbWidth;
69                        $newHeight = $height * ($newWidth/$width);
70                }
71                else
72                {
73                        $newHeight = $thumbHeight;
74                        $newWidth = $width * ($newHeight/$height);
75                }
76
77                /* create the new image */
78                $output = imagecreatetruecolor($newWidth, $newHeight);
79                imagecopyresampled($output, $image, 0, 0, 0, 0,$newWidth, $newHeight, $width, $height);
80
81                return $output;
82        }
83
84        /**
85         * Serve a imagem do usuário
86         * @return void
87         * @access public
88         */
89        public function serve()
90        {
91                if ($this->userID === false)
92                        return;
93
94                /* encontra o diretório onde os arquivos estão armazenados */
95                $baseDirectory = '';
96                if (isset($_SESSION['workflow']['ResourcesRedirector']['baseDirectory']))
97                {
98                        $baseDirectory = $_SESSION['workflow']['ResourcesRedirector']['baseDirectory'];
99                }
100                else
101                {
102                        if (isset($_SESSION['phpgw_info']['workflow']['vfs_basedir']))
103                        {
104                                $baseDirectory = $_SESSION['phpgw_info']['workflow']['vfs_basedir'];
105                        }
106                        else
107                        {
108                                if (isset($_SESSION['phpgw_info']['expressomail']['server']['files_dir']))
109                                {
110                                        $baseDirectory = $_SESSION['phpgw_info']['expressomail']['server']['files_dir'];
111                                }
112                                else
113                                {
114                                        /* em último caso, tenta buscar a informação em banco de dados */
115                                        /* prepara o ambiente para a carga de informações sobre o banco de dados */
116                                        $result = Factory::getInstance('WorkflowObjects')->getDBExpresso()->Link_ID->query('SELECT config_value FROM phpgw_config WHERE config_app = ? AND config_name = ?', array('phpgwapi', 'files_dir'));
117                                        if (empty($result))
118                                                return;
119                                        $fields = $result->fetchRow();
120                                        $baseDirectory = $fields['config_value'];
121                                }
122                        }
123                        $_SESSION['workflow']['ResourcesRedirector']['baseDirectory'] = $baseDirectory;
124                }
125
126                $baseDirectory = str_replace('//', '/', $baseDirectory . '/workflow/workflow/ldapPictures');
127                $filename =  $baseDirectory . '/' . $this->userID;
128
129                $createPictureFile = true;
130                if (file_exists($filename))
131                {
132                        if ((filemtime($filename) + ($this->cacheDays * 24 * 60 * 60)) > mktime())
133                                $createPictureFile = false;
134                        else
135                                unlink($filename);
136                }
137
138                if ($createPictureFile)
139                {
140                        if (!is_dir($baseDirectory))
141                                mkdir($baseDirectory, 0770, true);
142
143                        $contents = Factory::getInstance('WorkflowLDAP')->getUserPicture($this->userID);
144                        $success = true;
145                        if ($contents !== false)
146                        {
147                                $image = imagecreatefromstring($contents);
148                                $image = $this->resizeImage($image);
149                                $success = @imagepng($image, $filename);
150                        }
151
152                        if (($contents === false) || ($success === false))
153                        {
154                                $filename = $baseDirectory . '/default.png';
155                                if (!file_exists($filename))
156                                {
157                                        $image = imagecreatefrompng(dirname(__FILE__) . '/../../expressoMail1_2/templates/default/images/photo.png');
158                                        $image = $this->resizeImage($image);
159                                        $success = @imagepng($image, $filename);
160                                }
161
162                                if ($success === false)
163                                        return false;
164                        }
165                }
166
167                Factory::getInstance('ResourcesRedirector')->show($filename);
168        }
169}
170?>
Note: See TracBrowser for help on using the repository browser.