ts3lib.class.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * ts3lib.class.php <br />
  4. * ---------------- <br />
  5. * begin : Sunday, Dec 21, 2009 <br />
  6. * copyright : (C) 2009-2010 RK Programming <br />
  7. * email : [email protected] <br />
  8. * version : 1.1.0 <br />
  9. * last modified : Tuesday, Dec 29, 2009 <br />
  10. *
  11. * @author RK Programming <[email protected]>
  12. * @copyright Copyright (c) 2009-2010, Robin K.
  13. * @version 1.1.0
  14. TS3webinterface is free software. You can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation, version 1.3.
  17. **/
  18. define('TS3LIB_VERSION', '1.1.0');
  19. set_time_limit(15);
  20. define("TS3_OK", true);
  21. define("TS3_ERROR", false);
  22. define("TS3_PARSED", true);
  23. define("TS3_RAW", false);
  24. define("NL", '
  25. ');
  26. class TS3lib
  27. {
  28. private $socket;
  29. private $errorArray;
  30. private $deEscapeResults;
  31. private $performLog;
  32. function __construct($ip, $port)
  33. {
  34. $this->connected = false;
  35. $this->errorArray = array();
  36. $this->deEscapeResults = true;
  37. $this->performLog = array();
  38. $this->socket = fsockopen($ip, $port, $errno, $error, 5);
  39. stream_set_timeout($this->socket, 3);
  40. if( !is_resource($this->socket) || !$this->socket )
  41. {
  42. echo '<p>TS3lib error: connection failed: '.$errno.' '.$error.'</p>';
  43. /* $this->errorArray = array('error', $errnom, 'connection failed: '.$error); */
  44. }
  45. else
  46. {
  47. if( strpos(fgets($this->socket), 'TS3') === false )
  48. {
  49. echo ' - TS3lib error: no TS3 service running - '."\n";
  50. $this->errorArray = array('error', 0, 'no TS3 service running');
  51. }
  52. /*else
  53. {
  54. echo ' - TS3lib notice: TS3 service running - '."\n";
  55. }*/
  56. }
  57. }
  58. function __destruct()
  59. {
  60. $this->disconnect();
  61. }
  62. public function disconnect()
  63. {
  64. if( !$this->socket ) return;
  65. else if( is_resource($this->socket) )
  66. {
  67. fputs($this->socket, 'quit'."\n");
  68. fclose($this->socket);
  69. $this->performLog[] = 'quit';
  70. }
  71. }
  72. // alias for disconnect()
  73. public function close()
  74. {
  75. $this->disconnect();
  76. }
  77. public function isConnected()
  78. {
  79. if( is_resource($this->socket) )
  80. return true;
  81. else
  82. return false;
  83. }
  84. public function getError()
  85. {
  86. $tmpErrorArray = $this->errorArray; // backup error array
  87. $this->errorArray = array(); // clear error array
  88. return $this->de_escape($tmpErrorArray); // return backup error array
  89. }
  90. public function getLog()
  91. {
  92. return $this->performLog;
  93. }
  94. public function setDeEscapeResults($val)
  95. {
  96. $this->deEscapeResults = (bool)$val;
  97. }
  98. public function getDeEscapeResults()
  99. {
  100. return $this->deEscapeResults;
  101. }
  102. private function readResponse()
  103. {
  104. $result = '';
  105. $tmp = '';
  106. do
  107. {
  108. $tmp = fgets($this->socket);
  109. $result .= $tmp;
  110. } while ( preg_match('/id=[0-9]+? msg=.*/', $tmp, $tmparray) === 0 );
  111. return $result;
  112. }
  113. public function perform($command, $parse=TS3_PARSED)
  114. {
  115. if( !$this->socket ) return false;
  116. fputs($this->socket, $command."\n");
  117. $this->performLog[] = $command;
  118. if( $parse == TS3_PARSED )
  119. {
  120. if( $this->deEscapeResults )
  121. return $this->de_escape($this->parseResult($this->readResponse()));
  122. else
  123. return $this->parseResult($this->readResponse());
  124. }
  125. else
  126. {
  127. return trim($this->readResponse());
  128. }
  129. }
  130. public function performResultless($command)
  131. {
  132. if( !$this->socket ) return false;
  133. fputs($this->socket, $command."\n");
  134. $this->performLog[] = $command;
  135. return $this->checkResult($this->readResponse());
  136. }
  137. public function checkResult($resultString)
  138. {
  139. if( strpos($resultString, 'id=0 msg=ok') === false )
  140. {
  141. $tmparray = array();
  142. preg_match('/id=([0-9]+?) msg=(.*)/', $resultString, $tmparray);
  143. array_unshift($tmparray, 'ERROR');
  144. $this->errorArray = $tmparray;
  145. return false;
  146. }
  147. else
  148. {
  149. return true;
  150. }
  151. }
  152. private function parseResult($resultString)
  153. {
  154. if( $checkResult = $this->checkResult($resultString) )
  155. {
  156. // parse
  157. $result = array();
  158. $resultVars = explode('error id=', $resultString, -1);
  159. $resultVarsSplitted = explode('|', trim($resultVars[0]));
  160. $count = count($resultVarsSplitted);
  161. for($i=0; $i<$count; $i++)
  162. {
  163. $resultVarsSplitted[$i] = explode(' ', $resultVarsSplitted[$i]);
  164. $countSub = count($resultVarsSplitted[$i]);
  165. for($t=0; $t<$countSub; $t++)
  166. {
  167. if( strpos($resultVarsSplitted[$i][$t], '=') === false )
  168. {
  169. $resultVarsSplitted[$i][$t].= '=';
  170. }
  171. $resultVarsSplitted[$i][$t] = explode('=', $resultVarsSplitted[$i][$t], 2);
  172. $result[$i][$resultVarsSplitted[$i][$t][0]] = $resultVarsSplitted[$i][$t][1];
  173. }
  174. }
  175. //if( count($result) == 1 ) $result = $result[0];
  176. return $result;
  177. }
  178. else
  179. {
  180. return $checkResult;
  181. }
  182. }
  183. public function escape($string)
  184. {
  185. $search = array('\\', '/', ' ', '|', "\n");
  186. $replace = array('\\\\', '\\/', '\\s', '\\p', '\\n');
  187. if( is_array($string) )
  188. {
  189. foreach($string as $key => $value )
  190. {
  191. if( is_array($string[$key]) )
  192. {
  193. $string[$key] = $this->escape($string[$key]);
  194. }
  195. else
  196. {
  197. $string = str_replace($search, $replace, $string);
  198. }
  199. }
  200. }
  201. else if( is_string($string) )
  202. {
  203. $string = str_replace($search, $replace, $string);
  204. }
  205. return $string;
  206. }
  207. public function de_escape($string)
  208. {
  209. $search = array('\\n', '\\p', '\\s', '\\/', '\\\\');
  210. $replace = array('\n', '|', ' ', '/', '\\');
  211. if( is_array($string) )
  212. {
  213. foreach($string as $key => $value )
  214. {
  215. if( is_array($string[$key]) )
  216. {
  217. $string[$key] = $this->de_escape($string[$key]);
  218. }
  219. else
  220. {
  221. $string = str_replace($search, $replace, $string);
  222. }
  223. }
  224. }
  225. else if( is_string($string) )
  226. {
  227. $string = str_replace($search, $replace, $string);
  228. }
  229. return $string;
  230. }
  231. }
  232. //print_r($result = $ts3->perform('serverlist', TS3_PARSED));
  233. /*var_dump($result = $ts3->performResultless('use 8'));
  234. var_dump($result = $ts3->performResultless('login client_login_name=admin client_login_password=xxxx'));
  235. print_r($result = $ts3->perform('clientlist', TS3_PARSED));
  236. var_dump($result = $ts3->performResultless('clientmove clid=3 cid=51'));
  237. sleep(1);
  238. var_dump($result = $ts3->performResultless('clientmove clid=3 cid=55'));
  239. sleep(1);
  240. var_dump($result = $ts3->performResultless('clientmove clid=3 cid=56'));
  241. sleep(1);
  242. var_dump($result = $ts3->performResultless('clientmove clid=3 cid=51'));
  243. sleep(1);
  244. var_dump($result = $ts3->performResultless('clientmove clid=3 cid=55'));
  245. sleep(1);
  246. var_dump($result = $ts3->performResultless('clientmove clid=3 cid=56'));*/
  247. //$ts3 = new TS3lib('xxx.xxx.xxx', 10011);
  248. //
  249. //$ts3->disconnect();
  250. /*$param = "Test String fürs escapen|ziemlich X\\X normal/anders
  251. oder nicht?\nder autor denkt nichts\nziemlich sinnlos ;)";
  252. echo $param.'
  253. '.$ts3->escape($param).'
  254. '.$ts3->de_escape($ts3->escape($param));*/
  255. ?>