| // +---------------------------------------------------------------------------+ define('SGL_RESPONSEFORMAT_JSON', 1); define('SGL_RESPONSEFORMAT_PLAIN', 2); define('SGL_RESPONSEFORMAT_JAVASCRIPT', 3); define('SGL_RESPONSEFORMAT_HTML', 4); define('SGL_RESPONSEFORMAT_XML', 5); /** * Abstract model controller for all the 'ajax provider' classes. * * @package SGL * * @author Julien Casanova * @author Dmitri Lakachauskis * * @abstract */ class SGL_AjaxProvider { /** * Holds configuration * * @var array */ var $conf = array(); /** * DB abstract layer * * @var DB resource */ var $dbh = null; /** * Constant indicating response format. * * @var integer */ var $responseFormat = SGL_RESPONSEFORMAT_HTML; /** * Constructor. * * @access public */ function SGL_AjaxProvider() { SGL::logMessage(null, PEAR_LOG_DEBUG); $c = &SGL_Config::singleton(); $this->conf = $c->getAll(); $this->dbh = $this->_getDb(); } function &_getDb() { $locator = &SGL_ServiceLocator::singleton(); $dbh = $locator->get('DB'); if (!$dbh) { $dbh = &SGL_DB::singleton(); $locator->register('DB', $dbh); } return $dbh; } /** * Main routine of processing ajax requests. * * @param SGL_Registry $input * @param SGL_Output $output * * @return mixed */ function process(&$input, &$output) { SGL::logMessage(null, PEAR_LOG_DEBUG); $req = $input->getRequest(); $actionName = $req->getActionName(); // handle errors if (SGL_Error::count()) { // eg, authentication failure return; } elseif (!method_exists($this, $actionName)) { SGL::raiseError('requested method does not exist'); return; } // by default request is authorised $ok = true; // only implement on demand $providerContainer = ucfirst($req->getModuleName()) . 'AjaxProvider'; if (!empty($this->conf[$providerContainer]['requiresAuth']) && $this->conf['debug']['authorisationEnabled']) { $aMethods = explode(',', $this->conf[$providerContainer]['requiresAuth']); $aMethods = array_map('trim', $aMethods); if (in_array($actionName, $aMethods)) { $resourseId = $this->getAuthResourceId(); $ok = $this->isOwner($resourseId, SGL_Session::getUid()); } } if (!$ok) { SGL::raiseError('authorisation failed', SGL_ERROR_INVALIDAUTHORISATION); return; } $output->data = $this->$actionName(); } /** * Authorisation routine. * * @param mixed $resourseId * @param integer $userId * * @return boolean * * @abstract */ function isOwner($resourseId, $userId) { return true; } /** * Get resource ID. * * @return mixed * * @abstract */ function getAuthResourceId() { return 'resourceId'; } function jsonEncode($data) { if (function_exists('json_encode')) { $ret = json_encode($data); } else { require_once 'HTML/AJAX/JSON.php'; $json = new HTML_AJAX_JSON(); $ret = $json->encode($data); } return $ret; } function handleError($oError) { $aResponse = array( 'message' => $oError->getMessage(), 'debugInfo' => $oError->getDebugInfo(), 'level' => $oError->getCode(), 'errorType' => SGL_Error::constantToString($oError->getCode()) ); $ret = SGL_AjaxProvider::jsonEncode($aResponse); return $ret; } } ?>