/** * @param Hal $hal * @param array $links */ private static function addJsonLinkData($hal, $links) { foreach ($links as $rel => $links) { if (!isset($links[0]) or !is_array($links[0])) { $links = array($links); } foreach ($links as $link) { $href = $link['href']; unset($link['href']); $hal->addLink($rel, $href, $link); } } }
/** * Singleton pattern. Get the instance of the latest created object or create a new one. * @return Hal The instance of this class. */ public static function Instance() { if (self::$instance == null) { self::$instance = new Hal(); } return self::$instance; }
/** * render * * @param Hal $resource * @param bool $pretty * @return string */ public function render(Hal $resource, $pretty) { $doc = new \SimpleXMLElement('<resource></resource>'); $doc->addAttribute('href', $resource->getUri()); $this->linksForXml($doc, $resource->getLinks()); $this->arrayToXml($resource->getData(), $doc); foreach ($resource->getResources() as $rel => $resources) { $this->resourcesForXml($doc, $rel, $resources); } $dom = dom_import_simplexml($doc); if ($pretty) { $dom->ownerDocument->preserveWhiteSpace = false; $dom->ownerDocument->formatOutput = true; } return $dom->ownerDocument->saveXML(); }
/** * Decode a application/hal+xml document into a Nocarrier\Hal object. * * @param Hal $hal * @param $data * @param int $depth * * @throws \RuntimeException * @static * @access public * @return \Nocarrier\Hal */ public static function fromXml(Hal $hal, $data, $depth = 0) { if (!$data instanceof \SimpleXMLElement) { try { $data = new \SimpleXMLElement($data); } catch (\Exception $e) { throw new \RuntimeException('The $data parameter must be valid XML'); } } $children = $data->children(); $links = clone $children->link; unset($children->link); $embedded = clone $children->resource; unset($children->resource); $hal->setUri((string) $data->attributes()->href); $hal->setData((array) $children); foreach ($links as $links) { if (!is_array($links)) { $links = array($links); } foreach ($links as $link) { list($rel, $href, $attributes) = self::extractKnownData($link); $hal->addLink($rel, $href, $attributes); } } if ($depth > 0) { foreach ($embedded as $embed) { list($rel, $href, $attributes) = self::extractKnownData($embed); $hal->addResource($rel, self::fromXml($embed, $depth - 1)); } } $hal->setShouldStripAttributes(false); return $hal; }
/** * Authenticate and login a user. */ public function Login() { $form = new FormUserLogin($this); if ($form->Check() === false) { $this->AddMessage('error', 'You must fill in acronym and password.'); $this->RedirectToController('login'); } $this->views->SetTitle('Login')->AddInclude(HAL_INSTALL_PATH . '/view/userlogin.tpl.php', array('login_form' => $form, 'allow_create_user' => Hal::Instance()->config['create_new_users'], 'create_user_url' => $this->CreateUrl(null, 'create'))); }
/** * Constructor * * @param string name of the element. * @param array attributes to set to the element. Default is an empty array. */ public function __construct($name, $attributes = array()) { $this->attributes = $attributes; $this['name'] = $name; if (is_callable('Hal::Instance()')) { $this->characterEncoding = Hal::Instance()->config['character_encoding']; } else { $this->characterEncoding = 'UTF-8'; } }
/** * Constructor, can be instantiated by sending in the $hal reference. */ protected function __construct($hal = null) { if (!$hal) { $hal = Hal::Instance(); } $this->hal =& $hal; $this->config =& $hal->config; $this->request =& $hal->request; $this->data =& $hal->data; $this->db =& $hal->db; $this->views =& $hal->views; $this->session =& $hal->session; $this->user =& $hal->user; }
/** * Return an array (compatible with the hal+json format) representing the * complete response * * @param Hal $resource * @return array */ protected function arrayForJson(Hal $resource) { $data = $resource->getData(); $data['_links'] = $this->linksForJson($resource->getUri(), $resource->getLinks()); foreach ($resource->getResources() as $rel => $resources) { $data['_embedded'][$rel] = $this->resourcesForJson($resources); } return $data; }
<?php // // PHASE: BOOTSTRAP // define('HAL_INSTALL_PATH', dirname(__FILE__)); define('HAL_SITE_PATH', HAL_INSTALL_PATH . '/application'); require HAL_INSTALL_PATH . '/bootstrap.php'; $hal = Hal::Instance(); // // PHASE: FRONTCONTROLLER ROUTE // $hal->FrontControllerRoute(); // // PHASE: THEME ENGINE RENDER // $hal->ThemeEngineRender();
/** * Decode a application/hal+xml document into a Nocarrier\Hal object. * * @param string $text * @static * @access public * @return \Nocarrier\Hal */ public static function fromXml($text) { $data = new \SimpleXMLElement($text); $children = $data->children(); $links = clone $children->link; unset($children->link); $embedded = clone $children->resource; unset($children->resource); $hal = new Hal($data->attributes()->href, (array) $children); foreach ($links as $links) { if (!is_array($links)) { $links = array($links); } foreach ($links as $link) { $attributes = (array) $link->attributes(); $attributes = $attributes['@attributes']; $rel = $attributes['rel']; $href = $attributes['href']; unset($attributes['rel'], $attributes['href']); $hal->addLink($rel, $href, $attributes); } } return $hal; }
/** * Create password. * * @param $plain string the password plain text to use as base. * @param $algorithm string stating what algorithm to use, plain, md5, md5salt, sha1, sha1salt. * defaults to the settings of site/config.php. * @returns array with 'salt' and 'password'. */ public function CreatePassword($plain, $algorithm = null) { $password = array('algorithm' => $algorithm ? $algoritm : Hal::Instance()->config['hashing_algorithm'], 'salt' => null); switch ($password['algorithm']) { case 'sha1salt': $password['salt'] = sha1(microtime()); $password['password'] = sha1($password['salt'] . $plain); break; case 'md5salt': $password['salt'] = md5(microtime()); $password['password'] = md5($password['salt'] . $plain); break; case 'sha1': $password['password'] = sha1($plain); break; case 'md5': $password['password'] = md5($plain); break; case 'plain': $password['password'] = $plain; break; default: throw new Exception('Unknown hashing algorithm'); } return $password; }
/** * Return an array (compatible with the hal+json format) representing the * complete response. * * @param \Nocarrier\Hal $resource * @return array */ protected function arrayForJson(Hal $resource = null) { if ($resource == null) { return array(); } $data = $resource->getData(); if ($resource->getShouldStripAttributes()) { $data = $this->stripAttributeMarker($data); } $links = $this->linksForJson($resource->getUri(), $resource->getLinks(), $resource->getArrayLinkRels()); if (count($links)) { $data['_links'] = $links; } foreach ($resource->getRawResources() as $rel => $resources) { if (count($resources) === 1 && !in_array($rel, $resource->getArrayResourceRels())) { $resources = $resources[0]; } $data['_embedded'][$rel] = $this->resourcesForJson($resources); } return $data; }
/** * Check if region has views. Accepts variable amount of arguments as regions. * * @param $region string the region to draw the content in. */ function region_has_content($region = 'default') { return Hal::Instance()->views->RegionHasView(func_get_args()); }
/** * Callback to delete the content. */ public function DoDelete($form, $content) { $content['id'] = $form['id']['value']; $content->Delete(); Hal::Instance()->RedirectTo('content'); }
/** * Helper, wrap html_entites with correct character encoding */ function htmlEnt($str, $flags = ENT_COMPAT) { return htmlentities($str, $flags, Hal::Instance()->config['character_encoding']); }
/** * Return an array (compatible with the hal+json format) representing the * complete response. * * @param \Nocarrier\Hal $resource * @return array */ protected function arrayForJson(Hal $resource = null) { if ($resource == null) { return array(); } $data = $resource->getData(); $data = $this->stripAttributeMarker($data); $links = $this->linksForJson($resource->getUri(), $resource->getLinks()); if (count($links)) { $data['_links'] = $links; } foreach ($resource->getResources() as $rel => $resources) { $data['_embedded'][$rel] = $this->resourcesForJson($resources); } return $data; }