PHP Classes

File: config/_bootstrap.php

Recommend this page to a friend!
  Classes of Luis Toscano   Easy API   config/_bootstrap.php   Download  
File: config/_bootstrap.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: 5 years ago
Size: 3,428 bytes
 

Contents

Class file image Download
<?php


// RUN CORE
function run($val, $callback){
  if(
function_exists($callback)){
   
_autoload();
    if(!empty(
$val)){
     
$callback($val);
    }else{
     
$callback();
    }
  }else{
    echo
'This is a D\'oh';
  }
}

// Parsing Request Vars $_POST & $_GET
function parseVars($vars){
  foreach (
$vars as $key => $value) {
   
$vars[$key] = $value;
  }
  return
$vars;
}


// Autoloading & Instances, Factory Pattern
function _autoload(){
  GLOBAL
$library;
  foreach (
glob(LIBS."/*.php") AS $nombre_fichero) {
    include(
$nombre_fichero);
  }
 
factory($library);
}


// Instances Factory
function factory($libs, $get = ''){
  GLOBAL
$factory;
  if(!empty(
$get)){
    return
$factory[$get];
  }else{
    foreach (
$libs as $key => $value) {
      if(!empty(
$value)){
       
$factory[$key] = new $key($value);
      }else{
       
$factory[$key] = new $key();
      }
    }
  }
}

function
jwtgenerate($data){
  try {
    echo
Auth::SignIn($data);
  } catch (
Exception $e) {
    echo
$e;
  }
}

function
jwtcheck($tk){
  return
Auth::Check($tk);
}

// Runner Router
function runner(){
 GLOBAL
$onlypost, $qs, $config;

    if(
$config['appserver'] == 'ionic'){
      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);
        }
      }

    
$payload = file_get_contents("php://input");
    
$data = json_decode($payload);

    
// JSON WEB TOKENS CONTROL
    
if($config['jwtenabled']){
       if(
$data->action == 'jwtgenerate'){
         
run($data->getting,$data->action);
       }else{
         if(
jwtcheck($data->toktok)){
           
run($data->getting,$data->action);
         }
       }
     }else{
       
run($data->getting,$data->action);
     }
    

  }else{

      if(
$_POST){
       
$vars = parseVars($_POST);
        if(
$config['jwtenabled']){
            if(
$vars['action'] == 'jwtgenerate'){
              
run($vars['getting'],$vars['action']);
            }else{
              if(
jwtcheck($vars['toktok'])){
                  if(
in_array($vars['action'], $onlypost)) {
                   
run($vars['getting'],$vars['action']);
                  }else{
                    echo
'This is a D\'oh';
                  }
              }
            }
        }else{
            if(
in_array($vars['action'], $onlypost)) {
             
run($vars['getting'],$vars['action']);
            }else{
              echo
'This is a D\'oh';
            }
        }
      }

      if (
$_SERVER['REQUEST_METHOD'] == 'GET') {
        if(
$qs == ''){
         
run('','hello');
        }else{
          if(
$_GET){
           
$vars = parseVars($_GET);
            if (!
in_array($vars['action'], $onlypost)) {
             
run($vars['getting'],$vars['action']);
            }else{
              echo
'This is a D\'oh';
            }
          }
        }
      }
  }

}