public static function getInstance() { if (null === self::$_instance) { require EXTERNAL_LIBRARY_PATH . "/PHP-on-Couch/lib/couch.php"; require EXTERNAL_LIBRARY_PATH . "/PHP-on-Couch/lib/couchClient.php"; require EXTERNAL_LIBRARY_PATH . "/PHP-on-Couch/lib/couchDocument.php"; $registry = Zend_Registry::getInstance(); $config = $registry->get("config"); $dsn = $config['resources']['db']['couchdb']['dsn']; //start with a invalid database, always remember to set it before //retrieving / saving a object as the library being used doesn't //offer a better way to do right now $client = new couchClient($dsn, "invalid"); self::$_instance = $client; } return self::$_instance; }
/** * * Log important actions * @param array $data with at least action key * @param bool logPost whether to log POST data */ public function log(array $data, $logPost = false) { $auth = Zend_Auth::getInstance(); $couchDb = Ml_Model_CouchDb::getInstance(); if (!isset($data['action'])) { throw new Exception("Action key doesn't exists in the data array."); } if (isset($data['server']) || isset($data['raw_post']) || isset($data['uid']) || isset($data['_id']) || isset($data['_rev'])) { throw new Exception("Trying to use reserved log internal key."); } $data['server'] = filter_input_array(INPUT_SERVER, FILTER_UNSAFE_RAW); if ($logPost) { $data['raw_post'] = filter_input_array(INPUT_POST, FILTER_UNSAFE_RAW); } $data['uid'] = $auth->getIdentity(); $data['_id'] = Ml_Model_Request::getId(); $dataObject = Ml_Model_Types::arrayToObject($data); $couchDb->useDatabase("actions_log"); $couchDb->storeDoc($dataObject); }
/** * Write session data * * @param string $id * @param string $data * @return boolean */ public function write($id, $data) { $auth = Zend_Auth::getInstance(); $registry = Zend_Registry::getInstance(); $config = $registry->get("config"); //if user is identified, save additional information if ($auth->hasIdentity()) { $frontController = Zend_Controller_Front::getInstance(); //if for some reason (which might be or not right) the script //was earlier terminated before the creation of the response object //don't try to retrieve the response object if (is_object($frontController->getResponse())) { $responseCode = $frontController->getResponse()->getHttpResponseCode(); } else { $responseCode = ''; } $requestInfo = array("http_user_agent" => (string) $_SERVER['HTTP_USER_AGENT'], "request_method" => (string) $_SERVER['REQUEST_METHOD'], "remote_addr" => (string) $_SERVER['REMOTE_ADDR'], "request_time" => (int) $_SERVER['REQUEST_TIME'], "request_method" => (string) $_SERVER['REQUEST_METHOD'], "request_uri" => (string) $_SERVER['REQUEST_URI'], "http_response_code" => (string) $responseCode, "session" => (string) $id, "uid" => (string) $auth->getIdentity()); $this->_cache->save($requestInfo, $this->_lastActivityPrefix . $id, array(), $this->_getLifetime($id)); if ($config['log']['requests']) { $couchDb = Ml_Model_CouchDb::getInstance(); $requestInfo['_id'] = Ml_Model_Request::getId(); try { $couchDb->useDatabase("web_access_log"); $couchDb->storeDoc((object) $requestInfo); } catch (Exception $e) { trigger_error('Failure to store authenticated access log of user id ' . $auth->getIdentity(), E_USER_NOTICE); } } } return parent::write($id, $data); }