Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Updated tests, dropped PHP4 compatability, tabs to spaces

git-svn-id: http://svn.php.net/repository/pear/packages/Net_CDDB/trunk@304680 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
commit e909554d7565731e0ae9bf34eb38bb1b70080547 1 parent 5d2d29a
Daniel O'Connor CloCkWeRX authored
312 Net/CDDB.php
@@ -38,7 +38,7 @@
38 38 *
39 39 * @var string
40 40 */
41   -define('NET_CDDB_VERSION', '0.3.0');
  41 +define('NET_CDDB_VERSION', '0.4.0');
42 42
43 43 /**
44 44 * CDDB protocol level ***DO NOT CHANGE THIS***
@@ -202,159 +202,159 @@
202 202 */
203 203 class Net_CDDB
204 204 {
205   - /**
206   - * Status of debugging (enabled/disabled)
207   - *
208   - * @var bool
209   - * @access protected
210   - */
211   - var $_debug;
212   -
213   - /**
214   - * Enables or disables debugging ( prints out all responses/requests )
215   - *
216   - * @access public
217   - *
218   - * @param bool $true_or_false
219   - */
220   - function debug($true_or_false)
221   - {
222   - $this->_debug = $true_or_false;
223   - }
224   -
225   - /**
226   - * Prints/logs debugging messages
227   - *
228   - * @access protected
229   - *
230   - * @param string $msg The debugging message
231   - *
232   - * @return void
233   - */
234   - function _debug($msg)
235   - {
236   - if ($this->_debug) {
237   - print(date('Y-m-d H:i:s') . ': ' . $msg . "\n");
238   - }
239   - }
240   -
241   - /**
242   - * Parse a CDDB style database record into an array
243   - *
244   - * @see Net_CDDB::_parseResult()
245   - * @uses Net_CDDB_Utilities::parseRecord()
246   - *
247   - * @access protected
248   - *
249   - * @param string $str
250   - * @param string $category
251   - * @return array
252   - */
253   - function _parseRecord($str, $category = '')
254   - {
255   - return Net_CDDB_Utilities::parseRecord($str, $category);
256   - }
257   -
258   - /**
259   - * Parse a result string returned by a CDDB query into an array
260   - *
261   - * @see Net_CDDB::_parseRecord()
262   - *
263   - * @access protected
264   - *
265   - * @param string $str
266   - * @return array
267   - */
268   - function _parseResult($str)
269   - {
270   - $first_space = strpos($str, ' ');
271   - $second_space = strpos($str, ' ', $first_space + 1);
272   -
273   - $category = substr($str, 0, $first_space);
274   - $discid = substr($str, $first_space, $second_space - $first_space);
275   - $artist_and_title = substr($str, $second_space);
276   -
277   - $str = '';
278   - $str .= 'DISCID=' . $discid . "\n";
279   - $str .= 'DTITLE=' . $artist_and_title;
280   -
281   - return Net_CDDB_Utilities::parseRecord($str, $category);
282   - }
283   -
284   - /**
285   - * Calculate a disc ID based on the track offsets and the disc length
286   - *
287   - * @uses Net_CDDB_Utilities::calculateDiscId()
288   - *
289   - * @param array $track_offsets The offsets of the tracks on the CD
290   - * @param int $length The total number of seconds for the disc
291   - * @return string 8-character disc ID value
292   - */
293   - function calculateDiscId($track_offsets, $length)
294   - {
295   - /*
296   - $query = 'discid ' . count($track_offsets) . ' ';
297   -
298   - foreach ($track_offsets as $track => $track_offset) {
299   - $query = $query . $track_offset . ' ';
300   - }
301   -
302   - $query = $query . $length;
303   -
304   - $this->_send($query);
305   -
306   - switch($this->_readResponseStatus()) {
307   - case NET_CDDB_RESPONSE_OK: // 200, OK
308   - return substr(trim($this->_buffer), -8, 8);
309   - case NET_CDDB_RESPONSE_ERROR_SYNTAX: // 500, Syntax error
310   - default:
311   - return false;
312   - }
313   - */
314   -
315   - return Net_CDDB_Utilities::calculateDiscId($track_offsets, $length);
316   - }
317   -
318   - /**
319   - * Create a protocol instance of the given type with the given parameters
320   - *
321   - * Creates a protocol object instance of a given type by protocol type.
322   - * The array of connection parameters will be passed directly to the protocol
323   - * instance.
324   - *
325   - * @see Net_CDDB_Client
326   - * @see Net_CDDB_Server
327   - * @see Net_CDDB_Client::_createReader()
328   - *
329   - * @access public
330   - *
331   - * @param string $type
332   - * @param array $params
333   - * @return object
334   - */
335   - function _createProtocol($dsn, $options)
336   - {
337   - $parse = parse_url(str_replace(':///', '://null/', $dsn));
338   -
339   - if (false !== ($pos = strpos($parse['scheme'], '.'))) {
340   - $parse['scheme'] = substr($parse['scheme'], 0, $pos);
341   - $dsn = substr($dsn, $pos + 1);
342   - }
343   -
344   - $file = ucfirst(strtolower($parse['scheme']));
345   - $class = 'Net_CDDB_Protocol_' . $file;
346   -
347   - /**
348   - * Require the file the protocol class is stored in
349   - */
350   - include_once 'Net/CDDB/Protocol/' . $file . '.php';
351   -
352   - if (class_exists($class)) {
353   - return new $class($dsn, $options);
354   - } else {
355   - return PEAR::raiseError('Could not find protocol file for: ' . $file);
356   - }
357   - }
  205 + /**
  206 + * Status of debugging (enabled/disabled)
  207 + *
  208 + * @var bool
  209 + * @access protected
  210 + */
  211 + var $_debug;
  212 +
  213 + /**
  214 + * Enables or disables debugging ( prints out all responses/requests )
  215 + *
  216 + * @access public
  217 + *
  218 + * @todo Inject a log instead?
  219 + * @param bool $true_or_false
  220 + */
  221 + function debug($true_or_false)
  222 + {
  223 + $this->_debug = $true_or_false;
  224 + }
  225 +
  226 + /**
  227 + * Prints/logs debugging messages
  228 + *
  229 + * @access protected
  230 + *
  231 + * @param string $msg The debugging message
  232 + *
  233 + * @return void
  234 + */
  235 + function _debug($msg)
  236 + {
  237 + if ($this->_debug) {
  238 + print(date('Y-m-d H:i:s') . ': ' . $msg . "\n");
  239 + }
  240 + }
  241 +
  242 + /**
  243 + * Parse a CDDB style database record into an array
  244 + *
  245 + * @todo Inject utilities instead?
  246 + * @see Net_CDDB::_parseResult()
  247 + * @uses Net_CDDB_Utilities::parseRecord()
  248 + *
  249 + * @access protected
  250 + *
  251 + * @param string $str
  252 + * @param string $category
  253 + * @return array
  254 + */
  255 + function _parseRecord($str, $category = '')
  256 + {
  257 + return Net_CDDB_Utilities::parseRecord($str, $category);
  258 + }
  259 +
  260 + /**
  261 + * Parse a result string returned by a CDDB query into an array
  262 + *
  263 + * @see Net_CDDB::_parseRecord()
  264 + *
  265 + * @access protected
  266 + *
  267 + * @param string $str
  268 + * @return array
  269 + */
  270 + function _parseResult($str)
  271 + {
  272 + $first_space = strpos($str, ' ');
  273 + $second_space = strpos($str, ' ', $first_space + 1);
  274 +
  275 + $category = substr($str, 0, $first_space);
  276 + $discid = substr($str, $first_space, $second_space - $first_space);
  277 + $artist_and_title = substr($str, $second_space);
  278 +
  279 + $str = '';
  280 + $str .= 'DISCID=' . $discid . "\n";
  281 + $str .= 'DTITLE=' . $artist_and_title;
  282 +
  283 + return Net_CDDB_Utilities::parseRecord($str, $category);
  284 + }
  285 +
  286 + /**
  287 + * Calculate a disc ID based on the track offsets and the disc length
  288 + *
  289 + * @uses Net_CDDB_Utilities::calculateDiscId()
  290 + *
  291 + * @param array $track_offsets The offsets of the tracks on the CD
  292 + * @param int $length The total number of seconds for the disc
  293 + * @return string 8-character disc ID value
  294 + */
  295 + function calculateDiscId($track_offsets, $length)
  296 + {
  297 + /*
  298 + $query = 'discid ' . count($track_offsets) . ' ';
  299 +
  300 + foreach ($track_offsets as $track => $track_offset) {
  301 + $query = $query . $track_offset . ' ';
  302 + }
  303 +
  304 + $query = $query . $length;
  305 +
  306 + $this->_send($query);
  307 +
  308 + switch($this->_readResponseStatus()) {
  309 + case NET_CDDB_RESPONSE_OK: // 200, OK
  310 + return substr(trim($this->_buffer), -8, 8);
  311 + case NET_CDDB_RESPONSE_ERROR_SYNTAX: // 500, Syntax error
  312 + default:
  313 + return false;
  314 + }
  315 + */
  316 +
  317 + return Net_CDDB_Utilities::calculateDiscId($track_offsets, $length);
  318 + }
  319 +
  320 + /**
  321 + * Create a protocol instance of the given type with the given parameters
  322 + *
  323 + * Creates a protocol object instance of a given type by protocol type.
  324 + * The array of connection parameters will be passed directly to the protocol
  325 + * instance.
  326 + *
  327 + * @see Net_CDDB_Client
  328 + * @see Net_CDDB_Server
  329 + * @see Net_CDDB_Client::_createReader()
  330 + *
  331 + * @access public
  332 + *
  333 + * @param string $type
  334 + * @param array $params
  335 + * @return object
  336 + */
  337 + function _createProtocol($dsn, $options)
  338 + {
  339 + $parse = parse_url(str_replace(':///', '://null/', $dsn));
  340 +
  341 + if (false !== ($pos = strpos($parse['scheme'], '.'))) {
  342 + $parse['scheme'] = substr($parse['scheme'], 0, $pos);
  343 + $dsn = substr($dsn, $pos + 1);
  344 + }
  345 +
  346 + $file = ucfirst(strtolower($parse['scheme']));
  347 + $class = 'Net_CDDB_Protocol_' . $file;
  348 +
  349 + /**
  350 + * Require the file the protocol class is stored in
  351 + */
  352 + include_once 'Net/CDDB/Protocol/' . $file . '.php';
  353 +
  354 + if (class_exists($class)) {
  355 + return new $class($dsn, $options);
  356 + }
  357 +
  358 + throw new Exception('Could not find protocol file for: ' . $file);
  359 + }
358 360 }
359   -
360   -?>
612 Net/CDDB/Protocol/Cddbp.php
@@ -39,312 +39,312 @@
39 39 */
40 40 class Net_CDDB_Protocol_CDDBP extends Net_CDDB_Protocol
41 41 {
42   - /**
43   - * Connection resource to remove server
44   - *
45   - * @var resource
46   - * @access protected
47   - */
48   - var $_conn;
49   -
50   - /**
51   - * String buffer for recieving responses
52   - *
53   - * @var string
54   - * @access protected
55   - */
56   - var $_buffer;
57   -
58   - /**
59   - * String buffer for holding status line of last request
60   - *
61   - * @var string
62   - * @access protected
63   - */
64   - var $_status_buffer;
65   -
66   - /**
67   - * CDDB server to connect to
68   - *
69   - * @var string
70   - * @access protected
71   - */
72   - var $_server;
73   -
74   - /**
75   - * CDDB port to connect on
76   - *
77   - * @var integer
78   - * @access protected
79   - */
80   - var $_port;
81   -
82   - /**
83   - * User to connect to CDDB server as
84   - *
85   - * @var string
86   - * @access protected
87   - */
88   - var $_user;
89   -
90   - /**
91   - * Hostname of remote CDDB server
92   - *
93   - * @var string
94   - * @access protected
95   - */
96   - var $_host;
97   -
98   - /**
99   - * Client name to report to CDDB server
100   - *
101   - * @var string
102   - * @access protected
103   - */
104   - var $_client;
105   -
106   - /**
107   - * Client version string to report to CDDB server
108   - *
109   - * @var string
110   - * @access protected
111   - */
112   - var $_version;
113   -
114   - /**
115   - * String indicating CDDB server returned a multi-line response
116   - *
117   - * @var string
118   - * @access protected
119   - */
120   - var $_multi_line_ident;
121   -
122   - /**
123   - * Construct a CDDBP object
124   - *
125   - * @see Net_CDDB
126   - * @see Net_CDDB_HTTP
127   - *
128   - * @param string $protocol DSN String
129   - * @param array $option_params
130   - * @return void
131   - */
132   - function Net_CDDB_Protocol_CDDBP($protocol, $option_params)
133   - {
134   - $protocol_params = $this->_parseDsn($protocol);
135   -
136   - $this->_conn = null;
137   -
138   - $this->_server = $protocol_params['host'];
139   - $this->_port = $protocol_params['port'];
140   - $this->_user = $protocol_params['user'];
141   -
142   - $this->_host = $option_params['host'];
143   -
144   - $this->_client = 'PHP/PEAR(' . get_class($this) . ')';
145   - $this->_version = NET_CDDB_VERSION;
146   -
147   - $this->_buffer = '';
148   - $this->_status_buffer = '';
149   -
150   - $this->_buffer = '';
151   - $this->_status_buffer = '';
152   -
153   - $this->_multi_line_ident = 'until terminating';
154   - }
155   -
156   - /**
157   - * Tell whether or not a CDDBP response is a multi-line response
158   - *
159   - * @access private
160   - *
161   - * @param string $str_first_line The first line of the response
162   - * @return bool
163   - */
164   - function _isMultiLineResponse($str_first_line)
165   - {
166   - return false !== strpos($str_first_line, $this->_multi_line_ident);
167   - }
168   -
169   - /**
170   - * Connect to the CDDBP server
171   - *
172   - * @access public
173   - *
174   - * @return bool
175   - */
176   - function connect()
177   - {
178   - $this->_conn = new Net_Socket();
179   -
180   - if (true == ($err = $this->_conn->connect($this->_server, $this->_port))) {
181   -
182   - // Read welcome banner
183   - $this->_conn->readLine();
184   -
185   - // Handshake/login
186   - $this->_conn->writeLine('cddb hello ' . $this->_user . ' ' . $this->_host . ' ' . $this->_client . ' ' . $this->_version);
187   - $response = $this->_conn->readLine();
188   -
189   - // Change protocol levels
190   - $this->_conn->writeLine('proto ' . NET_CDDB_PROTO_LEVEL);
191   - $this->_conn->readLine();
192   -
193   - switch((int) substr($response, 0, 3)) {
194   - case NET_CDDB_RESPONSE_OK: // 200, Handshake OK
195   - case NET_CDDB_RESPONSE_SERVER_ALREADY: // 402, Already shook hands...?
196   - return true;
197   - case NET_CDDB_RESPONSE_SERVER_BADHANDSHAKE: // 431, Handshake not OK, closing connection
198   - default:
199   - return false;
200   - }
201   - } else {
202   - $this->_conn = null;
203   - return $err;
204   - }
205   - }
206   -
207   - /**
208   - * Tell whether or not a connection has been established
209   - *
210   - * @access public
211   - *
212   - * @return bool
213   - */
214   - function connected()
215   - {
216   - return !is_null($this->_conn);
217   - }
218   -
219   - /**
220   - * Send a query to the CDDB server
221   - *
222   - * If a connection has not yet been established, it will try to connect
223   - * before sending any data. Otherwise, the established connection will be
224   - * used.
225   - *
226   - * @access public
227   - *
228   - * @param string $query
229   - * @return void
230   - */
231   - function send($query, $try_again = true)
232   - {
233   - if (!$this->connected()) {
234   - $this->connect();
235   - }
236   -
237   - if ($this->connected()) {
238   -
239   - $char = ' '; // Stores character by character read
240   - $response = ''; // Stores entire response
241   -
242   - // Send the command
243   - $this->_conn->writeLine($query);
244   -
245   - // Read first (only?) line of response
246   - $response = $this->_conn->readLine() . "\n";
247   -
248   - // If response has more than one line, keep on reading
249   - // (We can't just use the Net_Socket::readAll() method here because
250   - // it hangs - the data is terminated by a '.', not an EOF, so we just
251   - // need to read until we find the '.')
252   - if ($this->_isMultiLineResponse($response)) {
253   -
254   - $last_char = ' ';
255   - while (true) {
256   -
257   - $char = $this->_conn->read(1);
258   - if ($char == "." and $last_char == "\n") {
259   - break;
260   - } else {
261   - $last_char = $char;
262   - }
263   -
264   - $response = $response . $char;
265   - }
266   - }
267   -
268   - // Get rid of leading newlines...?
269   - $response = trim($response);
270   -
271   - //p/rint('entire response: ' . "\n");
272   - //print($response);
273   - //print("\n" . '--------------------' . "\n");
274   -
275   - // If you are doing multiple queries then the CDDBP server may drop
276   - // the connection without warning and we'll get an empty response.
277   - // If this happens, we'll try to re-establish the connection and
278   - // send the query again (just once, so it won't get caught in an
279   - // infinite loop)
280   - if (!strlen($response) and $try_again)
281   - {
282   - $this->disconnect();
283   - return $this->send($query, false);
284   - }
285   - else if (!strlen($response))
286   - {
287   - $this->disconnect();
288   - return false;
289   - }
290   -
291   - if (false !== strpos($response, "\n")) { // Multi-line
292   - $this->_status_buffer = substr($response, 0, strpos($response, "\n"));
293   - $this->_buffer = substr($response, strpos($response, "\n") + 1);
294   - } else { // Single-line response
295   - $this->_status_buffer = $response;
296   - $this->_buffer = substr($response, 4);
297   - }
298   -
299   - return true;
300   - }
301   -
302   - return false;
303   - }
304   -
305   - /**
306   - * Recieve a string response from the CDDB server
307   - *
308   - * @access public
309   - *
310   - * @return string
311   - */
312   - function recieve()
313   - {
314   - $buffer = $this->_buffer;
315   - $this->_buffer = '';
316   - return $buffer;
317   - }
318   -
319   - /**
320   - * Get the integer status code the last response contained
321   - *
322   - * @access public
323   - *
324   - * @return integer
325   - */
326   - function status()
327   - {
328   - $status = (int) $this->_status_buffer;
329   - $this->_status_buffer = 0;
330   - return $status;
331   - }
332   -
333   - /**
334   - * Disconnect from the CDDBP server
335   - *
336   - * @access public
337   - *
338   - * @return bool
339   - */
340   - function disconnect()
341   - {
342   - //fclose($this->_conn);
343   - $this->_conn->disconnect();
344   - $this->_conn = null;
345   - return true;
346   - }
  42 + /**
  43 + * Connection resource to remove server
  44 + *
  45 + * @var resource
  46 + * @access protected
  47 + */
  48 + var $_conn;
  49 +
  50 + /**
  51 + * String buffer for recieving responses
  52 + *
  53 + * @var string
  54 + * @access protected
  55 + */
  56 + var $_buffer;
  57 +
  58 + /**
  59 + * String buffer for holding status line of last request
  60 + *
  61 + * @var string
  62 + * @access protected
  63 + */
  64 + var $_status_buffer;
  65 +
  66 + /**
  67 + * CDDB server to connect to
  68 + *
  69 + * @var string
  70 + * @access protected
  71 + */
  72 + var $_server;
  73 +
  74 + /**
  75 + * CDDB port to connect on
  76 + *
  77 + * @var integer
  78 + * @access protected
  79 + */
  80 + var $_port;
  81 +
  82 + /**
  83 + * User to connect to CDDB server as
  84 + *
  85 + * @var string
  86 + * @access protected
  87 + */
  88 + var $_user;
  89 +
  90 + /**
  91 + * Hostname of remote CDDB server
  92 + *
  93 + * @var string
  94 + * @access protected
  95 + */
  96 + var $_host;
  97 +
  98 + /**
  99 + * Client name to report to CDDB server
  100 + *
  101 + * @var string
  102 + * @access protected
  103 + */
  104 + var $_client;
  105 +
  106 + /**
  107 + * Client version string to report to CDDB server
  108 + *
  109 + * @var string
  110 + * @access protected
  111 + */
  112 + var $_version;
  113 +
  114 + /**
  115 + * String indicating CDDB server returned a multi-line response
  116 + *
  117 + * @var string
  118 + * @access protected
  119 + */
  120 + var $_multi_line_ident;
  121 +
  122 + /**
  123 + * Construct a CDDBP object
  124 + *
  125 + * @see Net_CDDB
  126 + * @see Net_CDDB_HTTP
  127 + *
  128 + * @param string $protocol DSN String
  129 + * @param array $option_params
  130 + * @return void
  131 + */
  132 + function Net_CDDB_Protocol_CDDBP($protocol, $option_params)
  133 + {
  134 + $protocol_params = $this->_parseDsn($protocol);
  135 +
  136 + $this->_conn = null;
  137 +
  138 + $this->_server = $protocol_params['host'];
  139 + $this->_port = $protocol_params['port'];
  140 + $this->_user = $protocol_params['user'];
  141 +
  142 + $this->_host = $option_params['host'];
  143 +
  144 + $this->_client = 'PHP/PEAR(' . get_class($this) . ')';
  145 + $this->_version = NET_CDDB_VERSION;
  146 +
  147 + $this->_buffer = '';
  148 + $this->_status_buffer = '';
  149 +
  150 + $this->_buffer = '';
  151 + $this->_status_buffer = '';
  152 +
  153 + $this->_multi_line_ident = 'until terminating';
  154 + }
  155 +
  156 + /**
  157 + * Tell whether or not a CDDBP response is a multi-line response
  158 + *
  159 + * @access private
  160 + *
  161 + * @param string $str_first_line The first line of the response
  162 + * @return bool
  163 + */
  164 + function _isMultiLineResponse($str_first_line)
  165 + {
  166 + return false !== strpos($str_first_line, $this->_multi_line_ident);
  167 + }
  168 +
  169 + /**
  170 + * Connect to the CDDBP server
  171 + *
  172 + * @access public
  173 + *
  174 + * @return bool
  175 + */
  176 + function connect()
  177 + {
  178 + $this->_conn = new Net_Socket();
  179 +
  180 + if (true == ($err = $this->_conn->connect($this->_server, $this->_port))) {
  181 +
  182 + // Read welcome banner
  183 + $this->_conn->readLine();
  184 +
  185 + // Handshake/login
  186 + $this->_conn->writeLine('cddb hello ' . $this->_user . ' ' . $this->_host . ' ' . $this->_client . ' ' . $this->_version);
  187 + $response = $this->_conn->readLine();
  188 +
  189 + // Change protocol levels
  190 + $this->_conn->writeLine('proto ' . NET_CDDB_PROTO_LEVEL);
  191 + $this->_conn->readLine();
  192 +
  193 + switch((int) substr($response, 0, 3)) {
  194 + case NET_CDDB_RESPONSE_OK: // 200, Handshake OK
  195 + case NET_CDDB_RESPONSE_SERVER_ALREADY: // 402, Already shook hands...?
  196 + return true;
  197 + case NET_CDDB_RESPONSE_SERVER_BADHANDSHAKE: // 431, Handshake not OK, closing connection
  198 + default:
  199 + return false;
  200 + }
  201 + } else {
  202 + $this->_conn = null;
  203 + return $err;
  204 + }
  205 + }
  206 +
  207 + /**
  208 + * Tell whether or not a connection has been established
  209 + *
  210 + * @access public
  211 + *
  212 + * @return bool
  213 + */
  214 + function connected()
  215 + {
  216 + return !is_null($this->_conn);
  217 + }
  218 +
  219 + /**
  220 + * Send a query to the CDDB server
  221 + *
  222 + * If a connection has not yet been established, it will try to connect
  223 + * before sending any data. Otherwise, the established connection will be
  224 + * used.
  225 + *
  226 + * @access public
  227 + *
  228 + * @param string $query
  229 + * @return void
  230 + */
  231 + function send($query, $try_again = true)
  232 + {
  233 + if (!$this->connected()) {
  234 + $this->connect();
  235 + }
  236 +
  237 + if ($this->connected()) {
  238 +
  239 + $char = ' '; // Stores character by character read
  240 + $response = ''; // Stores entire response
  241 +
  242 + // Send the command
  243 + $this->_conn->writeLine($query);
  244 +
  245 + // Read first (only?) line of response
  246 + $response = $this->_conn->readLine() . "\n";
  247 +
  248 + // If response has more than one line, keep on reading
  249 + // (We can't just use the Net_Socket::readAll() method here because
  250 + // it hangs - the data is terminated by a '.', not an EOF, so we just
  251 + // need to read until we find the '.')
  252 + if ($this->_isMultiLineResponse($response)) {
  253 +
  254 + $last_char = ' ';
  255 + while (true) {
  256 +
  257 + $char = $this->_conn->read(1);
  258 + if ($char == "." and $last_char == "\n") {
  259 + break;
  260 + } else {
  261 + $last_char = $char;
  262 + }
  263 +
  264 + $response = $response . $char;
  265 + }
  266 + }
  267 +
  268 + // Get rid of leading newlines...?
  269 + $response = trim($response);
  270 +
  271 + //p/rint('entire response: ' . "\n");
  272 + //print($response);
  273 + //print("\n" . '--------------------' . "\n");
  274 +
  275 + // If you are doing multiple queries then the CDDBP server may drop
  276 + // the connection without warning and we'll get an empty response.
  277 + // If this happens, we'll try to re-establish the connection and
  278 + // send the query again (just once, so it won't get caught in an
  279 + // infinite loop)
  280 + if (!strlen($response) and $try_again)
  281 + {
  282 + $this->disconnect();
  283 + return $this->send($query, false);
  284 + }
  285 + else if (!strlen($response))
  286 + {
  287 + $this->disconnect();
  288 + return false;
  289 + }
  290 +
  291 + if (false !== strpos($response, "\n")) { // Multi-line
  292 + $this->_status_buffer = substr($response, 0, strpos($response, "\n"));
  293 + $this->_buffer = substr($response, strpos($response, "\n") + 1);
  294 + } else { // Single-line response
  295 + $this->_status_buffer = $response;
  296 + $this->_buffer = substr($response, 4);
  297 + }
  298 +
  299 + return true;
  300 + }
  301 +
  302 + return false;
  303 + }
  304 +
  305 + /**
  306 + * Recieve a string response from the CDDB server
  307 + *
  308 + * @access public
  309 + *
  310 + * @return string
  311 + */
  312 + function recieve()
  313 + {
  314 + $buffer = $this->_buffer;
  315 + $this->_buffer = '';
  316 + return $buffer;
  317 + }
  318 +
  319 + /**
  320 + * Get the integer status code the last response contained
  321 + *
  322 + * @access public
  323 + *
  324 + * @return integer
  325 + */
  326 + function status()
  327 + {
  328 + $status = (int) $this->_status_buffer;
  329 + $this->_status_buffer = 0;
  330 + return $status;
  331 + }
  332 +
  333 + /**
  334 + * Disconnect from the CDDBP server
  335 + *
  336 + * @access public
  337 + *
  338 + * @return bool
  339 + */
  340 + function disconnect()
  341 + {
  342 + //fclose($this->_conn);
  343 + $this->_conn->disconnect();
  344 + $this->_conn = null;
  345 + return true;
  346 + }
347 347
348 348 }
349 349
350   -?>
  350 +?>
