LOB.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
  6. // | Stig. S. Bakken, Lukas Smith |
  7. // | All rights reserved. |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
  10. // | API as well as database abstraction for PHP applications. |
  11. // | This LICENSE is in the BSD license style. |
  12. // | |
  13. // | Redistribution and use in source and binary forms, with or without |
  14. // | modification, are permitted provided that the following conditions |
  15. // | are met: |
  16. // | |
  17. // | Redistributions of source code must retain the above copyright |
  18. // | notice, this list of conditions and the following disclaimer. |
  19. // | |
  20. // | Redistributions in binary form must reproduce the above copyright |
  21. // | notice, this list of conditions and the following disclaimer in the |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // | |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission. |
  28. // | |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
  40. // | POSSIBILITY OF SUCH DAMAGE. |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@pooteeweet.org> |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: LOB.php,v 1.34 2006/10/25 11:52:21 lsmith Exp $
  46. /**
  47. * @package MDB2
  48. * @category Database
  49. * @author Lukas Smith <smith@pooteeweet.org>
  50. */
  51. require_once('MDB2.php');
  52. /**
  53. * MDB2_LOB: user land stream wrapper implementation for LOB support
  54. *
  55. * @package MDB2
  56. * @category Database
  57. * @author Lukas Smith <smith@pooteeweet.org>
  58. */
  59. class MDB2_LOB
  60. {
  61. /**
  62. * contains the key to the global MDB2 instance array of the associated
  63. * MDB2 instance
  64. *
  65. * @var integer
  66. * @access protected
  67. */
  68. var $db_index;
  69. /**
  70. * contains the key to the global MDB2_LOB instance array of the associated
  71. * MDB2_LOB instance
  72. *
  73. * @var integer
  74. * @access protected
  75. */
  76. var $lob_index;
  77. // {{{ stream_open()
  78. /**
  79. * open stream
  80. *
  81. * @param string specifies the URL that was passed to fopen()
  82. * @param string the mode used to open the file
  83. * @param int holds additional flags set by the streams API
  84. * @param string not used
  85. *
  86. * @return bool
  87. * @access public
  88. */
  89. function stream_open($path, $mode, $options, &$opened_path)
  90. {
  91. if (!preg_match('/^rb?\+?$/', $mode)) {
  92. return false;
  93. }
  94. $url = parse_url($path);
  95. if (empty($url['host'])) {
  96. return false;
  97. }
  98. $this->db_index = (int)$url['host'];
  99. if (!isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
  100. return false;
  101. }
  102. $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  103. $this->lob_index = (int)$url['user'];
  104. if (!isset($db->datatype->lobs[$this->lob_index])) {
  105. return false;
  106. }
  107. return true;
  108. }
  109. // }}}
  110. // {{{ stream_read()
  111. /**
  112. * read stream
  113. *
  114. * @param int number of bytes to read
  115. *
  116. * @return string
  117. * @access public
  118. */
  119. function stream_read($count)
  120. {
  121. if (isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
  122. $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  123. $db->datatype->_retrieveLOB($db->datatype->lobs[$this->lob_index]);
  124. $data = $db->datatype->_readLOB($db->datatype->lobs[$this->lob_index], $count);
  125. $length = strlen($data);
  126. if ($length == 0) {
  127. $db->datatype->lobs[$this->lob_index]['endOfLOB'] = true;
  128. }
  129. $db->datatype->lobs[$this->lob_index]['position'] += $length;
  130. return $data;
  131. }
  132. }
  133. // }}}
  134. // {{{ stream_write()
  135. /**
  136. * write stream, note implemented
  137. *
  138. * @param string data
  139. *
  140. * @return int
  141. * @access public
  142. */
  143. function stream_write($data)
  144. {
  145. return 0;
  146. }
  147. // }}}
  148. // {{{ stream_tell()
  149. /**
  150. * return the current position
  151. *
  152. * @return int current position
  153. * @access public
  154. */
  155. function stream_tell()
  156. {
  157. if (isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
  158. $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  159. return $db->datatype->lobs[$this->lob_index]['position'];
  160. }
  161. }
  162. // }}}
  163. // {{{ stream_eof()
  164. /**
  165. * Check if stream reaches EOF
  166. *
  167. * @return bool
  168. * @access public
  169. */
  170. function stream_eof()
  171. {
  172. if (!isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
  173. return true;
  174. }
  175. $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  176. $result = $db->datatype->_endOfLOB($db->datatype->lobs[$this->lob_index]);
  177. if (version_compare(phpversion(), "5.0", ">=")
  178. && version_compare(phpversion(), "5.1", "<")
  179. ) {
  180. return !$result;
  181. }
  182. return $result;
  183. }
  184. // }}}
  185. // {{{ stream_seek()
  186. /**
  187. * Seek stream, not implemented
  188. *
  189. * @param int offset
  190. * @param int whence
  191. *
  192. * @return bool
  193. * @access public
  194. */
  195. function stream_seek($offset, $whence)
  196. {
  197. return false;
  198. }
  199. // }}}
  200. // {{{ stream_stat()
  201. /**
  202. * return information about stream
  203. *
  204. * @access public
  205. */
  206. function stream_stat()
  207. {
  208. if (isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
  209. $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  210. return array(
  211. 'db_index' => $this->db_index,
  212. 'lob_index' => $this->lob_index,
  213. );
  214. }
  215. }
  216. // }}}
  217. // {{{ stream_close()
  218. /**
  219. * close stream
  220. *
  221. * @access public
  222. */
  223. function stream_close()
  224. {
  225. if (isset($GLOBALS['_MDB2_databases'][$this->db_index])) {
  226. $db =& $GLOBALS['_MDB2_databases'][$this->db_index];
  227. if (isset($db->datatype->lobs[$this->lob_index])) {
  228. $db->datatype->_destroyLOB($db->datatype->lobs[$this->lob_index]);
  229. unset($db->datatype->lobs[$this->lob_index]);
  230. }
  231. }
  232. }
  233. // }}}
  234. }
  235. // register streams wrapper
  236. if (!stream_wrapper_register("MDB2LOB", "MDB2_LOB")) {
  237. MDB2::raiseError();
  238. return false;
  239. }
  240. ?>