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

Revision 2848, 11.7 KB checked in by amuller, 14 years ago (diff)

Ticket #1086 - Utiliza no filemanager a classe RSA implementada na API

  • 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                        'setRestricted'=>True,
24                        'editComment'=> True,
25                        'rename'=>True,
26                        'delete'=>True,
27                        'archive'=>True,
28                        'unarchive'=>True,
29                        'copyto'=>True,
30                        'moveto'=>True,
31                        'summary' => True
32                );
33
34                //keep
35                var $bo;
36                var $dispath;
37                var $cwd;
38                var $lesspath;
39                var $dispsep;
40
41                var $target;
42
43                var $prefs;//array
44
45                var $current_config;
46                // this ones must be checked thorougly;
47                var $fileman = Array();
48                //var $fileman;
49                var $path;
50                var $file;
51                var $debug = false;
52                var $now;
53
54                function vfs_functions()
55                {
56                        $this->now = date('Y-m-d H:i:s');
57
58                        $this->bo = CreateObject('filemanager.bofilemanager');
59
60                        $c = CreateObject('phpgwapi.config','filemanager');
61                        $c->read_repository();
62                        $this->current_config = $c->config_data;
63
64
65                        // here local vars are created from the HTTP vars
66                        @reset($GLOBALS['HTTP_POST_VARS']);
67                        while(list($name,) = @each($GLOBALS['HTTP_POST_VARS']))
68                        {
69                                $this->$name = $GLOBALS['HTTP_POST_VARS'][$name];
70                        }
71
72                        @reset($GLOBALS['HTTP_GET_VARS']);
73                        while(list($name,) = @each($GLOBALS['HTTP_GET_VARS']))
74                        {
75                                $$name = $GLOBALS['HTTP_GET_VARS'][$name];
76                                $this->$name = $GLOBALS['HTTP_GET_VARS'][$name];
77
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
133                        $this->prefs[name] =1;         
134                       
135                }
136        // String format is YYYY-MM-DD HH:MM
137                function dateString2timeStamp($string){
138                        return mktime($string[11].$string[12],
139                                $string[14].$string[15],
140                                $string[17].$string[18],
141                                $string[5].$string[6],
142                                $string[8].$string[9],
143                                $string[0].$string[1].
144                                $string[2].$string[3]);
145                }
146                function verifyLock($file,$relative){
147                        $ls_array = $this->bo->vfs->ls(array(
148                                'string'        => $file,
149                                'relatives'     => array($relative),
150                                'checksubdirs'  => False,
151                                'nofiles'       => True
152                        ));
153                        $timestamp = $this->dateString2timeStamp($ls_array[0]['modified']);
154                        if (time() - $timestamp < 60 && $ls_array[0]['modifiedby_id'] != $GLOBALS['phpgw_info']['user']['account_id']) // recently than last minute: someone is editing
155                        {
156                                $this->messages[]=lang('Error:').lang('This file is being edited right now by:').$GLOBALS['phpgw']->accounts->id2name($ls_array[0]['modifiedby_id']);
157                                return False;
158
159                        }
160                        else
161                                return True;
162                }
163
164                function setRestricted(){
165                        $GLOBALS['phpgw_info']['flags'] = array
166                                (
167                                        'currentapp'    => 'filemanager',
168                                        'noheader'      => True,
169                                        'nonavbar' => True,
170                                        'nofooter'      => True,
171                                        'noappheader'   => True,
172                                        'enable_browser_class'  => True
173                                );
174
175                        if ($this->file)
176                        {
177                                $filename=$this->path.'/'.$this->file;
178                                if(!$this->verifyLock($filename,RELATIVE_NONE))
179                                {
180                                        echo "False";
181                                        return False;
182                                }
183                               
184                                $ls_array = $this->bo->vfs->ls(array(
185                                        'string'        => $filename,
186                                        'relatives'     => array(RELATIVE_NONE),
187                                        'checksubdirs'  => False,
188                                        'nofiles'       => True
189                                ));
190                                if (intval($ls_array[0]['type']) == 0)
191                                        $type = 1;
192                                else
193                                        $type = 0;
194
195                                if($this->bo->vfs->set_attributes (array(
196                                        'string'        => $filename,
197                                        'relatives'     => RELATIVE_NONE,
198                                        'attributes'    => array('type' => $type)
199                                        ))
200                                )
201                                {
202                                        echo "True|".$this->file;
203                                }
204                                else
205                                {
206                                        echo "False";
207                                }
208                        }
209
210                }
211                function touch(){
212                        if($this->file)
213                                if ($this->bo->vfs->touch(array('string'=> $this->file,'relatives' => array(RELATIVE_ALL))))
214                                {
215                                        echo "True";
216                                        return True;
217                                }
218                                else
219                                        return False;
220
221                }
222                function summary()
223                {
224                        header('Content-Type: image/png');
225                        $expires = 60*60*24*15;
226                        header("Cache-Control: maxage=".$expires);
227                        header("Pragma: public");
228                        header("Expires: ".gmdate('D, d M Y H:i:s', time()+$expires));
229                        if($this->file)
230                        {       
231                                $content = $this->bo->vfs->summary(array('path' => $this->path,
232                                        'string' => $this->file
233                                ));
234                                if (strlen($content) < 1)
235                                {
236                                        $filename = './filemanager/templates/default/images/error.png';
237                                        $handle = fopen($filename,'rb');
238                                        $content = fread($handle,filesize($filename));
239                                        fclose($handle);
240                                }
241                                echo $content;
242                        }
243                        $GLOBALS['phpgw']->common->phpgw_exit();
244                }
245       
246                function delete(){
247                        foreach($this->fileman as $filename)
248                        {
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 $filename)
265                        {
266                                if(!$this->verifyLock($filename,RELATIVE_ALL))
267                                {
268                                        echo "locked|".$filename;
269                                        return False;
270                                }
271                                $command .= " ".escapeshellarg($filename);
272                        }
273                        $zipFileName=$GLOBALS['phpgw_info']['user']['account_lid'].date("Y-m-d,H:i:s").".zip";
274                        $zipFilePath=ini_get("session.save_path")."/".$zipFileName;
275                        $command = $zipFilePath.$command;
276                        require_once('phpgwapi/inc/class.rsa.inc.php');
277                        $rsa = new rsa();
278
279                        if (strlen($this->pswd) > 0){
280                                $command = " -P ".$rsa->decode(base64_decode($this->pswd))." ".$command;
281                        }
282                        session_start();
283                        $_SESSION['debug'][] = $_SESSION['phpgw_session']['publ_key'];
284                        $_SESSION['debug'][] = $_SESSION['phpgw_session']['priv_key'];
285                        $_SESSION['debug'][] = $_SESSION['phpgw_session']['modulus'];
286                                                               
287                        $_SESSION['debug'][] = $this->pswd;
288                        $_SESSION['debug'][] = base64_decode($this->pswd);
289                        $_SESSION['debug'][] = $rsa->decode(base64_decode($this->pswd));
290                        $_SESSION['debug'][] = $command;
291
292
293                        exec("cd ".$this->bo->vfs->basedir.$this->path.";".escapeshellcmd("nice -n19 zip -9 ".$command),$output,$return_var);
294                        exec("history -c"); // privacy is good, we dont want get passwords!
295                        if ($return_var > 1){
296                                echo "False|".$return_var;
297                                return false;
298                        }
299
300                        $this->bo->vfs->cp(array(
301                                'from'=> $zipFilePath,
302                                'to'=> $zipFileName,
303                                'relatives'     => array(RELATIVE_NONE|VFS_REAL, RELATIVE_ALL)
304                        ));
305                        $this->bo->vfs->set_attributes(array(
306                                'string'=> $zipFileName,
307                                'relatives'     => array(RELATIVE_ALL),
308                                'attributes'=> array(
309                                        'mime_type' => "application/zip"
310                                )
311                        ));
312                        exec("rm -f ".escapeshellcmd(escapeshellarg($zipFilePath)));
313                        $this->delete();
314                }
315                function unarchive(){
316                        $command = escapeshellarg($this->file);
317                        require_once('phpgwapi/inc/class.rsa.inc.php');
318                        $rsa = new rsa();
319
320                        if (strlen($this->pswd) > 0){
321                                $command = " -P ".$rsa->decode(base64_decode($this->pswd))." ".$command;
322                        }
323
324                        exec("cd ".escapeshellarg($this->bo->vfs->basedir.$this->path).";".escapeshellcmd("nice -n19 unzip ".$command),$output, $return_var);
325                        exec("history -c"); // privacy is good, we dont want get passwords!
326
327                        if ($return_var == 9 || $return_var == 5 || $return_var == 82){
328                                echo "wpasswd|";
329                                return false;
330                        }else if($return_var > 1){
331                                echo "False|";
332                        }
333
334                        $this->fileman[] = $this->file;
335                        $this->delete();
336                        $this->bo->vfs->update_real(array(
337                                        'string'        => $this->path,
338                                        'relatives'     => array(RELATIVE_NONE)
339                                ));
340                }
341                function editComment()
342                {
343                                if($badchar = $this->bo->bad_chars($this->comment, False, True))
344                                {
345                                        echo "False|badchar|".$badchar;
346                                        return False;
347                                }
348
349                                if ($this->bo->vfs->set_attributes(array(
350                                        'string'        => $this->file,
351                                        'relatives'     => array(RELATIVE_ALL),
352                                        'attributes'    => array(
353                                        'comment' => stripslashes($this->comment)
354                                        )
355                                )))
356                                {
357                                        echo "True|".$this->file;
358                                        return True;
359                                }
360                }
361                # Handle Moving Files and Directories
362                function moveto()
363                {
364                        if(!$this->to)
365                        {
366                                echo "NODIR|";
367                                return;
368                        }
369                        else
370                        {
371                                while(list($num, $file) = each($this->fileman))
372                                {
373                                        if($this->bo->vfs->mv(array(
374                                                'from'  => $this->from . '/' . $file,
375                                                'to'    => $this->to . '/' . $file,
376                                                'relatives'     => array(RELATIVE_NONE, RELATIVE_NONE)
377                                        )))
378                                                $moved++;
379                                        else
380                                                $error++;
381                                }
382                        }
383                        if($error > 0){
384                                echo "SOMEERROR|".$error;
385                                return;
386                        }
387                        if($moved > 0)
388                                echo "MOVED|".$moved;
389                }
390
391                // Handle Copying of Files and Directories
392                function copyto()
393                {
394                        if(!$this->to)
395                        {
396                                echo "NODIR|";
397                                return;
398                        }
399                        else
400                        {
401                                while(list($num, $file) = each($this->fileman))
402                                {
403                                        if($this->bo->vfs->cp(array(
404                                                'from'  => $this->from . '/' . $file,
405                                                'to'    => $this->to . '/' . $file,
406                                                'relatives'     => array(RELATIVE_NONE, RELATIVE_NONE)
407                                        )))
408                                                $copied++;
409                                        else
410                                                $error++;
411                                }
412                        }
413                        if($error > 0){
414                                echo "SOMEERROR|".$error;
415                                return;
416                        }
417                        if($copied > 0)
418                                echo "COPIED|".$copied;
419                }
420
421                # Handle Renaming Files and Directories
422                function rename()
423                {
424                        if ($this->file)
425                        {
426                                if($badchar = $this->bo->bad_chars($this->to, True, True))
427                                {
428                                        echo "Error|badchar|".$badchar;
429                                        return false;
430                                }
431                                if(ereg("/", $this->to) || ereg("\\\\", $this->to))
432                                {
433                                        echo "Error|slashes";
434                                        return false;
435                                }
436                                elseif(!$this->verifyLock($this->file,RELATIVE_CURRENT))
437                                        echo "Error|editing";
438                                elseif ($this->bo->vfs->mv(array(
439                                        'from'  => $this->path.'/'.$this->file,
440                                        'to'    => $this->path.'/'.$this->to,
441                                        'relatives' => array(RELATIVE_NONE,RELATIVE_NONE)
442                                )))
443                                {
444                                        echo "True|".$this->file."|".$this->to;
445                                }
446                                else
447                                {
448                                        echo "Error|".$this->file."|".$this->to;
449                                }
450                        }
451                }
452
453        }
Note: See TracBrowser for help on using the repository browser.