예제 #1
0
파일: Error.php 프로젝트: madoka8/ice-base
 /**
  * Error constructor
  *
  * @param string $message
  * @param int $code
  * @param mixed $previous
  */
 public function __construct($message, $code = 0, $previous = null)
 {
     // Make sure everything is assigned properly
     parent::__construct($message, (int) $code, $previous);
     $e = $previous ? $previous : $this;
     $error = get_class($e) . '[' . $e->getCode() . ']: ' . $e->getMessage();
     $info = $e->getFile() . '[' . $e->getLine() . ']';
     $debug = "Trace: \n" . $e->getTraceAsString() . "\n";
     // Get the error settings depending on environment
     $di = Di::fetch();
     if ($di->has("dump")) {
         $dump = $di->dump;
     } else {
         $dump = new Dump(true);
     }
     $err = $di->config->env->error;
     if ($err->debug) {
         // Display debug
         if (PHP_SAPI == 'cli') {
             var_dump($error, $info, $debug);
         } else {
             echo $dump->vars($error, $info, $debug);
         }
     } else {
         if (PHP_SAPI == 'cli') {
             echo $message;
         } else {
             if ($err->hide) {
                 $message = _t('somethingIsWrong');
             }
             // Load and display error view
             echo self::view($di, $code, $message);
         }
     }
     if ($err->log) {
         // Log error into the file
         $logger = new Logger(__ROOT__ . '/app/log/' . date('Ymd') . '.log');
         $logger->error($error);
         $logger->info($info);
         $logger->debug($debug);
     }
     if ($err->email) {
         // Send email to admin
         $log = $dump->vars($error, $info, $debug);
         $email = new Email();
         $email->prepare(_t('somethingIsWrong'), $di->config->app->admin, 'email/error', ['log' => $log]);
         if ($email->Send() !== true) {
             $logger = new Logger(__ROOT__ . '/app/log/' . date('Ymd') . '.log');
             $logger->error($email->ErrorInfo);
         }
     }
 }
예제 #2
0
 /**
  * Validate _POST and send email
  *
  * @throws Error
  */
 public function postContactAction()
 {
     $validation = new Validation();
     $validation->rules(['fullName' => 'required', 'email' => 'required|email', 'repeatEmail' => 'same:email', 'content' => 'required|length:10,5000']);
     $valid = $validation->validate($_POST);
     if (!$valid) {
         $this->view->setVar('errors', $validation->getMessages());
         $this->flash->warning(_t('flash/warning/errors'));
     } else {
         // Prepare an email
         $email = new Email();
         $email->prepare(_t('contact'), $this->config->app->admin, 'email/contact', ['fullName' => $this->request->getPost('fullName'), 'email' => $this->request->getPost('email'), 'content' => $this->request->getPost('content')]);
         $email->addReplyTo($this->request->getPost('email'));
         // Try to send email
         if ($email->Send() === true) {
             $this->flash->success(_t('flash/success/contact'));
             unset($_POST);
         } else {
             throw new Error($email->ErrorInfo);
         }
     }
 }
예제 #3
0
 /**
  * Sign up new user
  * @param mixed data
  *
  * @return mixed
  */
 public function signup($data = null)
 {
     $auth = $this->di->auth;
     if (!is_array($data)) {
         // Get _POST data
         $data = $this->request->getPost()->getData();
     }
     // Hash password after validate and before save
     $this->di->hook('model.after.validate', function ($this) use($auth) {
         $this->set('password', $auth->hash($this->get('password')));
     });
     // Add extra validation for fields that won't be save but must pass
     $extra = new Validation($data);
     $extra->rules(['repeatPassword' => 'same:password', 'repeatEmail' => 'same:email']);
     // Only valid fields are accepted from the $data
     if ($this->create($data, $extra) === true) {
         // If user was created, send activation email
         $hash = md5($this->getId() . $this->get('email') . $this->get('password') . $this->config->auth->hash_key);
         $email = new Email();
         $email->prepare(_t('activation'), $this->get('email'), 'email/activation', ['username' => $this->get('username'), 'id' => $this->getId(), 'hash' => $hash]);
         if ($email->Send() === true) {
             unset($_POST);
             // Return the user
             return $this->getModel();
         } else {
             throw new Error($this->getError());
         }
     } else {
         return $this->getMessages();
     }
 }