1,210 Net/CDDB/Protocol/Filesystem.php
@@ -35,16 +35,16 @@
35 35 * The FreeDB.org project provides database dumps of the entire CDDB/FreeDB
36 36 * database. The database downloads are usually provided in the following
37 37 * formats:
38   - * - .tar.bz2 (tarred and bzipped)
39   - * - .tar.7z (tarred and 7-zipped)
40   - * - .torrent (Bittorrent download)
  38 + * - .tar.bz2 (tarred and bzipped)
  39 + * - .tar.7z (tarred and 7-zipped)
  40 + * - .torrent (Bittorrent download)
41 41 *
42 42 * In order to use the database dumps (and thus this protocol) you need to
43 43 * download and extract one of the database dumps. The DSN provided to the
44 44 * {@link Net_CDDB_Client} should point to the directory of the database dumps:
45 45 * <code>
46   - * $proto = 'filesystem:///usr/home/keith/FreeDB Database/';
47   - * $client = new Net_CDDB_Client($proto, 'cddiscid:///dev/acd0');
  46 + * $proto = 'filesystem:///usr/home/keith/FreeDB Database/';
  47 + * $client = new Net_CDDB_Client($proto, 'cddiscid:///dev/acd0');
48 48 * </code>
49 49 *
50 50 * You probably don't want to use this protocol unless you have a very specific
@@ -60,8 +60,8 @@
60 60 * the 'stat_file' option. This is because it needs to count the number of files
61 61 * in each of the CDDB category directories (i.e.: count about 1.8 to 2-million
62 62 * files). These two parameters default to:
63   - * - use_stat_file = true
64   - * - stat_file = 'stat.db'
  63 + * - use_stat_file = true
  64 + * - stat_file = 'stat.db'
