Reply.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * $Id: Reply.php 06/06/2016 22:27:13 scp@Svens-iMac $
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. * @package TeamSpeak3
  22. * @version 1.1.24
  23. * @author Sven 'ScP' Paulsen
  24. * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
  25. */
  26. /**
  27. * @class TeamSpeak3_Adapter_ServerQuery_Reply
  28. * @brief Provides methods to analyze and format a ServerQuery reply.
  29. */
  30. class TeamSpeak3_Adapter_ServerQuery_Reply
  31. {
  32. /**
  33. * Stores the command used to get this reply.
  34. *
  35. * @var TeamSpeak3_Helper_String
  36. */
  37. protected $cmd = null;
  38. /**
  39. * Stores the servers reply (if available).
  40. *
  41. * @var TeamSpeak3_Helper_String
  42. */
  43. protected $rpl = null;
  44. /**
  45. * Stores connected TeamSpeak3_Node_Host object.
  46. *
  47. * @var TeamSpeak3_Node_Host
  48. */
  49. protected $con = null;
  50. /**
  51. * Stores an assoc array containing the error info for this reply.
  52. *
  53. * @var array
  54. */
  55. protected $err = array();
  56. /**
  57. * Sotres an array of events that occured before or during this reply.
  58. *
  59. * @var array
  60. */
  61. protected $evt = array();
  62. /**
  63. * Indicates whether exceptions should be thrown or not.
  64. *
  65. * @var boolean
  66. */
  67. protected $exp = TRUE;
  68. /**
  69. * Creates a new TeamSpeak3_Adapter_ServerQuery_Reply object.
  70. *
  71. * @param array $rpl
  72. * @param string $cmd
  73. * @param boolean $exp
  74. * @param TeamSpeak3_Node_Host $con
  75. * @return TeamSpeak3_Adapter_ServerQuery_Reply
  76. */
  77. public function __construct(array $rpl, $cmd = null, TeamSpeak3_Node_Host $con = null, $exp = TRUE)
  78. {
  79. $this->cmd = new TeamSpeak3_Helper_String($cmd);
  80. $this->con = $con;
  81. $this->exp = (bool) $exp;
  82. $this->fetchError(array_pop($rpl));
  83. $this->fetchReply($rpl);
  84. }
  85. /**
  86. * Returns the reply as an TeamSpeak3_Helper_String object.
  87. *
  88. * @return TeamSpeak3_Helper_String
  89. */
  90. public function toString()
  91. {
  92. return (!func_num_args()) ? $this->rpl->unescape() : $this->rpl;
  93. }
  94. /**
  95. * Returns the reply as a standard PHP array where each element represents one item.
  96. *
  97. * @return array
  98. */
  99. public function toLines()
  100. {
  101. if(!count($this->rpl)) return array();
  102. $list = $this->toString(0)->split(TeamSpeak3::SEPARATOR_LIST);
  103. if(!func_num_args())
  104. {
  105. for($i = 0; $i < count($list); $i++) $list[$i]->unescape();
  106. }
  107. return $list;
  108. }
  109. /**
  110. * Returns the reply as a standard PHP array where each element represents one item in table format.
  111. *
  112. * @return array
  113. */
  114. public function toTable()
  115. {
  116. $table = array();
  117. foreach($this->toLines(0) as $cells)
  118. {
  119. $pairs = $cells->split(TeamSpeak3::SEPARATOR_CELL);
  120. if(!func_num_args())
  121. {
  122. for($i = 0; $i < count($pairs); $i++) $pairs[$i]->unescape();
  123. }
  124. $table[] = $pairs;
  125. }
  126. return $table;
  127. }
  128. /**
  129. * Returns a multi-dimensional array containing the reply splitted in multiple rows and columns.
  130. *
  131. * @return array
  132. */
  133. public function toArray()
  134. {
  135. $array = array();
  136. $table = $this->toTable(1);
  137. for($i = 0; $i < count($table); $i++)
  138. {
  139. foreach($table[$i] as $pair)
  140. {
  141. if(!count($pair))
  142. {
  143. continue;
  144. }
  145. if(!$pair->contains(TeamSpeak3::SEPARATOR_PAIR))
  146. {
  147. $array[$i][$pair->toString()] = null;
  148. }
  149. else
  150. {
  151. list($ident, $value) = $pair->split(TeamSpeak3::SEPARATOR_PAIR, 2);
  152. $array[$i][$ident->toString()] = $value->isInt() ? $value->toInt() : (!func_num_args() ? $value->unescape() : $value);
  153. }
  154. }
  155. }
  156. return $array;
  157. }
  158. /**
  159. * Returns a multi-dimensional assoc array containing the reply splitted in multiple rows and columns.
  160. * The identifier specified by key will be used while indexing the array.
  161. *
  162. * @param $key
  163. * @return array
  164. */
  165. public function toAssocArray($ident)
  166. {
  167. $nodes = (func_num_args() > 1) ? $this->toArray(1) : $this->toArray();
  168. $array = array();
  169. foreach($nodes as $node)
  170. {
  171. if(isset($node[$ident]))
  172. {
  173. $array[(is_object($node[$ident])) ? $node[$ident]->toString() : $node[$ident]] = $node;
  174. }
  175. else
  176. {
  177. throw new TeamSpeak3_Adapter_ServerQuery_Exception("invalid parameter", 0x602);
  178. }
  179. }
  180. return $array;
  181. }
  182. /**
  183. * Returns an array containing the reply splitted in multiple rows and columns.
  184. *
  185. * @return array
  186. */
  187. public function toList()
  188. {
  189. $array = func_num_args() ? $this->toArray(1) : $this->toArray();
  190. if(count($array) == 1)
  191. {
  192. return array_shift($array);
  193. }
  194. return $array;
  195. }
  196. /**
  197. * Returns an array containing stdClass objects.
  198. *
  199. * @return ArrayObject
  200. */
  201. public function toObjectArray()
  202. {
  203. $array = (func_num_args() > 1) ? $this->toArray(1) : $this->toArray();
  204. for($i = 0; $i < count($array); $i++)
  205. {
  206. $array[$i] = (object) $array[$i];
  207. }
  208. return $array;
  209. }
  210. /**
  211. * Returns the command used to get this reply.
  212. *
  213. * @return TeamSpeak3_Helper_String
  214. */
  215. public function getCommandString()
  216. {
  217. return new TeamSpeak3_Helper_String($this->cmd);
  218. }
  219. /**
  220. * Returns an array of events that occured before or during this reply.
  221. *
  222. * @return array
  223. */
  224. public function getNotifyEvents()
  225. {
  226. return $this->evt;
  227. }
  228. /**
  229. * Returns the value for a specified error property.
  230. *
  231. * @param string $ident
  232. * @param mixed $default
  233. * @return mixed
  234. */
  235. public function getErrorProperty($ident, $default = null)
  236. {
  237. return (array_key_exists($ident, $this->err)) ? $this->err[$ident] : $default;
  238. }
  239. /**
  240. * Parses a ServerQuery error and throws a TeamSpeak3_Adapter_ServerQuery_Exception object if
  241. * there's an error.
  242. *
  243. * @param string $err
  244. * @throws TeamSpeak3_Adapter_ServerQuery_Exception
  245. * @return void
  246. */
  247. protected function fetchError($err)
  248. {
  249. $cells = $err->section(TeamSpeak3::SEPARATOR_CELL, 1, 3);
  250. foreach($cells->split(TeamSpeak3::SEPARATOR_CELL) as $pair)
  251. {
  252. list($ident, $value) = $pair->split(TeamSpeak3::SEPARATOR_PAIR);
  253. $this->err[$ident->toString()] = $value->isInt() ? $value->toInt() : $value->unescape();
  254. }
  255. TeamSpeak3_Helper_Signal::getInstance()->emit("notifyError", $this);
  256. if($this->getErrorProperty("id", 0x00) != 0x00 && $this->exp)
  257. {
  258. if($permid = $this->getErrorProperty("failed_permid"))
  259. {
  260. if($permsid = key($this->con->request("permget permid=" . $permid, FALSE)->toAssocArray("permsid")))
  261. {
  262. $suffix = " (failed on " . $permsid . ")";
  263. }
  264. else
  265. {
  266. $suffix = " (failed on " . $this->cmd->section(TeamSpeak3::SEPARATOR_CELL) . " " . $permid . "/0x" . strtoupper(dechex($permid)) . ")";
  267. }
  268. }
  269. elseif($details = $this->getErrorProperty("extra_msg"))
  270. {
  271. $suffix = " (" . trim($details) . ")";
  272. }
  273. else
  274. {
  275. $suffix = "";
  276. }
  277. throw new TeamSpeak3_Adapter_ServerQuery_Exception($this->getErrorProperty("msg") . $suffix, $this->getErrorProperty("id"));
  278. }
  279. }
  280. /**
  281. * Parses a ServerQuery reply and creates a TeamSpeak3_Helper_String object.
  282. *
  283. * @param string $rpl
  284. * @return void
  285. */
  286. protected function fetchReply($rpl)
  287. {
  288. foreach($rpl as $key => $val)
  289. {
  290. if($val->startsWith(TeamSpeak3::GREET))
  291. {
  292. unset($rpl[$key]);
  293. }
  294. elseif($val->startsWith(TeamSpeak3::EVENT))
  295. {
  296. $this->evt[] = new TeamSpeak3_Adapter_ServerQuery_Event($rpl[$key], $this->con);
  297. unset($rpl[$key]);
  298. }
  299. }
  300. $this->rpl = new TeamSpeak3_Helper_String(implode(TeamSpeak3::SEPARATOR_LIST, $rpl));
  301. }
  302. }