source: trunk/filemanager/inc/class.vfs_functions.inc.php @ 2000

Revision 2000, 11.2 KB checked in by amuller, 14 years ago (diff)

Ticket #597 - Implementação do módulo gerenciador de arquivos

  • Property svn:executable set to *
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 = $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
81                        $to_decode = array
82                        (
83                                'op'    => array('op' => ''),
84                                'path'  => array('path' => ''),
85                                'file'  => array('file' => ''),
86                                'sortby'        => array('sortby' => ''),
87                                'messages'      => array('messages'     => ''),
88                                'comment'       => array('comment' => ''),
89                                'from'  => array('from' => ''),
90                                'fileman' => array('fileman' => ''),
91                                'to'    => array('to' => '')
92                        );
93
94                        reset($to_decode);
95                        while(list($var, $conditions) = each($to_decode))
96                        {
97                                while(list($condvar, $condvalue) = each($conditions))
98                                {
99                                        if(isset($$condvar) && ($condvar == $var || $$condvar == $condvalue))
100                                        {
101                                                if(is_array($$var))
102                                                {
103                                                        $temp = array();
104                                                        while(list($varkey, $varvalue) = each($$var))
105                                                        {
106                                                                if(is_int($varkey))
107                                                                {
108                                                                        $temp[$varkey] = stripslashes(base64_decode($varvalue));
109                                                                }
110                                                                else
111                                                                {
112                                                                        $temp[stripslashes(base64_decode($varkey))] = $varvalue;
113                                                                }
114                                                        }
115                                                        $this->$var = $temp;
116                                                }
117                                                elseif(isset($$var))
118                                                {
119                                                        $this->$var = stripslashes(base64_decode($$var));
120                                                }
121                                        }
122                                }
123                        }
124
125                        // get appl. and user prefs
126                        $pref = CreateObject('phpgwapi.preferences', $this->bo->userinfo['username']);
127                        $pref->read_repository();
128                        $pref->save_repository(True);
129                        $pref_array = $pref->read_repository();
130                        $this->prefs = $pref_array[$this->bo->appname];
131
132                        //always show name
133
134                        $this->prefs[name] =1;         
135                       
136                }
137        // String format is YYYY-MM-DD HH:MM
138                function dateString2timeStamp($string){
139                        return mktime($string[11].$string[12],
140                                $string[14].$string[15],
141                                $string[17].$string[18],
142                                $string[5].$string[6],
143                                $string[8].$string[9],
144                                $string[0].$string[1].
145                                $string[2].$string[3]);
146                }
147                function verifyLock($file,$relative){
148                        $ls_array = $this->bo->vfs->ls(array(
149                                'string'        => $file,
150                                'relatives'     => array($relative),
151                                'checksubdirs'  => False,
152                                'nofiles'       => True
153                        ));
154                        $timestamp = $this->dateString2timeStamp($ls_array[0]['modified']);
155                        if (time() - $timestamp < 60 && $ls_array[0]['modifiedby_id'] != $GLOBALS['phpgw_info']['user']['account_id']) // recently than last minute: someone is editing
156                        {
157                                $this->messages[]=lang('Error:').lang('This file is being edited right now by:').$GLOBALS['phpgw']->accounts->id2name($ls_array[0]['modifiedby_id']);
158                                return False;
159
160                        }
161                        else
162                                return True;
163                }
164
165                function setRestricted(){
166                        $GLOBALS['phpgw_info']['flags'] = array
167                                (
168                                        'currentapp'    => 'filemanager',
169                                        'noheader'      => True,
170                                        'nonavbar' => True,
171                                        'nofooter'      => True,
172                                        'noappheader'   => True,
173                                        'enable_browser_class'  => True
174                                );
175
176                        if ($this->file)
177                        {
178                                $filename=$this->path.'/'.$this->file;
179                                if(!$this->verifyLock($filename,RELATIVE_NONE))
180                                {
181                                        echo "False";
182                                        return False;
183                                }
184                               
185                                $ls_array = $this->bo->vfs->ls(array(
186                                        'string'        => $filename,
187                                        'relatives'     => array(RELATIVE_NONE),
188                                        'checksubdirs'  => False,
189                                        'nofiles'       => True
190                                ));
191                                if (intval($ls_array[0]['type']) == 0)
192                                        $type = 1;
193                                else
194                                        $type = 0;
195
196                                if($this->bo->vfs->set_attributes (array(
197                                        'string'        => $filename,
198                                        'relatives'     => RELATIVE_NONE,
199                                        'attributes'    => array('type' => $type)
200                                        ))
201                                )
202                                {
203                                        echo "True|".$this->file;
204                                }
205                                else
206                                {
207                                        echo "False";
208                                }
209                        }
210
211                }
212                function touch(){
213                        if($this->file)
214                                if ($this->bo->vfs->touch(array('string'=> $this->file,'relatives' => array(RELATIVE_ALL))))
215                                {
216                                        echo "True";
217                                        return True;
218                                }
219                                else
220                                        return False;
221
222                }
223                function summary()
224                {
225                        header('Content-Type: image/png');
226                        $expires = 60*60*24*15;
227                        header("Cache-Control: maxage=".$expires);
228                        header("Pragma: public");
229                        header("Expires: ".gmdate('D, d M Y H:i:s', time()+$expires));
230                        if($this->file)
231                        {       
232                                $content = $this->bo->vfs->summary(array('path' => $this->path,
233                                        'string' => $this->file
234                                ));
235                                if (strlen($content) < 1)
236                                {
237                                        $filename = './filemanager/templates/default/images/error.png';
238                                        $handle = fopen($filename,'rb');
239                                        $content = fread($handle,filesize($filename));
240                                        fclose($handle);
241                                }
242                                echo $content;
243                        }
244                        $GLOBALS['phpgw']->common->phpgw_exit();
245                }
246       
247                function delete(){
248                        foreach($this->fileman as $filename)
249                        {
250                                if($this->verifyLock($filename,RELATIVE_ALL) && $this->bo->vfs->rm(array(
251                                        'string' => $this->path.'/'.$filename,
252                                        'relatives' => array (RELATIVE_NONE)
253                                )))
254                                {
255                                        echo $filename."|";
256                                }
257                                else
258                                {
259                                        echo "False|".$filename;
260                                        return False;
261                                }
262                        }
263                }
264                function archive(){
265                        foreach($this->fileman as $filename)
266                        {
267                                if(!$this->verifyLock($filename,RELATIVE_ALL))
268                                {
269                                        echo "locked|".$filename;
270                                        return False;
271                                }
272                                $command .= " ".escapeshellarg($filename);
273                        }
274                        $zipFileName=$GLOBALS['phpgw_info']['user']['account_lid'].date("Y-m-d,H:i:s").".zip";
275                        $zipFilePath=ini_get("session.save_path")."/".$zipFileName;
276                        $command = $zipFilePath.$command;
277
278                        if (strlen($this->pswd) > 0){
279                                $command = " -P ".(base64_decode($this->pswd) ^ $_SESSION['phpgw_info']['filemanager']['user']['sec_key'])." ".$command;
280                        }
281
282                        exec("cd ".$this->bo->vfs->basedir.$this->path.";".escapeshellcmd("nice -n19 zip -9 ".$command),$output,$return_var);
283                        exec("history -c"); // privacy is good, we dont want get passwords!
284                        if ($return_var > 1){
285                                echo "False|".$return_var;
286                                return false;
287                        }
288
289                        $this->bo->vfs->cp(array(
290                                'from'=> $zipFilePath,
291                                'to'=> $zipFileName,
292                                'relatives'     => array(RELATIVE_NONE|VFS_REAL, RELATIVE_ALL)
293                        ));
294                        $this->bo->vfs->set_attributes(array(
295                                'string'=> $zipFileName,
296                                'relatives'     => array(RELATIVE_ALL),
297                                'attributes'=> array(
298                                        'mime_type' => "application/zip"
299                                )
300                        ));
301                        exec("rm -f ".escapeshellcmd(escapeshellarg($zipFilePath)));
302                        $this->delete();
303                }
304                function unarchive(){
305                        $command = escapeshellarg($this->file);
306                        if (strlen($this->pswd) > 0){
307                                $command = " -P ".(base64_decode($this->pswd) ^ $_SESSION['phpgw_info']['filemanager']['user']['sec_key'])." ".$command;
308                        }
309
310                        exec("cd ".escapeshellarg($this->bo->vfs->basedir.$this->path).";".escapeshellcmd("nice -n19 unzip ".$command),$output, $return_var);
311                        exec("history -c"); // privacy is good, we dont want get passwords!
312
313                        if ($return_var == 9 || $return_var == 5 || $return_var == 82){
314                                echo "wpasswd|";
315                                return false;
316                        }else if($return_var > 1){
317                                echo "False|";
318                        }
319
320                        $this->fileman[] = $this->file;
321                        $this->delete();
322                        $this->bo->vfs->update_real(array(
323                                        'string'        => $this->path,
324                                        'relatives'     => array(RELATIVE_NONE)
325                                ));
326                }
327                function editComment()
328                {
329                                if($badchar = $this->bo->bad_chars($this->comment, False, True))
330                                {
331                                        echo "False|badchar|".$badchar;
332                                        return False;
333                                }
334
335                                if ($this->bo->vfs->set_attributes(array(
336                                        'string'        => $this->file,
337                                        'relatives'     => array(RELATIVE_ALL),
338                                        'attributes'    => array(
339                                        'comment' => stripslashes($this->comment)
340                                        )
341                                )))
342                                {
343                                        echo "True|".$this->file;
344                                        return True;
345                                }
346                }
347                # Handle Moving Files and Directories
348                function moveto()
349                {
350                        if(!$this->to)
351                        {
352                                echo "NODIR|";
353                                return;
354                        }
355                        else
356                        {
357                                while(list($num, $file) = each($this->fileman))
358                                {
359                                        if($this->bo->vfs->mv(array(
360                                                'from'  => $this->from . '/' . $file,
361                                                'to'    => $this->to . '/' . $file,
362                                                'relatives'     => array(RELATIVE_NONE, RELATIVE_NONE)
363                                        )))
364                                                $moved++;
365                                        else
366                                                $error++;
367                                }
368                        }
369                        if($error > 0){
370                                echo "SOMEERROR|".$error;
371                                return;
372                        }
373                        if($moved > 0)
374                                echo "MOVED|".$moved;
375                }
376
377                // Handle Copying of Files and Directories
378                function copyto()
379                {
380                        if(!$this->to)
381                        {
382                                echo "NODIR|";
383                                return;
384                        }
385                        else
386                        {
387                                while(list($num, $file) = each($this->fileman))
388                                {
389                                        if($this->bo->vfs->cp(array(
390                                                'from'  => $this->from . '/' . $file,
391                                                'to'    => $this->to . '/' . $file,
392                                                'relatives'     => array(RELATIVE_NONE, RELATIVE_NONE)
393                                        )))
394                                                $copied++;
395                                        else
396                                                $error++;
397                                }
398                        }
399                        if($error > 0){
400                                echo "SOMEERROR|".$error;
401                                return;
402                        }
403                        if($copied > 0)
404                                echo "COPIED|".$copied;
405                }
406
407                # Handle Renaming Files and Directories
408                function rename()
409                {
410                        if ($this->file)
411                        {
412                                if($badchar = $this->bo->bad_chars($this->to, True, True))
413                                {
414                                        echo "Error|badchar|".$badchar;
415                                        return false;
416                                }
417                                if(ereg("/", $this->to) || ereg("\\\\", $this->to))
418                                {
419                                        echo "Error|slashes";
420                                        return false;
421                                }
422                                elseif(!$this->verifyLock($this->file,RELATIVE_CURRENT))
423                                        echo "Error|editing";
424                                elseif ($this->bo->vfs->mv(array(
425                                        'from'  => $this->path.'/'.$this->file,
426                                        'to'    => $this->path.'/'.$this->to,
427                                        'relatives' => array(RELATIVE_NONE,RELATIVE_NONE)
428                                )))
429                                {
430                                        echo "True|".$this->file."|".$this->to;
431                                }
432                                else
433                                {
434                                        echo "Error|".$this->file."|".$this->to;
435                                }
436                        }
437                }
438
439        }
Note: See TracBrowser for help on using the repository browser.