I have two controllers. One of them is called OAMLController and the other is called LoginController. I would like to set HTTP Cookies in OAMLController and then make a call to LoginController and read it in this controller.
I know how to do this in PHP, but I don't know how to do this in Zend Framework 2.
PHP (OAML.php):
setcookie("_ga", "GA1.2.1622977711.1433494392", 0, "/", "http://gnsys.local");
setcookie("_gat", "1", 0, "/", "http://gnsys.local");
header("Location: http://gnsys.local/publico/login.php");
PHP (login.php):
$cookie = "";
foreach (getallheaders() as $name => $value) {
echo "$name: $value</br>";
if ($name == "Cookie")
$cookie = $value;
}
I have tried to follow the ZF2 tutorial but is so confusing.
More questions:
I have redirect to the other controller using $this->redirect()->toUrl($url).
$cookie = new \Zend\Http\Header\SetCookie("param1", "Hola", null, null, "http://gnsys.local", null, null, null, null);
$this->getResponse()->getHeaders()->addHeader($cookie);
return $this->redirect()->toUrl("http://gnsys.local/publico/login");
Because if I redirect with:
$controllerName = "LoginController";
$actionName = "index";
return $this->redirect()->toRoute(
"publico",
array(
"controller" => $controllerName,
"action" => $actionName
));
I always access to http://gnsys.local/publico and not to where I want http://gnsys.local/publico/login.
Another question, in LoginController I can't read the cookie. If I check the cookies via Firebug I can see that I have created the cookie in ".gnsys.local" domain and not in "gnsys.local" domain.
Why has that happened? If I make the redirect using toRoute or toUrl I create the cookie on the same domain ".gnsys.local" and not in "gnsys.local".
module.config:
'router' => array(
'routes' => array(
// The following is a route to simplify getting started creating
// new controllers and actions without needing to create a new
// module. Simply drop new controllers in, and you can access them
// using the path /application/:controller/:action
'publico' => array(
'type' => 'Literal',
'options' => array(
'route' => '/publico',
'defaults' => array(
'__NAMESPACE__' => 'Publico\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action]]',
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
Updated 2:
Finally, the redirection works fine with the solution from Jangya Satapathy. But I have a new problem and this is that I can't read the cookie because the domain is not correct. With these code I create a cookie with domain ".gnsys.local" and not with the domain "gnsys.local"
$cookie = new \Zend\Http\Header\SetCookie("param1", "Hola", null, null, "http://gnsys.local", null, null, null, null);
$this->getResponse()->getHeaders()->addHeader($cookie);
$controllerName = "login";
$actionName = "index";
return $this->redirect()->toRoute(
"publico/default",
array(
"controller" => $controllerName,
"action" => $actionName
));
So, when I try to write the value of cookie "param1", I've got the next error:
Notice: Undefined index: param1 in /var/www/html/gnsys/module/Publico/src/Publico/Controller/LoginController.php
If we check the value of the cookies with firebug, we've got the next screen capture:
What am I doing wrong?
Updated 3:
I don't understand what happened but getCookie is null.
getcookie IS NULL
And print_r($this->getRequest()->getCookie()); doesn't write anything.
$getcookie = $this->getRequest()->getCookie(); // returns object of Zend\Http\Header\Cookie
if ($getcookie != null)
echo "getcookie is NOT NULL";
else
echo "getcookie IS NULL";
print_r($this->getRequest()->getCookie());
return new ViewModel();
Updated 4:
I have found the cookie, but I cannot retrieve its value. To find the cookie, I have to indicate the path where I'm going to read it then.
$cookie = new \Zend\Http\Header\SetCookie("param1", "Hola", 0, "/publico/login/index", "gnsys.local");
$this->getResponse()->getHeaders()->addHeader($cookie);
$controllerName = "login";
$actionName = "index";
return $this->redirect()->toRoute(
"publico/default",
array(
"controller" => $controllerName,
"action" => $actionName
));
And, now I have this exit ...
getcookie is NOT NULL
Zend\Http\Header\Cookie Object ( [encodeValue:protected] => 1 [storage:ArrayObject:private] => Array ( [zdt-hidden] => 0 ) )
If I try to retrieve the value of the cookie through
$cookie = $getcookie->param1;
I've got the next error ...
Notice: Undefined index: param1 in /var/www/html/gnsys/module/Publico/src/Publico/Controller/LoginController.php on line 84
If I try to get all the values from $getcookie
foreach ($getcookie as $key => $value){
echo "Key: " . $key . " Value: " . $value . "<br />";
}
And I've got ...
Key: zdt-hidden Value: 0
Update 5:
I don't understand anything here. I'm not creating the cookie with these code!
$cookie = new \Zend\Http\Header\SetCookie("param1", "Hola", 0, "/", "http://gnsys.local");
$this->getResponse()->getHeaders()->addHeader($cookie);
$controllerName = "login";
$actionName = "index";
return $this->redirect()->toRoute(
"publico/default",
array(
"controller" => $controllerName,
"action" => $actionName
));
Checking the cookies using firebug we can't see the cookie.
In your cookie set action:
Public function cookiesetAction(){
$cookie = new \Zend\Http\Header\SetCookie($name, $value, $expires, $path, $domain, $secure, $httponly, $maxAge, $version);
$this->getResponse()->getHeaders()->addHeader($cookie);
return $this->redirect()->toRoute('cookieget'); //to your login controller
}
In your cookie get action:
public function cookiegetAction(){
$getcookie = $this->getRequest()->getCookie(); // returns object of Zend\Http\Header\Cookie
$getcookie->name1; // value1
$getcookie->name2; // value2
return new ViewModel();
}
Question about cookies in ZF2.
Answer Update
Add child route followed by the main route.
return $this->redirect()->toRoute('publicio/default',array(
'controller'=>$controllername,
'action'=>$actioname
));
Related
The situation:
I build an authentication service that uses Basic Authentication to check if the user exists on an external database and fetches some data. The users in question only exist on the external database.
The problem:
Typo3 needs to have an user entry in the fe_user table to login the user.
So whenever this entry does not exist, the user cannot login.
What I want to do:
Create the user in the authentication service to avoid using a sql dump from the external database and ensure that synchronisation is possible.
The relevant code:
public function authUser(array $user) {
$a_user = $this->login['uname'];
$a_pwd = $this->login['uident_text'];
$url = 'https://soliday.fluchtpunkt.at/api/queryMediaItems';
$data = json_decode('{"language":"de-at"}');
$basicAuth = base64_encode("$a_user:$a_pwd");
// use key 'http' even if you send the request to https://...
$options = array (
'http' => array (
'header' => array(
"Content-Type: application/json",
"Accept: application/json",
"Authorization: Basic {$basicAuth}"
),
'method' => 'POST',
'content' => '{"language":"de-at"}'
)
);
$context = stream_context_create ( $options );
$result = file_get_contents ($url, false, $context);
$response = gzdecode($result);
$checkUser = $this->fetchUserRecord ( $this->login ['uname'] );
if (!is_array($checkUser)&& $result!== FALSE) {
$this->createUser();
}
// failure
if ($result === FALSE) {
return static::STATUS_AUTHENTICATION_FAILURE_BREAK;
}
$this->processData($response);
// success
return static::STATUS_AUTHENTICATION_SUCCESS_BREAK;
}
public function createUser() {
$username = $this->login ['uname'];
$password = $this->login ['uident_text'];
$record = $GLOBALS ['TYPO3_DB']->exec_SELECTgetSingleRow ( '*', 'fe_users', "username = '" . $username . "' AND disable = 0 AND deleted = 0" );
if (! $record) {
// user has no DB record (yet), create one using defaults registered in extension config
// password is not important, username is set to the user's input
$record = array (
'username' => $username,
'password' => $password,
'name' => '',
'email' => '',
'disable' => '0',
'deleted' => '0',
'pid' => $this->config ['storagePid'],
'usergroup' => $this->config ['addUsersToGroups'],
'tstamp' => time ()
);
if (t3lib_extMgm::isLoaded ( 'extbase' )) {
$record ['tx_extbase_type'] = $this->config ['recordType'];
}
$GLOBALS ['TYPO3_DB']->exec_INSERTquery ( 'fe_users', $record );
$uid = $GLOBALS ['TYPO3_DB']->sql_insert_id ();
$record = $GLOBALS ['TYPO3_DB']->exec_SELECTgetSingleRow ( '*', 'fe_users', 'uid = ' . intval ( $uid ) );
}
$_SESSION [$this->sessionKey] ['user'] ['fe'] = $record;
}
the ext_localconf.php file:
<?php
if (!defined('TYPO3_MODE')) {
die ('Access denied.');
}
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addService(
$_EXTKEY,
'auth' /* sv type */,
'AuthService' /* sv key */,
array(
'title' => 'GET Authentication service',
'description' => 'Authenticates users with GET request.',
'subtype' => 'getUserFE, authUserFE',
'available' => true,
'priority' => 90,
'quality' => 90,
'os' => '',
'exec' => '',
'className' => Plaspack\professionalZoneLogin\Service\AuthService::class,
)
);
You should extend AuthenticationService with your own code, way of doing that is described here https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Xclasses/Index.html
Not sure if it's related, but t3lib_extMgm should be \TYPO3\CMS\Core\Utility\ExtensionManagementUtility unless you're using TYPO3 6.
You can also see if you get any SQL errors by calling $GLOBALS['TYPO3_DB']->sql_error().
I am sending post requests in PHP to get a boolean value from my API (so it should return wither true or false)
This is the code I am using in the file for my API. The file is called users.php
if ($_POST['type'] == "authenticateMinecraft"){
$p = new dibdibs\post(
array(
'url' => 'https://authserver.mojang.com/authenticate',
'data' => array(
'agent' => array(
'name' => 'Minecraft',
'version' => 1
),
'username' => $_POST['username'],
'password' => $_POST['password'],
'clientToken' => "33225A179D9A4E1BDA73C012C1C3CBAB8BD00326883BDBEB6FA682482E40F68D"
)
)
);
$res = $p->json();
if (isset($res["selectedProfile"])){
echo("true");
}
else{
echo("false");
}
}
This is the code I am using to reference it (I am using a class which I have put on Pastebin to actually send the request).
$params = array(
'data' => array(
'type' => 'authenticateMinecraft',
'username' => $mcuname,
'password' => $mcpasswd
),
'url' => "api/users.php"
);
$c = new dibdibs\post($params);
$r = $c->http();
var_dump($r);
Whenever I use the .php fule extension when defining url, the whole PHP code of the API page is returned, but when I remove the extension, only true or false is returned. Why is this and is it a problem that I should be aware of and I should fox?
I am following this tutorial http://www.yiiframework.com/wiki/459/integrating-hybridauth-directly-into-yii-without-an-extension/
into my own application but I get an error and I spend many hours to find wich step that I miss.
Here is my error.
Alias "components.HybridAuthIdentity" is invalid. Make sure it points to an existing directory or file.
In components directory, I create a file named HybridAuthIdentity.php
class HybridAuthIdentity extends CUserIdentity{`
public $hybridAuth;
public $adapter;
public $userProfile;
public $allowedProviders = array('google', 'facebook', 'linkedin', 'yahoo', 'live',);
protected $config;
function __construct()
{
$path = Yii::getPathOfAlias('ext.hybridauth');
require_once $path . '/hybridauth/Hybrid/Auth.php';
$this->config = array(
"base_url" => "touchtalent.dev/login/SocialLogin",
"providers" => array(
"LinkedIn" => array(
"enabled" => true,
"keys" => array (
"key" => "My key",
"secret" => "my secret id",
),
),
),
"debug_mode" => false,
"debug_file" => "",
);
$this->hybridAuth = new Hybrid_Auth($this->config);
}
}
In my siteController, I write :
Yii::import('components.HybridAuthIdentity');
$haComp = new HybridAuthIdentity;
if(!$haComp->validateProviderName($_GET['provider']))
{
throw new CHttpException("500", "Invalid action. Please Try again");
}
else
{
$haComp->adaptor = $haComp->hibrydAuth->authenticate($_GET['provider']);
$haComp->userProfile = $haComp->adaptor->getUserProfile();
$haComp->login();
$this->redirect('create');
}
How I can fix it? Thanks for all resposes...
I am working on optimizing Zend Framework Application with Doctrine ORM. I can't figure it out what particular code would I use in my controller to get this caching. Whenever I pass again the same url it should use the cache code instead of processing that logic again.
My Bootstrap file for cache looks like this:-
protected function _initCache() {
$frontendOptions = array(
'lifetime' => 7200, 'content_type_memorization' => true,
'default_options' => array(
'cache' => true,
'cache_with_get_variables' => true,
'cache_with_post_variables' => true,
'cache_with_session_variables' => true,
'cache_with_cookie_variables' => true, ),
'regexps' => array(
// cache the whole IndexController
'^/.*' => array('cache' => true),
'^/index/' => array('cache' => true),
// place more controller links here to cache them
)
);
$backendOptions = array(
'cache_dir' => APPLICATION_PATH ."/../cache" // Directory where to put the cache files
);
$cache = Zend_Cache::factory('Page', 'File', $frontendOptions, $backendOptions);
$cache->start();
Zend_Registry::set("cache", $cache);
}
Any help would be appreciated.
Check this below code to set cache if not exist or get cache if exists.
$result =””;
$cache = Zend_Registry::get('cache');
if(!$result = $cache->load('mydata')) {
echo 'caching the data…..';
$data=array(1,2,3); // demo data which you want to store in cache
$cache->save($data, 'mydata');
} else {
echo 'retrieving cache data…….';
Zend_Debug::dump($result);
}
I'm saving my session data on my database(mongodb), the customer asked that he want the system to be idle for 2 hours(he has videos on the site which may take 2 hours to finish, so he asked to set session timeout value as 2 hour). I did it as Zend Framework suggested. see my config.php below
return array(
'env2_session' => array(
'config' => array(
'class' => 'Zend\Session\Config\SessionConfig',
'options' => array(
'name' => 'test',
'remember_me_seconds' => 7200,
'gc_maxlifetime' => 7200,
),
),
'mongo_handler' => array(
'options' => array(
'class' => 'Zend\Session\SaveHandler\MongoDBOptions',
'collection' => 'sessions',
),
),
'save_handler' => 'Env2Session/Mongo/Handler',
'validators' => array(
array(
'Zend\Session\Validator\RemoteAddr',
'Zend\Session\Validator\HttpUserAgent',
),
),
),
);
But this is not the correct code as I couldn't see the session extended. it still get timed out after 24 minutes.
It will work if I add 'cookie_lifetime' => 7200 after 'gc_maxlifetime' => 7200 But this get timedout even if I'm using the site.
I want the timeout only when the system is idle. Please get me some thing on this.
Here is the save handler code which I written in Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'Env2Session/Mongo/Handler' => function ($sm) {
$config = $sm->get('config');
$session = $config['env2_session'];
$mongo = $sm->get('Env2\Mongo');
$class = isset($session['mongo_handler']['options']['class']) ? $session['mongo_handler']['options']['class'] : 'Zend\Session\SaveHandler\MongoDBOptions';
$options = array();
$options['collection'] = $session['mongo_handler']['options']['collection'];
$options['database'] = $config['db']['mongo']['db_name'];
$mongoOption = new $class($options);
$sessionSaveHandler = new MongoDB($mongo, $mongoOption);
return $sessionSaveHandler;
},
'Env2Session' => function ($sm) {
$config = $sm->get('config');
if (isset($config['env2_session'])) {
$session = $config['env2_session'];
$sessionConfig = null;
if (isset($session['config'])) {
$class = isset($session['config']['class']) ? $session['config']['class'] : 'Zend\Session\Config\SessionConfig';
$options = isset($session['config']['options']) ? $session['config']['options'] : array();
$sessionConfig = new $class();
$sessionConfig->setOptions($options);
}
$sessionStorage = null;
if (isset($session['storage'])) {
$class = $session['storage'];
$sessionStorage = new $class();
}
$sessionSaveHandler = null;
if (isset($session['save_handler'])) {
$sessionSaveHandler = $sm->get($session['save_handler']);
}
$sessionManager = new SessionManager($sessionConfig, $sessionStorage, $sessionSaveHandler);
if (isset($session['validator'])) {
$chain = $sessionManager->getValidatorChain();
foreach ($session['validator'] as $validator) {
$validator = new $validator();
$chain->attach('session.validate', array($validator, 'isValid'));
}
}
} else {
$sessionManager = new SessionManager();
}
Container::setDefaultManager($sessionManager);
return $sessionManager;
}
),
);
}
I was struggling with the same issue for zend session timeouts. Finally I implemented own solution for this case. I set up the expiration time which is refreshed during each request. But if user is not active, the time will expire and session will be destroyed.
I put all tutorial how to do it here: http://blog.tulikowski.com/zend-framework-2-implementation-of-session-authentication-timeouts/
Go to your autoload/global.php and either create or edit this key:
'session_config' => array
(
'cache_expire' => 60*60*2,
'name' => 'sessionName',
'cookie_lifetime' => 60*60*2,
'gc_maxlifetime' => 60*60*2,
'cookie_path' => '/',
'cookie_secure' => FALSE,
'remember_me_seconds' => 60*60*2,
'use_cookies' => true,
),