WecomCenterView.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php declare(strict_types = 0);
  2. /*
  3. ** WeCom Center - GET controller. Gathers current state and renders the page.
  4. */
  5. namespace Modules\WecomCenter\Actions;
  6. use CController;
  7. use CControllerResponseData;
  8. use CWebUser;
  9. use Modules\WecomCenter\Lib\Helper;
  10. use Modules\WecomCenter\Lib\I18n;
  11. class WecomCenterView extends CController {
  12. protected function init() {
  13. $this->disableCsrfValidation();
  14. }
  15. protected function checkInput() {
  16. return true;
  17. }
  18. protected function checkPermissions() {
  19. return CWebUser::getType() == USER_TYPE_SUPER_ADMIN;
  20. }
  21. protected function doAction() {
  22. $module_dir = dirname(__DIR__);
  23. $mediatypes = Helper::webhookMediatypes();
  24. // selected media type
  25. $mediatypeid = (string) ($_REQUEST['mediatypeid'] ?? '');
  26. if ($mediatypeid === '' && $mediatypes) {
  27. // prefer one whose name mentions wecom
  28. foreach ($mediatypes as $mt) {
  29. if (stripos($mt['name'], 'wecom') !== false) {
  30. $mediatypeid = (string) $mt['mediatypeid'];
  31. break;
  32. }
  33. }
  34. if ($mediatypeid === '') {
  35. $mediatypeid = (string) $mediatypes[0]['mediatypeid'];
  36. }
  37. }
  38. $mediatype = null;
  39. $webhook_url = '';
  40. $robot = null;
  41. $media = null;
  42. if ($mediatypeid !== '') {
  43. $mediatype = Helper::getMediatype($mediatypeid);
  44. if ($mediatype) {
  45. $webhook_url = Helper::webhookUrl($mediatype);
  46. }
  47. $robot = Helper::robotUser('wecomrobot');
  48. $media = Helper::currentMedia($robot, $mediatypeid);
  49. }
  50. // selected host for map tab
  51. $selected_hostid = (string) ($_REQUEST['hostid'] ?? '');
  52. $host_loc = ['location_lat' => '', 'location_lon' => ''];
  53. if ($selected_hostid !== '') {
  54. $h = Helper::hostLocation($selected_hostid);
  55. $inv = $h['inventory'] ?? [];
  56. $host_loc = [
  57. 'location_lat' => $inv['location_lat'] ?? '',
  58. 'location_lon' => $inv['location_lon'] ?? ''
  59. ];
  60. }
  61. // triggers (per-trigger alerting switch, requires a search term)
  62. $trig_search = trim((string) ($_REQUEST['trig_search'] ?? ''));
  63. $triggers = Helper::triggers($trig_search);
  64. $trigger_count = Helper::triggerCount();
  65. $data = [
  66. 'title' => I18n::t('page.title'),
  67. 'mediatypes' => $mediatypes,
  68. 'mediatypeid' => $mediatypeid,
  69. 'mediatype' => $mediatype,
  70. 'webhook_url' => $webhook_url,
  71. 'robot' => $robot,
  72. 'robot_name' => 'wecomrobot',
  73. 'media' => $media,
  74. 'severity' => $media['severity'] ?? 48,
  75. 'active' => $media['active'] ?? 0,
  76. 'sendto' => $media['sendto'] ?? 'WeCom',
  77. 'schemes' => Helper::SCHEMES,
  78. 'templates' => Helper::bundledTemplates($module_dir),
  79. 'hosts' => Helper::hosts(),
  80. 'selected_hostid' => $selected_hostid,
  81. 'host_loc' => $host_loc,
  82. 'trig_search' => $trig_search,
  83. 'triggers' => $triggers,
  84. 'trigger_count' => $trigger_count,
  85. 'lang' => I18n::lang()
  86. ];
  87. $response = new CControllerResponseData($data);
  88. $response->setTitle(I18n::t('page.title'));
  89. $this->setResponse($response);
  90. }
  91. }