RazorAPI

razorAPI is the class used to mainly by the rars REST server, using the methods for things like login etc. In addition to the base rars functionality, razorAPI is also used as the parent class to all RESTfull models, extending it and using the various methods available for all REST functionality.

PHP static clean_data(data)

Cleans any incomming data ready to be used by razorDB. This cleaned data is only cleaned for use by razorDB, please sanitize any non razorDB data manually. This method is called automatically by any incomming REST requests so no need to clean manually. All data available to REST models will have ready cleaned data available. This function is available to all REST models in rars that extend the RazorAPI class.

$data = RazorAPI::clean_data($data)

Parameters

  • data mixed The data being supplied to the function can be of any type and multi level.

Return

mixed Returns the data in the form it was supplied, with the data cleaned sufficiently for razorDB.

Example

// include class if not loaded
include_once(RAZOR_BASE_PATH.'library/php/razor/razor_api.php');

// clean generic data
$data = array(1 => array(1, false), 2 => "test");
$data = RazorAPI::clean_data($data);

PHP static clean_output(data)

Cleans any outgoing data that has come from razorDB. This method is called automatically by any outgoing REST requests so no need to clean manually. All comming from the REST models will have ready cleaned data available. This function is available to all REST models in rars that extend the RazorAPI class.

$data = RazorAPI::clean_output($data)

Parameters

  • data mixed The data being supplied to the function can be of any type and multi level.

Return

mixed Returns the data in the form it was supplied, with the data cleaned sufficiently from razorDB.

Example

// include class if not loaded
include_once(RAZOR_BASE_PATH.'library/php/razor/razor_api.php');

// clean generic data
$data = array(1 => array(1, false), 2 => "test");
$data = RazorAPI::clean_output($data);

PHP static create_hash(text, [salt_hash], [mode])

Provides a custom hash from a string. Takes a string, creates a hash using sha1 or [mode], creates a random salty string and places it into the hash at a position equal to the length of the original string. To recreate the hash, simplly send the same string back in, send the first half of the hash as the salt_hash, this will recreate the same hash. Can be used for password hash creation and verification.

$hash = RazorAPI::create_hash($text)

Parameters

  • text string The text that wants to be hashed.
  • salt_hash string optional The first half of the original hashed string (when recreating a hash for verification).
  • mode string optional The hash mode to use if hash function available (see php hash()), falls back to sha1 if no hash function or mode set.

Return

string Returns the a hash of the text string.

Example

// include class if not loaded
include_once(RAZOR_BASE_PATH.'library/php/razor/razor_api.php');

// your incomming password from form
$password = "test";

// Create hash of text string, you can save the result to your database
$hash = RazorAPI::create_hash($password);

// re-create the same hash for verification, like when checking against a saved password hash
$password_hash = "the saved password hash from your database which looks like 44jruf78euwi9r8rufjej....";
$hash = RazorAPI::create_hash($password, substr($password_hash, 0, (strlen($password_hash)/2)));

// verify, result is true
var_dump($password_hash == $hash);

PHP login(data)

Will perform a login attempt against data stored in razorDB, outputs the result of the login as a 200 HTML response with JSON data. On successfull login, a token will be returned as JSON, which can be used for future communication to the rars REST server, on failure will return a login error code and message. To access the login function via AJAX, make a request to //your-web-root/rars/login with the username and password parameters (u, p) via GET or POST as parameters or payload.

This function is not called manually, it is used by the rars server to perform authentication via AJAX. The result will be a valid token that will be live for up to 24 hours. After 24 hours of inactivity, the token will become invalid. Any change to system IP or user agent details during the issue of the token will invalidate the token.

Please consult rars api docs for further info on logging in via AJAX.

$hash = RazorAPI::create_hash($text)

Parameters

  • data string The username and password to log in with.
  • data[username] string The username to find (maps to email address).
  • data[password] string The password to attempt to log in with.

Return

JSON Will output a 200 HTML response with JSON data, either a token for future authentication or error code and message.

Example

// include class if not loaded
include_once(RAZOR_BASE_PATH.'library/php/razor/razor_api.php');

// your incomming password from form
$data = array("useranme" => "test@test.com", "password" => "test");

// Create hash of text string, you can save the result to your database
$api = new RazorAPI();
$api->login($data);

// to see how to perform an AJAX login, please consult rars api docs

PHP check_access([access_timeout])

Checks a users access level from either the athorization header or the logged in users cookie, returning the access level for the user. Running this function will look for an authorization token from the headers, if none found it will fall back to cookie token. On finding a token, it will verify the user first as logged in and then work out if they still have an active token. If the token is still active, the token is then verified against the contact, returning the access level if they have a valid token that is active. Default connection limit is 1 day, after this time an unused token will expire requiring a login. This limit can be overridden.

$access_level = $obj->check_access()

Parameters

  • [access_timeout] int optional The time before token should expire, default is 86400 seconds (1 Day).

Return

mixed Returns false on failure or access level of the logged in user as an int.

Example

// include class if not loaded
include_once(RAZOR_BASE_PATH.'library/php/razor/razor_api.php');

// if extending RazorAPI from REST model
$access_level = $this->check_access();

// if using function directly from RazorAPI object
$api = new RazorAPI();
$access_level = $api->check_access();

PHP email(from, to, subject, message)

Send an email with correct headers to an email address. Set the from address, subject and message of the email you want to send; excepts HTML as message.

$obj->email('razorcms@razorcms.co.uk', 'someone@razorcms.co.uk', 'Hello', $message)

Parameters

  • from string The email you want to send from, ensure this is matched to your domain (some JUNK filters will junk addresses not matching sender).
  • to string The email you want to send to.
  • subject string The subject line of the email you are sending.
  • message string The email message as plain text or HTML.

Example

// include class if not loaded
include_once(RAZOR_BASE_PATH.'library/php/razor/razor_api.php');

// message
$message = <<<output 
<h1>Hello</h1>
<p>This is an email</p>
<br>
<p>Thank you!</p>
output;

// if extending RazorAPI from REST model
$this->email('razorcms@razorcms.co.uk', 'someone@razorcms.co.uk', 'Hello', $message);

// if using function directly from RazorAPI object
$api = new RazorAPI();
$api->email();

PHP static response(data, [type], [code])

Outputs a response from the api and ends the script immediately after. Used by the rars server, response will provide the server with a simple way to create a RESTfull response. As default the data will be served via a 200 response along with any data as raw text output. Use the optional perameters to configure the type of response along with data structure of the response. Any responses made will have the correct headers set to enforce the code requested.

RazorAPI::response("hello")

Parameters

  • data mixed Any kind of data to be output/returned to the requester. Send strings, arrays, ints, bools etc.
  • type string optional The structure of the data to be output/returned as 'raw', 'json' or 'xml' (raw is default).
  • code int optional The HTML code to serve for the page (sets headers etc), default is 200. Available codes are 200, 201, 202, 204, 205, 206, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 500, 501

Example

// include class if not loaded
include_once(RAZOR_BASE_PATH.'library/php/razor/razor_api.php');

// Standard 200 response
RazorAPI::response("hello"); // standard raw text response
RazorAPI::response(["data" => "hello", "count" => 1], "json"); // will return {data: "hello", count: 1} 
RazorAPI::response("hello", "xml"); // will return correct headers along with hello

// error response
RazorAPI::response("Failed to find car", json, 404); // Will return correct 404 headers along with {"error":"HTTP/1.0 404 Not Found","response":"Failed to find car"}