public function indexAction()
    {
        $form = new RegisterForm();
        $request = $this->getRequest();
        if ($request->isPost()) {
            // Initializing User Model
            $user = new User();
            // Getting Data Posted
            $data = $request->getPost();
            $form->setInputFilter($user->getInputFilter());
            $form->setData($data);
            $sm = $this->getServiceLocator();
            $appArray = $sm->get('Config')['app'];
            $appId = base64_decode($data->appId);
            //var_dump($form->isValid());
            if (array_key_exists($appId, $appArray)) {
                if ($form->isValid()) {
                    if ($this->getUserTable()->isUniqueEmail($data->email)) {
                        $crypt = new Crypt();
                        $output = $crypt->encryptArrayResponse($data->email);
                        $from = '*****@*****.**';
                        $email = $data->email;
                        $assunto = 'Ativação Conta MarkSend';
                        $activationLink = $appArray[$appId]['url'] . 'user/activate/' . urlencode(base64_encode($output));
                        $mensagem = <<<EOD
                            <a href='{$activationLink}'>{$activationLink}</a>
EOD;
                        $ses = new SimpleEmailService('AKIAIX32JUETXGGVTYGA', '1/D6IFvP6VAs3yKsqTsh7l179nj7m5PBogwAYc23');
                        //cria uma nova instancia
                        $m = new SimpleEmailServiceMessage();
                        //seta valores definidos nas variaveis acima
                        $m->addTo($email);
                        $m->setFrom($from);
                        $m->setSubjectCharset('ISO-8859-1');
                        $m->setMessageCharset('ISO-8859-1');
                        $m->setSubject('=?UTF-8?B?' . base64_encode($assunto) . '?= ');
                        $m->setMessageFromString(NULL, $mensagem);
                        //envia email
                        $ses->sendEmail($m);
                        $user->exchangeArray($data);
                        $saveUser = $this->getUserTable()->saveUser($user);
                        if ($saveUser > 0) {
                            //Salvou
                            $response = array('success' => true);
                        } else {
                            //Deu algo errado e nao salvou =/
                            $response = array('success' => false, 'errorCode' => '1', 'message' => 'Not saved');
                        }
                    } else {
                        //Email ja Cadastrado
                        $response = array('success' => false, 'errorCode' => '2', 'message' => 'Email already register');
                    }
                } else {
                    // Form Invalido
                    $response = array('success' => false, 'errorCode' => '3', 'message' => 'Invalid Form');
                }
                $crypt = new Crypt();
                $output = $crypt->encryptArrayResponse($response);
            } else {
                //throw new \Exception('Invalid AppId');
                $crypt = new Crypt();
                $output = $crypt->encryptArrayResponse('deu ruim hein');
            }
        }
        $url = $appArray[$appId]['url'] . 'register/?q=' . $output;
        return $this->redirect()->toUrl($url);
    }
    public function registerAction()
    {
        $encrypted = $this->params()->fromQuery('q');
        $crypt = new Crypt();
        if ($crypt->decryptArrayResponse($encrypted) != false) {
            $decryptedArray = $crypt->decryptArrayResponse(json_encode($encrypted));
            if ($this->getUserTable()->isUniqueEmail($decryptedArray->response->email)) {
                $output = $crypt->encryptArrayResponse($decryptedArray->response->email);
                //var_dump($decryptedArray);
                $appId = base64_decode($decryptedArray->response->appId);
                $from = '*****@*****.**';
                $email = $decryptedArray->response->email;
                $assunto = 'Ativação Conta MarkSend';
                $activationLink = $this->getServiceLocator()->get('Config')['app'][$appId]['url'] . 'user/activate/' . urlencode(base64_encode($output));
                $mensagem = <<<EOD
                            <a href='{$activationLink}'>{$activationLink}</a>
EOD;
                $ses = new SimpleEmailService('AKIAIX32JUETXGGVTYGA', '1/D6IFvP6VAs3yKsqTsh7l179nj7m5PBogwAYc23');
                //cria uma nova instancia
                $m = new SimpleEmailServiceMessage();
                //seta valores definidos nas variaveis acima
                $m->addTo($email);
                $m->setFrom($from);
                $m->setSubjectCharset('ISO-8859-1');
                $m->setMessageCharset('ISO-8859-1');
                $m->setSubject('=?UTF-8?B?' . base64_encode($assunto) . '?= ');
                $m->setMessageFromString(NULL, $mensagem);
                //envia email
                $ses->sendEmail($m);
                // New User
                //var_dump($decryptedArray);
                $user = new User();
                $user->setEmail($decryptedArray->response->email);
                $user->setFirstName($decryptedArray->response->name);
                $user->setLastName($decryptedArray->response->surname);
                $user->setPassword($decryptedArray->response->pass);
                //var_dump($user);
                $saveUser = $this->getUserTable()->saveUser($user);
                if ($saveUser > 0) {
                    //Salvou
                    $response = array('success' => true);
                } else {
                    //Deu algo errado e nao salvou =/
                    $response = array('success' => false, 'errorCode' => '1', 'message' => 'Not saved');
                }
            } else {
                //Email ja Cadastrado
                $response = array('success' => false, 'errorCode' => '2', 'message' => 'Email already registered');
            }
            $sm = $this->getServiceLocator();
            $this->_adapter = $sm->get('zend_db_adapter');
            $output = $crypt->encryptArrayResponse($response);
            $result = new JsonModel(array('return' => $output));
            return $result;
        }
    }