Nickname_blacklist.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
  33. /**
  34. * Data class for Nickname blacklist
  35. *
  36. * @category Action
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  40. * @link http://status.net/
  41. *
  42. * @see DB_DataObject
  43. */
  44. class Nickname_blacklist extends Managed_DataObject
  45. {
  46. public $__table = 'nickname_blacklist'; // table name
  47. public $pattern; // varchar(255) pattern
  48. public $created; // datetime not_null
  49. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  50. public static function schemaDef()
  51. {
  52. return array(
  53. 'fields' => array(
  54. 'pattern' => array('type' => 'varchar', 'not null' => true, 'length' => 255, 'description' => 'blacklist pattern'),
  55. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  56. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  57. ),
  58. 'primary key' => array('pattern'),
  59. );
  60. }
  61. /**
  62. * Return a list of patterns to check
  63. *
  64. * @return array string patterns to check
  65. */
  66. static function getPatterns()
  67. {
  68. $patterns = self::cacheGet('nickname_blacklist:patterns');
  69. if ($patterns === false) {
  70. $patterns = array();
  71. $nb = new Nickname_blacklist();
  72. $nb->find();
  73. while ($nb->fetch()) {
  74. $patterns[] = $nb->pattern;
  75. }
  76. self::cacheSet('nickname_blacklist:patterns', $patterns);
  77. }
  78. return $patterns;
  79. }
  80. /**
  81. * Save new list of patterns
  82. *
  83. * @return array of patterns to check
  84. */
  85. static function saveNew($newPatterns)
  86. {
  87. $oldPatterns = self::getPatterns();
  88. // Delete stuff that's old that not in new
  89. $toDelete = array_diff($oldPatterns, $newPatterns);
  90. // Insert stuff that's in new and not in old
  91. $toInsert = array_diff($newPatterns, $oldPatterns);
  92. foreach ($toDelete as $pattern) {
  93. $nb = Nickname_blacklist::getKV('pattern', $pattern);
  94. if (!empty($nb)) {
  95. $nb->delete();
  96. }
  97. }
  98. foreach ($toInsert as $pattern) {
  99. $nb = new Nickname_blacklist();
  100. $nb->pattern = $pattern;
  101. $nb->created = common_sql_now();
  102. $nb->insert();
  103. }
  104. self::blow('nickname_blacklist:patterns');
  105. }
  106. static function ensurePattern($pattern)
  107. {
  108. $nb = Nickname_blacklist::getKV('pattern', $pattern);
  109. if (empty($nb)) {
  110. $nb = new Nickname_blacklist();
  111. $nb->pattern = $pattern;
  112. $nb->created = common_sql_now();
  113. $nb->insert();
  114. self::blow('nickname_blacklist:patterns');
  115. }
  116. }
  117. }