Esempio n. 1
0
 /**
  * @param $request
  * @return array of validation messages
  */
 protected function validateRegisterRequest($request)
 {
     $messages = array();
     // email validations
     $emailNotEmpty = new \Zend\Validator\NotEmpty();
     $emailNotEmpty->setMessage('Email cannot be empty', \Zend\Validator\NotEmpty::IS_EMPTY);
     $emailValidEmail = new \Zend\Validator\EmailAddress();
     $emailValidEmail->setMessage('User email is not a valid email address. Use the basic format local-part@hostname', \Zend\Validator\EmailAddress::INVALID_FORMAT);
     $emailChain = new \Zend\Validator\ValidatorChain();
     $emailChain->attach($emailValidEmail);
     // is unique
     $user = $this->em()->getRepository('\\Application\\Entity\\User')->findBy(array('email' => $request['email']));
     if (count($user)) {
         $messages[] = "User with this email already exists";
     }
     // password validations
     $passwordNotEmpty = new \Zend\Validator\NotEmpty();
     $passwordNotEmpty->setMessage("User password cannot be empty", \Zend\Validator\NotEmpty::IS_EMPTY);
     $passwordStringLength = new \Zend\Validator\StringLength(['min' => 4, 'max' => 20]);
     $passwordStringLength->setMessage("User password is less than %min% characters long", \Zend\Validator\StringLength::TOO_SHORT);
     $passwordStringLength->setMessage("User password is more than %max% characters long", \Zend\Validator\StringLength::TOO_LONG);
     $passwordChain = new \Zend\Validator\ValidatorChain();
     $passwordChain->attach($passwordNotEmpty)->attach($passwordStringLength);
     if (!$passwordChain->isValid($request['password'])) {
         $messages = array_merge($messages, $passwordChain->getMessages());
     }
     if (!$emailChain->isValid($request['email'])) {
         $messages = array_merge($messages, $emailChain->getMessages());
     }
     return $messages;
 }
Esempio n. 2
0
 public function __construct($items = array(), $values = array())
 {
     /* run validator */
     foreach ($items as $key => $item) {
         /* apply default for unset value, and get value */
         if (isset($values[$key])) {
             $value = $values[$key];
             $items[$key]['value'] = $value;
         } else {
             if (isset($item['value'])) {
                 $value = $item['value'];
             } else {
                 $value = null;
                 $items[$key]['value'] = null;
             }
         }
         /* store values internally */
         $this->_values[$key] = $value;
         /* loop roles */
         foreach ($item as $role => $options) {
             /* exclude value */
             if ($role != 'value') {
                 /* prepare validation chain */
                 $chain = new \Zend\Validator\ValidatorChain();
                 /* attach role to chain */
                 switch ($role) {
                     case 'notempty':
                         $chain->attach(new \Zend\Validator\NotEmpty($options));
                         break;
                     case 'alnum':
                         $chain->attach(new \Zend\I18n\Validator\Alnum($options));
                         break;
                     case 'digits':
                         $chain->attach(new \Zend\Validator\Digits($options));
                         break;
                     case 'greaterthan':
                         $chain->attach(new \Zend\Validator\GreaterThan($options));
                         break;
                 }
                 /* validate, if invalid exit loop! */
                 if (!($this->_valid = $chain->isValid($value))) {
                     $this->_field = $key;
                     $this->_messages[$key] = $chain->getMessages();
                     break;
                 }
             }
             //value check
         }
         /* invalid? just exit loop */
         if (!$this->_valid) {
             break;
         }
     }
     /* store items internally */
     $this->items = $items;
 }
Esempio n. 3
0
 public function index19Action()
 {
     echo "<h3 style='color:red;font-weight:bold'>" . __METHOD__ . "</h3>";
     //username
     //khong duoc rong
     //chieu dai tu 5 -8
     //bat dau bang Z ket thuc bang 1 số
     $validator = new \Zend\Validator\ValidatorChain();
     if ($this->getRequest()->isPost()) {
         $username = $this->params()->fromPost("username");
         $validator->attach(new \Zend\Validator\NotEmpty(), true)->attach(new \Zend\Validator\StringLength(5, 8), true)->attach(new \Zend\Validator\Regex("#^z[a-zA-Z0-9_-]{3,6}[0-9]{1}\$#imsU"));
         if (!$validator->isValid($username)) {
             $message = $validator->getMessages();
             echo "<pre style='font-weight:bold'>";
             print_r($message);
             echo "</pre>";
         } else {
             echo "ok";
         }
     }
 }
