Passing params from cakePHP to file outside of application [duplicate] - cakephp

My situation is the following. I have a cakephp project and a seperated plain php script running on the same server.
When I use my client browser to connect to the cakephp project, it builds up a session as it should.
Now I want to continue the session data with my plain php script. Again I use the same client browser to access the plain php script (so the request meta data should be the same and the session should be recognized) and I set cakephp session option to PHP.
'Session' => [
'defaults' => 'php',
],
However, I cant find out how to continue the session on the plain php script.
I would have assumed the following two lines of my plain php script would do the magic:
session_start();
echo json_encode($_SESSION);
Kind regards,
Marius

CakePHPs PHP session defaults (like all built-in defaults) do change the name of the cookie / the name of the session (session.name INI setting) to CAKEPHP:
https://github.com/cakephp/cakephp/blob/3.5.3/src/Network/Session.php#L133-L138
So you either have to change that to match the defaults used by your vanilla PHP app (which is most probably PHPSESSID, ie the PHP default):
'Session' => [
'defaults' => 'php',
'cookie' => session_name(), // would use the PHP default
],
// ...
or change the latter app to use the name configured for your CakePHP application:
session_name('CAKEPHP');
session_start();
// ...
Also make sure that the session.cookie_path and session.cookie_domain configuration covers both of your applications locations.
See also
Cookbook > Sessions > Session Configuration
Cookbook > Sessions > Setting ini directives

Related

Can't identify Selenium requests in CakePHP 2.6

