hostsubmit.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * The host entry point of ZenTaoPMS.
  4. *
  5. * @copyright Copyright 2009-2023 禅道软件(青岛)有限公司(ZenTao Software (Qingdao) Co., Ltd. www.cnezsoft.com)
  6. * @license ZPL(http://zpl.pub/page/zplv12.html) or AGPL(https://www.gnu.org/licenses/agpl-3.0.en.html)
  7. * @author Ke Zhao <zhaoke@easycorp.ltd>
  8. * @package entries
  9. * @version 1
  10. * @link https://www.zentao.net
  11. */
  12. class hostSubmitEntry extends baseEntry
  13. {
  14. /**
  15. * Listen host task finish submit.
  16. *
  17. * @param int|string $userID
  18. * @access public
  19. * @return void
  20. */
  21. public function post()
  22. {
  23. /* Check authorize. */
  24. $header = getallheaders();
  25. $token = isset($header['Authorization']) ? substr($header['Authorization'], 7) : '';
  26. if(!$token) return $this->sendError(401, 'Unauthorized');
  27. $now = helper::now();
  28. /* Check param. */
  29. $image = new stdclass();
  30. $task = $this->requestBody->task;
  31. $image->status = $this->requestBody->status;
  32. $image->status == 'complete' && $image->status = "completed";
  33. $this->dao = $this->loadModel('common')->dao;
  34. $id = $this->dao->select('id')->from(TABLE_ZAHOST)
  35. ->where('tokenSN')->eq($token)
  36. ->andWhere('tokenTime')->gt($now)->fi()
  37. ->fetch('id');
  38. if(!$id) return $this->sendError(400, 'Secret error.');
  39. $imageInfo = $this->dao->select('`status`,`from`,name,host')->from(TABLE_IMAGE)
  40. ->where('id')->eq($task)
  41. ->fetch();
  42. if(empty($imageInfo)) return $this->sendSuccess(200, 'success');
  43. if($imageInfo->from == 'snapshot' && $imageInfo->status == 'restoring')
  44. {
  45. if(in_array($image->status, array('failed', 'completed'))) $image->status = $image->status == 'completed' ? 'restore_completed' : 'restore_failed';
  46. }
  47. else if($imageInfo->from == 'snapshot')
  48. {
  49. if($image->status == 'failed')
  50. {
  51. $this->dao->delete()->from(TABLE_IMAGE)->where("id")->eq($task)->exec();
  52. return $this->sendSuccess(200, 'success');
  53. }
  54. }
  55. $this->dao->update(TABLE_IMAGE)->data($image)->where("id")->eq($task)->exec();
  56. if($imageInfo->from != 'zentao' && in_array($imageInfo->status, array('creating', 'restoring')))
  57. {
  58. $hostID = is_numeric($imageInfo->from) ? $imageInfo->from : $imageInfo->host;
  59. $this->dao->update(TABLE_ZAHOST)->data(array("status" => "wait"))->where("id")->eq($hostID)->exec();
  60. }
  61. return $this->sendSuccess(200, 'success');
  62. }
  63. }