public function handleRequest(array $data = array()) { $errortext = ''; $this->setEnableErrorReporting(); // log incoming request $this->log(ShopgateLogger::getInstance()->cleanParamsForLog($data), ShopgateLogger::LOGTYPE_ACCESS); // save the params $this->params = $data; // save trace_id if (isset($this->params['trace_id'])) { $this->trace_id = $this->params['trace_id']; } try { if (!in_array($this->params['action'], $this->authlessActionWhitelist)) { $this->authService->checkAuthentication(); } // set error handler to Shopgate's handler if requested if (!empty($this->params['use_errorhandler'])) { if (isset($this->params['print_stack_trace'])) { $printStackTrace = $this->params['print_stack_trace']; } set_error_handler('ShopgateErrorHandler'); $this->setEnablePrintErrorsToLog($this->config->getErrorLogPath()); } if (!empty($this->params['use_shutdown_handler'])) { register_shutdown_function('ShopgateShutdownHandler'); } // enable debugging if requested if (!empty($this->params['debug_log'])) { ShopgateLogger::getInstance()->enableDebug(); ShopgateLogger::getInstance()->keepDebugLog(!empty($this->params['keep_debug_log'])); } // enable error reporting if requested if (!empty($this->params['error_reporting'])) { error_reporting($this->params['error_reporting']); ini_set('display_errors', version_compare(PHP_VERSION, '5.2.4', '>=') ? 'stdout' : true); } // memory logging size unit setup if (!empty($this->params['memory_logging_unit'])) { ShopgateLogger::getInstance()->setMemoryAnalyserLoggingSizeUnit($this->params['memory_logging_unit']); } else { // MB by default if none is set ShopgateLogger::getInstance()->setMemoryAnalyserLoggingSizeUnit('MB'); } // check if the request is for the correct shop number or an adapter-plugin if (!$this->config->getIsShopgateAdapter() && !empty($this->params['shop_number']) && $this->params['shop_number'] != $this->config->getShopNumber()) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_UNKNOWN_SHOP_NUMBER, "{$this->params['shop_number']}"); } // check if an action to call has been passed, is known and enabled if (empty($this->params['action'])) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_NO_ACTION, 'Passed parameters: ' . var_export($this->params, true)); } // check if the action is white-listed if (!in_array($this->params['action'], $this->actionWhitelist)) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_UNKNOWN_ACTION, "{$this->params['action']}"); } // check if action is enabled in the config $configArray = $this->config->toArray(); if (empty($configArray['enable_' . $this->params['action']])) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_DISABLED_ACTION, "{$this->params['action']}"); } // response output is active by default and can be deactivated to allow actions to print a custom output $this->preventResponseOutput = false; // call the action $action = $this->camelize($this->params['action']); if (in_array($this->params['action'], $this->exportActionList)) { if (isset($this->params['memory_limit'])) { $this->plugin->setExportMemoryLimit((int) $this->params['memory_limit']); } else { $this->plugin->setExportMemoryLimit($this->config->getDefaultMemoryLimit()); } if (isset($this->params['max_execution_time'])) { $this->plugin->setExportTimeLimit((int) $this->params['max_execution_time']); } else { $this->plugin->setExportTimeLimit($this->config->getDefaultExecutionTime()); } } $this->{$action}(); } catch (ShopgateLibraryException $e) { $error = $e->getCode(); $errortext = $e->getMessage(); } catch (ShopgateMerchantApiException $e) { $error = ShopgateLibraryException::MERCHANT_API_ERROR_RECEIVED; $errortext = ShopgateLibraryException::getMessageFor(ShopgateLibraryException::MERCHANT_API_ERROR_RECEIVED) . ': "' . $e->getCode() . ' - ' . $e->getMessage() . '"'; } catch (Exception $e) { $message = "\n" . get_class($e) . "\n"; $message .= 'with code: ' . $e->getCode() . "\n"; $message .= 'and message: \'' . $e->getMessage() . "'\n"; // new ShopgateLibraryException to build proper error message and perform logging $se = new ShopgateLibraryException($message, null, false, true, $e); $error = $se->getCode(); $errortext = $se->getMessage(); } // print out the response if (!empty($error)) { if (empty($this->response)) { $this->response = new ShopgatePluginApiResponseAppJson($this->trace_id); } $this->response->markError($error, $errortext); } if (empty($this->response)) { trigger_error('No response object defined. This should _never_ happen.', E_USER_ERROR); } if (!$this->preventResponseOutput) { $this->response->setData($this->responseData); if (empty($this->params['error_reporting']) && ob_get_contents()) { ob_clean(); } $this->response->send(); } // return true or false return empty($error); }
public function handleRequest(array $data = array()) { // log incoming request $this->log(ShopgateLogger::getInstance()->cleanParamsForLog($data), ShopgateLogger::LOGTYPE_ACCESS); // save the params $this->params = $data; // save trace_id if (isset($this->params['trace_id'])) { $this->trace_id = $this->params['trace_id']; } try { $this->authService->checkAuthentification(); // set error handler to Shopgate's handler if requested if (!empty($this->params['use_errorhandler'])) { set_error_handler('ShopgateErrorHandler'); } // check if the request is for the correct shop number or an adapter-plugin if (!$this->config->getIsShopgateAdapter() && !empty($this->params['shop_number']) && $this->params['shop_number'] != $this->config->getShopNumber()) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_UNKNOWN_SHOP_NUMBER, "{$this->params['shop_number']}"); } // check if an action to call has been passed, is known and enabled if (empty($this->params['action'])) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_NO_ACTION, 'Passed parameters: ' . var_export($data, true)); } // check if the action is white-listed if (!in_array($this->params['action'], $this->actionWhitelist)) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_UNKNOWN_ACTION, "{$this->params['action']}"); } // check if action is enabled in the config $configArray = $this->config->toArray(); if (empty($configArray['enable_' . $this->params['action']])) { throw new ShopgateLibraryException(ShopgateLibraryException::PLUGIN_API_DISABLED_ACTION, "{$this->params['action']}"); } // enable debugging if requested if (!empty($data['debug_log'])) { ShopgateLogger::getInstance()->enableDebug(); ShopgateLogger::getInstance()->keepDebugLog(!empty($data['keep_debug_log'])); } // enable error handler if requested if (!empty($data['error_reporting'])) { error_reporting($data['error_reporting']); } // call the action $action = $this->camelize($this->params['action']); $this->{$action}(); } catch (ShopgateLibraryException $e) { $error = $e->getCode(); $errortext = $e->getMessage(); } catch (ShopgateMerchantApiException $e) { $error = ShopgateLibraryException::MERCHANT_API_ERROR_RECEIVED; $errortext = ShopgateLibraryException::getMessageFor(ShopgateLibraryException::MERCHANT_API_ERROR_RECEIVED) . ': "' . $e->getCode() . ' - ' . $e->getMessage() . '"'; } catch (Exception $e) { $message = "\n" . get_class($e) . "\n"; $message .= 'with code: ' . $e->getCode() . "\n"; $message .= 'and message: \'' . $e->getMessage() . "'\n"; // new ShopgateLibraryException to build proper error message and perform logging $se = new ShopgateLibraryException($message); $error = $se->getCode(); $errortext = $se->getMessage(); } // print out the response if (!empty($error)) { if (empty($this->response)) { $this->response = new ShopgatePluginApiResponseAppJson($this->trace_id); } $this->response->markError($error, $errortext); } if (empty($this->response)) { trigger_error('No response object defined. This should _never_ happen.', E_USER_ERROR); } $this->response->setData($this->responseData); $this->response->send(); // return true or false return empty($error); }