]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Blacklist/BlacklistPlugin.php
Added more checked type-hints.
[quix0rs-gnu-social.git] / plugins / Blacklist / BlacklistPlugin.php
index d4a4c9626ad9d33f9af9391666b40332927dcfe9..1780f42e62736fdaedba9982513999251f10df8c 100644 (file)
@@ -42,7 +42,7 @@ if (!defined('STATUSNET')) {
  */
 class BlacklistPlugin extends Plugin
 {
-    const VERSION = STATUSNET_VERSION;
+    const VERSION = GNUSOCIAL_VERSION;
 
     public $nicknames = array();
     public $urls      = array();
@@ -80,27 +80,8 @@ class BlacklistPlugin extends Plugin
         $schema = Schema::get();
 
         // For storing blacklist patterns for nicknames
-        $schema->ensureTable('nickname_blacklist',
-                             array(new ColumnDef('pattern',
-                                                 'varchar',
-                                                 255,
-                                                 false,
-                                                 'PRI'),
-                                   new ColumnDef('created',
-                                                 'datetime',
-                                                 null,
-                                                 false)));
-
-        $schema->ensureTable('homepage_blacklist',
-                             array(new ColumnDef('pattern',
-                                                 'varchar',
-                                                 255,
-                                                 false,
-                                                 'PRI'),
-                                   new ColumnDef('created',
-                                                 'datetime',
-                                                 null,
-                                                 false)));
+        $schema->ensureTable('nickname_blacklist', Nickname_blacklist::schemaDef());
+        $schema->ensureTable('homepage_blacklist', Homepage_blacklist::schemaDef());
 
         return true;
     }
@@ -126,7 +107,9 @@ class BlacklistPlugin extends Plugin
         } else if (is_string($config)) {
             return explode("\r\n", $config);
         } else {
-            throw new Exception("Unknown data type for config $section + $setting");
+            // TRANS: Exception thrown if the Blacklist plugin configuration is incorrect.
+            // TRANS: %1$s is a configuration section, %2$s is a configuration setting.
+            throw new Exception(sprintf(_m('Unknown data type for config %1$s + %2$s.'),$section, $setting));
         }
     }
 
@@ -139,23 +122,25 @@ class BlacklistPlugin extends Plugin
      *
      * @return boolean hook value
      */
-    function onStartRegistrationTry($action)
+    function onStartRegisterUser(User &$user, Profile &$profile)
     {
-        $homepage = strtolower($action->trimmed('homepage'));
+        $homepage = strtolower($profile->homepage);
 
         if (!empty($homepage)) {
             if (!$this->_checkUrl($homepage)) {
-                $msg = sprintf(_m("You may not register with homepage '%s'."),
+                // TRANS: Validation failure for URL. %s is the URL.
+                $msg = sprintf(_m("You may not register with homepage \"%s\"."),
                                $homepage);
                 throw new ClientException($msg);
             }
         }
 
-        $nickname = strtolower($action->trimmed('nickname'));
+        $nickname = strtolower($profile->nickname);
 
         if (!empty($nickname)) {
             if (!$this->_checkNickname($nickname)) {
-                $msg = sprintf(_m("You may not register with nickname '%s'."),
+                // TRANS: Validation failure for nickname. %s is the nickname.
+                $msg = sprintf(_m("You may not register with nickname \"%s\"."),
                                $nickname);
                 throw new ClientException($msg);
             }
@@ -179,7 +164,8 @@ class BlacklistPlugin extends Plugin
 
         if (!empty($homepage)) {
             if (!$this->_checkUrl($homepage)) {
-                $msg = sprintf(_m("You may not use homepage '%s'."),
+                // TRANS: Validation failure for URL. %s is the URL.
+                $msg = sprintf(_m("You may not use homepage \"%s\"."),
                                $homepage);
                 throw new ClientException($msg);
             }
@@ -189,7 +175,8 @@ class BlacklistPlugin extends Plugin
 
         if (!empty($nickname)) {
             if (!$this->_checkNickname($nickname)) {
-                $msg = sprintf(_m("You may not use nickname '%s'."),
+                // TRANS: Validation failure for nickname. %s is the nickname.
+                $msg = sprintf(_m("You may not use nickname \"%s\"."),
                                $nickname);
                 throw new ClientException($msg);
             }
@@ -231,6 +218,7 @@ class BlacklistPlugin extends Plugin
         $url = htmlspecialchars_decode($url);
 
         if (!$this->_checkUrl($url)) {
+            // TRANS: Validation failure for URL. %s is the URL.
             $msg = sprintf(_m("You may not use URL \"%s\" in notices."),
                            $url);
             throw new ClientException($msg);
@@ -286,40 +274,16 @@ class BlacklistPlugin extends Plugin
     /**
      * Add our actions to the URL router
      *
-     * @param Net_URL_Mapper $m URL mapper for this hit
+     * @param URLMapper $m URL mapper for this hit
      *
      * @return boolean hook return
      */
-    function onRouterInitialized($m)
+    public function onRouterInitialized(URLMapper $m)
     {
-        $m->connect('admin/blacklist', array('action' => 'blacklistadminpanel'));
+        $m->connect('panel/blacklist', array('action' => 'blacklistadminpanel'));
         return true;
     }
 
-    /**
-     * Auto-load our classes if called
-     *
-     * @param string $cls Class to load
-     *
-     * @return boolean hook return
-     */
-    function onAutoload($cls)
-    {
-        switch (strtolower($cls))
-        {
-        case 'nickname_blacklist':
-        case 'homepage_blacklist':
-            include_once INSTALLDIR.'/plugins/Blacklist/'.ucfirst($cls).'.php';
-            return false;
-        case 'blacklistadminpanelaction':
-            $base = strtolower(mb_substr($cls, 0, -6));
-            include_once INSTALLDIR.'/plugins/Blacklist/'.$base.'.php';
-            return false;
-        default:
-            return true;
-        }
-    }
-
     /**
      * Plugin version data
      *
@@ -327,7 +291,7 @@ class BlacklistPlugin extends Plugin
      *
      * @return boolean hook value
      */
-    function onPluginVersion(&$versions)
+    function onPluginVersion(array &$versions)
     {
         $versions[] = array('name' => 'Blacklist',
                             'version' => self::VERSION,
@@ -335,6 +299,7 @@ class BlacklistPlugin extends Plugin
                             'homepage' =>
                             'http://status.net/wiki/Plugin:Blacklist',
                             'description' =>
+                            // TRANS: Plugin description.
                             _m('Keeps a blacklist of forbidden nickname '.
                                'and URL patterns.'));
         return true;
@@ -365,15 +330,17 @@ class BlacklistPlugin extends Plugin
      *
      * @return boolean hook value
      */
-    function onEndAdminPanelNav($nav)
+    function onEndAdminPanelNav(Menu $nav)
     {
         if (AdminPanelAction::canAdmin('blacklist')) {
 
             $action_name = $nav->action->trimmed('action');
 
             $nav->out->menuItem(common_local_url('blacklistadminpanel'),
-                                _m('Blacklist'),
-                                _m('Blacklist configuration'),
+                                // TRANS: Menu item in admin panel.
+                                _m('MENU','Blacklist'),
+                                // TRANS: Tooltip for menu item in admin panel.
+                                _m('TOOLTIP','Blacklist configuration.'),
                                 $action_name == 'blacklistadminpanel',
                                 'nav_blacklist_admin_panel');
         }
@@ -381,7 +348,7 @@ class BlacklistPlugin extends Plugin
         return true;
     }
 
-    function onEndDeleteUserForm($action, $user)
+    function onEndDeleteUserForm(Action $action, User $user)
     {
         $cur = common_current_user();
 
@@ -399,6 +366,7 @@ class BlacklistPlugin extends Plugin
         $action->elementStart('li');
         $this->checkboxAndText($action,
                                'blacklistnickname',
+                               // TRANS: Checkbox label in the blacklist user form.
                                _m('Add this nickname pattern to blacklist'),
                                'blacklistnicknamepattern',
                                $this->patternizeNickname($user->nickname));
@@ -408,6 +376,7 @@ class BlacklistPlugin extends Plugin
             $action->elementStart('li');
             $this->checkboxAndText($action,
                                    'blacklisthomepage',
+                                   // TRANS: Checkbox label in the blacklist user form.
                                    _m('Add this homepage pattern to blacklist'),
                                    'blacklisthomepagepattern',
                                    $this->patternizeHomepage($profile->homepage));
@@ -417,7 +386,7 @@ class BlacklistPlugin extends Plugin
         $action->elementEnd('ul');
     }
 
-    function onEndDeleteUser($action, $user)
+    function onEndDeleteUser(Action $action, User $user)
     {
         if ($action->boolean('blacklisthomepage')) {
             $pattern = $action->trimmed('blacklisthomepagepattern');
@@ -432,7 +401,7 @@ class BlacklistPlugin extends Plugin
         return true;
     }
 
-    function checkboxAndText($action, $checkID, $label, $textID, $value)
+    private function checkboxAndText(Action $action, $checkID, $label, $textID, $value)
     {
         $action->element('input', array('name' => $checkID,
                                         'type' => 'checkbox',
@@ -486,19 +455,23 @@ class BlacklistPlugin extends Plugin
 
         if (!empty($homepage)) {
             if (!$this->_checkUrl($homepage)) {
-                $msg = sprintf(_m("Users from '%s' blocked."),
+                // TRANS: Exception thrown trying to post a notice while having set a blocked homepage URL. %s is the blocked URL.
+                $msg = sprintf(_m("Users from \"%s\" are blocked."),
                                $homepage);
                 throw new ClientException($msg);
             }
         }
 
-        $nickname = strtolower($actor->poco->preferredUsername);
+        if (!empty($actor->poco)) {
+            $nickname = strtolower($actor->poco->preferredUsername);
 
-        if (!empty($nickname)) {
-            if (!$this->_checkNickname($nickname)) {
-                $msg = sprintf(_m("Posts from nickname '%s' disallowed."),
-                               $nickname);
-                throw new ClientException($msg);
+            if (!empty($nickname)) {
+                if (!$this->_checkNickname($nickname)) {
+                    // TRANS: Exception thrown trying to post a notice while having a blocked nickname. %s is the blocked nickname.
+                    $msg = sprintf(_m("Notices from nickname \"%s\" are disallowed."),
+                                   $nickname);
+                    throw new ClientException($msg);
+                }
             }
         }
 
@@ -508,8 +481,7 @@ class BlacklistPlugin extends Plugin
     /**
      * Check URLs and homepages for blacklisted users.
      */
-
-    function onStartSubscribe($subscriber, $other)
+    function onStartSubscribe(Profile $subscriber, Profile $other)
     {
         foreach (array($other->profileurl, $other->homepage) as $url) {
 
@@ -520,7 +492,8 @@ class BlacklistPlugin extends Plugin
             $url = strtolower($url);
 
             if (!$this->_checkUrl($url)) {
-                $msg = sprintf(_m("Users from '%s' blocked."),
+                // TRANS: Client exception thrown trying to subscribe to a person with a blocked homepage or site URL. %s is the blocked URL.
+                $msg = sprintf(_m("Users from \"%s\" are blocked."),
                                $url);
                 throw new ClientException($msg);
             }
@@ -530,7 +503,8 @@ class BlacklistPlugin extends Plugin
 
         if (!empty($nickname)) {
             if (!$this->_checkNickname($nickname)) {
-                $msg = sprintf(_m("Can't subscribe to nickname '%s'."),
+                // TRANS: Client exception thrown trying to subscribe to a person with a blocked nickname. %s is the blocked nickname.
+                $msg = sprintf(_m("Cannot subscribe to nickname \"%s\"."),
                                $nickname);
                 throw new ClientException($msg);
             }