PageRenderTime 29ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/class/api/corp/corpCorporationSheet.php

https://code.google.com/p/yapeal/
PHP | 330 lines | 228 code | 0 blank | 102 comment | 35 complexity | 59c7bcfc708e6b8d5d0e246b2eb4ef86 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Contains CorporationSheet class.
  4. *
  5. * PHP version 5
  6. *
  7. * LICENSE: This file is part of Yet Another Php Eve Api library also know
  8. * as Yapeal which will be used to refer to it in the rest of this license.
  9. *
  10. * Yapeal is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Yapeal is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with Yapeal. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @author Michael Cummings <mgcummings@yahoo.com>
  24. * @copyright Copyright (c) 2008-2012, Michael Cummings
  25. * @license http://www.gnu.org/copyleft/lesser.html GNU LGPL
  26. * @package Yapeal
  27. * @link http://code.google.com/p/yapeal/
  28. * @link http://www.eveonline.com/
  29. */
  30. /**
  31. * @internal Allow viewing of the source code in web browser.
  32. */
  33. if (isset($_REQUEST['viewSource'])) {
  34. highlight_file(__FILE__);
  35. exit();
  36. };
  37. /**
  38. * @internal Only let this code be included.
  39. */
  40. if (count(get_included_files()) < 2) {
  41. $mess = basename(__FILE__)
  42. . ' must be included it can not be ran directly.' . PHP_EOL;
  43. if (PHP_SAPI != 'cli') {
  44. header('HTTP/1.0 403 Forbidden', TRUE, 403);
  45. die($mess);
  46. } else {
  47. fwrite(STDERR, $mess);
  48. exit(1);
  49. }
  50. };
  51. /**
  52. * Class used to fetch and store CorporationSheet API.
  53. *
  54. * @package Yapeal
  55. * @subpackage Api_corp
  56. */
  57. class corpCorporationSheet extends ACorp {
  58. /**
  59. * Constructor
  60. *
  61. * @param array $params Holds the required parameters like keyID, vCode, etc
  62. * used in HTML POST parameters to API servers which varies depending on API
  63. * 'section' being requested.
  64. *
  65. * @throws LengthException for any missing required $params.
  66. */
  67. public function __construct(array $params) {
  68. // Cut off 'A' and lower case abstract class name to make section name.
  69. $this->section = strtolower(substr(get_parent_class($this), 1));
  70. $this->api = str_replace($this->section, '', __CLASS__);
  71. parent::__construct($params);
  72. }// function __construct
  73. /**
  74. * Used to store XML to MySQL table(s).
  75. *
  76. * @return Bool Return TRUE if store was successful.
  77. */
  78. public function apiStore() {
  79. // Need to exclude some params when passing them to API server so it doesn't
  80. // get confused.
  81. $apiParams = $this->params;
  82. unset($apiParams['corporationID']);
  83. // First get a new cache instance.
  84. $cache = new YapealApiCache($this->api, $this->section, $this->ownerID, $apiParams);
  85. try {
  86. // See if there is a valid cached copy of the API XML.
  87. $result = $cache->getCachedApi();
  88. // If it's not cached need to try to get it.
  89. if (FALSE === $result) {
  90. $proxy = $this->getProxy();
  91. $con = new YapealNetworkConnection();
  92. $result = $con->retrieveXml($proxy, $apiParams);
  93. // FALSE means there was an error and it has already been report so just
  94. // return to caller.
  95. if (FALSE === $result) {
  96. return FALSE;
  97. };
  98. // Cache the received XML.
  99. $cache->cacheXml($result);
  100. // Check if XML is valid.
  101. if (FALSE === $cache->isValid()) {
  102. // No use going any farther if the XML isn't valid.
  103. return FALSE;
  104. };
  105. };// if FALSE === $result ...
  106. // Create XMLReader.
  107. $this->xr = new XMLReader();
  108. // Pass XML to reader.
  109. $this->xr->XML($result);
  110. $cuntil = '';
  111. // Outer structure of XML is processed here.
  112. while ($this->xr->read()) {
  113. switch ($this->xr->nodeType) {
  114. case XMLReader::ELEMENT:
  115. switch ($this->xr->localName) {
  116. case 'currentTime':
  117. break;
  118. case 'result':
  119. // Call the per API parser.
  120. $result = $this->parserAPI();
  121. break;
  122. case 'cachedUntil':
  123. $this->xr->read();
  124. $cuntil = $this->xr->value;
  125. break;
  126. };// switch $this->xr->localName ...
  127. break;
  128. case XMLReader::END_ELEMENT:
  129. break;
  130. };// switch $this->xr->nodeType
  131. };// while $xr->read() ...
  132. // Update CachedUntil time since we should have a new one.
  133. $data = array( 'api' => $this->api, 'cachedUntil' => $cuntil,
  134. 'ownerID' => $this->ownerID, 'section' => $this->section
  135. );
  136. $cu = new CachedUntil($data);
  137. $cu->store();
  138. $this->xr->close();
  139. return $result;
  140. }
  141. catch (YapealApiErrorException $e) {
  142. // Any API errors that need to be handled in some way are handled in this
  143. // function.
  144. $this->handleApiError($e);
  145. return FALSE;
  146. }
  147. catch (ADODB_Exception $e) {
  148. $mess = 'Uncaught ADOdb exception' . PHP_EOL;
  149. Logger::getLogger('yapeal')->warn($mess);
  150. // Catch any uncaught ADOdb exceptions here.
  151. return FALSE;
  152. }
  153. }// function apiStore
  154. /**
  155. * Per API parser for XML.
  156. *
  157. * @return bool Returns TRUE if XML was parsered correctly, FALSE if not.
  158. */
  159. protected function parserAPI() {
  160. $tableName = YAPEAL_TABLE_PREFIX . $this->section . $this->api;
  161. // Get a new query instance.
  162. $qb = new YapealQueryBuilder($tableName, YAPEAL_DSN);
  163. $qb->setDefault('allianceName', '');
  164. $row = array();
  165. try {
  166. while ($this->xr->read()) {
  167. switch ($this->xr->nodeType) {
  168. case XMLReader::ELEMENT:
  169. switch ($this->xr->localName) {
  170. case 'allianceID':
  171. case 'allianceName':
  172. case 'ceoID':
  173. case 'ceoName':
  174. case 'corporationID':
  175. case 'corporationName':
  176. case 'description':
  177. case 'memberCount':
  178. case 'memberLimit':
  179. case 'shares':
  180. case 'stationID':
  181. case 'stationName':
  182. case 'taxRate':
  183. case 'ticker':
  184. case 'url':
  185. // Grab node name.
  186. $name = $this->xr->localName;
  187. // Move to text node.
  188. $this->xr->read();
  189. $row[$name] = $this->xr->value;
  190. break;
  191. case 'logo':
  192. // Check if empty.
  193. if ($this->xr->isEmptyElement == 1) {
  194. break;
  195. };// if $this->xr->isEmptyElement ...
  196. // Grab node name.
  197. $subTable = $this->xr->localName;
  198. // Check for method with same name as node.
  199. if (!is_callable(array($this, $subTable))) {
  200. $mess = 'Unknown what-to-be rowset ' . $subTable;
  201. $mess .= ' found in ' . $this->api;
  202. Logger::getLogger('yapeal')->warn($mess);
  203. return FALSE;
  204. };
  205. $this->$subTable();
  206. break;
  207. case 'rowset':
  208. // Check if empty.
  209. if ($this->xr->isEmptyElement == 1) {
  210. break;
  211. };// if $this->xr->isEmptyElement ...
  212. // Grab rowset name.
  213. $subTable = $this->xr->getAttribute('name');
  214. if (empty($subTable)) {
  215. $mess = 'Name of rowset is missing in ' . $this->api;
  216. Logger::getLogger('yapeal')->warn($mess);
  217. return FALSE;
  218. };
  219. $this->rowset($subTable);
  220. break;
  221. default:// Nothing to do here.
  222. };// $this->xr->localName ...
  223. break;
  224. case XMLReader::END_ELEMENT:
  225. if ($this->xr->localName == 'result') {
  226. $qb->addRow($row);
  227. if (count($qb) > 0) {
  228. $qb->store();
  229. };// if count $rows ...
  230. $qb = NULL;
  231. return TRUE;
  232. };// if $this->xr->localName == 'row' ...
  233. break;
  234. default:// Nothing to do.
  235. };// switch $this->xr->nodeType ...
  236. };// while $this->xr->read() ...
  237. }
  238. catch (ADODB_Exception $e) {
  239. Logger::getLogger('yapeal')->error($e);
  240. return FALSE;
  241. }
  242. $mess = 'Function ' . __FUNCTION__ . ' did not exit correctly' . PHP_EOL;
  243. Logger::getLogger('yapeal')->warn($mess);
  244. return FALSE;
  245. }// function parserAPI
  246. /**
  247. * Used to store XML to CorporationSheet's logo table.
  248. *
  249. * @return Bool Return TRUE if store was successful.
  250. */
  251. protected function logo() {
  252. $tableName = YAPEAL_TABLE_PREFIX . $this->section . 'Logo';
  253. // Get a new query instance.
  254. $qb = new YapealQueryBuilder($tableName, YAPEAL_DSN);
  255. $qb->setDefault('ownerID', $this->ownerID);
  256. $row = array();
  257. while ($this->xr->read()) {
  258. switch ($this->xr->nodeType) {
  259. case XMLReader::ELEMENT:
  260. switch ($this->xr->localName) {
  261. case 'color1':
  262. case 'color2':
  263. case 'color3':
  264. case 'graphicID':
  265. case 'shape1':
  266. case 'shape2':
  267. case 'shape3':
  268. $name = $this->xr->localName;
  269. $this->xr->read();
  270. $row[$name] = $this->xr->value;
  271. break;
  272. };// switch $xr->localName ...
  273. break;
  274. case XMLReader::END_ELEMENT:
  275. if ($this->xr->localName == 'logo') {
  276. $qb->addRow($row);
  277. return $qb->store();
  278. };// if $this->xr->localName ...
  279. break;
  280. default:// Nothing to do here.
  281. };// switch $this->xr->nodeType ...
  282. };// while $xr->read() ...
  283. $mess = 'Function ' . __FUNCTION__ . ' did not exit correctly' . PHP_EOL;
  284. Logger::getLogger('yapeal')->warn($mess);
  285. return FALSE;
  286. }// function logo
  287. /**
  288. * Used to store XML to rowset tables.
  289. *
  290. * @param string $table Name of the table for this rowset.
  291. *
  292. * @return Bool Return TRUE if store was successful.
  293. */
  294. protected function rowset($table) {
  295. $tableName = YAPEAL_TABLE_PREFIX . $this->section . ucfirst($table);
  296. // Get a new query instance.
  297. $qb = new YapealQueryBuilder($tableName, YAPEAL_DSN);
  298. $qb->setDefault('ownerID', $this->ownerID);
  299. while ($this->xr->read()) {
  300. switch ($this->xr->nodeType) {
  301. case XMLReader::ELEMENT:
  302. switch ($this->xr->localName) {
  303. case 'row':
  304. $row = array();
  305. // Walk through attributes and add them to row.
  306. while ($this->xr->moveToNextAttribute()) {
  307. $row[$this->xr->name] = $this->xr->value;
  308. };// while $this->xr->moveToNextAttribute() ...
  309. $qb->addRow($row);
  310. break;
  311. };// switch $this->xr->localName ...
  312. break;
  313. case XMLReader::END_ELEMENT:
  314. if ($this->xr->localName == 'rowset') {
  315. // Insert any leftovers.
  316. if (count($qb) > 0) {
  317. $qb->store();
  318. };// if count $rows ...
  319. $qb = NULL;
  320. return TRUE;
  321. };// if $this->xr->localName == 'row' ...
  322. break;
  323. };// switch $this->xr->nodeType
  324. };// while $this->xr->read() ...
  325. $mess = 'Function ' . __FUNCTION__ . ' did not exit correctly' . PHP_EOL;
  326. Logger::getLogger('yapeal')->warn($mess);
  327. return FALSE;
  328. }// function rowset
  329. }
  330. ?>