| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Aisa\Weather; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Anax\Commons\ContainerInjectableInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Anax\Commons\ContainerInjectableTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | // use Anax\Route\Exception\ForbiddenException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | // use Anax\Route\Exception\NotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | // use Anax\Route\Exception\InternalErrorException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * A sample controller to show how a controller class can be implemented. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * The controller will be injected with $di if implementing the interface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * ContainerInjectableInterface, like this sample class does. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * The controller is mounted on a particular route and can then handle all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * requests for that mount point. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @SuppressWarnings(PHPMD.TooManyPublicMethods) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class JsonWeather extends WeatherIpController implements ContainerInjectableInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     use ContainerInjectableTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @var string $db a sample member variable that gets initialised | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private $ipAddress; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $time; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $WeatherRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     protected $apiKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     protected $darkKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 2 |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 2 |  |         $apiKeys = require ANAX_INSTALL_PATH . "/config/apiKeys.php"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2 |  |         $this->apiKey = $apiKeys["ipstack"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 2 |  |         $this->darkKey = $apiKeys["darksky"]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * Display the view | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @return object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |     public function indexAction() : object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |         $title = "Check IP (JSON)"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |         $page = $this->di->get("page"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |         $request = $this->di->get("request"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         $this->WeatherRequest = $this->di->get("WeatherRequest"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |         $this->ipAddress = $request->getGet("ip"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |         $this->time = $request->getGet("time"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         $currentIp = $this->ipAddress; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |         $this->object = new WeatherIp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 1 |  |         $currentIp = $this->object->validateIp($this->ipAddress); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |         $accessKey  = $this->apiKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 1 |  |         $BaseUrl = 'http://api.ipstack.com/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 1 |  |         $details =  $this->WeatherRequest->curlJson($BaseUrl.$currentIp.'?access_key='.$accessKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |         $weather = $this->multiCurlJson($details); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 1 |  |         $data["details"] = $details; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |         $data["weather"] = $weather; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         $data["currentIp"] = $currentIp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         $page->add("weatherjson", $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         return $page->render([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |             "title" => $title, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 | 1 |  |     public function indexActionPost() | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 1 |  |         $title = "Check IP (JSON)"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 70 | 1 |  |         $page = $this->di->get("page"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 1 |  |         $request = $this->di->get("request"); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 1 |  |         $this->WeatherRequest = $this->di->get("WeatherRequest"); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 | 1 |  |         $this->ipAddress = $request->getPost("ip"); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 1 |  |         $this->time = $request->getPost("time"); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 1 |  |         $currentIp = $this->ipAddress; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 1 |  |         $this->object = new WeatherIp(); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 1 |  |         $currentIp = $this->object->validateIp($this->ipAddress); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 1 |  |         $accessKey  = $this->apiKey; | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 1 |  |         $BaseUrl = 'http://api.ipstack.com/'; | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 1 |  |         $details = $this->WeatherRequest->curlJson($BaseUrl.$currentIp.'?access_key='.$accessKey); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 1 |  |         $weather = $this->multiCurlJson($details); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 1 |  |         $data["details"] = $details; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 1 |  |         $data["weather"] = $weather; | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 1 |  |         $data["currentIp"] = $currentIp; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 1 |  |         $data = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 1 |  |             foreach ($weather as $key => $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |                 $day = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 89 | 1 |  |                     "date" => gmdate("Y-m-d", $value["currently"]["time"]), | 
            
                                                                        
                            
            
                                    
            
            
                | 90 | 1 |  |                     "summary" =>  $value["currently"]["summary"], | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 1 |  |                     "temperature" =>  $value["currently"]["temperature"] | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |                 ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 1 |  |                 $data[] = $day; | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |             $json = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 96 | 1 |  |                 "ip" => $currentIp, | 
            
                                                                        
                            
            
                                    
            
            
                | 97 | 1 |  |                 "timezone" => $weather[0]["timezone"], | 
            
                                                                        
                            
            
                                    
            
            
                | 98 | 1 |  |                 "data" => $data, | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |             ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 100 | 1 |  |             return json_encode($json, JSON_PRETTY_PRINT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function weatherCheckActionGet() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $title = "Check IP (JSON)"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $page = $this->di->get("page"); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $request = $this->di->get("request"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $this->WeatherRequest = $this->di->get("WeatherRequest"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $this->ipAddress = $request->getGet("ip"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->time = $request->getGet("time"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $currentIp = $this->ipAddress; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $this->object = new WeatherIp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $currentIp = $this->object->validateIp($this->ipAddress); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $accessKey  = $this->apiKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $BaseUrl = 'http://api.ipstack.com/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $details = $this->WeatherRequest->curlJson($BaseUrl.$currentIp.'?access_key='.$accessKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $weather = $this->multiCurlJson($details); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $data["details"] = $details; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $data["weather"] = $weather; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         $data["currentIp"] = $currentIp; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $data = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             foreach ($weather as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 $day = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                     "date" => gmdate("Y-m-d", $value["currently"]["time"]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                     "summary" =>  $value["currently"]["summary"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     "temperature" =>  $value["currently"]["temperature"] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 $data[] = $day; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             $json = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 "ip" => $currentIp, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 "timezone" => $weather[0]["timezone"], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 "data" => $data, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             return json_encode($json, JSON_PRETTY_PRINT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 2 |  |     public function multiCurlJson($details) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 2 |  |         $accessKey  = $this->darkKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 2 |  |         $multiRequests = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         #future weather | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 2 |  |         if ($this->time === "future") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 1 |  |             for ($i=0; $i < 7; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 1 |  |                 $unixTime = time() + ($i * 24 * 60 * 60); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 1 |  |                 $BaseUrl = 'https://api.darksky.net/forecast/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 1 |  |                 $apiSetting = '?exclude=minutely,hourly,daily,flags'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 1 |  |                 $multiRequests[] = $BaseUrl.$accessKey .'/'.$details['latitude'].','.$details['longitude'].','.$unixTime.$apiSetting; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         #previous weather | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 2 |  |         if ($this->time === "past") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             for ($i=0; $i < 30; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                 $unixTime = time() - ($i * 24 * 60 * 60); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 $BaseUrl = 'https://api.darksky.net/forecast/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |                 $apiSetting = '?exclude=minutely,hourly,daily,flags'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                 $multiRequests[] = $BaseUrl.$accessKey .'/'.$details['latitude'].','.$details['longitude'].','.$unixTime.$apiSetting; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 2 |  |         $weather = $this->WeatherRequest->multiRequest($multiRequests); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 2 |  |         foreach ($weather as $key => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 1 |  |             $weather[$key] = json_decode(stripslashes($value), JSON_PRETTY_PRINT); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 2 |  |         return $weather; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 172 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |  |