UpgradeController.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. class UpgradeController extends Zend_Controller_Action
  3. {
  4. public function indexAction()
  5. {
  6. $this->view->layout()->disableLayout();
  7. $this->_helper->viewRenderer->setNoRender(true);
  8. if (!$this->verifyAuth()) {
  9. return;
  10. }
  11. $didWePerformAnUpgrade = false;
  12. try {
  13. $upgradeManager = new UpgradeManager();
  14. $didWePerformAnUpgrade = $upgradeManager->doUpgrade();
  15. if (!$didWePerformAnUpgrade) {
  16. $this->getResponse()
  17. ->setHttpResponseCode(200)
  18. ->appendBody("No upgrade was performed. The current schema version is " . Application_Model_Preference::GetSchemaVersion() . ".<br>");
  19. } else {
  20. $this->getResponse()
  21. ->setHttpResponseCode(200)
  22. ->appendBody("Upgrade to Airtime schema version " . Application_Model_Preference::GetSchemaVersion() . " OK<br>");
  23. }
  24. }
  25. catch (Exception $e)
  26. {
  27. $this->getResponse()
  28. ->setHttpResponseCode(400)
  29. ->appendBody($e->getMessage());
  30. }
  31. }
  32. private function verifyAuth()
  33. {
  34. //The API key is passed in via HTTP "basic authentication":
  35. //http://en.wikipedia.org/wiki/Basic_access_authentication
  36. $CC_CONFIG = Config::getConfig();
  37. //Decode the API key that was passed to us in the HTTP request.
  38. $authHeader = $this->getRequest()->getHeader("Authorization");
  39. $encodedRequestApiKey = substr($authHeader, strlen("Basic "));
  40. $encodedStoredApiKey = base64_encode($CC_CONFIG["apiKey"][0] . ":");
  41. if ($encodedRequestApiKey !== $encodedStoredApiKey)
  42. {
  43. $this->getResponse()
  44. ->setHttpResponseCode(401)
  45. ->appendBody("Error: Incorrect API key.<br>");
  46. return false;
  47. }
  48. return true;
  49. }
  50. }