]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Censor.php
MINOR: Please don't set a+x on files which are not being executed as shell script...
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Censor.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
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
13  *
14  * @category  Phergie
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
20  */
21
22 /**
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.
29  *
30  * @category Phergie
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
36  */
37 class Phergie_Plugin_Censor extends Phergie_Plugin_Abstract
38 {
39     /**
40      * SOAP client to interact with the CDYNE Profanity Filter API
41      *
42      * @var SoapClient
43      */
44     protected $soap;
45
46     /**
47      * Checks for dependencies.
48      *
49      * @return void
50      */
51     public function onLoad()
52     {
53         if (!extension_loaded('soap')) {
54             $this->fail('The PHP soap extension is required');
55         }
56
57         if (!in_array($this->config['censor.mode'], array('censor', 'discard'))) {
58             $this->plugins->removePlugin($this);
59         }
60     }
61
62     /**
63      * Returns a "clean" version of a given string.
64      *
65      * @param string $string String to clean
66      *
67      * @return string Cleaned string
68      */
69     public function cleanString($string)
70     {
71         if (empty($this->soap)) {
72             $this->soap = new SoapClient('http://ws.cdyne.com/ProfanityWS/Profanity.asmx?wsdl');
73         }
74         $params = array('Text' => $string);
75         $attempts = 0;
76         while ($attempts < 3) {
77             try {
78                 $response = $this->soap->SimpleProfanityFilter($params);
79                 break;
80             } catch (SoapFault $e) {
81                 $attempts++;
82                 sleep(1);
83             }
84         }
85         if ($attempts == 3) {
86             return $string;
87         }
88         return $response->SimpleProfanityFilterResult->CleanText;
89     }
90
91     /**
92      * Processes events before they are dispatched and either censors their
93      * content or discards them if they contain potentially offensive
94      * content.
95      *
96      * @return void
97      */
98     public function preDispatch()
99     {
100         $events = $this->events->getEvents();
101
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);
112                         } else {
113                             $this->events->removeEvent($event);
114                         }
115                     }
116                     break;
117             }
118         }
119     }
120 }