Пример #1
0
 /**
  * @throws \exceptions\DbException
  */
 public static function post()
 {
     if (isset($_POST) && count($_POST) > 0) {
         if (!isset(self::$data) || !self::$data instanceof Data) {
             self::$data = new Data();
         }
         if (isset($_POST['action'])) {
             $messages = self::$data->getMessagesAfter($_POST['maxId']);
             $counter = 0;
             while (!$messages) {
                 sleep(1);
                 $counter++;
                 if ($counter > 25) {
                     echo '[]';
                     exit;
                 }
                 $messages = self::$data->getMessagesAfter($_POST['maxId']);
             }
             echo json_encode($messages);
             exit;
         }
         if (isset($_POST['message'])) {
             self::$data->addMessage($_POST['message'], $_POST['author'] ? $_POST['author'] : 'anon');
             exit;
         }
     }
 }
Пример #2
0
 public static function collect(callable $callback = null, $filter = '')
 {
     $name = self::type();
     return Data::instance()->query(self::XPATH)->find($name . $filter)->map($callback ?: function ($item) use($name) {
         return [$name => new self($item)];
     });
 }
Пример #3
0
 public function references($type)
 {
     $name = self::type();
     $query = "{$type}[@{$name}='{$this->context['@id']}']";
     return Data::instance()->query(self::XPATH)->find($query)->map(function ($item) use($type) {
         return [$type => Data::Factory($type, $item)];
     });
 }
Пример #4
0
 public function save()
 {
     if (empty($this->errors) && Data::instance()->storage->validate()) {
         $this->beforeSave();
         return Data::instance()->storage->save();
     } else {
         $this->errors = array_merge(["Did not save"], $this->errors, array_map(function ($error) {
             return $error->message;
         }, Data::instance()->storage->errors()));
         return false;
     }
 }
Пример #5
0
 protected function initialize()
 {
     return Data::instance()->query(self::XPATH)->pick('.');
 }
Пример #6
0
 public function POSTlogin($request)
 {
     $pin = $request->post('pin') ?: 0;
     $uid = $request->post('uid') ?: false;
     try {
         if ($uid && $pin) {
             // authenticate user based on password
             (new \models\instructor(\models\Data::ID($uid)))->authenticate($pin);
             \bloc\Application::instance()->session('COLUM', ['id' => $uid]);
             \bloc\router::redirect('/records/courses');
         } else {
             // user must be in database based on oasis id, find them: ;
             $user = \models\Data::ID(\models\Student::BLEAR($pin));
             // if found, generate token with sha1 of $email address and token
             $token = \bloc\types\token::generate($user['@email'], getenv('EMAIL_TOKEN'));
             // set the token on the user field
             if ($user->hasAttribute('token') && $user->getAttribute('token') === $token) {
                 throw new \InvalidArgumentException("Token Already Requested", 2);
             } else {
                 $user->setAttribute('token', $token);
                 \models\Data::instance()->storage->save();
                 // email the user a link.
                 $template = new \bloc\View('views/layouts/email.html');
                 $template->content = 'views/layouts/forms/transaction.html';
                 $output = ['link' => DOMAIN . "/records/token/{$user['@id']}/{$token}", 'title' => $user['@name'], 'message' => 'login to course site'];
                 \models\Message::TRANSACTION('login', $user['@email'], (string) $template->render($output));
             }
         }
     } catch (\InvalidArgumentException $e) {
         $type = $e->getCode() == 1 ? 'invalid' : 'duplicate';
         $path = sprintf('/%s/login/%s/', $this->template, $type);
         \bloc\router::redirect($path);
     }
     $view = new \bloc\View(self::layout);
     $view->content = 'views/layouts/forms/transaction.html';
     return $view->render(['link' => 'http://www.colum.edu/loopmail', 'title' => 'Email Sent', 'message' => 'check your email']);
 }