Ejemplo n.º 1
0
 /**
  * Handle return of twitter authentication.
  *
  * @throws UserException
  */
 public function callback()
 {
     $di = Di::getInstance();
     $requestToken = $di->slim->request->get('oauth_token');
     $sessionOauthToken = $di->session->get('oauth_token');
     $requestSecret = $di->session->get('oauth_token_secret');
     if ($requestToken !== $sessionOauthToken) {
         throw new UserException('Unable to signin using twitter, try later');
     }
     $oauthVerifier = $di->slim->request->get('oauth_verifier');
     // Fetching access token using authorize request token
     $di->twitterOAuth->setOauthToken($requestToken, $requestSecret);
     $accessToken = $di->twitterOAuth->oauth('oauth/access_token', ['oauth_verifier' => $oauthVerifier]);
     $di->session->delete('oauth_token');
     $di->session->delete('oauth_token_secret');
     if (empty($accessToken['user_id']) || empty($accessToken['screen_name']) || empty($accessToken['oauth_token']) || empty($accessToken['oauth_token_secret'])) {
         throw new UserException('Unable to signin using twitter, try later');
     }
     $twitterUserId = $accessToken['user_id'];
     $twitterScreenName = $accessToken['screen_name'];
     $oauthToken = $accessToken['oauth_token'];
     $oauthTokenSecret = $accessToken['oauth_token_secret'];
     $di->twitterOAuth->setOauthToken($oauthToken, $oauthTokenSecret);
     // Check if access token works
     $credentials = $di->twitterOAuth->get('account/verify_credentials');
     $user = UserModel::getCollection(['twitterId' => $twitterUserId], 0, 1);
     if (empty($user)) {
         // Create user
         $user = UserModel::get();
         $user->name = $twitterScreenName;
         $user->twitterId = $twitterUserId;
         $user->twitterToken = $oauthToken;
         $user->twitterTokenSecret = $oauthTokenSecret;
         $user->save();
         // Create source
         $source = TwitterSourceModel::get();
         $source->method = 'user';
         $source->accessTokenKey = $oauthToken;
         $source->accessTokenSecret = $oauthTokenSecret;
         $source->save();
         // Associate user to source
         $source->associate($user);
         // Start source indexer
         $source->startIndexer();
     } else {
         $user = $user[0];
         if ($user->name !== $twitterScreenName || $user->twitterToken !== $oauthToken || $user->twitterTokenSecret !== $oauthTokenSecret) {
             $user->name = $twitterScreenName;
             $user->twitterToken = $oauthToken;
             $user->twitterTokenSecret = $oauthTokenSecret;
             $user->save();
         }
     }
     $this->authentify($user);
     $di->slim->redirect('/home', 302);
 }
Ejemplo n.º 2
0
 /**
  * Return session value for $name.
  *
  * @param string $name
  *
  * @return mixed
  */
 public function get($name)
 {
     $di = Di::getInstance();
     // Check if cookie is on the client in order to avoid useless session start
     $cookie = $di->slim->getCookie(session_name());
     if (empty($cookie)) {
         return;
     }
     $this->enable();
     return isset($_SESSION[$name]) ? $_SESSION[$name] : null;
 }
 public function __construct()
 {
     $di = Di::getInstance();
     $this->user = $di->session->get('user');
     $request = $di->slim->request();
     $clientIp = $request->getIp();
     $clientUserAgent = $request->getUserAgent();
     if (empty($this->user) || $clientIp !== $di->session->get('clientIp') || $clientUserAgent !== $di->session->get('clientUserAgent')) {
         $di->slim->render('auth' . DIRECTORY_SEPARATOR . 'login', ['unauthenticated' => true]);
         $di->slim->response->setStatus(401);
         $di->slim->stop();
     }
 }
Ejemplo n.º 4
0
 public function display($template, $data = null)
 {
     $di = Di::getInstance();
     if ($this->data !== false) {
         $this->htmlData['htmlBody'] = $di->mustache->loadTemplate($template)->render($this->data);
     } else {
         $this->htmlData['htmlBody'] = $template;
     }
     // If not in dev, flush previous content
     if ($di->env !== ENV_DEVELOPMENT) {
         ob_end_clean();
     }
     echo $di->mustache->loadTemplate('layouts' . DIRECTORY_SEPARATOR . 'html')->render($this->htmlData);
 }
Ejemplo n.º 5
0
 /**
  * Add js to the head.
  *
  * @return callable
  */
 public function addJs()
 {
     return function ($path) {
         $di = Di::getInstance();
         $di->layoutHtml->addJs($this->getJsUrl($path));
     };
 }
<?php

return \NwWebsite\Di::getInstance()->rootPath . 'templates' . DIRECTORY_SEPARATOR;
Ejemplo n.º 7
0
<?php

namespace NwWebsite;

use Slim\Slim;
use Slim\Middleware\ContentTypes as ContentTypesMiddleware;
use NwWebsite\Controllers\Auth\Twitter as AuthTwitterController;
use NwWebsite\Controllers\Home as HomeController;
use NwWebsite\Controllers\Auth\Authentifier as AuthentifierController;
use NwWebsite\Controllers\Articles as ArticlesController;
$di = Di::getInstance();
if ($di->env === ENV_DEVELOPMENT) {
    $slimMode = 'development';
    $debug = true;
} else {
    $slimMode = 'production';
    $debug = true;
}
$app = new Slim(['mode' => $slimMode, 'debug' => $debug, 'view' => $di->layoutHtml]);
// Allow to decode json request body
$app->add(new ContentTypesMiddleware());
$app->get('/auth/login', function () use($app) {
    $app->render('auth/login');
});
$app->get('/auth/twitter/login', function () {
    AuthTwitterController::getInstance()->login();
});
$app->get('/auth/twitter/callback', function () {
    AuthTwitterController::getInstance()->callback();
});
$app->get('/auth/logout', function () {
Ejemplo n.º 8
0
 private function response($body)
 {
     $di = Di::getInstance();
     $di->slim->response->headers->set('Content-Type', static::DEFAULT_CONTENT_TYPE);
     $di->slim->response->setBody(json_encode($body));
 }
Ejemplo n.º 9
0
<?php

/**
 * Api Configuration.
 */
$di = \NwWebsite\Di::getInstance();
if ($di->env === ENV_TEST) {
    $endPoint = 'http://192.168.99.100:8080/';
} else {
    $endPoint = 'http://api:80/';
}
Ejemplo n.º 10
0
 public function home()
 {
     $di = Di::getInstance();
     $di->slim->render('home', []);
 }
Ejemplo n.º 11
0
<?php

return \NwWebsite\Di::getInstance()->rootPath . 'public' . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR;
 /**
  * Logout user.
  */
 public function logout()
 {
     $di = Di::getInstance();
     $di->session->delete('user');
 }
Ejemplo n.º 13
0
 public function startIndexer()
 {
     $di = Di::getInstance();
     $twitterConsumerConfig = $di->config->get('twitterConsumer');
     $di->twitterIndexerExchange->publish(json_encode(['authentication' => ['consumer_key' => $twitterConsumerConfig->consumerKey, 'consumer_secret' => $twitterConsumerConfig->consumerSecret, 'access_token_key' => $this->accessTokenKey, 'access_token_secret' => $this->accessTokenSecret], 'method' => $this->method, 'sourceId' => $this->getId()]));
 }
<?php

return \NwWebsite\Di::getInstance()->rootPath . 'checksums' . DIRECTORY_SEPARATOR;
Ejemplo n.º 15
0
 /**
  * Return a collection of model instances according to filters.
  *
  * @param array $filters
  * @param int   $offset
  * @param int   $limit
  *
  * @return array
  */
 public static function getCollection($filters = [], $offset = 0, $limit = 10)
 {
     $params = ['offset' => $offset, 'limit' => $limit];
     if (!empty($filters)) {
         $params['filters'] = $filters;
     }
     $instance = self::getNewInstance();
     $di = Di::getInstance();
     $resources = $di->api->getResources($instance->getResourcePath(), $params);
     $collection = [];
     foreach ($resources as $resource) {
         $instance = self::getNewInstance();
         $instance->mergeResource($resource);
         $collection[] = $instance;
     }
     return $collection;
 }
Ejemplo n.º 16
0
<?php

require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
\NwWebsite\Di::getInstance()->slim->run();