1 <?php 2 /* Copyright (c) 2013, Geert Bergman (geert@scrivo.nl) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of "Scrivo" nor the names of its contributors may be 14 * used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $Id: Context.php 841 2013-08-19 22:19:47Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\Context class. 34 */ 35 36 namespace Scrivo; 37 38 /** 39 * Class that defines a Scrivo context. 40 * 41 * Working with Scrivo data requires a number of resources to be present. Of 42 * course there should be a database connection to a Scrivo database and also 43 * an instance id that refers to the data if a scrivo instance in the database 44 * (a Scrivo database can store data for multiple instances/websites). 45 * 46 * Also a principal id is required. This principal id is used to determine the 47 * permissions for actions that are excecuted on behalf of the principal. 48 * 49 * Then there is the configuration data: the entries in the ".htscrivo" file 50 * and the object id labels. 51 * 52 * All this information is what we call the Scrivo context. Working with Scrivo 53 * data will almost always require you to setup a Scrivo context first. The 54 * parameters for the database connection and the instance id are mandatory 55 * entries in the Scrivo config file, a principal id will usually be retrieved 56 * from a session or is set progammatically. Thus configuration data and a 57 * principal id is all what is required to instantiate a Scrivo context. 58 * 59 * $context = new \Scrivo\Context( 60 * new \Scrivo\Config(), \Scrivo\User::PRIMARY_ADMIN_ID); 61 * $homepage = new \Scrivo\Page($context, $context->labels->HOME); 62 * 63 * Some other usage examples: 64 * 65 * // Get the current (user) principal from the context. 66 * $currentUser = new \Scrivo\User($context, $context->principalId); 67 * 68 * // Get a reference to the contexts PDO connection and instance id. 69 * $conn = $context->connection; 70 * $instId = $context->instanceId; 71 * 72 * // Get the value of a configuration entry 73 * $wwwRoot = $context->config->WWW_ROOT; 74 * 75 * // Get an object id by its label. 76 * $contactPageId = $context->labels->CONTACT; 77 * 78 * // Check write permission on the page labelled contact. 79 * $context->checkPermission( 80 * \Scrivo\AccessController::WRITE_ACCESS, $context->labels->CONTACT); 81 * 82 * @property-read \Scrivo\LocalCache $cache The object cache. 83 * @property-read \Scrivo\Config $config A Config object holding this site's 84 * configuration data 85 * @property-read \Scrivo\PdoConnection $connection A PDO connection to a 86 * Scrivo database. 87 * @property-read \Scrivo\IdLabel $labels A IdLabel object that holding this 88 * sites id-label pairs. 89 * @property-read \Scrivo\User $principal The principal. 90 */ 91 class Context { 92 93 /** 94 * A Config object holding this site's configuration data 95 * @var \Scrivo\Config 96 */ 97 private $config = null; 98 99 /** 100 * A PDO connection to a Scrivo database. 101 * @var \Scrivo\PdoConnection 102 */ 103 private $conn = null; 104 105 /** 106 * The principal. 107 * @var \Scrivo\User 108 */ 109 private $principal = null; 110 111 /** 112 * A IdLabel object that holding this sites id-label pairs. 113 * @var \Scrivo\IdLabel 114 */ 115 private $idLabel = null; 116 117 /** 118 * The object cache. 119 * @var \Scrivo\LocalCache 120 */ 121 private $cache = null; 122 123 /** 124 * Construct a Scrivo context using a configuration file and a user id. 125 * 126 * @param \Scrivo\Config $config A Scrivo configuration object. 127 * @param \Scrivo\User|int $user A Scrivo user. 128 */ 129 public function __construct(\Scrivo\Config $config, $user) { 130 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 131 null, 132 //array(array("\Scrivo\User", \Scrivo\ArgumentCheck::TYPE_INTEGER)) 133 null // TODO multiple type check in ArgumentCheck 134 )); 135 136 $this->config = $config; 137 $this->conn = new \Scrivo\PdoConnection($config); 138 $this->cache = new \Scrivo\LocalCache(); 139 $this->idLabel = \Scrivo\IdLabel::select($this); 140 $this->principal = $user instanceof \Scrivo\User 141 ? $user : \Scrivo\User::fetch($this, $user); 142 } 143 144 /** 145 * Implementation of the readable properties using the PHP magic 146 * method __get(). 147 * 148 * @param string $name The name of the property to get. 149 * 150 * @return mixed The value of the requested property. 151 */ 152 public function __get($name) { 153 switch($name) { 154 case "config": return $this->config; 155 case "connection": return $this->conn; 156 case "principal": return $this->principal; 157 case "labels": return $this->idLabel; 158 case "cache": return $this->cache; 159 } 160 throw new \Scrivo\SystemException("No such get-property '$name'."); 161 } 162 163 /** 164 * Check a permission on an object using this Scrivo context. 165 * 166 * @param int $perm The permission to test (READ_ACCESS || WRITE_ACCESS 167 * || PUBLISH_ACCESS) 168 * @param int $objectId A valid object id of a page or asset. 169 * 170 * @throws \Scrivo\ApplicationException if no access was granted. 171 */ 172 public function checkPermission($perm, $objectId=null) { 173 \Scrivo\ArgumentCheck::assertArgs(func_get_args(), array( 174 array(\Scrivo\ArgumentCheck::TYPE_INTEGER, array( 175 \Scrivo\AccessController::READ_ACCESS, 176 \Scrivo\AccessController::WRITE_ACCESS, 177 \Scrivo\AccessController::PUBLISH_ACCESS)), 178 array(\Scrivo\ArgumentCheck::TYPE_INTEGER) 179 ), 1); 180 181 if (!AccessController::checkPermission($this, $perm, $objectId)) { 182 throw new \Scrivo\ApplicationException("Access violation"); 183 } 184 } 185 186 /** 187 * Prepare for serialization. Context data can never be stored in caches, 188 * so prevent this by returning an empty array when the context is 189 * serialized. 190 * 191 * @return array An empty array. 192 */ 193 public function __sleep() { 194 return array(); 195 } 196 197 } 198 199 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013