]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Blacklist/Nickname_blacklist.php
Merge branch '0.9.x' into tinymce
[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
48 class Nickname_blacklist extends Memcached_DataObject
49 {
50     public $__table = 'nickname_blacklist'; // table name
51     public $pattern;                        // string pattern
52     public $created;                        // datetime
53
54     /**
55      * Get an instance by key
56      *
57      * This is a utility method to get a single instance with a given key value.
58      *
59      * @param string $k Key to use to lookup
60      * @param mixed  $v Value to lookup
61      *
62      * @return Nickname_blacklist object found, or null for no hits
63      *
64      */
65
66     function staticGet($k, $v=null)
67     {
68         return Memcached_DataObject::staticGet('Nickname_blacklist', $k, $v);
69     }
70
71     /**
72      * return table definition for DB_DataObject
73      *
74      * @return array array of column definitions
75      */
76
77     function table()
78     {
79         return array('pattern' => DB_DATAOBJECT_STR + DB_DATAOBJECT_NOTNULL,
80                      'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
81     }
82
83     /**
84      * return key definitions for DB_DataObject
85      *
86      * @return array key definitions
87      */
88
89     function keys()
90     {
91         return array_keys($this->keyTypes());
92     }
93
94     /**
95      * return key definitions for Memcached_DataObject
96      *
97      * @return array key definitions
98      */
99
100     function keyTypes()
101     {
102         return array('pattern' => 'K');
103     }
104
105     /**
106      * Return a list of patterns to check
107      *
108      * @return array string patterns to check
109      */
110
111     static function getPatterns()
112     {
113         $patterns = self::cacheGet('nickname_blacklist:patterns');
114
115         if ($patterns === false) {
116
117             $patterns = array();
118
119             $nb = new Nickname_blacklist();
120
121             $nb->find();
122
123             while ($nb->fetch()) {
124                 $patterns[] = $nb->pattern;
125             }
126
127             self::cacheSet('nickname_blacklist:patterns', $patterns);
128         }
129
130         return $patterns;
131     }
132
133     /**
134      * Save new list of patterns
135      *
136      * @return array of patterns to check
137      */
138
139     static function saveNew($newPatterns)
140     {
141         $oldPatterns = self::getPatterns();
142
143         // Delete stuff that's old that not in new
144
145         $toDelete = array_diff($oldPatterns, $newPatterns);
146
147         // Insert stuff that's in new and not in old
148
149         $toInsert = array_diff($newPatterns, $oldPatterns);
150
151         foreach ($toDelete as $pattern) {
152             $nb = Nickname_blacklist::staticGet('pattern', $pattern);
153             if (!empty($nb)) {
154                 $nb->delete();
155             }
156         }
157
158         foreach ($toInsert as $pattern) {
159             $nb = new Nickname_blacklist();
160             $nb->pattern = $pattern;
161             $nb->created = common_sql_now();
162             $nb->insert();
163         }
164
165         self::blow('nickname_blacklist:patterns');
166     }
167
168     static function ensurePattern($pattern)
169     {
170         $nb = Nickname_blacklist::staticGet('pattern', $pattern);
171
172         if (empty($nb)) {
173             $nb = new Nickname_blacklist();
174             $nb->pattern = $pattern;
175             $nb->created = common_sql_now();
176             $nb->insert();
177             self::blow('nickname_blacklist:patterns');
178         }
179     }
180 }