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
Related
I'm writing a PHP library for accessing the Odoo XML-RPC API and I need to know the Odoo version of the server I'm talking to - but I can't figure out how to determine the version. Is there a certain model that will tell me or how do I do that?
UPDATE
I thought I have figured it out. The ir.module.module model will give you a list of all the installed modules. Then in the base module you look at the installed_version property. BUT this requires admin access! I need to do this as the regular user that is normally using the API.
But for anyone who has that kind of access this is what you would do. Using ripcord (see example here) you would use this line to retrive just the base module:
$models->execute_kw($db, $username, $password, 'ir.module.module', 'search_read', array(array(array('name', '=', 'base'))) );
You can get the Odoo version even without authentication from the API common endpoint. See documentation on https://www.odoo.com/documentation/12.0/webservices/odoo.html heading ”Logging in” and the first code sample there. You can find the server_version property there.
$common = ripcord::client($url.'/xmlrpc/2/common');
$common->version();
Following code is valid and working fine tested on multiple servers.
$url = 'https://###.###.###.##:8069';
$db = 'demo';
$username = 'user_name';
$password = 'password';
$common = ripcord::client("$url/xmlrpc/2/common");
$models = ripcord::client("$url/xmlrpc/2/object");
$common->version();
$uid = $common->authenticate($db, $username, $password, array());
These examples use the Ripcord library, which provides a simple
XML-RPC API. Ripcord requires that XML-RPC support be enabled in your
PHP installation.
Since calls are performed over HTTPS, it also requires that the
OpenSSL extension be enabled.
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
I am working over Oracle DataBase and CakePHP 2.3.
As CakePHP doesn't support Oracle (there are no drivers for it), I am using Oracle procedures or php OCI8 functions in my models.
As a result of it, I am working with CakePHP without any effective database link in the eyes of CakePHP framework.
I am trying to use the Sanitize::clean method in order to clean a comment before saving it in the database and I am having troubles as it seems to look in the database for its task.
This is the resulting error:
Database connection "Mysql" is missing, or could not be created.
And this is how I try to sanitize it:
$comment = Sanitize::clean($this->request->data['comment']);
It works perfectly well if i just do this:
$comment = $this->request->data['comment'];
Is it possible somehow to use Sanitize::clean without any configured database at CakePHP 2.3?
Thanks
The function Sanitize::clean() expects 2 arguments, by default when you do not give a second argument CakePHP uses its default values and tries to connect to a DB with the 'default' connection. After a quick glance at the Sanitize Class in the library, it appears that it is the 'escape' option that requires a DB connection. It is called by default to make a string SQL-safe.
So in your case, as you don't need a SQL connection, this request should do the trick:
$comment = Sanitize::clean($this->request->data['comment'], array('escape' => false);
Check the CookBook for more information on the Sanitize class.
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.
I'm looking for a CakePHP best practice to serve folders/files to clients after they are authentificated. I know it's simpler to use a .htpasswd/.htaccess based solution but i wonder for a better way.
What is it for?
I want to create a client-area where authenticated clients can see contents of there private folder(s). E.g. to test some static html templates before CMS Integration or upload some documents like commented screenshots or pdf files.
A usecase could be:
Create a new client (only by admin)
Generate Login credentials for different user of the same client
Create a new client folder (only by admin)
Upload some static html to the client folder
After login the client can access the folder and view the html
After logout access to the static files is restricted
Any suggestions?
Do you know about CakePHP's "Media Views"? I think that you might be able to do what you want with them.
quick & dirty example...
public function serve($filename = null) {
if($filename && $this->Auth->user()) {
$this->viewClass = 'Media';
$params = array(
'id' => $filename, // full filename
'name' => 'example',
'download' => FALSE, // true, then you get a download box
'extension' => get_the_file_extension($filename),
'path' => APP . 'outside_webroot_dir' . DS
);
$this->set($params);
} else {
// redirect to login or something
}
}
I think the easiest way is to use a database structure for this.
The files are stored on the server anyway, where does not matter.
This is how you do:
Create a table in the database called DataFile (due "File" causes problems with the Cake "File" class). Fields should be something like: id, data_folder_id, name, size, mime_type etc. Use what fits your needs.
Create a table in the database call DataFileFolder. Fields here: id, parent_id, name, visible. Same as above, whatever fits your needs.
Create an association key in the client table or a whole assocation table if needed. (For example: one client and 50 folders in different places). Be aware of the assocation you create. If you use Client->DataFolder the client has automatically access to all files within that folder.
Bake models and a FileController with an index frontend method and admin actions as well as views.
Optimize admin methods for creating either a file or a folder record.
The index method for the frontend has one parameter which represents the folder id. You output each an every folder and file in the folder starting with the first the user is allowed to access. You could also just ouput a list of folders the user is allowed to access in case these folders are on different levels of the new "file manager". You have to check permission on each an every new page call for the given folder id. But that's clear, i think.
Implement a download method for the files based on the media view mentioned above. This should be it.
I think this is the best and easiest way to control the access for such folders.
Due there are some limitations if it is not your server by post_max_size etc. you should maybe think about an external script (or write it on your own if you have the time ;)) to load those file over ftp.
You could also think about a folder accessible on your ftp to upload files. In the "new file" dialog in backend this folder will be outputted and you can include the file into the system by just copying it (via PHP of course). Advantage: only one upload (though it only be two if you are using the ftp upload method mentioned before this).
If you are just into sharing files with clients and those clients are not going to have access on anything else based in your cakephp project just use ftp with a folder for each client. Faster and easier to handle because you can send them urls like "ftp://username:password#yourserver.com" and done. They are logged in, they can view the html files due they are accessing the ftp via the browser and it should be noob safe.
Hope anything of this will feed your needs :)
Greetings
func0der