[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * Net_SMS_win_http Class implements the HTTP API for accessing the WIN 4 * (www.winplc.com) SMS gateway. 5 * 6 * Copyright 2003-2006 Marko Djukic <marko@oblo.com> 7 * 8 * See the enclosed file COPYING for license information (LGPL). If you did not 9 * receive this file, see http://www.fsf.org/copyleft/lgpl.html. 10 * 11 * $Horde: framework/Net_SMS/SMS/win_http.php,v 1.9.2.2 2006/01/01 21:28:29 jan Exp $ 12 * 13 * @author Marko Djukic <marko@oblo.com> 14 * @package Net_SMS 15 */ 16 class Net_SMS_win_http extends Net_SMS { 17 18 var $_base_url = 'gateway3.go2mobile.net:10030/gateway/v3/gateway.aspx'; 19 20 /** 21 * An array of capabilities, so that the driver can report which operations 22 * it supports and which it doesn't. Possible values are:<pre> 23 * auth - The gateway requires authentication before sending; 24 * batch - Batch sending is supported; 25 * multi - Sending of messages to multiple recipients is supported; 26 * receive - Whether this driver is capable of receiving SMS; 27 * credit - Is use of the gateway based on credits; 28 * addressbook - Are gateway addressbooks supported; 29 * lists - Gateway support for distribution lists. 30 * </pre> 31 * 32 * @var array 33 */ 34 var $capabilities = array('auth' => false, 35 'batch' => 100, 36 'multi' => true, 37 'receive' => false, 38 'credit' => false, 39 'addressbook' => false, 40 'lists' => false); 41 42 /** 43 * This function does the actual sending of the message. 44 * 45 * @access private 46 * 47 * @param array $message The array containing the message and its send 48 * parameters. 49 * @param string $to The destination string. 50 * 51 * @return array An array with the success status and additional 52 * information. 53 */ 54 function _send(&$message, $to) 55 { 56 /* Start the XML. */ 57 $xml = '<SMSMESSAGE><TEXT>' . $message['text'] . '</TEXT>'; 58 59 /* Check if source from is set. */ 60 if (!empty($message['send_params']['from'])) { 61 $xml .= '<SOURCE_ADDR>' . $message['send_params']['from'] . '</SOURCE_ADDR>'; 62 } 63 64 /* Loop through recipients and do some minimal validity checking. */ 65 if (is_array($to)) { 66 foreach ($to as $key => $val) { 67 if (preg_match('/^.*?<?(\+?\d{7,})(>|$)/', $val, $matches)) { 68 $to[$key] = $matches[1]; 69 } else { 70 /* If a recipient is invalid stop all further sending. */ 71 return array(0, sprintf(_("Invalid recipient: \"%s\""), $val)); 72 } 73 } 74 75 $to = implode('</DESTINATION_ADDR><DESTINATION_ADDR>', $to); 76 } else { 77 if (preg_match('/^.*?<?(\+?\d{7,})(>|$)/', $to, $matches)) { 78 $to = $matches[1]; 79 } else { 80 return array(0, sprintf(_("Invalid recipient: \"%s\""), $to)); 81 } 82 } 83 $xml .= '<DESTINATION_ADDR>' . $to . '</DESTINATION_ADDR>'; 84 85 /* TODO: Should we have something more intelligent? Could actually 86 * be part of send parameters. */ 87 $xml .= '<TRANSACTIONID>' . time() . '</TRANSACTIONID>'; 88 89 /* TODO: Add some extra tags, just tacked on for now. */ 90 $xml .= '<TYPEID>2</TYPEID><SERVICEID>1</SERVICEID></SMSMESSAGE>'; 91 92 /* Send this message. */ 93 $response = $this->_post($xml); 94 if (is_a($response, 'PEAR_Error')) { 95 return array(0, $response->getMessage()); 96 } 97 98 /* Parse the response, check for new lines in case of multiple 99 * recipients. */ 100 $lines = explode("\n", $response); 101 $response = array(); 102 103 if (count($lines) > 1) { 104 /* Multiple recipients. */ 105 foreach ($lines as $line) { 106 $parts = explode('To:', $line); 107 $recipient = trim($parts[1]); 108 if ($lines[0] == 'AQSMS-OK') { 109 $response[$recipient] = array(1, null); 110 } else { 111 $response[$recipient] = array(0, $lines[0]); 112 } 113 } 114 } else { 115 /* Single recipient. */ 116 if ($lines[0] == 'AQSMS-OK') { 117 $response[$to] = array(1, null); 118 } else { 119 $response[$to] = array(0, $lines[0]); 120 } 121 } 122 123 return $response; 124 } 125 126 /** 127 * Identifies this gateway driver and returns a brief description. 128 * 129 * @return array Array of driver info. 130 */ 131 function getInfo() 132 { 133 $info['name'] = _("WIN via HTTP"); 134 $info['desc'] = _("This driver allows sending of messages through the WIN (http://winplc.com) gateway, using the HTTP API"); 135 136 return $info; 137 } 138 139 /** 140 * Returns the required parameters for this gateway driver. The settable 141 * parameters for this gateway are: 142 * - user - The username for authentication on the gateway; 143 * - password - The password for authentication on the gateway; 144 * 145 * @return array Array of required parameters. 146 */ 147 function getParams() 148 { 149 $params = array(); 150 $params['user'] = array('label' => _("Username"), 'type' => 'text'); 151 $params['password'] = array('label' => _("Password"), 'type' => 'text'); 152 153 return $params; 154 } 155 156 /** 157 * Returns the parameters that can be set as default for sending messages 158 * using this gateway driver and displayed when sending messages. 159 * 160 * @return array Array of parameters that can be set as default. 161 */ 162 function getDefaultSendParams() 163 { 164 $params = array(); 165 $params['from'] = array( 166 'label' => _("Source address"), 167 'type' => 'text'); 168 169 $params['cost_id'] = array( 170 'label' => _("Cost ID"), 171 'type' => 'int'); 172 173 return $params; 174 } 175 176 /** 177 * Returns the parameters for sending messages using this gateway driver, 178 * displayed when sending messages. These are filtered out using the 179 * default values set up when creating the gateway. 180 * 181 * @return array Array of required parameters. 182 * @todo Would be nice to use a time/date setup rather than minutes from 183 * now for the delivery time. Upload field for ringtones/logos? 184 */ 185 function getSendParams($params) 186 { 187 if (empty($params['from'])) { 188 $params['from'] = array( 189 'label' => _("Source address"), 190 'type' => 'text'); 191 } 192 193 if (empty($params['cost_id'])) { 194 $params['deliv_time'] = array( 195 'label' => _("Cost ID"), 196 'type' => 'int'); 197 } 198 199 return $params; 200 } 201 202 /** 203 * Returns a string representation of an error code. 204 * 205 * @param integer $error The error code to look up. 206 * @param string $text An existing error text to use to raise a 207 * PEAR Error. 208 * 209 * @return mixed A textual message corresponding to the error code or a 210 * PEAR Error if passed an existing error text. 211 * 212 * @todo Check which of these are actually required and trim down the 213 * list. 214 */ 215 function getError($error, $error_text = '') 216 { 217 $error = trim($error); 218 219 /* An array of error codes returned by the gateway. */ 220 $errors = array( 221 'AQSMS-NOAUTHDETAILS' => _("No username and/or password sent."), 222 'AQSMS-DISTLISTUPDATEERROR' => _("There was an error updating the distribution list. Please try again later.")); 223 224 if (empty($error_text)) { 225 return $errors[$error]; 226 } else { 227 return PEAR::raiseError(sprintf($error_text, $errors[$error])); 228 } 229 } 230 231 /** 232 * Do the http call using a url passed to the function. 233 * 234 * @access private 235 * 236 * @param string $xml The XML information passed to the gateway. 237 * 238 * @return mixed The response on success or PEAR Error on failure. 239 */ 240 function _post($xml) 241 { 242 $options['method'] = 'POST'; 243 $options['timeout'] = 5; 244 $options['allowRedirects'] = true; 245 246 /* Wrap the xml with the standard tags. */ 247 $xml = '<?xml version="1.0" standalone="no"?><!DOCTYPE WIN_DELIVERY_2_SMS SYSTEM "winbound_messages_v1.dtd"><WIN_DELIVERY_2_SMS>' . $xml . '</WIN_DELIVERY_2_SMS>'; 248 249 if (!@include_once 'HTTP/Request.php') { 250 return PEAR::raiseError(_("Missing PEAR package HTTP_Request.")); 251 } 252 $http = &new HTTP_Request($this->_base_url, $options); 253 254 /* Add the authentication values to POST. */ 255 $http->addPostData('User', $this->_params['user']); 256 $http->addPostData('Password', $this->_params['password']); 257 258 /* Add the XML and send the request. */ 259 $http->addPostData('WIN_XML', $xml); 260 @$http->sendRequest(); 261 if ($http->getResponseCode() != 200) { 262 return PEAR::raiseError(sprintf(_("Could not open %s."), $this->_base_url)); 263 } 264 265 return $http->getResponseBody(); 266 } 267 268 }
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 |