api.darkvoid.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. /**
  3. * Notification area aka DarkVoid class
  4. */
  5. class DarkVoid {
  6. /**
  7. *
  8. * Contains system alter config as key=>value
  9. *
  10. * @var array
  11. */
  12. protected $altCfg = array();
  13. /**
  14. * Contains current user login
  15. *
  16. * @var string
  17. */
  18. protected $myLogin = '';
  19. /**
  20. * Contains alerts cache
  21. *
  22. * @var string
  23. */
  24. protected $alerts = '';
  25. /**
  26. * Contains default cache timeout in minutes
  27. *
  28. * @var int
  29. */
  30. protected $cacheTime = '10';
  31. /**
  32. * UbillingConfig object placeholder
  33. *
  34. * @var object
  35. */
  36. protected $ubConfig = null;
  37. /**
  38. * Array of modules that must be skipped on alert updates
  39. *
  40. * @var array
  41. */
  42. protected $skipOnModules = array();
  43. /**
  44. * Cache storage path
  45. */
  46. const CACHE_PATH = 'exports/';
  47. /**
  48. * Cache prefix
  49. */
  50. const CACHE_PREFIX = 'darkvoid.';
  51. public function __construct() {
  52. if (LOGGED_IN) {
  53. $this->setModSkip();
  54. $this->setMyLogin();
  55. $this->loadAlter();
  56. $this->loadAlerts();
  57. }
  58. }
  59. /**
  60. * Sets modules array to be skipped on alert updates to prevent DB ops
  61. *
  62. * @return void
  63. */
  64. protected function setModSkip() {
  65. $this->skipOnModules = array('turbosms', 'senddog', 'remoteapi', 'updatemanager');
  66. $this->skipOnModules = array_flip($this->skipOnModules);
  67. }
  68. /**
  69. * Loads alerts from per-user cache or from database if needed
  70. *
  71. * @return void
  72. */
  73. protected function loadAlerts() {
  74. $cacheName = self::CACHE_PATH . self::CACHE_PREFIX . $this->myLogin;
  75. $cacheTime = time() - ($this->cacheTime * 60); //in minutes
  76. $updateCache = false;
  77. if (file_exists($cacheName)) {
  78. $updateCache = false;
  79. if ((filemtime($cacheName) > $cacheTime)) {
  80. $updateCache = false;
  81. } else {
  82. $updateCache = true;
  83. }
  84. } else {
  85. $updateCache = true;
  86. }
  87. if ($updateCache) {
  88. //ugly hack to prevent alerts update on tsms and senddog modules
  89. if (isset($_GET['module'])) {
  90. if (!isset($this->skipOnModules[$_GET['module']])) {
  91. //renew cache
  92. $this->updateAlerts();
  93. }
  94. } else {
  95. //renew cache
  96. $this->updateAlerts();
  97. }
  98. } else {
  99. //read from cache
  100. @$this->alerts = file_get_contents($cacheName);
  101. }
  102. }
  103. /**
  104. * Sets private login property
  105. *
  106. * @return
  107. */
  108. protected function setMyLogin() {
  109. $this->myLogin = whoami();
  110. }
  111. /**
  112. * Loads global alter.ini config into protected property
  113. *
  114. * @global type $ubillingConfig
  115. * @return void
  116. */
  117. protected function loadAlter() {
  118. global $ubillingConfig;
  119. $this->ubConfig = $ubillingConfig;
  120. $this->altCfg = $ubillingConfig->getAlter();
  121. if (isset($this->altCfg['DARKVOID_CACHETIME'])) {
  122. if ($this->altCfg['DARKVOID_CACHETIME']) {
  123. $this->cacheTime = $this->altCfg['DARKVOID_CACHETIME'];
  124. }
  125. }
  126. }
  127. /**
  128. * Renders available and enabled alerts into DarkVoid notification area
  129. *
  130. * @return void
  131. */
  132. protected function updateAlerts() {
  133. //new tickets alert
  134. if ($this->altCfg['TB_NEWTICKETNOTIFY']) {
  135. $newticketcount = zb_TicketsGetAllNewCount();
  136. if ($newticketcount != 0) {
  137. $this->alerts .= wf_Link('?module=ticketing', wf_img('skins/ticketnotify.gif', $newticketcount . ' ' . __('support tickets expected processing')), false);
  138. }
  139. }
  140. //new signups notification
  141. if ($this->altCfg['SIGREQ_ENABLED']) {
  142. $signups = new SignupRequests();
  143. $newreqcount = $signups->getAllNewCount();
  144. if ($newreqcount != 0) {
  145. $this->alerts .= wf_Link('?module=sigreq', wf_img('skins/sigreqnotify.gif', $newreqcount . ' ' . __('signup requests expected processing')), false);
  146. }
  147. }
  148. //check for unread messages in instant messanger
  149. if ($this->altCfg['TB_UBIM']) {
  150. if (cfr('UBIM')) {
  151. $ubIm = new UBMessenger();
  152. $unreadMessageCount = $ubIm->checkForUnreadMessages();
  153. if ($unreadMessageCount) {
  154. //yes, we have new messages
  155. $unreadIMNotify = __('You received') . ' ' . $unreadMessageCount . ' ' . __('new messages');
  156. $this->alerts .= wf_Link($ubIm::URL_ME . '&' . $ubIm::ROUTE_REFRESH . '=true', wf_img("skins/ubim_blink.gif", $unreadMessageCount . ' ' . __('new message received')), false, '');
  157. }
  158. }
  159. }
  160. //check sms sending queue
  161. if ($this->altCfg['SENDDOG_ENABLED']) {
  162. $smsQueueCount = rcms_scandir(DATA_PATH . 'tsms/');
  163. $smsQueueCount = sizeof($smsQueueCount);
  164. if ($smsQueueCount > 0) {
  165. $this->alerts .= wf_Link('?module=tsmsqueue', wf_img('skins/sms.png', $smsQueueCount . ' ' . __('SMS in queue')), false, '');
  166. }
  167. if ($this->altCfg['SENDDOG_PARALLEL_MODE']) {
  168. $sendDogPid = SendDog::PID_PATH;
  169. if (file_exists($sendDogPid)) {
  170. $this->alerts .= wf_Link('?module=tsmsqueue', wf_img('skins/dog_stand.png', __('SendDog is working')), false, '');
  171. }
  172. }
  173. }
  174. //police dog alerts
  175. if ($this->altCfg['POLICEDOG_ENABLED']) {
  176. $policeDogQuery = "SELECT COUNT(`id`) from `policedogalerts`";
  177. $policeDogCount = simple_query($policeDogQuery);
  178. $policeDogCount = $policeDogCount['COUNT(`id`)'];
  179. if ($policeDogCount > 0) {
  180. $this->alerts .= wf_Link('?module=policedog&show=fastscan', wf_img('skins/policedogalert.png', $policeDogCount . ' ' . __('Wanted MAC detected')), false, '');
  181. }
  182. }
  183. if ($this->altCfg['TB_TASKMANNOTIFY']) {
  184. //only "for me" tasks notification
  185. if ($this->altCfg['TB_TASKMANNOTIFY'] == 1) {
  186. $undoneTasksCount = ts_GetUndoneCountersMy();
  187. if ($undoneTasksCount > 0) {
  188. $undoneAlert = $undoneTasksCount . ' ' . __('Undone tasks') . ' ' . __('for me');
  189. $this->alerts .= wf_Link("?module=taskman&show=undone", wf_img("skins/jobnotify.png", $undoneAlert), false, '');
  190. }
  191. }
  192. //total undone tasks count notification
  193. if ($this->altCfg['TB_TASKMANNOTIFY'] == 2) {
  194. $undoneTasksCount = ts_GetUndoneCountersAll();
  195. if ($undoneTasksCount > 0) {
  196. $undoneAlert = $undoneTasksCount . ' ' . __('Undone tasks') . ' ' . __('for all');
  197. $this->alerts .= wf_Link("?module=taskman&show=undone", wf_img("skins/jobnotify.png", $undoneAlert), false, '');
  198. }
  199. }
  200. //total+my undone tasks count notification
  201. if ($this->altCfg['TB_TASKMANNOTIFY'] == 3) {
  202. $undoneTasksCount = ts_GetUndoneCountersAll();
  203. if ($undoneTasksCount > 0) {
  204. $undoneTasksCountMy = ts_GetUndoneCountersMy();
  205. $undoneAlert = $undoneTasksCount . ' ' . __('Undone tasks') . ': ' . __('for all') . ' ' . ($undoneTasksCount - $undoneTasksCountMy) . ' / ' . __('for me') . ' ' . $undoneTasksCountMy;
  206. $this->alerts .= wf_Link("?module=taskman&show=undone", wf_img("skins/jobnotify.png", $undoneAlert), false, '');
  207. }
  208. }
  209. }
  210. //missed calls notification
  211. if (@$this->altCfg['WDYC_ENABLED']) {
  212. $wdycCache = 'exports/whydoyoucall.dat';
  213. if (file_exists($wdycCache)) {
  214. $cacheData = file_get_contents($wdycCache);
  215. if (!empty($wdycCache)) {
  216. $cacheData = unserialize($cacheData);
  217. $missedCallsCount = sizeof($cacheData);
  218. if ($missedCallsCount > 0) {
  219. $missedCallsAlert = $missedCallsCount . ' ' . __('users tried to contact you but could not');
  220. $this->alerts .= wf_Link('?module=whydoyoucall', wf_img("skins/wdycnotify.png", $missedCallsAlert), false, '');
  221. }
  222. }
  223. }
  224. }
  225. //callback services
  226. if (@$this->altCfg['CALLMEBACK_ENABLED']) {
  227. $callMeBack = new CallMeBack();
  228. $undoneCallsCount = $callMeBack->getUndoneCount();
  229. if ($undoneCallsCount > 0) {
  230. $callmeBackAlert = $undoneCallsCount . ' ' . __('Users are waiting for your call');
  231. $this->alerts .= wf_Link('?module=callmeback', wf_img("skins/cmbnotify.png", $callmeBackAlert), false, '');
  232. }
  233. }
  234. //NAS servers monitoring
  235. if (isset($this->altCfg['NASMON_ENABLED'])) {
  236. if ($this->altCfg['NASMON_ENABLED']) {
  237. $nasMon = new NasMon();
  238. $this->alerts .= $nasMon->getNasAlerts();
  239. }
  240. }
  241. //watchdog maintenance mode notification
  242. if (isset($this->altCfg['WATCHDOG_ENABLED'])) {
  243. if ($this->altCfg['WATCHDOG_ENABLED']) {
  244. $watchDogMaintenance = zb_StorageGet('WATCHDOG_MAINTENANCE');
  245. $watchDogSmsSilence = zb_StorageGet('WATCHDOG_SMSSILENCE');
  246. if ($watchDogMaintenance) {
  247. $this->alerts .= wf_Link('?module=watchdog', wf_img('skins/maintenance.png', __('Watchdog') . ': ' . __('Disabled')));
  248. }
  249. if ($watchDogSmsSilence) {
  250. $this->alerts .= wf_Link('?module=watchdog', wf_img('skins/smssilence.png', __('Watchdog') . ': ' . __('SMS silence')));
  251. }
  252. }
  253. }
  254. //switchmon at notify area
  255. if ($this->altCfg['TB_SWITCHMON']) {
  256. $dead_raw = zb_StorageGet('SWDEAD');
  257. $last_pingtime = zb_StorageGet('SWPINGTIME');
  258. if (!is_numeric($last_pingtime)) {
  259. $last_pingtime = 0;
  260. }
  261. $deathTime = zb_SwitchesGetAllDeathTime();
  262. $deadarr = array();
  263. $content = '';
  264. if ($this->altCfg['SWYMAP_ENABLED']) {
  265. $content = wf_Link('?module=switchmap', wf_img('skins/swmapsmall.png', __('Switches map')), false);
  266. }
  267. $content .= wf_AjaxLoader() . wf_AjaxLink("?module=switches&forcereping=true&ajaxping=true", wf_img('skins/refresh.gif', __('Force ping')), 'switchping', true, '');
  268. if ($dead_raw) {
  269. $deadarr = unserialize($dead_raw);
  270. if (!empty($deadarr)) {
  271. //there is some dead switches
  272. $deadcount = sizeof($deadarr);
  273. if ($this->altCfg['SWYMAP_ENABLED']) {
  274. //getting geodata
  275. $switchesGeo = zb_SwitchesGetAllGeo();
  276. }
  277. //ajax container
  278. $content .= wf_tag('div', false, '', 'id="switchping"');
  279. foreach ($deadarr as $ip => $switch) {
  280. if ($this->altCfg['SWYMAP_ENABLED']) {
  281. if (isset($switchesGeo[$ip])) {
  282. if (!empty($switchesGeo[$ip])) {
  283. $devicefind = wf_Link('?module=switchmap&finddevice=' . $switchesGeo[$ip], wf_img('skins/icon_search_small.gif', __('Find on map'))) . ' ';
  284. } else {
  285. $devicefind = '';
  286. }
  287. } else {
  288. $devicefind = '';
  289. }
  290. } else {
  291. $devicefind = '';
  292. }
  293. //check morgue records for death time
  294. if (isset($deathTime[$ip])) {
  295. $deathClock = wf_img('skins/clock.png', __('Switch dead since') . ' ' . $deathTime[$ip]) . ' ';
  296. } else {
  297. $deathClock = '';
  298. }
  299. //switch location link
  300. $switchLocator = wf_Link('?module=switches&gotoswitchbyip=' . $ip, web_edit_icon(__('Go to switch')));
  301. //add switch as dead
  302. $content .= $devicefind . ' ' . $switchLocator . ' ' . $deathClock . $ip . ' - ' . $switch . '<br>';
  303. }
  304. //ajax container end
  305. $content .= wf_delimiter() . __('Cache state at time') . ': ' . date("H:i:s", $last_pingtime) . wf_tag('div', true);
  306. $this->alerts .= wf_tag('div', false, 'ubButton') . wf_modal(__('Dead switches') . ': ' . $deadcount, __('Dead switches'), $content, '', '500', '400') . wf_tag('div', true);
  307. } else {
  308. $content .= wf_tag('div', false, '', 'id="switchping"') . __('Switches are okay, everything is fine - I guarantee') . wf_delimiter() . __('Cache state at time') . ': ' . date("H:i:s", $last_pingtime) . wf_tag('div', true);
  309. $this->alerts .= wf_tag('div', false, 'ubButton') . wf_modal(__('All switches alive'), __('All switches alive'), $content, '', '500', '400') . wf_tag('div', true);
  310. }
  311. } else {
  312. $content .= wf_tag('div', false, '', 'id="switchping"') . __('Switches are okay, everything is fine - I guarantee') . wf_delimiter() . __('Cache state at time') . ': ' . @date("H:i:s", $last_pingtime) . wf_tag('div', true);
  313. $this->alerts .= wf_tag('div', false, 'ubButton') . wf_modal(__('All switches alive'), __('All switches alive'), $content, '', '500', '400') . wf_tag('div', true);
  314. }
  315. }
  316. //ForWhotTheBellTolls notification widget
  317. if (isset($this->altCfg['FWTBT_ENABLED'])) {
  318. if ($this->altCfg['FWTBT_ENABLED']) {
  319. $fwtbtFront = new ForWhomTheBellTolls();
  320. $this->alerts .= $fwtbtFront->renderWidget();
  321. }
  322. }
  323. //Dreamkas notification
  324. if ($this->ubConfig->getAlterParam('DREAMKAS_ENABLED') and $this->ubConfig->getAlterParam('DREAMKAS_NOTIFICATIONS_ENABLED')) {
  325. $dsNotifyFront = new DreamKasNotifications();
  326. $this->alerts .= $dsNotifyFront->renderWidget();
  327. }
  328. //I hope this service died.
  329. if ($this->ubConfig->getAlterParam('INSURANCE_ENABLED')) {
  330. $insurance = new Insurance(false);
  331. $hinsReqCount = $insurance->getUnprocessedHinsReqCount();
  332. if ($hinsReqCount > 0) {
  333. $insuranceRequestsAlert = $hinsReqCount . ' ' . __('insurance requests waiting for your reaction');
  334. $this->alerts .= wf_Link($insurance::URL_ME, wf_img('skins/insurance_notify.png', $insuranceRequestsAlert));
  335. }
  336. }
  337. //aerial alerts basic notification
  338. if ($this->ubConfig->getAlterParam('AERIAL_ALERTS_ENABLED')) {
  339. if ($this->ubConfig->getAlterParam('AERIAL_ALERTS_NOTIFY')) {
  340. $monitorRegion = $this->ubConfig->getAlterParam('AERIAL_ALERTS_NOTIFY');
  341. $aerialAlerts = new AerialAlerts($monitorRegion);
  342. $regionAlert = $aerialAlerts->renderRegionNotification($monitorRegion);
  343. if (!empty($regionAlert)) {
  344. $this->alerts .= $regionAlert;
  345. }
  346. }
  347. }
  348. //appending some debug string to validate cache expire
  349. $this->alerts .= '<!-- DarkVoid saved: ' . curdatetime() . ' -->';
  350. //saving per-admin cache data
  351. file_put_contents(self::CACHE_PATH . self::CACHE_PREFIX . $this->myLogin, $this->alerts);
  352. }
  353. /**
  354. * Returns raw alerts data
  355. *
  356. * @return string
  357. */
  358. public function render() {
  359. return ($this->alerts);
  360. }
  361. /**
  362. * Flushes all users alert cache
  363. *
  364. * @return void
  365. */
  366. public function flushCache() {
  367. $allCache = rcms_scandir(self::CACHE_PATH, self::CACHE_PREFIX . '*', 'file');
  368. if (!empty($allCache)) {
  369. foreach ($allCache as $io => $each) {
  370. @unlink(self::CACHE_PATH . $each);
  371. }
  372. }
  373. }
  374. }