File: hook_emailcommit.php

package info (click to toggle)
php-svn 1.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 204 kB
  • ctags: 260
  • sloc: ansic: 3,053; xml: 281; php: 182; sh: 5; makefile: 5
file content (157 lines) | stat: -rwxr-xr-x 4,467 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php


/* 
  example email on commit hook script
  usage:
  php ..path..to..this..file../hook_emailcommit.php "$REPOS" "$REV" who@gets.it >> /tmp/svnlog 
  
  Features:
    - emails diff to email address
    - adds error messages if it's a PHP file.
    - sends popup messages to author on errors (using /hooks/popup.ini)
      (use www.realpoup.it for winxp boxes)
      
  TODO: 
    - write bindings for diff so that it doesnt have to use the command line..
  

*/
dl('svn.so');

class Subversion_EmailCommit {

    var $repos;
    var $rev;
    var $email; //who gets the commit messages.
    
    function start($args) {
        print_r($args);
        list( $cmd , $this->repos, $this->rev , $this->email ) = $args; 
        
        if ($this->repos{0} == '/') {
            $this->repos = $this->repos = 'file://'. $this->repos;
        }
        
        $this->rev  = (int) $this->rev;
        $last = $this->rev -1 ;
        // techncially where the diff is!?
        require_once 'System.php';
        $svn = System::which('svn','/usr/bin/svn');
        
        $cmd = "$svn diff -r{$last}:{$this->rev} $this->repos";
        $this->log = svn_log($this->repos, $this->rev,  $this->rev-1, 0, SVN_DISCOVER_CHANGED_PATHS);
        
        $syntax = $this->checkSyntax();
        //echo $cmd;
        
        
        $diff = `$cmd`;
        $diff = $this->log[0]['msg'] ."\n\n". $diff;
        
        
        if ($syntax) {
            $diff = $syntax ."\n\n". $diff;
        }
        
       
        $bits = explode('@', $this->email);
        
        $headers['From']    = "{$this->log[0]['author']} <{$this->log[0]['author']}@{$bits[1]}>";
        $headers['To']      = $this->email;
        $headers['Subject'] = "[SVN {$bits[1]}] ".
            ($syntax ? "ERROR!" : "") . 
            $this->getFilenames() . " ({$this->rev})";
            
        $headers['Date']    = date('r');
        $headers['X-Mailer']  = 'svn hook';
        // Create the mail object using the Mail::factory method
        require_once 'Mail.php';
        $mail_object =& Mail::factory('smtp', $params);
        $mail_object->send($this->email, $headers, $diff); 
        
        $this->sendPopup($syntax);
        
    }
    
    function sendPopup($syntax) {
        if (!$syntax) {
            
            return;
        }
        if (substr($this->repos,0,strlen("file://")) != "file://") {
        //    echo "repos is not file://";
            return;
        }
        $file = substr($this->repos,strlen("file://")) . '/hooks/popup.ini';
        if (!file_exists($file)) {
        //    echo "$file does not exist";
            return;
        }
        $ar = parse_ini_file($file);
        //print_r($ar);
        if (!isset($ar[$this->log[0]['author']])) {
            // no ip for this author.
            echo "no match for author";
            return;
        }
        $ip = $ar[$this->log[0]['author']];
        $cmd = "/usr/bin/smbclient -M {$this->log[0]['author']} -I  {$ip}";
        //echo $cmd;
        $fh = popen($cmd,'w');
        fwrite($fh, $data);
        // end
        fwrite($fh,chr(04));
        fclose($fh); 
    }
    
    
    
    
    function checkSyntax()
    {
        $ret = '';
        $ar = $this->log;
        foreach($ar[0]['paths'] as $action) {
            if (!in_array($action['action'],array('M','A'))) {
                continue;
            }
            if (!preg_match('#\.php$#', $action['path'])) {
                continue;
            }
            $tmp = ini_get('session.save_path') . '/'.uniqid('tmp_php.').'.php';
            
            $this->writeFile($tmp , 
                    svn_cat($this->repos . $action['path'],$this->rev));
            
            $data = $data = `/usr/bin/php -l $tmp`;
            unlink($tmp);
            if (preg_match('/^No syntax errors/',$data)) {
                continue;
            }
            $ret .= "Error in {$action['path']}\n".$data;
        }
        return strlen($ret) ? $ret : false; 
             
    }
    
    function writeFile($target,$data) 
    {
        $fh = fopen($target,'w');
        fwrite($fh, $data);
        fclose($fh);
    }
    
    function getFileNames()
    {
        $ar = $this->log;
        if (count($ar[0]['paths']) > 1) {
            return "Multiple Files";
        }
        return $ar[0]['paths'][0]['path'];
    }
     
}
ini_set('memory_limit','64M');
$x = new Subversion_EmailCommit;
$x->start($_SERVER['argv']);