File indexing completed on 2024-12-22 05:36:24

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 class Local_Validate_UsernameValid extends Zend_Validate_Abstract
0024 {
0025     const INVALID   = 'regexInvalid';
0026     const NOT_MATCH = 'regexNotMatch';
0027     const ERROROUS  = 'regexErrorous';
0028 
0029     /**
0030      * @var array
0031      */
0032     protected $_messageTemplates = array(
0033         self::INVALID   => "Invalid type given. String, integer or float expected",
0034         self::NOT_MATCH => "'%value%' does not match our rules for username",
0035         self::ERROROUS  => "There was an internal error while checking input",
0036     );
0037 
0038     /**
0039      * @param mixed $value
0040      * @param null  $context
0041      *
0042      * @return bool
0043      * @throws Zend_Exception
0044      */
0045     public function isValid($value, $context = null)
0046     {
0047         $value = (string)$value;
0048         $this->_setValue($value);
0049 
0050         return $this->isUsernameValid($value, $context);
0051     }
0052 
0053     /**
0054      * @param $member_data
0055      *
0056      * @return bool
0057      * @throws Zend_Validate_Exception
0058      */
0059     private function isUsernameValid($username, $context)
0060     {
0061         $usernameValidChars = new Zend_Validate_Regex('/^(?=.{4,20}$)(?![-])(?!.*[-]{2})[a-z0-9-]+(?<![-])$/');
0062 
0063         if (false == $usernameValidChars->isValid($username)) {
0064             $this->setMessages($usernameValidChars->getMessages());
0065 
0066             return false;
0067         }
0068 
0069         return true;
0070     }
0071 
0072 }