comm.inc.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. require('config.inc.php');
  3. class ctl_socket {
  4. private $sock;
  5. function __construct()
  6. {
  7. $this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  8. $res = socket_connect($this->sock, CTL_IP, CTL_PORT);
  9. if ($res === false) {
  10. die("Failed connecting to bot, reason: ". socket_strerror(socket_last_error($this->sock)));
  11. }
  12. }
  13. function send()
  14. {
  15. $args = array();
  16. foreach (func_get_args() as $a) {
  17. $args[] = str_replace(array("%", ":", "\015", "\012"), array("%25", "%3A", "%0D", "%0A"), $a);
  18. }
  19. socket_write($this->sock, implode(":", $args)."\012");
  20. }
  21. function recv()
  22. {
  23. $data = socket_read($this->sock, 1048576, PHP_NORMAL_READ);
  24. if ($data === false) {
  25. die("Bot communication error: ". socket_strerror(socket_last_error($this->sock)));
  26. }
  27. $args = explode(":", trim($data));
  28. while (list($k, $v) = each($args)) {
  29. $args[$k] = rawurldecode($v);
  30. }
  31. return $args;
  32. }
  33. function cmd()
  34. {
  35. call_user_func_array(array($this, "send"), func_get_args());
  36. return $this->recv();
  37. }
  38. }
  39. $ctl = new ctl_socket();