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

Revision 1869, 10.0 KB checked in by amuller, 14 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 H:i:s');
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 $filecode)
247                        {
248                                $filename = base64_decode($filecode);
249                                if($this->verifyLock($filename,RELATIVE_ALL) && $this->bo->vfs->rm(array(
250                                        'string' => $this->path.'/'.$filename,
251                                        'relatives' => array (RELATIVE_NONE)
252                                )))
253                                {
254                                        echo $filename."|";
255                                }
256                                else
257                                {
258                                        echo "False|".$filename;
259                                        return False;
260                                }
261                        }
262                }
263                function archive(){
264                        foreach($this->fileman as $filecode)
265                        {
266                                $filename = base64_decode($filecode);
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) ^ $this->path)." ".$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) ^ $this->path)." ".$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 Renaming Files and Directories
348                function rename()
349                {
350                        if ($this->file)
351                        {
352                                if($badchar = $this->bo->bad_chars($this->to, True, True))
353                                {
354                                        echo "Error|badchar|".$badchar;
355                                        return false;
356                                }
357                                if(ereg("/", $this->to) || ereg("\\\\", $this->to))
358                                {
359                                        echo "Error|slashes";
360                                        return false;
361                                }
362                                elseif(!$this->verifyLock($this->file,RELATIVE_CURRENT))
363                                        echo "Error|editing";
364                                elseif ($this->bo->vfs->mv(array(
365                                        'from'  => $this->path.'/'.$this->file,
366                                        'to'    => $this->path.'/'.$this->to,
367                                        'relatives' => array(RELATIVE_NONE,RELATIVE_NONE)
368                                )))
369                                {
370                                        echo "True|".$this->file."|".$this->to;
371                                }
372                                else
373                                {
374                                        echo "Error|".$this->file."|".$this->to;
375                                }
376                        }
377                }
378
379        }
Note: See TracBrowser for help on using the repository browser.