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

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