/** * Makes an HTTP request to the provided URL * * @param Boolean - should this throw an exception? (Default = TRUE) * * @return **Object** - * Example response object * <code> * { * status: "http_response_header", * route: "STRING", * errors: "ARRAY/OBJECT/STRING", * data: "ARRAY/OBJECT/STRING" * } * </code> * * @since 2015-11-05 * @author Deac Karns <*****@*****.**> * @author Wesley Dekkers <*****@*****.**> */ public function run($throw_exception = TRUE) { $contents = json_decode(file_get_contents($this->url, false, $this->context)); $result = new \stdClass(); $result->status = $http_response_header[0]; $result->route = $this->url; $result->errors = ""; $result->success = TRUE; if (!strpos($http_response_header[0], '200')) { if (is_object($contents) && !empty($contents->errors)) { foreach ($contents->errors as $error) { \Rhonda\Error::add_summary_item($error); } } else { \Rhonda\Error::add_summary_item(implode(" - ", $http_response_header)); } $result->errors = \Rhonda\Error::summary(); $result->success = FALSE; // When hard error End call here if ($throw_exception) { throw new \Exception("Rhonda API Gateway detected an error"); } } $result->data = $contents->data; return $result; }
/** * Read average temperature value of multiple sensors * <pre class="POST"> POST [url]/i2c/temperature/</pre> * * @example * [ * "sensor_id" * , "sensor_id" * ] * * @return JSON - **Object** result info * * @since 2016-02-07 * @author Wesley Dekkers <*****@*****.**> * @todo check if id exists **/ public function temperature_multiple() { try { $config = \Rhonda\Config::get('config'); // Load post body $body = \Rhonda\RequestBody::get(); $celsius = 0; $fahrenheit = 0; $count = count($body); foreach ($body as $sensor_id) { $sensor = new \Models\I2c(); $sensor->id = $sensor_id; $sensor->path = $config->BUS_PATH; $temp = $sensor->get_temperature(); $celsius = $celsius + $temp->celsius; $fahrenheit = $fahrenheit + $temp->fahrenheit; } $average = new \stdClass(); $average->celsius = $celsius / $count; $average->fahrenheit = $fahrenheit / $count; echo json_encode($average); } catch (\Exception $e) { echo \Rhonda\Error::handle($e); } }
/** * Requires a database config file or object to be loaded and the mysqli extension for PHP to be installed</br> * * Escape a String * @example * <code> * $string = "that's all folks"; * $string = \Rhonda\Mysql::real_escape($string); * </code> * * Escape an Object * @example * <code> * $object = new \stdClass(); * $object->thing = "it's for real"; * $object = \Rhonda\Mysql::real_escape($object); * </code> * * Escape an Array * @example * <code> * $array = array( * "ray"=>"it's escaping arrays" * , "ray2"=>"escape's this one too" * ); * $array = \Rhonda\Mysql::real_escape($ray); * * @return Type - Mysql escaped resource that was operated on * * @since 2015-11-20 * @author Deac Karns <*****@*****.**> **/ public static function real_escape($thing) { // check that a configuration object exists for DB try { $db = \Rhonda\Config::get('DB')->connections->local; } catch (\Exception $e) { echo \Rhonda\Error::handle($e); } $mysqli = new \mysqli($db->host, $db->user, $db->password, $db->database, $db->port); $mysqli->set_charset("utf8"); if ($mysqli->connect_errno) { echo \Rhonda\Error::handle($mysqli->connect_error); } // test the thing that is coming in switch (gettype($thing)) { case 'string': $escaped = $mysqli->real_escape_string($thing); break; case 'object': $escaped = self::escape_collection($thing, $mysqli); break; case 'array': $escaped = self::escape_collection($thing, $mysqli); break; default: $escaped = $thing; break; } return $escaped; }
/** * Switch pin ON / OFF * <pre class="PUT"> PUT [url]/switch/set/:pin/:value/</pre> * * @param String - pin number (wPi pin) * @param String - value (ON/OFF) * * @example * No POST Body * * @return JSON - **Object** success message * * @since 2016-02-05 * @author Wesley Dekkers <*****@*****.**> * @todo check if values are set correctly * @todo check if pin exists **/ public function set($pin, $value) { try { if (!is_numeric($pin)) { throw new \Exception("Pin should be numeric"); } # Load the model $set_gpio = new \Models\GPIO(); $set_gpio->pin = $pin; # Decide what to do if ($value == 'ON') { $set_gpio->mode = 'OUT'; $set_gpio->status = 0; } elseif ($value == 'OFF') { $set_gpio->mode = 'IN'; $set_gpio->status = 1; } else { throw new \Exception("No valid pin value entered"); } // Execute the commands $set_gpio->mode(); $set_gpio->write(); // Reload the pin echo json_encode($set_gpio->get()); } catch (\Exception $e) { echo \Rhonda\Error::handle($e); } }
/** * Write a pin value * <pre class="PUT"> PUT [url]/gpio/write/:pin/:value/</pre> * * @param String - pin number * @param String - value * * @example * No POST Body * * @return JSON - **Object** success message * * @since 2016-02-05 * @author Wesley Dekkers <*****@*****.**> * @todo check if values are set correctly * @todo check if pin exists **/ public function send($id) { try { $config = \Rhonda\Config::get('config'); $send = new \Models\RFID(); $send->id = $id; $send->path = $config->RFID_PATH; $send->send(); echo \Rhonda\Success::create(); } catch (\Exception $e) { echo \Rhonda\Error::handle($e); } }
/** * Package incoming data along with the error summary from \Ronda\Error::summary() * * In order an error to be populated it uses \Rhonda\Error::add_summary_item(); and \Rhonda\Error::summary(); * * @param Parameter - The result you want to return * * @example * <code> * \Rhonda\Response:: package($result); * </code> * * @return Return - **Object** * { * "errors": ARRAY of error OBJECTS, * "data": STRING/OBJECT/ARRAY, * "request": { * "url": "REQUEST URL", * "method": "POST/PUT/DELETE/GET/PATCH.. etcetera" * } * } * * @since 2016-07-29 * @author Deac Karns <*****@*****.**> * @author Wesley Dekkers <*****@*****.**> **/ public static function package($data, $response_code = false) { $result = new \stdClass(); $result->errors = \Rhonda\Error::summary(); $result->data = $data; $result->request = new \stdClass(); $result->request->url = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $result->request->method = $_SERVER['REQUEST_METHOD']; // When error not empty and data not empty change headers 206 if (!$response_code && !empty($result->errors) && !empty($data)) { $response_code = 209; } // when error not empty and data empty return 400 if (!$response_code && !empty($result->errors) && empty($data)) { $response_code = 400; } \Rhonda\Headers::set_response_code($response_code); return $result; }
/** * Tell onkyo what setting to change * * @param String - What source type you wanna change PWR/MVL/AMT/SLI * * @example * <code> * { * param: "ID" * , ip: "ip address of device" * , port: "port of device" * } * </code> * * @return Return * * @since 2016-01-01 * @author Wesley Dekkers <*****@*****.**> **/ public function command($type) { try { $body = \Rhonda\RequestBody::get(); $body->type = $type; $onkyo = new \Models\Onkyo(); $socket = 'tcp://' . $body->ip . ':' . $body->port; $fp = stream_socket_client($socket, $errno, $errstr, 30); if (!$fp) { echo "{$errstr} ({$errno})<br />\n"; } if ($body->type == 'MVL') { $body->param = min($body->param, 64); $body->param = str_pad($body->param, 2, '0', STR_PAD_LEFT); } $set = $onkyo->set($fp, $body); if (!$set) { throw new \Exception("Failed to send request"); } echo json_encode($set); } catch (\Exception $e) { echo \Rhonda\Error::handle($e); } }
public function PDO_packager() { try { if (isset($_GET['q'])) { json_decode(urldecode($_GET['q'])); if (json_last_error() !== JSON_ERROR_NONE) { throw new \Exception("Malformed JSON in query string [q]: " . json_last_error_msg()); } } if (isset($_GET)) { self::$get = self::sanitize_get_array($_GET); } $post_body = \Rhonda\RequestBody::get(TRUE); if (!empty($post_body)) { self::$post = $post_body; } } catch (\Exception $e) { echo \Rhonda\Error::handle($e); die; } }
<?php echo "<h3>\\Rhonda\\Error</h3>"; // Format an exception the error log and for return try { throw new Exception("Demo Error Exception 1"); } catch (\Exception $e) { echo \Rhonda\Error::handle($e); } echo "</br>"; try { throw new Exception("Demo Error Exception 2"); } catch (\Exception $e) { $error = new \Rhonda\Error(); echo $error->handle($e); } \Rhonda\Error::deprecation_warning("message", "http://alternate/route"); echo "</br>";
<?php echo "<h3>\\Rhonda\\Arrays</h3>"; // Verify email string structure try { // PASS NON EMPTY CHECK $array = array("non_empty_property"); if (\Rhonda\Arrays::empty_property_check($array)) { echo "We do not have any empty properties SUCCES!"; } else { echo "Something went wrong"; } // PASS EMPTY CHECK $array = array(""); if (!\Rhonda\Arrays::empty_property_check($array)) { echo "We discover empty properties SUCCES!"; } else { echo "Something went wrong"; } // Catch will be invoked } catch (\Exception $e) { echo \Rhonda\Error::handle($e); }
try { throw new Exception("Demo Error Exception 2"); } catch (\Exception $e) { $error = new \Rhonda\Error(); echo $error->handle($e); } echo "</br>"; try { throw new Exception("Demo Error Exception 3", 404); } catch (\Exception $e) { $error = new \Rhonda\Error(); echo $error->handle($e); } echo "</br>"; try { throw new Exception("Demo Error Exception 4"); } catch (\Exception $e) { $error = new \Rhonda\Error(); echo $error->handle($e, 402); } \Rhonda\Error::deprecation_warning("message", "http://alternate/route"); echo "</br>"; $error = new \stdClass(); $error->code = 444; $error->message = "test message"; \Rhonda\Error::add_summary_item($error); \Rhonda\Error::add_summary_item($error); echo "<pre>"; print_r(\Rhonda\Error::summary()); echo "</pre>"; echo "</br>";
/** * Set a pin mode * <pre class="PUT"> PUT [url]/gpio/mode/:pin/:mode/</pre> * * @param String - pin number (wPi pin) * @param String - mode IN/OUT (or others) * * @example * No POST Body * * @return JSON - **Object** success message * * @since 2016-02-05 * @author Wesley Dekkers <*****@*****.**> * @todo check if modes are set correctly * @todo check if pin exists **/ public function mode($pin, $mode) { try { if (!is_numeric($pin)) { throw new \Exception("Pin should be numeric", 1); } $mode_gpio = new \Models\GPIO(); $mode_gpio->pin = $pin; $mode_gpio->mode = $mode; $mode_gpio->mode(); echo \Rhonda\Success::create(); } catch (\Exception $e) { echo \Rhonda\Error::handle($e); } }
<h3>\Rhonda\Response</h3> <?php $data = "Will we have success"; \Rhonda\Error::add_summary_item("We show an error"); echo "<pre>"; print_r(\Rhonda\Response::package($data)); echo "</pre>";
/** * Get the country of the receiver * * @param Parameter - String * * @return Return - String * * @since 2016-02-13 * @author Wesley Dekkers <*****@*****.**> **/ public function code_to_country($code) { try { switch ($code) { case 'DX': return 'North America'; case 'XX': return 'Europe/Asia'; case 'JJ': return 'Japan'; default: return 'Unknown'; } } catch (\Exception $e) { echo \Rhonda\Error::handle($e); } }