Libraries
FlexiReed core contains one default library (fxr_lib
) which is itself composed of 6 classes:
- Authentication
- Cache
- Database
- Error
- Input
- Template
Four of them are directly available for the developer, the Database class, the Template class, the Input class and the Authentication class. All the methods callings to these classes must be prefixed with Fxr_lib\
.
Thanks to their namespaces, all classes are automatically loaded with PHP autoloader in: ./core/(front_end/back_end)/libraries/autoload_(frt/bck).php
Database
Database class is available anywhere via the static method loadQuery
:
use libraries\fxr_lib as Fxr_lib;
#Enables to load SQL query
Fxr_lib\loadQuery($query_use, $query, $raws_count, $bind_values);
$query_use = select|insert|delete|update;
$query = 'query string';
$raws_count = 'void|+'; //let this argument empty if you are expecting only one raw
$bind_values = array('value_to_bind'=>'i|d|s|b'); //optional
#Example
define("FOO", Fxr_lib\Database::loadQuery('select', "SELECT foo_body
FROM " . DB_PREFIX . "foo_table
WHERE foo_id = '1'", '', array('foo_body' => 's')));
Template
Template class is available in actions files with view via the static method loadSubTemplates
:
use libraries\fxr_lib as Fxr_lib;
#Enables to load sub-templates
Fxr_lib\loadSubTemplates($sub_tpl_name_file, $loop_nb, $sql_sub_tags,
$add_sub_tags, $output_filter = 1);
$sub_tpl_name_file = 'sub-template name file'; // sub-template file displayed
$loop_nb = 'integer'; // loop number to define in case of non SQL query, or let it empty
$sql_sub_tags = 'query string'; //query must produce more than one raw, or let it empty
$add_sub_tags = array('sub_tag_name' => 'sub_tag_value'); //optional, additional sub-tags
$output_filter = 0|1|2 (default = 1); // 0 = no filter, 1 = utf8_encode(), 2 = htmlentities(utf8_encode())
#Example
$tags_view['foo_tag'] = function()
{
$action_tag = 'foo_tag';
$content = '';
// SQL query with raw = 1
$content .= Fxr_lib\Template::loadSubTemplates(
'_all' . DS . 'content' . DS . 'sub_template_name', '',
Fxr_lib\Database::loadQuery('select', "SELECT foo_body
FROM " . DB_PREFIX . "foo_table
WHERE foo_id = '" . GET_FOO_ID . "'",
'', ''), array('extra_tag'=>'foo_value'), '');
// SQL query with raws > 1 (additional + sign)
$content .= Fxr_lib\Template::loadSubTemplates(
'_all' . DS . 'content' . DS . 'sub_template_name', '',
Fxr_lib\Database::loadQuery('select', "SELECT foo_body
FROM " . DB_PREFIX . "foo_table",
'+', ''), array('extra_tag'=>'foo_value'), '');
// no SQL query
$content .= Fxr_lib\Template::loadSubTemplates(
'_all' . DS . 'content' . DS . 'sub_template_name', '1',
'', array('extra_tag'=>'foo_value'), '');
return $content;
};
To display content, add {foo_tag}
in the associated tpl file:
<html>
<body>
{foo_tag}
</body>
</html>
Then, add {{foo_body}}
& {{extra_tag}}
in the associated sub-tpl file:
<!-- SQL query -->
<p>{{foo_body}}</p>
<p>{{extra_tag}}</p>
<!-- no SQL query -->
<p>{{extra_tag}}</p>
Input
Input class is available in the superglobals init file via the static method getUntaintedInput
:
use libraries\fxr_lib as Fxr_lib;
#Enables to get untainted input
Fxr_lib\getUntaintedInput($input, $data_type, $regex = '', $sql_table_name = '',
$sql_input_name = '', $filter_output = 0, $cache_input = 0);
$input = 'input var' // e.g. $_GET['foo_id']
$data_type = 'digit|alpha|alnum|regex|none' // type of data expected
$regex = 'regex string' //optional, specify 'regex' in $data_type to use it
$sql_table_name = 'sql_table_string' // if applicable, checks table existence in DB
$sql_input_name = 'sql_input_string' // if applicable, checks input existence in DB
$filter_output = 0|1 // htmlentities(utf8_encode($input), ENT_QUOTES)
$cache_input = 0|1 // superglobals cache activation
Only values in green have to be changed when creating a new superglobal constant:
#Example
//======================================================================
// GET_FOO_ID_FRT
//======================================================================
if (isset($_GET['foo_id']) && Fxr_lib\Input::getUntaintedInput(
$_GET['foo_id'], 'digit', '', '', '', '', $code_exception = 8,
$page_exception = ERROR_PAGE_FRT, '', $cache_input = 1)) {
if ($cache_input === 1 && !empty($cache_superglobals)) {
foreach ($cache_superglobals as $input => $array_cache_values) {
if ($input === 'foo_id_frt') {
foreach ($array_cache_values as $cache_value) {
if ($_GET['foo_id'] === $cache_value) {
define("GET_FOO_ID_FRT", $_GET['foo_id']);
}
}
}
}
if (!defined("GET_FOO_ID_FRT")) {
throw new Fxr_lib\Error($_GET['foo_id'], 3, ERROR_PAGE_FRT);
}
} else {
if (!defined('GET_FOO_ID_FRT')) {
if (Fxr_lib\Input::getUntaintedInput($_GET['foo_id'], 'none', '',
'' . DB_PREFIX . 'foo_table_frt', 'foo_id_frt', array(
':foo_id_frt' => array(
$_GET['foo_id'] => 'i'
)
), $code_exception = 7, $page_exception = ERROR_PAGE_FRT, '', ''))
{
define("GET_FOO_ID_FRT", $_GET['foo_id']);
}
}
}
} else {
if (isset($_GET['foo_id']) && empty($_GET['foo_id'])) {
define("GET_FOO_ID_FRT", '');
}
}
By default, 2 constants are already defined in the front-end part:
GET_PAGE_LANG_FRT // language chosen by user
GET_PAGE_ID_FRT // page chosen by user
Input can be manipulated in actions files using the constant created (e.g. GET_FOO_ID_FRT)
Errors
Error class is available when throwing an exception:
#Example
if (...)
...
} else {
throw new Fxr_lib\Error($input, $error_number);
}
If necessary, a custom error number can be created in exceptions.php:
// replace "x" by the number of your choice
$error[x] = 'Your custom message here!';
Extra classes
To load an extra class, create a file with the suffix ".class.php" and an initial uppercase letter, and put it into: ../core/(front_end/back_end)/libraries/add/extra_lib
<?php
namespace libraries\add\extra_lib;
use libraries\fxr_lib as Fxr_lib;
use libraries\add\extra_lib as Extra_lib;
if (!defined('FXR_INDEX')) {
header('HTTP/1.1 403 Forbidden');
exit('No direct script access allowed');
}
class Foo
{
/**
* Extra function
*
* @access public
* @param void
* @return void
*/
public function foo()
{
...
}
}
If the namespace use libraries\add\extra_lib as Extra_lib;
is setted at the top of the page, Foo class can be called like this new Extra_lib\Foo;
and the method called like this Extra_lib\Foo::foo;