9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
15 * @package Phergie_Plugin_Censor
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie_Plugin_Censor
23 * Facilitates censoring of event content or discardment of events
24 * containing potentially offensive phrases depending on the value of the
25 * configuration setting censor.mode ('off', 'censor', 'discard'). Also
26 * provides access to a web service for detecting censored words so that
27 * other plugins may optionally integrate and adjust behavior accordingly to
28 * prevent discardment of events.
31 * @package Phergie_Plugin_Censor
32 * @author Phergie Development Team <team@phergie.org>
33 * @license http://phergie.org/license New BSD License
34 * @link http://pear.phergie.org/package/Phergie_Plugin_Censor
35 * @uses extension soap
37 class Phergie_Plugin_Censor extends Phergie_Plugin_Abstract
40 * SOAP client to interact with the CDYNE Profanity Filter API
47 * Checks for dependencies.
51 public function onLoad()
53 if (!extension_loaded('soap')) {
54 $this->fail('The PHP soap extension is required');
57 if (!in_array($this->config['censor.mode'], array('censor', 'discard'))) {
58 $this->plugins->removePlugin($this);
63 * Returns a "clean" version of a given string.
65 * @param string $string String to clean
67 * @return string Cleaned string
69 public function cleanString($string)
71 if (empty($this->soap)) {
72 $this->soap = new SoapClient('http://ws.cdyne.com/ProfanityWS/Profanity.asmx?wsdl');
74 $params = array('Text' => $string);
76 while ($attempts < 3) {
78 $response = $this->soap->SimpleProfanityFilter($params);
80 } catch (SoapFault $e) {
88 return $response->SimpleProfanityFilterResult->CleanText;
92 * Processes events before they are dispatched and either censors their
93 * content or discards them if they contain potentially offensive
98 public function preDispatch()
100 $events = $this->events->getEvents();
102 foreach ($events as $event) {
103 switch ($event->getType()) {
104 case Phergie_Event_Request::TYPE_PRIVMSG:
105 case Phergie_Event_Request::TYPE_ACTION:
106 case Phergie_Event_Request::TYPE_NOTICE:
107 $text = $event->getArgument(1);
108 $clean = $this->cleanString($text);
109 if ($text != $clean) {
110 if ($this->config['censor.mode'] == 'censor') {
111 $event->setArgument(1, $clean);
113 $this->events->removeEvent($event);