<?php
/*
********************************************************************
** wPush. *
** Cesar D. Rodas <hide@address.com> *
** *
********************************************************************
** The author disclaims the copyright of this class. You are *
** legaly free to use, modify and redistribute the class. *
** Helps, bugs reports, and contributions could be done at *
** hide@address.com Also this email is a GTalk account, and *
** I can be contacted throught this way too. *
********************************************************************
*/
class wPush {
var $boundaryName;
var $Log;
/* privates vars!, do not access to it!. */
var $http;
var $arguments;
var $error;
/*
** the class contructor
*/
function wPush() {
$this->http=new http_class;
$this->http->timeout=0;
$this->http->data_timeout=0;
$this->http->debug=0;
$this->http->html_debug=0;
$this->http->user_agent="Saddor WAP Push Client (+http://www.phpclasses.org/wpush)";
$this->http->follow_redirect=1;
$this->http->redirection_limit=5;
$this->http->exclude_address="";
$this->http->request_method="POST";
$this->boundaryName = "testBoundary";
}
/*
** set the server information. It must by a valid URL
** like this (http|https)://push.proxy.url/direction
*/
function setServer($url) {
$this->error=$this->http->GetRequestArguments($url,$this->arguments);
$this->arguments["Headers"]["Content-Type"] = 'multipart/related; boundary='.$this->boundaryName.'; type="application/xml"';
}
/*
** set the wap push content. ;)
*/
function setContent($user, $pushid, $destiny, $url, $text) {
$body = '';
$body .= '--'.$this->boundaryName."\r\n";
$body .= 'Content-Type: application/xml'."\r\n\r\n";
$body .= '<?xml version="1.0"?>'."\r\n";
$body .= '<!DOCTYPE pap PUBLIC "-//WAPFORUM//DTD PAP 1.0//EN" "http://www.wapforum.org/DTD/pap_1.0.dtd">'."\r\n";
$body .= '<pap>'."\r\n";
$body .= '<push-message push-id="'.$pushid.'">'."\r\n";
$body .= '<address address-value="WAPPUSH='.$destiny.'/TYPE='.$user.'@'.$this->arguments['HostName'].'" />'."\r\n";
$body .= '</push-message>'."\r\n";
$body .= '</pap>'."\r\n\r\n";
$body .= '--'.$this->boundaryName."\r\n";
$body .= 'Content-Type: text/vnd.wap.si'."\r\n\r\n";
$body .= '<?xml version="1.0"?>'."\r\n";
$body .= '<!DOCTYPE si PUBLIC "-//WAPFORUM//DTD SI 1.0//EN" "http://www.wapforum.org/DTD/si.dtd">'."\r\n";
$body .= '<si>'."\r\n";
$body .= '<indication si-id='.$pushid.' href="'.$url.'">'."\r\n";
$body .= $text."\r\n";
$body .= '</indication>'."\r\n";
$body .= '</si>';
$this->arguments["Body"]= &$body;
$this->arguments'Headers']['Content-Length'] = strlen( $arguments["Body"] );
}
/*
** set proxy setup if it is needed.
*/
function setProxy($addr='',$port='',$user='',$pass='',$realm='',$workstation='') {
$this->arguments["ProxyHostName"]=$addr;
$this->arguments["ProxyHostPort"]=$port;
$this->arguments["ProxyUser"]=$user;
$this->arguments["ProxyPassword"]=$pass;
$this->arguments["ProxyRealm"]=$realm; // Proxy authentication realm or domain
$this->arguments["ProxyWorkstation"]=$workstation; // Workstation for NTLM proxy authentication
$this->http->proxy_authentication_mechanism=""; // force a given proxy authentication mechanism;
}
/*
** send the wap push ;)
*/
function Send() {
$this->error = $this->http->Open($this->arguments);
if ($this->error!="")
return false;
$this->error = $this->http->SendRequest($this->arguments);
if ($this->error!="")
return false;
$error=$this->http->ReadReplyHeaders($headers);
if ($this->error!="")
return false;
/*
** Af far I know, A wap-push has not a confimation, to avoid a high quantity
** of request per minutes, so i only check if the status is 200 (if the page exist)
** or not.
*/
if ($this->http->response_status != 200) {
$this->error = "Page not found. Page status: ".$this->http->response_status;
$this->http->Close();
return false;
}
$accBody="";
for(;;)
{
$error=$this->http->ReadReplyBody($body,1000);
if($error!="" || strlen($body)==0) break;
$accBody.=$body;
}
$this->Log[] = array(
'Query' => $this->http->request."\r\n".$this->pzImplode("\r\n",$this->http->request_headers)."\r\n\r\n".$this->http->request_body,
'Answer' => $this->pzImplode("\r\n",$headers)."\r\n\r\n".$accBody
);
$this->http->Close();
return true;
}
/*
** A re implementation of implode. used for log.
*/
function pzImplode($glue, $arr)
{
$ret = "";
if (!is_array($arr)) return "";
for(Reset($arr),$header=0;$header < count($arr);Next($arr),$header++)
{
$header_name=Key($arr);
if(GetType($arr[$header_name])=="array")
for($header_value=0;$header_value < count($arr[$header_name]);$header_value++)
$ret .= $header_name.": ".$arr[$header_name][$header_value].$glue;
else
$ret .= $header_name.": ".$arr[$header_name].$glue;
}
return $ret;
}
}
?>