source: branches/2.2/filemanager/inc/class.vfs_functions.inc.php @ 5059

Revision 5059, 12.3 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #2263 - Corrigido total usado dentro do filemanager (não estava contabilizando corretamente)

Line 
1<?php
2        /**************************************************************************\
3        * -------------------------------------------------------------------------*
4        * This library is free software; you can redistribute it and/or modify it  *
5        * under the terms of the GNU Lesser General Public License as published by *
6        * the Free Software Foundation; either version 2.1 of the License,         *
7        * or any later version.                                                    *
8        * This library is distributed in the hope that it will be useful, but      *
9        * WITHOUT ANY WARRANTY; without even the implied warranty of               *
10        * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
11        * See the GNU Lesser General Public License for more details.              *
12        * You should have received a copy of the GNU Lesser General Public License *
13        * along with this library; if not, write to the Free Software Foundation,  *
14        * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
15        \**************************************************************************/
16
17        /* $Id: class.vfs_functions.inc.php 2009-11-11 amuller $ */
18
19        class vfs_functions
20        {
21                var $public_functions = array(
22                        'touch' => True,
23                        'download'=>True,
24                        'setRestricted'=>True,
25                        'editComment'=> True,
26                        'rename'=>True,
27                        'delete'=>True,
28                        'archive'=>True,
29                        'unarchive'=>True,
30                        'copyto'=>True,
31                        'moveto'=>True,
32                        'summary' => True
33                );
34
35                //keep
36                var $bo;
37                var $dispath;
38                var $cwd;
39                var $lesspath;
40                var $dispsep;
41
42                var $target;
43
44                var $prefs;//array
45
46                var $current_config;
47                // this ones must be checked thorougly;
48                var $fileman = Array();
49                //var $fileman;
50                var $path;
51                var $file;
52                var $debug = false;
53                var $now;
54
55                function vfs_functions()
56                {
57                        $this->now = date('Y-m-d H:i:s');
58
59                        $this->bo = CreateObject('filemanager.bofilemanager');
60
61                        $c = CreateObject('phpgwapi.config','filemanager');
62                        $c->read_repository();
63                        $this->current_config = $c->config_data;
64
65
66                        // here local vars are created from the HTTP vars
67                        @reset($GLOBALS['HTTP_POST_VARS']);
68                        while( list($name,) = @each($GLOBALS['HTTP_POST_VARS']) )
69                        {
70                                $this->$name = base64_decode($GLOBALS['HTTP_POST_VARS'][$name]);
71                        }
72
73                        @reset($GLOBALS['HTTP_GET_VARS']);
74                        while(list($name,) = @each($GLOBALS['HTTP_GET_VARS']) )
75                        {
76                                $$name = $GLOBALS['HTTP_GET_VARS'][$name];
77                                $this->$name = $GLOBALS['HTTP_GET_VARS'][$name];
78                        }
79
80                        $to_decode = array
81                        (
82                                'op'            => array('op' => ''),
83                                'path'  => array('path' => ''),
84                                'file'          => array('file' => ''),
85                                'sortby'        => array('sortby' => ''),
86                                'messages'      => array('messages'     => ''),
87                                'comment'       => array('comment' => ''),
88                                'from'  => array('from' => ''),
89                                'fileman'       => array('fileman' => ''),
90                                'to'    => array('to' => '')
91                        );
92
93                        reset($to_decode);
94                        while(list($var, $conditions) = each($to_decode))
95                        {
96                                while(list($condvar, $condvalue) = each($conditions))
97                                {
98                                        if(isset($$condvar) && ($condvar == $var || $$condvar == $condvalue))
99                                        {
100                                                if(is_array($$var))
101                                                {
102                                                        $temp = array();
103                                                        while(list($varkey, $varvalue) = each($$var))
104                                                        {
105                                                                if(is_int($varkey))
106                                                                {
107                                                                        $temp[$varkey] = stripslashes(base64_decode($varvalue));
108                                                                }
109                                                                else
110                                                                {
111                                                                        $temp[stripslashes(base64_decode($varkey))] = $varvalue;
112                                                                }
113                                                        }
114                                                        $this->$var = $temp;
115                                                }
116                                                elseif(isset($$var))
117                                                {
118                                                        $this->$var = stripslashes(base64_decode($$var));
119                                                }
120                                        }
121                                }
122                        }
123                       
124                        // get appl. and user prefs
125                        $pref = CreateObject('phpgwapi.preferences', $this->bo->userinfo['username']);
126                        $pref->read_repository();
127                        $pref->save_repository(True);
128                        $pref_array = $pref->read_repository();
129                        $this->prefs = $pref_array[$this->bo->appname];
130
131                        //always show name
132                        $this->prefs[name] =1;         
133                       
134                }
135               
136                function convertDateForm($pDate)
137                {
138                        /**
139                         * Recebe a data no formato      : aaaa-mm-dd
140                         * Retorna a data no formato : dd/mm/aaaa
141                         **/
142               
143                        $cDate = date_parse($pDate);
144                        $day   = ( strlen($cDate['day']) > 1 ) ? $cDate['day'] : "0".$cDate['day'];
145                        $month = ( strlen($cDate['month']) > 1 ) ? $cDate['month'] : "0".$cDate['month'];
146                        $year  = $cDate['year'];
147               
148                        return $day."/".$month."/".$year;
149                }
150               
151                // String format is YYYY-MM-DD HH:MM
152                function dateString2timeStamp($string)
153                {
154                        /**
155                         * Recebe a data no formato      : aaaa-mm-dd
156                         * Retorna a data no formato : dd/mm/aaaa
157                         **/
158               
159                        $cDate = date_parse($string);
160                        $day   = ( strlen($cDate['day']) > 1 ) ? $cDate['day'] : "0".$cDate['day'];
161                        $month = ( strlen($cDate['month']) > 1 ) ? $cDate['month'] : "0".$cDate['month'];
162                        $year  = $cDate['year'];
163               
164                        return $day."/".$month."/".$year;
165                }
166               
167                function verifyLock($file,$relative){
168                        $ls_array = $this->bo->vfs->ls(array(
169                                'string'        => $file,
170                                'relatives'     => array($relative),
171                                'checksubdirs'  => False,
172                                'nofiles'       => True
173                        ));
174                        $timestamp = $this->dateString2timeStamp($ls_array[0]['modified']);
175                        if (time() - $timestamp < 60 && $ls_array[0]['modifiedby_id'] != $GLOBALS['phpgw_info']['user']['account_id']) // recently than last minute: someone is editing
176                        {
177                                $this->messages[]=lang('Error:').lang('This file is being edited right now by:').$GLOBALS['phpgw']->accounts->id2name($ls_array[0]['modifiedby_id']);
178                                return False;
179
180                        }
181                        else
182                                return True;
183                }
184
185                function setRestricted()
186                {
187                        $GLOBALS['phpgw_info']['flags'] = array
188                                (
189                                        'currentapp'            => 'filemanager',
190                                        'noheader'              => True,
191                                        'nonavbar'              => True,
192                                        'nofooter'              => True,
193                                        'noappheader'   => True,
194                                        'enable_browser_class'  => True
195                                );
196
197                        if ($this->file)
198                        {
199                                $filename=$this->path.'/'.$this->file;
200                                if(!$this->verifyLock($filename,RELATIVE_NONE))
201                                {
202                                        echo "False";
203                                        return False;
204                                }
205                               
206                                $ls_array = $this->bo->vfs->ls(array(
207                                        'string'        => $filename,
208                                        'relatives'     => array(RELATIVE_NONE),
209                                        'checksubdirs'  => False,
210                                        'nofiles'       => True
211                                ));
212                                if (intval($ls_array[0]['type']) == 0)
213                                        $type = 1;
214                                else
215                                        $type = 0;
216
217                                if($this->bo->vfs->set_attributes (array(
218                                        'string'        => $filename,
219                                        'relatives'     => RELATIVE_NONE,
220                                        'attributes'    => array('type' => $type)
221                                        ))
222                                )
223                                {
224                                        echo "True|".$this->file;
225                                }
226                                else
227                                {
228                                        echo "False";
229                                }
230                        }
231
232                }
233                function touch(){
234                        if($this->file)
235                                if ($this->bo->vfs->touch(array('string'=> $this->file,'relatives' => array(RELATIVE_ALL))))
236                                {
237                                        echo "True";
238                                        return True;
239                                }
240                                else
241                                        return False;
242
243                }
244                function summary()
245                {
246                        header('Content-Type: image/png');
247                        $expires = 60*60*24*15;
248                        header("Cache-Control: maxage=".$expires);
249                        header("Pragma: public");
250                        header("Expires: ".gmdate('D, d M Y H:i:s', time()+$expires));
251                        if($this->file)
252                        {       
253                                $content = $this->bo->vfs->summary(array('path' => $this->path,
254                                        'string' => $this->file
255                                ));
256                                if (strlen($content) < 1)
257                                {
258                                        $filename = './filemanager/templates/default/images/error.png';
259                                        $handle = fopen($filename,'rb');
260                                        $content = fread($handle,filesize($filename));
261                                        fclose($handle);
262                                }
263                                echo $content;
264                        }
265                        $GLOBALS['phpgw']->common->phpgw_exit();
266                }
267       
268                function delete(){
269                        foreach($this->fileman as $filename)
270                        {
271                                if($this->verifyLock($filename,RELATIVE_ALL) && $this->bo->vfs->rm(array(
272                                        'string' => $this->path.'/'.$filename,
273                                        'relatives' => array (RELATIVE_NONE)
274                                )))
275                                {
276                                        echo $filename."|";
277                                }
278                                else
279                                {
280                                        echo "False|".$filename;
281                                        return False;
282                                }
283                        }
284                }
285                function archive(){
286                        foreach($this->fileman as $filename)
287                        {
288                                if(!$this->verifyLock($filename,RELATIVE_ALL))
289                                {
290                                        echo "locked|".$filename;
291                                        return False;
292                                }
293                                $command .= " ".escapeshellarg($filename);
294                        }
295                        $zipFileName=$GLOBALS['phpgw_info']['user']['account_lid'].date("Y-m-d,H:i:s").".zip";
296                        $zipFilePath=ini_get("session.save_path")."/".$zipFileName;
297                        $command = $zipFilePath.$command;
298
299                        if (strlen($this->pswd) > 0){
300                                $command = " -P ".(base64_decode($this->pswd) ^ $_SESSION['phpgw_info']['filemanager']['user']['sec_key'])." ".$command;
301                        }
302
303                        exec("cd ".$this->bo->vfs->basedir.$this->path.";".escapeshellcmd("nice -n19 zip -9 ".$command),$output,$return_var);
304                        exec("history -c"); // privacy is good, we dont want get passwords!
305                        if ($return_var > 1){
306                                echo "False|".$return_var;
307                                return false;
308                        }
309
310                        $this->bo->vfs->cp(array(
311                                'from'=> $zipFilePath,
312                                'to'=> $zipFileName,
313                                'relatives'     => array(RELATIVE_NONE|VFS_REAL, RELATIVE_ALL)
314                        ));
315                        $this->bo->vfs->set_attributes(array(
316                                'string'=> $zipFileName,
317                                'relatives'     => array(RELATIVE_ALL),
318                                'attributes'=> array(
319                                        'mime_type' => "application/zip"
320                                )
321                        ));
322                        exec("rm -f ".escapeshellcmd(escapeshellarg($zipFilePath)));
323                        $this->delete();
324                }
325                function unarchive(){
326                        $command = escapeshellarg($this->file);
327                        if (strlen($this->pswd) > 0){
328                                $command = " -P ".(base64_decode($this->pswd) ^ $_SESSION['phpgw_info']['filemanager']['user']['sec_key'])." ".$command;
329                        }
330
331                        exec("cd ".escapeshellarg($this->bo->vfs->basedir.$this->path).";".escapeshellcmd("nice -n19 unzip ".$command),$output, $return_var);
332                        exec("history -c"); // privacy is good, we dont want get passwords!
333
334                        if ($return_var == 9 || $return_var == 5 || $return_var == 82){
335                                echo "wpasswd|";
336                                return false;
337                        }else if($return_var > 1){
338                                echo "False|";
339                        }
340
341                        $this->fileman[] = $this->file;
342                        $this->delete();
343                        $this->bo->vfs->update_real(array(
344                                        'string'        => $this->path,
345                                        'relatives'     => array(RELATIVE_NONE)
346                                ));
347                }
348                function editComment()
349                {
350                                if($badchar = $this->bo->bad_chars($this->comment, False, True))
351                                {
352                                        echo "False|badchar|".$badchar;
353                                        return False;
354                                }
355
356                                if ($this->bo->vfs->set_attributes(array(
357                                        'string'        => $this->file,
358                                        'relatives'     => array(RELATIVE_ALL),
359                                        'attributes'    => array(
360                                        'comment' => stripslashes($this->comment)
361                                        )
362                                )))
363                                {
364                                        echo "True|".$this->file;
365                                        return True;
366                                }
367                }
368                # Handle Moving Files and Directories
369                function moveto()
370                {
371                        if(!$this->to)
372                        {
373                                echo "NODIR|";
374                                return;
375                        }
376                        else
377                        {
378                                while(list($num, $file) = each($this->fileman))
379                                {
380                                        if($this->bo->vfs->mv(array(
381                                                'from'  => $this->from . '/' . $file,
382                                                'to'    => $this->to . '/' . $file,
383                                                'relatives'     => array(RELATIVE_NONE, RELATIVE_NONE)
384                                        )))
385                                                $moved++;
386                                        else
387                                                $error++;
388                                }
389                        }
390                        if($error > 0){
391                                echo "SOMEERROR|".$error;
392                                return;
393                        }
394                        if($moved > 0)
395                                echo "MOVED|".$moved;
396                }
397
398                // Handle Copying of Files and Directories
399                function copyto()
400                {
401                        if(!$this->to)
402                        {
403                                echo "NODIR|";
404                                return;
405                        }
406                        else
407                        {
408                                while(list($num, $file) = each($this->fileman))
409                                {
410                                        if($this->bo->vfs->cp(array(
411                                                'from'  => $this->from . '/' . $file,
412                                                'to'    => $this->to . '/' . $file,
413                                                'relatives'     => array(RELATIVE_NONE, RELATIVE_NONE)
414                                        )))
415                                                $copied++;
416                                        else
417                                                $error++;
418                                }
419                        }
420                        if($error > 0){
421                                echo "SOMEERROR|".$error;
422                                return;
423                        }
424                        if($copied > 0)
425                                echo "COPIED|".$copied;
426                }
427
428                # Handle Renaming Files and Directories
429                function rename()
430                {
431                        $_return = array();
432                       
433                        if ( $this->file )
434                        {
435                                if( $badchar = $this->bo->bad_chars($this->to, True, True) )
436                                {
437                                        $_return[] = array( "error" => $badchar);       
438                                }
439                                if(ereg("/", $this->to) || ereg("\\\\", $this->to))
440                                {
441                                        $_return[] = array( "error"=> "slashes");
442                                }
443                                elseif(!$this->verifyLock($this->file,RELATIVE_CURRENT))
444                                {
445                                        $_return[] = array( "error" => "editing" );
446                                }
447                                elseif ($this->bo->vfs->mv(array(
448                                                'from'  => $this->path.'/'.$this->file,
449                                                'to'    => $this->path.'/'.$this->to,
450                                                'relatives' => array(RELATIVE_NONE,RELATIVE_NONE)
451                                )))
452                                {
453                                        $_return[] = array( "true" => lang('renamed %1 to %2', $this->file ,$this->to ) );
454                                       
455                                        // Get Type Mime Type
456                                        $mimeType = $this->bo->vfs->get_ext_mime_type(array ('string' => $this->to ));
457                                       
458                                        $this->bo->vfs->set_attributes( array(
459                                                                                                                        'string'                => $this->to,
460                                                                                                                        'relatives'     => array(RELATIVE_ALL),
461                                                                                                                        'attributes'    => array('mime_type' => $mimeType)
462                                         ));
463                                }
464                                else
465                                {
466                                        $_return[] = array( "error"     => $this->file . " " . $this->to );     
467                                }
468                        }
469                        else
470                        {
471                                $_return[] = array("error" => "whitout file ");
472                        }
473                       
474                        echo serialize( $_return );
475                }
476        }
Note: See TracBrowser for help on using the repository browser.