1<?php
2/**
3 * censor.class.php
4 *
5 * Copyright © 2006 Stephane Gully <stephane.gully@gmail.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, 51 Franklin St, Fifth Floor,
20 * Boston, MA  02110-1301  USA
21 */
22require_once dirname(__FILE__)."/../pfci18n.class.php";
23require_once dirname(__FILE__)."/../pfcuserconfig.class.php";
24require_once dirname(__FILE__)."/../pfcproxycommand.class.php";
25
26/**
27 * pfcProxyCommand_censor
28 * this proxy will filter bad words from messages
29 * @author Stephane Gully <stephane.gully@gmail.com>
30 */
31class pfcProxyCommand_censor extends pfcProxyCommand
32{
33  function run(&$xml_reponse, $p)
34  {
35    $clientid    = $p["clientid"];
36    $param       = $p["param"];
37    $sender      = $p["sender"];
38    $recipient   = $p["recipient"];
39    $recipientid = $p["recipientid"];
40
41    $c =& pfcGlobalConfig::Instance();
42    $u =& pfcUserConfig::Instance();
43
44    $cmdtocheck = array("send", "nick", "me");
45    if ( in_array($this->name, $cmdtocheck) )
46    {
47      $words     = $c->proxies_cfg[$this->proxyname]["words"];
48      $replaceby = $c->proxies_cfg[$this->proxyname]["replaceby"];
49      $regex     = $c->proxies_cfg[$this->proxyname]["regex"];
50
51      $patterns = array();
52      $replacements = array();
53      foreach($words as $w)
54      {
55        if ($regex)
56        {
57          // the words are regular expressions
58          $patterns[] = "/".$w."/ie";
59          $replacements[] = "'\\1'.str_repeat('$replaceby',strlen('\\2')).'\\3'";
60        }
61        else
62        {
63          // the words are simple words
64          $patterns[] = "/".preg_quote($w)."/i";
65          $replacements[] = str_repeat($replaceby,strlen($w));
66        }
67      }
68      $param = preg_replace($patterns, $replacements, $param);
69    }
70
71    // forward the command to the next proxy or to the final command
72    $p["clientid"]    = $clientid;
73    $p["param"]       = $param;
74    $p["sender"]      = $sender;
75    $p["recipient"]   = $recipient;
76    $p["recipientid"] = $recipientid;
77    return $this->next->run($xml_reponse, $p);
78  }
79}
80
81?>