PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/ManiaLivePlugins/Standard/TeamSpeak/TeamSpeak3/Adapter/ServerQuery/Reply.php

http://manialive.googlecode.com/
PHP | 334 lines | 162 code | 43 blank | 129 comment | 15 complexity | 05af1e924c7bb261837ead4a28d052bc MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * @file
  4. * TeamSpeak 3 PHP Framework
  5. *
  6. * $Id: Reply.php 9/26/2011 7:06:29 scp@orilla $
  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.8-beta
  23. * @author Sven 'ScP' Paulsen
  24. * @copyright Copyright (c) 2010 by Planet TeamSpeak. All rights reserved.
  25. */
  26. namespace ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Adapter\ServerQuery;
  27. /**
  28. * @class TeamSpeak3_Adapter_ServerQuery_Reply
  29. * @brief Provides methods to analyze and format a ServerQuery reply.
  30. */
  31. class Reply
  32. {
  33. /**
  34. * Stores the command used to get this reply.
  35. *
  36. * @var \ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Helper\String
  37. */
  38. protected $cmd = null;
  39. /**
  40. * Stores the servers reply (if available).
  41. *
  42. * @var \ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Helper\String
  43. */
  44. protected $rpl = null;
  45. /**
  46. * Stores connected TeamSpeak3_Node_Host object.
  47. *
  48. * @var \ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Node\Host
  49. */
  50. protected $con = null;
  51. /**
  52. * Stores an assoc array containing the error info for this reply.
  53. *
  54. * @var array
  55. */
  56. protected $err = array();
  57. /**
  58. * Sotres an array of events that occured before or during this reply.
  59. *
  60. * @var array
  61. */
  62. protected $evt = array();
  63. /**
  64. * Creates a new TeamSpeak3_Adapter_ServerQuery_Reply object.
  65. *
  66. * @param array $rpl
  67. * @param string $cmd
  68. * @param \ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Node\Host $con
  69. * @return Reply
  70. */
  71. public function __construct(array $rpl, $cmd = null, \ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Node\Host $con = null)
  72. {
  73. $this->cmd = new \ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Helper\String($cmd);
  74. $this->con = $con;
  75. $this->fetchError(array_pop($rpl));
  76. $this->fetchReply($rpl);
  77. }
  78. /**
  79. * Returns the reply as an \ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Helper\String object.
  80. *
  81. * @return \ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Helper\String
  82. */
  83. public function toString()
  84. {
  85. return (!func_num_args()) ? $this->rpl->unescape() : $this->rpl;
  86. }
  87. /**
  88. * Returns the reply as a standard PHP array where each element represents one item.
  89. *
  90. * @return array
  91. */
  92. public function toLines()
  93. {
  94. if(!count($this->rpl)) return array();
  95. $list = $this->toString(0)->split(\ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\TeamSpeak3::SEPARATOR_LIST);
  96. if(!func_num_args())
  97. {
  98. for($i = 0; $i < count($list); $i++) $list[$i]->unescape();
  99. }
  100. return $list;
  101. }
  102. /**
  103. * Returns the reply as a standard PHP array where each element represents one item in table format.
  104. *
  105. * @return array
  106. */
  107. public function toTable()
  108. {
  109. $table = array();
  110. foreach($this->toLines(0) as $cells)
  111. {
  112. $pairs = $cells->split(\ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\TeamSpeak3::SEPARATOR_CELL);
  113. if(!func_num_args())
  114. {
  115. for($i = 0; $i < count($pairs); $i++) $pairs[$i]->unescape();
  116. }
  117. $table[] = $pairs;
  118. }
  119. return $table;
  120. }
  121. /**
  122. * Returns a multi-dimensional array containing the reply splitted in multiple rows and columns.
  123. *
  124. * @return array
  125. */
  126. public function toArray()
  127. {
  128. $array = array();
  129. $table = $this->toTable(1);
  130. for($i = 0; $i < count($table); $i++)
  131. {
  132. foreach($table[$i] as $pair)
  133. {
  134. if(!$pair->contains(\ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\TeamSpeak3::SEPARATOR_PAIR))
  135. {
  136. $array[$i][$pair->toString()] = null;
  137. }
  138. else
  139. {
  140. list($ident, $value) = $pair->split(\ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\TeamSpeak3::SEPARATOR_PAIR, 2);
  141. $array[$i][$ident->toString()] = $value->isInt() ? $value->toInt() : (!func_num_args() ? $value->unescape() : $value);
  142. }
  143. }
  144. }
  145. return $array;
  146. }
  147. /**
  148. * Returns a multi-dimensional assoc array containing the reply splitted in multiple rows and columns.
  149. * The identifier specified by key will be used while indexing the array.
  150. *
  151. * @param $key
  152. * @return array
  153. */
  154. public function toAssocArray($ident)
  155. {
  156. $nodes = (func_num_args() > 1) ? $this->toArray(1) : $this->toArray();
  157. $array = array();
  158. foreach($nodes as $node)
  159. {
  160. if(array_key_exists($ident, $node))
  161. {
  162. $array[(is_object($node[$ident])) ? $node[$ident]->toString() : $node[$ident]] = $node;
  163. }
  164. else
  165. {
  166. throw new Exception("invalid parameter", 0x602);
  167. }
  168. }
  169. return $array;
  170. }
  171. /**
  172. * Returns an array containing the reply splitted in multiple rows and columns.
  173. *
  174. * @return array
  175. */
  176. public function toList()
  177. {
  178. $array = func_num_args() ? $this->toArray(1) : $this->toArray();
  179. if(count($array) == 1)
  180. {
  181. return array_shift($array);
  182. }
  183. return $array;
  184. }
  185. /**
  186. * Returns an array containing stdClass objects.
  187. *
  188. * @return ArrayObject
  189. */
  190. public function toObjectArray()
  191. {
  192. $array = (func_num_args() > 1) ? $this->toArray(1) : $this->toArray();
  193. for($i = 0; $i < count($array); $i++)
  194. {
  195. $array[$i] = (object) $array[$i];
  196. }
  197. return $array;
  198. }
  199. /**
  200. * Returns the command used to get this reply.
  201. *
  202. * @return \ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Helper\String
  203. */
  204. public function getCommandString()
  205. {
  206. return new \ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Helper\String($this->cmd);
  207. }
  208. /**
  209. * Returns an array of events that occured before or during this reply.
  210. *
  211. * @return array
  212. */
  213. public function getNotifyEvents()
  214. {
  215. return $this->evt;
  216. }
  217. /**
  218. * Returns the value for a specified error property.
  219. *
  220. * @param string $ident
  221. * @param mixed $default
  222. * @return mixed
  223. */
  224. public function getErrorProperty($ident, $default = null)
  225. {
  226. return (array_key_exists($ident, $this->err)) ? $this->err[$ident] : $default;
  227. }
  228. /**
  229. * Parses a ServerQuery error and throws a TeamSpeak3_Adapter_ServerQuery_Exception object if
  230. * there's an error.
  231. *
  232. * @param string $err
  233. * @throws Exception
  234. * @return void
  235. */
  236. protected function fetchError($err)
  237. {
  238. $cells = $err->section(\ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\TeamSpeak3::SEPARATOR_CELL, 1, 3);
  239. foreach($cells->split(\ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\TeamSpeak3::SEPARATOR_CELL) as $pair)
  240. {
  241. list($ident, $value) = $pair->split(\ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\TeamSpeak3::SEPARATOR_PAIR);
  242. $this->err[$ident->toString()] = $value->isInt() ? $value->toInt() : $value->unescape();
  243. }
  244. \ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Helper\Signal::getInstance()->emit("notifyError", $this);
  245. if($this->getErrorProperty("id", 0x00) != 0x00)
  246. {
  247. if($permid = $this->getErrorProperty("failed_permid"))
  248. {
  249. try
  250. {
  251. $suffix = " (failed on " . $this->con->permissionGetNameById($permid) . ")";
  252. }
  253. catch(Exception $e)
  254. {
  255. $suffix = " (failed on " . $this->cmd->section(\ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\TeamSpeak3::SEPARATOR_CELL) . " " . $permid . "/0x" . strtoupper(dechex($permid)) . ")";
  256. }
  257. }
  258. elseif($details = $this->getErrorProperty("extra_msg"))
  259. {
  260. $suffix = " (" . $details . ")";
  261. }
  262. else
  263. {
  264. $suffix = "";
  265. }
  266. throw new Exception($this->getErrorProperty("msg") . $suffix, $this->getErrorProperty("id"));
  267. }
  268. }
  269. /**
  270. * Parses a ServerQuery reply and creates a \ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Helper\String object.
  271. *
  272. * @param string $rpl
  273. * @return void
  274. */
  275. protected function fetchReply($rpl)
  276. {
  277. foreach($rpl as $key => $val)
  278. {
  279. if($val->startsWith(\ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\TeamSpeak3::GREET))
  280. {
  281. unset($rpl[$key]);
  282. }
  283. elseif($val->startsWith(\ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\TeamSpeak3::EVENT))
  284. {
  285. $this->evt[] = new Event($rpl[$key], $this->con);
  286. unset($rpl[$key]);
  287. }
  288. }
  289. $this->rpl = new \ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\Helper\String(implode(\ManiaLivePlugins\Standard\TeamSpeak\TeamSpeak3\TeamSpeak3::SEPARATOR_LIST, $rpl));
  290. }
  291. }