action.class.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace DebenOldert\AutoDeploy;
  3. include_once 'IAction.interface.php';
  4. use ZipArchive;
  5. class Action implements IAction
  6. {
  7. protected $settings;
  8. protected $key;
  9. protected $json;
  10. protected $downloadUrl;
  11. protected $zipName;
  12. protected $unzipName;
  13. protected $pluginName;
  14. protected $success = false;
  15. public function __construct(\stdClass $json, array $settings, string $key)
  16. {
  17. $this->json = $json;
  18. $this->settings = $settings;
  19. $this->key = $key;
  20. var_dump($this->json);
  21. }
  22. public function start(): bool
  23. {
  24. global $PHPWORK;
  25. if($this->shouldDownload())
  26. {
  27. $this->downloadUrl = $this->formatUrl();
  28. $this->zipName = $this->formatDownloadName();
  29. $this->unzipName = $this->formatUnZipName();
  30. if($this->download() && $this->unzip() > 0)
  31. {
  32. $this->pluginName = $this->formatRealName();
  33. $this->success = $this->move();
  34. return $this->isSuccess();
  35. }
  36. else
  37. {
  38. $PHPWORK->sendHttpCode(500);
  39. }
  40. }
  41. else
  42. {
  43. $PHPWORK->sendHttpCode(412);
  44. }
  45. }
  46. public function isSuccess(): bool
  47. {
  48. return $this->success;
  49. }
  50. public function move(): bool
  51. {
  52. $plugindir = \DebenOldert\PHPWork\Base::PLUGIN_DIR;
  53. $dest = "{$plugindir}/{$this->pluginName}";
  54. if(is_dir($dest))
  55. {
  56. return rename($this->unzipName, $dest);
  57. }
  58. return false;
  59. }
  60. public function unzip(): int
  61. {
  62. $zip = new ZipArchive();
  63. if($zip->open($this->zipName) !== 'true'){
  64. return false;
  65. }
  66. $zip->extractTo($this->unzipName);
  67. $zip->close();
  68. return $zip->numFiles;
  69. }
  70. public function formatDownloadName(): string
  71. {
  72. return Null;
  73. }
  74. public function formatUnZipName(): string
  75. {
  76. return Null;
  77. }
  78. public function formatUrl() : string
  79. {
  80. return Null;
  81. }
  82. public function shouldDownload(): bool
  83. {
  84. return false;
  85. }
  86. public function download(): bool
  87. {
  88. $file = fopen($this->zipName, 'wb');
  89. $ch = curl_init();
  90. curl_setopt($ch, CURLOPT_URL, $this->downloadUrl);
  91. curl_setopt($ch, CURLOPT_FAILONERROR, false);
  92. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  93. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  94. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  95. curl_setopt($ch, CURLOPT_FILE, $file);
  96. fclose($file);
  97. return file_exists($this->zipName);
  98. }
  99. public function formatRealName(): string
  100. {
  101. return Null;
  102. }
  103. public function createFolder($path) : bool
  104. {
  105. return mkdir($path, 0777, true);
  106. }
  107. }