Esempio n. 4
0
 /**
  * Call
  */
 public function call()
 {
     //print_r('MiddlewareTest middleware call method------');
     //print_r($this->next);
     //Read flash messaging from previous request if available
     //$this->loadMessages();
     //Prepare flash messaging for current request
     //$env = $this->app->environment();
     //$env['slim.flash'] = $this;
     // Create a validator chain and add validators to it
     $validatorChain = new \Zend\Validator\ValidatorChain();
     $validatorChain->attach(new \Zend\Validator\StringLength(array('min' => 6, 'max' => 12)))->attach(new \Zend\I18n\Validator\Alnum());
     // Validate the username
     if ($validatorChain->isValid("testteeewwwwwwwwwwwww__")) {
         // username passed validation
     } else {
         // username failed validation; print reasons
         /* foreach ($validatorChain->getMessages() as $message) {
                echo "$message\n";
            }*/
         //$this->app->redirect('/error');
         //$this->app->error();
         //$this->app->halt(500, "info status test!!!!");
         /*$this->app->contentType('application/json');
           $this->app->halt(500, '{"error":"Something went wrong"}');
           $this->app->stop();*/
         //exit();
         //$this->app->run();
         /*$response = $this->app->response();
         
                     //Generate Response headers
                     $response->header('Content-Type', "application/json");
                     $response->status(DEFAULT_RESPONSE_CODE);                  
                     $response->header('Content-Length', '500');
         
                     $responseBody = array('message'=> $message);
                     $response->body(json_encode($responseBody));
         
         
                     $response->send();*/
         //ob_clean();
         $publicHash = '3441df0babc2a2dda551d7cd39fb235bc4e09cd1e4556bf261bb49188f548348';
         $privateHash = 'e249c439ed7697df2a4b045d97d4b9b7e1854c3ff8dd668c779013653913572e';
         $content = json_encode(array('test' => 'content'));
         //$this->app->setPublicHash('3441df0babc2a2dda551d7cd39fb235bc4e09cd1e4556bf261bb49188f548348');
         //
         //print_r("------public hash---------".$this->app->getPublicHash()."------public hash---------");
         /*$hash = hash_hmac('sha256', $content, $privateHash);
         
                     $headers = array(
                         'X-Public: '.$publicHash,
                         'X-Hash: '.$hash
                     );
                     //ob_flush();
                     
                     
                     $ch = curl_init('http://localhost/slim_redirect_test/index.php/redirected_path');
                     curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
                     curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
                     curl_setopt($ch,CURLOPT_POSTFIELDS,$content);
         
                     $result = curl_exec($ch);
                     curl_close($ch);*/
         //ob_end_flush();
         /*ob_end_clean();
           $newURL = 'http://localhost/slim_redirect_test/index.php/redirected_path';
           header("Location: {$newURL}");*/
     }
     $validator = new \Zend\Validator\Barcode('EAN13');
     $floatValidator = new \Zend\I18n\Validator\IsFloat();
     if ($floatValidator->isValid(5.3)) {
         //print_r ("--float test edildi onaylandı---");
         $intValidator = new \Zend\I18n\Validator\IsInt();
     }
     $intValidator->setMessage("test validation");
     if ($intValidator->isValid(5)) {
         //print_r ("--int test edildi onaylandı---");
         $validator = new \Zend\Validator\StringLength();
     }
     $validator->setMax(6);
     $validator->isValid("Test");
     // returns true
     $validator->isValid("Testing");
     // returns false
     /*print_r($validator->isValid("Test"));
       print_r("fffffffffffffffffffff----    ");
       print_r($validator->isValid("Testing"));*/
     if (!$validator->isValid("Testing")) {
         //print_r("---is not valid----");
         $logger = new \Zend\Log\Logger();
     }
     $writer = new \Zend\Log\Writer\Stream('php://output');
     /*$logger->addWriter($writer);
       $logger->log(\Zend\Log\Logger::INFO, 'Informational message');
       $this->app->log->debug("test loggg");*/
     $this->next->call();
     //$this->save();
 }
