Skip to content
Snippets Groups Projects
Config.php.template 7.05 KiB
Newer Older
root's avatar
root committed
<?php
namespace SmartData\Config
{
    // CAUTION: Pay attention to changing these parameters
    require_once( __DIR__ . '/SmartData.php');

    use \SmartData\SmartData;

    class Config_Common
    {
        const MYSQL_SEVERNAME = 'localhost';
        const MYSQL_PORT      = 3306;
        const MYSQL_PASSWORD  = '';
root's avatar
root committed

        const MYSQL_SUPERUSER = 'smartdata_admin';
        const MYSQL_SUPERPASS = '';
root's avatar
root committed

        const CASSANDRA_SERVERNAME   = 'localhost';
        const CASSANDRA_PORT         = 9042;

        const CASSANDRA_SUPERPASS = ''; 
root's avatar
root committed

        const CASSANDRA_MAX_ROW_SIZE = 2000000000; // (Two billion - The max is 2^31)
        const POINTS_LIMIT = 2000; // 84600 to 200000
                                     // maximum amount of points to retrieval
                                     // 86400 corresponds to a half day with resolution of one second

        const ADMIN_EMAIL = array('root@lisha.ufsc.br');
        const PUBLIC_DOMAIN    = 'public';          // default domain (public series)
        const DEFAULT_USERNAME = 'public';          // cassandra default user (PUBLIC)
        const DEFAULT_PASSWORD = ''; // cassandra default user's password
root's avatar
root committed

        const TIMEZONE = 'America/Sao_Paulo';

        const HYSTERICALLY_DEBUGGED = false;
        const DEBUGGED = array(
            'SmartData\Backend'       => self::HYSTERICALLY_DEBUGGED || false,
            'SmartData\Credentials'   => self::HYSTERICALLY_DEBUGGED || false,
            'SmartData\SmartData'     => self::HYSTERICALLY_DEBUGGED || false,
            'SmartData\Series'        => self::HYSTERICALLY_DEBUGGED || false,
            'SmartData\Unpackable'    => self::HYSTERICALLY_DEBUGGED || false,
            'SmartData\Logger'        => false,
        );

        const LOG_FILE = '/smartdata/log/api.log';
root's avatar
root committed
        const BUFFERED_LOG = true;

root's avatar
root committed
        const FATAL_ERROR_REPORT = true;
        const FATAL_ERROR_DIR = __DIR__.'/fatal_error/';

        const ACCESSIBLE_WITHOUT_SSL = array(
            self::PUBLIC_DOMAIN
            ,'hydro'
            ,'tutorial'
        );

        /*---------------------------------------------------------------------------------------------------------------+
        | V.1.0: This was the first version after we atopped using KairosDB as a platform                                |
        | V.1.1: This version has the same API of version 1.0, however the structure and                                 |
        |        the DataBase design are totally different. The dev number works fine in this version                    |
        +---------------------------------------------------------------------------------------------------------------*/

        public const VERSION_1_0 = (1 << 4) | (0 << 0);
        public const VERSION_1_1 = (1 << 4) | (1 << 0);
        public const DEFAULT_VERSION = self::VERSION_1_1;
        public const DEVELOPMENT = PHP_INT_MAX;

        private const _VERSIONS = array (
            self::VERSION_1_0 => '/api/v1_0/',
            self::VERSION_1_1 => '/api/v1_1/'
        );

        private const _CERT_GW_LISHA = 'A7B64D415BD3E97B';
root's avatar
root committed

        public static function api_version() {
            global $__FORCE_VERSION;
            if(isset($__FORCE_VERSION))
                return $__FORCE_VERSION;

            //switch (REQUEST_CERT) {
            //    case self::_CERT_GW_LISHA:
            //        return self::VERSION_1_1;
            //    default: break;
            //}

            foreach (self::_VERSIONS as $version => $url_pattern) {
                if(self::check_version($url_pattern))
                    return $version;
            }
            return self::DEFAULT_VERSION; //default version
        }

        private static function check_version(string $version) {
            global $FORCE_VERSION;
            return (strpos($_SERVER['REQUEST_URI']??$FORCE_VERSION??'', $version) !== false);
        }

        public static function pre_init() {
            define('REQUEST_HTTPS' , (($_SERVER['HTTPS'] ?? false) == 'on'));
            define('REQUEST_CERT'  , (REQUEST_HTTPS and ($_SERVER['SSL_CLIENT_VERIFY'] ?? false) == 'SUCCESS'));
            define('REQUEST_SERIAL', (REQUEST_CERT) ? @$_SERVER['SSL_CLIENT_M_SERIAL'] : '');
            define('REQUEST_URI'   , isset($_SERVER["REQUEST_URI"])?strtok(basename($_SERVER["REQUEST_URI"]),'?'):'{local}');
            define('REQUEST_DBG'   , ('HTTP'.((REQUEST_HTTPS)?'S':'').':'.REQUEST_URI.'  crt:{'.REQUEST_SERIAL.'}'));
        }

        public static function init() {
            date_default_timezone_set(Config_Common::TIMEZONE);
        }
    }

    Config_Common::pre_init();

    ## Configuration of API version 1.0 ###############################################
    class Config_V1_0 extends Config_Common
    {
        //const HYSTERICALLY_DEBUGGED = true;

        const MYSQL_DBNAME       = 'smartdata';
        const CASSANDRA_KEYSPACE = 'smartdata_1';

        const CONFIDENCE_ASSIGNER = __DIR__ . '/confidence_assigner/assigner0';

        protected const _MYSQL_TABLE = array (
            SmartData::STATIC_VERSION => 'series',
            SmartData::MOBILE_VERSION => 'series'
        );
    }

    ## Configuration of API version 1.1 ###############################################
    class Config_V1_1 extends Config_Common
    {
        const HYSTERICALLY_DEBUGGED = true;

        const MYSQL_DBNAME       = 'smartdata_v1';
        const CASSANDRA_KEYSPACE = 'smartdata_v1';

        protected const _MYSQL_TABLE = array (
            SmartData::STATIC_VERSION => 'series',
            SmartData::MOBILE_VERSION => 'trackers'
        );
    }

    ###################################################################################

    switch (Config_Common::api_version()) {
        case Config_Common::VERSION_1_0: class MetaConfig extends Config_V1_0 {} break;
        case Config_Common::VERSION_1_1: class MetaConfig extends Config_V1_1 {} break;
        default:
            die ("Bad API version!\n");
    }

    trait ConfigGetters
    {
        public function MySQL_Table($version) : string {
            if(isset(self::_MYSQL_TABLE[$version]))
                return self::MYSQL_DBNAME.'.'.self::_MYSQL_TABLE[$version];
            else
                die ("Unable to select mysql table\n");
        }
    }

    Config_Common::init();
}

namespace SmartData
{
    class Config_Common extends Config\Config_Common {};
    class Config {
        public static function config(){
            switch (Config_Common::api_version()) {
                case Config_Common::VERSION_1_0:
                    return new class() extends Config\Config_V1_0 {
                        use Config\ConfigGetters;
                    };
                   break;
                case Config_Common::VERSION_1_1:
                    return new class() extends Config\Config_V1_1 {
                        use Config\ConfigGetters;
                    };
                    break;
                default:
                    die ("Bad API version!\n");
            }
        }
    };
}