<?php
/* Copyright 2010 princehaku
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Created on : 2010-11-18
* Last Update: 2011-02-24
* Author : princehaku
* Site : http://3haku.net
*
* 注意 : 请不要将此脚本用于商业用途..
* 此脚本完全是模拟了一个浏览器让用户自主操作,不存在破解或重打包客户端等行为。
* 此脚本以学习为目的,不涉及任何商业利益。任何企业和个人与此接口有关的商业行为,请与移动公司联系。
* 任何人使用此脚本而造成的不良后果,均由使用者承担,与此脚本的作者没有任何关系。
*/
define("DEBUG",0);
if(DEBUG==1){
error_reporting(9);
}
else
{
error_reporting(0);
}
/**用curl做的联网类
*/
class httpconnector{
/**Curl类
*
*/
private $curl;
/**cookie字符串
*/
private $cookie;
/**源(用于最后结果调试)
*/
private $sourceWmlStack=array();
/**得到源wml栈
*/
public function getSource(){
return $this->sourceWmlStack;
}
/**get方式下载网页内容
*@param $url
*@return web conntent
*/
public function get($url){
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_URL, $url);
// 设置header
curl_setopt($this->curl, CURLOPT_HEADER, 1);
curl_setopt ($this->curl, CURLOPT_COOKIE , $this->cookie);
// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
// 运行cURL,请求网页
$data = curl_exec($this->curl);
// 关闭URL请求
curl_close($this->curl);
//找到cookie 放入cookiestring
preg_match("/Set-Cookie:(.*?);/",$data,$r);
if(@$r[1]!=""){
if($this->cookie==""){
$this->cookie .= str_replace("\r\n","",$r[1]);
}
}
//放入调试栈
array_push($this->sourceWmlStack,$data);
return $data;
}
/**POST方式下载网页内容
*@param $url
*@param $params post的信息串
*@return web conntent
*/
public function post($url,$params){
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_URL, $url);
// 设置header
curl_setopt($this->curl, CURLOPT_HEADER, 1);
curl_setopt ($this->curl, CURLOPT_COOKIE , $this->cookie);
curl_setopt($this->curl, CURLOPT_POST, 1);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $params);
// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
// 运行cURL,请求网页
$data = curl_exec($this->curl);
// 关闭URL请求
curl_close($this->curl);
//找到cookie 放入cookiestring
preg_match("/Set-Cookie:(.*?);/",$data,$r);
if(@$r[1]!=""){
if($this->cookie==""){
$this->cookie .= str_replace("\r\n","",$r[1]);
}
}
//放入调试栈
array_push($this->sourceWmlStack,$data);
return $data;
}
}
/**飞信类
*/
class fection{
private $r;
private $httpconnector;
/**发送方手机号码
*/
private $phonenum;
/**发送方飞信密码
*/
private $pwd;
/**状态 (success|failed)
*/
private $statu;
/**当前进行的操作
*/
private $action;
function __construct($phonenum,$pwd){
$this->phonenum=$phonenum;
$this->pwd=$pwd;
$this->httpconnector=new httpconnector();
//得到ssid
$this->httpconnector->get('http://f.10086.cn/im/login/login.action');
}
/**登陆飞信
*/
function login(){
$this->action="login";
$data=$this->httpconnector->post('http://f.10086.cn/im/login/inputpasssubmit1.action',"loginstatus=2&m=$this->phonenum&pass=$this->pwd");
preg_match("/ontimer=\"(.*?)\"/",$data,$r);
$r=parse_url($r[1]);
$r=explode("=",$r['query']);
$this->t=$r['1'];
if($this->t==""){
$this->statu="failed";
return;
}
$this->statu="success";
}
/**退出飞信
*/
function logout(){
$this->httpconnector->get("http://f.10086.cn/im/index/logoutsubmit.action?t=$this->t");
}
/**发送飞信短信
*@param $fid 接收方的飞信id
*@param $msg 消息正文
*/
function send($fid,$msg){
if($this->statu=="failed"){
return;
}
$this->action="sendmessage";
$data=$this->httpconnector->post("http://f.10086.cn/im/chat/sendMsg.action?touserid=$fid","msg=$msg&touchTextLength=&touchTitle=");
if(strpos($data,"消息成功")==-1||strpos($data,"请正确输入")==-1){
$this->statu="failed";
return;
}
$this->statu="success";
}
/**添加用户为好友
*/
function addfriend($phone,$name){
if($this->statu=="failed"){
return;
}
$this->action="addfriend";
if($name=="")
{
$name="haku";
}
$data=$this->httpconnector->post("http://f.10086.cn/im/user/insertfriendsubmit.action","nickname=$name&number=$phone&type=0");
if(strpos($data,"对不起,")!=-1){
$this->statu="failed";
return;
}
$this->statu="success";
}
/**搜索好友
*用于将手机号或者飞信号或者昵称转换为飞信id
*param $phonenum
*/
function search($val){
if($this->statu=="failed"){
return;
}
$this->action="search";
$data=$this->httpconnector->post("http://f.10086.cn/im/index/searchOtherInfoList.action?t=$this->t","searchText=$val");
//找到飞信id
preg_match("/touserid=(.*?)&/",$data,$r);
if(@$r[1]==""){
$this->statu="failed";
return;
}
$this->statu="success";
return $r[1];
}
/**输出结果至xml
*
*/
function toXml(){
header("Content-Type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
echo "<pafetion>";
echo "<action>".$this->action."</action>";
echo "<statu>".$this->statu."</statu>";
if(DEBUG==1){
echo "<sources>";
foreach($this->httpconnector->getSource() as $i=>$j){
echo "<source><![CDATA[";
echo ($j);
echo "]]></source>";
}
echo "</sources>";
}
echo "</pafetion>";
}
}
$phone=@$_GET["phone"];
$pwd=@$_GET["pwd"];
$to=@$_GET["to"];
$msg=@$_GET['msg'];
$add=@$_GET['add'];
if(@$_GET['u']==""){
@$msg=iconv("gbk","UTF-8",$msg);
}
if(!($phone&&$pwd&&($msg&&$to||$add))){
die("参数错误 请参考网站<a href='http://3haku.net/tag/pafetion'>princehaku</a>");
}
$fection=new fection($phone,$pwd);
$fection->login();
//添加好友的操作
if($add!=""){
$res=explode(",",$add);
foreach($res as $i=>$adds){
$fection->addfriend($adds,@$_GET['name']);
}
}
else{
//发送短信的操作
$res=explode(",",$to);
foreach($res as $i=>$tos){
$fection->send($fection->search($tos),$msg);
}
}
$fection->logout();
//打印结果
$fection->toXml();
?>