Create a REST API for MYSQL Views and display the appropriate REST API endpoints based on whether the MYSQL database view is updateable or not
Last active
December 16, 2020 04:24
-
-
Save ano/bbcc9c04f08863e9414f9fb4c1f45248 to your computer and use it in GitHub Desktop.
PHP-CRUD-API make available via a REST API access to MYSQL views. If the view is updateable allow updating via REST API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| * Function to enable Basic Authentiacation | |
| */ | |
| function require_basic_auth($AUTH_USER, $AUTH_PASS) { | |
| header('Cache-Control: no-cache, must-revalidate, max-age=0'); | |
| $has_supplied_credentials = !(empty($_SERVER['PHP_AUTH_USER']) && empty($_SERVER['PHP_AUTH_PW'])); | |
| $is_not_authenticated = ( | |
| !$has_supplied_credentials || | |
| $_SERVER['PHP_AUTH_USER'] != $AUTH_USER || | |
| password_verify($_SERVER['PHP_AUTH_PW'], $AUTH_PASS) | |
| ); | |
| if ($is_not_authenticated) { | |
| header('HTTP/1.1 401 Authorization Required'); | |
| header('WWW-Authenticate: Basic realm="Access denied"'); | |
| exit; | |
| } | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once("init.php"); | |
| $db_config = array( | |
| 'dbengine'=>'MySQL', | |
| 'hostname'=>MF_DB_HOST, | |
| 'username'=>MF_DB_USER, | |
| 'password'=>MF_DB_PASSWORD, | |
| 'database'=>MF_DB_NAME, | |
| 'table_authorizer'=> function($action,$database,$table){ | |
| $view = view_exists($table); //check if the view exists | |
| $is_updateable = view_is_updateable($table); //check if the view is updateable | |
| //if the view is not updateable only allow view and list access | |
| if (!$is_updateable){ | |
| return ($view === $table) && ($action == 'read' || $action == 'list'); | |
| } | |
| else { | |
| return ($view === $table); | |
| } | |
| }, | |
| 'charset'=>'utf8' | |
| ); | |
| $api = new PHP_CRUD_API($db_config); //Create the API | |
| ob_start('ob_gzhandler'); //Compress the JSON | |
| $api->executeCommand(); //Output the API | |
| function view_exists($table){ | |
| $mdb = new MeekroDB(MF_DB_HOST, MF_DB_USER, MF_DB_PASSWORD, MF_DB_NAME, 3306, 'utf8'); | |
| return $mdb->queryFirstField("SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s AND TABLE_TYPE LIKE 'VIEW'", MF_DB_NAME, $table); | |
| } | |
| function view_is_updateable($table){ | |
| $mdb = new MeekroDB(MF_DB_HOST, MF_DB_USER, MF_DB_PASSWORD, MF_DB_NAME, 3306, 'utf8'); | |
| return ("YES" == $mdb->queryFirstField("SELECT is_updatable FROM information_schema.views WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s", MF_DB_NAME, $table)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** Set Constants **/ | |
| define("BASE_PATH", "../../../"); //Set the relative path to the root directory as a constant | |
| /** MySQL Database settings **/ | |
| define('MF_DB_NAME', 'machform11'); //The name of your database. Note that this database must exist before running installer.php | |
| define('MF_DB_USER', 'root'); //Your database username | |
| define('MF_DB_PASSWORD', ''); //Your database users password | |
| define('MF_DB_HOST', 'localhost'); //The hostname for your database | |
| /** Basic Auth Settings **/ | |
| define("AUTH_USER",'an0tis@gmail.com'); //Basic Auth Username | |
| define("AUTH_PASS", 'machform_R0CK5!'); // Basic Auth Password | |
| /** Load PHP Libraries **/ | |
| require_once(BASE_PATH . "config.php"); //Load machforms configuration file | |
| require_once(BASE_PATH . "hooks/lib/vendor/autoload.php"); //Load composer libraries, specifically MeekroDB, PHP-CRUD-API | |
| require_once(BASE_PATH . "hooks/common/common.php"); //Load a bunch of custom made functions that were developed for Machforms | |
| require_once(BASE_PATH . "hooks/common/libs/basic.auth.function.php"); //Load Basic Auth functions | |
| require_basic_auth(AUTH_USER, AUTH_PASS); //Call the basic auth function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment