| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file contains the PageWidget class |
| 4 |
* |
| 5 |
* $Id: PageWidget.inc,v 1.5 2003/09/30 00:20:01 hemna Exp $ |
| 6 |
* |
| 7 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
| 8 |
* @package phpHtmlLib |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* Make sure we have the required parent class |
| 13 |
*/ |
| 14 |
require_once($phphtmllib."/widgets/HTMLPageClass.inc"); |
| 15 |
|
| 16 |
/** |
| 17 |
* We need to make sure we have the MessageBoxWidget loaded |
| 18 |
* for the permissions checking. |
| 19 |
* |
| 20 |
* NOTE: The InfoTable css classes must be included in any |
| 21 |
* page that uses the permissions checking. |
| 22 |
*/ |
| 23 |
require_once($phphtmllib."/widgets/MessageBoxWidget.inc"); |
| 24 |
|
| 25 |
/** |
| 26 |
* This class is used to build content |
| 27 |
* for an entire page. It uses the |
| 28 |
* HTMLPageClass widget from phphtmllib |
| 29 |
* to render the final output. |
| 30 |
* |
| 31 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
| 32 |
* @package phpHtmlLib |
| 33 |
*/ |
| 34 |
class PageWidget extends HTMLPageClass { |
| 35 |
|
| 36 |
/** CLASS VARS **/ |
| 37 |
|
| 38 |
/** |
| 39 |
* This enables the ability to view the |
| 40 |
* source of a page bu setting debug=1 |
| 41 |
* in the query string. |
| 42 |
* |
| 43 |
*/ |
| 44 |
var $_enable_debug = FALSE; |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* holds the page title text for |
| 49 |
* a page |
| 50 |
* |
| 51 |
*/ |
| 52 |
var $_title_text = NULL; |
| 53 |
|
| 54 |
/** |
| 55 |
* Does the user have permission |
| 56 |
* to build and view the content? |
| 57 |
*/ |
| 58 |
var $_perm_options = array("allowed" => TRUE, |
| 59 |
"message" => "You are not allowed to view this page.", |
| 60 |
"url" => NULL); |
| 61 |
|
| 62 |
/** |
| 63 |
* the message box for displaying |
| 64 |
* permissions errors |
| 65 |
*/ |
| 66 |
var $_permission_box = NULL; |
| 67 |
|
| 68 |
/** |
| 69 |
* This is to enable/disable the |
| 70 |
* permissions checking. |
| 71 |
* By default it is off. |
| 72 |
*/ |
| 73 |
var $_allow_permissions_checks = FALSE; |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* The width of the permissions dialog |
| 78 |
* table. |
| 79 |
*/ |
| 80 |
var $_permissions_error_width = "50%"; |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Constructor: |
| 85 |
* |
| 86 |
* @param mixed - $title Title string or TITLEtag object for the page. |
| 87 |
* @param string - one of 3 types of html to render. Setting this will |
| 88 |
* make the object declare the gobal define which tells |
| 89 |
* all of the tag objects what type of html tags to render. |
| 90 |
* some tags support special features. such as the <IMG> |
| 91 |
* tag. If xhtml is selected, the the IMGtag object and all |
| 92 |
* utility functions will not render "border=0" as a default |
| 93 |
* attribute, since this is not proper xhtml. |
| 94 |
* "html" - HTML 4.0 (default) |
| 95 |
* "xhtml_transitional" - render xhtml instead of html |
| 96 |
* - doctype is XHTML transitional. |
| 97 |
* "xhtml_strict" - render xhtml instead of html 4.0. |
| 98 |
* - doctype is XHTML strict. |
| 99 |
* @param int - one of 2 types. INDENT_NICE or INDENT_LEFT_JUSTIFY |
| 100 |
* This tells the page how to render the indenting of the |
| 101 |
* output. By default it is set to INDENT_NICE, which nicely |
| 102 |
* indents each nested tag. You can have all tags rendered |
| 103 |
* left justified (smaller size in output) by using |
| 104 |
* INDENT_LEFT_JUSTIFY |
| 105 |
* |
| 106 |
*/ |
| 107 |
function PageWidget( $title, $render_type = HTML, $indent_style=INDENT_NICE ) { |
| 108 |
|
| 109 |
if ( defined("DEBUG") && |
| 110 |
(isset($_GET["debug"]) || |
| 111 |
isset($_POST["debug"]) ) ) { |
| 112 |
$this->enable_debug( TRUE ); |
| 113 |
} |
| 114 |
//save the title for later. |
| 115 |
$this->_title_text = $title; |
| 116 |
|
| 117 |
//call the parent's constructor |
| 118 |
$this->HTMLPageClass( $title, $render_type, $indent_style ); |
| 119 |
|
| 120 |
//see if we are allowed to check permissions |
| 121 |
//You need to have the CSS definitions for the |
| 122 |
//InfoTable defined in order to see the error |
| 123 |
//table correctly. Or you can override the |
| 124 |
//_build_permission_box() method to provide |
| 125 |
//a different object for displaying the |
| 126 |
//permission errors. |
| 127 |
if ($this->_allow_permissions_checks) { |
| 128 |
//check the permissions on this page |
| 129 |
$this->_check_permissions(); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* gets the current title of the page. |
| 135 |
* |
| 136 |
* @return string |
| 137 |
*/ |
| 138 |
function get_title() { |
| 139 |
return $this->_title_text; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* This function is used to build |
| 144 |
* addition head content that isn't |
| 145 |
* built by the HTMLPageClass parent |
| 146 |
* class by default. |
| 147 |
* NOTE: you can add addition content |
| 148 |
* to the head in 1 of 2 ways. |
| 149 |
* 1) inside the call return the |
| 150 |
* addition content in the |
| 151 |
* return $foo; |
| 152 |
* 2) or use the HTMLPageClass' |
| 153 |
* $this->add_head_content() |
| 154 |
* from within the head_content() |
| 155 |
* call. |
| 156 |
* |
| 157 |
* @return mixed. |
| 158 |
*/ |
| 159 |
function head_content() { |
| 160 |
return NULL; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
/** |
| 165 |
* This function is meant to be overridden |
| 166 |
* by the child class. |
| 167 |
* This provides all of the content |
| 168 |
* for the page. |
| 169 |
* NOTE: You add the content to the |
| 170 |
* body in 1 of 2 ways. |
| 171 |
* 1) return the content from this |
| 172 |
* call. |
| 173 |
* 2) inside the call, you can |
| 174 |
* just call $this->add(); |
| 175 |
* and then return NULL; |
| 176 |
* |
| 177 |
* @return mixed. |
| 178 |
*/ |
| 179 |
function body_content() { |
| 180 |
return NULL; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
/** |
| 185 |
* This function is used to save |
| 186 |
* a frameset to the page. This will |
| 187 |
* automatically output a properly |
| 188 |
* formatted |
| 189 |
*/ |
| 190 |
function frameset() { |
| 191 |
return NULL; |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* This function is called to build |
| 196 |
* any JavaScript that is needed in the |
| 197 |
* <HEAD> portion of a document. |
| 198 |
* |
| 199 |
* @return string - the raw JS code to be |
| 200 |
* put inside the <head> |
| 201 |
*/ |
| 202 |
function head_javascript() { |
| 203 |
return NULL; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
/** |
| 208 |
* This sets the debug option for |
| 209 |
* the HTMLPageClass |
| 210 |
* |
| 211 |
* @param boolean TRUE for on, FALSE for off |
| 212 |
*/ |
| 213 |
function enable_debug( $flag = TRUE ) { |
| 214 |
$this->_enable_debug = $flag; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
/** |
| 219 |
* This is the function that renders the HTML |
| 220 |
* for this widget. |
| 221 |
* |
| 222 |
* @return string - the HTML |
| 223 |
*/ |
| 224 |
function render() { |
| 225 |
|
| 226 |
//test to see if they want debugging of |
| 227 |
//output enabled. |
| 228 |
if ( $this->_enable_debug ) { |
| 229 |
if ( isset($_GET["debug"]) ) { |
| 230 |
$this->set_text_debug( TRUE ); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
//see if we have permissions to build the |
| 235 |
//content for this page. If permissions checks |
| 236 |
//aren't allowed, this will pass. |
| 237 |
if ($this->_has_permission()) { |
| 238 |
//check to see if they want to render a frameset |
| 239 |
$frameset = $this->frameset(); |
| 240 |
if ( $frameset != NULL ) { |
| 241 |
//add and set the page output |
| 242 |
//to be a frameset. |
| 243 |
$this->set_frameset( $frameset ); |
| 244 |
} else { |
| 245 |
|
| 246 |
//Try and get some more head content |
| 247 |
$content = ""; |
| 248 |
$content = $this->head_content(); |
| 249 |
if ( $content != "" ) { |
| 250 |
$this->add_head_content( $content ); |
| 251 |
} |
| 252 |
|
| 253 |
//try and get the body content. |
| 254 |
//the user could just have added |
| 255 |
//the data manually. |
| 256 |
$content = ""; |
| 257 |
$content = $this->body_content(); |
| 258 |
if ( $content != "" ) { |
| 259 |
$this->add( $content ); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
//Lets see if they have any javascript |
| 264 |
//for the head |
| 265 |
$js = $this->head_javascript(); |
| 266 |
if ( $js != NULL ) { |
| 267 |
$this->add_head_js( $js ); |
| 268 |
} |
| 269 |
} else { |
| 270 |
$this->add( html_br(), $this->_permission_box); |
| 271 |
} |
| 272 |
|
| 273 |
return HTMLPageClass::render(); |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
/** |
| 278 |
* This method is used to enable or disable the |
| 279 |
* built in permissions checking mechanism. |
| 280 |
* |
| 281 |
* @param boolean TRUE = enable permissions checks |
| 282 |
*/ |
| 283 |
function allow_permissions_checks($flag=true) { |
| 284 |
$this->_allow_permissions_checks = $flag; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
/** |
| 289 |
* This method allows all PageWidget children to |
| 290 |
* do any kind of permissions checking before |
| 291 |
* any content methods are called. |
| 292 |
* This allows for a very secure method of building |
| 293 |
* and rendering the page content. |
| 294 |
* |
| 295 |
*/ |
| 296 |
function _check_permissions() { |
| 297 |
//call the child permission() method |
| 298 |
if ( !$this->permission() ) { |
| 299 |
//looks like the user doesn't have |
| 300 |
//permissions to view this page |
| 301 |
$this->_set_perms( FALSE ); |
| 302 |
|
| 303 |
$this->_build_permission_box(); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* This is meant to be extended by the child class |
| 309 |
* to do any generic permissions checking for access |
| 310 |
* to the content that the child builds |
| 311 |
* |
| 312 |
* @return boolean - TRUE = has permissions to build |
| 313 |
* and view content. |
| 314 |
*/ |
| 315 |
function permission() { |
| 316 |
//by default return true. |
| 317 |
return true; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* set the value of the permissions |
| 322 |
* |
| 323 |
* @param boolean - TRUE = has permission |
| 324 |
*/ |
| 325 |
function _set_perms( $flag ) { |
| 326 |
$this->_perm_options["allowed"] = $flag; |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* do we have permissions to build/view the content? |
| 331 |
* |
| 332 |
* @return boolean |
| 333 |
*/ |
| 334 |
function _has_permission() { |
| 335 |
if (!$this->_allow_permissions_checks) { |
| 336 |
//permissions checks aren't allowed |
| 337 |
//so just return true. |
| 338 |
return TRUE; |
| 339 |
} else { |
| 340 |
return $this->_perm_options["allowed"]; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* This is used to set the various options for displaying |
| 346 |
* the failed permissions box. This should be called |
| 347 |
* prior to returning false in the permissions() method |
| 348 |
* |
| 349 |
* @param string - the permissions message |
| 350 |
* NOTE: NULL message means use the default. |
| 351 |
* @param string - the url where to go to. |
| 352 |
* NOTE: if NULL, then there will be no |
| 353 |
* button shown |
| 354 |
*/ |
| 355 |
function set_permissions_message($message=NULL, $url=NULL) { |
| 356 |
if ( $message != NULL ) { |
| 357 |
$this->_perm_options["message"] = $message; |
| 358 |
} |
| 359 |
$this->_perm_options["url"] = $url; |
| 360 |
} |
| 361 |
|
| 362 |
/** |
| 363 |
* This is the method used to build the |
| 364 |
* object to display the permissions error. |
| 365 |
* |
| 366 |
* By default it uses either the MessageBoxWidget |
| 367 |
* or the MessageBoxOK widget which both rely on |
| 368 |
* having the InfoTable object's css included in the page. |
| 369 |
* |
| 370 |
* @return none |
| 371 |
*/ |
| 372 |
function _build_permission_box() { |
| 373 |
if ( $this->_perm_options["url"] == NULL ) { |
| 374 |
$this->_permission_box = new MessageBoxWidget("Error", $this->_permissions_error_width, |
| 375 |
$this->_perm_options["message"]); |
| 376 |
} else { |
| 377 |
$this->_permission_box = new MessageBoxOK("Error", $this->_permissions_error_width, |
| 378 |
$this->_perm_options["message"], |
| 379 |
$this->_perm_options["url"]); |
| 380 |
} |
| 381 |
} |
| 382 |
} |
| 383 |
?> |