Guest User

Untitled

a guest
Jun 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. <?php
  2.  
  3. class KyotoTycoon {
  4. const METHOD_GET = 'GET';
  5. const METHOD_POST = 'POST';
  6.  
  7. const DEFAULT_HOST = 'localhost';
  8. const DEFAULT_PORT = '1978';
  9. const DEFAULT_TIMEOUT = 0.5;
  10. const DEFAULT_METHOD = self::METHOD_GET;
  11.  
  12. private $host = null;
  13. private $port = null;
  14. private $method = null;
  15. private $timeout = null;
  16.  
  17. private $sockRes = false;
  18.  
  19. public function __construct($in_host = self::DEFAULT_HOST, $in_port = self::DEFAULT_PORT, $in_timeout = self::DEFAULT_TIMEOUT, $in_method = self::DEFAULT_METHOD)
  20. {
  21. $this->host = $in_host;
  22. $this->port = $in_port;
  23. $this->timeout = $in_timeout;
  24. $this->method = $in_method;
  25. }
  26.  
  27. public function set($key, $value, $expire = null)
  28. {
  29. // TODO
  30. $result = $this->write(__FUNCTION__, $key, $value, $expire);
  31. return true;
  32. }
  33.  
  34.  
  35. public function get($key)
  36. {
  37. $lines = explode("\n", trim($this->write(__FUNCTION__, $key)));
  38.  
  39. $result = array('value' => null);
  40. foreach($lines as $data) {
  41. $tmp = @explode("\t", $data);
  42. if(1 < count($tmp)) $result[$tmp[0]] = $tmp[1];
  43. }
  44.  
  45. return $result['value'];
  46. }
  47.  
  48. public function remove($key)
  49. {
  50. $result = trim($this->write(__FUNCTION__, $key));
  51. $IsOK = true;
  52. if(stristr($result,"ERROR")) $IsOK = false;
  53. return $IsOK;
  54. }
  55.  
  56.  
  57. public function getTimeout() { return $this->timeout; }
  58. public function setTimeout($in_timeout)
  59. {
  60. $IsOK = false;
  61.  
  62. //! Set Socket Timeout when socket is not open.
  63. if(!$this->sockRes && is_numeric($in_timeout)) {
  64. $this->timeout = (float) $in_timeout;
  65. $IsOK = true;
  66. }
  67.  
  68. return $IsOK;
  69. }
  70.  
  71. public function getMethod() { return $this->method; }
  72. public function setMethod($in_method)
  73. {
  74. $IsOK = false;
  75. switch($in_method) {
  76. case self::METHOD_GET:
  77. case self::METHOD_POST:
  78. $this->method = $in_method;
  79. $IsOK = true;
  80. break;
  81.  
  82. default:
  83. break;
  84. }
  85.  
  86. return $IsOK;
  87. }
  88.  
  89.  
  90. private function write($cmd, $key, $value = null, $expire = null)
  91. {
  92. $func = 'writeOn'. $this->method;
  93. $this->open();
  94. $result = $this->$func($cmd, $key, $value, $expire);
  95. $this->close();
  96.  
  97. return $result;
  98. }
  99.  
  100. private function writeOnGET($cmd, $key, $value, $expire)
  101. {
  102. $sendStr = '';
  103. $sendStr .= sprintf("GET /rpc/%s?%s HTTP/1.1\r\n", $cmd, $this->buildString($key, $value, $expire));
  104. $sendStr .= sprintf("Host: %s:%d\r\n", $this->host, $this->port);
  105. $sendStr .= sprintf("Connection: Close\r\n\r\n");
  106.  
  107. $result = '';
  108.  
  109. @fwrite($this->sockRes, $sendStr);
  110. while(!@feof($this->sockRes)) {
  111. $tmp = @fgets($this->sockRes, 1024);
  112. $result .= $tmp;
  113. }
  114.  
  115. return $result;
  116. }
  117.  
  118. private function writeOnPOST($cmd, $key, $value, $expire)
  119. {
  120. $postReq = $this->buildString($key, $value, $expire);
  121.  
  122. $sendStr = '';
  123. $sendStr .= sprintf("POST /rpc/%s HTTP/1.1\r\n", $cmd);
  124. $sendStr .= sprintf("Host: %s:%d\r\n", $this->host, $this->port);
  125. $sendStr .= sprintf("Content-Type: application/x-www-form-urlencoded\r\n");
  126. $sendStr .= sprintf("Content-Length: %d\r\n", strlen($postReq));
  127. $sendStr .= sprintf("Connection: Close\r\n\r\n");
  128. $sendStr .= sprintf("%s \r\n\r\n", $postReq);
  129.  
  130. $result = '';
  131.  
  132. @fwrite($this->sockRes, $sendStr);
  133. while(!@feof($this->sockRes)) {
  134. $tmp = @fgets($this->sockRes, 1024);
  135. $result .= $tmp;
  136. }
  137.  
  138. return $result;
  139.  
  140. }
  141.  
  142. private function buildString($key, $value, $expire)
  143. {
  144. $result = 'key=' . $key;
  145. if($value) $result .= "&value=$value";
  146. if($expire) $result .= "&xt=$expire";
  147.  
  148. return $result;
  149. }
  150.  
  151. private function open()
  152. {
  153. if($this->sockRes) return false;
  154.  
  155. $this->sockRes = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);
  156. if(!$this->sockRes) {
  157. throw new Exeption(sprintf("[Error] %s (%s)", $errstr, $errno));
  158. }
  159. }
  160.  
  161. private function close()
  162. {
  163. if($this->sockRes) fclose($this->sockRes);
  164. $this->sockRes = false;
  165. }
  166. }
Add Comment
Please, Sign In to add comment