Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
4
/**
5
 * HTTP::Download::PgLOB
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * @category   HTTP
10
 * @package    HTTP_Download
11
 * @author     Michael Wallner <mike@php.net>
12
 * @copyright  2003-2005 Michael Wallner
13
 * @license    BSD, revised
14
 * @version    CVS: $Id: PgLOB.php 304423 2010-10-15 13:36:46Z clockwerx $
15
 * @link       http://pear.php.net/package/HTTP_Download
16
 */
17
 
18
$GLOBALS['_HTTP_Download_PgLOB_Connection'] = null;
19
stream_register_wrapper('pglob', 'HTTP_Download_PgLOB');
20
 
21
/**
22
 * PgSQL large object stream interface for HTTP_Download
23
 *
24
 * Usage:
25
 * <code>
26
 * require_once 'HTTP/Download.php';
27
 * require_once 'HTTP/Download/PgLOB.php';
28
 * $db = &DB::connect('pgsql://user:pass@host/db');
29
 * // or $db = pg_connect(...);
30
 * $lo = HTTP_Download_PgLOB::open($db, 12345);
31
 * $dl = &new HTTP_Download;
32
 * $dl->setResource($lo);
33
 * $dl->send()
34
 * </code>
35
 *
36
 * @access  public
37
 * @version $Revision: 304423 $
38
 */
39
class HTTP_Download_PgLOB
40
{
41
    /**
42
     * Set Connection
43
     *
44
     * @static
45
     * @access  public
46
     * @return  bool
47
     * @param   mixed   $conn
48
     */
49
    function setConnection($conn)
50
    {
51
        if (is_a($conn, 'DB_Common')) {
52
            $conn = $conn->dbh;
53
        } elseif (  is_a($conn, 'MDB_Common') ||
54
                    is_a($conn, 'MDB2_Driver_Common')) {
55
            $conn = $conn->connection;
56
        }
57
        if ($isResource = is_resource($conn)) {
58
            $GLOBALS['_HTTP_Download_PgLOB_Connection'] = $conn;
59
        }
60
        return $isResource;
61
    }
62
 
63
    /**
64
     * Get Connection
65
     *
66
     * @static
67
     * @access  public
68
     * @return  resource
69
     */
70
    function getConnection()
71
    {
72
        if (is_resource($GLOBALS['_HTTP_Download_PgLOB_Connection'])) {
73
            return $GLOBALS['_HTTP_Download_PgLOB_Connection'];
74
        }
75
        return null;
76
    }
77
 
78
    /**
79
     * Open
80
     *
81
     * @static
82
     * @access  public
83
     * @return  resource
84
     * @param   mixed   $conn
85
     * @param   int     $loid
86
     * @param   string  $mode
87
     */
88
    function open($conn, $loid, $mode = 'rb')
89
    {
90
        HTTP_Download_PgLOB::setConnection($conn);
91
        return fopen('pglob:///'. $loid, $mode);
92
    }
93
 
94
    /**#@+
95
     * Stream Interface Implementation
96
     * @internal
97
     */
98
    var $ID = 0;
99
    var $size = 0;
100
    var $conn = null;
101
    var $handle = null;
102
 
103
    function stream_open($path, $mode)
104
    {
105
        if (!$this->conn = HTTP_Download_PgLOB::getConnection()) {
106
            return false;
107
        }
108
        if (!preg_match('/(\d+)/', $path, $matches)) {
109
            return false;
110
        }
111
        $this->ID = $matches[1];
112
 
113
        if (!pg_query($this->conn, 'BEGIN')) {
114
            return false;
115
        }
116
 
117
        $this->handle = pg_lo_open($this->conn, $this->ID, $mode);
118
        if (!is_resource($this->handle)) {
119
            return false;
120
        }
121
 
122
        // fetch size of lob
123
        pg_lo_seek($this->handle, 0, PGSQL_SEEK_END);
124
        $this->size = (int) pg_lo_tell($this->handle);
125
        pg_lo_seek($this->handle, 0, PGSQL_SEEK_SET);
126
 
127
        return true;
128
    }
129
 
130
    function stream_read($length)
131
    {
132
        return pg_lo_read($this->handle, $length);
133
    }
134
 
135
    function stream_seek($offset, $whence = SEEK_SET)
136
    {
137
        return pg_lo_seek($this->handle, $offset, $whence);
138
    }
139
 
140
    function stream_tell()
141
    {
142
        return pg_lo_tell($this->handle);
143
    }
144
 
145
    function stream_eof()
146
    {
147
        return pg_lo_tell($this->handle) >= $this->size;
148
    }
149
 
150
    function stream_flush()
151
    {
152
        return true;
153
    }
154
 
155
    function stream_stat()
156
    {
157
        return array('size' => $this->size, 'ino' => $this->ID);
158
    }
159
 
160
    function stream_write($data)
161
    {
162
        return pg_lo_write($this->handle, $data);
163
    }
164
 
165
    function stream_close()
166
    {
167
        if (pg_lo_close($this->handle)) {
168
            return pg_query($this->conn, 'COMMIT');
169
        } else {
170
            pg_query($this->conn ,'ROLLBACK');
171
            return false;
172
        }
173
    }
174
    /**#@-*/
175
}
176
 
177
?>