/adei/ui

To get this branch, use:
bzr branch http://darksoft.org/webbzr/adei/ui

« back to all changes in this revision

Viewing changes to classes/stream/appfilter.php

  • Committer: Suren A. Chilingaryan
  • Date: 2008-04-02 10:23:22 UTC
  • Revision ID: csa@dside.dyndns.org-20080402102322-okib92sicg2dx3o3
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
class APPFilter extends IOFilter {
 
4
 var $opts;
 
5
 var $start_opts;
 
6
 var $end_opts;
 
7
 
 
8
 var $app_block_mode;
 
9
 
 
10
 function __construct(&$info = NULL, STREAMWriterInterface $output = NULL) {
 
11
 
 
12
    $this->writer = new IO();
 
13
 
 
14
    $all_opts = $info['opts'] . " " .$info['start_opts'] . " " . $info['end_opts'];
 
15
    if (preg_match_all("/@([\w\d]+)__[\w\d_]+@/", $all_opts, $m, PREG_PATTERN_ORDER)) {
 
16
        $this->specials = $m[1];
 
17
    }
 
18
 
 
19
    if ($info['opts'])  {
 
20
        if (strpos($info['opts'], "@TMPFILE@") !== false) {
 
21
            parent::__construct($info, $output, NULL, false);
 
22
        } else {
 
23
            parent::__construct($info, $output, NULL, NULL);
 
24
        }
 
25
    } else {
 
26
        parent::__construct($info, $output, NULL, NULL);
 
27
    }
 
28
    
 
29
    if (isset($this->info['groupmode']))
 
30
        $this->app_block_mode = $this->info['groupmode'];
 
31
    else
 
32
        $this->app_block_mode = true;
 
33
 }
 
34
 
 
35
 static function DoReplace ($m) {
 
36
    $expr = "if (" . $m[1] . ") \$res=\"" . $m[2] . "\"; else \$res=\"" . $m[5] . "\";";
 
37
    if (@eval($expr) === false) {
 
38
        if (preg_match("/(@[\w\d_]+@)/", $expr, $mm)) 
 
39
            throw new ADEIException(translate("APPFilter. Unknown option (%s) is supplied", $mm[1]));
 
40
        throw new ADEIException(translate("APPFilter. Invalid conditional (%s)?(%s):(%s)", $m[1], $m[2], $m[5]));
 
41
    }
 
42
    return $res;
 
43
 }
 
44
 
 
45
 function ExpandConditionals(&$str) {
 
46
    return preg_replace_callback("/\?{([^?]+)\?((\\\\{|\\\\}|\\\\:|[^{}:])+)(:((\\\\{|\\\\}|\\\\:|[^{}:])+))?}/", 'APPFilter::DoReplace', $str);
 
47
 }
 
48
 
 
49
 function Configure(&$args = NULL, $semode = false) {
 
50
    if (($this->info['opts'])||(($semode)&&(($this->info['start_opts'])||($this->info['end_opts'])))) {
 
51
        if ($this->filewriter) {
 
52
            if ($args['tmpfile']) $tmpfile = $args['tmpfile'];
 
53
            else $tmpfile = $this->tmpfile;
 
54
 
 
55
            $pattern = array("/@TMPFILE@/");
 
56
            $replacement = array($tmpfile);
 
57
        } else {
 
58
            $pattern = array();
 
59
            $replacement = array();
 
60
        }
 
61
        
 
62
        if ($args) {
 
63
            foreach($args as $key => &$value) {
 
64
                array_push($pattern, "/@" . strtoupper($key) . "@/");
 
65
                array_push($replacement, $value);
 
66
            }
 
67
//          array_push($pattern, "/\?1\?([^:}]+)(:([^:}]+))?}/");
 
68
//          array_push($replacement, '${1}');
 
69
//          array_push($pattern, "/\?0\?([^:}]+)(:([^:}]+))?}/");
 
70
//          array_push($replacement, '${3}');
 
71
        }
 
72
        
 
73
        $this->opts = $this->ExpandConditionals(preg_replace($pattern, $replacement, $this->info['opts']));
 
74
        if ($semode) {
 
75
            if ($this->info['start_opts'])
 
76
                $this->start_opts = $this->ExpandConditionals(preg_replace($pattern, $replacement, $this->info['start_opts']));
 
77
            else
 
78
                $this->start_opts = false;          
 
79
            if ($this->info['end_opts'])
 
80
                $this->end_opts = $this->ExpandConditionals(preg_replace($pattern, $replacement, $this->info['end_opts']));
 
81
            else
 
82
                $this->end_opts = false;
 
83
        }
 
84
    } else {
 
85
        $this->opts = false;
 
86
        if ($semode) {
 
87
            $this->start_opts = false;
 
88
            $this->end_opts = false;
 
89
        }
 
90
    }
 
91
 }
 
92
 
 
93
 function OpenFilter() {
 
94
    $app = adei_app($this->info['app'], $this->opts, true);
 
95
//    echo $app . "\n";
 
96
    if ($this->filewriter) {
 
97
        $this->proc = popen($app, "w");
 
98
 
 
99
        $pipes = array($this->proc, NULL);
 
100
    } else {
 
101
        $spec = array(
 
102
            0 => array("pipe", "r"),
 
103
            1 => array("pipe", "w")
 
104
        );
 
105
 
 
106
        $this->proc = proc_open($app, $spec, $pipes);
 
107
    }
 
108
 
 
109
    return $pipes;
 
110
 }
 
111
 
 
112
 
 
113
 function CloseFilter(STREAMWriterInterface $h = NULL) {
 
114
    if ($this->filewriter) {
 
115
        $ret = pclose($this->proc);
 
116
    } else {
 
117
        $ret = proc_close($this->proc);
 
118
    }
 
119
    if ($ret) {
 
120
        throw new ADEIException(translate("APPFilter (%s) is finished with error %u", $this->info['app'], $ret));
 
121
    }
 
122
 }
 
123
 
 
124
 
 
125
 
 
126
 function Open(&$args = NULL) {
 
127
    parent::Open($args);
 
128
 
 
129
    if (($this->info['start_app'])||($this->info['start_opts'])||($this->info['end_app'])||($this->info['end_opts'])) {
 
130
        $this->Configure($args, true);
 
131
    }
 
132
 
 
133
    if ($this->info['start_app']) {
 
134
        $start_app = adei_app($this->info['start_app'], $this->start_opts, true);
 
135
    } else if ($this->start_opts) {
 
136
        $start_app = adei_app($this->info['app'], $this->start_opts, true);
 
137
    } else {
 
138
        $start_app = false;
 
139
    }
 
140
 
 
141
    if ($start_app) {
 
142
        $res = popen($start_app, "r");
 
143
        if ($this->filewriter) {
 
144
            while (!feof($res)) fread($fp, STREAM::BUFER_SIZE);
 
145
        } else {
 
146
            if ($this->output) {
 
147
                while (!feof($res)) {
 
148
                    $this->output->WriteData(fread($fp, STREAM::BUFER_SIZE));
 
149
                }
 
150
            } else {
 
151
                $this->extra_data = "";
 
152
                while (!feof($res)) {
 
153
                    $this->extra_data .= fread($fp, STREAM::BUFER_SIZE);
 
154
                }
 
155
            }
 
156
        } 
 
157
        $ret = pclose($res);
 
158
        
 
159
        if ($ret)
 
160
            throw new ADEIException(translate("APPFilter (%s) [start] is finished with error %u", $this->info['app'], $ret));
 
161
    }
 
162
 
 
163
    if (!$this->app_block_mode) {
 
164
        parent::BlockStart($args, $this->OpenFilter(), $this->filewriter?0:STREAM::GIFT);
 
165
    }
 
166
 }
 
167
 
 
168
 function BlockStart(&$args = NULL, $flags = 0) {
 
169
    if ($this->app_block_mode) {
 
170
        $this->Configure($args);
 
171
        parent::BlockStart($args, $this->OpenFilter(), $this->filewriter?0:STREAM::GIFT);
 
172
    }
 
173
 }
 
174
 
 
175
 function BlockEnd(STREAMHandler $h = NULL) {
 
176
    if ($this->app_block_mode) {
 
177
        $res = parent::BlockEnd($h);
 
178
        $this->CloseFilter();
 
179
        return $res;
 
180
    }    
 
181
 }
 
182
 
 
183
 function Close(STREAMHandler $h = NULL, $flags = 0) {
 
184
    $res = "";
 
185
    if (!$this->app_block_mode) {
 
186
        $res = parent::BlockEnd($h);
 
187
        $this->CloseFilter();
 
188
    }
 
189
 
 
190
    if ($this->info['end_app']) {
 
191
        $end_app = adei_app($this->info['end_app'], $this->end_opts, true);
 
192
    } else if ($this->end_opts) {
 
193
        $end_app = adei_app($this->info['app'], $this->end_opts, true);
 
194
    } else {
 
195
        $end_app = false;
 
196
    }
 
197
 
 
198
    if ($end_app) {
 
199
        $res = popen($end_app, "r");
 
200
        if ($this->filewriter) {
 
201
            while (!feof($res)) fread($fp, STREAM::BUFER_SIZE);
 
202
        } else {
 
203
            if ($this->output) $h = $this->output;
 
204
            
 
205
            if ($h) {
 
206
                while (!feof($res)) {
 
207
                    $h->WriteData(fread($fp, STREAM::BUFER_SIZE));
 
208
                }           
 
209
            } else {
 
210
                while (!feof($res)) {
 
211
                    $res .= fread($fp, STREAM::BUFER_SIZE);
 
212
                }           
 
213
            }
 
214
        } 
 
215
        $ret = pclose($res);
 
216
        
 
217
        if ($ret)
 
218
            throw new ADEIException(translate("APPFilter (%s) [end] is finished with error %u", $this->info['app'], $ret));
 
219
 
 
220
    }
 
221
 
 
222
    if ($res) return $res . parent::Close();
 
223
    return parent::Close();
 
224
 }
 
225
 
 
226
}
 
227
 
 
228
 
 
229
?>
 
 
b'\\ No newline at end of file'