1: <?php
2: /**
3: * Filters the given text based on the words found in a word list
4: * file.
5: *
6: * Parameters:
7: * <pre>
8: * words_file -- Filename containing the words to replace.
9: * replacement -- The replacement string. Defaults to "*****".
10: * </pre>
11: *
12: * Copyright 2004-2012 Horde LLC (http://www.horde.org/)
13: *
14: * See the enclosed file COPYING for license information (LGPL). If you
15: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
16: *
17: * @author Jan Schneider <jan@horde.org>
18: * @category Horde
19: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
20: * @package Text_Filter
21: */
22: class Horde_Text_Filter_Words extends Horde_Text_Filter_Base
23: {
24: /**
25: * Filter parameters.
26: *
27: * @var array
28: */
29: protected $_params = array(
30: 'replacement' => '*****'
31: );
32:
33: /**
34: * Returns a hash with replace patterns.
35: *
36: * @return array Patterns hash.
37: */
38: public function getPatterns()
39: {
40: $regexp = array();
41:
42: if (is_readable($this->_params['words_file'])) {
43: /* Read the file and iterate through the lines. */
44: $lines = file($this->_params['words_file']);
45: foreach ($lines as $line) {
46: /* Strip whitespace and comments. */
47: $line = preg_replace('|#.*$|', '', trim($line));
48:
49: /* Filter the text. */
50: if (!empty($line)) {
51: $regexp["/(\b(\w*)$line\b|\b$line(\w*)\b)/i"] = $this->_getReplacement($line);
52: }
53: }
54: }
55:
56: return array('regexp' => $regexp);
57: }
58:
59: /**
60: * Returns a hash with replace patterns.
61: *
62: * @return array Patterns hash.
63: */
64: protected function _getReplacement($line)
65: {
66: return $this->_params['replacement']
67: ? $this->_params['replacement']
68: :substr($line, 0, 1) . str_repeat('*', strlen($line) - 1);
69: }
70:
71: }
72: