public static function write($http_status_code, $data, $parent_tag, $return_type = 'json') { $function = array('class_name' => __NAMESPACE__, 'method_name' => __METHOD__); //setting default values to the input parameters $http_status_code = set_default($http_status_code, 200); $data = set_default($data, array()); $parent_tag = set_default($parent_tag, 'datas'); $return_type = set_default($return_type, Constants::get('default_return_type')); //retrieve required limits from tools/constants/limits $text_limit = Limits::get('text_limit'); //ensuring that inputs are validated against the array list of requirements $enforcement contains the results of the validation and message of the error if any. $enforcement = enforce_inputs(array($data, 'string:array', null, null, false), array($parent_tag, 'string', $text_limit['min'], $text_limit['max'], false), array($return_type, 'string', Constants::get('allowed_return_types'), null, false)); $http_status_codes = Constants::get('http_status_codes'); if (isset($http_status_codes[$http_status_code])) { //validates if the $http_status_code provided is in the list //check if data type is an array if (!is_array($data)) { $error = Tool::prepare('Data provided must be an array.', '', __LINE__, $return_type, Constants::get('default_error_code')); Tool::error($function, $error, false); } //formats the data return with the appropriate http status code. header('HTTP/1.0 ' . $http_status_code . ' ' . $http_status_codes[$http_status_code], true, $http_status_code); if (compare_string($return_type, Constants::get('xml'))) { //checks if the user requested data to be returned in xml (by default json) header('Content-type: text/xml'); echo '<' . $parent_tag . '>'; foreach ($data as $index => $post) { if (is_array($post)) { foreach ($post as $key => $value) { echo '<', $key, '>'; if (is_array($value)) { foreach ($value as $tag => $val) { echo '<', $tag, '>', htmlentities($val), '</', $tag, '>'; } } echo '</', $key, '>'; } } else { echo '<' . $index . '>' . $post . '</' . $index . '>'; } } echo '</' . $parent_tag . '>'; } else { //header('Content-type: application/json; charset=UTF-8'); echo json_encode(array($parent_tag => $data)); } } else { Writer::write(400, 'Invalid HTTP status code.', Constants::get('error_tag'), $return_type); return; } }
public static function error($info, $error, $loggable = false) { //preset $info('class_name', 'method_name', 'return_type') $class_name = set_default($info['class_name'], ''); $method_name = set_default($info['method_name'], ''); $return_type = set_default($error['return_type'], Constants::get('default_return_type')); //preset $error('message', 'details', 'line', 'error_code') $message = set_default($error['message'], 'Unknown Error Occurred.'); $details = set_default($error['details'], ''); $line = set_default($error['line'], ''); $http_status_code = set_default($error['http_status_code'], Constants::get('default_error_code')); $error['function'] = str_replace($class_name . '/', '', $method_name); Writer::write($http_status_code, $error, Constants::get('error_tag'), $return_type); $debuggable = Config::get('enable_debugging'); if ($debuggable == true) { } if ($loggable == true) { } //write to log exit; }
function enforce_https($return_type = 'json') { $enable_https = Config::get('enable_https'); if ($enable_https == true) { if (validate_https() == false) { Writer::write(412, 'Request is not sent securely with https.', Constants::get('error_tag'), $return_type); } else { return true; } } }
<?php $script = 'tools/writer/writer.php'; echo 'Current Test Script: ' . $script . '<br/>'; /***** BEGIN FILE TESTING HERE *****/ include_once $script; echo '<h3>Writer</h3>'; include_once 'tools/writer/writer.php'; use tools\writer\Writer; /* prints the requested data (error or normal) and return the response as per http status code * $http_status_code accepted are defined in tools/constants/constants.php - $http_status_codes * $data are accepted in either simple text eg. 'error message goes here' or in an array where it contains the return results * $parent_tag can be in any text form, however if the $parent_tag used is the reserved $error_tag defined in tools/constants/constants.php, it will print as an error * $return_type accepted are defined in tools/constants/constants.php - $allowed_return_types */ echo '<hr/>Signature: write($http_status_code, $data, $parent_tag, $return_type = "json")<br/>'; echo 'Writer::write(200, "asd", "parent", "json") <br/>' . Writer::write(200, 'asd', 'parent', 'json'); ?>
public function delete_accessor($param, $post_params) { //define tags for parent tags and child tag $parent_tag = 'results'; $child_tag = 'result'; //handle authentication //initalizing and set default for params $statement = ''; $param[0] = set_default($param[0], ''); //handle $param parsing if (is_ready($param[0]) == false) { //delete all records Writer::write(501, 'Method not implemented.', Constants::get('error_tag'), parent::get_route()->get_return_type()); } else { if (count($param) == 1) { if (is_numeric($param[0])) { //delete record based on the id $statement = 'DELETE FROM accessor WHERE _id = ' . $param[0]; } else { if (is_string($param[0])) { //delete record based on the search term on all columns } } } else { if (count($param) == 2) { //delete record based on the search term on a row column } } } }