Esempio n. 1
0
 public function run()
 {
     /* Get our system config */
     $systemConfig = new ApiConfig();
     /* Create a new Slim app */
     $slimApp = $this->createSlim($systemConfig->get('debugMode'));
     /* Format the API Response as JSON */
     //$slimApp->add(new \API\JsonResponseView());
     /* Add API Routes */
     $this->addRoutes($slimApp, $systemConfig->get('debugMode'));
     /* Start Slim */
     $slimApp->run();
 }
Esempio n. 2
0
 public function actionOpenApiConfig()
 {
     header('Content-type: application/json');
     if (!Yii::app()->request->isPostRequest) {
         IjoyPlusServiceUtils::exportServiceError(Constants::METHOD_NOT_SUPPORT);
         return;
     }
     if (!IjoyPlusServiceUtils::validateAPPKey()) {
         IjoyPlusServiceUtils::exportServiceError(Constants::APP_KEY_INVALID);
         return;
     }
     try {
         $device_name = Yii::app()->request->getParam("device_name");
         if (!(isset($device_name) && !is_null($device_name) && strlen($device_name) > 0)) {
             IjoyPlusServiceUtils::exportServiceError(Constants::PARAM_IS_INVALID);
             return;
         }
         $apiConfig = ApiConfig::model()->find(array("condition" => "device_name like '%" . $device_name . ",%' "));
         if ($apiConfig == null) {
             IjoyPlusServiceUtils::exportEntity(array());
         } else {
             IjoyPlusServiceUtils::exportEntity(array('api_url' => $apiConfig['api_url'], 'logo_url' => $apiConfig['logo_url'], 'app_key' => $apiConfig['app_key']));
         }
     } catch (Exception $e) {
         IjoyPlusServiceUtils::exportServiceError(Constants::SYSTEM_ERROR);
     }
 }
Esempio n. 3
0
 private static function connect()
 {
     // If a PDO instance does not already exist
     if (!self::$pdo) {
         // Get the system configuration file
         $config = new ApiConfig();
         $c = $config->get();
         // Create a set of PDO options
         $options = array(\PDO::ATTR_PERSISTENT => true, \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION);
         //
         $invocation = $c['dbHost'] ? "host={$c['dbHost']}" : "unix_socket={$c['dbUnixSocket']}";
         try {
             self::$pdo = new \PDO("mysql:{$invocation};dbname={$c['db']}", $c["dbUser"], $c["dbPass"], $options);
         } catch (\PDOException $e) {
             self::logError("Could not connect to the DB \"mysql:{$invocation};dbname={$c['db']}\" from 'api.dbconn'.");
             // If we cant connect to the database die
             die('Could not connect to the database:<br/>' . $e);
         }
     }
     // Return the cached instance of PDO
     return self::$pdo;
 }
 function __construct()
 {
     $config = new ApiConfig();
     $this->config = $config->get();
     $this->checkDirectories();
 }
Esempio n. 5
0
 /**
  * API请求处理开始
  *
  * @access public
  * @param 无
  * @return void
  */
 public function execute()
 {
     Logger::debug('********************START***********************');
     Logger::debug('Headers: ', $this->getAllRequestHeaderValues());
     try {
         // 初始化请求处理
         $this->initial();
         $path_info = $this->getPathInfo();
         // API白名单检查
         if (!in_array($path_info, array_keys(ApiConfig::getApiList()))) {
             throw new NotFoundException('The "PATH_INFO" is not defined.');
         }
         // 执行过滤器
         $config = FiltersConfig::$API_FILTER_CONFIG;
         if (isset($config[$path_info])) {
             $list = $config[$path_info];
             Logger::debug(' ' . count($list) . ' filters are defined.');
             foreach ($list as $filter_name) {
                 // 过滤器初始化
                 $filter = new $filter_name($this);
                 Logger::debug('Executing filter "' . $filter_name . '".');
                 // 执行过滤器
                 $filter->execute($this);
             }
         }
         Logger::debug('Executing action "' . $this->getActionName() . '#' . $this->getMethodName() . '".');
         // 连接后关闭
         Database::destroy();
         // 过滤结束,执行对应的方法
         $view = $this->startRequest();
         if (!$view instanceof BaseView) {
             throw new Exception('"' . $this->getActionName() . '#' . $this->getMethodName() . '" returned invalid value.');
         }
         Logger::debug('********************FINISH***********************');
         // 输出结果
         $view->display();
     } catch (NotFoundException $ne) {
         // 404回应
         header("HTTP/1.1 404 Not Found");
     } catch (ForbiddenException $fe) {
         // 403回应
         header("HTTP/1.1 403 Forbidden");
     } catch (ModelException $me) {
         //在Model层异常(DB相关的处理)
         Logger::error($me->getMessage() . "\n" . $me->getTraceAsString());
         // E-mail通知
         HandlerManager::exceptionReport($me);
         // 服务器错误
         header('HTTP/1.1 500 Internal Server Error');
     } catch (PDOException $pe) {
         Logger::error($pe->getMessage() . "\n" . $pe->getTraceAsString());
         // E-mail通知
         HandlerManager::exceptionReport($pe);
         // 服务器错误
         header('HTTP/1.1 500 Internal Server Error');
     } catch (Exception $e) {
         Logger::error($e->getMessage() . "\n" . $e->getTraceAsString());
         // E-mail通知
         HandlerManager::exceptionReport($e);
         // 服务器错误
         header('HTTP/1.1 500 Internal Server Error');
     }
 }