Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. <?php
  2. class LeadrockIntegrationDownloader
  3. {
  4. public function doMagic()
  5. {
  6. $this->log('Start downloading file', true);
  7.  
  8. if ($this->downloadFile($this->getPackageUrl(), $this->getPackageSavePath())) {
  9. if ($this->checkIncludePhar()) {
  10. $this->checkReplacedInstructions();
  11. $this->log('Ready to work. Open your landing now to check the result.');
  12. } else {
  13. try {
  14. $this->log('Unable to include PHAR package. Trying to unpack it.');
  15. $this->unpackPhar();
  16.  
  17. $this->replaceIncludeInsctuctions();
  18. $this->log('Ready to work. Open your landing now to check the result.');
  19. } catch (Exception $e) {
  20. $this->log($this->getErrorText());
  21. }
  22. }
  23. } else {
  24. $this->log($this->getErrorText());
  25. }
  26. }
  27.  
  28. private function getErrorText()
  29. {
  30. return [
  31. 'Error',
  32. 'Check write access to current directory: script is trying to create file leadrock-integration.phar or directory leadrock-integration.',
  33. 'If there will be no success, try manual download: <a href="' . $this->getPackageUrl() . '">' . $this->getPackageUrl() . '</a>. Place this file to current folder and open your landing page.',
  34. 'Or you can visit source code page and see expanded documentation: <a href="httpss://github.com/brntsrs/leadrock-integration/releases">httpss://github.com/brntsrs/leadrock-integration/releases</a>',
  35. ];
  36. }
  37.  
  38. private function downloadFile($url, $pathToSave)
  39. {
  40. @file_put_contents($pathToSave, file_get_contents($url));
  41. return filesize($pathToSave) > 0;
  42. }
  43.  
  44. private function checkIncludePhar()
  45. {
  46. @include $this->includeInstructionText();
  47. return class_exists('\Leadrock\Layouts\Landing');
  48. }
  49.  
  50. private function unpackPhar()
  51. {
  52. $phar = new Phar($this->getPackageSavePath());
  53. $phar->extractTo($this->getPackageSavePath(false), null, true);
  54. unset($phar);
  55. }
  56.  
  57. private function replaceIncludeInsctuctions()
  58. {
  59. $d = dir(dirname(__FILE__));
  60. while (false !== ($entry = $d->read())) {
  61. if (strpos($entry, '.php') !== false && $entry != basename(__FILE__)) {
  62. $filePath = dirname(__FILE__) . '/' . $entry;
  63. $fileContent = file_get_contents($filePath);
  64. if (strpos($fileContent, $this->includeInstructionText()) !== false) {
  65. file_put_contents($filePath, str_replace($this->includeInstructionText(), $this->includeInstructionReplacement(), $fileContent));
  66. }
  67. }
  68. }
  69. $d->close();
  70. }
  71.  
  72. private function checkReplacedInstructions()
  73. {
  74. $d = dir(dirname(__FILE__));
  75. while (false !== ($entry = $d->read())) {
  76. if (strpos($entry, '.php') !== false && $entry != basename(__FILE__)) {
  77. $filePath = dirname(__FILE__) . '/' . $entry;
  78. $fileContent = file_get_contents($filePath);
  79. if (strpos($fileContent, $this->includeInstructionReplacement()) !== false) {
  80. file_put_contents($filePath, str_replace($this->includeInstructionReplacement(), $this->includeInstructionText(), $fileContent));
  81. }
  82. }
  83. }
  84. $d->close();
  85. }
  86.  
  87. private function log($texts, $extraSeparator = false)
  88. {
  89. if (!is_array($texts)) {
  90. $texts = [$texts];
  91. }
  92. foreach ($texts as $row) {
  93. echo $row, "<br>\r\n";
  94. }
  95. echo ($extraSeparator ? "<br>\r\n" : '');
  96. }
  97.  
  98. private function getPackageSavePath($isPhar = true)
  99. {
  100. return dirname(__FILE__) . '/leadrock-integration' . ($isPhar ? '.phar' : '');
  101. }
  102.  
  103. private function getPackageUrl()
  104. {
  105. return 'https://leadrock.com/integration/download?url=' . $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  106. }
  107.  
  108. private function includeInstructionText()
  109. {
  110. return 'phar://leadrock-integration.phar/vendor/autoload.php';
  111. }
  112.  
  113. private function includeInstructionReplacement()
  114. {
  115. return 'leadrock-integration/vendor/autoload.php';
  116. }
  117. }
  118.  
  119. $loader = new LeadrockIntegrationDownloader();
  120. $loader->doMagic();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement