This file is indexed.

/usr/lib/nanoweb/modules/mod_pfilters.php is in nanoweb 2.2.9-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php

/// mod_pfilters - parser wrapping filters
///
/// <mario@erphesfurt·de>
///
///
///  # .nwaccess:
///
///     FilterEnable = 1
///     Filter =    */*      null
///     Filter =    */*      unchunk   128
///     Filter = text/html   pipe      /usr/bin/tidy -clean -latin1 -quiet
///     Filter = text/xhtml  pipe      /usr/bin/tidy -xml -q
///     Filter =    .bak     null
///     Filter = .html|doc   null
///
///  Note: "*/*" can now be abbreviated as "*" or left out


// ------------------------------------------ std nanoweb module corp ---


class mod_pfilters {


	function mod_pfilters() {

		$this->modtype="core_before_response";
		$this->modname="Filter support";

		// this one works with static, parsed and chunked:
		register_filter("null", new pfilter(false, "filter_null"), NW_PFILTER_ALL|NW_PFILTER_MORE);

		// any external program with this one:
		register_filter("pipe", new filter_pipe(false, "piping through arbitrary filter programs"), NW_PFILTER_ALL|NW_PFILTER_MORE);

		// this one makes many other filters happy:
		register_filter("unchunk", new filter_unchunk(false, "filter to resemble chunky parser content"), NW_PFILTER_CHUNKY);

	}



	function main() {

		global $pfilters, $lf, $out_contenttype, $chunky, $keepalive, $http_version;
		$used_filters = array();

		$chunky = !isset($lf->content_length) || ($lf->content_length == NW_PLF_MAGIC);

		$hbn = basename($GLOBALS["http_uri"]);
		$hbnext = array_flip(explode(".", $hbn));

		// assign filters
 		if (access_query("filterenable", 0))
		foreach (access_query("filter") as $filter_rule) {

			// split rule into: [ mime/ext _ filter _ fargs ]
			list($mimematch, $filter_rule) = explode(" ", ltrim($filter_rule), 2);
			@list($fname, $fargs) = explode(" ", ltrim($filter_rule), 2);
			$fargs = trim($fargs);

			// check for missing mime match
			if (empty($fname) || empty($pfilters[strtolower($fname)])) {
				if ((strpos($mimematch, "/")===false) && (strpos($mimematch, "|")===false) && (strpos($mimematch, "*")===false) && (strpos($mimematch, ".")===false)) {
					$fargs = $fname . " " . $fargs;
					$fname = $mimematch;
					$mimematch = "*/*";
				}
			}

			// is filter available
			$fname = strtolower($fname);
			if (empty($pfilters[$fname])) {
				techo("filter '$fname' not available", NW_EL_ERROR);
				continue;
			}

			// match mime / extension
			@list($mime, $uu) = explode(";", $out_contenttype, 2);
			$mime = trim($mime);
			$no_match = true;
			foreach (explode("|", $mimematch) as $match) {
				$ext = ltrim($match, ".");
				if (($match == $mime) || ($match == "*/*") || ($match == "*")
				 || (strpos($match, '*')!==false) && (strpos($mime, rtrim($match, "*"))!==false)
				 || ($hbn==$match) || isset($hbnext[$ext])  )
				{
					$no_match = false;
					break;
				}
			}
			if ($no_match) { continue; }

			// most filters _may_ be used once only
			if (@$used_filters[$fname]++ && !($pfilters[$fname][1] & NW_PFILTER_MORE)) {
				continue;
			}

			// convert to parser object (this should have been done already in the core)
			if (! is_object($lf)) {
				$lf = new static_response($lf);
			}

			// real-static or parsed/chunked
			$fflags = $pfilters[$fname][1];
			if (  (NW_PFILTER_IMMEDIATE & $fflags) || (NW_PFILTER_STATIC & $fflags) && is_a($lf, "static_response")  ) {

				// filter content on the fly / immediate
				$GLOBALS["first_chunk"]=true;
				$pfilters[$fname][0]->filter_func($lf->str, $fargs);
				$lf->content_length=strlen($lf->str);

				techo("filter '$fname' run on static \$lf content", NW_EL_DEBUG);

			}

			elseif (($fflags & NW_PFILTER_ALL) >= NW_PFILTER_PARSED) {

				if ($chunky && !($fflags & NW_PFILTER_CHUNKY)) {
					continue;
				}

				// create wrapper around current $lf
				$newf = $pfilters[$fname][0];
				$newf->pp = $lf;
				$newf->args = $fargs;
				$newf->fflags = $fflags;
				$newf->content_length = $lf->content_length;
				$lf = $newf;
				unset($newf);

				techo("filter object '$fname' wrapped around current \$lf object", NW_EL_DEBUG);

			}

		}#foreach(filter_rule)

		core_modules_hook("after_pfilters");

        }#main

}