Esempio n. 5
0
 public function isValid($tmp_name)
 {
     $chain = new \Zend\Validator\ValidatorChain();
     $chain->addValidator(new \Zend\Validator\File\FilesSize(array('min' => 100, 'max' => '2MB')));
     $chain->addValidator(new \Zend\Validator\File\Extension(array('jpg', 'jpeg', 'png', 'gif')));
     $chain->addValidator(new \Zend\Validator\File\MimeType(array('image/jpeg', 'image/png', 'image/jpg', 'image/gif')));
     if ($chain->isValid($tmp_name)) {
         return true;
     }
     $this->_errors = $chain->getMessages();
     return false;
 }
 /** this function called by indexAction to reduce complexity of function */
 protected function authenticate($form = null, $viewModel = null)
 {
     $request = $this->getRequest();
     if ($request->isPost()) {
         $authManager = $this->getServiceLocator()->get('authenticationManagerDefault');
         // Create a validator chain and add validators to it
         $validatorChain = new \Zend\Validator\ValidatorChain();
         $validatorChain->attach(new \Zend\Validator\StringLength(array('min' => 6, 'max' => 100)))->attach(new \Zend\Validator\NotEmpty())->attach(new \Zend\Validator\EmailAddress());
         // Validate the email
         if ($validatorChain->isValid($_POST['eposta'])) {
             $authManager->getAdapter()->setIdentity($_POST['eposta'])->setCredential($_POST['sifre']);
             $result = $authManager->authenticate();
             //print_r($result);
             if ($result->getCode() == 1) {
                 /**
                  * creating a public key for every login operation
                  * @author Mustafa Zeynel Dağlı
                  * @since 04/01/2016
                  */
                 $publicKey = $this->getServiceLocator()->get('servicePublicKeyGenerator');
                 //print_r($publicKey);
                 /**
                  * when public key not created service returns true,
                  * if public key true we should logout
                  * @author Mustafa Zeynel Dağlı
                  * @since 27/01/2016
                  */
                 if ($publicKey != true) {
                     $event = $this->getEvent();
                     $authManager->getStorage()->clear();
                     $response = $this->getResponse();
                     $url = $event->getRouter()->assemble(array('action' => 'index'), array('name' => 'sanalfabrika'));
                     $response->setHeaders($response->getHeaders()->addHeaderLine('Location', $url));
                     $response->setStatusCode(302);
                     $response->sendHeaders();
                     $event->stopPropagation();
                     exit;
                 }
                 $this->getServiceLocator()->setService('identity', $result->getIdentity());
                 //print_r($this->getServiceLocator()->get('identity'));
                 $userID = null;
                 $userIDService = $this->getServiceLocator()->get('serviceUserIDFinder');
                 if (is_integer($userIDService)) {
                     $userID = $userIDService;
                 }
                 $userID = $userIDService;
                 $authManager->getStorage()->write(array('id' => $userID, 'username' => $result->getIdentity(), 'ip_address' => $this->getRequest()->getServer('REMOTE_ADDR'), 'user_agent' => $request->getServer('HTTP_USER_AGENT'), 'pk' => $publicKey));
                 /**
                  * user role service will be tested
                  * @author Mustafa Zeynel Dağlı
                  * @since 28/01/2016
                  */
                 $this->getServiceLocator()->get('serviceRoleSessionWriter');
                 //print_r('---serviceRoleSessionWriter çağırıldı');
                 /**
                  * the public key cretaed is being inserted to database
                  * @author Mustafa Zeynel Dağlı
                  * @since 04/01/2016
                  */
                 $this->getServiceLocator()->get('servicePublicKeySaver');
                 //print_r('---servicePublicKeySaver çağırıldı');
                 //exit();
                 /**
                  * user login logged by rabbitMQ messaging
                  * @author Mustafa Zeynel Dağlı
                  * @since 17/03/2016
                  */
                 $this->getServiceLocator()->get('serviceLoginLogRabbitMQ');
                 /**
                  * redirecting after success
                  */
                 $this->getServiceLocator()->get('serviceAuthenticatedRedirectManager');
             }
         } else {
             $authManager->getStorage()->clear();
             $viewModel->notValidated = true;
         }
     }
 }
Esempio n. 7
0
<?php

require_once './autoloader.php';
$translator = Zend\I18n\Translator\Translator::factory(array('locale' => 'zh', 'translation_file_patterns' => array('zf' => array('type' => 'PhpArray', 'base_dir' => EVA_LIB_PATH . '/Zend/resources/languages/', 'pattern' => '%s/Zend_Validate.php'))));
Zend\Validator\AbstractValidator::setDefaultTranslator($translator);
$validatorChain = new Zend\Validator\ValidatorChain();
$validatorChain->addValidator(new Zend\Validator\StringLength(array('min' => 6, 'max' => 12)))->addValidator(new Zend\Validator\NotEmpty());
$username = '';
// Validate the username
if ($validatorChain->isValid($username)) {
    // username passed validation
} else {
    // username failed validation; print reasons
    foreach ($validatorChain->getMessages() as $message) {
        p($message);
    }
}