<?php

/**
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS
 * @copyright Copyright (c) 2013 - 2017 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

/**
 * cfApiController Class
 *
 * The API controller class.
 *
 * @package clearFusionCMS
 */
class cfApiController extends flController {
	/**
	 * Run API functions.
	 */
	function runAction() {
		// Look up the site that we're running
		if(!($r = clearFusionCMS::getDb()->fetchRow('SELECT shortname,displayname,redirectto,locale,twitterusername,puressl FROM cf_sites WHERE domain=?', $this->request->getServerName())))
			throw new flExceptionNotFound();

		// If redirected site
		if(!empty($r['redirectto']))
			throw new flExceptionNotFound();

		// Save the site information
		clearFusionCMS::setSite($r['shortname'], $r['displayname'], $r['twitterusername'], $r['puressl']);
		fusionLib::setLocale($r['locale']);

		$server = new flApiServer(clearFusionCMS::getDb());

		// Register known endpoints
		$server->registerServiceClass('user', 'flUserServerApi');

		// Run
		$server->processRequest($this->request, $this->response);
	}
}
