]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Blacklist/Nickname_blacklist.php
Merge ActivitySpam plugin
[quix0rs-gnu-social.git] / plugins / Blacklist / Nickname_blacklist.php
1 <?php
2 /**
3  * Data class for nickname blacklisting
4  *
5  * PHP version 5
6  *
7  * @category Data
8  * @package  StatusNet
9  * @author   Evan Prodromou <evan@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2009, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
35
36 /**
37  * Data class for Nickname blacklist
38  *
39  * @category Action
40  * @package  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
43  * @link     http://status.net/
44  *
45  * @see      DB_DataObject
46  */
47 class Nickname_blacklist extends Memcached_DataObject
48 {
49     public $__table = 'nickname_blacklist'; // table name
50     public $pattern;                        // string pattern
51     public $created;                        // datetime
52
53     /**
54      * Get an instance by key
55      *
56      * This is a utility method to get a single instance with a given key value.
57      *
58      * @param string $k Key to use to lookup
59      * @param mixed  $v Value to lookup
60      *
61      * @return Nickname_blacklist object found, or null for no hits
62      *
63      */
64     function staticGet($k, $v=null)
65     {
66         return Memcached_DataObject::staticGet('Nickname_blacklist', $k, $v);
67     }
68
69     /**
70      * return table definition for DB_DataObject
71      *
72      * @return array array of column definitions
73      */
74     function table()
75     {
76         return array('pattern' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
77                      'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
78     }
79
80     /**
81      * return key definitions for DB_DataObject
82      *
83      * @return array key definitions
84      */
85     function keys()
86     {
87         return array_keys($this->keyTypes());
88     }
89
90     /**
91      * return key definitions for Memcached_DataObject
92      *
93      * @return array key definitions
94      */
95     function keyTypes()
96     {
97         return array('pattern' => 'K');
98     }
99
100     /**
101      * Return a list of patterns to check
102      *
103      * @return array string patterns to check
104      */
105     static function getPatterns()
106     {
107         $patterns = self::cacheGet('nickname_blacklist:patterns');
108
109         if ($patterns === false) {
110
111             $patterns = array();
112
113             $nb = new Nickname_blacklist();
114
115             $nb->find();
116
117             while ($nb->fetch()) {
118                 $patterns[] = $nb->pattern;
119             }
120
121             self::cacheSet('nickname_blacklist:patterns', $patterns);
122         }
123
124         return $patterns;
125     }
126
127     /**
128      * Save new list of patterns
129      *
130      * @return array of patterns to check
131      */
132     static function saveNew($newPatterns)
133     {
134         $oldPatterns = self::getPatterns();
135
136         // Delete stuff that's old that not in new
137         $toDelete = array_diff($oldPatterns, $newPatterns);
138
139         // Insert stuff that's in new and not in old
140         $toInsert = array_diff($newPatterns, $oldPatterns);
141
142         foreach ($toDelete as $pattern) {
143             $nb = Nickname_blacklist::staticGet('pattern', $pattern);
144             if (!empty($nb)) {
145                 $nb->delete();
146             }
147         }
148
149         foreach ($toInsert as $pattern) {
150             $nb = new Nickname_blacklist();
151             $nb->pattern = $pattern;
152             $nb->created = common_sql_now();
153             $nb->insert();
154         }
155
156         self::blow('nickname_blacklist:patterns');
157     }
158
159     static function ensurePattern($pattern)
160     {
161         $nb = Nickname_blacklist::staticGet('pattern', $pattern);
162
163         if (empty($nb)) {
164             $nb = new Nickname_blacklist();
165             $nb->pattern = $pattern;
166             $nb->created = common_sql_now();
167             $nb->insert();
168             self::blow('nickname_blacklist:patterns');
169         }
170     }
171 }