/** * create a new instance of local $rest Restler object */ protected function initRest($uri, $version, $multipart = false) { require_once $this->rootDir . "/framework/src/Maveriks/Extension/Restler/UploadFormat.php"; // $servicesDir contains directory where Services Classes are allocated $servicesDir = $this->workflowDir . 'engine' . DS . 'src' . DS . 'ProcessMaker' . DS . 'Services' . DS; // $apiDir - contains directory to scan classes and add them to Restler $apiDir = $servicesDir . 'Api' . DS; // $apiIniFile - contains file name of api ini configuration $apiIniFile = $servicesDir . DS . 'api.ini'; // $authenticationClass - contains the class name that validate the authentication for Restler $authenticationClass = 'ProcessMaker\\Services\\OAuth2\\Server'; // $pmOauthClientId - contains PM Local OAuth Id (Web Designer) $pmOauthClientId = 'x-pm-local-client'; /* * Load Api ini file for Rest Service */ $config = array(); if (file_exists($apiIniFile)) { $cachedConfig = $this->workspaceCacheDir . "api-config.php"; // verify if config cache file exists, is array and the last modification date is the same when cache was created. if (!file_exists($cachedConfig) || !is_array($config = (include $cachedConfig)) || $config["_chk"] != filemtime($apiIniFile)) { $config = Util\Common::parseIniFile($apiIniFile); $config["_chk"] = filemtime($apiIniFile); if (!is_dir(dirname($cachedConfig))) { Util\Common::mk_dir(dirname($cachedConfig)); } file_put_contents($cachedConfig, "<?php return " . var_export($config, true) . ";"); Util\Logger::log("Configuration cache was loaded and cached to: {$cachedConfig}"); } else { Util\Logger::log("Loading Api Configuration from: {$cachedConfig}"); } } // Setting current workspace to Api class Services\Api::setWorkspace(SYS_SYS); $cacheDir = defined("PATH_C") ? PATH_C : sys_get_temp_dir(); $sysConfig = \System::getSystemConfiguration(); \Luracast\Restler\Defaults::$cacheDirectory = $cacheDir; $productionMode = (bool) (!(isset($sysConfig["service_api_debug"]) && $sysConfig["service_api_debug"])); Util\Logger::log("Serving API mode: " . ($productionMode ? "production" : "development")); // create a new Restler instance //$rest = new \Luracast\Restler\Restler(); $this->rest = new \Maveriks\Extension\Restler($productionMode); // setting flag for multipart to Restler $this->rest->setFlagMultipart($multipart); // setting api version to Restler $this->rest->setAPIVersion($version); // adding $authenticationClass to Restler $this->rest->addAuthenticationClass($authenticationClass, ''); // Setting database connection source list($host, $port) = strpos(DB_HOST, ':') !== false ? explode(':', DB_HOST) : array(DB_HOST, ''); $port = empty($port) ? '' : ";port={$port}"; Services\OAuth2\Server::setDatabaseSource(DB_USER, DB_PASS, DB_ADAPTER . ":host={$host};dbname=" . DB_NAME . $port); if (DB_NAME != DB_RBAC_NAME) { //it's PM < 3 list($host, $port) = strpos(DB_RBAC_HOST, ':') !== false ? explode(':', DB_RBAC_HOST) : array(DB_RBAC_HOST, ''); $port = empty($port) ? '' : ";port={$port}"; Services\OAuth2\Server::setDatabaseSourceRBAC(DB_RBAC_USER, DB_RBAC_PASS, DB_ADAPTER . ":host={$host};dbname=" . DB_RBAC_NAME . $port); } // Setting default OAuth Client id, for local PM Web Designer Services\OAuth2\Server::setPmClientId($pmOauthClientId); $this->rest->setOverridingFormats('JsonFormat', 'UploadFormat'); // scan all api directory to find api classes $classesList = Util\Common::rglob($apiDir . "/*"); foreach ($classesList as $classFile) { if (pathinfo($classFile, PATHINFO_EXTENSION) === 'php') { $relClassPath = str_replace('.php', '', str_replace($servicesDir, '', $classFile)); $namespace = '\\ProcessMaker\\Services\\' . str_replace(DS, '\\', $relClassPath); $namespace = strpos($namespace, "//") === false ? $namespace : str_replace("//", '', $namespace); //if (! class_exists($namespace)) { require_once $classFile; //} $this->rest->addAPIClass($namespace); } } // adding aliases for Restler if (array_key_exists('alias', $config)) { foreach ($config['alias'] as $alias => $aliasData) { if (is_array($aliasData)) { foreach ($aliasData as $label => $namespace) { $namespace = '\\' . ltrim($namespace, '\\'); $this->rest->addAPIClass($namespace, $alias); } } } } // // Register API Plugins classes $isPluginRequest = strpos($uri, '/plugin-') !== false ? true : false; if ($isPluginRequest) { $tmp = explode('/', $uri); array_shift($tmp); $tmp = array_shift($tmp); $tmp = explode('-', $tmp); $pluginName = $tmp[1]; $uri = str_replace('plugin-' . $pluginName, strtolower($pluginName), $uri); } // hook to get rest api classes from plugins if (class_exists('PMPluginRegistry') && file_exists(PATH_DATA_SITE . 'plugin.singleton')) { $pluginRegistry = \PMPluginRegistry::loadSingleton(PATH_DATA_SITE . 'plugin.singleton'); $plugins = $pluginRegistry->getRegisteredRestServices(); if (!empty($plugins)) { foreach ($plugins as $pluginName => $plugin) { $pluginSourceDir = PATH_PLUGINS . $pluginName . DIRECTORY_SEPARATOR . 'src'; $loader = \Maveriks\Util\ClassLoader::getInstance(); $loader->add($pluginSourceDir); foreach ($plugin as $class) { if (class_exists($class['namespace'])) { $this->rest->addAPIClass($class['namespace'], strtolower($pluginName)); } } } } } Services\OAuth2\Server::setWorkspace(SYS_SYS); $this->rest->addAPIClass('\\ProcessMaker\\Services\\OAuth2\\Server', 'oauth2'); return $uri; }
protected function getAuthorizationCode($client) { \ProcessMaker\Services\OAuth2\Server::setDatabaseSource($this->getDsn()); \ProcessMaker\Services\OAuth2\Server::setPmClientId($client['CLIENT_ID']); $oauthServer = new \ProcessMaker\Services\OAuth2\Server(); $userId = $_SESSION['USER_LOGGED']; $authorize = true; $_GET = array_merge($_GET, array( 'response_type' => 'code', 'client_id' => $client['CLIENT_ID'], 'scope' => implode(' ', $oauthServer->getScope()) )); $response = $oauthServer->postAuthorize($authorize, $userId, true); $code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=') + 5, 40); return $code; }
public function getUserId() { return \ProcessMaker\Services\OAuth2\Server::getUserId(); }