File: copy.php

package info (click to toggle)
horde3 3.1.3-4etch7
  • links: PTS
  • area: main
  • in suites: etch
  • size: 22,876 kB
  • ctags: 18,071
  • sloc: php: 75,151; xml: 2,979; sql: 1,069; makefile: 79; sh: 64
file content (150 lines) | stat: -rw-r--r-- 4,942 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php

/**
 * Strategy for copying input tree out of a VFS
 *
 * $Horde: framework/VFS_ISOWriter/ISOWriter/RealInputStrategy/copy.php,v 1.1.8.5 2006/01/01 21:28:44 jan Exp $
 *
 * Copyright 2004-2006 Cronosys, LLC <http://www.cronosys.com/>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  Jason M. Felice <jfelice@cronosys.com>
 * @package VFS_ISO
 * @since   Horde 3.0
 */
class VFS_ISOWriter_RealInputStrategy_copy extends VFS_ISOWriter_RealInputStrategy {

    var $_tempPath = null;

    function getRealPath()
    {
        if (is_null($this->_tempPath)) {
            $tmp_locations = array('/tmp', '/var/tmp', 'c:\WUTemp', 'c:\temp',
                                   'c:\windows\temp', 'c:\winnt\temp');

            /* First, try PHP's upload_tmp_dir directive. */
            $tmp = ini_get('upload_tmp_dir');

            /* Otherwise, try to determine the TMPDIR environment
             * variable. */
            if (empty($tmp)) {
                $tmp = getenv('TMPDIR');
            }

            /* If we still cannot determine a value, then cycle through a
             * list of preset possibilities. */
            while (empty($tmp) && count($tmp_locations)) {
                $tmp_check = array_shift($tmp_locations);
                if (@is_dir($tmp_check)) {
                    $tmp = $tmp_check;
                }
            }

            if (empty($tmp)) {
                return PEAR::raiseError(_("Cannot find a temporary directory."));
            }

            $this->_tempPath = tempnam($tmp, 'isod');
            @unlink($this->_tempPath);

            $res = $this->_copyToTempPath();
            if (is_a($res, 'PEAR_Error')) {
                return $res;
            }
        }

        return $this->_tempPath;
    }

    function finished()
    {
        return VFS_ISOWriter_RealInputStrategy_copy::_removeRecursive($this->_tempPath);
    }

    function _removeRecursive($path)
    {
        $dh = @opendir($path);
        if (!is_resource($dh)) {
            return PEAR::raiseError(sprintf(_("Could not open directory \"%s\"."),
                                            $path));
        }
        while (($ent = readdir($dh)) !== false) {
            if ($ent == '.' || $ent == '..') {
                continue;
            }

            $full = sprintf('%s/%s', $path, $ent);
            if (is_dir($full)) {
                $res = VFS_ISOWriter_RealInputStrategy_copy::_removeRecursive($full);
                if (is_a($res, 'PEAR_Error')) {
                    return $res;
                }
            } else {
                if (!@unlink($full)) {
                    return PEAR::raiseError(sprintf(_("Could not unlink \"%s\"."),
                                                    $full));
                }
            }
        }
        closedir($dh);

        if (!@rmdir($path)) {
            return PEAR::raiseError(sprintf(_("Could not rmdir \"%s\"."), $full));
        }
    }

    function _copyToTempPath()
    {
        $dirStack = array('');

        while (count($dirStack) > 0) {
            $dir = array_shift($dirStack);
            if (empty($dir)) {
                $target = $this->_tempPath;
            } else {
                $target = sprintf('%s/%s', $this->_tempPath, $dir);
            }
            if (!@mkdir($target)) {
                return PEAR::raiseError(sprintf(_("Could not mkdir \"%s\"."), $target));
            }

            $sourcePath = $this->_sourceRoot;
            if (!empty($dir)) {
                $sourcePath .= '/' . $dir;
            }

            $list = $this->_sourceVfs->listFolder($sourcePath, null, true);
            if (is_a($list, 'PEAR_Error')) {
                return $list;
            }

            foreach ($list as $entry) {
                if ($entry['type'] == '**dir') {
                    if (empty($dir)) {
                        $dirStack[] = $entry['name'];
                    } else {
                        $dirStack[] = sprintf('%s/%s', $dir, $entry['name']);
                    }
                } else {
                    $data = $this->_sourceVfs->read($sourcePath, $entry['name']);
                    if (is_a($data, 'PEAR_Error')) {
                        return $data;
                    }

                    $targetFile = sprintf('%s/%s', $target, $entry['name']);
                    $fh = @fopen($targetFile, 'w');
                    if (!is_resource($fh)) {
                        return PEAR::raiseError(sprintf(_("Could not open \"%s\" for writing."), $targetFile));
                    }
                    if (fwrite($fh, $data) != strlen($data)) {
                        return PEAR::raiseError(sprintf(_("Error writing \"%s\"."), $targetFile));
                    }
                    fclose($fh);
                }
            }
        }
    }

}