Running a script
From PHPDevShell
(written on July 17, 2009 for version 2.7.1)
A plugin script is executed either when a user action make it necessary, or because of a cron task. In both case, the script is not run on its own: it's executed in a controlled environment, providing it with all what is necessary to have its job done.
At the time your script starts, a lot of configuration and checking already took place (for example to ensure only accredited users can access it), and the page creation has already started (menu creation, stylesheets...).
This page gives you a overall view of the key features of PHPDevShell development. Please refer to the Reference Guide for more informations.
Contents |
Context
The script is not run "top level" (as a regular PHP script would be), but include'd from a method of an instance of the core class. This means that:
- - variables are not globals, they are local to the method and therefore disappear at the end of the script
- - $this is defined as being the core instance ; however you're not advised to rely on this as it may change in the future
- - you don't have to deal with sessions
- - the output is completly buffered, so you are free to you
header()for exeaple
Available objects
All the object which are the structure of PHPDS are available (through a little magic); namely:
- -
$this->configuration - -
$this->db - -
$this->navigation - -
$this->security - -
$this->core - -
$this->template - -
$this->lang - -
$this->debug - -
$this->error
Several other variables are useful for output (either when using Smarty or not)
- -
$this->template->HTML - -
$module
Usage
If you're using Smarty, use the $view object as you would normally do:
// Assign $this->template->set('user_name', $my_user_name); // Then call the template. $this->template->show();
If not, fill the $HTML variable:
$HTML = <<<HTML <tr class="highlight"> <td>User name</td> <td>$my_user_name</td> </tr> HTML;
Exceptions
PHPDS will nicely catch any exception you throw and display an error message:
throw new Exception('Something bad happened!!');
Data
A script in PHPDevShell can access data in several ways:
User interaction (i.e. from/to the browser) : PHP offer unfiltered superglobals ($_POST, $_GET, $_REQUEST) ; however, PHPDevShell offers a filter (injection-safe) version of these:
$this->security->post $this->security->get $this->security->request
Database requests using PHPDevShell own db class:
$this->db->new_query('SELECT * FROM _db_core_users');
Database request using other calls:
(you're on your own, consult PHP online documentation)
PHPDevShell preference system (a.k.a. Settings), currently embedded inside the db class:
$this->db->write_settings(array('user_key'=>'secret'), 'crypto'); $key = $this->db->get_settings (array('user_key'), 'crypto');

