} return $this->rules[$route]; } } class EntryController { private $controller; private $view; public function __construct(Router $router, $routeName, $action = null) { $route = $router->getRoute($routeName); $modelName = $route->model; $controllerName = $route->controller; $viewName = $route->view; $model = new $modelName(); $this->controller = new $controllerName($model); $this->view = new $viewName($routeName, $model); if (!empty($action)) { $this->controller->{$action}(); $this->controller->renderOut(); } } public function out() { require_once "layout.php"; } } $entryController = new EntryController(new Router(), isset($_POST['route']) ? $_POST['route'] : null, isset($_POST['action']) ? $_POST['action'] : null); $entryController->out(); ?>
/** * * * @param EntryController $Sender * @param array $Args */ public function entryController_openID_create($Sender, $Args) { $this->EventArguments = $Args; try { $OpenID = $this->getOpenID(); } catch (Gdn_UserException $ex) { $Sender->Form->addError('@' . $ex->getMessage()); $Sender->render('Url', '', 'plugins/OpenID'); } $Mode = $Sender->Request->get('openid_mode'); switch ($Mode) { case 'cancel': $Sender->render('Cancel', '', 'plugins/OpenID'); break; case 'id_res': if ($OpenID->validate()) { $Attributes = $OpenID->getAttributes(); print_r($_GET); } break; default: if (!$OpenID->identity) { $Sender->CssClass = 'Dashboard Entry connect'; $Sender->setData('Title', t('Sign In with OpenID')); $Sender->render('Url', '', 'plugins/OpenID'); } else { try { $Url = $OpenID->authUrl(); redirect($Url); } catch (Exception $Ex) { $Sender->Form->addError($Ex); $Sender->render('Url', '', 'plugins/OpenID'); } } break; } }
/** * * * @param EntryController $Sender * @param array $Args */ public function base_connectData_handler($Sender, $Args) { if (val(0, $Args) != 'googleplus') { return; } // Grab the google plus profile from the session staff. $GooglePlus = Gdn::session()->stash(self::ProviderKey, '', false); $AccessToken = val('AccessToken', $GooglePlus); $Profile = val('Profile', $GooglePlus); $Form = $Sender->Form; $Form->setFormValue('UniqueID', val('id', $Profile)); $Form->setFormValue('Provider', self::ProviderKey); $Form->setFormValue('ProviderName', 'Google+'); $Form->setFormValue('FullName', val('name', $Profile)); $Form->setFormValue('Email', val('email', $Profile)); if (c('Plugins.GooglePlus.UseAvatars', true)) { $Form->setFormValue('Photo', val('picture', $Profile)); } if (c('Plugins.GooglePlus.UseFullNames')) { $Form->setFormValue('Name', val('name', $Profile)); saveToConfig(array('Garden.User.ValidationRegex' => UserModel::USERNAME_REGEX_MIN, 'Garden.User.ValidationLength' => '{3,50}', 'Garden.Registration.NameUnique' => false), '', false); } // Save some original data in the attributes of the connection for later API calls. $Attributes = array(); $Attributes[self::ProviderKey] = array('AccessToken' => $AccessToken, 'Profile' => $Profile); $Form->setFormValue('Attributes', $Attributes); $Sender->setData('Verified', true); $this->EventArguments['Form'] = $Form; $this->fireEvent('AfterConnectData'); }
/** * * @param EntryController $Sender * @param array $Args */ public function EntryController_OpenID_Create($Sender, $Args) { $this->EventArguments = $Args; $Sender->Form->InputPrefix = ''; $OpenID = $this->GetOpenID(); $Mode = $Sender->Request->Get('openid_mode'); switch ($Mode) { case 'cancel': $Sender->Render('Cancel', '', 'plugins/OpenID'); break; case 'id_res': if ($OpenID->validate()) { $Attributes = $OpenID->getAttributes(); print_r($_GET); } break; default: if (!$OpenID->identity) { $Sender->CssClass = 'Dashboard Entry connect'; $Sender->SetData('Title', T('Sign In with OpenID')); $Sender->Render('Url', '', 'plugins/OpenID'); } else { try { $Url = $OpenID->authUrl(); Redirect($Url); } catch (Exception $Ex) { $Sender->Form->AddError($Ex); $Sender->Render('Url', '', 'plugins/OpenID'); } } break; } }
/** * An intermediate page for jsConnect that checks SSO against and then posts the information to /entry/connect. * * @param EntryController $Sender * @param string $Action A specific action. It can be one of the following: * * - blank: The default action. * - guest: There is no user signed in. * - * @param string $Target The url to redirect to after a successful connect. * @throws /Exception Throws an exception when the jsConnect provider is not found. */ public function entryController_jsConnect_create($Sender, $Action = '', $Target = '') { $Sender->SetData('_NoMessages', true); if ($Action) { if ($Action == 'guest') { // Redirect('/'); $Sender->AddDefinition('CheckPopup', TRUE); $Target = $Sender->Form->GetFormValue('Target', '/'); $Sender->RedirectUrl = $Target; $Sender->Render('JsConnect', '', 'plugins/jsconnect'); } else { parse_str($Sender->Form->GetFormValue('JsConnect'), $JsData); $Error = GetValue('error', $JsData); $Message = GetValue('message', $JsData); if ($Error === 'timeout' && !$Message) { $Message = T('Your sso timed out.', 'Your sso timed out during the request. Please try again.'); } Gdn::Dispatcher()->PassData('Exception', $Message ? htmlspecialchars($Message) : htmlspecialchars($Error))->Dispatch('home/error'); // $Sender->Form->AddError($Message ? htmlspecialchars($Message) : htmlspecialchars($Error)); // $Sender->SetData('Title', T('Error')); // $Sender->Render('JsConnect_Error', '', 'plugins/jsconnect'); } } else { $client_id = $Sender->SetData('client_id', $Sender->Request->Get('client_id', 0)); $Provider = self::getProvider($client_id); if (empty($Provider)) { throw NotFoundException('Provider'); } $Get = ArrayTranslate($Sender->Request->Get(), array('client_id', 'display')); $Sender->AddDefinition('JsAuthenticateUrl', self::connectUrl($Provider, TRUE)); $Sender->AddJsFile('jsconnect.js', 'plugins/jsconnect'); $Sender->SetData('Title', T('Connecting...')); $Sender->Form->Action = Url('/entry/connect/jsconnect?' . http_build_query($Get)); $Sender->Form->AddHidden('JsConnect', ''); $Sender->Form->AddHidden('Target', $Target); $Sender->MasterView = 'empty'; $Sender->Render('JsConnect', '', 'plugins/jsconnect'); } }
/** * Redirect users to the /post/discussion end point after email confirmation. * * @param EntryController $sender Sending controller instance. */ public function entryController_render_after($sender) { if ($sender->data('EmailConfirmed')) { echo '<meta http-equiv="Refresh" content="1; url=' . url('/post/discussion?welcomepost=true') . '">'; } }
/** * Add AgeGate javascript file. * * @param EntryController $sender Sending Controller. */ public function entryController_render_before($sender) { $sender->addJsFile('agegate.js', 'plugins/AgeGate'); }
/** * * * @param EntryController $Sender * @param array $Args */ public function base_connectData_handler($Sender, $Args) { if (val(0, $Args) != 'disqus') { return; } if (isset($_GET['error'])) { throw new Gdn_UserException(val('error_description', $_GET, t('There was an error connecting to Disqus'))); } $Provider = $this->provider(); if (!$Provider) { throw new Gdn_UserException('The Disqus plugin has not been configured correctly.'); } $AppID = $Provider['AuthenticationKey']; $Secret = $Provider['AssociationSecret']; $Code = val('code', $_GET); $Query = ''; if ($Sender->Request->get('display')) { $Query = 'display=' . urlencode($Sender->Request->get('display')); } $RedirectUri = concatSep('&', $this->redirectUri(), $Query); $Form = $Sender->Form; $AccessToken = $Form->getFormValue('AccessToken'); //Gdn::Session()->Stash('Disqus.AccessToken', NULL, NULL); // Get the access token. if ($Code && !$AccessToken) { // Exchange the token for an access token. $Qs = array('grant_type' => 'authorization_code', 'client_id' => $AppID, 'client_secret' => $Secret, 'redirect_uri' => $RedirectUri, 'code' => $Code); $Url = 'https://disqus.com/api/oauth/2.0/access_token/'; //.http_build_query($Qs); // Get the redirect URI. $C = curl_init(); curl_setopt($C, CURLOPT_POST, true); curl_setopt($C, CURLOPT_POSTFIELDS, $Qs); curl_setopt($C, CURLOPT_RETURNTRANSFER, true); curl_setopt($C, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($C, CURLOPT_URL, $Url); $Contents = curl_exec($C); $Info = curl_getinfo($C); if (strpos(val('content_type', $Info, ''), '/json') !== false) { $Tokens = json_decode($Contents, true); } else { parse_str($Contents, $Tokens); } if (val('error', $Tokens)) { throw new Gdn_UserException('Disqus returned the following error: ' . valr('error.message', $Tokens, 'Unknown error.'), 400); } $AccessToken = val('access_token', $Tokens); $Expires = val('expires_in', $Tokens, null); $Form->addHidden('AccessToken', $AccessToken); } if ($AccessToken) { // Grab the user's profile. $Qs = array('access_token' => $AccessToken, 'api_key' => $AppID, 'api_secret' => $Secret); $Url = 'https://disqus.com/api/3.0/users/details.json?' . http_build_query($Qs); $C = curl_init(); curl_setopt($C, CURLOPT_RETURNTRANSFER, true); curl_setopt($C, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($C, CURLOPT_URL, $Url); $Contents = curl_exec($C); $Info = curl_getinfo($C); if (strpos(val('content_type', $Info, ''), '/json') !== false) { $Profile = json_decode($Contents, true); $Profile = $Profile['response']; } else { throw new Gdn_UserException('There was an error trying to get your profile information from Disqus.'); } } else { throw new Gdn_UserException('There was an error trying to get an access token from Disqus.'); } $Form->setFormValue('UniqueID', val('id', $Profile)); $Form->setFormValue('Provider', 'disqus'); $Form->setFormValue('ProviderName', 'Disqus'); $Form->setFormValue('FullName', val('name', $Profile)); $Form->setFormValue('Name', val('username', $Profile)); $Form->setFormValue('Photo', valr('avatar.permalink', $Profile)); $Sender->setData('Verified', true); }
$newEntry->text = $input['Text']; $newEntry->timeStamp = $input['TimeStamp']; $this->dbContext->entries->add($newEntry); break; case 'PUT': //Put request: extract posted data from request body, parse it into an array, //extrat values from array and make the new entity entry, //Aslo take the id of entry to edit from query string //update the old entry with new one $newEntry = new Entry(); $inputJSON = file_get_contents('php://input'); $input = json_decode($inputJSON, TRUE); $newEntry->title = $input['Title']; $newEntry->text = $input['Text']; $newEntry->timeStamp = $input['TimeStamp']; $this->dbContext->entries->update($parameters['Id'], $newEntry); break; case 'Delete': //delete request: extract the id from queyr string, delete entry from database. $this->dbContext->entries->delete($parameters['Id']); break; } header('Content-Type: application/json'); $result = json_encode($result); echo $result; } } //when theis page is requested, make a new controller object //and execute the controller $controller = new EntryController(); $controller->execute();