PHP Classes

File: core/core.fn.php

Recommend this page to a friend!
  Packages of Luis Toscano   Easy API   core/core.fn.php   Download  
File: core/core.fn.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Easy API
Handle API calls by mapping requests to functions
Author: By
Last change:
Date: 9 months ago
Size: 1,858 bytes
 

Contents

Class file image Download
<?php


// RUN CORE
function invoke($val, $action){
  GLOBAL
$actions;

  if(isset(
$actions[$action])){
   
$fn = $actions[$action];
    if(
is_callable($fn)){
      if(!empty(
$val)){
       
$fn($val);
      }else{
       
$fn();
      }
    }else{
      echo
'Action is not callable.';
    }
  }else{
    echo
'Action not found.';
  }
}



/**
 *
 * Principal Runner Fn, control, validating and re-routing actions.
 *
 */

function runner()
{

  GLOBAL
$qs, $config;

  if(
$config['cors'] == 'true'){
    if (isset(
$_SERVER['HTTP_ORIGIN'])) {
       
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
       
header('Access-Control-Allow-Credentials: true');
       
header('Access-Control-Max-Age: 86400'); // cache for 1 day
   
}

    if (
$_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
      if (isset(
$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])){
         
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
      }

      if (isset(
$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])){
         
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
          exit(
0);
      }
    }
  }

 
$headers = apache_request_headers();
 
$req = Request::getRequest();

  if(isset(
$req['qs'])){
    list(
$params, $action) = explode('/', $req['qs']);
   
$data = (object) ['params' => $params, 'action' => $action];
  }else{
   
$payload = file_get_contents("php://input");
   
$data = json_decode($payload);
  }
 
  if(
$config['jwtenabled'] && !in_array($data->action, ['login','register']) && !$_GET){

    if(isset(
$headers['Authorization'])){
      if(
jwtcheck($headers['Authorization'])){
       
invoke($data->params, $data->action);
      }
    }else{
      exit(
'error : Missing Authorization Header');
    }

  }else{
   
invoke($data->params, $data->action);
  }


}