I have a problem using CakePHP with PHPUnit and Selenium and it is being INCREDIBLY difficult finding any help in the internet. I simply can't figure out how to identify in CakePHP that a request came from my Selenium agent, so that I can set the connection and database environment accordingly.
Any help would be highly appreciated! Further information regarding the best way to set my CakePHP app's database environment when requests come from Selenium are also most welcome.
The first step would be to set the user agent in Selenium to something your app would recognize as special. See the Selenium WebDriver Documentation.
Then in CakePHP you can use the global function env() to test the HTTP_USER_AGENT value. env is a wrapper for checking environment variables like $_SERVER.
For example in your database.php file:
var $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => '****',
'database' => 'production_db',
'prefix' => ''
);
function __construct() {
// set database connection settings for testing environment
if (stristr(env('HTTP_USER_AGENT'), 'selenium') {
$this->default['database'] = 'test_db';
}
}
If you can't set the user agent with Selenium, perhaps you could pass a get variable with the URL.
Example using Selenium extension for PHPUnit:
$this->setBrowserUrl('http://www.example.com?selenium=true');
In CakePHP you would access the variable in the $_GET array.

CakePHP support for initial configuration / web app installation?

I've got a CakePHP based web app that needs some initial configuration. I'd like to do the equivalent of the mysql source command to set up a bunch of tables / initial rows, then execute a $this->User->save() command to create the root account (I think this needs to be done via code since it'll use the salt value for the local install of CakePHP, which might/should be different than the one on my dev machine), etc, etc.
My hack-y solution is to expose a public method on a controller that does this, direct my browser to it, then set stuff up (via the Configure::load and Configure::dump) so that the route from that URL to the method is removed after the installation is complete.
Does CakePHP provide any support for 'installing' a web app?
Part of my problem is that my attempts at Googling for "CakePHP web app installation" are all overshadowed by the various tutorials (etc) about how to install CakePHP itself. My issue is not installing CakePHP, it's providing an easy and safe way to set up the stuff my web app needs (like SQL database tables, etc) for it's particular needs.
It's called Cake Schemas....
The simplest thing you can do in your development environment is run the following via command line from root:
./app/Console/cake schema dump --write filename.sql
Which gives you a dump of your SQL file then you can edit the sql file directly before using it.
You specifically ask for running $this->User->save(), while learning about Schemas might be a bit complicated, you can accomplish this by running
./app/Console/cake schema generate
Which creates your schema.php, then:
App::uses('User', 'Model');
public function after($event = array()) {
if (isset($event['create'])) {
switch ($event['create']) {
case 'users':
App::uses('ClassRegistry', 'Utility');
$user = ClassRegistry::init('User');
$user->create();
$user->save(
array('User' =>
array(
'username' => 'admin',
'role' => 'admin',
'password' => 'admin'
)));
break;
}
}
}
Which makes a definition as you wish, then when you run:
./app/Console/cake schema create
Your tables get dropped, but remade as per your schema definitions and your model files, and with your specific "after" function
http://book.cakephp.org/2.0/en/console-and-shells/schema-management-and-migrations.html

Using CakePHP 1.3, how do you handle HTTPS URLs on live and normal HTTP URLs on dev?

The project I inherited had all of the links hardcoded as "https" and my development environment doesn't recognize "https://somesite.internal-domain.com". Which means some things are occasionally broken.
Does CakePHP offer some way of producing formatted links that take into account whether or not an SSL certificate is available?
Yes it does by default.
If you use relative or array links like
$this->Html->link('Some link', array('controller' => 'foo', 'action' => 'bar', ...));
you will see that cake uses http/https dependend on what you currently use.
That would be the way to go.
If you want to prevent one way just 301 redirect then to the other.
So if at one point you want to switch to https, redirect to from http to http (same link but with different protocol prepended).

CakePHP Cake Bake "database configuration was not found"

I am working on a legacy CakePHP 1.3 app and while I have years of experience with PHP, I am still finding my feet with CakePHP. I am following Chapter 4 'How to use the bakery' in Jamie Munro's Rapid Application Development with CakePHP, however the steps he suggests do not seem to go the way I'd expect them too.
I feel a good way of explaining this is going through the steps involved:
Following the books 'Hello World' example outlined in earlier chapters, I have setup a basic CakePHP app at this location on my machine: /home/public_html/helloworld.local. I can see the 'Hello World' example in a web browser on my local machine when I access: http://helloworld.local/users/add
Chapter 4 suggests that I move to this directory: home/public_html/helloworld.local/cake/console
I then run: ./cake bake
I get prompted to enter the location of the app and I add:
/home/public_html/helloworld.local/app
I then proceed to select defaults for the next few selections and there are no problems until I run into the line:
Your database configuration was not found. Take a moment to create one.
I do not understand this since there is a database file configured in ~/public_html/helloworld.local/app/config/database.php, and when I access the helloworld app outlined earlier (available on my local machine at http://helloworld.local/users/add), there is a database connection successfully established and records can be inserted.
I have also tried re-entering my database details when offered the chance to by cake bake, but end up with the error after succesfully adding the correct details:
Fatal error: Class 'DATABASE_CONFIG' not found in
/home/public_html/helloworld.local/cake/console/libs/tasks/db_config.php
on line 260
But either way, it should have found the existing database connection details, so not sure what is going on.
For using console command like cake bake you have to use your operating system terminal(for linux)/ command prompt(for windows). So you have to do the step mentioned in step 2 and 3 in you console. You can read documentation here to know how to use console commands.
Then, make sure that you have the file home/public_html/helloworld.local/app/config/database.php. I hope you removed .default from its name and rename it to database.php. To link up your database with your cakephp project you have to specify credentials in database.php.
var $default = array('driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => 'password',
'database' => 'database_name',
'prefix' => ''
);
I don't have a running CakePHP 1.3 installation here at hand, but this is what is happening at that location:
// #link: https://github.com/cakephp/cakephp/blob/1.3/cake/console/libs/tasks/db_config.php#L260
config('database');
$db = new $this->databaseClassName; // i.o.w. $db = new DATABASE_CONFIG;
This line:
config('database');
Does nothing more than including the database.php configuration file, simplified to;
include_once(CONFIGS . $arg . '.php'); // i.o.w. include_once(CONFIGS . 'database.php');
(https://github.com/cakephp/cakephp/blob/1.3/cake/basics.php#L77)
So IMO two problems may cause your error;
app/config/database.php was not found
You can try to check if this outputs the right path:
die(CONFIGS . 'database.php');
There is an error in your app/config/database.php, causing the DATABASE_CONFIG class to be malformed and unable to be initialized
A word of notice
Aparently your running 'bake' for everything including setting up a new database configuration. This may overwrite your
existing database configuration. It is possible to bake only parts of your application (e.g. bake controllers only or models).
The manual on baking in CakePHP 1.3 is located here:
http://book.cakephp.org/1.3/en/The-Manual/Core-Console-Applications/Code-Generation-with-Bake.html
And this
If this is your first CakePHP project, you should realy consider the option to upgrade to CakePHP 2.x CakePHP 1.3 is really outdated and, although it's still able to run fine, I wouldn't invest too much time in 1.3 as a lot of things have changed in CakePHP 2.x. It's probably better to start with CakePHP 2.x then to start with 1.3 and learn things that no longer work in CakePHP 2.
Copy your app/Config folder to app/Console, so the final path would be app/Console/Config. That worked for me.

Vanilla forum and CAKEPHP integration with ProxyConnect SingleSignOn (SSO)

I have been trying in vain for many hours to get this working. I have scoured the forums and cannot for the life of me get this to work. Any illumination on the matter would be much appreciated.
I am running:
Vanilla version 2.0.17.8
ProxyConnect version 1.8.4
Cakephp Version 1.3.3
Croogo Version 1.3.2 (cakephp CMS)
I have installed the Vanilla forum in a subfolder app/webroot/vanilla
Install goes smoothly and I upload the proxyconnect plugin into the vanilla plugins folder.
I activate it, and the load the following urls (I have taken out http:// because I am only allowed to post 2 links as I am a newbie here)
Main Site URL The URL of your website where you will use ProxyConnect
localhost:8888/cmrs
Authenticate URL The behind-the-scenes URL that shares identity information with Vanilla
localhost:8888/cmrs/users/authenticate
Registration URL The URL where users can sign up for new accounts on your site
localhost:8888/cmrs/register
Sign-In URL The URL where users sign in on your site
localhost:8888/cmrs/users/login?vanilla=1
Sign-Out URL The URL where users sign out of your site
localhost:8888/cmrs/users/logout?vanilla=1
I have created an action in my users_controller called authenticate()
public function authenticate() {
$this->layout = 'ajax';
$this->header('Content-Type: text/plain');
if($this->Auth->user()) {
$data = $this->Auth->user();
$this->set('data', $data);
}
}
I have created a view authenticate.ctp which outputs the data correctly if you access it directly and you are logged in
<?php
if(isset($data)) {
echo 'UniqueID='.$data['User']['id']."\n";
echo 'Name='.$data['User']['username']."\n";
echo 'Email='.$data['User']['email']."\n";
echo 'TransientKey='."\n";
echo 'DateOfBirth='."\n";
echo 'Gender=';
}
?>
Outputs
UniqueID=1
Name=admin
Email=you#your-site.com
TransientKey=
DateOfBirth=
Gender=
In vanilla config.php I have set
$Configuration['Garden']['Cookie']['Domain'] = '.localhost';
In Cakephp bootstrap.php I have set
ini_set('session.cookie_domain', '.localhost');
So, after all that when I click on signin from vanilla I get redirected to the cake app login and when I login I am not logged in in Vanilla Forum.
When I logout from Vanilla I get redirected to the cake app and am logged out from that but not from Vanilla.
Any suggestions would be greatly appreciated.
Get rid of
TransientKey=
DateOfBirth=
Gender=
from your output as this will fail the ini string format due to blank strings
Firstly, I have been stung once in the past when working with CakePHP's AuthComponent, with cookies not working the way I had expected on localhost. I didn't spend too much time investigating as the code worked in the production environment (on a real domain name).
I suggest you add an entry to your hosts file with a realistic looking domain name. Assuming your production URL will be http://www.example.com/ or http://forum.example.com/, you can map a fake development subdomain to that same domain name by updating your hosts file like so:
127.0.0.1 localhost dev.example.com
You would then access your development environment using http://dev.example.com:8888/ instead of http://localhost:8888/. After that, you would then need to update all the URLs in the Proxyconnect settings, and the cookie domains in the Vanilla/CakePHP configuration files to match this new domain.
// http://dev.example.com:8888/cmrs
// http://dev.example.com:8888/cmrs/users/authenticate
// http://dev.example.com:8888/cmrs/register
// http://dev.example.com:8888/cmrs/users/login?vanilla=1
// http://dev.example.com:8888/cmrs/users/logout?vanilla=1
$Configuration['Garden']['Cookie']['Domain'] = '.example.com';
ini_set('session.cookie_domain', '.example.com');
Secondly, you should be using a development tool to inspect any cookies being created, making sure they are actually being created with the correct domain and path settings (I guess CakePHP should be creating cookies that Proxyconnect can see). A popular combination for doing this easily is to use Firefox + Firebug + Firecookie, but many new browsers have these tools built in (eg. the Resources tab in Chrome's included Developer Tools).
If CakePHP (or Vanilla) is installed in a subdirectory, you may need to check cookies are not being created that are "sandboxed" within the subdirectory. I believe CakePHP will do this by default unless you ini_set('session.cookie_path', '/');.
Also, when CakePHP's Security.level setting has a value of high, it will regenerate a (random) session ID on each request. I would set this to medium, at least while testing.

Resources