ProduceRequest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. namespace ACES\Common;
  3. use ACES\Common\domain\JosBaseInfo;
  4. use ACES\TDEClient;
  5. use ACES\Core\MSG_LEVEL;
  6. use ACES\Core\MSG_TYPE;
  7. class ProduceRequest{
  8. private $accessToken;
  9. private $customerUserId;
  10. private $serverUrl;
  11. /**
  12. * @var BasicMessage
  13. */
  14. private $messages;
  15. /**
  16. * ProduceRequest constructor.
  17. * @param $accessToken string
  18. * @param $serverUrl string
  19. * @param $messages BasicMessage
  20. */
  21. public function __construct($accessToken, $serverUrl, $messages){
  22. // 区分accessToken和customerUserId
  23. if (isset($accessToken)) {
  24. if (stripos($accessToken, '_') === 0) {
  25. $split = explode('_', $accessToken);
  26. $this->customerUserId = $split[1];
  27. }else{
  28. $this->accessToken = $accessToken;
  29. }
  30. }
  31. $this->serverUrl = $serverUrl;
  32. $this->messages = $messages;
  33. }
  34. public static function getInitRequest($accessToken, $serverUrl, $service, $tid){
  35. // $messages = array();
  36. // $messages[] = new InitMessage($service, $tid);
  37. return new ProduceRequest($accessToken, $serverUrl, new InitMessage($service, $tid));
  38. }
  39. public static function getEventRequest($accessToken, $serverUrl, $service, $tid, $event_code, $event) {
  40. // $messages = array();
  41. // $messages[] = new EventMessage($service, $tid, $event_code, $event);
  42. return new ProduceRequest($accessToken, $serverUrl, new EventMessage($service, $tid, $event_code, $event));
  43. }
  44. public static function getKPEventRequest($accessToken, $serverUrl, $service, $tid, $event_code, $event, $major_kver, $keylist) {
  45. // $messages = array();
  46. // $messages[] = new KPEventMessage($service, $tid, $event_code, $event, $major_kver, $keylist);
  47. return new ProduceRequest($accessToken, $serverUrl, new KPEventMessage($service, $tid, $event_code, $event, $major_kver, $keylist));
  48. }
  49. public static function getErrorRequest($accessToken, $serverUrl, $service, $tid,$level, $err_code, $err_msg, $stack_trace) {
  50. // $messages = array();
  51. // $messages[] = new ErrorMessage($service, $tid, $level, $err_code, $err_msg, $stack_trace);
  52. return new ProduceRequest($accessToken, $serverUrl, new ErrorMessage($service, $tid, $level, $err_code, $err_msg, $stack_trace));
  53. }
  54. public static function getStatisticRequest($accessToken, $serverUrl, $service, $tid, $stat) {
  55. // $messages = array();
  56. // $messages[] = new StatisticMessage($service, $tid, $stat);
  57. return new ProduceRequest($accessToken, $serverUrl, new StatisticMessage($service, $tid, $stat));
  58. }
  59. // public function jsonSerialize()
  60. // {
  61. // $vars = get_object_vars($this);
  62. //
  63. // return $vars;
  64. // }
  65. /**
  66. * @param JosBaseInfo $josBaseInfo
  67. * @return array
  68. */
  69. public function toFormParams($josBaseInfo)
  70. {
  71. return $josBaseInfo->getFormParams($this);
  72. }
  73. public function to360buyParamJson()
  74. {
  75. $paramJson = array();
  76. if (isset($this->customerUserId)) {
  77. $paramJson['customer_user_id'] = $this->customerUserId;
  78. }else {
  79. $paramJson['access_token'] = $this->accessToken;
  80. }
  81. $paramJson['businessId'] = $this->messages->getBusinessId();
  82. $paramJson['text'] = $this->messages->getText();
  83. $paramJson['attribute'] = $this->messages->getAttributes();
  84. return json_encode($paramJson);
  85. }
  86. public function getJosMethod()
  87. {
  88. return 'jingdong.jos.secret.api.report.get';
  89. }
  90. }
  91. class BasicMessage {
  92. protected $businessId;
  93. protected $text;
  94. protected $attributes;
  95. // public function jsonSerialize()
  96. // {
  97. // $vars = get_object_vars($this);
  98. //
  99. // return $vars;
  100. // }
  101. public static function getRandomString(){
  102. $abc = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz=+-*/_|<>^~@?%&";
  103. $res = "";
  104. for($i = 0;$i<40;$i++){
  105. $r = rand(0,39);
  106. $res = $res.$abc[$r];
  107. }
  108. return $res;
  109. }
  110. /**
  111. * @return mixed
  112. */
  113. public function getBusinessId()
  114. {
  115. return $this->businessId;
  116. }
  117. /**
  118. * @return mixed
  119. */
  120. public function getText()
  121. {
  122. return $this->text;
  123. }
  124. /**
  125. * @return mixed
  126. */
  127. public function getAttributes()
  128. {
  129. $json = json_encode($this->attributes);
  130. return $json;
  131. }
  132. }
  133. class Environment {
  134. private static $hostInfo = NULL;
  135. private static $systemInfo = NULL;
  136. public static function getHost(){
  137. if(self::$hostInfo == NULL){
  138. try {
  139. $preg = "/((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))/";
  140. $out = NULL;
  141. $stats = NULL;
  142. if(PHP_OS == "Windows" || PHP_OS == "WINNT"){
  143. // windows get host info
  144. exec("ipconfig", $out, $stats);
  145. if (!empty($out)) {
  146. foreach ($out AS $row) {
  147. if (strstr($row, "IP") && strstr($row, ":") && !strstr($row, "IPv6")) {
  148. $tmpIp = explode(":", $row);
  149. if (filter_var(trim($tmpIp[1]), FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {
  150. self::$hostInfo = trim($tmpIp[1]);
  151. break;
  152. }else{
  153. self::$hostInfo = trim($tmpIp[1]);
  154. }
  155. }
  156. }
  157. }
  158. }elseif (PHP_OS == "Darwin"||PHP_OS == "Linux") {
  159. exec("ifconfig", $out, $stats);
  160. if (!empty($out)) {
  161. foreach ($out as $oneline){
  162. if(preg_match($preg, $oneline)){
  163. $tmp = preg_grep($preg, explode(" ", $oneline));
  164. $ip = current($tmp);
  165. if(strstr($ip, "addr:")){
  166. $ip_ = explode(":", $ip);
  167. $ip = $ip_[1];
  168. }
  169. if (isset($ip) && $ip != "127.0.0.1") {
  170. if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {
  171. self::$hostInfo = $ip;
  172. break;
  173. }else{
  174. self::$hostInfo = $ip;
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. if(self::$hostInfo == NULL){
  182. self::$hostInfo = "Unknown host";
  183. }
  184. } catch (\Exception $e) {
  185. self::$hostInfo = "Unknown host";
  186. }
  187. }
  188. return self::$hostInfo;
  189. }
  190. public static function getSystemInfo(){
  191. if(self::$systemInfo == NULL){
  192. try{
  193. $cpuout = NULL;
  194. $stats = NULL;
  195. $phpv = NULL;
  196. $version = "php version unknown";
  197. if(PHP_OS == "Linux"){
  198. exec("php -v",$phpv, $stats);
  199. if($phpv != NULL && sizeof($phpv)!=0){
  200. $version = $phpv[0];
  201. }else{
  202. $version = " PHP " . PHP_VERSION;
  203. }
  204. exec("cat /proc/cpuinfo | grep 'model name'|uniq", $cpuout, $stats);
  205. $cpuInfo = explode(":", $cpuout[0]);
  206. $cpuInfo = $cpuInfo[1];
  207. self::$systemInfo = PHP_OS."|".$version."|".$cpuInfo;
  208. // mac
  209. } elseif (PHP_OS == "Darwin"){
  210. exec("php -v",$phpv, $stats);
  211. if($phpv != NULL && sizeof($phpv)!=0){
  212. $version = $phpv[0];
  213. }
  214. exec("sysctl -n machdep.cpu.brand_string", $cpuout, $stats);
  215. $cpuInfo = $cpuout[0];
  216. self::$systemInfo = PHP_OS."|".$version."|".$cpuInfo;
  217. } elseif (PHP_OS == "Windows" || PHP_OS == "WINNT"){
  218. exec("php -v", $phpv,$stats);
  219. if($phpv != NULL && sizeof($phpv)!=0){
  220. $version = $phpv[0];
  221. }
  222. exec("wmic cpu get name", $cpuout, $stats);
  223. $cpuInfo = $cpuout[1];
  224. self::$systemInfo = PHP_OS."|".$version."|".$cpuInfo;
  225. }
  226. if(self::$systemInfo == NULL){
  227. self::$systemInfo = PHP_OS . "|" . " PHP " . PHP_VERSION;
  228. }
  229. } catch (\Exception $e){
  230. self::$systemInfo = PHP_OS . "|" . " PHP " . PHP_VERSION;
  231. }
  232. }
  233. return self::$systemInfo;
  234. }
  235. }
  236. class BasicAttribute {
  237. public $type;
  238. public $host;
  239. public $level;
  240. public $service;
  241. public $tid;
  242. public $sdk_ver;
  243. public $env;
  244. public $ts;
  245. public function __construct($type, $level, $service, $tid){
  246. $this->type = $type;
  247. $this->host = Environment::getHost();
  248. $this->level = $level;
  249. $this->service = $service;
  250. $this->tid = $tid;
  251. $this->sdk_ver = TDEClient::getSDKVer();
  252. $this->env = Environment::getSystemInfo();
  253. $this->ts = date_timestamp_get(new \DateTime()) * 1000;
  254. }
  255. // public function jsonSerialize()
  256. // {
  257. // $vars = get_object_vars($this);
  258. //
  259. // return $vars;
  260. // }
  261. }
  262. class EventAttribute extends BasicAttribute{
  263. public $code;
  264. public $event;
  265. public function __construct($type, $level, $service, $tid, $code, $event){
  266. parent::__construct($type, $level, $service, $tid);
  267. $this->code = $code;
  268. $this->event = $event;
  269. }
  270. // public function jsonSerialize()
  271. // {
  272. // $vars = get_object_vars($this);
  273. // return $vars;
  274. // }
  275. }
  276. class KPEventAttribute extends EventAttribute{
  277. public $cur_key;
  278. public $keylist;
  279. public function __construct($type, $level, $service, $tid, $code, $event, $major_kver, $keylist) {
  280. parent::__construct($type, $level, $service, $tid, $code, $event);
  281. $this->cur_key = $major_kver;
  282. $this->keylist = $keylist;
  283. }
  284. }
  285. class ErrorAttribute extends BasicAttribute {
  286. public $code;
  287. public $msg;
  288. public $heap;
  289. public function __construct($type, $level, $service, $tid, $err_code, $err_msg, $stacktrace) {
  290. parent::__construct($type, $level, $service, $tid);
  291. $this->code = $err_code;
  292. $this->msg = $err_msg;
  293. $this->heap = $stacktrace;
  294. }
  295. }
  296. class StatisticAttribute extends BasicAttribute {
  297. public $enccnt;
  298. public $deccnt;
  299. public $encerrcnt;
  300. public $decerrcnt;
  301. public $signcnt;
  302. public $verifycnt;
  303. public $signerrcnt;
  304. public $verifyerrcnt;
  305. public function __construct($type, $level, $service, $tid, $stat) {
  306. parent::__construct($type, $level, $service, $tid);
  307. $this->enccnt = $stat === null ? "0":$stat[0];
  308. $this->deccnt = $stat === null ? "0":$stat[1];
  309. $this->encerrcnt = $stat === null ? "0":$stat[2];
  310. $this->decerrcnt = $stat === null ? "0":$stat[3];
  311. $this->signcnt = $stat === null ? "0":$stat[4];
  312. $this->verifycnt = $stat === null ? "0":$stat[5];
  313. $this->signerrcnt = $stat === null ? "0":$stat[6];
  314. $this->verifyerrcnt = $stat === null ? "0":$stat[7];
  315. }
  316. }
  317. class InitMessage extends BasicMessage {
  318. public function __construct($service, $tid) {
  319. $this->businessId = BasicMessage::getRandomString();
  320. $this->text = "INIT";
  321. $this->attributes = new BasicAttribute(MSG_TYPE::INIT, MSG_LEVEL::INFO, $service, $tid);
  322. }
  323. }
  324. class EventMessage extends BasicMessage {
  325. public function __construct($service, $tid, $event_code, $event) {
  326. $this->businessId = BasicMessage::getRandomString();
  327. $this->text = "EVENT";
  328. $this->attributes = new EventAttribute(MSG_TYPE::EVENT, MSG_LEVEL::INFO, $service, $tid, $event_code, $event);
  329. }
  330. }
  331. class KPEventMessage extends BasicMessage {
  332. public function __construct($service, $tid, $event_code, $event, $major_kver, $keylist) {
  333. $this->businessId = BasicMessage::getRandomString();
  334. $this->text = "EVENT";
  335. $this->attributes = new KPEventAttribute(MSG_TYPE::EVENT, MSG_LEVEL::INFO, $service, $tid, $event_code, $event, $major_kver, $keylist);
  336. }
  337. }
  338. class ErrorMessage extends BasicMessage {
  339. public function __construct($service, $tid, $level, $err_code, $err_msg, $stacktrace) {
  340. $this->businessId = BasicMessage::getRandomString();
  341. $this->text = "EXCEPTION";
  342. $this->attributes = new ErrorAttribute(MSG_TYPE::EXCEPTION, $level, $service, $tid, $err_code, $err_msg, $stacktrace);
  343. }
  344. }
  345. class StatisticMessage extends BasicMessage {
  346. public function __construct($service, $tid, $stat) {
  347. $this->businessId = BasicMessage::getRandomString();
  348. $this->text = "STATISTIC";
  349. $this->attributes = new StatisticAttribute(MSG_TYPE::STATISTIC, MSG_LEVEL::INFO, $service, $tid, $stat);
  350. }
  351. }