/** * Checks that var is phone * * @param string $key The field name * @param mixed $var The field content * @return bool|string * @since 2.0.0 **/ private static function phone($key, $var) { return \Hubzero\Utility\Validate::phone($var) ? false : "{$key} does not appear to be a valid phone number"; }
/** * Change publication status * * @return string */ public function publishDraft() { // Incoming $pid = $this->_pid ? $this->_pid : Request::getInt('pid', 0); $confirm = Request::getInt('confirm', 0); $version = Request::getVar('version', 'dev'); $agree = Request::getInt('agree', 0); $pubdate = Request::getVar('publish_date', '', 'post'); $submitter = Request::getInt('submitter', $this->_uid, 'post'); $notify = 1; $block = Request::getVar('section', ''); $blockId = Request::getInt('step', 0); $element = Request::getInt('element', 0); // Check permission if (!$this->model->access('content')) { throw new Exception(Lang::txt('ALERTNOTAUTH'), 403); return; } // Load review step if (!$confirm && $this->_task != 'revert') { $this->_task = 'review'; return $this->editDraft(); } // Load publication model $pub = new \Components\Publications\Models\Publication($pid, $version); // Error loading publication record if (!$pub->exists()) { \Notify::message(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_NOT_FOUND'), 'error', 'projects'); App::redirect(Route::url($pub->link('editbase'))); return; } // Agreement to terms is required if ($confirm && !$agree) { \Notify::message(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_REVIEW_AGREE_TERMS_REQUIRED'), 'error', 'projects'); App::redirect(Route::url($pub->link('editversion') . '&action=' . $this->_task)); return; } // Check against quota if ($this->_overQuota()) { \Notify::message(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_NO_DISK_SPACE'), 'error', 'projects'); App::redirect(Route::url($pub->link('editversion') . '&action=' . $this->_task)); return; } // Set curation $pub->setCuration(); // Require DOI? $requireDoi = isset($pub->_curationModel->_manifest->params->require_doi) ? $pub->_curationModel->_manifest->params->require_doi : 0; // Make sure the publication belongs to the project if (!$pub->belongsToProject($this->model->get('id'))) { Notify::message(Lang::txt('PLG_PROJECTS_PUBLICATIONS_ERROR_PROJECT_ASSOC'), 'error', 'projects'); App::redirect(Route::url($this->model->link('publications'))); return; } // Check that version label was not published before $used_labels = $pub->version->getUsedLabels($pid, $version); if (!$pub->version->version_label || in_array($pub->version->version_label, $used_labels)) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_VERSION_LABEL_USED')); } // Is draft complete? if (!$pub->curation('complete') && $this->_task != 'revert') { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_NOT_ALLOWED')); } // Is revert allowed? $revertAllowed = $this->_pubconfig->get('graceperiod', 0); if ($revertAllowed && $pub->version->state == 1 && $pub->version->accepted && $pub->version->accepted != '0000-00-00 00:00:00') { $monthFrom = Date::of($pub->version->accepted . '+1 month')->toSql(); if (strtotime($monthFrom) < strtotime(Date::of())) { $revertAllowed = 0; } } // Embargo? if ($pubdate) { $pubdate = $this->_parseDate($pubdate); $tenYearsFromNow = Date::of(strtotime("+10 years"))->toSql(); // Stop if more than 10 years from now if ($pubdate > $tenYearsFromNow) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_ERROR_EMBARGO')); } } // Contact info is required for repositories if ($pub->config()->get('repository')) { $contact = Request::getVar('contact', array(), 'post'); if (!$contact || empty($contact)) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_ERROR_CONTACT_INFO_MISSING')); } foreach (array('name', 'email', 'phone') as $key) { if (!isset($contact[$key]) || !$contact[$key]) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_ERROR_CONTACT_INFO_MISSING')); } } if (!\Hubzero\Utility\Validate::email($contact['email'])) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_ERROR_CONTACT_INVALID_EMAIL')); } if (!\Hubzero\Utility\Validate::phone($contact['phone'])) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_ERROR_CONTACT_INVALID_PHONE')); } $data = array(); preg_match_all("#<nb:(.*?)>(.*?)</nb:(.*?)>#s", $pub->version->metadata, $matches, PREG_SET_ORDER); if (count($matches) > 0) { foreach ($matches as $match) { $data[$match[1]] = $match[2]; } } foreach ($contact as $key => $val) { $data['repository_' . $key] = $val; } $metadata = ''; foreach ($data as $k => $v) { $metadata .= "\n" . '<nb:' . $k . '>' . $v . '</nb:' . $k . '>' . "\n"; } $pub->version->metadata = $metadata; } // Main version? $main = $this->_task == 'republish' ? $pub->version->main : 1; $main_vid = $pub->version->getMainVersionId($pid); // current default version // Save version before changes $originalStatus = $pub->version->state; // Checks if ($this->_task == 'republish' && $pub->version->state != 0) { // Can only re-publish unpublished version $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_CANNOT_REPUBLISH')); } elseif ($this->_task == 'revert' && $pub->version->state != 5 && !$revertAllowed) { // Can only revert a pending resource $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_CANNOT_REVERT')); } // On error if ($this->getError()) { \Notify::message($this->getError(), 'error', 'projects'); App::redirect(Route::url($pub->link('editversion') . '&action=' . $this->_task)); return; } // Determine state $state = 5; // Default - pending approval if ($this->_task == 'share' || $this->_task == 'revert') { $state = 4; // No approval needed } elseif ($this->_task == 'republish') { $state = 1; // No approval needed } else { $pub->version->set('submitted', Date::toSql()); // Save submitter $pa = new \Components\Publications\Tables\Author($this->_database); $pa->saveSubmitter($pub->version->id, $submitter, $this->model->get('id')); if ($this->_pubconfig->get('autoapprove') == 1) { $state = 1; } else { $apu = $this->_pubconfig->get('autoapproved_users'); $apu = explode(',', $apu); $apu = array_map('trim', $apu); if (in_array(User::get('username'), $apu)) { // Set status to published $state = 1; } else { // Set status to pending review (submitted) $state = 5; } } } // Save state $pub->version->set('state', $state); $pub->version->set('main', $main); if ($this->_task != 'revert') { $publishedUp = $this->_task == 'republish' ? $pub->version->published_up : Date::toSql(); $publishedUp = $pubdate ? $pubdate : $publishedUp; $pub->version->set('rating', '0.0'); $pub->version->set('published_up', $publishedUp); $pub->version->set('published_down', ''); } $pub->version->set('modified', Date::toSql()); $pub->version->set('modified_by', $this->_uid); // Issue DOI /*if ($requireDoi > 0 && $this->_task == 'publish' && !$pub->version->doi) { // Get DOI service $doiService = new \Components\Publications\Models\Doi($pub); $extended = $state == 5 ? false : true; $doi = $doiService->register($extended); // Store DOI if ($doi) { $pub->version->set('doi', $doi); } // Can't proceed without a valid DOI if (!$doi || $doiService->getError()) { $this->setError(Lang::txt('PLG_PROJECTS_PUBLICATIONS_ERROR_DOI') . ' ' . $doiService->getError()); } }*/ // Proceed if no error if (!$this->getError()) { if ($state == 1) { // Get and save manifest and its version $versionNumber = $pub->_curationModel->checkCurationVersion(); $pub->version->set('curation', json_encode($pub->_curationModel->_manifest)); $pub->version->set('curation_version_id', $versionNumber); } // Save data if (!$pub->version->store()) { throw new Exception(Lang::txt('PLG_PROJECTS_PUBLICATIONS_PUBLICATION_FAILED'), 403); return; } // Remove main flag from previous default version if ($main && $main_vid && $main_vid != $pub->version->get('id')) { $pub->version->removeMainFlag($main_vid); } } // OnAfterPublish $this->onAfterChangeState($pub, $originalStatus); // Redirect App::redirect(Route::url($pub->link('editversion'))); return; }
/** * Registers a new authy user * * @return void **/ private function register() { $authy = new AuthyApi($this->params->get('key')); // Gather and validate inputs $email = Request::getVar('email', null); $phone = Request::getVar('phone', null); $cc = Request::getInt('country_code', 1); if (!Validate::email($email) || !Validate::phone($phone)) { Notify::error("Invalid email or phone provided. Please try again"); App::redirect(Request::current()); } // Register the user $user = $authy->registerUser($email, $phone, $cc); // If everything checks out, we store the user id in the database if ($user->ok()) { // Store factor domain id in the database Factor::oneOrNew(0)->set(['user_id' => User::get('id'), 'domain' => 'authy', 'factor_id' => $user->id(), 'data' => json_encode(['email' => $email, 'phone' => $phone, 'country_code' => $cc])])->save(); } else { // Return errors foreach ($user->errors() as $field => $message) { Notify::error("{$field}: {$message}"); } } // Redirect for verification process to occur App::redirect(Request::current()); }