hathor-cpp-scanner/app/core/api/Hathor.php

187 lines
4.2 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: eric
* Date: 8/22/15
* Time: 6:36 PM
*/
namespace Hathor;
class Hathor
{
public function __construct() {
}
}
class Format extends Hathor
{
/**
*
*/
public function __construct() {
parent::__construct();
return true;
}
/**
*
*/
public function __destruct() {
return true;
}
/**
*
*/
public function __toString() {
return "This is neat";
}
/**
* @param $size
* @return string
* @throws \InvalidArgumentException
*/
public static function tokb( $size ) {
if( is_numeric($size) ):
return sprintf('%.02fk', $size / 1024 );
else:
throw new \InvalidArgumentException('Must be an int in function tokb( int $size )!');
endif;
}
/**
* [tomb description]
* @param int $size [description]
* @return string [description]
* @throws \InvalidArgumentException [<description>]
*/
public function tomb( $size ) {
if( is_numeric($size) ):
return sprintf('%.02fmb', ($size / 1024) / 1024 );
else:
throw new \InvalidArgumentException('Must be an int in function tomb( int $size)!');
endif;
}
/**
* @param $size
* @return string
*/
public function togb( $size ) {
return sprintf('%.02fgb', ( ($size / 1024) / 1024 ) / 1024 );
}
/**
* @param array $array
* @param bool|false $jpp
* @return string
* @throws \InvalidArgumentException
*/
public static function tojson(array $array, $jpp = false)
{
if (!is_array ($array)):
throw new \InvalidArgumentException;
elseif (is_object ($array)):
$array = (array)$array;
endif;
if($jpp):
return json_encode ($array, JSON_NUMERIC_CHECK | JSON_PRESERVE_ZERO_FRACTION | JSON_PRETTY_PRINT );
else:
return json_encode ($array, JSON_NUMERIC_CHECK | JSON_PRESERVE_ZERO_FRACTION );
endif;
}
}
/**
* Class Connection
* @package Hathor
* @description
* Small class to connect to the database
*/
use \Slim\PDO\Database;
class Connection extends Database {
private $dsn;
/**
* @param none
* @description
* Get database information from configuration
* and setup the PDO connection
* @return \Slim\PDO\Database connection
*/
public function __construct() {
global $config;
$this->dsn = sprintf('mysql:host=%s;dbname=%s;charset=utf8', $config->db['host'], $config->db['db']);
return parent::__construct($this->dsn, $config->db['user'], $config->db['pass']);
}
public function __toString() {
return $this->dsn;
}
public function __destruct() {
unset($this);
}
}
/**
* Class Log
* @package Hathor
*/
class Log extends Hathor {
public function write($message) {
var_dump($message);
}
}
/**
* Class DefaultHeaders *
* @package Hathor
* @description
* Middleware to set default headers that
* we want on every request.
*/
use \Slim\Middleware;
class DefaultHeaders extends Middleware
{
private $strln;
public function call()
{
global $config;
$defaults = [
'X-Powered-By' => 'SlimPHP/2, Hathor Music Api/1.0 (gentoo)',
'X-Hathor-Version' => '1.0/gentoo',
'Allow' => 'GET, HEAD, OPTIONS',
'Content-Language' => 'en',
'Content-Type' => 'application/json; charset=utf-8',
'Server' => 'Hathor Music Api/1.0 (gentoo)',
'X-XSS-Prottection' => '1; mode=block',
'ETag' => md5(time())
];
$env = $this->app->environment;
$env['slim.errors'] = fopen($config->logs['error'], 'w');
// Loop through default headers we want set on every response and set them.
foreach($defaults as $headerKey => $headerVal):
$this->app->response->headers->set($headerKey, $headerVal);
endforeach;
// Run inner middleware and application
$this->next->call();
// Set the Content-Length + 1
$this->strln = strlen($this->app->response->getBody());
// Check if it is head. If it is we don't want any content length.
$this->strln = $this->app->request->isHead() ? 0 : $this->strln;
// Set the Content-Length header
$this->app->response->headers->set('Content-Length', $this->strln);
}
}