/** * @return Validator */ public function getValidator() { $validator = new Validator(); $validator->addRule('name', 'text', true, 'Name is invalid'); $validator->addRule('url', 'url', true, 'Url is required but invalid'); return $validator; }
/** * @return Validator */ public function getValidator() { $validator = new Validator(); $validator->addRule('given', 'text', true, 'Given name is required but invalid'); $validator->addRule('family', 'text', true, 'Family name is required but invalid'); $validator->addRule('website', 'url', false, 'Website URL is invalid'); $validator->addRule('contact', 'text', false, 'Contact info is invalid'); $validator->addRule('about', 'text', false, 'About text is invalid'); return $validator; }
/** * @return Validator */ public function getValidator() { $validator = new Validator(); $validator->addRule('name', 'text', false, 'File name is invalid'); $validator->addRule('title', 'text', true, 'File title is required but invalid'); $validator->addRule('full_text', 'boolean', false, 'Full text flag must be boolean'); $validator->addRule('restricted', 'boolean', false, 'Restricted flag must be boolean'); $validator->addRule('hidden', 'boolean', false, 'Hidden flag must be boolean'); return $validator; }
/** * @param Request $request * * @return bool */ public function searchAuthors(Request $request) { $field = Validator::sanitizeText($request->get('field')); $search = Validator::sanitizeText($request->get('search')); if (!$search) { $this->errors[] = 'Your search input is invalid'; return false; } $search_words = explode(' ', $search); $repo = new AuthorRepository($this->db); switch (true) { case $field === 'given': foreach ($search_words as $word) { $repo->where('given', 'LIKE', '%' . $word . '%'); } break; case $field === 'family': foreach ($search_words as $word) { $repo->where('family', 'LIKE', '%' . $word . '%'); } break; case $field === 'about': foreach ($search_words as $word) { $repo->where('about', 'LIKE', '%' . $word . '%'); } break; default: throw new UnexpectedValueException(); } $this->result = $repo->order('family', 'ASC')->find(); return true; }
/** @noinspection PhpUnusedPrivateMethodInspection * @param Request $request * * @return bool */ private function deleteUser(Request $request) { $user_id = Validator::sanitizeNumber($request->post('user_id')); if (!$user_id) { throw new UnexpectedValueException(); } $model = new UserModel($this->db); return $model->delete($user_id); }
/** @noinspection PhpUnusedPrivateMethodInspection * @param Request $request * * @return bool|mixed * @throws PermissionRequiredException * @throws exceptions\LoginRequiredException */ private function addUrl(Request $request) { if (!$this->auth->checkPermission(Auth::EDIT_PUBLICATION)) { throw new PermissionRequiredException(Auth::EDIT_PUBLICATION); } $id = Validator::sanitizeNumber($request->get('id')); if (!$id) { throw new UnexpectedValueException(); } $url_model = new UrlModel($this->db); $validator = $url_model->getValidator(); if ($validator->validate($request->post())) { $data = $validator->getSanitizedResult(); $url = new Url($data); try { return $url_model->store($url, $id); } catch (DBDuplicateEntryException $e) { $this->errors[] = 'This url is already assigned to this publication'; return false; } } else { $this->errors = array_merge($this->errors, $validator->getErrors()); return false; } }
/** @noinspection PhpUnusedPrivateMethodInspection * @param Request $request * * @return string * @throws Exception * @throws NotFoundException */ private function login(Request $request) { $errors = array(); if ($request->post('username') && $request->post('password')) { $username = Validator::sanitizeText($request->post('username')); $password = Validator::sanitizeText($request->post('password')); if ($this->auth->login($username, $password)) { $destination = !empty($_SESSION['referrer']) ? $_SESSION['referrer'] : Request::createUrl(array(), true); $this->redirect($destination); } else { $errors[] = 'Invalid user name or password'; } } $view = new View('login', $errors); return $view->display(); }
/** @noinspection PhpUnusedPrivateMethodInspection * @param Request $request * * @return bool|int * @throws PermissionRequiredException * @throws exceptions\LoginRequiredException */ private function edit(Request $request) { if (!$this->auth->checkPermission(Auth::EDIT_KEYWORD)) { throw new PermissionRequiredException(Auth::EDIT_KEYWORD); } $id = Validator::sanitizeNumber($request->get('id')); if (!$id) { throw new UnexpectedValueException(); } $validator = $this->model->getValidator(); if ($validator->validate($request->post())) { $input = $validator->getSanitizedResult(); $this->model->update($id, $input); return true; } else { $this->errors = array_merge($this->errors, $validator->getErrors()); return false; } }
/** * @param $type * * @return Validator */ public function getValidator($type) { $validator = new Validator(); $validator->addRule('title', 'text', true, 'Title is required but invalid'); $validator->addRule('journal', 'text', false, 'Journal is required but invalid'); $validator->addRule('volume', 'number', false, 'Volume is invalid'); $validator->addRule('number', 'number', false, 'Number is invalid'); $validator->addRule('booktitle', 'text', false, 'Booktitle is required but invalid'); $validator->addRule('series', 'text', false, 'Series is invalid'); $validator->addRule('edition', 'text', false, 'Edition is invalid'); $validator->addRule('pages_from', 'number', false, 'First page is invalid'); $validator->addRule('pages_to', 'number', false, 'Last page is invalid'); $validator->addRule('note', 'text', false, 'Note is invalid'); $validator->addRule('location', 'text', false, 'Location is invalid'); $validator->addRule('date_published', 'date', true, 'Publication date is required but invalid. It must in the format YYYY-MM-DD'); $validator->addRule('publisher', 'text', false, 'Publisher is invalid'); $validator->addRule('institution', 'text', false, 'Institution is invalid'); $validator->addRule('school', 'text', false, 'School is invalid'); $validator->addRule('address', 'text', false, 'Address is invalid'); $validator->addRule('howpublished', 'text', false, 'Howpublished is invalid'); $validator->addRule('copyright', 'text', false, 'Copyright is invalid'); $validator->addRule('doi', 'doi', false, 'DOI is invalid'); $validator->addRule('isbn', 'text', false, 'ISBN invalid'); // TODO: validate ISBN $validator->addRule('study_field', 'text', true, 'Field of Study is required but invalid'); $validator->addRule('type', 'text', true, 'Type is required but invalid'); $validator->addRule('abstract', 'text', false, 'Abstract is invalid'); $validator->addRule('foreign', 'number', false, 'Foreign is valid'); /* Overwrite rules with required rules depending on type */ switch ($type) { case 'article': $validator->addRule('journal', 'text', true, 'Journal is required but invalid'); break; case 'book': $validator->addRule('publisher', 'text', true, 'Publisher is required but invalid'); break; case 'incollection': case 'inproceedings': case 'inbook': $validator->addRule('booktitle', 'text', true, 'Booktitle is required but invalid'); break; case 'mastersthesis': case 'phdthesis': $validator->addRule('institution', 'text', true, 'Institution is required but invalid'); break; case 'misc': $validator->addRule('howpublished', 'text', true, 'How published is required but invalid'); break; case 'techreport': $validator->addRule('institution', 'text', true, 'Institution is required but invalid'); break; case 'unpublished': // Nothing here break; } return $validator; }
/** @noinspection PhpUnusedPrivateMethodInspection * @param Request $request * * @return bool|int */ private function changePassword(Request $request) { $password = Validator::sanitizeText($request->post('password')); if (!$password || !$this->auth->validateLogin($this->user->getName(), $password)) { $this->errors[] = 'Invalid current password'; return false; } $password_new = Validator::sanitizeText($request->post('password_new')); $password_confirm = Validator::sanitizeText($request->post('password_confirm')); if (!$password_new || !$password_confirm) { $this->errors[] = 'New password required but invalid'; return false; } if ($password_new !== $password_confirm) { $this->errors[] = 'Entered passwords are not the same'; return false; } return $this->auth->setPassword($this->user->getName(), $password_new); }
/** * * @param string $url * * @return boolean */ private function getInputFromUrl($url) { if (ini_get('allow_url_fopen')) { $sccOpts = array('http' => array('timeout' => 200)); $input = @file_get_contents($url, false, stream_context_create($sccOpts)); if ($input) { return Validator::sanitizeText($input); } } else { $this->errors[] = 'Can not get content from URL. file_get_contents is disabled by server configuration allow_url_fopen=0'; } return false; }
/** * @return Validator */ public function getValidator() { $validator = new Validator(); $validator->addRule('name', 'text', true, 'Username is required but invalid'); $validator->addRule('mail', 'email', true, 'Email address is required but invalid'); $validator->addRule('active', 'boolean', false, 'Active is invalid'); return $validator; }