3
class APPFilter extends IOFilter {
10
function __construct(&$info = NULL, STREAMWriterInterface $output = NULL) {
12
$this->writer = new IO();
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];
20
if (strpos($info['opts'], "@TMPFILE@") !== false) {
21
parent::__construct($info, $output, NULL, false);
23
parent::__construct($info, $output, NULL, NULL);
26
parent::__construct($info, $output, NULL, NULL);
29
if (isset($this->info['groupmode']))
30
$this->app_block_mode = $this->info['groupmode'];
32
$this->app_block_mode = true;
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]));
45
function ExpandConditionals(&$str) {
46
return preg_replace_callback("/\?{([^?]+)\?((\\\\{|\\\\}|\\\\:|[^{}:])+)(:((\\\\{|\\\\}|\\\\:|[^{}:])+))?}/", 'APPFilter::DoReplace', $str);
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;
55
$pattern = array("/@TMPFILE@/");
56
$replacement = array($tmpfile);
59
$replacement = array();
63
foreach($args as $key => &$value) {
64
array_push($pattern, "/@" . strtoupper($key) . "@/");
65
array_push($replacement, $value);
67
// array_push($pattern, "/\?1\?([^:}]+)(:([^:}]+))?}/");
68
// array_push($replacement, '${1}');
69
// array_push($pattern, "/\?0\?([^:}]+)(:([^:}]+))?}/");
70
// array_push($replacement, '${3}');
73
$this->opts = $this->ExpandConditionals(preg_replace($pattern, $replacement, $this->info['opts']));
75
if ($this->info['start_opts'])
76
$this->start_opts = $this->ExpandConditionals(preg_replace($pattern, $replacement, $this->info['start_opts']));
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']));
82
$this->end_opts = false;
87
$this->start_opts = false;
88
$this->end_opts = false;
93
function OpenFilter() {
94
$app = adei_app($this->info['app'], $this->opts, true);
96
if ($this->filewriter) {
97
$this->proc = popen($app, "w");
99
$pipes = array($this->proc, NULL);
102
0 => array("pipe", "r"),
103
1 => array("pipe", "w")
106
$this->proc = proc_open($app, $spec, $pipes);
113
function CloseFilter(STREAMWriterInterface $h = NULL) {
114
if ($this->filewriter) {
115
$ret = pclose($this->proc);
117
$ret = proc_close($this->proc);
120
throw new ADEIException(translate("APPFilter (%s) is finished with error %u", $this->info['app'], $ret));
126
function Open(&$args = NULL) {
129
if (($this->info['start_app'])||($this->info['start_opts'])||($this->info['end_app'])||($this->info['end_opts'])) {
130
$this->Configure($args, true);
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);
142
$res = popen($start_app, "r");
143
if ($this->filewriter) {
144
while (!feof($res)) fread($fp, STREAM::BUFER_SIZE);
147
while (!feof($res)) {
148
$this->output->WriteData(fread($fp, STREAM::BUFER_SIZE));
151
$this->extra_data = "";
152
while (!feof($res)) {
153
$this->extra_data .= fread($fp, STREAM::BUFER_SIZE);
160
throw new ADEIException(translate("APPFilter (%s) [start] is finished with error %u", $this->info['app'], $ret));
163
if (!$this->app_block_mode) {
164
parent::BlockStart($args, $this->OpenFilter(), $this->filewriter?0:STREAM::GIFT);
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);
175
function BlockEnd(STREAMHandler $h = NULL) {
176
if ($this->app_block_mode) {
177
$res = parent::BlockEnd($h);
178
$this->CloseFilter();
183
function Close(STREAMHandler $h = NULL, $flags = 0) {
185
if (!$this->app_block_mode) {
186
$res = parent::BlockEnd($h);
187
$this->CloseFilter();
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);
199
$res = popen($end_app, "r");
200
if ($this->filewriter) {
201
while (!feof($res)) fread($fp, STREAM::BUFER_SIZE);
203
if ($this->output) $h = $this->output;
206
while (!feof($res)) {
207
$h->WriteData(fread($fp, STREAM::BUFER_SIZE));
210
while (!feof($res)) {
211
$res .= fread($fp, STREAM::BUFER_SIZE);
218
throw new ADEIException(translate("APPFilter (%s) [end] is finished with error %u", $this->info['app'], $ret));
222
if ($res) return $res . parent::Close();
223
return parent::Close();
b'\\ No newline at end of file'