XingeApp.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659
  1. <?php
  2. /*
  3. * Copyright ? 1998 - 2014 Tencent. All Rights Reserved. 腾讯公司 版权所有
  4. */
  5. class XingeApp {
  6. const DEVICE_ALL = 0;
  7. const DEVICE_BROWSER = 1;
  8. const DEVICE_PC = 2;
  9. const DEVICE_ANDROID = 3;
  10. const DEVICE_IOS = 4;
  11. const DEVICE_WINPHONE = 5;
  12. const IOSENV_PROD = 1;
  13. const IOSENV_DEV = 2;
  14. const IOS_MIN_ID = 2200000000;
  15. public function __construct($accessId, $secretKey)
  16. {
  17. assert(isset($accessId) && isset($secretKey));
  18. $this->accessId = $accessId;
  19. $this->secretKey = $secretKey;
  20. }
  21. public function __destruct(){}
  22. /**
  23. * 使用默认设置推送消息给单个android设备
  24. */
  25. public static function PushTokenAndroid($accessId, $secretKey, $title, $content, $token)
  26. {
  27. $push = new XingeApp($accessId, $secretKey);
  28. $mess = new PushMessage();
  29. $mess->setTitle($title);
  30. $mess->setContent($content);
  31. $mess->setType(PushMessage::TYPE_NOTIFICATION);
  32. $mess->setStyle(new Style(0, 1, 1, 1, 0));
  33. $action = new ClickAction();
  34. $action->setActionType(ClickAction::TYPE_ACTIVITY);
  35. $mess->setAction($action);
  36. $ret = $push->PushSingleDevice($token, $mess);
  37. return $ret;
  38. }
  39. /**
  40. * 使用默认设置推送消息给单个ios设备
  41. */
  42. public static function PushTokenIos($accessId, $secretKey, $content, $token, $environment)
  43. {
  44. $push = new XingeApp($accessId, $secretKey);
  45. $mess = new MessageIOS();
  46. $mess->setAlert($content);
  47. $ret = $push->PushSingleDevice($token, $mess, $environment);
  48. return $ret;
  49. }
  50. /**
  51. * 使用默认设置推送消息给单个android版账户
  52. */
  53. public static function PushAccountAndroid($accessId, $secretKey, $title, $content, $account)
  54. {
  55. $push = new XingeApp($accessId, $secretKey);
  56. $mess = new PushMessage();
  57. $mess->setTitle($title);
  58. $mess->setContent($content);
  59. $mess->setType(PushMessage::TYPE_NOTIFICATION);
  60. $mess->setStyle(new Style(0, 1, 1, 1, 0));
  61. $action = new ClickAction();
  62. $action->setActionType(ClickAction::TYPE_ACTIVITY);
  63. $mess->setAction($action);
  64. $ret = $push->PushSingleAccount(0, $account, $mess);
  65. return $ret;
  66. }
  67. /**
  68. * 使用默认设置推送消息给单个ios版账户
  69. */
  70. public static function PushAccountIos($accessId, $secretKey, $content, $account, $environment)
  71. {
  72. $push = new XingeApp($accessId, $secretKey);
  73. $mess = new MessageIOS();
  74. $mess->setAlert($content);
  75. $ret = $push->PushSingleAccount(0, $account, $mess, $environment);
  76. return $ret;
  77. }
  78. /**
  79. * 使用默认设置推送消息给所有设备android版
  80. */
  81. public static function PushAllAndroid($accessId, $secretKey, $title, $content)
  82. {
  83. $push = new XingeApp($accessId, $secretKey);
  84. $mess = new PushMessage();
  85. $mess->setTitle($title);
  86. $mess->setContent($content);
  87. $mess->setType(PushMessage::TYPE_NOTIFICATION);
  88. $mess->setStyle(new Style(0, 1, 1, 1, 0));
  89. $action = new ClickAction();
  90. $action->setActionType(ClickAction::TYPE_ACTIVITY);
  91. $mess->setAction($action);
  92. $ret = $push->PushAllDevices(0, $mess);
  93. return $ret;
  94. }
  95. /**
  96. * 使用默认设置推送消息给所有设备ios版
  97. */
  98. public static function PushAllIos($accessId, $secretKey, $content, $environment)
  99. {
  100. $push = new XingeApp($accessId, $secretKey);
  101. $mess = new MessageIOS();
  102. $mess->setAlert($content);
  103. $ret = $push->PushAllDevices(0, $mess, $environment);
  104. return $ret;
  105. }
  106. /**
  107. * 使用默认设置推送消息给标签选中设备android版
  108. */
  109. public static function PushTagAndroid($accessId, $secretKey, $title, $content, $tag)
  110. {
  111. $push = new XingeApp($accessId, $secretKey);
  112. $mess = new PushMessage();
  113. $mess->setTitle($title);
  114. $mess->setContent($content);
  115. $mess->setType(PushMessage::TYPE_NOTIFICATION);
  116. $mess->setStyle(new Style(0, 1, 1, 1, 0));
  117. $action = new ClickAction();
  118. $action->setActionType(ClickAction::TYPE_ACTIVITY);
  119. $mess->setAction($action);
  120. $ret = $push->PushTags(0, array(0=>$tag), 'OR', $mess);
  121. return $ret;
  122. }
  123. /**
  124. * 使用默认设置推送消息给标签选中设备ios版
  125. */
  126. public static function PushTagIos($accessId, $secretKey, $content, $tag, $environment)
  127. {
  128. $push = new XingeApp($accessId, $secretKey);
  129. $mess = new MessageIOS();
  130. $mess->setAlert($content);
  131. $ret = $push->PushTags(0, array(0=>$tag), 'OR', $mess, $environment);
  132. return $ret;
  133. }
  134. /**
  135. * 推送消息给单个设备
  136. */
  137. public function PushSingleDevice($deviceToken, $message, $environment=0)
  138. {
  139. $ret = array('ret_code'=>-1, 'err_msg'=>'message not valid');
  140. if (!($message instanceof PushMessage) && !($message instanceof MessageIOS)) return $ret;
  141. if (!$this->ValidateMessageType($message))
  142. {
  143. $ret['err_msg'] = 'message type not fit accessId';
  144. return $ret;
  145. }
  146. if($message instanceof MessageIOS)
  147. {
  148. if($environment!=XingeApp::IOSENV_DEV && $environment!=XingeApp::IOSENV_PROD)
  149. {
  150. $ret['err_msg'] = "ios message environment invalid";
  151. return $ret;
  152. }
  153. }
  154. if(!$message->isValid()) return $ret;
  155. $params = array();
  156. $params['access_id'] = $this->accessId;
  157. $params['expire_time'] = $message->getExpireTime();
  158. $params['send_time'] = $message->getSendTime();
  159. if($message instanceof PushMessage) $params['multi_pkg'] = $message->getMultiPkg();
  160. $params['device_token'] = $deviceToken;
  161. $params['message_type'] = $message->getType();
  162. $params['message'] = $message->toJson();
  163. $params['timestamp'] = time();
  164. $params['environment'] = $environment;
  165. return $this->callRestful(self::RESTAPI_PUSHSINGLEDEVICE, $params);
  166. }
  167. /**
  168. * 推送消息给单个账户
  169. */
  170. public function PushSingleAccount($deviceType, $account, $message, $environment=0)
  171. {
  172. $ret = array('ret_code'=>-1);
  173. if (!is_int($deviceType) || $deviceType<0 || $deviceType >5)
  174. {
  175. $ret['err_msg'] = 'deviceType not valid';
  176. return $ret;
  177. }
  178. if (!is_string($account) || empty($account))
  179. {
  180. $ret['err_msg'] = 'account not valid';
  181. return $ret;
  182. }
  183. if (!($message instanceof PushMessage) && !($message instanceof MessageIOS))
  184. {
  185. $ret['err_msg'] = 'message is not android or ios';
  186. return $ret;
  187. }
  188. if (!$this->ValidateMessageType($message))
  189. {
  190. $ret['err_msg'] = 'message type not fit accessId';
  191. return $ret;
  192. }
  193. if($message instanceof MessageIOS)
  194. {
  195. if($environment!=XingeApp::IOSENV_DEV && $environment!=XingeApp::IOSENV_PROD)
  196. {
  197. $ret['err_msg'] = "ios message environment invalid";
  198. return $ret;
  199. }
  200. }
  201. if (!$message->isValid())
  202. {
  203. $ret['err_msg'] = 'message not valid';
  204. return $ret;
  205. }
  206. $params = array();
  207. $params['access_id'] = $this->accessId;
  208. $params['expire_time'] = $message->getExpireTime();
  209. $params['send_time'] = $message->getSendTime();
  210. if($message instanceof PushMessage)
  211. $params['multi_pkg'] = $message->getMultiPkg();
  212. $params['device_type'] = $deviceType;
  213. $params['account'] = $account;
  214. $params['message_type'] = $message->getType();
  215. $params['message'] = $message->toJson();
  216. $params['timestamp'] = time();
  217. $params['environment'] = $environment;
  218. return $this->callRestful(self::RESTAPI_PUSHSINGLEACCOUNT, $params);
  219. }
  220. /**
  221. * 推送消息给多个账户
  222. */
  223. public function PushAccountList($deviceType, $accountList, $message, $environment=0)
  224. {
  225. $ret = array('ret_code'=>-1);
  226. if (!is_int($deviceType) || $deviceType<0 || $deviceType >5)
  227. {
  228. $ret['err_msg'] = 'deviceType not valid';
  229. return $ret;
  230. }
  231. if (!is_array($accountList) || empty($accountList))
  232. {
  233. $ret['err_msg'] = 'accountList not valid';
  234. return $ret;
  235. }
  236. if (!($message instanceof PushMessage) && !($message instanceof MessageIOS))
  237. {
  238. $ret['err_msg'] = 'message is not android or ios';
  239. return $ret;
  240. }
  241. if (!$this->ValidateMessageType($message))
  242. {
  243. $ret['err_msg'] = 'message type not fit accessId';
  244. return $ret;
  245. }
  246. if($message instanceof MessageIOS)
  247. {
  248. if($environment!=XingeApp::IOSENV_DEV && $environment!=XingeApp::IOSENV_PROD)
  249. {
  250. $ret['err_msg'] = "ios message environment invalid";
  251. return $ret;
  252. }
  253. }
  254. if (!$message->isValid())
  255. {
  256. $ret['err_msg'] = 'message not valid';
  257. return $ret;
  258. }
  259. $params = array();
  260. $params['access_id'] = $this->accessId;
  261. $params['expire_time'] = $message->getExpireTime();
  262. if($message instanceof PushMessage)
  263. $params['multi_pkg'] = $message->getMultiPkg();
  264. $params['device_type'] = $deviceType;
  265. $params['account_list'] = json_encode($accountList);
  266. $params['message_type'] = $message->getType();
  267. $params['message'] = $message->toJson();
  268. $params['timestamp'] = time();
  269. $params['environment'] = $environment;
  270. return $this->callRestful(self::RESTAPI_PUSHACCOUNTLIST, $params);
  271. }
  272. /**
  273. * 推送消息给APP所有设备
  274. */
  275. public function PushAllDevices($deviceType, $message, $environment=0)
  276. {
  277. $ret = array('ret_code'=>-1, 'err_msg'=>'message not valid');
  278. if (!is_int($deviceType) || $deviceType<0 || $deviceType >5)
  279. {
  280. $ret['err_msg'] = 'deviceType not valid';
  281. return $ret;
  282. }
  283. if (!($message instanceof PushMessage) && !($message instanceof MessageIOS)) return $ret;
  284. if (!$this->ValidateMessageType($message))
  285. {
  286. $ret['err_msg'] = 'message type not fit accessId';
  287. return $ret;
  288. }
  289. if($message instanceof MessageIOS)
  290. {
  291. if($environment!=XingeApp::IOSENV_DEV && $environment!=XingeApp::IOSENV_PROD)
  292. {
  293. $ret['err_msg'] = "ios message environment invalid";
  294. return $ret;
  295. }
  296. }
  297. if(!$message->isValid()) return $ret;
  298. $params = array();
  299. $params['access_id'] = $this->accessId;
  300. $params['expire_time'] = $message->getExpireTime();
  301. $params['send_time'] = $message->getSendTime();
  302. if($message instanceof PushMessage) $params['multi_pkg'] = $message->getMultiPkg();
  303. $params['device_type'] = $deviceType;
  304. $params['message_type'] = $message->getType();
  305. $params['message'] = $message->toJson();
  306. $params['timestamp'] = time();
  307. $params['environment'] = $environment;
  308. if (!is_null($message->getLoopInterval()) && $message->getLoopInterval() > 0
  309. && !is_null($message->getLoopTimes()) && $message->getLoopTimes() > 0) {
  310. $params['loop_interval'] = $message->getLoopInterval();
  311. $params['loop_times'] = $message->getLoopTimes();
  312. }
  313. //var_dump($params);
  314. return $this->callRestful(self::RESTAPI_PUSHALLDEVICE, $params);
  315. }
  316. /**
  317. * 推送消息给指定tags的设备
  318. * 若要推送的tagList只有一项,则tagsOp应为OR
  319. */
  320. public function PushTags($deviceType, $tagList, $tagsOp, $message, $environment=0)
  321. {
  322. $ret = array('ret_code'=>-1, 'err_msg'=>'message not valid');
  323. if (!is_int($deviceType) || $deviceType<0 || $deviceType >5)
  324. {
  325. $ret['err_msg'] = 'deviceType not valid';
  326. return $ret;
  327. }
  328. if (!is_array($tagList) || empty($tagList))
  329. {
  330. $ret['err_msg'] = 'tagList not valid';
  331. return $ret;
  332. }
  333. if (!is_string($tagsOp) || ($tagsOp!='AND' && $tagsOp!='OR'))
  334. {
  335. $ret['err_msg'] = 'tagsOp not valid';
  336. return $ret;
  337. }
  338. if (!($message instanceof PushMessage) && !($message instanceof MessageIOS)) return $ret;
  339. if (!$this->ValidateMessageType($message))
  340. {
  341. $ret['err_msg'] = 'message type not fit accessId';
  342. return $ret;
  343. }
  344. if($message instanceof MessageIOS)
  345. {
  346. if($environment!=XingeApp::IOSENV_DEV && $environment!=XingeApp::IOSENV_PROD)
  347. {
  348. $ret['err_msg'] = "ios message environment invalid";
  349. return $ret;
  350. }
  351. }
  352. if(!$message->isValid()) return $ret;
  353. $params = array();
  354. $params['access_id'] = $this->accessId;
  355. $params['expire_time'] = $message->getExpireTime();
  356. $params['send_time'] = $message->getSendTime();
  357. if($message instanceof PushMessage) $params['multi_pkg'] = $message->getMultiPkg();
  358. $params['device_type'] = $deviceType;
  359. $params['message_type'] = $message->getType();
  360. $params['tags_list'] = json_encode($tagList);
  361. $params['tags_op'] = $tagsOp;
  362. $params['message'] = $message->toJson();
  363. $params['timestamp'] = time();
  364. $params['environment'] = $environment;
  365. if (!is_null($message->getLoopInterval()) && $message->getLoopInterval() > 0
  366. && !is_null($message->getLoopTimes()) && $message->getLoopTimes() > 0) {
  367. $params['loop_interval'] = $message->getLoopInterval();
  368. $params['loop_times'] = $message->getLoopTimes();
  369. }
  370. return $this->callRestful(self::RESTAPI_PUSHTAGS, $params);
  371. }
  372. /**
  373. * 创建批量推送任务
  374. */
  375. public function CreateMultipush($message, $environment=0)
  376. {
  377. $ret = array('ret_code'=>-1);
  378. if (!($message instanceof PushMessage) && !($message instanceof MessageIOS))
  379. {
  380. $ret['err_msg'] = 'message is not android or ios';
  381. return $ret;
  382. }
  383. if (!$this->ValidateMessageType($message))
  384. {
  385. $ret['err_msg'] = 'message type not fit accessId';
  386. return $ret;
  387. }
  388. if($message instanceof MessageIOS)
  389. {
  390. if($environment!=XingeApp::IOSENV_DEV && $environment!=XingeApp::IOSENV_PROD)
  391. {
  392. $ret['err_msg'] = "ios message environment invalid";
  393. return $ret;
  394. }
  395. }
  396. if (!$message->isValid())
  397. {
  398. $ret['err_msg'] = 'message not valid';
  399. return $ret;
  400. }
  401. $params = array();
  402. $params['access_id'] = $this->accessId;
  403. $params['expire_time'] = $message->getExpireTime();
  404. if($message instanceof PushMessage)
  405. $params['multi_pkg'] = $message->getMultiPkg();
  406. $params['message_type'] = $message->getType();
  407. $params['message'] = $message->toJson();
  408. $params['timestamp'] = time();
  409. $params['environment'] = $environment;
  410. return $this->callRestful(self::RESTAPI_CREATEMULTIPUSH, $params);
  411. }
  412. /**
  413. * 按帐号大批量推送
  414. */
  415. public function PushAccountListMultiple($pushId, $accountList)
  416. {
  417. $pushId = intval($pushId);
  418. $ret = array('ret_code'=>-1);
  419. if ($pushId <= 0)
  420. {
  421. $ret['err_msg'] = 'pushId not valid';
  422. return $ret;
  423. }
  424. if (!is_array($accountList) || empty($accountList))
  425. {
  426. $ret['err_msg'] = 'accountList not valid';
  427. return $ret;
  428. }
  429. $params = array();
  430. $params['access_id'] = $this->accessId;
  431. $params['push_id'] = $pushId;
  432. $params['account_list'] = json_encode($accountList);
  433. $params['timestamp'] = time();
  434. return $this->callRestful(self::RESTAPI_PUSHACCOUNTLISTMULTIPLE, $params);
  435. }
  436. /**
  437. * 按Token大批量推送
  438. */
  439. public function PushDeviceListMultiple($pushId, $deviceList)
  440. {
  441. $pushId = intval($pushId);
  442. $ret = array('ret_code'=>-1);
  443. if ($pushId <= 0)
  444. {
  445. $ret['err_msg'] = 'pushId not valid';
  446. return $ret;
  447. }
  448. if (!is_array($deviceList) || empty($deviceList))
  449. {
  450. $ret['err_msg'] = 'deviceList not valid';
  451. return $ret;
  452. }
  453. $params = array();
  454. $params['access_id'] = $this->accessId;
  455. $params['push_id'] = $pushId;
  456. $params['device_list'] = json_encode($deviceList);
  457. $params['timestamp'] = time();
  458. return $this->callRestful(self::RESTAPI_PUSHDEVICELISTMULTIPLE, $params);
  459. }
  460. /**
  461. * 查询消息推送状态
  462. * @param array $pushIdList pushId(string)数组
  463. */
  464. public function QueryPushStatus($pushIdList)
  465. {
  466. $ret = array('ret_code'=>-1);
  467. $idList = array();
  468. if (!is_array($pushIdList) || empty($pushIdList))
  469. {
  470. $ret['err_msg'] = 'pushIdList not valid';
  471. return $ret;
  472. }
  473. foreach ($pushIdList as $pushId)
  474. {
  475. $idList[] = array('push_id'=>$pushId);
  476. }
  477. $params = array();
  478. $params['access_id'] = $this->accessId;
  479. $params['push_ids'] = json_encode($idList);
  480. $params['timestamp'] = time();
  481. return $this->callRestful(self::RESTAPI_QUERYPUSHSTATUS, $params);
  482. }
  483. /**
  484. * 查询应用覆盖的设备数
  485. */
  486. public function QueryDeviceCount()
  487. {
  488. $params = array();
  489. $params['access_id'] = $this->accessId;
  490. $params['timestamp'] = time();
  491. return $this->callRestful(self::RESTAPI_QUERYDEVICECOUNT, $params);
  492. }
  493. /**
  494. * 查询应用标签
  495. */
  496. public function QueryTags($start=0, $limit=100)
  497. {
  498. $ret = array('ret_code'=>-1);
  499. if (!is_int($start) || !is_int($limit))
  500. {
  501. $ret['err_msg'] = 'start or limit not valid';
  502. return $ret;
  503. }
  504. $params = array();
  505. $params['access_id'] = $this->accessId;
  506. $params['start'] = $start;
  507. $params['limit'] = $limit;
  508. $params['timestamp'] = time();
  509. return $this->callRestful(self::RESTAPI_QUERYTAGS, $params);
  510. }
  511. /**
  512. * 查询标签下token数量
  513. */
  514. public function QueryTagTokenNum($tag)
  515. {
  516. $ret = array('ret_code'=>-1);
  517. if (!is_string($tag))
  518. {
  519. $ret['err_msg'] = 'tag is not valid';
  520. return $ret;
  521. }
  522. $params = array();
  523. $params['access_id'] = $this->accessId;
  524. $params['tag'] = $tag;
  525. $params['timestamp'] = time();
  526. return $this->callRestful(self::RESTAPI_QUERYTAGTOKENNUM, $params);
  527. }
  528. /**
  529. * 查询token的标签
  530. */
  531. public function QueryTokenTags($deviceToken)
  532. {
  533. $ret = array('ret_code'=>-1);
  534. if (!is_string($deviceToken))
  535. {
  536. $ret['err_msg'] = 'deviceToken is not valid';
  537. return $ret;
  538. }
  539. $params = array();
  540. $params['access_id'] = $this->accessId;
  541. $params['device_token'] = $deviceToken;
  542. $params['timestamp'] = time();
  543. return $this->callRestful(self::RESTAPI_QUERYTOKENTAGS, $params);
  544. }
  545. /**
  546. * 取消定时发送
  547. */
  548. public function CancelTimingPush($pushId)
  549. {
  550. $ret = array('ret_code'=>-1);
  551. if (!is_string($pushId) || empty($pushId))
  552. {
  553. $ret['err_msg'] = 'pushId not valid';
  554. return $ret;
  555. }
  556. $params = array();
  557. $params['access_id'] = $this->accessId;
  558. $params['push_id'] = $pushId;
  559. $params['timestamp'] = time();
  560. return $this->callRestful(self::RESTAPI_CANCELTIMINGPUSH, $params);
  561. }
  562. //json转换为数组
  563. protected function json2Array($json){
  564. $json=stripslashes($json);
  565. return json_decode($json,true);
  566. }
  567. protected function callRestful($url, $params)
  568. {
  569. $paramsBase = new ParamsBase($params);
  570. $sign = $paramsBase->generateSign(RequestBase::METHOD_POST, $url, $this->secretKey);
  571. $params['sign'] = $sign;
  572. $requestBase = new RequestBase();
  573. $ret = $this->json2Array($requestBase->exec($url, $params, RequestBase::METHOD_POST));
  574. return $ret;
  575. }
  576. private function ValidateToken($token)
  577. {
  578. if(($this->accessId) >= 2200000000) {
  579. return strlen($token) == 64;
  580. } else {
  581. return (strlen($token) == 40 || strlen($token) == 64);
  582. }
  583. }
  584. public function InitParams() {
  585. $params = array();
  586. $params['access_id'] = $this->accessId;
  587. $params['timestamp'] = time();
  588. return $params;
  589. }
  590. public function BatchSetTag($tagTokenPairs)
  591. {
  592. $ret = array('ret_code' => -1);
  593. foreach ($tagTokenPairs as $pair) {
  594. if (!($pair instanceof TagTokenPair)) {
  595. $ret['err_msg'] = 'tag-token pair type error!';
  596. return $ret;
  597. }
  598. if (!$this->ValidateToken($pair->token)) {
  599. $ret['err_msg'] = sprintf("invalid token %s", $pair->token);
  600. return $ret;
  601. }
  602. }
  603. $params = $this->InitParams();
  604. $tag_token_list = array();
  605. foreach ($tagTokenPairs as $pair) {
  606. array_push($tag_token_list, array($pair->tag, $pair->token));
  607. }
  608. $params['tag_token_list'] = json_encode($tag_token_list);
  609. return $this->callRestful(self::RESTAPI_BATCHSETTAG, $params);
  610. }
  611. public function BatchDelTag($tagTokenPairs)
  612. {
  613. $ret = array('ret_code' => -1);
  614. foreach ($tagTokenPairs as $pair) {
  615. if (!($pair instanceof TagTokenPair)) {
  616. $ret['err_msg'] = 'tag-token pair type error!';
  617. return $ret;
  618. }
  619. if (!$this->ValidateToken($pair->token)) {
  620. $ret['err_msg'] = sprintf("invalid token %s", $pair->token);
  621. return $ret;
  622. }
  623. }
  624. $params = $this->InitParams();
  625. $tag_token_list = array();
  626. foreach ($tagTokenPairs as $pair) {
  627. array_push($tag_token_list, array($pair->tag, $pair->token));
  628. }
  629. $params['tag_token_list'] = json_encode($tag_token_list);
  630. return $this->callRestful(self::RESTAPI_BATCHDELTAG, $params);
  631. }
  632. public function QueryInfoOfToken($deviceToken)
  633. {
  634. $ret = array('ret_code'=>-1);
  635. if (!is_string($deviceToken))
  636. {
  637. $ret['err_msg'] = 'deviceToken is not valid';
  638. return $ret;
  639. }
  640. $params = array();
  641. $params['access_id'] = $this->accessId;
  642. $params['device_token'] = $deviceToken;
  643. $params['timestamp'] = time();
  644. return $this->callRestful(self::RESTAPI_QUERYINFOOFTOKEN, $params);
  645. }
  646. public function QueryTokensOfAccount($account)
  647. {
  648. $ret = array('ret_code'=>-1);
  649. if (!is_string($account))
  650. {
  651. $ret['err_msg'] = 'account is not valid';
  652. return $ret;
  653. }
  654. $params = array();
  655. $params['access_id'] = $this->accessId;
  656. $params['account'] = $account;
  657. $params['timestamp'] = time();
  658. return $this->callRestful(self::RESTAPI_QUERYTOKENSOFACCOUNT, $params);
  659. }
  660. public function DeleteTokenOfAccount($account, $deviceToken)
  661. {
  662. $ret = array('ret_code'=>-1);
  663. if (!is_string($account) || !is_string($deviceToken))
  664. {
  665. $ret['err_msg'] = 'account or deviceToken is not valid';
  666. return $ret;
  667. }
  668. $params = array();
  669. $params['access_id'] = $this->accessId;
  670. $params['account'] = $account;
  671. $params['device_token'] = $deviceToken;
  672. $params['timestamp'] = time();
  673. return $this->callRestful(self::RESTAPI_DELETETOKENOFACCOUNT, $params);
  674. }
  675. public function DeleteAllTokensOfAccount($account)
  676. {
  677. $ret = array('ret_code'=>-1);
  678. if (!is_string($account))
  679. {
  680. $ret['err_msg'] = 'account is not valid';
  681. return $ret;
  682. }
  683. $params = array();
  684. $params['access_id'] = $this->accessId;
  685. $params['account'] = $account;
  686. $params['timestamp'] = time();
  687. return $this->callRestful(self::RESTAPI_DELETEALLTOKENSOFACCOUNT, $params);
  688. }
  689. private function ValidateMessageType($message)
  690. {
  691. if(($this->accessId) >= XingeApp::IOS_MIN_ID and $message instanceof MessageIOS)
  692. return true;
  693. else if(($this->accessId) < XingeApp::IOS_MIN_ID and $message instanceof PushMessage)
  694. return true;
  695. else
  696. return false;
  697. }
  698. public $accessId = ''; //应用的接入Id
  699. public $secretKey = ''; //应用的skey
  700. const RESTAPI_PUSHSINGLEDEVICE = 'http://openapi.xg.qq.com/v2/push/single_device';
  701. const RESTAPI_PUSHSINGLEACCOUNT = 'http://openapi.xg.qq.com/v2/push/single_account';
  702. const RESTAPI_PUSHACCOUNTLIST = 'http://openapi.xg.qq.com/v2/push/account_list';
  703. const RESTAPI_PUSHALLDEVICE = 'http://openapi.xg.qq.com/v2/push/all_device';
  704. const RESTAPI_PUSHTAGS = 'http://openapi.xg.qq.com/v2/push/tags_device';
  705. const RESTAPI_QUERYPUSHSTATUS = 'http://openapi.xg.qq.com/v2/push/get_msg_status';
  706. const RESTAPI_QUERYDEVICECOUNT = 'http://openapi.xg.qq.com/v2/application/get_app_device_num';
  707. const RESTAPI_QUERYTAGS = 'http://openapi.xg.qq.com/v2/tags/query_app_tags';
  708. const RESTAPI_CANCELTIMINGPUSH = 'http://openapi.xg.qq.com/v2/push/cancel_timing_task';
  709. const RESTAPI_BATCHSETTAG = 'http://openapi.xg.qq.com/v2/tags/batch_set';
  710. const RESTAPI_BATCHDELTAG = 'http://openapi.xg.qq.com/v2/tags/batch_del';
  711. const RESTAPI_QUERYTOKENTAGS = 'http://openapi.xg.qq.com/v2/tags/query_token_tags';
  712. const RESTAPI_QUERYTAGTOKENNUM = 'http://openapi.xg.qq.com/v2/tags/query_tag_token_num';
  713. const RESTAPI_CREATEMULTIPUSH = 'http://openapi.xg.qq.com/v2/push/create_multipush';
  714. const RESTAPI_PUSHACCOUNTLISTMULTIPLE = 'http://openapi.xg.qq.com/v2/push/account_list_multiple';
  715. const RESTAPI_PUSHDEVICELISTMULTIPLE = 'http://openapi.xg.qq.com/v2/push/device_list_multiple';
  716. const RESTAPI_QUERYINFOOFTOKEN = 'http://openapi.xg.qq.com/v2/application/get_app_token_info';
  717. const RESTAPI_QUERYTOKENSOFACCOUNT = 'http://openapi.xg.qq.com/v2/application/get_app_account_tokens';
  718. const RESTAPI_DELETETOKENOFACCOUNT = 'http://openapi.xg.qq.com/v2/application/del_app_account_tokens';
  719. const RESTAPI_DELETEALLTOKENSOFACCOUNT = 'http://openapi.xg.qq.com/v2/application/del_app_account_all_tokens';
  720. }
  721. class TagTokenPair {
  722. public function __construct($tag, $token)
  723. {
  724. $this->tag = strval($tag);
  725. $this->token = strval($token);
  726. }
  727. public function __destruct(){}
  728. public $tag;
  729. public $token;
  730. }
  731. class PushMessage {
  732. public function __construct()
  733. {
  734. $this->m_acceptTimes = array();
  735. $this->m_multiPkg = 0;
  736. $this->m_raw = "";
  737. $this->m_style = new Style(0);
  738. $this->m_action = new ClickAction();
  739. }
  740. public function __destruct(){}
  741. public function setTitle($title)
  742. {
  743. $this->m_title = $title;
  744. }
  745. public function setContent($content)
  746. {
  747. $this->m_content = $content;
  748. }
  749. public function setExpireTime($expireTime)
  750. {
  751. $this->m_expireTime = $expireTime;
  752. }
  753. public function getExpireTime()
  754. {
  755. return $this->m_expireTime;
  756. }
  757. public function setSendTime($sendTime)
  758. {
  759. $this->m_sendTime = $sendTime;
  760. }
  761. public function getSendTime()
  762. {
  763. return $this->m_sendTime;
  764. }
  765. public function addAcceptTime($acceptTime)
  766. {
  767. $this->m_acceptTimes[] = $acceptTime;
  768. }
  769. public function acceptTimeToJson()
  770. {
  771. $ret = array();
  772. foreach ($this->m_acceptTimes as $acceptTime)
  773. {
  774. $ret[] = $acceptTime->toArray();
  775. }
  776. return $ret;
  777. }
  778. /**
  779. * 消息类型
  780. * @param int $type 1:通知 2:透传消息
  781. */
  782. public function setType($type)
  783. {
  784. $this->m_type = $type;
  785. }
  786. public function getType()
  787. {
  788. return $this->m_type;
  789. }
  790. public function setMultiPkg($multiPkg)
  791. {
  792. $this->m_multiPkg = $multiPkg;
  793. }
  794. public function getMultiPkg()
  795. {
  796. return $this->m_multiPkg;
  797. }
  798. public function setStyle($style)
  799. {
  800. $this->m_style = $style;
  801. }
  802. public function setAction($action)
  803. {
  804. $this->m_action = $action;
  805. }
  806. public function setCustom($custom)
  807. {
  808. $this->m_custom = $custom;
  809. }
  810. public function setRaw($raw)
  811. {
  812. $this->m_raw = $raw;
  813. }
  814. public function getLoopInterval()
  815. {
  816. return $this->m_loopInterval;
  817. }
  818. public function setLoopInterval($loopInterval)
  819. {
  820. $this->m_loopInterval = $loopInterval;
  821. }
  822. public function getLoopTimes()
  823. {
  824. return $this->m_loopTimes;
  825. }
  826. public function setLoopTimes($loopTimes)
  827. {
  828. $this->m_loopTimes = $loopTimes;
  829. }
  830. public function toJson()
  831. {
  832. if(!empty($this->m_raw)) return $this->m_raw;
  833. $ret = array();
  834. if ($this->m_type == self::TYPE_NOTIFICATION)
  835. {
  836. $ret['title'] = $this->m_title;
  837. $ret['content'] = $this->m_content;
  838. $ret['accept_time'] = $this->acceptTimeToJson();
  839. $ret['builder_id'] = $this->m_style->getBuilderId();
  840. $ret['ring'] = $this->m_style->getRing();
  841. $ret['vibrate'] = $this->m_style->getVibrate();
  842. $ret['clearable'] = $this->m_style->getClearable();
  843. $ret['n_id'] = $this->m_style->getNId();
  844. if (!is_null($this->m_style->getRingRaw())) {
  845. $ret['ring_raw'] = $this->m_style->getRingRaw();
  846. }
  847. $ret['lights'] = $this->m_style->getLights();
  848. $ret['icon_type'] = $this->m_style->getIconType();
  849. if (!is_null($this->m_style->getIconRes())) {
  850. $ret['icon_res'] = $this->m_style->getIconRes();
  851. }
  852. $ret['style_id'] = $this->m_style->getStyleId();
  853. if (!is_null($this->m_style->getSmallIcon())) {
  854. $ret['small_icon'] = $this->m_style->getSmallIcon();
  855. }
  856. $ret['action'] = $this->m_action->toJson();
  857. }
  858. else if($this->m_type == self::TYPE_MESSAGE)
  859. {
  860. $ret['title'] = $this->m_title;
  861. $ret['content'] = $this->m_content;
  862. $ret['accept_time'] = $this->acceptTimeToJson();
  863. }
  864. $ret['custom_content'] = $this->m_custom;
  865. return json_encode($ret);
  866. }
  867. public function isValid()
  868. {
  869. if (is_string($this->m_raw) && !empty($this->raw)) return true;
  870. if(!isset($this->m_title))
  871. $this->m_title = "";
  872. else if(!is_string($this->m_title) || empty($this->m_title))
  873. return false;
  874. if(!isset($this->m_content))
  875. $this->m_content = "";
  876. else if(!is_string($this->m_content) || empty($this->m_content))
  877. return false;
  878. if(!is_int($this->m_type) || $this->m_type<self::TYPE_NOTIFICATION || $this->m_type>self::TYPE_MESSAGE) return false;
  879. if(!is_int($this->m_multiPkg) || $this->m_multiPkg<0 || $this->m_multiPkg>1) return false;
  880. if($this->m_type == self::TYPE_NOTIFICATION)
  881. {
  882. if(!($this->m_style instanceof Style) || !($this->m_action instanceof ClickAction))
  883. return false;
  884. if(!$this->m_style->isValid() || !$this->m_action->isValid())
  885. return false;
  886. }
  887. if (isset($this->m_expireTime))
  888. {
  889. if(!is_int($this->m_expireTime) || $this->m_expireTime>3*24*60*60)
  890. return false;
  891. }
  892. else
  893. {
  894. $this->m_expireTime = 0;
  895. }
  896. if(isset($this->m_sendTime))
  897. {
  898. if(strtotime($this->m_sendTime)===false) return false;
  899. }
  900. else
  901. {
  902. $this->m_sendTime = "2013-12-19 17:49:00";
  903. }
  904. foreach ($this->m_acceptTimes as $value)
  905. {
  906. if(!($value instanceof TimeInterval) || !$value->isValid())
  907. return false;
  908. }
  909. if(isset($this->m_custom))
  910. {
  911. if(!is_array($this->m_custom))
  912. return false;
  913. }
  914. else
  915. {
  916. $this->m_custom = array();
  917. }
  918. if(isset($this->m_loopInterval)) {
  919. if(!(is_int($this->m_loopInterval) && $this->m_loopInterval > 0)) {
  920. return false;
  921. }
  922. }
  923. if(isset($this->m_loopTimes)) {
  924. if(!(is_int($this->m_loopTimes) && $this->m_loopTimes > 0)) {
  925. return false;
  926. }
  927. }
  928. if(isset($this->m_loopInterval) && isset($this->m_loopTimes)) {
  929. if(($this->m_loopTimes - 1) * $this->m_loopInterval + 1 > self::MAX_LOOP_TASK_DAYS) {
  930. return false;
  931. }
  932. }
  933. return true;
  934. }
  935. private $m_title;
  936. private $m_content;
  937. private $m_expireTime;
  938. private $m_sendTime;
  939. private $m_acceptTimes;
  940. private $m_type;
  941. private $m_multiPkg;
  942. private $m_style;
  943. private $m_action;
  944. private $m_custom;
  945. private $m_raw;
  946. private $m_loopInterval;
  947. private $m_loopTimes;
  948. const TYPE_NOTIFICATION = 1;
  949. const TYPE_MESSAGE = 2;
  950. const MAX_LOOP_TASK_DAYS = 15;
  951. }
  952. class MessageIOS
  953. {
  954. public function __construct()
  955. {
  956. $this->m_acceptTimes = array();
  957. }
  958. public function __destruct(){}
  959. public function setExpireTime($expireTime)
  960. {
  961. $this->m_expireTime = $expireTime;
  962. }
  963. public function getExpireTime()
  964. {
  965. return $this->m_expireTime;
  966. }
  967. public function setSendTime($sendTime)
  968. {
  969. $this->m_sendTime = $sendTime;
  970. }
  971. public function getSendTime()
  972. {
  973. return $this->m_sendTime;
  974. }
  975. public function addAcceptTime($acceptTime)
  976. {
  977. $this->m_acceptTimes[] = $acceptTime;
  978. }
  979. public function acceptTimeToJson()
  980. {
  981. $ret = array();
  982. foreach ($this->m_acceptTimes as $acceptTime)
  983. {
  984. $ret[] = $acceptTime->toArray();
  985. }
  986. return $ret;
  987. }
  988. public function setCustom($custom)
  989. {
  990. $this->m_custom = $custom;
  991. }
  992. public function setRaw($raw)
  993. {
  994. $this->m_raw = $raw;
  995. }
  996. public function setAlert($alert)
  997. {
  998. $this->m_alert = $alert;
  999. }
  1000. public function setBadge($badge)
  1001. {
  1002. $this->m_badge = $badge;
  1003. }
  1004. public function setSound($sound)
  1005. {
  1006. $this->m_sound = $sound;
  1007. }
  1008. public function getType()
  1009. {
  1010. return 0;
  1011. }
  1012. public function getCategory()
  1013. {
  1014. return $this->m_category;
  1015. }
  1016. public function setCategory($category)
  1017. {
  1018. $this->m_category = $category;
  1019. }
  1020. public function getLoopInterval()
  1021. {
  1022. return $this->m_loopInterval;
  1023. }
  1024. public function setLoopInterval($loopInterval)
  1025. {
  1026. $this->m_loopInterval = $loopInterval;
  1027. }
  1028. public function getLoopTimes()
  1029. {
  1030. return $this->m_loopTimes;
  1031. }
  1032. public function setLoopTimes($loopTimes)
  1033. {
  1034. $this->m_loopTimes = $loopTimes;
  1035. }
  1036. public function toJson()
  1037. {
  1038. if(!empty($this->m_raw)) return $this->m_raw;
  1039. $ret = $this->m_custom;
  1040. $aps = array();
  1041. $ret['accept_time'] = $this->acceptTimeToJson();
  1042. $aps['alert'] = $this->m_alert;
  1043. if(isset($this->m_badge)) $aps['badge'] = $this->m_badge;
  1044. if(isset($this->m_sound))$aps['sound'] = $this->m_sound;
  1045. if(isset($this->m_category))$aps['category'] = $this->m_category;
  1046. $ret['aps'] = $aps;
  1047. return json_encode($ret);
  1048. }
  1049. public function isValid()
  1050. {
  1051. if (isset($this->m_expireTime))
  1052. {
  1053. if(!is_int($this->m_expireTime) || $this->m_expireTime>3*24*60*60)
  1054. return false;
  1055. }
  1056. else
  1057. {
  1058. $this->m_expireTime = 0;
  1059. }
  1060. if(isset($this->m_sendTime))
  1061. {
  1062. if(strtotime($this->m_sendTime)===false) return false;
  1063. }
  1064. else
  1065. {
  1066. $this->m_sendTime = "2014-03-13 12:00:00";
  1067. }
  1068. if (!empty($this->m_raw))
  1069. {
  1070. if (is_string($this->m_raw))
  1071. return true;
  1072. else
  1073. return false;
  1074. }
  1075. foreach ($this->m_acceptTimes as $value)
  1076. {
  1077. if(!($value instanceof TimeInterval) || !$value->isValid())
  1078. return false;
  1079. }
  1080. if(isset($this->m_custom))
  1081. {
  1082. if(!is_array($this->m_custom))
  1083. return false;
  1084. }
  1085. else
  1086. {
  1087. $this->m_custom = array();
  1088. }
  1089. if(!isset($this->m_alert)) return false;
  1090. if(!is_string($this->m_alert) && !is_array($this->m_alert))
  1091. return false;
  1092. if(isset($this->m_badge))
  1093. {
  1094. if (!is_int($this->m_badge))
  1095. return false;
  1096. }
  1097. if(isset($this->m_sound))
  1098. {
  1099. if (!is_string($this->m_sound))
  1100. return false;
  1101. }
  1102. if(isset($this->m_loopInterval)) {
  1103. if(!(is_int($this->m_loopInterval) && $this->m_loopInterval > 0)) {
  1104. return false;
  1105. }
  1106. }
  1107. if(isset($this->m_loopTimes)) {
  1108. if(!(is_int($this->m_loopTimes) && $this->m_loopTimes > 0)) {
  1109. return false;
  1110. }
  1111. }
  1112. if(isset($this->m_loopInterval) && isset($this->m_loopTimes)) {
  1113. if(($this->m_loopTimes - 1) * $this->m_loopInterval + 1 > self::MAX_LOOP_TASK_DAYS) {
  1114. return false;
  1115. }
  1116. }
  1117. return true;
  1118. }
  1119. private $m_expireTime;
  1120. private $m_sendTime;
  1121. private $m_acceptTimes;
  1122. private $m_custom;
  1123. private $m_raw;
  1124. private $m_alert;
  1125. private $m_badge;
  1126. private $m_sound;
  1127. private $m_category;
  1128. private $m_loopInterval;
  1129. private $m_loopTimes;
  1130. const MAX_LOOP_TASK_DAYS = 15;
  1131. }
  1132. class ClickAction {
  1133. /**
  1134. * 动作类型
  1135. * @param int $actionType 1打开activity或app本身,2打开url,3打开Intent
  1136. */
  1137. public function __construct()
  1138. {
  1139. $this->m_atyAttrIntentFlag = 0;
  1140. $this->m_atyAttrPendingIntentFlag = 0;
  1141. $this->m_confirmOnPackageDownloadUrl = 1;
  1142. }
  1143. public function setActionType($actionType) {
  1144. $this->m_actionType = $actionType;
  1145. }
  1146. public function setUrl($url) {
  1147. $this->m_url = $url;
  1148. }
  1149. public function setComfirmOnUrl($comfirmOnUrl) {
  1150. $this->m_confirmOnUrl = $comfirmOnUrl;
  1151. }
  1152. public function setActivity($activity) {
  1153. $this->m_activity = $activity;
  1154. }
  1155. public function setIntent($intent) {
  1156. $this->m_intent = $intent;
  1157. }
  1158. public function setAtyAttrIntentFlag($atyAttrIntentFlag) {
  1159. $this->m_atyAttrIntentFlag = $atyAttrIntentFlag;
  1160. }
  1161. public function setAtyAttrPendingIntentFlag($atyAttrPendingIntentFlag) {
  1162. $this->m_atyAttrPendingIntentFlag = $atyAttrPendingIntentFlag;
  1163. }
  1164. public function setPackageDownloadUrl($packageDownloadUrl) {
  1165. $this->m_packageDownloadUrl = $packageDownloadUrl;
  1166. }
  1167. public function setConfirmOnPackageDownloadUrl($confirmOnPackageDownloadUrl) {
  1168. $this->m_confirmOnPackageDownloadUrl = $confirmOnPackageDownloadUrl;
  1169. }
  1170. public function setPackageName($packageName) {
  1171. $this->m_packageName = $packageName;
  1172. }
  1173. public function toJson()
  1174. {
  1175. $ret = array();
  1176. $ret['action_type'] = $this->m_actionType;
  1177. $ret['browser'] = array('url'=>$this->m_url, 'confirm'=>$this->m_confirmOnUrl);
  1178. $ret['activity'] = $this->m_activity;
  1179. $ret['intent'] = $this->m_intent;
  1180. $aty_attr = array();
  1181. if (isset($this->m_atyAttrIntentFlag)) {
  1182. $aty_attr['if'] = $this->m_atyAttrIntentFlag;
  1183. }
  1184. if (isset($this->m_atyAttrPendingIntentFlag)) {
  1185. $aty_attr['pf'] = $this->m_atyAttrPendingIntentFlag;
  1186. }
  1187. $ret['aty_attr'] = $aty_attr;
  1188. return $ret;
  1189. }
  1190. public function isValid()
  1191. {
  1192. if (!isset($this->m_actionType)) $this->m_actionType = self::TYPE_ACTIVITY;
  1193. if (!is_int($this->m_actionType)) return false;
  1194. if ($this->m_actionType<self::TYPE_ACTIVITY || $this->m_actionType>self::TYPE_INTENT)
  1195. return false;
  1196. if($this->m_actionType == self::TYPE_ACTIVITY)
  1197. {
  1198. if (!isset($this->m_activity))
  1199. {
  1200. $this->m_activity = "";
  1201. return true;
  1202. }
  1203. if (isset($this->m_atyAttrIntentFlag))
  1204. {
  1205. if (!is_int($this->m_atyAttrIntentFlag)) {
  1206. return false;
  1207. }
  1208. }
  1209. if (isset($this->m_atyAttrPendingIntentFlag))
  1210. {
  1211. if (!is_int($this->m_atyAttrPendingIntentFlag)) {
  1212. return false;
  1213. }
  1214. }
  1215. if (is_string($this->m_activity) && !empty($this->m_activity))
  1216. return true;
  1217. return false;
  1218. }
  1219. if($this->m_actionType == self::TYPE_URL)
  1220. {
  1221. if (is_string($this->m_url) && !empty($this->m_url) &&
  1222. is_int($this->m_confirmOnUrl) &&
  1223. $this->m_confirmOnUrl>=0 && $this->m_confirmOnUrl<=1
  1224. )
  1225. return true;
  1226. return false;
  1227. }
  1228. if($this->m_actionType == self::TYPE_INTENT)
  1229. {
  1230. if (is_string($this->m_intent) && !empty($this->m_intent))
  1231. return true;
  1232. return false;
  1233. }
  1234. }
  1235. private $m_actionType;
  1236. private $m_url;
  1237. private $m_confirmOnUrl;
  1238. private $m_activity;
  1239. private $m_intent;
  1240. private $m_atyAttrIntentFlag;
  1241. private $m_atyAttrPendingIntentFlag;
  1242. private $m_packageDownloadUrl;
  1243. private $m_confirmOnPackageDownloadUrl;
  1244. private $m_packageName;
  1245. const TYPE_ACTIVITY = 1;
  1246. const TYPE_URL = 2;
  1247. const TYPE_INTENT = 3;
  1248. }
  1249. class Style {
  1250. public function __construct($builderId, $ring=0, $vibrate=0, $clearable=1, $nId=0, $lights=1, $iconType=0, $styleId=1)
  1251. {
  1252. $this->m_builderId = $builderId;
  1253. $this->m_ring = $ring;
  1254. $this->m_vibrate = $vibrate;
  1255. $this->m_clearable = $clearable;
  1256. $this->m_nId = $nId;
  1257. $this->m_lights = $lights;
  1258. $this->m_iconType = $iconType;
  1259. $this->m_styleId = $styleId;
  1260. }
  1261. public function __destruct(){}
  1262. public function getBuilderId()
  1263. {
  1264. return $this->m_builderId;
  1265. }
  1266. public function getRing()
  1267. {
  1268. return $this->m_ring;
  1269. }
  1270. public function getVibrate()
  1271. {
  1272. return $this->m_vibrate;
  1273. }
  1274. public function getClearable()
  1275. {
  1276. return $this->m_clearable;
  1277. }
  1278. public function getNId()
  1279. {
  1280. return $this->m_nId;
  1281. }
  1282. public function getLights()
  1283. {
  1284. return $this->m_lights;
  1285. }
  1286. public function getIconType()
  1287. {
  1288. return $this->m_iconType;
  1289. }
  1290. public function getStyleId()
  1291. {
  1292. return $this->m_styleId;
  1293. }
  1294. public function setRingRaw($ringRaw)
  1295. {
  1296. return $this->m_ringRaw = $ringRaw;
  1297. }
  1298. public function getRingRaw()
  1299. {
  1300. return $this->m_ringRaw;
  1301. }
  1302. public function setIconRes($iconRes)
  1303. {
  1304. return $this->m_iconRes = $iconRes;
  1305. }
  1306. public function getIconRes() {
  1307. return $this->m_iconRes;
  1308. }
  1309. public function setSmallIcon($smallIcon)
  1310. {
  1311. return $this->m_smallIcon = $smallIcon;
  1312. }
  1313. public function getSmallIcon()
  1314. {
  1315. return $this->m_smallIcon;
  1316. }
  1317. public function isValid()
  1318. {
  1319. if (!is_int($this->m_builderId) || !is_int($this->m_ring) ||
  1320. !is_int($this->m_vibrate) || !is_int($this->m_clearable) ||
  1321. !is_int($this->m_lights) || !is_int($this->m_iconType) ||
  1322. !is_int($this->m_styleId)
  1323. )
  1324. return false;
  1325. if ($this->m_ring<0 || $this->m_ring>1) return false;
  1326. if ($this->m_vibrate<0 || $this->m_vibrate>1) return false;
  1327. if ($this->m_clearable<0 || $this->m_clearable>1) return false;
  1328. if ($this->m_lights<0 || $this->m_lights>1) return false;
  1329. if ($this->m_iconType<0 || $this->m_iconType>1) return false;
  1330. if ($this->m_styleId<0 || $this->m_styleId>1) return false;
  1331. return true;
  1332. }
  1333. private $m_builderId;
  1334. private $m_ring;
  1335. private $m_vibrate;
  1336. private $m_clearable;
  1337. private $m_nId;
  1338. private $m_ringRaw;
  1339. private $m_lights;
  1340. private $m_iconType;
  1341. private $m_iconRes;
  1342. private $m_styleId;
  1343. private $m_smallIcon;
  1344. }
  1345. class TimeInterval
  1346. {
  1347. public function __construct($startHour, $startMin, $endHour, $endMin)
  1348. {
  1349. $this->m_startHour = $startHour;
  1350. $this->m_startMin = $startMin;
  1351. $this->m_endHour = $endHour;
  1352. $this->m_endMin = $endMin;
  1353. }
  1354. public function __destruct(){}
  1355. public function toArray()
  1356. {
  1357. return array(
  1358. 'start' => array('hour'=>strval($this->m_startHour), 'min'=>strval($this->m_startMin)),
  1359. 'end' => array('hour'=>strval($this->m_endHour), 'min'=>strval($this->m_endMin))
  1360. );
  1361. }
  1362. public function isValid()
  1363. {
  1364. if (!is_int($this->m_startHour) || !is_int($this->m_startMin) ||
  1365. !is_int($this->m_endHour) || !is_int($this->m_endMin)
  1366. )
  1367. return false;
  1368. if ($this->m_startHour>=0 && $this->m_startHour<=23 &&
  1369. $this->m_startMin>=0 && $this->m_startMin<=59 &&
  1370. $this->m_endHour>=0 && $this->m_endHour<=23 &&
  1371. $this->m_endMin>=0 && $this->m_endMin<=59
  1372. )
  1373. return true;
  1374. else
  1375. return false;
  1376. }
  1377. private $m_startHour;
  1378. private $m_startMin;
  1379. private $m_endHour;
  1380. private $m_endMin;
  1381. }
  1382. class ParamsBase
  1383. {
  1384. /**
  1385. * @var array 当前传入的参数列表
  1386. */
  1387. public $_params = array();
  1388. /**
  1389. * 构造函数
  1390. */
  1391. public function __construct($params)
  1392. {
  1393. if(!is_array($params)){
  1394. return array();
  1395. }
  1396. foreach ($params as $key => $value) {
  1397. //如果是非法的key值,则不使用这个key
  1398. $this->_params[$key] = $value;
  1399. }
  1400. }
  1401. public function set($k, $v){
  1402. if(!isset($k) || !isset($v)){
  1403. return;
  1404. }
  1405. $this->_params[$k] = $v;
  1406. }
  1407. /**
  1408. * 根据实例化传入的参数生成签名
  1409. */
  1410. public function generateSign($method, $url, $secret_key)
  1411. {
  1412. //将参数进行升序排序
  1413. $param_str = '';
  1414. $method = strtoupper($method);
  1415. $url_arr = parse_url($url);
  1416. if(isset($url_arr['host']) && isset($url_arr['path'])){
  1417. $url = $url_arr['host'].$url_arr['path'];
  1418. }
  1419. if(!empty($this->_params)){
  1420. ksort($this->_params);
  1421. foreach ($this->_params as $key => $value) {
  1422. $param_str.=$key.'='.$value;
  1423. }
  1424. }
  1425. //print $method.$url.$param_str.$secret_key."\n";
  1426. return md5($method.$url.$param_str.$secret_key);
  1427. }
  1428. }
  1429. class RequestBase{
  1430. //get请求方式
  1431. const METHOD_GET = 'get';
  1432. //post请求方式
  1433. const METHOD_POST = 'post';
  1434. /**
  1435. * 发起一个get或post请求
  1436. * @param $url 请求的url
  1437. * @param int $method 请求方式
  1438. * @param array $params 请求参数
  1439. * @param array $extra_conf curl配置, 高级需求可以用, 如
  1440. * $extra_conf = array(
  1441. * CURLOPT_HEADER => true,
  1442. * CURLOPT_RETURNTRANSFER = false
  1443. * )
  1444. * @return bool|mixed 成功返回数据,失败返回false
  1445. * @throws Exception
  1446. */
  1447. public static function exec($url, $params = array(), $method = self::METHOD_GET, $extra_conf = array())
  1448. {
  1449. $params = is_array($params)? http_build_query($params): $params;
  1450. //如果是get请求,直接将参数附在url后面
  1451. if($method == self::METHOD_GET)
  1452. {
  1453. $url .= (strpos($url, '?') === false ? '?':'&') . $params;
  1454. }
  1455. //默认配置
  1456. $curl_conf = array(
  1457. CURLOPT_URL => $url, //请求url
  1458. CURLOPT_HEADER => false, //不输出头信息
  1459. CURLOPT_RETURNTRANSFER => true, //不输出返回数据
  1460. CURLOPT_CONNECTTIMEOUT => 3 // 连接超时时间
  1461. );
  1462. //配置post请求额外需要的配置项
  1463. if($method == self::METHOD_POST)
  1464. {
  1465. //使用post方式
  1466. $curl_conf[CURLOPT_POST] = true;
  1467. //post参数
  1468. $curl_conf[CURLOPT_POSTFIELDS] = $params;
  1469. }
  1470. //添加额外的配置
  1471. foreach($extra_conf as $k => $v)
  1472. {
  1473. $curl_conf[$k] = $v;
  1474. }
  1475. $data = false;
  1476. try
  1477. {
  1478. //初始化一个curl句柄
  1479. $curl_handle = curl_init();
  1480. //设置curl的配置项
  1481. curl_setopt_array($curl_handle, $curl_conf);
  1482. //发起请求
  1483. $data = curl_exec($curl_handle);
  1484. if($data === false)
  1485. {
  1486. throw new Exception('CURL ERROR: ' . curl_error($curl_handle));
  1487. }
  1488. }
  1489. catch(Exception $e)
  1490. {
  1491. echo $e->getMessage();
  1492. }
  1493. curl_close($curl_handle);
  1494. return $data;
  1495. }
  1496. }
  1497. ?>