API::__construct PHP Method

__construct() public method

public __construct ( )
    public function __construct()
    {
        global $CFG_GLPI, $DB;
        // construct api url
        self::$api_url = trim($CFG_GLPI['url_base_api'], "/");
        // Don't display error in result
        set_error_handler(array('Toolbox', 'userErrorHandlerNormal'));
        ini_set('display_errors', 'Off');
        // Avoid keeping messages between api calls
        $_SESSION["MESSAGE_AFTER_REDIRECT"] = '';
        // check if api is enabled
        if (!$CFG_GLPI['enable_api']) {
            $this->returnError(__("API disabled"), "", "", false);
            exit;
        }
        // retrieve ip of client
        $this->iptxt = isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
        $this->ipnum = strstr($this->iptxt, ':') === false ? ip2long($this->iptxt) : '';
        // check ip access
        $apiclient = new APIClient();
        $where_ip = "";
        if ($this->ipnum) {
            $where_ip .= " AND (`ipv4_range_start` IS NULL\n                             OR (`ipv4_range_start` <= '{$this->ipnum}'\n                                 AND `ipv4_range_end` >= '{$this->ipnum}'))";
        } else {
            $where_ip .= " AND (`ipv6` IS NULL\n                             OR `ipv6` = '" . $DB->escape($this->iptxt) . "')";
        }
        $found_clients = $apiclient->find("`is_active` = '1' {$where_ip}");
        if (count($found_clients) <= 0) {
            $this->returnError(__("There isn't an active api client matching your ip adress in the configuration") . " (" . $this->iptxt . ")", "", "ERROR_NOT_ALLOWED_IP", false);
        }
        $app_tokens = array_column($found_clients, 'app_token');
        $apiclients_id = array_column($found_clients, 'id');
        $this->app_tokens = array_combine($apiclients_id, $app_tokens);
    }

Usage Example

Example #1
0
 function __construct()
 {
     /* Since parent constructors are not called implicitly if */
     /* the child class defines a constructor we simpley call it */
     /* here to inherit the parent's methods and bad habits. */
     parent::__construct();
 }
All Usage Examples Of API::__construct