source: sandbox/filemanager/inc/class.vfs_functions.inc.php @ 1865

Revision 1865, 9.8 KB checked in by amuller, 15 years ago (diff)

Ticket #597 - Melhoria do FM, Implementação do arquivamento

  • 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                        'summary' => True
31                );
32
33                //keep
34                var $bo;
35                var $dispath;
36                var $cwd;
37                var $lesspath;
38                var $dispsep;
39
40                var $target;
41
42                var $prefs;//array
43
44                var $current_config;
45                // this ones must be checked thorougly;
46                var $fileman = Array();
47                //var $fileman;
48                var $path;
49                var $file;
50                var $debug = false;
51                var $now;
52
53                function vfs_functions()
54                {
55                        $this->now = date('Y-m-d');
56
57                        $this->bo = CreateObject('filemanager.bofilemanager');
58
59                        $c = CreateObject('phpgwapi.config','filemanager');
60                        $c->read_repository();
61                        $this->current_config = $c->config_data;
62
63
64                        // here local vars are created from the HTTP vars
65                        @reset($GLOBALS['HTTP_POST_VARS']);
66                        while(list($name,) = @each($GLOBALS['HTTP_POST_VARS']))
67                        {
68                                $this->$name = $GLOBALS['HTTP_POST_VARS'][$name];
69                        }
70
71                        @reset($GLOBALS['HTTP_GET_VARS']);
72                        while(list($name,) = @each($GLOBALS['HTTP_GET_VARS']))
73                        {
74                                $$name = $GLOBALS['HTTP_GET_VARS'][$name];
75                                $this->$name = $GLOBALS['HTTP_GET_VARS'][$name];
76
77                        }
78
79                        $to_decode = array
80                        (
81                                'op'    => array('op' => ''),
82                                'path'  => array('path' => ''),
83                                'file'  => array('file' => ''),
84                                'sortby'        => array('sortby' => ''),
85                                'messages'      => array('messages'     => ''),
86                                'comment'       => array('comment' => ''),
87                                'to'    => array('to' => '')
88                        );
89
90                        reset($to_decode);
91                        while(list($var, $conditions) = each($to_decode))
92                        {
93                                while(list($condvar, $condvalue) = each($conditions))
94                                {
95                                        if(isset($$condvar) && ($condvar == $var || $$condvar == $condvalue))
96                                        {
97                                                if(is_array($$var))
98                                                {
99                                                        $temp = array();
100                                                        while(list($varkey, $varvalue) = each($$var))
101                                                        {
102                                                                if(is_int($varkey))
103                                                                {
104                                                                        $temp[$varkey] = stripslashes(base64_decode(urldecode(($varvalue))));
105                                                                }
106                                                                else
107                                                                {
108                                                                        $temp[stripslashes(base64_decode(urldecode(($varkey))))] = $varvalue;
109                                                                }
110                                                        }
111                                                        $this->$var = $temp;
112                                                }
113                                                elseif(isset($$var))
114                                                {
115                                                        $this->$var = stripslashes(base64_decode(urldecode($$var)));
116                                                }
117                                        }
118                                }
119                        }
120
121                        // get appl. and user prefs
122                        $pref = CreateObject('phpgwapi.preferences', $this->bo->userinfo['username']);
123                        $pref->read_repository();
124                        $pref->save_repository(True);
125                        $pref_array = $pref->read_repository();
126                        $this->prefs = $pref_array[$this->bo->appname];
127
128                        //always show name
129
130                        $this->prefs[name] =1;         
131                       
132                }
133        // String format is YYYY-MM-DD HH:MM
134                function dateString2timeStamp($string){
135                        return mktime($string[11].$string[12],
136                                $string[14].$string[15],
137                                $string[17].$string[18],
138                                $string[5].$string[6],
139                                $string[8].$string[9],
140                                $string[0].$string[1].
141                                $string[2].$string[3]);
142                }
143                function verifyLock($file,$relative){
144                        $ls_array = $this->bo->vfs->ls(array(
145                                'string'        => $file,
146                                'relatives'     => array($relative),
147                                'checksubdirs'  => False,
148                                'nofiles'       => True
149                        ));
150                        $timestamp = $this->dateString2timeStamp($ls_array[0]['modified']);
151                        if (time() - $timestamp < 60 && $ls_array[0]['modifiedby_id'] != $GLOBALS['phpgw_info']['user']['account_id']) // recently than last minute: someone is editing
152                        {
153                                $this->messages[]=lang('Error:').lang('This file is being edited right now by:').$GLOBALS['phpgw']->accounts->id2name($ls_array[0]['modifiedby_id']);
154                                return False;
155
156                        }
157                        else
158                                return True;
159                }
160
161                function setRestricted(){
162                        $GLOBALS['phpgw_info']['flags'] = array
163                                (
164                                        'currentapp'    => 'filemanager',
165                                        'noheader'      => True,
166                                        'nonavbar' => True,
167                                        'nofooter'      => True,
168                                        'noappheader'   => True,
169                                        'enable_browser_class'  => True
170                                );
171
172                        if ($this->file)
173                        {
174                                $filename=$this->path.'/'.$this->file;
175                                if(!$this->verifyLock($filename,RELATIVE_NONE))
176                                {
177                                        echo "False";
178                                        return False;
179                                }
180                               
181                                $ls_array = $this->bo->vfs->ls(array(
182                                        'string'        => $filename,
183                                        'relatives'     => array(RELATIVE_NONE),
184                                        'checksubdirs'  => False,
185                                        'nofiles'       => True
186                                ));
187                                if (intval($ls_array[0]['type']) == 0)
188                                        $type = 1;
189                                else
190                                        $type = 0;
191
192                                if($this->bo->vfs->set_attributes (array(
193                                        'string'        => $filename,
194                                        'relatives'     => RELATIVE_NONE,
195                                        'attributes'    => array('type' => $type)
196                                        ))
197                                )
198                                {
199                                        echo "True:".$this->file;
200                                }
201                                else
202                                {
203                                        echo "False";
204                                }
205                        }
206
207                }
208                function touch(){
209                        if($this->file)
210                                if ($this->bo->vfs->touch(array('string'=> $this->file,'relatives' => array(RELATIVE_ALL))))
211                                {
212                                        echo "True";
213                                        return True;
214                                }
215                                else
216                                        return False;
217
218                }
219                function summary()
220                {
221                        if($this->file)
222                        {       
223                                header('Content-Type: image/gif');
224                                $expires = 60*60*24*15; /* 15 days */
225                                header("Cache-Control: maxage=".$expires);
226                                header("Pragma: public");
227                                header("Expires: ".gmdate('D, d M Y H:i:s', time()+$expires));
228                                $content = $this->bo->vfs->summary(array('path' => $this->path,
229                                        'string' => $this->file
230                                ));
231                                if (strlen($content) > 0)
232                                        echo $content;
233                                else
234                                {
235                                        $filename = './filemanager/templates/default/images/error.gif';
236                                        $handle = fopen($filename,'rb');
237                                        echo fread($handle,filesize($filename));
238                                        fclose($handle);
239                                }
240
241                                $GLOBALS['phpgw']->common->phpgw_exit();
242                        }
243                }
244       
245                function delete(){
246                        foreach($this->fileman as $filename)
247                        {
248                                if($this->verifyLock($filename,RELATIVE_ALL) && $this->bo->vfs->rm(array(
249                                        'string' => $this->path.'/'.$filename,
250                                        'relatives' => array (RELATIVE_NONE)
251                                )))
252                                {
253                                        echo $filename.":";
254                                }
255                                else
256                                {
257                                        echo "False:".$filename;
258                                        return False;
259                                }
260                        }
261                }
262                function archive(){
263                        foreach($this->fileman as $filename)
264                        {
265                                if(!$this->verifyLock($filename,RELATIVE_ALL))
266                                {
267                                        echo "locked:".$filename;
268                                        return False;
269                                }
270                                $command .= " ".escapeshellarg($filename);
271                        }
272                        $zipFileName=$GLOBALS['phpgw_info']['user']['account_lid'].date("Ymd").".zip";
273                        $zipFilePath=ini_get("session.save_path")."/".$zipFileName;
274                        $command = $zipFilePath.$command;
275
276                        if (strlen($this->pswd) > 0){
277                                $command = " -P ".(base64_decode($this->pswd) ^ $this->path)." ".$command;
278                        }
279
280                        exec("cd ".$this->bo->vfs->basedir.$this->path.";".escapeshellcmd("nice -n19 zip -9 ".$command)."; history -c");
281
282                        $this->bo->vfs->cp(array(
283                                'from'=> $zipFilePath,
284                                'to'=> $zipFileName,
285                                'relatives'     => array(RELATIVE_NONE|VFS_REAL, RELATIVE_ALL)
286                        ));
287                        $this->bo->vfs->set_attributes(array(
288                                'string'=> $zipFileName,
289                                'relatives'     => array(RELATIVE_ALL),
290                                'attributes'=> array(
291                                        'mime_type' => "application/zip"
292                                )
293                        ));
294                        exec("rm -f ".escapeshellcmd(escapeshellarg($zipFilePath)));
295                        $this->delete();
296                }
297                function unarchive(){
298                        $command = escapeshellarg($this->file);
299                        if (strlen($this->pswd) > 0){
300                                $command = " -P ".(base64_decode($this->pswd) ^ $this->path)." ".$command;
301                        }
302
303                        exec("cd ".escapeshellarg($this->bo->vfs->basedir.$this->path).";".escapeshellcmd("nice -n19 unzip ".$command),$output, $return_var);
304                        exec("history -c"); // privacy is good, we dont want get passwords!
305
306                        if ($return_var == 9 || $return_var == 5 || $return_var == 82){
307                                echo "wpasswd:$command";
308                                return false;
309                        }else if($return_var > 1){
310                                echo "False:";
311                        }
312
313                        $this->fileman[] = $this->file;
314                        $this->delete();
315                        $this->bo->vfs->update_real(array(
316                                        'string'        => $this->path,
317                                        'relatives'     => array(RELATIVE_NONE)
318                                ));
319                }
320                function editComment()
321                {
322                                if($badchar = $this->bo->bad_chars($this->comment, False, True))
323                                {
324                                        echo "False:badchar:".$badchar;
325                                        return False;
326                                }
327
328                                if ($this->bo->vfs->set_attributes(array(
329                                        'string'        => $this->file,
330                                        'relatives'     => array(RELATIVE_ALL),
331                                        'attributes'    => array(
332                                        'comment' => stripslashes($this->comment)
333                                        )
334                                )))
335                                {
336                                        echo "True:".$this->file;
337                                        return True;
338                                }
339                }
340                # Handle Renaming Files and Directories
341                function rename()
342                {
343                        if ($this->file)
344                        {
345                                if($badchar = $this->bo->bad_chars($this->to, True, True))
346                                {
347                                        echo "Error:badchar:".$badchar;
348                                        return false;
349                                }
350                                if(ereg("/", $this->to) || ereg("\\\\", $this->to))
351                                {
352                                        echo "Error:slashes";
353                                        return false;
354                                }
355                                elseif(!$this->verifyLock($this->file,RELATIVE_CURRENT))
356                                        echo "Error:editing";
357                                elseif ($this->bo->vfs->mv(array(
358                                        'from'  => $this->path.'/'.$this->file,
359                                        'to'    => $this->path.'/'.$this->to,
360                                        'relatives' => array(RELATIVE_NONE,RELATIVE_NONE)
361                                )))
362                                {
363                                        echo "True:".$this->file.":".$this->to;
364                                }
365                                else
366                                {
367                                        echo "Error:".$this->file.":".$this->to;
368                                }
369                        }
370                }
371
372        }
Note: See TracBrowser for help on using the repository browser.