source: branches/2.2/filemanager/inc/upload.php @ 3879

Revision 3879, 7.5 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #1648 - Correcao( codigo ) e melhoria( laytou/informacao ) no upload de arquivos para o servidor.

Line 
1<?php
2
3require_once '../../header.session.inc.php';
4
5/* This single file is used to increase upload_max_filesize and post_max_size using .htaccess*/
6if(!isset($GLOBALS['phpgw_info'])){
7        $GLOBALS['phpgw_info']['flags'] = array(
8                'currentapp' => 'filemanager',
9                'nonavbar'   => true,
10                'noheader'   => true
11        );
12}
13require_once '../../header.inc.php';
14
15$bo = CreateObject('filemanager.bofilemanager');
16$c      = CreateObject('phpgwapi.config','filemanager');
17$c->read_repository();
18
19$current_config                 = $c->config_data;
20$upload_max_size                = $current_config['filemanager_Max_file_size'];
21$path                                   = $_POST['path'];
22$notifUser                              = $_POST['notifTo'];
23$show_upload_boxes      = count($_FILES['upload_file']['name']);
24$filesUpload                            = $_FILES['upload_file'];
25
26
27error_log( print_r($_FILES['upload_file'], true), 3, "/tmp/log" );
28
29function create_summaryImage($file)
30{
31        list($width, $height,$image_type) = getimagesize($file);
32
33        switch($image_type)
34        {
35                case 1:
36                        $image_big = imagecreatefromgif($file);
37                        break;
38                case 2:
39                        $image_big = imagecreatefromjpeg($file);
40                        break;
41                case 3:
42                        $image_big = imagecreatefrompng($file);
43                        break;
44                default:
45                        return false;
46        }
47
48        $max_resolution = 48;
49
50        if ($width > $height)
51        {
52                $new_width = $max_resolution;
53                $new_height = $height*($new_width/$width);
54        }
55        else
56        {
57                $new_height = $max_resolution;
58                $new_width = $width*($new_height/$height);
59        }
60       
61        $image_new = imagecreatetruecolor($new_width, $new_height);
62        imagecopyresampled($image_new, $image_big, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
63        ob_start();
64        imagepng($image_new);
65        $content = ob_get_clean();
66        return $content;
67}
68
69/* Its much faster test all files only one time */
70if(strlen($current_config['filemanager_antivirus_command']) > 0)
71{
72        $command = "nice -n19 ".$current_config['filemanager_antivirus_command'];
73        for($i = 0; $i != $show_upload_boxes; $i++)
74        {
75                $command .= " ".$_FILES['upload_file']['tmp_name'][$i];
76        }
77       
78        $return = 0;
79       
80        exec("bash -c ".escapeshellcmd(escapeshellarg($command)),$output,$return);
81       
82        if ($return == 1)
83        {
84                $_SESSION['response'] = serialize(array(0 => lang('Error:').lang('One of the files sent was considered infected')));
85                return;
86        }
87}
88
89if( $path != '/' )
90{
91        for( $i = 0; $i != $show_upload_boxes; $i++)
92        {
93                if ( $_FILES['upload_file']['size'][$i] > ($upload_max_size*1024*1024) )
94                {
95                        $return[] = array(
96                                                                "file"          => $_FILES['upload_file']['name'][$i] ,
97                                                                "size"          => $_FILES['upload_file']['size'][$i] ,
98                                                                "size_max"      => ($upload_max_size*1024*1024)
99                         );
100                        continue;
101                }
102                elseif( $_FILES['upload_file']['size'][$i] > 0 )
103                {
104                        if( $badchar = $bo->bad_chars($_FILES['upload_file']['name'][$i], True, True ))
105                        {
106                                $return[] = array(
107                                                                "file"          => $_FILES['upload_file']['name'][$i],
108                                                                "badchar"       => lang('File names cannot contain "%1"', $badchar)
109                                );                                     
110                                continue;
111                        }
112
113                        # Check to see if the file exists in the database, and get its info at the same time
114                        $ls_array = $bo->vfs->ls(array(
115                                                'string'=> $path . '/' . $_FILES['upload_file']['name'][$i],
116                                                'relatives'     => array(RELATIVE_NONE),
117                                                'checksubdirs'  => False,
118                                                'nofiles'       => True
119                        ));
120
121                        $fileinfo = $ls_array[0];
122
123                        if( $fileinfo['name'] )
124                        {
125                                if( $fileinfo['mime_type'] == 'Directory' )
126                                {
127                                        $return[] = array(
128                                                                                "file"          => $_FILES['upload_file']['name'][$i],
129                                                                                "directory"     => lang('Cannot replace %1 because it is a directory', $fileinfo['name'] )
130                                        );
131                                        continue;
132                                }
133                        }
134
135                        if($fileinfo['name'] && $fileinfo['deleteable'] != 'N')
136                        {
137                                $_FILES['upload_file']['name'][$i] = date('Ymd-H:i')."-".$_FILES['upload_file']['name'][$i];
138                                $tmp_arr=array(
139                                        'from'  => $_FILES['upload_file']['tmp_name'][$i],
140                                        'to'    => $_FILES['upload_file']['name'][$i],
141                                        'relatives'     => array(RELATIVE_NONE|VFS_REAL, RELATIVE_ALL)
142
143                                );
144                                $bo->vfs->cp($tmp_arr);
145                                $tmp_arr=array(
146                                                'string'=> $_FILES['upload_file']['name'][$i],
147                                                'relatives'     => array(RELATIVE_ALL),
148                                                'attributes'    => array(
149                                                        'owner_id' => $bo->userinfo['username'],
150                                                        'modifiedby_id' => $bo->userinfo['username'],
151                                                        'size' => $_FILES['upload_file']['size'][$i],
152                                                        'mime_type' => $_FILES['upload_file']['type'][$i],
153                                                        'deleteable' => 'Y',
154                                                        'comment' => stripslashes($_POST['upload_comment'][$i])
155                                        )
156                                );
157                                $bo->vfs->set_attributes($tmp_arr);
158
159                                $return[] = array(
160                                                                "file"          => $_FILES['upload_file']['name'][$i],                                                                 
161                                                                "undefined"     => lang( "There is a file %1, that was not replaced", $_FILES['upload_file']['name'][$i] )
162                                );
163                        }
164                        else
165                        {
166                                if ($bo->vfs->cp(array(
167                                        'from'=> $_FILES['upload_file']['tmp_name'][$i],
168                                        'to'=> $_FILES['upload_file']['name'][$i],
169                                        'relatives'     => array(RELATIVE_NONE|VFS_REAL, RELATIVE_ALL)
170                                )))
171                                {
172                                        $bo->vfs->set_attributes(array(
173                                                        'string'                => $_FILES['upload_file']['name'][$i],
174                                                        'relatives'     => array(RELATIVE_ALL),
175                                                        'attributes'    => array(
176                                                                'mime_type'     => $_FILES['upload_file']['type'][$i],
177                                                                'comment'       => stripslashes($_POST['upload_comment'][$i])
178                                                        )
179                                        ));
180                                }
181                                else
182                                {
183                                        $return[] = array (
184                                                                                "file"          => $_FILES['upload_file']['name'][$i],
185                                                                                "sendFile"      => lang('It was not possible to send your file')
186                                        );
187                                }
188                        }
189                }
190                elseif( $_FILES['upload_file']['name'][$i] )
191                {
192                        $bo->vfs->touch(array(
193                                'string'=> $_FILES['upload_file']['name'][$i],
194                                'relatives'     => array(RELATIVE_ALL)
195                        ));
196
197                        $bo->vfs->set_attributes(array(
198                                        'string'                => $_FILES['upload_file']['name'][$i],
199                                        'relatives'     => array(RELATIVE_ALL),
200                                        'attributes'    => array(
201                                                                                'mime_type'     => $_FILES['upload_file']['type'][$i],
202                                                                                'comment'       => stripslashes($_POST['upload_comment'][$i])
203                                        )
204                        ));
205                }
206
207                if ( !(strpos(strtoupper($_FILES['upload_file']['type'][$i]),'IMAGE') === FALSE ) )
208                {
209                        $content = create_summaryImage($_FILES['upload_file']['tmp_name'][$i]);
210                        if ($content)
211                        {
212                                $bo->vfs->set_summary(array(
213                                        'string'=> $_FILES['upload_file']['name'][$i],
214                                        'relatives' => array(RELATIVE_ALL),
215                                        'summary'=> $content
216                                ));
217                        }
218
219                }
220        }
221}
222
223if( count($notifUser) > 0 )
224{
225        define('PHPGW_INCLUDE_ROOT','../../');
226        define('PHPGW_API_INC','../../phpgwapi/inc');
227        include_once(PHPGW_API_INC.'/class.phpmailer.inc.php');
228        $mail = new PHPMailer();
229        $mail->IsSMTP();
230        $boemailadmin = CreateObject('emailadmin.bo');
231        $emailadmin_profile = $boemailadmin->getProfileList();
232        $emailadmin = $boemailadmin->getProfile($emailadmin_profile[0]['profileID']);
233        $mail->Host = $emailadmin['smtpServer'];
234        $mail->Port = $emailadmin['smtpPort'];
235        $mail->From = $GLOBALS['phpgw']->preferences->values['email'];
236        $mail->FromName = $GLOBALS['phpgw_info']['user']['fullname'];
237        $mail->IsHTML(true);
238       
239        foreach( $notifUser as $userMail )
240        {
241                $mail->AddAddress($userMail);
242                $mail->Subject = lang("Filemanager notification");
243               
244                $body  = "<div style='font-size: 9pt !important;'>";
245                $body .= lang("The user %1 sent the following files", "<span style='font-weight: bold;'>" . $GLOBALS['phpgw_info']['user']['fullname'] . "</span>") . "<br/><br/>";
246
247                foreach( $filesUpload['name'] as $key => $name )
248                        $body .= "<div style='font-weight: bold;'> - " . $name ." ( " . $filesUpload['type'][$key] . " )</div>";
249
250                $body  .= "<div style='margin-top:25px;'>".lang("To view the files %1", "<a href='../filemanager/index.php'>".lang("Click here")."</a>")."</div>";
251                $body  .= "</div>";
252               
253                $mail->Body = $body;
254               
255                if( !$mail->Send() )
256                {
257                        $return[] = $mail->ErrorInfo;
258                }
259        }
260       
261        unset( $filesUpload );
262       
263}
264
265$_SESSION['response'] = ( count($return) > 0 ) ? serialize($return) : serialize( array( 0 => 'Ok' ) );
266
267?>
Note: See TracBrowser for help on using the repository browser.