public function actionGet($id = NULL) { $api_key = $this->getHttpRequest()->getHeader('public-api-key'); $api_hash = $this->getHttpRequest()->getHeader('api-hash'); date_default_timezone_set('UTC'); //TODO: -1min $date = date('YmdHi'); $actual_url = $this->getHttpRequest()->getUrl()->getAbsoluteUrl(); $hmac = hash_hmac('sha512', $actual_url . $date, 'PRIVATE_KEY'); //na základě public key ($api_key) $payload = array('api_hash' => $this->getHttpRequest()->getHeader('api-hash')); $response['method'] = 'GET'; if ($api_hash !== $hmac) { $response['err_code'] = 10; $response['err_info'] = 'You are not authorized.'; $this->sendResponse(new JsonResponse($response)); } elseif ($id === NULL) { $response['err_code'] = 20; $response['err_info'] = 'Requested ID cannot be empty.'; $this->sendResponse(new JsonResponse($response)); } else { $response['data'] = $this->posts->findForApi(['id' => $id]); //FIXME: vracet 1 result, ne $result[0] $this->sendResponse(new JsonResponse($response)); } }
public function handleRandom() { if (!$this->setting->random_search) { $this->error(); } $post = $this->posts->rand(); if ($post) { $this->redirect(':Single:article', $post->slug); } $this->redirect(':Homepage:default'); }
public function renderDefault($search) { //FIXME tagy ::: 'publish_date <=' => new \DateTime() $string = Strings::lower(Strings::normalize($search)); $string = Strings::replace($string, '/[^\\d\\w]/u', ' '); $words = Strings::split(Strings::trim($string), '/\\s+/u'); $words = array_unique(array_filter($words, function ($word) { return Strings::length($word) > 1; })); $words = array_map(function ($word) { return Strings::toAscii($word); }, $words); $string = implode(' ', $words); $this->template->tag = $this->tags->findOneBy(['name' => $string]); $result = $this->posts->fulltextSearch($string); if (count($result) == 0) { $this->template->search = $search; $this->template->error = 'Nic nebylo nalezeno'; } else { $this->template->search = $search; $this->template->result = $result; } }
public function postFormSucceeded(UI\Form $form, Nette\Utils\ArrayHash $vals) { try { if (!$this->post) { $this->post = new Entity\Post(); $this->post->date = new \DateTime(); } $this->post->publish_date = $vals->publish_date ? new \DateTime($vals->publish_date) : new \DateTime('now'); $this->post->title = $vals->title; $this->post->slug = $vals->slug; $this->post->body = $vals->editor; $this->post->disable_comments = $vals->disable_comments; $this->post->draft = FALSE; foreach (array_unique(preg_split('/\\s*,\\s*/', $vals->tags)) as $tag_name) { $tag = $this->tags->findOneBy(['name' => $tag_name]); if (!$tag) { $tag = new Entity\Tag(); $tag->name = $tag_name; $tag->color = substr(md5(rand()), 0, 6); //Short and sweet } if (!empty($tag_name)) { $this->post->addTag($tag); } } $this->posts->save($this->post); $this->presenter->flashMessage('Příspěvek byl úspěšně uložen a publikován.', 'success'); $this->onSave(); } catch (Kdyby\Doctrine\DuplicateEntryException $exc) { //DBALException $this->presenter->flashMessage('Tento URL slug je již v databázi uložen, zvolte prosím jiný.', 'danger'); } catch (Nette\Security\AuthenticationException $exc) { $this->presenter->flashMessage('Myslím to vážně, editovat opravdu **ne**můžete!', 'danger'); $this->redirect('this'); return; } }