65 65 *
66 66 * When you first start using this protocol, you should create a 'stat.db' file
67 67 * in the CDDB database dump directory and chmod it so that its read/write by
@@ -78,603 +78,603 @@
78 78 */
79 79 class Net_CDDB_Protocol_Filesystem extends Net_CDDB_Protocol
80 80 {
81   - /**
82   - * The directory the FreeDB database dump is stored in
83   - *
84   - * @var string
85   - * @access protected
86   - */
87   - var $_dir;
88   -
89   - /**
90   - * Whether or not to use a 'Message of the Day' file for the 'motd' command
91   - *
92   - * @var boolean
93   - * @access protected
94   - */
95   - var $_use_motd_file;
96   -
97   - /**
98   - * The filename of the message of the day message (i.e.: motd.txt, which should be located in the FreeDB database dump directory)
99   - *
100   - * @var string
101   - * @access protected
102   - */
103   - var $_motd_file;
104   -
105   - /**
106   - * Whether or not to use a cached statistics file for the 'stat' command
107   - *
108   - * If you don't use a the cached statistics file, then the number of files
109   - * in each of the CDDB category directories needs to be counted each time
110   - * you issue a 'stat' command. For a full dump of the database, this can
111   - * take 10 or 20 *minutes*.
112   - *
113   - * @var boolean
114   - * @access protected
115   - */
116   - var $_use_stat_file;
117   -
118   - /**
119   - * The name of the statistics file (defaults to 'stat.db' in the CDDB database directory)
120   - *
121   - * @var string
122   - * @access protected
123   - */
124   - var $_stat_file;
125   -
126   - /**
127   - *
128   - * @todo Implement...
129   - * @var boolean
130   - * @access protected
131   - */
132   - var $_use_sites_file;
133   -
134   - /**
135   - * The filename of a file containing mirror site entries (i.e.: sites.txt)
136   - *
137   - * @var string
138   - * @access protected
139   - */
140   - var $_sites_file;
141   -
142   - /**
143   - * String buffer containing protocol data
144   - *
145   - * @var string
146   - * @access protected
147   - */
148   - var $_buffer;
149   -
150   - /**
151   - * Integer buffer containing protocol status information
152   - *
153   - * @var integer
154   - * @access protected
155   - */
156   - var $_status_buffer;
157   -
158   - /**
159   - * Constructor (PHP v4.x)
160   - *
161   - * @access public
162   - *
163   - * @see Net_CDDB_Protocol_Filesystem::__construct()
164   - */
165   - function Net_CDDB_Protocol_Filesystem($dsn = 'filesystem:///FreeDB', $options)
166   - {
167   - $this->__construct($dsn, $options);
168   - }
169   -
170   - /**
171   - * Constructor (PHP v5.x)
172   - *
173   - * @access public
174   - *
175   - * @param string $dsn
176   - * @param array $options
177   - */
178   - function __construct($dsn = 'filesystem:///FreeDB', $options)
179   - {
180   - $dsn_params = $this->_parseDsn($dsn);
181   -
182   - // Directory where the FreeDB database is stored
183   - $this->_dir = $dsn_params['path'];
184   -
185   - // Default parameter values
186   - $defaults = array(
187   - 'use_motd_file' => true,
188   - 'motd_file' => 'motd.txt',
189   - 'use_stat_file' => true,
190   - 'stat_file' => 'stat.db',
191   - 'use_sites_file' => false,
192   - 'sites_file' => 'sites.db',
193   - );
194   -
195   - $defaults = array_merge($defaults, $options);
196   -
197   - $this->_use_motd_file = (boolean) $defaults['use_motd_file'];
198   - $this->_motd_file = $defaults['motd_file'];
199   -
200   - $this->_use_stat_file = (boolean) $defaults['use_stat_file'];
201   - $this->_stat_file = $defaults['stat_file'];
202   -
203   - $this->_use_sites_file = (boolean) $defaults['use_sites_file'];
204   - $this->_sites_file = $defaults['sites_file'];
205   -
206   - // Initialize buffer and status buffer
207   - $this->_buffer = '';
208   - $this->_status_buffer = NET_CDDB_RESPONSE_ERROR_SYNTAX;
209   - }
210   -
211   - /**
212   - * Pretend to connect to a remote server while we actually just check if the database directory is readable
213   - *
214   - * Function will return false if either the filesystem directory does not
215   - * exist or if the directory is not readable.
216   - *
217   - * @access public
218   - *
219   - * @return boolean
220   - */
221   - function connect()
222   - {
223   - if (is_dir($this -> _dir) and is_readable($this -> _dir)) {
224   - return true;
225   - } else {
226   - return false;
227   - }
228   - }
229   -
230   - /**
231   - * Pretend to check if we are connected to a server
232   - *
233   - * @access public
234   - *
235   - * @return boolean
236   - */
237   - function connected()
238   - {
239   - return $this->connect();
240   - }
241   -
242   - /**
243   - * Send a query to the Net_CDDB_Protocol_Filesystem object, the query will be parsed and the buffers will be filled with the response
244   - *
245   - * Not all CDDB commands are implemented for this protocol, some don't make
246   - * sense in the context of a filesystem protocol and some just havn't been
247   - * implemented yet.
248   - *
249   - * This method basically parses and dispatches the query to other protected
250   - * methods of the class for further processing.
251   - *
252   - * @access public
253   - * @see Net_CDDB_Protocol_Filesystem::recieve()
254   - *
255   - * @param string $query
256   - * @return void
257   - */
258   - function send($query)
259   - {
260   - // First, break the query up into two parts, $cmd and $query
261   - // - $cmd holds the base command (i.e.: cddb read)
262   - // - $query holds the command parameters (i.e.: rock 7709a259)
263   - $cmd = trim($query);
264   -
265   - if (current($explode = explode(' ', $query)) == 'cddb') {
266   - $cmd = current($explode) . ' ' . next($explode);
267   - } else {
268   - $cmd = current($explode);
269   - }
270   -
271   - $query = trim(substr($query, strlen($cmd)));
272   -
273   - // Initial buffers
274   - $this->_buffer = '';
275   - $this->_status_buffer = NET_CDDB_RESPONSE_ERROR_SYNTAX; // 500 error by default
276   -
277   - $impl_cmds = array(
278   - 'cddb read' => '_cddbRead',
279   - 'cddb lscat' => '_cddbLscat',
280   - 'cddb query' => '_cddbQuery',
281   - 'discid' => '_discid',
282   - 'ver' => '_ver',
283   - 'cddb hello' => '_cddbHello',
284   - //'help' => '_help',
285   - 'motd' => '_motd',
286   - //'proto' => '_proto',
287   - 'quit' => '_quit',
288   - //'sites' => '_sites',
289   - 'stat' => '_stat',
290   - //'whom' => '_whom',
291   - );
292   -
293   - if (isset($impl_cmds[$cmd]) and method_exists($this, $impl_cmds[$cmd])) {
294   - $this->{$impl_cmds[$cmd]}($cmd, $query);
295   - return;
296   - } else {
297   - return;
298   - }
299   - }
300   -
301   - /**
302   - * Handle a CDDB 'discid' (Calculate a Disc ID) query and fill the buffer with a response
303   - *
304   - * @access protected
305   - *
306   - * @param string $cmd
307   - * @param string $query
308   - * @return void
309   - */
310   - function _discid($cmd, $query)
311   - {
312   - $track_offsets = explode(' ', $query);
313   - array_pop($track_offsets);
314   - array_shift($track_offsets);