<commit_before><?php

/**
 * Controller designed to wrap around calls to defined services
 * 
 * To call a service, use jsonservice/servicename/methodname
 * 
 * @author <EMAIL>
 * @license BSD License http://silverstripe.org/bsd-license/
 */
class JsonServiceController extends Controller {
	
	/**
	 * List of object -> json converter classes
	 *
	 * @var array
	 */
	protected $converters = array();
	
	public function init() {
		parent::init();
		$this->converters['DataObject'] = new DataObjectJsonConverter();
		$this->converters['DataObjectSet'] = new DataObjectSetJsonConverter();
	}

	public function handleRequest(SS_HTTPRequest $request) {
		try {
			if (!Member::currentUserID()) {
				$token = $request->requestVar('token');
				if (!$token) {
					throw new WebServiceException(403, "Missing token parameter");
				}
				$user = singleton('TokenAuthenticator')->authenticate($token);
				if (!$user) {
					throw new WebServiceException(403, "Invalid user token");
				}
			} else {
				// we check the SecurityID parameter
				$securityID = $request->requestVar('SecurityID');
				if ($securityID != SecurityToken::inst()->getValue()) {
					throw new WebServiceException(403, "Invalid security ID");
				}
			}
			return parent::handleRequest($request);
		} catch (WebServiceException $exception) {
			$this->response = new SS_HTTPResponse();
			$this->response->setStatusCode($exception->status);
			$this->response->setBody($this->ajaxResponse($exception->getMessage(), $exception->status));
		} catch (SS_HTTPResponse_Exception $e) {
			$this->response = $e->getResponse();
			$this->response->setBody($this->ajaxResponse($e->getMessage(), $e->getCode()));
		} catch (Exception $exception) {
			$this->response = new SS_HTTPResponse();
			$this->response->setStatusCode(500);
			$this->response->setBody($this->ajaxResponse($exception->getMessage(), 500));
		}

		return $this->response;
	}

	public function index() {
		$service = ucfirst($this->request->param('Service')) . 'Service';
		$method = $this->request->param('Method');

		$svc = singleton($service);

		if ($svc && $svc instanceof JsonServiceable || method_exists($svc, 'webEnabledMethods')) {
			$allowedMethods = array();
			if (method_exists($svc, 'webEnabledMethods')) {
				$allowedMethods = $svc->webEnabledMethods();
			}

			$refObj = new ReflectionObject($svc);
			$refMeth = $refObj->getMethod($method);
			/* @var $refMeth ReflectionMethod */
			if ($refMeth) {
				$refParams = $refMeth->getParameters();
				$params = array();
				$allArgs = $this->request->requestVars();
				foreach ($refParams as $refParm) {
					/* @var $refParm ReflectionParameter */
					if (isset($allArgs[$refParm->getName()])) {
						$params[$refParm->getName()] = $allArgs[$refParm->getName()];<commit_msg>Check allwed methods before calling<commit_after><?php

/**
 * Controller designed to wrap around calls to defined services
 * 
 * To call a service, use jsonservice/servicename/methodname
 * 
 * @author <EMAIL>
 * @license BSD License http://silverstripe.org/bsd-license/
 */
class JsonServiceController extends Controller {
	
	/**
	 * List of object -> json converter classes
	 *
	 * @var array
	 */
	protected $converters = array();

	public function init() {
		parent::init();
		$this->converters['DataObject'] = new DataObjectJsonConverter();
		$this->converters['DataObjectSet'] = new DataObjectSetJsonConverter();
	}

	public function handleRequest(SS_HTTPRequest $request) {
		try {
			if (!Member::currentUserID()) {
				$token = $request->requestVar('token');
				if (!$token) {
					throw new WebServiceException(403, "Missing token parameter");
				}
				$user = singleton('TokenAuthenticator')->authenticate($token);
				if (!$user) {
					throw new WebServiceException(403, "Invalid user token");
				}
			} else {
				// we check the SecurityID parameter
				$securityID = $request->requestVar('SecurityID');
				if ($securityID != SecurityToken::inst()->getValue()) {
					throw new WebServiceException(403, "Invalid security ID");
				}
			}
			return parent::handleRequest($request);
		} catch (WebServiceException $exception) {
			$this->response = new SS_HTTPResponse();
			$this->response->setStatusCode($exception->status);
			$this->response->setBody($this->ajaxResponse($exception->getMessage(), $exception->status));
		} catch (SS_HTTPResponse_Exception $e) {
			$this->response = $e->getResponse();
			$this->response->setBody($this->ajaxResponse($e->getMessage(), $e->getCode()));
		} catch (Exception $exception) {
			$this->response = new SS_HTTPResponse();
			$this->response->setStatusCode(500);
			$this->response->setBody($this->ajaxResponse($exception->getMessage(), 500));
		}

		return $this->response;
	}

	public function index() {
		$service = ucfirst($this->request->param('Service')) . 'Service';
		$method = $this->request->param('Method');

		$svc = singleton($service);

		if ($svc && ($svc instanceof JsonServiceable || method_exists($svc, 'webEnabledMethods'))) {
			$allowedMethods = array();
			if (method_exists($svc, 'webEnabledMethods')) {
				$allowedMethods = $svc->webEnabledMethods();
			}
			
			// if we have a list of methods, lets use those to restrict
			if (count($allowedMethods) && !in_array($method, $allowedMethods)) {
				throw new WebServiceException(403, "You do not have permission to $method");
			}

			$refObj = new ReflectionObject($svc);
			$refMeth = $refObj->getMethod($method);
			/* @var $refMeth ReflectionMethod */
			if ($refMeth) {
				$refParams = $refMeth->getParameters();
				$params = array();
				$allArgs = $this->request->requestVars();
				foreach ($refParams as $refParm) {
					/* @var $refParm ReflectionParameter */
					if (isset($allArgs[$refParm->getName()])) {
						$params[$refParm->getName()] = $allArgs[$refParm->getName()];
{'fraction_non_alphanumeric': 0.16792972786772303, 'fraction_numerical': 0.005683775404753703, 'mean_word_length': 4.371877890841813, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1357329', 'n_tokens_mistral': 2016, 'n_tokens_neox': 1716, 'n_words': 371}