CvsPassTask.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /*
  3. * $Id: CvsPassTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. include_once 'phing/system/io/BufferedReader.php';
  23. include_once 'phing/system/io/BufferedWriter.php';
  24. include_once 'phing/util/StringHelper.php';
  25. /**
  26. * Adds an new entry to a CVS password file.
  27. *
  28. * @author Hans Lellelid <hans@xmpl.org> (Phing)
  29. * @author Jeff Martin <jeff@custommonkey.org> (Ant)
  30. * @version $Revision: 905 $
  31. * @package phing.tasks.system
  32. */
  33. class CVSPassTask extends Task {
  34. /** CVS Root */
  35. private $cvsRoot;
  36. /** Password file to add password to */
  37. private $passFile;
  38. /** Password to add to file */
  39. private $password;
  40. /** Array contain char conversion data */
  41. private static $shifts = array(
  42. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  43. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  44. 114, 120, 53, 79, 96, 109, 72, 108, 70, 64, 76, 67, 116, 74, 68, 87,
  45. 111, 52, 75, 119, 49, 34, 82, 81, 95, 65, 112, 86, 118, 110, 122, 105,
  46. 41, 57, 83, 43, 46, 102, 40, 89, 38, 103, 45, 50, 42, 123, 91, 35,
  47. 125, 55, 54, 66, 124, 126, 59, 47, 92, 71, 115, 78, 88, 107, 106, 56,
  48. 36, 121, 117, 104, 101, 100, 69, 73, 99, 63, 94, 93, 39, 37, 61, 48,
  49. 58, 113, 32, 90, 44, 98, 60, 51, 33, 97, 62, 77, 84, 80, 85, 223,
  50. 225, 216, 187, 166, 229, 189, 222, 188, 141, 249, 148, 200, 184, 136, 248, 190,
  51. 199, 170, 181, 204, 138, 232, 218, 183, 255, 234, 220, 247, 213, 203, 226, 193,
  52. 174, 172, 228, 252, 217, 201, 131, 230, 197, 211, 145, 238, 161, 179, 160, 212,
  53. 207, 221, 254, 173, 202, 146, 224, 151, 140, 196, 205, 130, 135, 133, 143, 246,
  54. 192, 159, 244, 239, 185, 168, 215, 144, 139, 165, 180, 157, 147, 186, 214, 176,
  55. 227, 231, 219, 169, 175, 156, 206, 198, 129, 164, 150, 210, 154, 177, 134, 127,
  56. 182, 128, 158, 208, 162, 132, 167, 209, 149, 241, 153, 251, 237, 236, 171, 195,
  57. 243, 233, 253, 240, 194, 250, 191, 155, 142, 137, 245, 235, 163, 242, 178, 152
  58. );
  59. /**
  60. * Create a CVS task using the default cvspass file location.
  61. */
  62. public function __construct() {
  63. $this->passFile = new PhingFile(
  64. Phing::getProperty("cygwin.user.home",
  65. Phing::getProperty("user.home"))
  66. . DIRECTORY_SEPARATOR . ".cvspass");
  67. }
  68. /**
  69. * Does the work.
  70. *
  71. * @throws BuildException if someting goes wrong with the build
  72. */
  73. public final function main() {
  74. if ($this->cvsRoot === null) {
  75. throw new BuildException("cvsroot is required");
  76. }
  77. if ($this->password === null) {
  78. throw new BuildException("password is required");
  79. }
  80. $this->log("cvsRoot: " . $this->cvsRoot, Project::MSG_DEBUG);
  81. $this->log("password: " . $this->password, Project::MSG_DEBUG);
  82. $this->log("passFile: " . $this->passFile->__toString(), Project::MSG_DEBUG);
  83. $reader = null;
  84. $writer = null;
  85. try {
  86. $buf = "";
  87. if ($this->passFile->exists()) {
  88. $reader = new BufferedReader(new FileReader($this->passFile));
  89. $line = null;
  90. while (($line = $reader->readLine()) !== null) {
  91. if (!StringHelper::startsWith($this->cvsRoot, $line)) {
  92. $buf .= $line . PHP_EOL;
  93. }
  94. }
  95. }
  96. $pwdfile = $buf . $this->cvsRoot . " A" . $this->mangle($this->password);
  97. $this->log("Writing -> " . $pwdfile , Project::MSG_DEBUG);
  98. $writer = new BufferedWriter(new FileWriter($this->passFile));
  99. $writer->write($pwdfile);
  100. $writer->newLine();
  101. $writer->close();
  102. if ($reader) {
  103. $reader->close();
  104. }
  105. } catch (IOException $e) {
  106. if ($reader) {
  107. try {
  108. $reader->close();
  109. } catch (Exception $e) {}
  110. }
  111. if ($writer) {
  112. try {
  113. $writer->close();
  114. } catch (Exception $e) {}
  115. }
  116. throw new BuildException($e);
  117. }
  118. }
  119. /**
  120. * "Encode" the password.
  121. */
  122. private final function mangle($password){
  123. $buf = "";
  124. for ($i = 0, $plen = strlen($password); $i < $plen; $i++) {
  125. $buf .= chr(self::$shifts[ord($password{$i})]);
  126. }
  127. return $buf;
  128. }
  129. /**
  130. * The CVS repository to add an entry for.
  131. * @param string $cvsRoot
  132. */
  133. public function setCvsroot($cvsRoot) {
  134. $this->cvsRoot = $cvsRoot;
  135. }
  136. /**
  137. * Password file to add the entry to.
  138. * @param PhingFile $passFile
  139. */
  140. public function setPassfile(PhingFile $passFile) {
  141. $this->passFile = $passFile;
  142. }
  143. /**
  144. * Password to be added to the password file.
  145. * @param string $password
  146. */
  147. public function setPassword($password) {
  148. $this->password = $password;
  149. }
  150. }