| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- /**
- * Gasteizko Margolariak API v3 - Location
- *
- * Used to get notifications.
- * This file is to be called directly from a URL request.
- *
- * https://margolariak.com/API/v3/help/
- *
- * @since 1.0.0
- */
- //Posible notification target
- define('TARGET_ALL', 'all');
- define('TARGET_GM', 'gm');
- //Default target
- define('DEF_TARGET', TARGET_ALL);
- //$_GET valid parameters
- define('GET_TARGET', 'target');
- //Error messages
- define('ERR_TARGET', '-TARGET:');
- /**
- * Initializes the MySQL database connection.
- *
- * Called at the beggining of the script. It connects to the database using the
- * parameters in the .htpasswd file. It also sets database and page encodings.
- *
- * @since 1.0.0
- * @return object Database connection.
- */
- function startdb(){
- //Include the db configuration file. It's somehow like this
- /*
- <?php
- $host = 'XXXX';
- $db_name = 'XXXX';
- $username_ro = 'XXXX';
- $username_rw = 'XXXX';
- $pass_ro = 'XXXX';
- $pass_rw = 'XXXX';
- ?>
- */
- include('../../.htpasswd');
- //Connect to to database
- $con = mysqli_connect($host, $username_rw, $pass_rw, $db_name);
- //Set encoding options
- mysqli_set_charset($con, 'utf-8');
- header('Content-Type: text/html; charset=utf8');
- mysqli_query($con, 'SET NAMES utf8;');
- //Return the db connection
- return $con;
- }
- /**
- * Discerns the type of notifications requested.
- *
- * Determines which notificatios the request is looking for: public
- * notification or members-only notifications. To do so, it looks for a
- * request parameter keyed 'target'
- *
- * @since 3.0.0
- * @param array $get Array with the request parameters.
- * @return string 'all' or 'gm' if it was passed as the 'target' GET
- * parameter. 'gm' if there was no 'target' parameter or
- * null if some invalid value was passed as the 'target'
- * parameter.
- */
- function get_target($get){
- $target = $get[GET_TARGET];
- if (strlen($target) < 1){
- return DEF_TARGET;
- }
- else{
- if ($target != TARGET_ALL && $target != TARGET_GM){
- return null;
- }
- else{
- return $target;
- }
- }
- }
- /**
- * Retrieves the notifications
- *
- * Retrieves the notificatios for the selected target that are still on
- * time to be delivered and formats them as a JSON object.
- *
- * @since 3.0.0
- * @param object $con Open database connection.
- * @param string $target Optional. 'gm' or 'all'. Default is 'all'.
- * @return string JSON object with the fetched notifications.
- */
- function select_notifications($con, $target = TARGET_ALL){
- if ($target == TARGET_GM){
- $query = "SELECT id, title_es, title_en, title_eu, text_es, text_en, text_eu, dtime, internal AS gm, duration, action, 0 AS seen FROM notification WHERE internal = 1 AND dtime > NOW() - INTERVAL duration MINUTE ORDER BY dtime DESC";
- }
- else{
- $query = "SELECT id, title_es, title_en, title_eu, text_es, text_en, text_eu, dtime, internal AS gm, duration, action, 0 AS seen FROM notification WHERE dtime > NOW() - INTERVAL duration MINUTE ORDER BY dtime DESC";
- }
- $q = mysqli_query($con, $query);
- if (mysqli_num_rows($q) == 0){
- return null;
- }
- else{
- $rows = array();
- while($r = mysqli_fetch_assoc($q)) {
- $rows[] = $r;
- }
- return(json_encode($rows));
- }
- }
- /**
- * Prints the notifications.
- *
- * @since 3.0.0
- * @param string $notifications JSON object with the notifications,
- * as generated by
- * {@see select_notifications($con, $target)}
- */
- function print_notifications($notifications){
- echo($notifications);
- }
- // SCRIPT START
- // Connect to the database
- $con = startdb('rw');
- // Select target
- $target = get_target($_GET);
- if ($target == null){
- // Bad request
- http_response_code(400);
- exit(-1);
- }
- // Get notifications
- $notifications = select_notifications($con, $_GET[GET_TARGET]);
- if ($notifications == null){
- // No content
- http_response_code(204);
- }
- else{
- print_notifications($notifications);
- }
- ?>
|