cache not getting cleared? - caching

In my content type I have a field (field_fbunique) whose default value is 0; programatically, I change its value to 1 with below code and clear cache.
The value is changed to 1, but when admin users edit the node, the field value is displayed as 0, even though the value in the database is 1 (as expected).
However, to display it as 1, I need to clear cache manually. I have Memcached enabled.
$node = node_load($nid);
$node->field_fbunique['und'][0]['value'] = 1;
field_attach_presave('node', $node);
field_attach_update('node', $node);
$nodeurl = url('node/'. $nid);
cache_clear_all($nodeurl, 'cache_page');
entity_get_controller('node')->resetCache(array($nid));
drupal_flush_all_caches();

With help of this code to exclude form_cache from memcache_bins it started working for me:
$conf['memcache_bins'] = array( 'cache' => 'default', 'cache_form' => 'none', );

Related

How to decrypt cookie?

I've just caught a crash reported on sentry, I am trying to debug and see the root cause for the problem.
Luckily, in the cookies panel, I can see the laravel_session value that was used while crash happened.
The question, is, how can decrypt the cookie?
You can decrypt the cookie with the following code:
$cookie = 'eyJpdiI6ImFUQ0FvMWFSVlNvTmhlQjdLWGw1Z1E9PSIsInZhbHVlIjoicFh6Q09iTDl0K0huWU1Nc1NYVmxSY2hPRGU5Vk85dDJyYUpRbUVjRWg5R0JxYkVobkF3YkZVcVQrakFFUmxaVnZrTjFST3F3RTZ4akpDZEpvUFJiQXc9PSIsIm1hYyI6IjlhYmJhMTY3MWMxYWI3YjJmNmFjMmNkZWE0MWZmMmVhNTNiMjI5ZWY3NzUwNzQ0ZjAzMGQ1ZGU0YzVhNjJmZGYifQ==';
$cookie_contents = json_decode( base64_decode( $cookie, true ));
$value = base64_decode( $cookie_contents->value );
$iv = base64_decode( $cookie_contents->iv );
$clear = unserialize( \openssl_decrypt($value, \Config::get( 'app.cipher' ), \Config::get( 'app.key' ), OPENSSL_RAW_DATA, $iv));
echo "Cookie contents (Session ID): $clear\n";
You should end up with a session ID that looks something like this:
Laravel 5.1: 55782b00dbfcc3f848585ac2cefc66802d773cf5
Laravel 5.4: yPjeV74joY4MtMNNtTpeOYBP2CMixJBBChc9HRND
I didn't test with Laravel 5.3, but I'm confident it will work.
When using this code, make sure you paste the entire contents of the cookie into the $cookie variable, including the two equals signs at the end.
For laravel 6 I think it's pretty much the same
$base64_key = "base64:ISAcSPwQ0HDqqLygaS9LyPzs5ZujMAKOjBou+gyz9sw=";
$payload = json_decode(base64_decode($_COOKIE["your_cookie_name"]), true);
$iv = base64_decode($payload['iv']);
$key = base64_decode(substr($base64_key, 7));
$sessionId = openssl_decrypt($payload['value'], 'AES-256-CBC', $key, 0, $iv);
echo "Session Id: $sessionId";
But check few things:
Cipher encoding, mine is 'AES-256-CBC', it can be 'AES-128-CBC' if your key length is 16
Key format, mine start with "base64:" so I have to remove this part first

Overriding joomla session variables for form handling

I have a joomla website with plethora theme. This is the link to the website where I'm facing problems. http://miraghotels.com/joomjob/membership/planadd?step=2
The form on this link is processed somewhere and an array of variables referring to the form fields and their values is stored in the Joomla session. The problem is that I want to change/override the value of a parameter which is actually in the array variable of the session. To explain this, I'm attaching this code.
$planChosen = $session->get('planChosen', 0, 'register');
First of all, what syntax is this session following? I get a different ouput from just
$session->get('planChosen').
Ok now, when I ouput this variable($planChosen), I get the following output:
Array ( [planname3] => Basic
[planperiod3] => 15 Days
[plancredit3] => 5
[price3] => 0
[plan_id] => 4
[planname4] => Premium
[planperiod4] => 3 Months
[plancredit4] => 25
[price4] => 50
[gateway] => paypal
[option] => com_joomjob
[task] => guest.grabplaninfo
[2d13d7c9e4ffff248cf29092b199f5b2] => 1 )
Now what I want to know is that where is this session stored i.e. where can I find the file where these items are processed in the session.
Secondly, if I want to override a value for e.g. change the value of "gateway" from "paypal" to "moneybookers", how can I achieve that? I tried the following code but no luck:
$string="('planChosen', 0, 'register')[\"gateway\"]";
$session->set($string, "moneybookers");
Please help me out with this
I got this from Joomla.org website
The following code gets the current session then sets the value of the session variable 'myvar' to 'helloworld'.
$session =& JFactory::getSession();
$session->set( 'myvar', 'helloworld' );
The session variable can be retrieved later in a similar way.
$session =& JFactory::getSession();
echo 'Session variable myvar has value: ' . $session->get( 'myvar', 'empty' );
So if you used this code to set paypal as payment gateway
$session->set( 'gateway', 'paypal' );
Now if you want moneybookers you can override paypal by
$session->clear('gateway');
$session->set( 'gateway', 'moneybookers' );
And your session data are saved in database by default unless you changed it to files in the global configuration area. So #__sessions has all your session data.
EDIT:
In that case you can do like this
$session =& JFactory::getSession();
$session->set( 'planChosen', $planChosen );//store array in session variable
//Now you have entire array in this session variable which contains paypal as payment gateway
// Suppose you want to update it then use the code below
$session =& JFactory::getSession();
$planChosen = $session->get( 'planChosen' );
unset($planChosen[$gateway]);
$planChosen["gateway"] = "moneybookers";
$session->set('planChosen',$planChosen);

