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

Revision 1889, 11.3 KB checked in by amuller, 15 years ago (diff)

Ticket #597 - Melhoria do FM. melhorias na interface, melhoria na cópia, ao mover e etc...

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