| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | if (!defined('DEBUG_MODE')) { die(); } |
| 10: | define('APOD_URL', 'https://api.nasa.gov/planetary/apod?concept_tags=True&api_key=%s'); |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | class Hm_Handler_process_nasa_connection extends Hm_Handler_Module { |
| 16: | public function process() { |
| 17: | list($success, $form) = $this->process_form(array('api_key')); |
| 18: | if ($success) { |
| 19: | $api = new Hm_API_Curl(); |
| 20: | $result = $api->command(sprintf(APOD_URL, $form['api_key'])); |
| 21: | if (empty($result) || (array_key_exists('error', $result) && !empty($result['error']))) { |
| 22: | Hm_Msgs::add(sprintf('%s', $result['error']['message']), 'danger'); |
| 23: | $this->out('nasa_action_status', false); |
| 24: | } |
| 25: | else { |
| 26: | $this->user_config->set('nasa_api_key', $form['api_key']); |
| 27: | $user_data = $this->user_config->dump(); |
| 28: | $this->session->set('user_data', $user_data); |
| 29: | $this->session->record_unsaved('NASA API connection'); |
| 30: | Hm_Msgs::add('Successfully connected to NASA APIs'); |
| 31: | $this->out('nasa_action_status', true); |
| 32: | } |
| 33: | return; |
| 34: | } |
| 35: | list($success, $form) = $this->process_form(array('nasa_disconnect')); |
| 36: | if ($success) { |
| 37: | $this->user_config->set('nasa_api_key', ''); |
| 38: | $user_data = $this->user_config->dump(); |
| 39: | $this->session->set('user_data', $user_data); |
| 40: | $this->session->record_unsaved('NASA API connection disabled'); |
| 41: | Hm_Msgs::add('NASA APIs disabled'); |
| 42: | $this->out('nasa_action_status', true); |
| 43: | } |
| 44: | } |
| 45: | } |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | class Hm_Handler_fetch_apod_content extends Hm_Handler_Module { |
| 51: | public function process() { |
| 52: | $key = $this->user_config->get('nasa_api_key', ''); |
| 53: | |
| 54: | $date = dateInESTTZ(); |
| 55: | if ($key) { |
| 56: | if (array_key_exists('apod_date', $this->request->get)) { |
| 57: | $apod_date = $this->request->get['apod_date']; |
| 58: | if (preg_match("/^\d{4}-\d{2}-\d{2}$/", $apod_date)) |
| 59: | $date = $apod_date; |
| 60: | } |
| 61: | $api = new Hm_API_Curl(); |
| 62: | $res = $api->command(sprintf(APOD_URL.'&date=%s', $key, $date)); |
| 63: | $this->out('apod_data', $res); |
| 64: | $this->out('apod_date', $date); |
| 65: | $headers = $this->get('http_headers'); |
| 66: | $headers['Content-Security-Policy'] = str_replace('img-src', 'img-src http://apod.nasa.gov', $headers['Content-Security-Policy']); |
| 67: | $this->out('http_headers', $headers); |
| 68: | } |
| 69: | } |
| 70: | } |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | class Hm_Handler_nasa_folder_data extends Hm_Handler_Module { |
| 76: | public function process() { |
| 77: | $this->out('nasa_api_key', $this->user_config->get('nasa_api_key', '')); |
| 78: | } |
| 79: | } |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | class Hm_Output_apod_content extends Hm_Output_Module { |
| 85: | protected function output() { |
| 86: | $data = $this->get('apod_data'); |
| 87: | $date = $this->get('apod_date', date('Y-m-d')); |
| 88: | $res = '<div class="content_title">'.$this->trans('Astronomy Picture of the Day'); |
| 89: | $res .= apod_date_form($date, $this); |
| 90: | $res .= '</div>'; |
| 91: | if (!$data || $data == array() || array_key_exists('error', $data) || array_key_exists('code', $data)) { |
| 92: | $res .= '<div class="apod_error">'; |
| 93: | if (is_array($data) && array_key_exists('error', $data) && array_key_exists('message', $data['error'])) { |
| 94: | $res .= $this->html_safe($data['error']['message']); |
| 95: | } |
| 96: | else { |
| 97: | $res .= $this->trans('Could not find a picture for the requested day'); |
| 98: | } |
| 99: | $res .= '</div>'; |
| 100: | } |
| 101: | else { |
| 102: | if (array_key_exists('title', $data)) { |
| 103: | $res .= '<div class="apod_title">'.$this->html_safe($data['title']).'</div>'; |
| 104: | } |
| 105: | if (array_key_exists('media_type', $data)) { |
| 106: | if ($data['media_type'] == 'image' && array_key_exists('url', $data)) { |
| 107: | $res .= '<div class="apod_image"><img class="msg_img" src="'.$this->html_safe($data['url']). |
| 108: | '" alt="'.$this->trans('Picture of the day').'" /></div>'; |
| 109: | } |
| 110: | elseif ($data['media_type'] == 'video' && array_key_exists('url', $data)) { |
| 111: | $res .= '<div class="apod_video"><a href="'.$this->html_safe($data['url']).'" target="_blank" rel="noopener">YouTube</a></div>'; |
| 112: | } |
| 113: | } |
| 114: | elseif (array_key_exists('url', $data) && preg_match("/jpg$/i", $data['url'])) { |
| 115: | $res .= '<div class="apod_image"><img class="msg_img" alt="'.$this->trans('Picture of the day'). |
| 116: | '" src="'.$this->html_safe($data['url']).'" /></div>'; |
| 117: | } |
| 118: | if (array_key_exists('explanation', $data)) { |
| 119: | $res .= '<div class="apod_desc">'.$this->html_safe($data['explanation']).'</div>'; |
| 120: | } |
| 121: | } |
| 122: | return $res; |
| 123: | } |
| 124: | } |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | class Hm_Output_nasa_connect_section extends Hm_Output_Module { |
| 130: | protected function output() { |
| 131: | $res = '<div class="nasa_connect"><div data-target=".nasa_connect_section" class="server_section border-bottom cursor-pointer px-1 py-3 pe-auto"> |
| 132: | <a href="#" class="pe-auto"> |
| 133: | <i class="bi bi-key-fill me-3"></i> |
| 134: | <b>'.$this->trans('NASA APIs').'</b> |
| 135: | </a> |
| 136: | </div>'; |
| 137: | |
| 138: | $res .= '<div class="nasa_connect_section"><div class="nasa_connect_inner_1" '; |
| 139: | |
| 140: | if ($this->get('nasa_api_key')) { |
| 141: | $res .= 'style="display: none;"'; |
| 142: | } |
| 143: | $res .= '><div>Connect to NASA APIs</div>'; |
| 144: | $res .= '<div class="col-lg-4 col-sm-12"><input type="text" size="50" class="nasa_api_key form-control warn_on_paste" placeholder="'.$this->trans('Enter your API key').'" /><br/>'; |
| 145: | $res .= '<input type="button" class="nasa_api_connect btn btn-secondary" value="'.$this->trans('Connect').'" /></div></div>'; |
| 146: | $res .= '<div class="nasa_connect_inner_2" '; |
| 147: | if (!$this->get('nasa_api_key')) { |
| 148: | $res .= 'style="display: none;"'; |
| 149: | } |
| 150: | $res .= '><div>'.$this->trans('Already connected').'</div>'; |
| 151: | $res .= '<div><input type="button" class="nasa_api_disconnect btn btn-danger" value="'.$this->trans('Disconnect').'" /></div>'; |
| 152: | $res .= '</div></div></div>'; |
| 153: | return $res; |
| 154: | } |
| 155: | } |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | class Hm_Output_nasa_folders extends Hm_Output_Module { |
| 161: | protected function output() { |
| 162: | if ($this->get('nasa_api_key')) { |
| 163: | $res = '<li class="menu_nasa_apod"><a class="unread_link" href="?page=nasa_apod">'; |
| 164: | if (!$this->get('hide_folder_icons')) { |
| 165: | $res .= '<i class="bi bi-globe-europe-africa account_icon"></i> '; |
| 166: | } |
| 167: | $res .= $this->trans('APOD').'</a></li>'; |
| 168: | $this->append('folder_sources', array('nASA_folders', $res)); |
| 169: | } |
| 170: | } |
| 171: | } |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | if (!hm_exists('apod_date_form')) { |
| 177: | function apod_date_form($date, $output_mod) { |
| 178: | $next = ''; |
| 179: | |
| 180: | |
| 181: | |
| 182: | if (strtotime(dateInESTTZ()) > strtotime($date)) { |
| 183: | if (date('Y-m-d', strtotime($date)) == "1995-06-16") |
| 184: | $nextday = strtotime("1995-06-20"); |
| 185: | else |
| 186: | $nextday = strtotime('+1 days', strtotime($date)); |
| 187: | $next = sprintf('?page=nasa_apod&apod_date=%s', date('Y-m-d', $nextday)); |
| 188: | } |
| 189: | if (strtotime($date) > strtotime("1995-06-16")) { |
| 190: | if (date("Y-m-d", strtotime($date)) == "1995-06-20") |
| 191: | $prevday = strtotime("1995-06-16"); |
| 192: | else |
| 193: | $prevday = strtotime('-1 days', strtotime($date)); |
| 194: | $prev = sprintf('?page=nasa_apod&apod_date=%s', date('Y-m-d', $prevday)); |
| 195: | } |
| 196: | $res = '<form class="apod_date" method="get">'; |
| 197: | |
| 198: | if ($prev) { |
| 199: | $res .= '<a href="'.$prev.'">'.$output_mod->trans('Previous').'</a>'; |
| 200: | } |
| 201: | $res .= '<input name="apod_date" class="apod_date_fld" type="date" value="'.$date.'" min="1995-06-16" max="'.dateInESTTZ().'"/>'; |
| 202: | $res .= '<input type="hidden" name="page" value="nasa_apod" />'; |
| 203: | $res .= '<input type="submit" value="'.$output_mod->trans('Update').'" />'; |
| 204: | |
| 205: | if ($next) { |
| 206: | $res .= '<a href="'.$next.'">'.$output_mod->trans('Next').'</a>'; |
| 207: | } |
| 208: | $res .= '</form>'; |
| 209: | return $res; |
| 210: | }} |
| 211: | |
| 212: | function dateInESTTZ() { |
| 213: | |
| 214: | $tz = "America/New_York"; |
| 215: | $timestamp = time(); |
| 216: | $dt = new DateTime("now", new DateTimeZone($tz)); |
| 217: | $dt->setTimeStamp($timestamp); |
| 218: | $date = $dt->format('Y-m-d'); |
| 219: | return $date; |
| 220: | } |
| 221: | |