For now we're using the readme file which contains a quick documentation section.
INDEX
- 1: Installation
- 2: Usage
- 3: Included classes (Quick Documentation)
- 3.01: Nphp — #Default NaturePhp environment class (loaded directly in init.php)
- 3.02: Log — #Error/debug background system
- 3.03: Check — #Check stuff (client and server environments, variables correctness, etc)
- 3.04: Utils — #Utilities (variable transformations, arrays, options, etc)
- 3.05: Text — #Text functionalities (strings, html, words, characters, etc)
- 3.06: Path — #Path/url string transformations
- 3.07: Mem — #Global cache system (big variables, file cache, etc)
- 3.08: Headers — #HTTP Headers functionalities
- 3.09: Disk — #Filesystem functionalities
- 3.10: Format — #Measures/conversion/standards
- 3.11: Time — #Time and Date functionalities
- 3.12: Template — #Templates manager class
- 3.13: Database — #Awesome sql interface (based on dbFacile by greaterscope)
- 3.14: Event — #Event/action and filtering functionalities (based on Wordpress add_action and add_filter)
- 3.15: Mail — #Email functionalities
- 3.16: Image — #Image manipulation class
Notes:
All [ ] brackets in this readme file indicate code examples (eg. [
phpinfo(); ] ).
All paths, unless otherwise noted, are defined as if you are running an instance (file.php) at a folder in which the “nphp/” subfolder is located.
1. INSTALLATION
Requirements:
NaturePHP requires PHP 5+, preferably 5.2+, ideally 5.3+.
1. Copy “nphp/” folder to your site’s folder (or whatever subfolder you wish).
2. Use [
NaturePHP requires PHP 5+, preferably 5.2+, ideally 5.3+.
1. Copy “nphp/” folder to your site’s folder (or whatever subfolder you wish).
2. Use [
include('nphp/init.php'); ] to include NaturePHP’s autoloader into you site/app instance.
(or can include it in your header/conf/init file?)
2. USAGE
1. Using Lib Classes //
Instance any [
2. Using your own classes //
All available classes are stored at “nphp/libs/”, if you want to use some of your own classes just add them to this folder.
All classes should be standard php classes [
Therefore, using a non-loaded class [
3. Checking if you are on a NaturePhp enviornment //
use [
Instance any [
new Example(); ] or call [ Example::Method(); ] and NaturePHP automatically loads the class for you if not yet loaded.
2. Using your own classes //
All available classes are stored at “nphp/libs/”, if you want to use some of your own classes just add them to this folder.
All classes should be standard php classes [
class Example{ } ] at file “Example.php”.
Therefore, using a non-loaded class [
new Example(); ] in your instance will result in NaturePHP autoloading the file “nphp/libs/Example.php”.
3. Checking if you are on a NaturePhp enviornment //
use [
if(class_exists('Nphp')) print "You're using NaturePhp v".Nphp::version; ]
3. INCLUDED CLASSES
(QUICK DOCUMENTATION)
Documentation notes:
the arguments {options} are interpreted via Utils::combine_args() and should be an options array, object, querystring or multiple arguments of the previous types.
(eg. an options example {options} can be defined like [
“self-instanced” and “static” classes do not require to and should NOT be instanced ( [
All methods in these classes should used statically [
way [
The rest of the documentation follows most of the rules used at php.net for php itself
the arguments {options} are interpreted via Utils::combine_args() and should be an options array, object, querystring or multiple arguments of the previous types.
(eg. an options example {options} can be defined like [
array('option1'=>'value1', 'option2'=>$value2) ] ).
“self-instanced” and “static” classes do not require to and should NOT be instanced ( [
$var=new Example(); ] ).
All methods in these classes should used statically [
Example::method(); ] and NOT the instanced way [
$var->method(); ]
The rest of the documentation follows most of the rules used at php.net for php itself
3.01 Nphp → #Default NaturePhp environment class //
Notes:
You don’t really should need to use this class, this is mainly for internal use of the autoloader and management UI. This class is static
Methods
lib_is_loaded
//
Checks if Class is loaded in the NaturePhp environment
eg. [
lib_exists
//
Checks if Class is available in the NaturePhp environment
eg. [
nphp_folder
//
Returns “nphp/” folder full physical path
eg. [
lib_path
//
Return full physical path to file for class $lib
eg. [
load_lib
//
Loads a class from NaturePhp’s Library (you don’t really need to use this, all libs are autoloaded when necessary by default)
eg. [
Properties
version // NaturePhp core version
eg. [
Notes:
You don’t really should need to use this class, this is mainly for internal use of the autoloader and management UI. This class is static
Methods
lib_is_loaded
//
bool Nphp::lib_is_loaded(string $lib)
Checks if Class is loaded in the NaturePhp environment
eg. [
Nphp::lib_is_loaded('N'); ]
lib_exists
//
bool Nphp::lib_exists(string $lib [, $complete_path=false])
Checks if Class is available in the NaturePhp environment
eg. [
Nphp::lib_exists('N'); ]
nphp_folder
//
string Nphp::nphp_folder()
Returns “nphp/” folder full physical path
eg. [
print Nphp::nphp_folder(); ]
lib_path
//
string Nphp::lib_path(string $lib)
Return full physical path to file for class $lib
eg. [
Nphp::lib_path('N'); ]
load_lib
//
void Nphp::load_lib(string $lib [, $complete_path=false])
Loads a class from NaturePhp’s Library (you don’t really need to use this, all libs are autoloaded when necessary by default)
eg. [
Nphp::load_lib('N'); ]
Properties
version // NaturePhp core version
eg. [
if(class_exists('Nphp')) print "You're using NaturePhp v".Nphp::$version; ]
3.02 Log → #Default NaturePhp environment class //
Note: This class is self-instanced
Methods
init //
Initiates the N error control system.
eg. [
debug
//
Use $debug=true for all log content and php errors to be shown on Log::kill(), $debug=true returns a user friendly message.
eg. [
notify
//
Notifies the email when fatal error occurs on non-debug mode.
eg. [
has_warnings
//
Check if a WARNING level error has already occurred on this page.
eg. [
log
//
Logs an application event to the Log instance (trace stack)
eg. [
kill
//
Like die(); but prints a pretty error/debug log to the user/developer.
eg. [
Note: after you’ve initiated N [
Note: This class is self-instanced
Methods
init //
void Log::init([bool $debug=false])
Initiates the N error control system.
eg. [
Log::init(true); ]
debug
//
void Log::debug(bool $debug)
Use $debug=true for all log content and php errors to be shown on Log::kill(), $debug=true returns a user friendly message.
eg. [
Log::debug(true); ]
notify
//
void Log::notify(string $email)
Notifies the email when fatal error occurs on non-debug mode.
eg. [
Log::notify('example@example.com'); ]
has_warnings
//
bool Log::has_warnings()
Check if a WARNING level error has already occurred on this page.
eg. [
if(Log::has_warnings()) print "there are critical errors already on this page"; ]
log
//
void Log::add(string $where, string $text)
Logs an application event to the Log instance (trace stack)
eg. [
Log::add("footer", "The footer has been printed"); ]
kill
//
void Log::kill(string $text)
Like die(); but prints a pretty error/debug log to the user/developer.
eg. [
Log::kill("Required file not found"); ]
Note: after you’ve initiated N [
Log::init(bool $debug); ] when can use trigger_error() throughout your application/libraries and N will collect your notices and errors and take according action.
3.03 Check → #Check stuff (client and server environments, variables correctness, etc)
Note: This class is static
Methods
common usage
http_host
//
Checks if string is a valid email;
eg. [
email
//
Checks if string is a valid http host;
eg. [
username
//
Checks if string is a valid username (common usage);
eg. [
ip_address
//
Checks if string is a valid ipv4 address;
eg. [
hex_color //
Checks if string is a valid hex color;
eg. [
phone_number
//
Checks if string is a valid phone number;
eg. [
url //
Checks if string is a valid url;
eg. [
Server Type Detection
server_is_apache
//
Checks if server is Apache based;
eg. [
server_is_iis
//
Checks if server is Apache based;
eg. [
Server OS Detection
server_is_unix //
Checks if server is Unix based;
eg. [
server_is_windows
//
Checks if server is Windows based;
eg. [
Client OS Detection
client_is_mac
//
Checks if client is Mac based;
eg. [
client_is_windows
//
Checks if client is Windows based;
eg. [
client_is_winxp
//
Checks if client is Windows XP based;
eg. [
client_is_linux
//
Checks if client is Linux based;
eg. [
Client Browser Detection
browser_is_lynx
//
Checks if browser is Lynx;
eg. [
browser_is_gecko
//
Checks if browser is Gecko based;
eg. [
browser_is_ie
//
Checks if browser is IE;
eg. [
browser_is_opera
//
Checks if browser is Opera;
eg. [
browser_is_ns4
//
Checks if browser is NS4 based;
eg. [
browser_is_firefox
//
Checks if browser is Firefox;
eg. [
browser_is_webkit
//
Checks if browser is Webkit based;
eg. [
browser_is_safari
//
Checks if browser is Safari;
eg. [
browser_is_chrome
//
Checks if browser is Google Chrome;
eg. [
Request type detection
request_is_ajax
//
Checks if request was made in ajax;
eg. [
request_is_ssl
//
Checks if request was made in SSL (https);
eg. [
Requested format detection
is_json_requested
//
Checks if JSON output is requested;
eg. [
is_javascript_requested
//
Checks if JSON output is requested;
eg. [
is_xhtml_requested
//
Checks if XHTML output is requested;
eg. [
is_html_requested
//
Checks if HTML output is requested;
eg. [
is_xml_requested
//
Checks if XML output is requested;
eg. [
Generic functions
request_accepts
//
Checks if request accepts type of $output;
eg. [
request_inHeader
//
Checks if $needle exists in request headers $header;
eg. [
Note: This class is static
Methods
common usage
http_host
//
bool Check::http_host(string $http_host)
Checks if string is a valid email;
eg. [
if(Check::http_host("example.domain.com")) print "HTTP Host correct!"; ]
//
bool Check::is_email(string $email)
Checks if string is a valid http host;
eg. [
if(Check::is_email("example@example.com")) print “Email correct!”; ]
username
//
bool Check::username(string $email)
Checks if string is a valid username (common usage);
eg. [
if(Check::username("My_Username27")) print "Username correct!"; ]
ip_address
//
bool Check::ip_address(string $ipaddress)
Checks if string is a valid ipv4 address;
eg. [
if(Check::ip_address("255.255.255.0")) print "Ip address correct!"; ]
hex_color //
bool Check::hex_color(string $hex_color)
Checks if string is a valid hex color;
eg. [
if(Check::hex_color("#ffcc00")) print "Hex color correct!"; ]
phone_number
//
bool Check::phone_number(string $phone_number)
Checks if string is a valid phone number;
eg. [
if(Check::phone_number("(+351) 123 45 67 89")) print "Phone number correct!"; ]
url //
bool Check::url($url [, $absolute = false])
Checks if string is a valid url;
eg. [
if(Check::url("http://my.domain.com/app.php?id=1", true)) print "Url format correct!"; ]
Server Type Detection
server_is_apache
//
bool Check::server_is_apache()
Checks if server is Apache based;
eg. [
if(Check::server_is_apache()) print "This is an apache based server!"; ]
server_is_iis
//
bool Check::server_is_iis()
Checks if server is Apache based;
eg. [
if(Check::server_is_iis()) print "This is a Microsoft IIS server!"; ]
Server OS Detection
server_is_unix //
bool Check::server_is_unix()
Checks if server is Unix based;
eg. [
if(Check::server_is_unix()) print "This is an Unix based server!"; ]
server_is_windows
//
bool Check::server_is_windows()
Checks if server is Windows based;
eg. [
if(Check::server_is_windows()) print "This is a Windows based server!"; ]
Client OS Detection
client_is_mac
//
bool Check::client_is_mac()
Checks if client is Mac based;
eg. [
if(Check::client_is_mac()) print "You are on a Mac!"; ]
client_is_windows
//
bool Check::client_is_windows()
Checks if client is Windows based;
eg. [
if(Check::client_is_windows()) print "You are on Windows!"; ]
client_is_winxp
//
bool Check::client_is_winxp()
Checks if client is Windows XP based;
eg. [
if(Check::client_is_winxp()) print "You are on Windows XP!"; ]
client_is_linux
//
bool Check::client_is_linux()
Checks if client is Linux based;
eg. [
if(Check::client_is_linux()) print "You are on Linux!"; ]
Client Browser Detection
browser_is_lynx
//
bool Check::browser_is_lynx()
Checks if browser is Lynx;
eg. [
if(Check::browser_is_lynx()) print "You are using Lynx!"; ]
browser_is_gecko
//
bool Check::browser_is_gecko()
Checks if browser is Gecko based;
eg. [
if(Check::browser_is_gecko()) print "You are using Gecko (Firefox)!"; ]
browser_is_ie
//
bool Check::browser_is_ie()
Checks if browser is IE;
eg. [
if(Check::browser_is_ie()) print "You are using IE!"; ]
browser_is_opera
//
bool Check::browser_is_opera()
Checks if browser is Opera;
eg. [
if(Check::browser_is_opera()) print "You are using Opera!"; ]
browser_is_ns4
//
bool Check::browser_is_ns4()
Checks if browser is NS4 based;
eg. [
if(Check::browser_is_ns4()) print "You are using Netscape!"; ]
browser_is_firefox
//
bool Check::browser_is_firefox()
Checks if browser is Firefox;
eg. [
if(Check::browser_is_firefox()) print "You are using Firefox!"; ]
browser_is_webkit
//
bool Check::browser_is_webkit()
Checks if browser is Webkit based;
eg. [
if(Check::browser_is_webkit()) print "You are using Webkit!"; ]
browser_is_safari
//
bool Check::browser_is_safari()
Checks if browser is Safari;
eg. [
if(Check::browser_is_safari()) print "You are using Safari!"; ]
browser_is_chrome
//
bool Check::browser_is_chrome()
Checks if browser is Google Chrome;
eg. [
if(Check::browser_is_chrome()) print "You are using Chrome!"; ]
Request type detection
request_is_ajax
//
bool Check::request_is_ajax()
Checks if request was made in ajax;
eg. [
if(Check::request_is_ajax()) print "You are requesting via Ajax!"; ]
request_is_ssl
//
bool Check::request_is_ssl()
Checks if request was made in SSL (https);
eg. [
if(Check::request_is_ssl()) print "You are requesting via SSL (https)!"; ]
Requested format detection
is_json_requested
//
bool Check::is_json_requested()
Checks if JSON output is requested;
eg. [
if(Check::is_json_requested()) print "You are requesting JSON content!"; ]
is_javascript_requested
//
bool Check::is_javascript_requested()
Checks if JSON output is requested;
eg. [
if(Check::is_javascript_requested()) print "You are requesting JS content!"; ]
is_xhtml_requested
//
bool Check::is_xhtml_requested()
Checks if XHTML output is requested;
eg. [
if(Check::is_xhtml_requested()) print "You are requesting XHTML content!"; ]
is_html_requested
//
bool Check::is_html_requested()
Checks if HTML output is requested;
eg. [
if(Check::is_html_requested()) print "You are requesting HTML content!"; ]
is_xml_requested
//
bool Check::is_xml_requested()
Checks if XML output is requested;
eg. [
if(Check::is_xml_requested()) print "You are requesting XML content!"; ]
Generic functions
request_accepts
//
bool Check::request_accepts(string $output)
Checks if request accepts type of $output;
eg. [
if(Check::request_accepts("binary")) print "You are accepting binary content!"; ]
request_inHeader
//
bool Check::request_inHeader(string $header, string $needle)
Checks if $needle exists in request headers $header;
eg. [
if(Check::request_inHeader("Accept", "binary")) print "You are accepting binary content!"; ]
3.04 Utils → #Utilities (variable transformations, arrays, options, etc)
Note: This class is static
Methods
mixed_to_array
//
transforms mixed variables (querystring, object or array) into “$item=>$value” array;
eg. [
combine_args
//
combines function mixed arguments set with default variables, returns as object;
eg. [
s_var_dump
//
all famous var_dump in pretty string form;
eg. [
build_querystring
//
builds querystring from array (supports multi-dimensional arrays);
eg. [
array_insert
//
inserts value into specific array position, shifts higher items as necessary – overwrites position if $position is string;
eg. [
array_ikey_exists
//
same as array_key_exists() but case insensitive, returns key if found, false if not;
eg. [
generate_password
//
generates password with random characters from a possible string;
available {options} – int ‘lenght’, bool ‘repeat’, string ‘possible’ length defaults to 8, possible characters defaults to “0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”
eg. [
shuffle
//
same as shuffle() but supports associative arrays;
eg. [
Note: This class is static
Methods
mixed_to_array
//
array Utils::mixed_to_array(mixed $mixed)
transforms mixed variables (querystring, object or array) into “$item=>$value” array;
eg. [
Utils::mixed_to_array("tag=input&type=text&name=test&value=testing it"); ]
combine_args
//
array Utils::combine_args(array $function_args, int $start_index, array $defaults)
combines function mixed arguments set with default variables, returns as object;
eg. [
Utils::combine_args(func_get_args(), 1, array("type"=>"text", "value"=>"test")); ]
s_var_dump
//
string Utils::s_var_dump(mixed $var)
all famous var_dump in pretty string form;
eg. [
Utils::s_var_dump(array("type"=>"text", "value"=>"test", "intval"=>27)); ]
build_querystring
//
string Utils::build_querystring(array $get_as_array)
builds querystring from array (supports multi-dimensional arrays);
eg. [
Utils::build_querystring(array("type"=>"text", "value"=>"test")); ]
array_insert
//
string Utils::array_insert(array &$original_array, mixed $insert_piece, mixed $position)
inserts value into specific array position, shifts higher items as necessary – overwrites position if $position is string;
eg. [
Utils::array_insert($array, 'test', 3); ]
array_ikey_exists
//
mixed Utils::array_ikey_exists(mixed $needle, array $haystack)
same as array_key_exists() but case insensitive, returns key if found, false if not;
eg. [
Utils::array_ikey_exists("Test", array("joe"=>1, "test"=>2)); ]
generate_password
//
string Utils::generate_password([{options} $options])
generates password with random characters from a possible string;
available {options} – int ‘lenght’, bool ‘repeat’, string ‘possible’ length defaults to 8, possible characters defaults to “0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ”
eg. [
Utils::generate_password(array('length'=>15)); ]
shuffle
//
array Utils::shuffle(array $array)
same as shuffle() but supports associative arrays;
eg. [
$shuffled_array = Utils::shuffle(array("joe"=>"mary", "test"=>2, "foo"=>"bar")); ]
3.05 Text → #Text functionalities (strings, html, words, characters, etc)
Note: This class is static
Methods
str_replace_count
//
Better str_replace() with items nr to replace support;
eg. [
filter_nr_words
//
returns specific number of words from a text (strips html), $has_more will indicate if more words remain on the original string;
eg. [
correct_html_urls
//
filters all src, bgimage, href and css’s url() in html string through Path::url_to;
eg. [
simple_spaces
//
remove extras spaces, tabs, etc.
eg. [
to_javascript
//
convert a string to a safe javascript string
eg. [
to_plain
//
convert an html string to plain text
eg. [
to_plain_simple
//
plain simple – to_plain followed by simple_spaces
eg. [
to_html
//
convert a plain text string to html
eg. [
normalize
//
converts most accented international characters to the correspondent simple charater
eg. [
Note: This class is static
Methods
str_replace_count
//
string Text::str_replace_count(string $needle, string $replacement, string $haystack, int $replacements_number)
Better str_replace() with items nr to replace support;
eg. [
Text::str_replace_count(".", ",", "my email. email@namedomain.com", 1); ]
filter_nr_words
//
string Text::filter_nr_words(string $text, int $number_of_words, bool &$has_more)
returns specific number of words from a text (strips html), $has_more will indicate if more words remain on the original string;
eg. [
Text::filter_nr_words("this is some random text to examplify this function", 5, $has_more); ]
correct_html_urls
//
string Text::correct_html_urls(string $html_content, string $html_file_path)
filters all src, bgimage, href and css’s url() in html string through Path::url_to;
eg. [
Text::correct_html_urls($html_content, "path/to/source_file.html"); ]
simple_spaces
//
string Text::simple_spaces(string $str)
remove extras spaces, tabs, etc.
eg. [
Text::simple_spaces(" Hello, World !"); ]
to_javascript
//
string Text::to_javascript(string $str)
convert a string to a safe javascript string
eg. [
Text::to_javascript("Hi!\n I'm Joe."); ]
to_plain
//
string Text::to_plain(string $str)
convert an html string to plain text
eg. [
Text::to_plain("Hi!
I'm Joe."); ]
to_plain_simple
//
string Text::to_plain_simple(string $str)
plain simple – to_plain followed by simple_spaces
eg. [
Text::to_plain_simple("Hi!
I'm Joe."); ]
to_html
//
string Text::to_html(string $str)
convert a plain text string to html
eg. [
Text::to_html("Hi!\nI'm Joe."); ]
normalize
//
string Text::normalize(string $str)
converts most accented international characters to the correspondent simple charater
eg. [
Text::normalize("Atenção! Estão a ler um exemplo Português!"); ]
3.06 Path → #Path/url string transformations
Note: This class is self-instanced
Methods
to
//
returns physical path to relative location;
eg. [
url_to
//
returns url path to relative location;
eg. [
put
//
short for [
myBase
//
get’s current application instance location;
eg. [
setBase
//
set’s current application instance location (for url correction);
eg. allows usage of
eg. [
//
combine $base_relative_path as base with $relative_path to get new fullpath;
eg. [
relative
//
get relative path from $pathFrom to $pathTo;
eg. [
this_url
//
get current url, allowing get inclusions and/or exclusions;
available {options} – array ‘get_in’, array ‘get_out’, string ‘#’
eg. [
sanitize_url
//
clean uri string;
eg. [
Note: This class is self-instanced
Methods
to
//
string Path::to(string $relative_path, string $current_location)
returns physical path to relative location;
eg. [
Path::to("../folder/my_file.html", __FILE__); ]
url_to
//
string Path::url_to(string $relative_path, string $current_location)
returns url path to relative location;
eg. [
Path::url_to("../folder/my_file.php", __FILE__); ]
put
//
void Path::put(string $relative_path, string $current_location)
short for [
print Path::url_to("file.html"); ]
myBase
//
string Path::myBase()
get’s current application instance location;
eg. [
Path::myBase(); ]
setBase
//
void Path::setBase(string $relative_path, string $current_location)
set’s current application instance location (for url correction);
eg. allows usage of
Path::put() / Path::url_to() within ajax calls;
eg. [
Path::setBase('../invoices/', __FILE__); ]
//
string Path::combine(string $base_relative_path, string $relative_path [, bool $case_sensitive ])
combine $base_relative_path as base with $relative_path to get new fullpath;
eg. [
Path::combine("../app_folder/", "app_subfolder/somefile.inc"); ]
relative
//
string Path::relative(string $pathFrom, string $pathTo [, bool $case_sensitive ])
get relative path from $pathFrom to $pathTo;
eg. [
Path::relative("app_folder/", "app_folder/app_subfolder/file.php"); ]
this_url
//
string Path::this_url([{options} $options])
get current url, allowing get inclusions and/or exclusions;
available {options} – array ‘get_in’, array ‘get_out’, string ‘#’
eg. [
Path::this_url(array('get_in'=>array('id'=>21,'page'=>3), 'get_out'=>array('return', 'user_login'), '#'=>'top')); ]
sanitize_url
//
string Path::sanitize_url(string $url)
clean uri string;
eg. [
Path::sanitize_url("http;//wWw.example.com/exam|ple.%0dphp"); ]
3.07 Mem → #Global cache system (big variables, file cache, etc)
Note: This class is self-instanced
Methods
actions
set
//
set’s value associated with this key, in this pool;
eg. [
get
//
get’s value associated with this key, in this pool;
eg. [
lock
//
locks’s this key, in this pool;
eg. [
unlock
//
unlock’s this key, in this pool;
eg. [
By reference
getRef
//
set’s value associated with this key by reference, in this pool;
eg. [
setRef
//
get’s referenced value associated with this key, in this pool;
eg. [
Checks
is_locked
//
check’s if this key is locked, in this pool;
eg. [
is_set
//
check’s if this key is set, in this pool;
eg. [
Note: This class is self-instanced
Methods
actions
set
//
bool Mem::set(var $key, var $value, string $pool)
set’s value associated with this key, in this pool;
eg. [
Mem::set("my_location/example.txt", file_get_contents("my_location/example.txt"), "file_pool"); ]
get
//
var Mem::get(var $key, string $pool [, bool $required=false])
get’s value associated with this key, in this pool;
eg. [
Mem::get("my_location/example.txt", "some_file_pool"); ]
lock
//
bool Mem::lock(var $key, string $pool)
locks’s this key, in this pool;
eg. [
Mem::lock("my_location/example.txt", "some_file_pool"); ]
unlock
//
bool Mem::unlock(var $key, string $pool)
unlock’s this key, in this pool;
eg. [
Mem::unlock("my_location/example.txt", "some_file_pool"); ]
By reference
getRef
//
bool Mem::setRef(var $key, var &$value, string $pool)
set’s value associated with this key by reference, in this pool;
eg. [
Mem::setRef("some_object", $my_object, "some_objects_pool"); ]
setRef
//
reference &Mem::getRef(var $key, string $pool [, bool $required=false])
get’s referenced value associated with this key, in this pool;
eg. [
Mem::getRef("my_location/example.txt", "some_file_pool"); ]
Checks
is_locked
//
bool Mem::is_locked(var $key, string $pool)
check’s if this key is locked, in this pool;
eg. [
if(Mem::is_locked("my_location/example.txt", "some_file_pool")) print "content 'my_location/example.txt' is locked!"; ]
is_set
//
bool Mem::is_set(var $key, string $pool)
check’s if this key is set, in this pool;
eg. [
if(Mem::is_set("my_location/example.txt", "some_file_pool")) print "content 'my_location/example.txt' is already set!"; ]
3.08 Headers → #HTTP Headers functionalities
Note: This class is static
Methods
redirect
//
redirect function;
eg. [
http_status
//
set headers for http status (403, 500, 404, etc);
eg. [
json
//
set’s appropriate json headers;
available {options} – string $mode, int $cache
eg. [
cache
//
set’s caching headers;
eg. [
nocache
//
set’s appropriate no caching headers;
eg. [
javascript
//
set’s javascript headers;
eg. [
gzip
//
set’s gzip content headers;
eg. [
get_http_status_desc
//
get http status code description;
eg. [
get_all
//
Better getallheaders() for unsupported servers;
eg. [
Note: This class is static
Methods
redirect
//
void Headers::redirect(string $relative_path_or_url [, string $current_location])
redirect function;
eg. [
Headers::redirect("../", __FILE__); ]
http_status
//
bool Headers::http_status(int $status_number)
set headers for http status (403, 500, 404, etc);
eg. [
Headers::http_status(500); ]
json
//
void Headers::json([{options} $options])
set’s appropriate json headers;
available {options} – string $mode, int $cache
eg. [
Headers::json(array("mode" => "text", "cache"=>864000)); ]
cache
//
void Headers::cache([int $offset=864000])
set’s caching headers;
eg. [
Headers::cache(432000); ]
nocache
//
void Headers::nocache()
set’s appropriate no caching headers;
eg. [
Headers::nocache(); ]
javascript
//
void Headers::javascript([int $cache=0])
set’s javascript headers;
eg. [
Headers::javascript(); ]
gzip
//
void Headers::gzip()
set’s gzip content headers;
eg. [
Headers::gzip(); ]
get_http_status_desc
//
string Headers::get_http_status_desc(int $status_number)
get http status code description;
eg. [
Headers::get_http_status_desc(500); ]
get_all
//
array Headers::get_all()
Better getallheaders() for unsupported servers;
eg. [
var_dump(Headers::get_all()); ]
3.09 Disk → #Filesystem functionalities
Note: This class is static
Methods
make_dir
//
Make new directory (recursively, if possible);
eg. [
sanitize_file_name
//
make sure new filename is simple and has standard characters;
return the sanitized filename;
eg. [
unique_filename
//
same as Disk::sanitize_file_name() but guarantees uniqueness of the file in the destination folder and changes the filename as necessary;
returns the new unique filename (without folder);
eg. [
Note: This class is static
Methods
make_dir
//
bool Disk::make_dir(string $folder)
Make new directory (recursively, if possible);
eg. [
Disk::make_dir("nphp/manage/includes"); ]
sanitize_file_name
//
string Disk::sanitize_file_name(string $filename)
make sure new filename is simple and has standard characters;
return the sanitized filename;
eg. [
$filename = Disk::sanitize_file_name("nphp/manage/includes/test.php"); ]
unique_filename
//
string Disk::unique_filename(string $folder, string $filename)
same as Disk::sanitize_file_name() but guarantees uniqueness of the file in the destination folder and changes the filename as necessary;
returns the new unique filename (without folder);
eg. [
$filename = Disk::unique_filename("uploads/", "new_image.png"); ]
3.10 Format → #Measures/conversion/standards
Note: This class is static
Methods
byte_size
//
auto-format byte size – returns rounded KB, MB, TB, etc;
eg. [
number_i18n
//
number format in i18n (eg. 10.000,05);
eg. [
us_number
//
number format in i18n (eg. 10,000.05);
eg. [
Note: This class is static
Methods
byte_size
//
string Format::byte_size(number $bytes, int $decimals = 0)
auto-format byte size – returns rounded KB, MB, TB, etc;
eg. [
Format::byte_size(1024); ]
number_i18n
//
string Format::number_i18n(number $number, int $decimals = 2)
number format in i18n (eg. 10.000,05);
eg. [
Format::number_i18n(10000.05, 2); ]
us_number
//
string Format::us_number(number $number, int $decimals = 2)
number format in i18n (eg. 10,000.05);
eg. [
Format::number_i18n(10000.05, 2); ]
3.11 Time → #Time and Date functionalities
Note: This class is static
Methods
mysql_time
//
mysql formated time – same as NOW;@ ]
stopwatch_start
//
starts microtimer;
eg. [
stopwatch_read
//
reads microtimer, returns microtime passed since Time::stopwatch_start;
eg. [
stopwatch_stop
//
stops microtimer, returns microtime passed since Time::stopwatch_start;
eg. [
Note: This class is static
Methods
mysql_time
//
string Time::mysql_time(hours $gmt_offset = 0)
mysql formated time – same as NOW;@ ]
stopwatch_start
//
void Time::stopwatch_start(string $name)
starts microtimer;
eg. [
Time::stopwatch_start('MyTimer'); ]
stopwatch_read
//
int Time::stopwatch_read(string $name)
reads microtimer, returns microtime passed since Time::stopwatch_start;
eg. [
Time::stopwatch_read('MyTimer'); ]
stopwatch_stop
//
int Time::stopwatch_stop(string $name)
stops microtimer, returns microtime passed since Time::stopwatch_start;
eg. [
Time::stopwatch_stop('MyTimer'); ]
3.12 Tempalte → #Templates manager class
Methods
Template
//
Create a template instance;
available {options} – string $file, string $string, string $part, string $mode, bool $cache, string $content, bool $correct_paths
eg. [
addContent
//
Replaces template part (identifiable by tpl keys ¹);
eg. [
render
//
returns rendered current instance;
eg. [
Notes:
¹ Within the template, keys can be defined as “{#key/}” or “{#key}some content example{#/key}” (similar to xhtml’s “
Methods
Template
//
instance Template::__contruct([{options} $options])
Create a template instance;
available {options} – string $file, string $string, string $part, string $mode, bool $cache, string $content, bool $correct_paths
eg. [
$Template_instance = new Template(array('file' => "site_source/index.html", 'mode'=>'html')); ]
addContent
//
string Template::addContent(string $key, string $content)
Replaces template part (identifiable by tpl keys ¹);
eg. [
$Template_instance->addContent("key", "this is the key's content replacement"); ]
render
//
string Template::render()
returns rendered current instance;
eg. [
$Template_instance->render(); ]
Notes:
¹ Within the template, keys can be defined as “{#key/}” or “{#key}some content example{#/key}” (similar to xhtml’s “
<tag/>” and “<tag>content</tag>”).
Instead of “{}” you can use ‘mode’ specific comments (e.g. html “<!-//key/-->”, javascript “/#key/some content/#/key/”, etc)
3.13 Database → #sql interface (originally based on dbFacile by greaterscope)
Methods
instance function
open
//
creates a new database connection;
available {options}
● string ‘type’, string ‘database’, string ‘user’, string ‘password’, string ‘port’,string ‘host’,
● string ‘charset’, string ‘collation’, string ‘name’, resource ‘resource’,
● string ‘dsn’, string ‘dsn_type’, string ‘cursor_type’
eg. [
classic dbFacile functions
execute
//
executes a query in the database;
eg. [
fetch
//
returns database’s select resultset in vector table;
eg. [
[ $portuguese_users ] results like:
●
●
fechRow
//
return array with first line in database’s select resultset;
eg. [
[ $user_data ] results like:
●
fetchCell
//
returns first value of first line in database’s select resultset;
eg. [
fetchColumn
//
returns array of first values of each line in database’s select resultset;
eg. [
[
fetchKeyValue
//
returns array of first values as array keys, and second values as array values
eg. [
[
insert
//
returns inserted id if possible, false on failure;
eg. [
Enhanced functions by 9Tree
update
//
returns number of updated rows;
available {options} – array where, array group, array order, string limit
eg. [
This query is the same as “update users set location=‘Moon’ where location=‘Portugal’ and is_active=1”;
delete
//
returns number of deleted rows;
available {options} – array where, array group, array order, string limit
eg. [
This query is the same as “delete from users where location=‘Portugal’ and is_active=1”;
Properties
instance; // last created instance
eg. [
gets the latest Database::open instanced
instances; // for holding more than 1 instance
eg. [
gets the first Database::open instanced
eg. [
gets the first Database::open with the option ‘name’=>’myName’
Methods
instance function
open
//
instance Database::open({options})
creates a new database connection;
available {options}
● string ‘type’, string ‘database’, string ‘user’, string ‘password’, string ‘port’,string ‘host’,
● string ‘charset’, string ‘collation’, string ‘name’, resource ‘resource’,
● string ‘dsn’, string ‘dsn_type’, string ‘cursor_type’
eg. [
$Database_inst = Database::open(array('type'=>'mysql', 'database'=>'database_name', 'user'=>'myUser', 'password'=>'myPwd'); ]
classic dbFacile functions
execute
//
bool Database::execute(string $sql_query [, array $parameters = array() [, bool $cache = true]])
executes a query in the database;
eg. [
$Database_inst->execute("update users set password=md5(?) where user_id=?", array($newUserPwd, $user_id)); ]
fetch
//
array[][] Database::fetch(string $sql_query [, array $parameters = array()])
returns database’s select resultset in vector table;
eg. [
$portuguese_users = $Database_inst->fetch("select from users where location=? and is_active=? order by user_id desc limit 2", array('Portugal', true)); ]
[ $portuguese_users ] results like:
●
[ array('user_id'=>2, 'email'=>'someone@9tree.net’, ‘password’=>’SOme00MD5ed00ChArS’, ‘is_active’=>1) ];
●
[ array('user_id'=>1, 'email'=>'someother@9tree.net’, ‘password’=>’SOme00MD5ed00ChArS’, ‘is_active’=>1) ];
fechRow
//
array[] Database::fechRow(string $sql_query [, array $parameters = array()])
return array with first line in database’s select resultset;
eg. [
$user_data = $Database_inst->fechRow("select from users where email=? and password=? limit 1", array($email, $password)); ]
[ $user_data ] results like:
●
[ 'user_id'=>2, array('email'=>'someone@9tree.net’, ‘password’=>’SOme00MD5ed00ChArS’, ‘is_active’=>1) ];
fetchCell
//
var Database::fetchCell(string $sql_query [, array $parameters = array()])
returns first value of first line in database’s select resultset;
eg. [
$user_id = $Database_inst->fetchCell("select user_id from users where email=? and password=md5(?) limit 1", array($email, $password)); ]
fetchColumn
//
array[] Database::fetchColumn(string $sql_query [, array $parameters = array()])
returns array of first values of each line in database’s select resultset;
eg. [
$emails = $Database_inst->fetchColumn("select email from users where is_active=?", array(true)); ]
[
$emails ] results like [ array($email_1, $email_2, ...., $email_N)
];
fetchKeyValue
//
array[] Database::fetchKeyValue(string $sql_query [, array $parameters = array()])
returns array of first values as array keys, and second values as array values
eg. [
$emailPasswords = $Database_inst->fetchKeyValue("select email, password from users where is_active=?", array(true)); ]
[
$emailPasswords ] results like [ array($email_1=>$password_1, $email_2=>$password_2, ...., $email_N=>$password_N) ];
insert
//
var Database::insert(array $data, string $table)
returns inserted id if possible, false on failure;
eg. [
$Database_inst->insert(array("email"=>$email, $password=>md5($password), is_active=>true), "users"); ]
Enhanced functions by 9Tree
update
//
int Database::update(array(data), string $table [, {options} $options])
returns number of updated rows;
available {options} – array where, array group, array order, string limit
eg. [
$Database_inst->update(array("location"=>"Moon"), "users", array('where'=>array('location=? and is_active=?', 'Portugal', true))); ]
This query is the same as “update users set location=‘Moon’ where location=‘Portugal’ and is_active=1”;
delete
//
int Template::delete(string $table [, {options} $options])
returns number of deleted rows;
available {options} – array where, array group, array order, string limit
eg. [
$Database_inst->delete("users", array('where'=>array('location=? and is_active=?', 'Portugal', true))); ]
This query is the same as “delete from users where location=‘Portugal’ and is_active=1”;
Properties
instance; // last created instance
eg. [
$Database_inst=&Database::instance; ]
gets the latest Database::open instanced
instances; // for holding more than 1 instance
eg. [
$Database_inst=&Database::instances[0]; ]
gets the first Database::open instanced
eg. [
$Database_inst=&Database::instances['myName']; ]
gets the first Database::open with the option ‘name’=>’myName’
3.14 Event → #Event/action and filtering functionalities (based on Wordpress add_action and add_filter)
Note: This class is static
Methods
add //
adds callback to execute on event call, optional $position in queue.
eg. [
Note: Callback is called internally on Event::fire(“header_finished”) like myFunction(“header_finished”);
fire
//
fires all queued event functions in queue order
eg. [
add_filter //
adds filtering callback to execute on filter call, optional $position in queue.
eg. [
Note: Callback is called internally on Event::filter(“header_finished”) like myFunction($str, “header_finished”);
filter
//
filters string $str through all queued event functions in queue order
eg. [
Note: This class is static
Methods
add //
array Event::add(string $event, function $callback[, $position=false])
adds callback to execute on event call, optional $position in queue.
eg. [
Event::add("header_finished", "myFunction"); ]
Note: Callback is called internally on Event::fire(“header_finished”) like myFunction(“header_finished”);
fire
//
array Event::fire(string $event)
fires all queued event functions in queue order
eg. [
Event::fire("header_finished"); ]
add_filter //
string Event::add_filter(string $filter, function $callback[, $position=false])
adds filtering callback to execute on filter call, optional $position in queue.
eg. [
Event::add_filter("post_content", "myFunction"); ]
Note: Callback is called internally on Event::filter(“header_finished”) like myFunction($str, “header_finished”);
filter
//
string Event::filter(string $filter, string $str)
filters string $str through all queued event functions in queue order
eg. [
Event::filter("post_content", $post_content); ]
3.15 Mail → #Email functionalities
Note: This class is static
Methods
send
//
send an email (enhanced mail() );
available {options} – string ‘from’, string ‘cc’, string ‘bcc’, string ‘reply-to’, array ‘attachments’, string ‘html’
eg. [
send_html
//
same as Mail::send() but $body should be html instead of plain text
eg. [
Note: This class is static
Methods
send
//
bool Mail::send(string $to, string $subject, string $body [, {options} $options])
send an email (enhanced mail() );
available {options} – string ‘from’, string ‘cc’, string ‘bcc’, string ‘reply-to’, array ‘attachments’, string ‘html’
eg. [
Mail::send(string $to, string $subject, string $body, array('attachments'=>array('image.jpg', 'readme.txt'))); ]
send_html
//
array Mail::send_html(string $to, string $subject, string $body [, {options} $options])
same as Mail::send() but $body should be html instead of plain text
eg. [
Mail::send_html("example@example.com", "A subject", "Hello!
kthxbye"); ]
3.16 Image → #Image manipulation class
Methods
Image //
Create an image instance;
available {options} – string ‘folder’, string ‘basename’, string ‘bcc’, string ‘transparentColorRed’, string ‘transparentColorGreen’, string ‘transparentColorBlue’
eg. [
from_file
//
Create an instance directly from an image file;
eg. [
resize //
eg. [
● ‘scale’% …………. Height and width both scaled by specified percentage.
● ‘scale-x’x’scale-y’% . Height and width individually scaled by specified percentages.
● ‘width’ ………….. Width given, height automagically selected to preserve aspect ratio.
● x’height’ ………… Height given, width automagically selected to preserve aspect ratio.
● ’width’x’height’ ….. Maximum values of height and width given, aspect ratio preserved.
● ’width’x’height’^ …. Minimum values of width and height given, aspect ratio preserved.
● ’width’x’height’! …. Width and height emphatically given, original aspect ratio ignored.
● ’width’x’height’> …. Change as per ’width’x’height’ but only if an image dimension exceeds a specified dimension.
● ’width’x’height’< …. Same as ’width’x’height’^ but only if an image dimension is smaller than a specified dimension.
● ’width’x’height’# …. Same as ’width’x’height’^ but centered and cropped to the ‘not fit’ dimension.
children
//
eg. [
save //
saves the current image
available {options} – string ‘folder’, string ‘basename’, bool ‘secure’ (guarantees file uniqueness, true by default)
eg. [
save_children
//
same options as
eg. [
save //
saves both this image and the dependent children
same options as
eg. [
Methods
Image //
instance Image::__contruct(resource &$image, array $info [, {options} $options])
Create an image instance;
$info should be like: list($info['width'], $info['height'], $info['type']) = getimagesize($options['path']);
available {options} – string ‘folder’, string ‘basename’, string ‘bcc’, string ‘transparentColorRed’, string ‘transparentColorGreen’, string ‘transparentColorBlue’
eg. [
$Image_instance = new Image($image, $info); ]
from_file
//
instance Image::from_file(string $filepath)
Create an instance directly from an image file;
eg. [
$Image_instance = Image::from_file("images/image.jpg"); ]
resize //
void Image::resize(string $style)
eg. [
$instance->resize("100x100#"); ]
$style should be of the following:
● ‘scale’% …………. Height and width both scaled by specified percentage.
● ‘scale-x’x’scale-y’% . Height and width individually scaled by specified percentages.
● ‘width’ ………….. Width given, height automagically selected to preserve aspect ratio.
● x’height’ ………… Height given, width automagically selected to preserve aspect ratio.
● ’width’x’height’ ….. Maximum values of height and width given, aspect ratio preserved.
● ’width’x’height’^ …. Minimum values of width and height given, aspect ratio preserved.
● ’width’x’height’! …. Width and height emphatically given, original aspect ratio ignored.
● ’width’x’height’> …. Change as per ’width’x’height’ but only if an image dimension exceeds a specified dimension.
● ’width’x’height’< …. Same as ’width’x’height’^ but only if an image dimension is smaller than a specified dimension.
● ’width’x’height’# …. Same as ’width’x’height’^ but centered and cropped to the ‘not fit’ dimension.
children
//
void Image::children(array $styles)
$styles is like array('folder1'=>"300x300>", 'folder2'=>"100x100#")
eg. [
$instance->children(array('thumb'=>"300x300>", 'preview'=>"100x100#")); ]
save //
void Image::save([{options} $options])
saves the current image
available {options} – string ‘folder’, string ‘basename’, bool ‘secure’ (guarantees file uniqueness, true by default)
eg. [
$instance->save(array('folder'=>'uploads/images')); ]
save_children
//
void Image::save_children([{options} $options])
saves all the dependent images created via Image::children(), creating a subfolder for each dependent image within the main save folder
same options as
Image::save() but secure only acts on first image saved, consecutive images will have first’s basename
eg. [
$instance->save_children(array('folder'=>'uploads/images')); ]
save //
void Image::save_all([{options} $options])
saves both this image and the dependent children
same options as
Image::save(), secure only acts on original image
eg. [
$instance->save_all(array('folder'=>'uploads/images')); ]