optimal schema for user permission management

So i have some hardcoded persmissions. Class "A" user can create roles and define access level for each module. When users tries to access a resource system checks if users should be able to read resource and if 'edit' controlls should be displayed.
I'm making a Laravel app, so I'm going to use this also in custom Route::filter.
What I was thinking of is adding $table->integer('resource_title') column to roles table, where:
0 - no access to resource
1 - user level access (read and/or basic functionality)
2 - manage level access (define/edit/delete classifiactors and/or entities)
What would pros do? This? Some extra pivot or $table->boolean('resource_title_access_level')?
For mor flexability you can define access not only for resource but on certain actions on it.
As many roles as u wish.
For example if we have forum:
post
post_edit_own
post_edit
post_delete_own
post_delete
thread_create
thread_edit_own
thread_edit
thread_delete
category_create
category_edit
category_merge
category_delete
You even can store all of user's permissions in ONE field of its table.
users.permissions BIGINT
For doing this you should store this perms in some config like "bitfields" (bad english)
$bf = [
post_create => 1,
post_edit_own => 2,
post_edit => 4,
post_delete_own => 8,
post_delete => 16,
thread_create => 32,
thread_edit_own => 64,
thread_edit => 128,
thread_delete => 256,
category_create => 512,
category_edit => 1024,
category_merge => 2048,
category_delete => 4096,
]
After that you can calculate any needed set of permissions in this manner:
function getPermsSet(array $permsToAdd = ['post_create', 'thread_create']) {
$permSet = 0;
foreach ($permsToAdd as $premission) {
$permSet += $bf[$premission];
}
return $permSet;
}
for example if you want to change user permissions.
"UPDATE users SET permissions = " . getPermSet([some permissions...]). " WHERE id = ?"
Check user accessabity is too easy:
function hasPermission($permission) {
return $this->permissions & $bf[$permission];
}
And use it like this:
if ($user->hasPermission('post_edit') OR
$user->hasPermission('post_edit_own') AND $post->author->id == $user->id
) {
//do things related to post-edition
}
To get users with some permission you can use this
"SELECT * FROM users WHERE permissions & {$bf['category_delete']}" //somth near "Admin"
For example you can give users some specific permissions with their progress (like here on StackOverflow).
And so on. Very flexible.
Sorry for english, "pseudo-code" and almost no relation to Laravel, just variant how to organise permissions-system.
If you are interested i'll give some advices about this in exactly Laravel, little later.

How to use last modified date to cache single page for 1 hour?

What I want to do is to cache a page for 1 hour. The thing is that I want to be able to set the case stale during this 1 hour period if my object is modified.
Here is my code so far:
$response = new Response();
$response->setLastModified(new \DateTime($lastModified));
if ($response->isNotModified($this->getRequest()))
return $response;
else
$response->setCache(array(
'public' => true,
'max_age' => 3600,
's_maxage' => 3600,
));
The problem is the above code doesn't check lastModified. Once the 1 hour cache is created I have to wait full 60 minutes to see the changes I have made to my object ($lastModified).
Here is an example of caching page using Last-Modified header at the symfony2 documentation.
I think your mistake is that you tries to use Last-Modified and then rewrite it with Cache-Control header (max_age, s_maxage).

Zend Framework - session id regenerated, can't stay logged in [duplicate]

This question already has an answer here:
Duplicate DB sessions created upon Zend_Auth login
(1 answer)
Closed 2 years ago.
I'm trying to store sessions in a database using Zend Sessions however for some reason my sessions die out. Im not sure if there's some code being executed which does this or whether its something else.
I've noticed that the session ID seems to be regenerated after a breif time after having logged in.
This is even despite having added the following line in my htaccess file:
php_value session.auto_start 0
The end result is that I'm logged out every minute I'm logged in.
Heres my code in my bootstrap file
$config = array(
'name' => 'session',
'primary' => 'id',
'modifiedColumn' => 'modified',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime'
);
$saveHandler = new Zend_Session_SaveHandler_DbTable($config);
Zend_Session::rememberMe($seconds = (60 * 60 * 24 * 30));
$saveHandler->setLifetime($seconds)->setOverrideLifetime(true);
Zend_Session::setSaveHandler($saveHandler);
//start your session!
Zend_Session::start();
I'm not using any other session related function except perhaps for Zend_Auth when logging in.
Infact rememberme calls the regenerateID function of the Session class - the end result is that I'm constantly logged out every few minutes now.
I think that you might be having this problem because you're calling rememberMe BEFORE starting the session.
You have to start the session first otherwise rememberMe won't do anything since it needs a session to set the rememberMe time on.
rememberMe calls the regenerateId function and the regeneration of the Id is what really needs the session to exist.
Place the rememberMe call after the session start then see how that works for you.
If that isn't it then I don't know what it could be since my code looks similar to yours.
Have you tried something like this?
protected function _initSession() {
$config = array(
'name' => 'session',
'primary' => 'id',
'modifiedColumn' => 'modified',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime',
'lifetime' => 60*60*24*30,
);
Zend_Session::setSaveHandler(new F_Session_SaveHandler_DbTable($config));
}
This way the lifetime isn't set after initialising the database sessions, but is directly included in the initialisation options - it works for me, I see no reason why this should fail in your case :).
I think you need to look once into following values after bootstrap code
session.gc_maxlifetime
session.cookie_lifetime
If You configure session resources in *.ini config file, check resources.session.cookie_domain parameter.
I spend 3 hours when I remembered about it.

Resources