123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- <?php
- require_once "phing/Task.php";
- require_once 'phing/system/io/PhingFile.php';
- class ManifestTask extends Task
- {
- var $taskname = 'manifest';
-
-
- private $action = 'w';
-
-
- private $destFile = null;
-
-
- private $filesets = array();
-
- private $checksum = false;
-
-
- private $salt = '';
-
-
- private $meta = array('totalFileCount' => 0,'totalFileSize' => 0);
-
-
-
- public function setFile(PhingFile $file)
- {
- $this->file = $file;
- }
-
- public function setChecksum($mixed)
- {
- if(is_string($mixed)) {
- $data = array(strtolower($mixed));
- if(strpos($data[0],',')) {
- $data = explode(',',$mixed);
- }
-
- $this->checksum = $data;
-
- } elseif($mixed === true) {
- $this->checksum = array('md5');
-
- }
- }
-
-
- public function setSalt($string)
- {
- $this->salt = $string;
- }
-
-
- public function createFileSet()
- {
- $num = array_push($this->filesets, new FileSet());
- return $this->filesets[$num-1];
- }
-
- public function init()
- {
-
- }
-
- public function main()
- {
- $this->validateAttributes();
-
- if($this->action == 'w') {
- $this->write();
-
- } elseif($this->action == 'r') {
- $this->read();
-
- }
- }
-
- private function write()
- {
- $project = $this->getProject();
-
- if(!touch($this->file->getPath())) {
- throw new BuildException("Unable to write to ".$this->file->getPath().".");
- }
- $this->log("Writing to " . $this->file->__toString(), Project::MSG_INFO);
- if(is_array($this->checksum)) {
- $this->log("Using " . implode(', ',$this->checksum)." for checksuming.", Project::MSG_INFO);
- }
-
- foreach($this->filesets as $fs) {
-
- $dir = $fs->getDir($this->project)->getPath();
- $ds = $fs->getDirectoryScanner($project);
- $fromDir = $fs->getDir($project);
- $srcFiles = $ds->getIncludedFiles();
- $srcDirs = $ds->getIncludedDirectories();
- foreach($ds->getIncludedFiles() as $file_path) {
- $line = $file_path;
- if($this->checksum) {
- foreach($this->checksum as $algo) {
- if(!$hash = $this->hashFile($dir.'/'.$file_path,$algo)) {
- throw new BuildException("Hashing $dir/$file_path with $algo failed!");
- }
- $line .= "\t".$hash;
- }
- }
- $line .= "\n";
- $manifest[] = $line;
- $this->log("Adding file ".$file_path,Project::MSG_VERBOSE);
- $this->meta['totalFileCount'] ++;
- $this->meta['totalFileSize'] += filesize($dir.'/'.$file_path);
- }
-
- }
-
- file_put_contents($this->file,$manifest);
-
- $this->log("Done. Total files: ".$this->meta['totalFileCount'].". Total file size: ".$this->meta['totalFileSize']." bytes.", Project::MSG_INFO);
- }
-
-
- private function read()
- {
- throw new BuildException("Checking against manifest not yet supported.");
- }
-
-
- private function hash($msg,$algo)
- {
- if(extension_loaded('hash')) {
- $algo = strtolower($algo);
-
- if(in_array($algo,hash_algos())) {
- return hash($algo,$this->salt.$msg);
- }
-
- }
-
- if(extension_loaded('mhash')) {
- $algo = strtoupper($algo);
-
- if(defined('MHASH_'.$algo)) {
- return mhash('MHASH_'.$algo,$this->salt.$msg);
-
- }
- }
-
- switch(strtolower($algo)) {
- case 'md5':
- return md5($this->salt.$msg);
- case 'crc32':
- return abs(crc32($this->salt.$msg));
- }
-
- return false;
- }
-
-
- private function hashFile($file,$algo)
- {
- if(!file_exists($file)) {
- return false;
- }
-
- $msg = file_get_contents($file).filesize($file).filemtime($file);
-
- return $this->hash($msg,$algo);
- }
-
-
- protected function validateAttributes()
- {
- if($this->action != 'r' && $this->action != 'w') {
- throw new BuildException("'action' attribute has non valid value. Use 'r' or 'w'");
- }
-
- if(empty($this->salt)) {
- $this->log("No salt provided. Specify one with the 'salt' attribute.", Project::MSG_WARN);
- }
-
- if (is_null($this->file) && count($this->filesets) === 0) {
- throw new BuildException("Specify at least sources and destination - a file or a fileset.");
- }
- if (!is_null($this->file) && $this->file->exists() && $this->file->isDirectory()) {
- throw new BuildException("Destination file cannot be a directory.");
- }
-
- }
- }
|