Advertisement
gustavosg85

Untitled

Aug 10th, 2015
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.43 KB | None | 0 0
  1. /***************************** Module.config.php *****************************/
  2.  
  3. <?php
  4. namespace Application;
  5.  
  6. return array(
  7.     'router' => array(
  8.         'routes' => array(
  9.             'home' => array(
  10.                 'type' => 'segment',
  11.                 'options' => array(
  12.             'route'    => '[/:lang]/',
  13.                     'constraints'   =>  array(
  14.                         'lang'  =>  '[a-zA-Z][a-zA-Z0-9_-]*'
  15.                         ),
  16.                     'defaults' => array(
  17.                         'controller' => 'Application\Controller\Index',
  18.                         'action'     => 'index',
  19.                         'lang' =>  'en_US'
  20.                         ),
  21.                     ),
  22.                 'may_terminate' => true,
  23.                 'child_routes' => array(
  24.                     'registerCaptcha' => array(
  25.                         'type'    => 'segment',
  26.                         'options' => array(
  27.                             'route'    => '/[:action[/]]',
  28.                             'constraints' => array(
  29.                                 'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
  30.                                 ),
  31.                             'defaults' => array(
  32.                                 'action' => 'register',                    
  33.                                 ),
  34.                             ),
  35.                         ),
  36.  
  37.                     'captcha' => array(
  38.                         'type'    => 'segment',
  39.                         'options' => array(
  40.                             'route'    =>  '/captcha/[:id]',
  41.                             'constraints' => array(
  42.                                 'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
  43.                                 ),
  44.                             'defaults' => array(
  45.                                 'controller' => 'Application\Controller\Index',
  46.                                 'action'     => 'generate',
  47.                                 )
  48.                             )
  49.                         )
  50.                     )
  51.                 ),
  52.             'login' => array(
  53.                 'type' => 'segment',
  54.                 'options' => array(
  55.                     'route'    => '[/:lang]/login[/][:action][/:id]',
  56.                     'constraints'   =>  array(
  57.                         'lang'  =>  '[a-zA-Z][a-zA-Z0-9_-]*'),
  58.                     'defaults' => array(
  59.                         'controller' => 'Application\Controller\Login',
  60.                         'action'     => 'index',
  61.                         ),
  62.                     ),
  63.  
  64.                 ),
  65. [...]
  66.  
  67.  
  68. /***************************** ACTIONS *****************************/
  69. Action Forget My Password (In LoginController)
  70.  
  71. <?php
  72. public function forgetMyPasswordAction()
  73.     {
  74.         // todo @gustavo: criar função de esqueci senha
  75.  
  76.         $entityManager = $this->getEntityManager();
  77.  
  78.         $urlCaptcha = $this->getRequest()->getBaseUrl().'/home/captcha';
  79.  
  80.         // passing entityManager and urlCaptcha objects
  81.         $form = new UsuariosForm($entityManager, $urlCaptcha);
  82.  
  83.         // some stuff
  84.  
  85.         if ($this->request->isPost())
  86.         {
  87.             // stuff
  88.         }
  89.  
  90.         return array('form' => $form, 'lang' => $this->translator->getLocale());
  91.     }
  92. ?>
  93.  
  94. Action in IndexController:
  95. <?php
  96.     /**
  97.      * Generate Captcha
  98.      */
  99.     public function generateAction()
  100.     {
  101.         $response = $this->getResponse();
  102.         $response->getHeaders()->addHeaderLine('Content-Type', "image/png");
  103.  
  104.         $id = $this->params('id', false);
  105.  
  106.         if ($id) {
  107.  
  108.             $image = './data/captcha/' . $id;
  109.  
  110.             if (file_exists($image) !== false) {
  111.                 $imagegetcontent = @file_get_contents($image);
  112.  
  113.                 $response->setStatusCode(200);
  114.                 $response->setContent($imagegetcontent);
  115.  
  116.                 if (file_exists($image) == true) {
  117.                     unlink($image);
  118.                 }
  119.             }
  120.  
  121.         }
  122.  
  123.         return $response;
  124.     }
  125. ?>
  126.  
  127. /***************************** VIEW *****************************/
  128.  
  129. <?php
  130.  
  131. namespace Application;
  132.  
  133. $title = "Forget My Password";
  134. $this->headTitle($title);
  135. ?>
  136.  
  137. <p>
  138.     <a href="<?php echo $this->url("login", array('action'=>'forgetMyPassword', 'lang' => $locale))?>"><?php echo $this->escapeHtml($this->translate("Sign Up")) ?></a>
  139. </p>
  140.  
  141. <?php
  142.  
  143. $form->setAttribute('action' , $this->url('login', array('action' =>'forgetMyPassword', 'lang' => $locale)));
  144.  
  145. $form->prepare();
  146. echo $this->form()->openTag($form);
  147. echo $this->formRow($form->get('email'));
  148. echo $this->formRow($form->get('captcha'));
  149. echo $this->formSubmit($form->get('submit'));
  150. echo $this->form()->closeTag();
  151.  
  152. /***************************** FORM *****************************/
  153.  
  154. <?php
  155. namespace Application\Form\Usuarios;
  156.  
  157. use Doctrine\Common\Persistence\ObjectManager;
  158. use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator;
  159. use Zend\Captcha\Image as CaptchaImage;
  160. use Zend\Form\Form;
  161. use Zend\Form\Element;
  162. use Zend\Form\Element\Captcha;
  163. use Zend\InputFilter\InputFilter;
  164.  
  165. class UsuariosForm extends Form
  166. {
  167.  
  168.     /**
  169.      * undocumented function
  170.      *
  171.      * @return void
  172.      * @author
  173.      **/
  174.     public function __construct(ObjectManager $objectManager, $urlCaptcha = null)
  175.     {
  176.         parent::__construct('Usuarios');
  177.  
  178.         $this->setAttribute('enctype', 'multipart/form-data');
  179.         $this->setAttribute('method', 'post');
  180.  
  181.         $this->setHydrator(new DoctrineHydrator($objectManager, 'Application\Entity\Usuarios'));
  182.  
  183.         $dirdata = './data';
  184.         //pass captcha image options
  185.         $captchaImage = new CaptchaImage(  array(
  186.             'font' => $dirdata . '/fonts/Arial.ttf',
  187.             'width' => 250,
  188.             'height' => 100,
  189.             'dotNoiseLevel' => 40,
  190.             'lineNoiseLevel' => 3)
  191.         );
  192.         $captchaImage->setImgDir($dirdata.'/captcha');
  193.         $captchaImage->setImgUrl($urlCaptcha);
  194.  
  195.         $this->add(
  196.             array(
  197.                 'type'      =>      'hidden',
  198.                 'name'      =>      'idUsuarios'
  199.                 )
  200.                  //add captcha element...
  201.             ->add(array(
  202.                 'type' => 'Zend\Form\Element\Captcha',
  203.                 'name' => 'captcha',
  204.                 'options' => array(
  205.                     'label' => 'Please verify you are human',
  206.                     'captcha' => $captchaImage,
  207.                     ),
  208.                 )
  209.  
  210.              //add captcha element...
  211.             // @gustavo: Dummy Captcha (letters backwards and stuff)
  212.             // ->add(array(
  213.             //  'type' => 'Zend\Form\Element\Captcha',
  214.             //  'name' => 'captcha',
  215.             //  'options' => array(
  216.             //      'label' => 'Please verify you are human',
  217.             //      'captcha' => array(
  218.             //          'class' => 'Dumb'
  219.             //          ),
  220.             //      ),
  221.             //  )
  222.             )->add(array(
  223.                 'name'      =>      'submit',
  224.                 'attributes'    =>  array(
  225.                     'type'      =>  'submit',
  226.                     'value'     =>  'Go',
  227.                     'id'        =>  'submitbutton'
  228.                     )
  229.                 ));
  230.             }
  231.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement