api.aerialalerts.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Aerial raid notification class
  4. */
  5. class AerialAlerts {
  6. /**
  7. * Alerts datasource API placeholder
  8. *
  9. * @var object
  10. */
  11. protected $api = '';
  12. /**
  13. * System caching object placeholder
  14. *
  15. * @var object
  16. */
  17. protected $cache = '';
  18. /**
  19. * Default alerts caching timeout. May be configurable in future.
  20. *
  21. * @var int
  22. */
  23. protected $alertsCachingTimeout = 10;
  24. /**
  25. * System message helper object placeholder
  26. *
  27. * @var object
  28. */
  29. protected $messages = '';
  30. /**
  31. * Contains current alerts data
  32. *
  33. * @var array
  34. */
  35. protected $allAlerts = array();
  36. /**
  37. * Some predefined routes, URLS, etc
  38. */
  39. const DATA_SOURCE = 'http://ubilling.net.ua/aerialalerts/';
  40. const MAP_SOURCE = 'http://ubilling.net.ua/aerialalerts/?map=true';
  41. const ALERTS_KEY = 'AERIALALERTS';
  42. const URL_ME = '?module=report_aerial';
  43. const ROUTE_ALL = 'allregions';
  44. const ROUTE_MAP = 'showmap';
  45. public function __construct() {
  46. $this->initMessages();
  47. $this->initCache();
  48. $this->initApi();
  49. $this->loadAlerts();
  50. }
  51. /**
  52. * Inits system messages helper
  53. *
  54. * @return void
  55. */
  56. protected function initMessages() {
  57. $this->messages = new UbillingMessageHelper();
  58. }
  59. /**
  60. * Inits basic json api interraction layer
  61. *
  62. * @return void
  63. */
  64. protected function initApi() {
  65. $this->api = new OmaeUrl(self::DATA_SOURCE);
  66. $this->api->setUserAgent('Ubilling AerialAlerts');
  67. }
  68. /**
  69. * Inits system caching instance for further usage
  70. *
  71. * @return
  72. */
  73. protected function initCache() {
  74. $this->cache = new UbillingCache();
  75. }
  76. /**
  77. * Loads aerial alerts from cache or HTTP API
  78. *
  79. * @return void
  80. */
  81. protected function loadAlerts() {
  82. $this->allAlerts = $this->cache->get(self::ALERTS_KEY, $this->alertsCachingTimeout);
  83. if (empty($this->allAlerts)) {
  84. $jsonRaw = $this->api->response();
  85. $this->allAlerts = json_decode($jsonRaw, true);
  86. $this->cache->set(self::ALERTS_KEY, $this->allAlerts, $this->alertsCachingTimeout);
  87. }
  88. }
  89. /**
  90. * Just renders module controls
  91. *
  92. * @return string
  93. */
  94. public function renderControls() {
  95. $result = '';
  96. $result .= wf_Link(self::URL_ME, wf_img('skins/admannouncements.png') . ' ' . __('Alarm now'), false, 'ubButton');
  97. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_ALL . '=true', wf_img('skins/zbsannouncements.png') . ' ' . __('All'), false, 'ubButton');
  98. $result .= wf_Link(self::URL_ME . '&' . self::ROUTE_MAP . '=true', wf_img('skins/icon_map_small.png') . ' ' . __('Alerts map'), false, 'ubButton');
  99. return ($result);
  100. }
  101. /**
  102. * Renders aerial alerts basic report
  103. *
  104. * @return string
  105. */
  106. public function renderReport() {
  107. $result = '';
  108. $alertCount = 0;
  109. $renderAllFlag = (ubRouting::checkGet(self::ROUTE_ALL)) ? true : false;
  110. if (!empty($this->allAlerts)) {
  111. if (isset($this->allAlerts['states'])) {
  112. foreach ($this->allAlerts['states'] as $stateName => $stateParams) {
  113. if ($stateParams['alertnow']) {
  114. $result .= $this->messages->getStyledMessage($stateName, 'error');
  115. $alertCount++;
  116. } else {
  117. if ($renderAllFlag) {
  118. $result .= $this->messages->getStyledMessage($stateName, 'success');
  119. }
  120. }
  121. }
  122. // following code seems to be unused ;/
  123. if (!$alertCount) {
  124. $result .= $this->messages->getStyledMessage(__('Unbelievable, there are no air alarms at the moment. Does this happen at all?'), 'success');
  125. }
  126. } else {
  127. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('Data') . ' ' . __('is corrupted'), 'error');
  128. }
  129. } else {
  130. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('Empty reply received'), 'error');
  131. }
  132. return ($result);
  133. }
  134. /**
  135. * Returns precached alerts map html image code
  136. *
  137. * @return string
  138. */
  139. public function renderMap() {
  140. $result = '';
  141. $nowTime = time();
  142. $fileName = 'exports/alertsmap_' . date("Y-m-d_H_i", $nowTime) . '.dat';
  143. if (!file_exists($fileName)) {
  144. $mapApi = new OmaeUrl(self::MAP_SOURCE);
  145. $mapApi->setUserAgent('Ubilling AerialAlertsMap');
  146. $rawMap = $mapApi->response();
  147. if ($mapApi->httpCode() == 200) {
  148. $rawMap = base64_encode($rawMap);
  149. file_put_contents($fileName, $rawMap);
  150. } else {
  151. $rawMap = '';
  152. }
  153. } else {
  154. $rawMap = file_get_contents($fileName);
  155. }
  156. if (!empty($rawMap)) {
  157. $encodedImage = 'data:image/png;base64,' . $rawMap;
  158. $result = wf_tag('center') . wf_img_sized($encodedImage, date("Y-m-d H:i", $nowTime),'70%') . wf_tag('center', true);
  159. } else {
  160. $result .= $this->messages->getStyledMessage(__('Something went wrong') . ': ' . __('Unable to load data'), 'error');
  161. }
  162. return ($result);
  163. }
  164. /**
  165. * Renders DarkVoid notification if monitored region now under alarm
  166. *
  167. * @param string $region
  168. *
  169. * @return string
  170. */
  171. public function renderRegionNotification($region = '') {
  172. $result = '';
  173. if (!empty($region)) {
  174. $region = trim($region);
  175. if (!empty($this->allAlerts)) {
  176. if (isset($this->allAlerts['states'])) {
  177. if (isset($this->allAlerts['states'][$region])) {
  178. $regionAlarm = $this->allAlerts['states'][$region]['alertnow'];
  179. if ($regionAlarm) {
  180. $alarmStart = $this->allAlerts['states'][$region]['changed'];
  181. $icon = wf_img_sized('skins/nuclear_bomb.png', __('Alarm now') . ' - ' . $region . ' ' . __('from') . ' ' . $alarmStart, 32, 32);
  182. $result .= wf_Link(AerialAlerts::URL_ME, $icon);
  183. }
  184. }
  185. }
  186. }
  187. }
  188. return ($result);
  189. }
  190. /**
  191. * Returns json with notification region alert state
  192. *
  193. * @return string
  194. */
  195. public function usCallback($region = '') {
  196. $tmp = array();
  197. $result = '';
  198. if (!empty($region)) {
  199. $region = trim($region);
  200. if (!empty($this->allAlerts)) {
  201. if (isset($this->allAlerts['states'])) {
  202. if (isset($this->allAlerts['states'][$region])) {
  203. $tmp['region'] = $region;
  204. $tmp['alert'] = $this->allAlerts['states'][$region]['alertnow'];
  205. $tmp['changed'] = $this->allAlerts['states'][$region]['changed'];
  206. $result = json_encode($tmp);
  207. }
  208. }
  209. }
  210. }
  211. return ($result);
  212. }
  213. }