public static function handleRegistrationRequest($overrideFields = array())
 {
     if (!empty($_SESSION['User'])) {
         return static::throwError('You are already logged in. Please log out if you need to register a new account.');
     }
     if (!static::$enableRegistration) {
         return static::throwError('Sorry, self-registration is not currently available. Please contact an administrator.');
     }
     $filteredRequestFields = array_intersect_key($_REQUEST, array_flip(static::$registrationFields));
     $additionalErrors = array();
     if (is_callable(static::$createUser)) {
         $User = call_user_func_array(static::$createUser, array(&$filteredRequestFields, &$additionalErrors));
     } else {
         $className = User::getStaticDefaultClass();
         $User = new $className();
     }
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
         // save person fields
         $User->setFields(array_merge($filteredRequestFields, $overrideFields));
         if (!empty($_REQUEST['Password'])) {
             $User->setClearPassword($_REQUEST['Password']);
         }
         // additional checks
         if (empty($_REQUEST['Password']) || strlen($_REQUEST['Password']) < $User::$minPasswordLength) {
             $additionalErrors['Password'] = '******' . $User::$minPasswordLength . ' characters long.';
         } elseif (empty($_REQUEST['PasswordConfirm']) || $_REQUEST['Password'] != $_REQUEST['PasswordConfirm']) {
             $additionalErrors['PasswordConfirm'] = 'Please enter your password a second time for confirmation.';
         }
         // configurable hook
         if (is_callable(static::$applyRegistrationData)) {
             call_user_func_array(static::$applyRegistrationData, array($User, $_REQUEST, &$additionalErrors));
         }
         EventBus::fireEvent('beforeRegister', self::class, array('User' => $User, 'requestData' => $_REQUEST, 'additionalErrors' => &$additionalErrors));
         // validate
         if ($User->validate() && empty($additionalErrors)) {
             // save store
             $User->save();
             // upgrade session
             $GLOBALS['Session'] = $GLOBALS['Session']->changeClass('UserSession', array('PersonID' => $User->ID));
             // send welcome email
             Mailer::sendFromTemplate($User->EmailRecipient, 'registerComplete', array('User' => $User));
             if (is_callable(static::$onRegisterComplete)) {
                 call_user_func(static::$onRegisterComplete, $User, $_REQUEST);
             }
             EventBus::fireEvent('registerComplete', self::class, array('User' => $User, 'requestData' => $_REQUEST));
             return static::respond('registerComplete', array('success' => true, 'data' => $User));
         }
         if (count($additionalErrors)) {
             $User->addValidationErrors($additionalErrors);
         }
         // fall through back to form if validation failed
     } else {
         // apply overrides to phantom
         $User->setFields($overrideFields);
     }
     return static::respond('register', array('success' => false, 'data' => $User));
 }
 public static function handleRequest()
 {
     // initialize request object
     $request = new ApiRequest(static::getPath());
     $metrics = [];
     // fire beforeApiRequest event to configure request
     $beforeEvent = EventBus::fireEvent('beforeApiRequest', 'Gatekeeper\\ApiRequestHandler', ['request' => $request, 'metrics' => &$metrics]);
     // check that request is ready
     if (!$request->isReady()) {
         throw new Exception('ApiRequest is not ready');
     }
     // execute request against internal API
     HttpProxy::relayRequest(['autoAppend' => false, 'autoQuery' => false, 'url' => rtrim($request->getEndpoint()->InternalEndpoint, '/') . $request->getUrl(), 'interface' => static::$sourceInterface, 'passthruHeaders' => static::$passthruHeaders, 'timeout' => static::$defaultTimeout, 'timeoutConnect' => static::$defaultTimeoutConnect, 'afterResponse' => function ($responseBody, $responseHeaders, $options, $curlHandle) use($request, &$metrics, &$beforeEvent) {
         $curlInfo = curl_getinfo($curlHandle);
         list($path, $query) = explode('?', $request->getUrl());
         // initialize log record
         $Transaction = Transaction::create(['Endpoint' => $request->getEndpoint(), 'Key' => $request->getKey(), 'ClientIP' => ip2long($_SERVER['REMOTE_ADDR']), 'Method' => $_SERVER['REQUEST_METHOD'], 'Path' => $path, 'Query' => $query, 'ResponseTime' => $curlInfo['starttransfer_time'] * 1000, 'ResponseCode' => $curlInfo['http_code'], 'ResponseBytes' => $curlInfo['size_download']]);
         // fire afterApiRequest
         EventBus::fireEvent('afterApiRequest', 'Gatekeeper\\ApiRequestHandler', ['request' => $request, 'metrics' => &$metrics, 'beforeEvent' => &$beforeEvent, 'curlHandle' => $curlHandle, 'curlInfo' => $curlInfo, 'curlError' => curl_errno($curlHandle), 'Transaction' => $Transaction, 'responseCode' => $curlInfo['http_code'], 'responseHeaders' => &$responseHeaders, 'responseBody' => &$responseBody]);
     }]);
 }
 public static function fireFileEvent($path, $event, $payload = array())
 {
     if (!class_exists('Emergence\\EventBus')) {
         return;
     }
     return \Emergence\EventBus::fireEvent($event, array_merge(array('Emergence', 'FS'), $path), $payload);
 }