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

Revision 3889, 8.1 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #1660 - Enviar arquivos pelo filemanager, tratado o erro para o php.

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