PageRenderTime 38ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

/class/SectionCorp.php

https://code.google.com/p/yapeal/
PHP | 363 lines | 232 code | 0 blank | 131 comment | 51 complexity | a32af824ee984a2f27836733fa62ba55 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Contains Section Corp 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. };
  47. fwrite(STDERR, $mess);
  48. exit(1);
  49. };
  50. /**
  51. * Class used to pull Eve APIs for corp section.
  52. *
  53. * @package Yapeal
  54. * @subpackage Api_sections
  55. */
  56. class SectionCorp extends ASection {
  57. /**
  58. * Constructor
  59. */
  60. public function __construct() {
  61. $this->section = strtolower(str_replace('Section', '', __CLASS__));
  62. parent::__construct();
  63. }
  64. /**
  65. * Function called by Yapeal.php to start section pulling XML from servers.
  66. *
  67. * @return bool Returns TRUE if all APIs were pulled cleanly else FALSE.
  68. */
  69. public function pullXML() {
  70. if ($this->abort === TRUE) {
  71. return FALSE;
  72. };
  73. $apiCount = 0;
  74. $apiSuccess = 0;
  75. try {
  76. $con = YapealDBConnection::connect(YAPEAL_DSN);
  77. $sql = $this->getSQLQuery();
  78. $result = $con->GetAll($sql);
  79. if (count($result) == 0) {
  80. if (Logger::getLogger('yapeal')->isInfoEnabled()) {
  81. $mess = 'No corporations for corp section';
  82. Logger::getLogger('yapeal')->info($mess);
  83. };
  84. return FALSE;
  85. };// if empty $result ...
  86. // Build name of filter based on mode.
  87. $filter = array($this, YAPEAL_REGISTERED_MODE . 'Filter');
  88. $corpList = array_filter($result, $filter);
  89. if (empty($corpList)) {
  90. if (Logger::getLogger('yapeal')->isInfoEnabled()) {
  91. $mess = 'No active corporations for corp section';
  92. Logger::getLogger('yapeal')->info($mess);
  93. };
  94. return FALSE;
  95. };
  96. // Randomize order so no one corporation can starve the rest in case of
  97. // errors, etc.
  98. if (count($corpList) > 1) {
  99. shuffle($corpList);
  100. };
  101. // Ok now that we have a list of corps we can check which APIs need updated.
  102. foreach ($corpList as $crp) {
  103. // Skip corporations with no APIs.
  104. if ($crp['mask'] == 0) {
  105. continue;
  106. };
  107. $apis = $this->am->maskToAPIs($crp['mask'], $this->section);
  108. if ($apis === FALSE) {
  109. $mess = 'Problem retrieving API list using mask';
  110. Logger::getLogger('yapeal')->warn($mess);
  111. continue;
  112. };
  113. // Randomize order in which APIs are tried if there is a list.
  114. if (count($apis) > 1) {
  115. shuffle($apis);
  116. };
  117. foreach ($apis as $api) {
  118. // If the cache for this API has expired try to get update.
  119. if (CachedUntil::cacheExpired($api, $crp['corporationID']) === TRUE) {
  120. ++$apiCount;
  121. $class = $this->section . $api;
  122. // These are passed on to the API class instance and used as part of
  123. // hash for lock.
  124. $params = array('keyID' => $crp['keyID'], 'vCode' => $crp['vCode'],
  125. 'corporationID' => $crp['corporationID']);
  126. $parameters = '';
  127. foreach ($params as $k => $v) {
  128. $parameters .= $k . '=' . $v;
  129. };
  130. $hash = hash('sha1', $class . $parameters);
  131. // Use lock to keep from wasting time trying to do API that another
  132. // Yapeal is already working on.
  133. try {
  134. $con = YapealDBConnection::connect(YAPEAL_DSN);
  135. $sql = 'select get_lock(' . $con->qstr($hash) . ',5)';
  136. if ($con->GetOne($sql) != 1) {
  137. if (Logger::getLogger('yapeal')->isInfoEnabled()) {
  138. $mess = 'Failed to get lock for ' . $class . $hash;
  139. Logger::getLogger('yapeal')->info($mess);
  140. };
  141. continue;
  142. };// if $con->GetOne($sql) ...
  143. }
  144. catch(ADODB_Exception $e) {
  145. continue;
  146. }
  147. // Give each API 60 seconds to finish. This should never happen but
  148. // is here to catch runaways.
  149. set_time_limit(60);
  150. $instance = new $class($params);
  151. if ($instance->apiStore()) {
  152. ++$apiSuccess;
  153. };
  154. $instance = null;
  155. };// if CachedUntil::cacheExpired...
  156. // See if Yapeal has been running for longer than 'soft' limit.
  157. if (YAPEAL_MAX_EXECUTE < time()) {
  158. if (Logger::getLogger('yapeal')->isInfoEnabled()) {
  159. $mess = 'Yapeal has been working very hard and needs a break';
  160. Logger::getLogger('yapeal')->info($mess);
  161. };
  162. exit;
  163. };// if YAPEAL_MAX_EXECUTE < time() ...
  164. };// foreach $apis ...
  165. };// foreach $corpList
  166. }
  167. catch (ADODB_Exception $e) {
  168. Logger::getLogger('yapeal')->warn($e);
  169. }
  170. // Only truly successful if API was fetched and stored.
  171. if ($apiCount == $apiSuccess) {
  172. return TRUE;
  173. } else {
  174. return FALSE;
  175. }// else $apiCount == $apiSuccess ...
  176. }// function pullXML
  177. /**
  178. * Used to get the correct SQL for each mode of YAPEAL_REGISTERED_MODE.
  179. *
  180. * @return string Returns the SQL query string.
  181. */
  182. protected function getSQLQuery() {
  183. $sql = 'select akb.`keyID`,ac.`corporationID`,urk.`vCode`,aaki.`expires`,';
  184. switch (YAPEAL_REGISTERED_MODE) {
  185. case 'required':
  186. $sql .= 'urc.`isActive` as "RCActive",aaki.`accessMask`,';
  187. $sql .= 'urc.`activeAPIMask` as "RCMask"';
  188. $sql .= ' from';
  189. $sql .= ' `' . YAPEAL_TABLE_PREFIX . 'accountKeyBridge` as akb';
  190. $sql .= ' join `' . YAPEAL_TABLE_PREFIX . 'accountAPIKeyInfo` as aaki';
  191. $sql .= ' on (akb.`keyID` = aaki.`keyID`)';
  192. $sql .= ' join `' . YAPEAL_TABLE_PREFIX . 'utilRegisteredKey` as urk';
  193. $sql .= ' on (akb.`keyID` = urk.`keyID`)';
  194. $sql .= ' join `' . YAPEAL_TABLE_PREFIX . 'accountCharacters` as ac';
  195. $sql .= ' on (akb.`characterID` = ac.`characterID`)';
  196. $sql .= ' join `' . YAPEAL_TABLE_PREFIX . 'utilRegisteredCorporation` as urc';
  197. $sql .= ' on (ac.`corporationID` = urc.`corporationID`)';
  198. $sql .= ' where';
  199. $sql .= ' aaki.`type` = "Corporation"';
  200. break;
  201. case 'optional':
  202. $sql .= 'urk.`isActive` as "RKActive",urc.`isActive` as "RCActive",';
  203. $sql .= 'aaki.`accessMask`,urk.`activeAPIMask` as "RKMask",';
  204. $sql .= 'urc.`activeAPIMask` as "RCMask"';
  205. $sql .= ' from';
  206. $sql .= ' `' . YAPEAL_TABLE_PREFIX . 'accountKeyBridge` as akb';
  207. $sql .= ' join `' . YAPEAL_TABLE_PREFIX . 'accountAPIKeyInfo` as aaki';
  208. $sql .= ' on (akb.`keyID` = aaki.`keyID`)';
  209. $sql .= ' join `' . YAPEAL_TABLE_PREFIX . 'utilRegisteredKey` as urk';
  210. $sql .= ' on (akb.`keyID` = urk.`keyID`)';
  211. $sql .= ' join `' . YAPEAL_TABLE_PREFIX . 'accountCharacters` as ac';
  212. $sql .= ' on (akb.`characterID` = ac.`characterID`)';
  213. $sql .= ' left join `' . YAPEAL_TABLE_PREFIX . 'utilRegisteredCorporation` as urc';
  214. $sql .= ' on (ac.`corporationID` = urc.`corporationID`)';
  215. $sql .= ' where';
  216. $sql .= ' aaki.`type` = "Corporation"';
  217. break;
  218. case 'ignored':
  219. $sql .= 'urk.`isActive` as "RKActive",aaki.`accessMask`,';
  220. $sql .= 'urk.`activeAPIMask` as "RKMask"';
  221. $sql .= ' from';
  222. $sql .= ' `' . YAPEAL_TABLE_PREFIX . 'accountKeyBridge` as akb';
  223. $sql .= ' join `' . YAPEAL_TABLE_PREFIX . 'accountAPIKeyInfo` as aaki';
  224. $sql .= ' on (akb.`keyID` = aaki.`keyID`)';
  225. $sql .= ' join `' . YAPEAL_TABLE_PREFIX . 'utilRegisteredKey` as urk';
  226. $sql .= ' on (akb.`keyID` = urk.`keyID`)';
  227. $sql .= ' join `' . YAPEAL_TABLE_PREFIX . 'accountCharacters` as ac';
  228. $sql .= ' on (akb.`characterID` = ac.`characterID`)';
  229. $sql .= ' where';
  230. $sql .= ' aaki.`type` = "Corporation"';
  231. break;
  232. };
  233. return $sql;
  234. }// function getSQLQuery
  235. /**
  236. * Filter used when YAPEAL_REGISTERED_MODE == 'ignored'.
  237. *
  238. * This function is used to filter out non-active rows and merge all of the
  239. * different masks into one for each row.
  240. *
  241. * All the settings in utilRegisteredCorporation are ignored and the ones in
  242. * utilRegisteredKey used instead. If any of the optional settings in
  243. * utilRegisteredKey are null then returns FALSE but no error messages.
  244. *
  245. * @param array $row The row currently being checked.
  246. *
  247. * @return bool Returns TRUE if row should exist in result.
  248. */
  249. protected function ignoredFilter(&$row) {
  250. // Filter out if isActive is NULL or FALSE
  251. if (is_null($row['RKActive']) || $row['RKActive'] == 0) {
  252. return FALSE;
  253. };
  254. // If expire is not empty but has expired then return FALSE.
  255. if (!is_null($row['expires']) && gmdate('Y-m-d H:i:s') > $row['expires']) {
  256. return FALSE;
  257. };
  258. $row['mask'] = $this->mask;
  259. if (!is_null($row['RKMask'])) {
  260. $row['mask'] &= $row['RKMask'];
  261. } else {
  262. // Filter out if ActiveAPIMask is NULL
  263. return FALSE;
  264. };
  265. // Use APIKeyInfo mask if available.
  266. if (!is_null($row['accessMask'])) {
  267. $row['mask'] &= $row['accessMask'];
  268. };
  269. return TRUE;
  270. }// function ignoredFilter
  271. /**
  272. * Filter used when YAPEAL_REGISTERED_MODE == 'optional'.
  273. *
  274. * This function is used to filter out non-active rows and merge all of the
  275. * different masks into one for each row.
  276. *
  277. * The best way to view this mode is it allows you to farther restrict the
  278. * settings in utilSections using the optional settings from
  279. * utilRegisteredKey and utilRegisteredCorporation if you choose to. Any
  280. * non-null optional settings in utilRegisteredCorporation has priority over
  281. * the ones in utilRegisteredKey which are ignored in that case.
  282. *
  283. * @param array $row The row currently being checked.
  284. *
  285. * @return bool Returns TRUE if row should exist in result.
  286. */
  287. protected function optionalFilter(&$row) {
  288. // If isActive from utilRegisteredCorporation is not empty and set to FALSE
  289. // then return FALSE.
  290. if (!is_null($row['RCActive']) && $row['RCActive'] == 0) {
  291. return FALSE;
  292. // Else if isActive from utilRegisteredCorporation is null and isActive
  293. // from utilRegisteredKey is not empty and set to FALSE then return FALSE.
  294. } elseif (is_null($row['RCActive']) && !is_null($row['RKActive'])
  295. && $row['RKActive'] == 0) {
  296. return FALSE;
  297. };
  298. // If expire is not empty but has expired then return FALSE.
  299. if (!is_null($row['expires']) && gmdate('Y-m-d H:i:s') > $row['expires']) {
  300. return FALSE;
  301. };
  302. $row['mask'] = $this->mask;
  303. // Use Corporation mask if not empty ...
  304. if (!is_null($row['RCMask'])) {
  305. $row['mask'] &= $row['RCMask'];
  306. // else use key mask if not empty.
  307. } elseif (!is_null($row['RKMask'])) {
  308. $row['mask'] &= $row['RKMask'];
  309. };
  310. // Use APIKeyInfo mask if available.
  311. if (!is_null($row['accessMask'])) {
  312. $row['mask'] &= $row['accessMask'];
  313. };
  314. return TRUE;
  315. }// function optionalFilter
  316. /**
  317. * Filter used when YAPEAL_REGISTERED_MODE == 'required'.
  318. *
  319. * This function is used to filter out non-active rows and merge all of the
  320. * different masks into one for each row.
  321. *
  322. * The best way to view this mode is it forces you to farther restrict the
  323. * settings in utilSections using the optional settings from
  324. * utilRegisteredCorporation. The optional settings become required with the
  325. * exception of proxy which is still optional so activeAPIMask and isActive
  326. * can no longer be null. This mode is much like the old legacy mode of Yapeal.
  327. *
  328. * @param array $row The row currently being checked.
  329. *
  330. * @return bool Returns TRUE if row should exist in result.
  331. */
  332. protected function requiredFilter(&$row) {
  333. // isActive is not optional.
  334. if (is_null($row['RCActive'])) {
  335. $mess = 'IsActive can not be null in utilRegisteredCorporation when';
  336. $mess .= ' registered_mode = "required"';
  337. Logger::getLogger('yapeal')->warn($mess);
  338. return FALSE;
  339. };
  340. // Deactivated.
  341. if ($row['RCActive'] == 0) {
  342. return FALSE;
  343. };
  344. // If expire is not empty but has expired then return FALSE.
  345. if (!is_null($row['expires']) && gmdate('Y-m-d H:i:s') > $row['expires']) {
  346. return FALSE;
  347. };
  348. // activeAPIMask is not optional.
  349. if (is_null($row['RCMask'])) {
  350. $mess = 'activeAPIMask can not be null in utilRegisteredCorporation when';
  351. $mess .= ' registered_mode = "required"';
  352. Logger::getLogger('yapeal')->warn($mess);
  353. return FALSE;
  354. };
  355. $row['mask'] = $this->mask & $row['RCMask'];
  356. // Use APIKeyInfo mask if available.
  357. if (!is_null($row['accessMask'])) {
  358. $row['mask'] &= $row['accessMask'];
  359. };
  360. return TRUE;
  361. }// function requiredFilter
  362. }
  363. ?>