// --------------------------------------------- global register func ---

	define("NW_PFILTER_STATIC", 1);     # really static
	define("NW_PFILTER_PARSED", 2);     # simple parsed (read at once)
	define("NW_PFILTER_CHUNKY", 4);     # content is read in multiple steps
	define("NW_PFILTER_ALL", 7);
	define("NW_PFILTER_MORE", 8);       # filter can be applied more than once
	define("NW_PFILTER_IMMEDIATE", 16); # applied as 'static'-filter

        function register_filter($name, $func, $pflags=NW_PFILTER_STATIC) {

		global $pfilters;

                if (is_object($func)) {
			$pfilters[$name] = array($func, $pflags);
		}
		else {
			$pfilters[$name] = array(new pfilter($func, $func), $pflags);
                }

        }


// ---------------------------------------------- generic filter class ---


class pfilter {

	var $pp;                         // "parser parent"
	var $modtype = "pfilter";

	function pfilter($func=false, $name=false) {

		if ($func && function_exists($func)) {
			$this->real_filter_func = $func;
		}

		if (!$name) { $name = get_class($this); }
		$this->modname = $name;
        }

	function filter_func(&$lf, $args) {

		if ($func = $this->real_filter_func) {

			$func($lf, $args);
		}
        }

	function parser_open($args, $filename, $rq_err, $cgi_headers) {

		return($this->pp->parser_open($args, $filename, $rq_err, $cgi_headers));
        }

	function parser_eof() {

		return($this->pp->parser_eof());
        }

	function parser_close() {

		unset($this->content_length);
		return($this->pp->parser_close());
        }

	function parser_get_output() {

		$this->content_length = $this->pp->content_length;

		$tmp = $this->pp->parser_get_output();

		$this->filter_func($tmp, $this->args);
		techo("pfilter(" . get_class($this) .")->content_length=" . $this->content_length, NW_EL_DEBUG);

		return($tmp);
	}

}


// ---------------------------------------------------- core filters ---

class filter_pipe extends pfilter {


	function filter_func(&$lf, $f_args) {

		if ($GLOBALS["chunky"]) {
			techo("WARN: you must run the 'unchunk' filter before using 'pipe'!", NW_EL_WARNING);
			return;
		}

		list($f_prog, $f_args) = explode(" ", $f_args, 2);

		if (is_executable($f_prog)) {

			$lf_tmpfile = tempnam($conf["global"]["tempdirectory"][0], "nweb_filter.");
			$fp = fopen($lf_tmpfile, NW_BSAFE_WRITE_OPEN);
			fwrite($fp, $lf);
			fclose($fp);

			$f_cmd = $f_prog . " " . $f_args  . " < " . $lf_tmpfile;

			if ($fp = @popen($f_cmd, NW_BSAFE_READ_OPEN)) {

				$line = 0;
				while (! feof($fp)) {
					if (! ($line++)) { $lf = ""; }
					$lf .= fgets($fp);
				}

				pclose($fp);

			}
			#<off># else techo("WARN: cannot open pipe to '".$f_prog."'");

			$this->content_length = strlen($lf);

			unlink($lf_tmpfile);

		}

	}

}



class filter_unchunk extends pfilter {

	function parser_get_output() {

		global $chunky, $first_chunk;

		$tmp = "";

		$maxsize = $this->args ? $this->args * 1024 : 131072;

		while ((strlen($tmp) < $maxsize) && !($eof = $this->pp->parser_eof())) {
			$tmp .= $this->pp->parser_get_output();
		}

		$this->content_length = $this->pp->content_length;

		if ($eof && $first_chunk) {
			$chunky = false;
			$this->content_length = strlen($tmp);
		}

		return($tmp);
	}

}


?>