[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 3 include_once 'Log.php'; 4 5 // Constant Definitions 6 define('IMSP_OCTET_COUNT', "/({)([0-9]{1,})(\}$)/"); 7 define('IMSP_MUST_USE_LITERAL', "/[\W]/i"); 8 9 /** 10 * The Net_IMSP class provides a common interface to an IMSP server . 11 * 12 * Required parameters:<pre> 13 * 'server' Hostname of IMSP server. 14 * 'port' Port of IMSP server.</pre> 15 * 16 * $Horde: framework/Net_IMSP/IMSP.php,v 1.13.10.19 2006/02/10 04:58:26 selsky Exp $ 17 * 18 * Copyright 2003-2006 Michael Rubinsky <mrubinsk@horde.org> 19 * 20 * See the enclosed file COPYING for license information (LGPL). If you 21 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 22 * 23 * @author Michael Rubinsky <mrubinsk@horde.org> 24 * @package Net_IMSP 25 */ 26 class Net_IMSP { 27 28 /** 29 * String containing name/IP address of IMSP host. 30 * 31 * @var string 32 */ 33 var $imsp_server = 'localhost'; 34 35 /** 36 * String containing port for IMSP server. 37 * 38 * @var string 39 */ 40 var $imsp_port = '406'; 41 42 /** 43 * Boolean to set if we should write to a log, if one is set up. 44 * 45 * @var boolean 46 */ 47 var $logEnabled = true; 48 49 /** 50 * String buffer containing the last raw NO or BAD response from the 51 * server. 52 * 53 * @var string 54 */ 55 var $lastRawError = ''; 56 57 // Private Declarations 58 var $_commandPrefix = 'A'; 59 var $_commandCount = 1; 60 var $_tag = ''; 61 var $_stream = null; 62 var $_lastCommandTag = 'undefined'; 63 var $_logger = null; 64 var $_logSet = null; 65 var $_logLevel = PEAR_LOG_INFO; 66 var $_logBuffer = array(); 67 68 /** 69 * Constructor function. 70 * 71 * @param array $params Hash containing server parameters. 72 */ 73 function Net_IMSP($params) 74 { 75 if (is_array($params) && !empty($params['server'])) { 76 $this->imsp_server = $params['server']; 77 } 78 79 if (is_array($params) && !empty($params['port'])) { 80 $this->imsp_port = $params['port']; 81 } 82 83 } 84 85 /** 86 * Initialization function to be called after object is returned. This 87 * allows errors to occur and not break the script. 88 * 89 * @return mixed True on success PEAR_Error on connection failure. 90 */ 91 function init() 92 { 93 $result = $this->imspOpen(); 94 95 if (is_a($result, 'PEAR_Error')) { 96 return $result; 97 } 98 $this->writeToLog('Initializing Net_IMSP object.', __FILE__, __LINE__, 99 PEAR_LOG_INFO); 100 return true; 101 } 102 103 /** 104 * Logs out of the server and closes the IMSP stream 105 */ 106 function logout() 107 { 108 $this->writeToLog('Closing IMSP Connection.', __FILE__, __LINE__, 109 PEAR_LOG_INFO); 110 $command_string = 'LOGOUT'; 111 $result = $this->imspSend($command_string); 112 if (is_a($result, 'PEAR_Error')) { 113 fclose($this->_stream); 114 return $result; 115 } else { 116 fclose($this->_stream); 117 return true; 118 } 119 } 120 121 /** 122 * Returns the raw capability response from the server. 123 * 124 * @return string The raw capability response. 125 */ 126 function capability() 127 { 128 $command_string = 'CAPABILITY'; 129 $result = $this->imspSend($command_string); 130 if (is_a($result, 'PEAR_Error')) { 131 return $result; 132 } else { 133 $server_response = $this->imspReceive(); 134 if (preg_match("/^\* CAPABILITY/", $server_response)) { 135 $capability = preg_replace("/^\* CAPABILITY/", 136 '', $server_response); 137 138 $server_response = $this->imspReceive(); //OK 139 140 if (!$server_response == 'OK') { 141 return $this->imspError('Did not receive the expected response from the server.', 142 __FILE__, __LINE__); 143 } else { 144 $this->writeToLog('CAPABILITY completed OK', __FILE__, 145 __LINE__, PEAR_LOG_INFO); 146 return $capability; 147 } 148 } 149 } 150 } 151 152 /** 153 * Attempts to open an IMSP socket with the server. 154 * 155 * @return mixed True on success PEAR_Error on failure. 156 */ 157 function imspOpen() 158 { 159 $fp = @fsockopen($this->imsp_server, $this->imsp_port); 160 if (!$fp) { 161 return $this->imspError('Connection to IMSP host failed.', __FILE__, 162 __LINE__); 163 } 164 $this->_stream = $fp; 165 $server_response = $this->imspReceive(); 166 if (!preg_match("/^\* OK/", $server_response)) { 167 fclose($fp); 168 return $this->imspError('Did not receive the expected response from the server.', __FILE__, __LINE__); 169 } 170 return true; 171 } 172 173 /** 174 * Attempts to send a command to the server. 175 * 176 * @param string $commandText Text to send to the server. 177 * @param boolean $includeTag Determines if command tag is prepended. 178 * @param boolean $sendCRLF Determines if CRLF is appended. 179 * @return mixed True on success PEAR_Error on failure. 180 */ 181 function imspSend($commandText, $includeTag=true, $sendCRLF=true) 182 { 183 $command_text = ''; 184 185 if (!$this->_stream) { 186 return $this->imspError('Connection to IMSP host failed.', __FILE__, __LINE__); 187 } 188 189 if ($includeTag) { 190 $this->_tag = $this->_getNextCommandTag(); 191 $command_text = "$this->_tag "; 192 } 193 194 $command_text .= $commandText; 195 196 if ($sendCRLF) { 197 $command_text .= "\r\n"; 198 } 199 200 $this->writeToLog('To: ' . $command_text, __FILE__, 201 __LINE__, PEAR_LOG_DEBUG); 202 203 if (!fputs($this->_stream, $command_text)) { 204 return $this->imspError('Connection to IMSP host failed.', __FILE__, __LINE__); 205 } else { 206 return true; 207 } 208 } 209 210 /** 211 * Receives a single CRLF terminated server response string 212 * 213 * @return mixed 'NO', 'BAD', 'OK', raw response or PEAR_Error. 214 */ 215 function imspReceive() 216 { 217 if (!$this->_stream) { 218 return $this->imspError('Connection to IMSP host failed.', __FILE__, __LINE__); 219 } 220 $result = fgets($this->_stream, 512); 221 if (!$result) { 222 return $this->imspError('Did not receive the expected response from the server.', 223 __FILE__, __LINE__); 224 } 225 $meta = stream_get_meta_data($this->_stream); 226 if ($meta['timed_out']) { 227 return $this->imspError('Connection to IMSP host failed.' . ': Connection timed out!', 228 __FILE__, __LINE__); 229 } 230 231 $server_response = trim($result); 232 $this->writeToLog('From: ' . $server_response, __FILE__, 233 __LINE__, PEAR_LOG_DEBUG); 234 235 /* Parse out the response: 236 * First make sure that this is not for a previous command. 237 * If it is, it means we did not read all the server responses from 238 * the last command...read them now, but throw an error. */ 239 while (preg_match("/^" . $this->_lastCommandTag 240 ."/", $server_response)) { 241 $server_response = 242 trim(fgets($this->_stream, 512)); 243 $this->imspError('Did not receive the expected response from the server.' . ": $server_response", 244 __FILE__, __LINE__); 245 } 246 247 $currentTag = $this->_tag; 248 if (preg_match("/^" . $currentTag . " NO/", $server_response)) { 249 $this->lastRawError = $server_response; 250 return 'NO'; 251 } 252 253 if (preg_match("/^" . $currentTag . " BAD/", $server_response)) { 254 $this->imspError('The IMSP server did not understand your request.', __FILE__, __LINE__); 255 $this->lastRawError = $server_response; 256 return 'BAD'; 257 } 258 259 if (preg_match("/^" . $currentTag . " OK/", $server_response)) { 260 return 'OK'; 261 } 262 263 /* If it was not a 'NO', 'BAD' or 'OK' response, 264 * then it's up to the calling function to decide 265 * what to do with it. */ 266 return $server_response; 267 } 268 269 /** 270 * Retrieves CRLF terminated response from server and splits it into 271 * an array delimited by a <space>. 272 * 273 * @return array result from explode(). 274 */ 275 function getServerResponseChunks() 276 { 277 $server_response = 278 trim(fgets($this->_stream, 512)); 279 $chunks = explode(' ', $server_response); 280 return $chunks; 281 } 282 283 /* 284 * Receives fixed number of bytes from IMSP socket. Used when 285 * server returns a string literal. 286 * 287 * @param integer $length Number of bytes to read from socket. 288 * 289 * @return string Text of string literal. 290 */ 291 function receiveStringLiteral($length) 292 { 293 $temp = trim(fread($this->_stream, $length)); 294 $this->writeToLog('From{}: ' . $temp, __FILE__, 295 __LINE__, PEAR_LOG_DEBUG); 296 return $temp; 297 } 298 299 /** 300 * Increments the IMSP command tag token. 301 * 302 * @access private 303 * @return string Next command tag. 304 */ 305 function _getNextCommandTag() 306 { 307 $this->_lastCommandTag = $this->_tag ? $this->_tag : 'undefined'; 308 return $this->_commandPrefix . sprintf('%04d', $this->_commandCount++); 309 } 310 311 /** 312 * Determines if a string needs to be quoted before sending to the server. 313 * 314 * @param string $string String to be tested. 315 * @return string Original string quoted if needed. 316 */ 317 function quoteSpacedString($string) 318 { 319 if (strpos($string, ' ') !== false) { 320 return '"' . $string . '"'; 321 } else { 322 return $string; 323 } 324 } 325 326 /** 327 * Raises an IMSP error. Basically, only writes 328 * error out to the horde logfile and returns PEAR_Error 329 * 330 * @param string $err Either PEAR_Error object or text to write to log. 331 * @param string $file File name where error occured. 332 * @param integer $line Line number where error occured. 333 */ 334 function imspError($err = '', $file=__FILE__, $line=__LINE__) 335 { 336 if (is_a($err, 'PEAR_Error')) { 337 $log_text = $err->getMessage(); 338 } else { 339 $log_text = $err; 340 } 341 342 $this->writeToLog($log_text, $file, $line, PEAR_LOG_ERR); 343 if (is_a($err, 'PEAR_Error')) { 344 return $err; 345 } else { 346 return PEAR::raiseError($err); 347 } 348 } 349 350 /** 351 * Writes a message to the IMSP logfile. 352 * 353 * @param string $message Text to write. 354 */ 355 function writeToLog($message, $file = __FILE__, 356 $line = __LINE__, $priority = PEAR_LOG_INFO) 357 { 358 if (($this->logEnabled) && ($this->_logSet)) { 359 if ($priority > $this->_logLevel) { 360 return; 361 } 362 363 $logMessage = '[imsp] ' . $message . ' [on line ' . $line . ' of "' . $file . '"]'; 364 $this->_logger->log($logMessage, $priority); 365 } elseif ((!$this->_logSet) && ($this->logEnabled)) { 366 $this->_logBuffer[] = array('message' => $message, 367 'priority' => $priority, 368 'file' => $file, 369 'line' => $line 370 ); 371 } 372 } 373 374 /** 375 * Creates a new Log object based on $params 376 * 377 * @param array $params Log object parameters. 378 * @return mixed True on success or PEAR_Error on failure. 379 */ 380 function setLogger($params) 381 { 382 $this->_logLevel = $params['priority']; 383 $logger = &Log::singleton($params['type'], $params['name'], 384 $params['ident'], $params['params']); 385 $this->_logSet = true; 386 387 if (is_a($logger, 'PEAR_Error')) { 388 $this->logEnabled = false; 389 return $logger; 390 } else { 391 $this->_logger = &$logger; 392 $this->logEnabled = true; 393 $this->_writeLogBuffer(); 394 return true; 395 } 396 } 397 398 /** 399 * Writes out contents of $_logBuffer to log file. Allows messages 400 * to be logged during initialization of object before Log object is 401 * instantiated. 402 * 403 * @access private 404 */ 405 function _writeLogBuffer() 406 { 407 for ($i = 0; $i < count($this->_logBuffer); $i++) { 408 $this->writeToLog($this->_logBuffer[$i]['message'], 409 $this->_logBuffer[$i]['file'], 410 $this->_logBuffer[$i]['line'], 411 $this->_logBuffer[$i]['priority']); 412 } 413 } 414 415 /** 416 * Attempts to create a Net_IMSP object based on $driver. 417 * Must be called as $imsp = &Net_IMSP::factory($driver, $params); 418 * 419 * @param string $driver Type of Net_IMSP object to return. 420 * @param mixed $params Any parameters needed by the Net_IMSP object. 421 * @return mixed The requested Net_IMSP object or PEAR_Error on failure. 422 */ 423 function &factory($driver, $params) 424 { 425 $driver = basename($driver); 426 if (empty($driver) || $driver == 'none') { 427 $imsp = &new Net_IMSP($params); 428 return $imsp; 429 } 430 431 include_once dirname(__FILE__) . '/IMSP/' . $driver . '.php'; 432 $class = 'Net_IMSP_' . $driver; 433 if (class_exists($class)) { 434 $imsp = &new $class($params); 435 return $imsp; 436 } else { 437 Horde::fatal(PEAR::raiseError(sprintf(_("Unable to load the definition of %s."), $class)), __FILE__, __LINE__); 438 } 439 } 440 441 /** 442 * Attempts to return a Net_IMSP object based on $driver. Only 443 * creates a new object if one with the same parameters already 444 * doesn't exist. 445 * Must be called as $imsp = &Net_IMSP::singleton($driver, $params); 446 * 447 * @param string $driver Type of Net_IMSP object to return. 448 * @param mixed $params Any parameters needed by the Net_IMSP object. 449 * @return mixed Reference to the Net_IMSP object or PEAR_Error on failure. 450 */ 451 function &singleton($driver, $params) 452 { 453 static $instances; 454 if (!isset($instances)) { 455 $instances = array(); 456 } 457 458 $signature = serialize(array($driver, $params)); 459 if (!isset($instances[$signature])) { 460 $instances[$signature] = &Net_IMSP::factory($driver, $params); 461 } 462 463 return $instances[$signature]; 464 } 465 466 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |