Пример #1
0
 public function execute(CommandContext $context)
 {
     if (!UserStatus::isAdmin() || !Current_User::allow('hms', 'search')) {
         PHPWS_Core::initModClass('hms', 'exception/PermissionException.php');
         throw new PermissionException('You do not have permission to lookup student names!');
     }
     $student = null;
     $error = new JsonError(403);
     $username = $context->get('username');
     $banner_id = (int) $context->get('banner_id');
     try {
         if ($banner_id) {
             $student = StudentFactory::getStudentByBannerID($banner_id, Term::getSelectedTerm());
         } elseif (!empty($username)) {
             $student = StudentFactory::getStudentByUsername($username, Term::getSelectedTerm());
         } else {
             $error->setMessage('Did not receive Banner ID or user name.');
             $context->setContent(json_encode($error));
         }
         $student->gender_string = HMS_Util::formatGender($student->gender);
         $context->setContent(json_encode($student));
     } catch (\StudentNotFoundException $e) {
         $error->setMessage($e->getMessage());
         $context->setContent(json_encode($error));
     }
 }
Пример #2
0
 public function process()
 {
     // Set headers to allow Cross-origin scripting
     $rh = getallheaders();
     header('Allow: GET,HEAD,POST,PUT,DELETE,OPTIONS');
     if (array_key_exists('Origin', $rh)) {
         header('Access-Control-Allow-Origin:' . $rh['Origin']);
     }
     if (array_key_exists('Access-Control-Request-Headers', $rh)) {
         header('Access-Control-Allow-Headers:' . $rh['Access-Control-Request-Headers']);
     }
     header('Access-Control-Allow-Credentials: true');
     try {
         parent::process();
     } catch (PermissionException $e) {
         $error = new JsonError('401 Unauthorized');
         $error->setMessage('You are not authorized to perform this action.  You may need to sign back in.');
         $error->renderStatus();
         $content = $error->encode();
     } catch (Exception $e) {
         $error = new JsonError('500 Internal Server Error');
         $error->setMessage($e->getMessage());
         $error->renderStatus();
         $content = $error->encode();
         // Log the exception
         error_log('Caught API Exception: ' . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine() . ' Trace: ' . $e->getTraceAsString());
         $message = $this->formatException($e);
         $this->emailError($message);
     }
     $callback = $this->context->get('callback');
     $content = $this->context->getContent();
     // Wrap a jsonp request in it's function callback
     $response = !is_null($callback) ? "{$callback}({$content})" : $content;
     header('Content-Type: application/json; charset=utf-8');
     echo $response;
     // This sets NQ (notifications), which aren't valid for AJAX, so we aren't doing it. Just exit instead.
     //HMS::quit();
     exit;
 }
Пример #3
0
 /**
  * Encodes data as JSON.
  *
  * If a schema is passed, the value is validated against that schema before
  * encoding. The schema may be passed as file path or as object returned
  * from `JsonDecoder::decodeFile($schemaFile)`.
  *
  * You can adjust the decoding with the various setters in this class.
  *
  * @param mixed         $data   The data to encode.
  * @param string|object $schema The schema file or object.
  *
  * @return string The JSON string.
  *
  * @throws EncodingFailedException   If the data could not be encoded.
  * @throws ValidationFailedException If the data fails schema validation.
  * @throws InvalidSchemaException    If the schema is invalid.
  */
 public function encode($data, $schema = null)
 {
     if (null !== $schema) {
         $errors = $this->validator->validate($data, $schema);
         if (count($errors) > 0) {
             throw ValidationFailedException::fromErrors($errors);
         }
     }
     $options = 0;
     if (self::JSON_OBJECT === $this->arrayEncoding) {
         $options |= JSON_FORCE_OBJECT;
     }
     if (self::JSON_NUMBER === $this->numericEncoding) {
         $options |= JSON_NUMERIC_CHECK;
     }
     if ($this->gtLtEscaped) {
         $options |= JSON_HEX_TAG;
     }
     if ($this->ampersandEscaped) {
         $options |= JSON_HEX_AMP;
     }
     if ($this->singleQuoteEscaped) {
         $options |= JSON_HEX_APOS;
     }
     if ($this->doubleQuoteEscaped) {
         $options |= JSON_HEX_QUOT;
     }
     if (PHP_VERSION_ID >= 50400) {
         if (!$this->slashEscaped) {
             $options |= JSON_UNESCAPED_SLASHES;
         }
         if (!$this->unicodeEscaped) {
             $options |= JSON_UNESCAPED_UNICODE;
         }
         if ($this->prettyPrinting) {
             $options |= JSON_PRETTY_PRINT;
         }
     }
     if (PHP_VERSION_ID >= 50500) {
         $maxDepth = $this->maxDepth;
         // We subtract 1 from the max depth to make JsonDecoder and
         // JsonEncoder consistent. json_encode() and json_decode() behave
         // differently for their depth values. See the test cases for
         // examples.
         // HHVM does not have this inconsistency.
         if (!defined('HHVM_VERSION')) {
             --$maxDepth;
         }
         $encoded = json_encode($data, $options, $maxDepth);
     } else {
         $encoded = json_encode($data, $options);
     }
     if (PHP_VERSION_ID < 50400 && !$this->slashEscaped) {
         // PHP below 5.4 does not allow to turn off slash escaping. Let's
         // unescape slashes manually.
         $encoded = str_replace('\\/', '/', $encoded);
     }
     if (JSON_ERROR_NONE !== json_last_error()) {
         throw new EncodingFailedException(sprintf('The data could not be encoded as JSON: %s', JsonError::getLastErrorMessage()), json_last_error());
     }
     if ($this->terminatedWithLineFeed) {
         $encoded .= "\n";
     }
     return $encoded;
 }