PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/xinc-plugins/src/Xinc/Plugin/Repos/Documentation.php

https://gitlab.com/Tiger66639/xinc
PHP | 168 lines | 95 code | 13 blank | 60 comment | 9 complexity | 472d3c18ed06d648c4d98473dab6ac89 MD5 | raw file
  1. <?php
  2. /**
  3. * Xinc - Continuous Integration.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Development
  8. * @package Xinc.Plugin.Repos
  9. * @author Arno Schneider <username@example.org>
  10. * @copyright 2007 Arno Schneider, Barcelona
  11. * @license http://www.gnu.org/copyleft/lgpl.html GNU/LGPL, see license.php
  12. * This file is part of Xinc.
  13. * Xinc is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU Lesser General Public License as
  15. * published by the Free Software Foundation; either version 2.1 of
  16. * the License, or (at your option) any later version.
  17. *
  18. * Xinc is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with Xinc, write to the Free Software Foundation,
  25. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  26. * @link http://code.google.com/p/xinc/
  27. */
  28. require_once 'Xinc/Plugin/Abstract.php';
  29. require_once 'Xinc/Plugin/Repos/Publisher/Documentation/Task.php';
  30. require_once 'Xinc/Plugin/Repos/Gui/Documentation/Widget.php';
  31. class Xinc_Plugin_Repos_Documentation extends Xinc_Plugin_Abstract
  32. {
  33. const DOCUMENTATION_DIR = 'documentation';
  34. /**
  35. *
  36. * @return array of Gui Widgets
  37. */
  38. public function getGuiWidgets()
  39. {
  40. return array(new Xinc_Plugin_Repos_Gui_Documentation_Widget($this));
  41. }
  42. /**
  43. * Returns the defined tasks of the plugin
  44. *
  45. * @return Xinc_Plugin_Task[]
  46. */
  47. public function getTaskDefinitions()
  48. {
  49. return array(new Xinc_Plugin_Repos_Publisher_Documentation_Task($this));
  50. }
  51. public function getDocumentationDir(Xinc_Build_Interface $build)
  52. {
  53. $statusDir = Xinc::getInstance()->getStatusDir();
  54. $subDir = $build->getStatusSubDir();
  55. $fullDir = $statusDir . DIRECTORY_SEPARATOR . $subDir . DIRECTORY_SEPARATOR . self::DOCUMENTATION_DIR;
  56. return $fullDir;
  57. }
  58. /**
  59. * Copies a file into a special artifacts directory for the build
  60. *
  61. * @param Xinc_Build_Interface $build
  62. * @param string $sourceFile
  63. * @param string $alias
  64. * @param string $index
  65. *
  66. * @return boolean
  67. */
  68. public function registerDocumentation(
  69. Xinc_Build_Interface $build, $sourceFile, $alias, $index
  70. ) {
  71. $build->debug('Trying to register documentation: ' . $sourceFile);
  72. $sourceFile = realpath($sourceFile);
  73. $alias = basename($alias);
  74. $statusDir = Xinc::getInstance()->getStatusDir();
  75. $projectDir = Xinc::getInstance()->getProjectDir();
  76. $sourceFile = preg_replace('/\/+/', '/', $sourceFile);
  77. $index = preg_replace('/\/+/', '/', $index);
  78. $relativeIndex = str_replace($sourceFile, '', $index);
  79. $subDir = $build->getStatusSubDir();
  80. $fullDir = self::getDocumentationDir($build);
  81. $targetDir = $fullDir . DIRECTORY_SEPARATOR . basename($alias);
  82. $targetFile = $targetDir . DIRECTORY_SEPARATOR . basename($sourceFile);
  83. if (!is_dir($targetDir)) {
  84. mkdir($targetDir, 0755, true);
  85. }
  86. /**
  87. * Verify that the source is in the projectdir
  88. */
  89. $relativePath = str_replace($projectDir, '', $sourceFile);
  90. if ($relativePath == $sourceFile) {
  91. /**
  92. * the filename was not within the project path,
  93. * we need to prevent this file from being copied.
  94. *
  95. * Future: run Xinc in a chroot environment per project
  96. */
  97. $build->error('Registering doc: ' . $sourceFile . '->' . $targetFile . ' failed.');
  98. $build->error('-- ' . $sourceFile . ' is not within project dir. Security Problem.');
  99. return false;
  100. }
  101. if (!file_exists($fullDir)) {
  102. mkdir($fullDir, 0755, true);
  103. }
  104. if (is_dir($sourceFile)) {
  105. $relativePath = str_replace($sourceFile, '', $index);
  106. if ($relativePath == $index) {
  107. /**
  108. * the index file was not within the doc path,
  109. * we need to prevent this file from being copied.
  110. *
  111. * Future: run Xinc in a chroot environment per project
  112. */
  113. $build->error('Registering doc: ' . $sourceFile . '->' . $targetFile . ' failed.');
  114. $build->error('-- ' . $index . ' is not within ' . $sourceFile . ' dir. Security Problem.');
  115. return false;
  116. }
  117. if (DIRECTORY_SEPARATOR == '\\') {
  118. exec('xcopy /E /Y /I ' . escapeshellarg($sourceFile . '\*') . ' ' . escapeshellarg($targetDir), $out, $res1);
  119. } else {
  120. exec('cp -Rf ' . escapeshellarg($sourceFile) . '/. ' . escapeshellarg($targetDir), $out, $res1);
  121. }
  122. $res = false;
  123. if ($res1==0) {
  124. $status = 'OK';
  125. $res = true;
  126. } else {
  127. $status = 'FAILURE';
  128. $res = false;
  129. }
  130. $targetIndexFile = $targetDir . DIRECTORY_SEPARATOR . $relativeIndex;
  131. $registerFile = $targetDir;
  132. } else {
  133. $res = copy($sourceFile, $targetFile);
  134. $targetIndexFile = $targetFile;
  135. $registerFile = $targetFile;
  136. }
  137. if ($res) {
  138. chmod($targetDir, 0755);
  139. $status = 'OK';
  140. $docs = $build->getInternalProperties()->get('documentation');
  141. if (!is_array($docs)) {
  142. $docs = array();
  143. }
  144. $docsDir = dirname($targetFile);
  145. $docs[$alias] = array('file'=>$registerFile, 'index'=>$targetIndexFile);
  146. $build->getInternalProperties()->set('documentation', $docs);
  147. } else {
  148. $status = 'FAILURE';
  149. }
  150. $build->info('Registering documentation: ' . $sourceFile . '->' . $targetFile . ', result: ' . $status);
  151. return $res;
  152. }
  153. }