function getTopLevelKey($key) { if (isset($this->templateContent->{$key})) { return $this->templateContent->{$key}; } else { PKLog::fatalError('Field "' . $key . '" does not exist in pass.json', 400); } }
function validate($filePath) { $errors = array(); // Load file if (!file_exists($filePath)) { PKLog::fatalError('File "' . $filePath . '" does not exist.', 404); } $content = file_get_contents($filePath); if (empty($content)) { return $this->_error(0); } if (!($json = json_decode($content))) { switch (json_last_error()) { case JSON_ERROR_NONE: $json_error = 'Unknown JSON parsing error'; break; case JSON_ERROR_DEPTH: $json_error = 'Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: $json_error = 'Underflow or the modes mismatch'; break; case JSON_ERROR_CTRL_CHAR: $json_error = 'Unexpected control character found'; break; case JSON_ERROR_SYNTAX: $json_error = 'Syntax error, malformed JSON'; break; case JSON_ERROR_UTF8: $json_error = 'Malformed UTF-8 characters, possibly incorrectly encoded'; break; default: $json_error = 'Unknown JSON parsing error'; } return $this->_error(1, $json_error); } if (!is_object($json)) { return $this->_error(2); } // Check required Top-Level Keys if (!isset($json->passTypeIdentifier) || empty($json->passTypeIdentifier)) { $errors[] = $this->_error(3); } if (!isset($json->description) || empty($json->description)) { $errors[] = $this->_error(30); } if (!isset($json->formatVersion) || empty($json->formatVersion)) { $errors[] = $this->_error(4); } if (!isset($json->organizationName) || empty($json->organizationName)) { $errors[] = $this->_error(5); } if (!isset($json->serialNumber) || empty($json->serialNumber)) { $errors[] = $this->_error(6); } if (!isset($json->teamIdentifier) || empty($json->teamIdentifier)) { $errors[] = $this->_error(7); } // Check Top-Level Key values if ($json->formatVersion != 1) { $errors[] = $this->_error(8); } // Check for style-specific key (should be only 1) $nk = 0; $ptype = 'generic'; foreach ($this->PKTypes as $t) { if (isset($json->{$t})) { $ptype = $t; $nk++; } } if ($nk != 1) { $errors[] = $this->_error(9); } if (!is_object($json->{$ptype})) { $errors[] = $this->_error(10); } // Visual appearance keys if (isset($json->backgroundColor)) { if (!preg_match('/rgb\\(([0-9]{1,3})(,|, )([0-9]{1,3})(,|, )([0-9]{1,3})\\)/', $json->backgroundColor)) { $errors[] = $this->_error(11); } } if (isset($json->foregroundColor)) { if (!preg_match('/rgb\\(([0-9]{1,3})(,|, )([0-9]{1,3})(,|, )([0-9]{1,3})\\)/', $json->foregroundColor)) { $errors[] = $this->_error(12); } } if (isset($json->labelColor)) { if (!preg_match('/rgb\\(([0-9]{1,3})(,|, )([0-9]{1,3})(,|, )([0-9]{1,3})\\)/', $json->labelColor)) { $errors[] = $this->_error(13); } } // Relevance keys if (isset($json->locations)) { if (!is_array($json->locations)) { $errors[] = $this->_error(14); } foreach ($json->locations as $l) { if (!is_object($l)) { $errors[] = $this->_error(15); } if (!isset($l->latitude) || !isset($l->longitude)) { $errors[] = $this->_error(16); } } } if (isset($json->relevantDate)) { if (!is_string($json->relevantDate) || !strtotime($json->relevantDate)) { $errors[] = $this->_error(17); } } // Boarding Pass transit type key if ($ptype == 'boardingPass') { if (!isset($json->boardingPass->transitType)) { $errors[] = $this->_error(18); } if (!is_string($json->boardingPass->transitType)) { $errors[] = $this->_error(19); } if (!in_array($json->boardingPass->transitType, $this->PKTransitTypes)) { $errors[] = $this->_error(20); } } // Barcode key if (isset($json->barcode)) { if (!isset($json->barcode->message)) { $errors[] = $this->_error(21); } if (!isset($json->barcode->messageEncoding)) { $errors[] = $this->_error(22); } if (!isset($json->barcode->format)) { $errors[] = $this->_error(23); } if (!in_array($json->barcode->format, $this->PKBarcodeFormats)) { $errors[] = $this->_error(24); } } // Fields foreach ($this->PKFields as $ft) { if (isset($json->{$ptype}->{$ft})) { if (!is_array($json->{$ptype}->{$ft})) { $errors[] = $this->_error(25, $ft); } else { foreach ($json->{$ptype}->{$ft} as $field) { if (!is_object($field)) { $errors[] = $this->_error(26, $ft); } else { if (!isset($field->key)) { $errors[] = $this->_error(27, $ft); } else { if (!isset($field->value)) { $errors[] = $this->_error(28, $field->key, $ft); } if (isset($field->textAlignment)) { if (!in_array($field->textAlignment, $this->PKTextAlignments)) { $errors[] = $this->_error(29, $field->key, $ft); } } } } } } } } if (empty($errors)) { return true; } else { return $errors; } }