FileHashTask.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /*
  3. * $Id: FileHashTask.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. /**
  23. * fileHash
  24. *
  25. * Calculate either MD5 or SHA hash value of a specified file and retun the
  26. * value in a property
  27. *
  28. * @author Johan Persson <johan162@gmail.com>
  29. * @version $Id: FileHashTask.php 905 2010-10-05 16:28:03Z mrook $
  30. * @package phing.tasks.ext
  31. */
  32. class FileHashTask extends Task
  33. {
  34. /**
  35. * Property for File
  36. * @var PhingFile file
  37. */
  38. private $file;
  39. /**
  40. * Property to be set
  41. * @var string $property
  42. */
  43. private $propertyName = "filehashvalue";
  44. /**
  45. * Specify which hash algorithm to use.
  46. * 0 = MD5
  47. * 1 = SHA1
  48. *
  49. * @var integer $hashtype
  50. */
  51. private $hashtype=0;
  52. /**
  53. * Specify if MD5 or SHA1 hash should be used
  54. * @param integer $type 0=MD5, 1=SHA1
  55. */
  56. public function setHashtype($type)
  57. {
  58. $this->hashtype = $type;
  59. }
  60. /**
  61. * Which file for calculate the has value of
  62. * @param PhingFile $file
  63. */
  64. public function setFile($file)
  65. {
  66. $this->file = $file;
  67. }
  68. /**
  69. * Set the name of the property to use to return the has value
  70. * @param $property
  71. * @return
  72. */
  73. public function setPropertyName($property)
  74. {
  75. $this->propertyName = $property;
  76. }
  77. /**
  78. * Main-Method for the Task
  79. *
  80. * @return void
  81. * @throws BuildException
  82. */
  83. public function main()
  84. {
  85. $this->checkFile();
  86. $this->checkPropertyName();
  87. // read file
  88. if( (int)$this->hashtype === 0 ) {
  89. $this->log("Calculating MD5 hash from: ".$this->file);
  90. $hashValue = md5_file($this->file,false);
  91. }
  92. elseif( (int)$this->hashtype === 1 ) {
  93. $this->log("Calculating SHA1 hash from: ".$this->file);
  94. $hashValue = sha1_file($this->file,false);
  95. }
  96. else {
  97. throw new BuildException(
  98. sprintf('[FileHash] Unknown hashtype specified %d. Must be either 0 (=MD5) or 1 (=SHA1).',$this->hashtype));
  99. }
  100. // publish hash value
  101. $this->project->setProperty($this->propertyName, $hashValue);
  102. }
  103. /**
  104. * checks file attribute
  105. * @return void
  106. * @throws BuildException
  107. */
  108. private function checkFile()
  109. {
  110. // check File
  111. if ($this->file === null ||
  112. strlen($this->file) == 0) {
  113. throw new BuildException('[FileHash] You must specify an input file.', $this->file);
  114. }
  115. if( ! is_readable($this->file) ) {
  116. throw new BuildException(sprintf('[FileHash] Input file does not exist or is not readable: %s',$this->file));
  117. }
  118. }
  119. /**
  120. * checks property attribute
  121. * @return void
  122. * @throws BuildException
  123. */
  124. private function checkPropertyName()
  125. {
  126. if (is_null($this->propertyName) ||
  127. strlen($this->propertyName) === 0) {
  128. throw new BuildException('Property name for publishing hashvalue is not set');
  129. }
  130. }
  131. }