Thread: [Cs-content-commits] SF.net SVN: cs-content:[329] trunk/1.0
PHP Templating & Includes System
Brought to you by:
crazedsanity
From: <cra...@us...> - 2009-01-29 19:39:59
|
Revision: 329 http://cs-content.svn.sourceforge.net/cs-content/?rev=329&view=rev Author: crazedsanity Date: 2009-01-29 19:39:55 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Add an abstract parent class for all database types. NOTE::: this was tested on the pgsql version (in a roundabout way), but not for mysql or sqlite. /cs_phpDB.php: * MAIN::: -- requires the new abstract parent class. /abstract/cs_phpDB.abstract.class.php [NEW]: * holds a list of methods required to be a proper database layer, along with a few classes and such to help minimize code replication. /db_types/cs_phpDB__mysql.class.php: * MAIN::: -- now extends cs_phpDBAbstract{}. * __construct(): -- calls parent::__construct(). -- removed unnecessary code (now in cs_phpDBAbstract{}) * sanity_check() [DELETED]: -- moved into cs_phpDBAbstract{} * disconnect() [DELETED]: -- moved into cs_phpDBAbstract{} * currRow() [DELETED]: -- moved into cs_phpDBAbstract{} * querySafe() [DELETED]: -- moved into cs_phpDBAbstract{} * sqlSafe() [DELETED]: -- moved into cs_phpDBAbstract{} * get_transaction_status(): -- arbitrarily returns false (previously was using PostgreSQL functions, so any return it might have given would have been invalid at best). * start_copy() [DELETED]: -- removed; it was a PostgreSQL-specific implementation. * put_line() [DELETED]: -- removed; it was a PostgreSQL-specific implementation. * end_copy() [DELETED]: -- removed; it was a PostgreSQL-specific implementation. * get_transaction_level() [DELETED]: -- removed; it was a PostgreSQL-specific implementation. * is_in_transaction(): -- arbitrarily returns 0, as there are no transaction implementations. /db_types/cs_phpDB__pgsql.class.php: * MAIN::: -- slightly modified header comments so Eclipse can shrink the whole thing. -- extends cs_phpDBAbstract{} * __construct(): -- calls parent::__construct() -- removed unnecessary code (now in cs_phpDBAbstract{}) * sanity_check() [DELETED]: -- moved into cs_phpDBAbstract{} * disconnect() [DELETED]: -- moved into cs_phpDBAbstract{} * currRow() [DELETED]: -- moved into cs_phpDBAbstract{} * querySafe() [DELETED]: -- moved into cs_phpDBAbstract{} * sqlSafe() [DELETED]: -- moved into cs_phpDBAbstract{} /db_types/cs_phpDB__sqlite.class.php: * MAIN::: -- now extends cs_phpDBAbstract{}. * __construct(): -- calls parent::__construct(). -- removed unnecessary code (now in cs_phpDBAbstract{}) * sanity_check() [DELETED]: -- moved into cs_phpDBAbstract{} * disconnect() [DELETED]: -- moved into cs_phpDBAbstract{} * querySafe() [DELETED]: -- moved into cs_phpDBAbstract{} * sqlSafe() [DELETED]: -- moved into cs_phpDBAbstract{} Modified Paths: -------------- trunk/1.0/cs_phpDB.php trunk/1.0/db_types/cs_phpDB__mysql.class.php trunk/1.0/db_types/cs_phpDB__pgsql.class.php trunk/1.0/db_types/cs_phpDB__sqlite.class.php Added Paths: ----------- trunk/1.0/abstract/ trunk/1.0/abstract/cs_phpDB.abstract.class.php Added: trunk/1.0/abstract/cs_phpDB.abstract.class.php =================================================================== --- trunk/1.0/abstract/cs_phpDB.abstract.class.php (rev 0) +++ trunk/1.0/abstract/cs_phpDB.abstract.class.php 2009-01-29 19:39:55 UTC (rev 329) @@ -0,0 +1,166 @@ +<?php +/* + * Created on Jan 29, 2009 + * + * FILE INFORMATION: + * + * $HeadURL$ + * $Id$ + * $LastChangedDate$ + * $LastChangedBy$ + * $LastChangedRevision$ + */ + +abstract class cs_phpDBAbstract { + + /** Internal result set pointer. */ + protected $result = NULL; + + /** Internal error code. */ + protected $errorCode = 0; + + /** Status of the current transaction. */ + protected $transStatus = NULL; + + /** Whether there is a transaction in progress or not. */ + protected $inTrans = FALSE; + + /** Holds the last query performed. */ + protected $lastQuery = NULL; + + /** List of queries that have been run */ + protected $queryList=array(); + + /** How many seconds to wait for a query before cancelling it. */ + protected $timeOutSeconds = NULL; + + /** Internal check to determine if a connection has been established. */ + protected $isConnected=FALSE; + + /** Internal check to determine if the parameters have been set. */ + protected $paramsAreSet=FALSE; + + /** Resource handle. */ + protected $connectionID = -1; + + /** Hostname or IP to connect to */ + protected $host; + + /** Port to connect to (default for Postgres is 5432) */ + protected $port; + + /** Name of the database */ + protected $dbname; + + /** Username to connect to the database */ + protected $user; + + /** password to connect to the database */ + protected $password; + + /** Row counter for looping through records */ + protected $row = -1; + + /** cs_globalFunctions object, for string stuff. */ + protected $gfObj; + + /** Internal check to ensure the object has been properly created. */ + protected $isInitialized=FALSE; + + /** List of prepared statements, indexed off the name, with the sub-array being fieldname=>dataType. */ + protected $preparedStatements = array(); + + /** Set to TRUE to save all queries into an array. */ + protected $useQueryList=FALSE; + + /** array that essentially remembers how many times beginTrans() was called. */ + protected $transactionTree = NULL; + + + + //Define some abstract methods so they MUST be provided in order for things to work. + abstract public function set_db_info(array $params); + abstract public function close(); + abstract public function connect(array $dbParams=NULL, $forceNewConnection=FALSE); + abstract public function exec($query); + abstract public function errorMsg($setMessage=null, $logError=null); + abstract public function fobject(); + abstract public function farray(); + abstract public function farray_fieldnames($index=null, $numbered=null,$unsetIndex=1); + abstract public function farray_nvp($name, $value); + abstract public function farray_numbered(); + abstract public function numAffected(); + abstract public function numRows(); + abstract public function is_connected(); + + + //========================================================================= + public function __construct() { + $this->gfObj = new cs_globalFunctions; + $this->isInitialized = true; + }//end __construct() + //========================================================================= + + + + //========================================================================= + /** + * Make sure the object is sane. + */ + final protected function sanity_check() { + if($this->isInitialized !== TRUE) { + throw new exception(__METHOD__ .": not properly initialized"); + } + }//end sanity_check() + //========================================================================= + + + + //========================================================================= + /** + * Disconnect from the database (calls internal "close()" method). + */ + public function disconnect() { + return($this->close()); + }//end disconnect() + //========================================================================= + + + + //========================================================================= + public function affectedRows() { + return($this->numAffected()); + }//end affectedRows() + //========================================================================= + + + + //========================================================================= + public function currRow() { + return($this->row); + }//end currRow() + //========================================================================= + + + + //========================================================================= + public function querySafe($string) { + return($this->gfObj->cleanString($string,"query")); + }//end querySafe() + //========================================================================= + + + + //========================================================================= + /** + * Make it SQL safe. + */ + public function sqlSafe($string) { + return($this->gfObj->cleanString($string,"sql")); + }//end sqlSafe() + //========================================================================= + + + +} +?> \ No newline at end of file Modified: trunk/1.0/cs_phpDB.php =================================================================== --- trunk/1.0/cs_phpDB.php 2009-01-27 16:25:08 UTC (rev 328) +++ trunk/1.0/cs_phpDB.php 2009-01-29 19:39:55 UTC (rev 329) @@ -25,6 +25,7 @@ /////////////////////// require_once(dirname(__FILE__) ."/../cs-versionparse/cs_version.abstract.class.php"); +require_once(dirname(__FILE__) ."/abstract/cs_phpDB.abstract.class.php"); class cs_phpDB extends cs_versionAbstract { Modified: trunk/1.0/db_types/cs_phpDB__mysql.class.php =================================================================== --- trunk/1.0/db_types/cs_phpDB__mysql.class.php 2009-01-27 16:25:08 UTC (rev 328) +++ trunk/1.0/db_types/cs_phpDB__mysql.class.php 2009-01-29 19:39:55 UTC (rev 329) @@ -11,9 +11,8 @@ */ +class cs_phpDB__mysql extends cs_phpDBAbstract { -class cs_phpDB__mysql { - /** Internal result set pointer. */ protected $result = NULL; @@ -84,13 +83,7 @@ //========================================================================= public function __construct() { - $this->gfObj = new cs_globalFunctions; - - if(defined('DEBUGPRINTOPT')) { - $this->gfObj->debugPrintOpt = DEBUGPRINTOPT; - } - - $this->isInitialized = TRUE; + parent::__construct(); }//end __construct() //========================================================================= @@ -98,19 +91,6 @@ //========================================================================= /** - * Make sure the object is sane. - */ - final protected function sanity_check() { - if($this->isInitialized !== TRUE) { - throw new exception(__METHOD__ .": not properly initialized"); - } - }//end sanity_check() - //========================================================================= - - - - //========================================================================= - /** * Set appropriate parameters for database connection */ public function set_db_info(array $params){ @@ -143,18 +123,6 @@ //========================================================================= /** - * Wrapper for close() - */ - function disconnect() { - //Disconnect from $database - return($this->close()); - }//end disconnect() - //========================================================================= - - - - //========================================================================= - /** * Standard method to close connection. */ function close() { @@ -669,23 +637,11 @@ function affectedRows(){ return($this->numAffected()); }//end affectedRows() - //========================================================================= //========================================================================= /** - * Returns the current row number. - */ - function currRow(){ - return($this->row); - }//end currRow() - //========================================================================= - - - - //========================================================================= - /** * Get the number of fields in a result. */ // get the number of fields in a result @@ -750,28 +706,6 @@ //========================================================================= /** - * Gets rid of evil characters that might lead ot SQL injection attacks. - */ - function querySafe($string) { - return($this->gfObj->cleanString($string,"query")); - }//end querySafe() - //========================================================================= - - - - //========================================================================= - /** - * Make it SQL safe. - */ - function sqlSafe($string) { - return($this->gfObj->cleanString($string,"sql")); - }//end sqlSafe() - //========================================================================= - - - - //========================================================================= - /** * Gives textual explanation of the current status of our database * connection. * @@ -785,61 +719,7 @@ */ function get_transaction_status($goodOrBad=TRUE) { //TODO: implement MySQL version.. - $myStatus = pg_transaction_status($this->connectionID); - $text = 'unknown'; - switch($myStatus) { - case PGSQL_TRANSACTION_IDLE: { - //No query in progress: it's idle. - $goodOrBadValue = 1; - $text = 'idle'; - $this->inTrans = FALSE; - } - break; - - - case PGSQL_TRANSACTION_ACTIVE: { - //there's a command in progress. - $goodOrBadValue = 2; - $text = 'processing'; - } - break; - - - case PGSQL_TRANSACTION_INTRANS: { - //connection idle within a valid transaction block. - $goodOrBadValue = 1; - $text = 'valid transaction'; - $this->inTrans = TRUE; - } - break; - - - case PGSQL_TRANSACTION_INERROR: { - //connection idle within a broken transaction. - $goodOrBadValue = 0; - $text = 'failed transaction'; - $this->inTrans = TRUE; - } - break; - - - case PGSQL_TRANSACTION_UNKNOWN: - default: { - //the connection is bad. - $goodOrBadValue = -1; - $text = 'bad connection'; - } - break; - } - - //do they want text or the good/bad number? - $retval = $text; - $this->transactionStatus = $goodOrBadValue; - if($goodOrBad) { - //they want the number. - $retval = $goodOrBadValue; - } - + $retval = false; return($retval); }//end get_transaction_status() //========================================================================= @@ -861,105 +741,11 @@ //========================================================================= /** - * Starts a copy command. - * - * TODO: implement safeguards so they can only put a line until the copy is ended. - */ - public function start_copy($tableName, array $fields) { - //TODO: implement MySQL version.. - $retval = FALSE; - $copyStmt = "COPY ". $tableName ." (". $this->gfObj->string_from_array($fields, NULL, ", ") . ") FROM stdin;"; - $this->exec($copyStmt); - if(!strlen($this->errorMsg())) { - //TODO: set something here so that NOTHING ELSE can be done except put_line() and end_copy(). - $this->copyInProgress = TRUE; - $retval = TRUE; - } - else { - $this->end_copy(); - $retval = FALSE; - } - - return($retval); - }//end start_copy() - //========================================================================= - - - - //========================================================================= - /** - * Used to send a line to the COPY in progress (only if it was initiated by - * the internal start_copy() method). - * - * NOTE: the "end-of-copy" line, '\.', should NEVER be sent here. - */ - public function put_line($line) { - //TODO: implement MySQL version.. - $retval = FALSE; - if($this->copyInProgress === TRUE) { - $myLine = trim($line); - $myLine .= "\n"; - - $retval = pg_put_line($this->connectionID, $myLine); - } - else { - throw new exception(__METHOD__ .": cannot send line if no copy is in progress"); - } - - return($retval); - }//end put_line() - //========================================================================= - - - - //========================================================================= - public function end_copy() { - if($this->copyInProgress === TRUE) { - //send the end-of-copy line... - $this->put_line("\\.\n"); - } - - //TODO: implement MySQL version.. - $retval = pg_end_copy($this->connectionID); - - return($retval); - }//end end_copy() - //========================================================================= - - - - //========================================================================= - /** - * Determines how many times a transaction has been started. Starting - * multiple transactions does NOT protect the outer transaction from - * problems that occur in the inner transaction. In fact, it does the - * opposite: it protects the code from committing too early (which might - * destroy something that depending on the transaction). - */ - public function get_transaction_level() { - if(is_array($this->transactionTree)) { - $retval = count($this->transactionTree); - } - else { - $retval = 0; - } - - return($retval); - }//end get_transaction_level() - //========================================================================= - - - - //========================================================================= - /** * Simple way to determine if the current connection is inside a * transaction or not. */ public function is_in_transaction() { - $retval = 0; - if($this->inTrans || $this->get_transaction_level() != 0) { - $retval = TRUE; - } + $retval=0; return($retval); }//end is_in_transaction() //========================================================================= Modified: trunk/1.0/db_types/cs_phpDB__pgsql.class.php =================================================================== --- trunk/1.0/db_types/cs_phpDB__pgsql.class.php 2009-01-27 16:25:08 UTC (rev 328) +++ trunk/1.0/db_types/cs_phpDB__pgsql.class.php 2009-01-29 19:39:55 UTC (rev 329) @@ -8,29 +8,29 @@ * Last Committted Date: $Date$ * Last Committed Path:: $HeadURL$ * + * ////////////////////// + * ORIGINATION INFO: + * Author: Trevin Chow (with contributions from Lee Pang, wle...@ho...) + * Email: t1...@ma... + * Date: February 21, 2000 + * Last Updated: August 14, 2001 + * + * Description: + * Abstracts both the php function calls and the server information to POSTGRES + * databases. Utilizes class variables to maintain connection information such + * as number of rows, result id of last operation, etc. + * + * ///////////////////// + * + * TODO: option to not use layered transactions + * TODO: rollbackTrans() in layered transaction causes abort when final layer is committed/aborted + * TODO: stop sending queries to backend when transction is bad/aborted. + * TODO: commit/abort specific layer requests (i.e. if there's 8 layers & the first is named "x", calling commitTrans("x") will cause the whole transaction to commit & all layers to be destroyed. */ -/////////////////////// -// ORIGINATION INFO: -// Author: Trevin Chow (with contributions from Lee Pang, wle...@ho...) -// Email: t1...@ma... -// Date: February 21, 2000 -// Last Updated: August 14, 2001 -// -// Description: -// Abstracts both the php function calls and the server information to POSTGRES -// databases. Utilizes class variables to maintain connection information such -// as number of rows, result id of last operation, etc. -// -/////////////////////// -//TODO: option to not use layered transactions -//TODO: rollbackTrans() in layered transaction causes abort when final layer is committed/aborted -//TODO: stop sending queries to backend when transction is bad/aborted. -//TODO: commit/abort specific layer requests (i.e. if there's 8 layers & the first is named "x", calling commitTrans("x") will cause the whole transaction to commit & all layers to be destroyed. +class cs_phpDB__pgsql extends cs_phpDBAbstract { -class cs_phpDB__pgsql { - /** Internal result set pointer. */ protected $result = NULL; @@ -101,13 +101,7 @@ //========================================================================= public function __construct() { - $this->gfObj = new cs_globalFunctions; - - if(defined('DEBUGPRINTOPT')) { - $this->gfObj->debugPrintOpt = DEBUGPRINTOPT; - } - - $this->isInitialized = TRUE; + parent::__construct(); }//end __construct() //========================================================================= @@ -115,19 +109,6 @@ //========================================================================= /** - * Make sure the object is sane. - */ - final protected function sanity_check() { - if($this->isInitialized !== TRUE) { - throw new exception(__METHOD__ .": not properly initialized"); - } - }//end sanity_check() - //========================================================================= - - - - //========================================================================= - /** * Set appropriate parameters for database connection */ public function set_db_info(array $params){ @@ -160,18 +141,6 @@ //========================================================================= /** - * Wrapper for close() - */ - function disconnect() { - //Disconnect from $database - return($this->close()); - }//end disconnect() - //========================================================================= - - - - //========================================================================= - /** * Standard method to close connection. */ function close() { @@ -702,17 +671,6 @@ //========================================================================= /** - * Returns the current row number. - */ - function currRow(){ - return($this->row); - }//end currRow() - //========================================================================= - - - - //========================================================================= - /** * Get the number of fields in a result. */ // get the number of fields in a result @@ -882,28 +840,6 @@ //========================================================================= /** - * Gets rid of evil characters that might lead ot SQL injection attacks. - */ - function querySafe($string) { - return($this->gfObj->cleanString($string,"query")); - }//end querySafe() - //========================================================================= - - - - //========================================================================= - /** - * Make it SQL safe. - */ - function sqlSafe($string) { - return($this->gfObj->cleanString($string,"sql")); - }//end sqlSafe() - //========================================================================= - - - - //========================================================================= - /** * Gives textual explanation of the current status of our database * connection. * Modified: trunk/1.0/db_types/cs_phpDB__sqlite.class.php =================================================================== --- trunk/1.0/db_types/cs_phpDB__sqlite.class.php 2009-01-27 16:25:08 UTC (rev 328) +++ trunk/1.0/db_types/cs_phpDB__sqlite.class.php 2009-01-29 19:39:55 UTC (rev 329) @@ -11,7 +11,7 @@ */ -class cs_phpDB__sqlite { +class cs_phpDB__sqlite extends cs_phpDBAbstract { /** Internal result set pointer. */ protected $result = NULL; @@ -71,13 +71,7 @@ //========================================================================= public function __construct() { - $this->gfObj = new cs_globalFunctions; - - if(defined('DEBUGPRINTOPT')) { - $this->gfObj->debugPrintOpt = DEBUGPRINTOPT; - } - - $this->isInitialized = TRUE; + parent::__construct(); }//end __construct() //========================================================================= @@ -85,19 +79,6 @@ //========================================================================= /** - * Make sure the object is sane. - */ - final protected function sanity_check() { - if($this->isInitialized !== TRUE) { - throw new exception(__METHOD__ .": not properly initialized"); - } - }//end sanity_check() - //========================================================================= - - - - //========================================================================= - /** * Set appropriate parameters for database connection */ public function set_db_info(array $params){ @@ -130,18 +111,6 @@ //========================================================================= /** - * Wrapper for close() - */ - function disconnect() { - //Disconnect from $database - return($this->close()); - }//end disconnect() - //========================================================================= - - - - //========================================================================= - /** * Standard method to close connection. */ function close() { @@ -599,28 +568,6 @@ //========================================================================= - /** - * Gets rid of evil characters that might lead ot SQL injection attacks. - */ - function querySafe($string) { - return($this->gfObj->cleanString($string,"query")); - }//end querySafe() - //========================================================================= - - - - //========================================================================= - /** - * Make it SQL safe. - */ - function sqlSafe($string) { - return($this->gfObj->cleanString($string,"sql")); - }//end sqlSafe() - //========================================================================= - - - - //========================================================================= public function is_connected() { $retval = FALSE; if(is_resource($this->connectionID) && $this->isConnected === TRUE) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-01-29 19:44:03
|
Revision: 330 http://cs-content.svn.sourceforge.net/cs-content/?rev=330&view=rev Author: crazedsanity Date: 2009-01-29 19:44:01 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Updated SVN Keywords. Property Changed: ---------------- trunk/1.0/abstract/cs_phpDB.abstract.class.php trunk/1.0/db_types/cs_phpDB__mysql.class.php trunk/1.0/db_types/cs_phpDB__pgsql.class.php trunk/1.0/db_types/cs_phpDB__sqlite.class.php Property changes on: trunk/1.0/abstract/cs_phpDB.abstract.class.php ___________________________________________________________________ Added: svn:keywords + HeadURL Id LastChangedDate LastChangedBy LastChangedRevision Date Revision Author Property changes on: trunk/1.0/db_types/cs_phpDB__mysql.class.php ___________________________________________________________________ Modified: svn:keywords - Id HeadURL Date Revision Author + HeadURL Id LastChangedDate LastChangedBy LastChangedRevision Date Revision Author Property changes on: trunk/1.0/db_types/cs_phpDB__pgsql.class.php ___________________________________________________________________ Modified: svn:keywords - Id HeadURL Date Revision Author + HeadURL Id LastChangedDate LastChangedBy LastChangedRevision Date Revision Author Property changes on: trunk/1.0/db_types/cs_phpDB__sqlite.class.php ___________________________________________________________________ Modified: svn:keywords - Id HeadURL Date Revision Author + HeadURL Id LastChangedDate LastChangedBy LastChangedRevision Date Revision Author This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-01-29 20:18:43
|
Revision: 331 http://cs-content.svn.sourceforge.net/cs-content/?rev=331&view=rev Author: crazedsanity Date: 2009-01-29 20:18:41 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Implementation of cs_contentAbstract{}. NOTE::: to avoid segfaults, cs_globalFunctions does NOT extend cs_contentAbstract. /contentSystemClass.php: * MAIN::: -- requires /abstract/cs_content.abstract.class.php -- extends cs_contentAbstract * __construct(): -- calls parent::__construct() -- removed things involving version & cs_globalFunctions (in parent constructor). /cs_bbCodeParser.class.php: * MAIN::: -- requires /abstract/cs_content.abstract.class.php -- extends cs_contentAbstract * __construct(): -- calls parent::__construct() -- removed things involving version & cs_globalFunctions (in parent constructor). /cs_fileSystemClass.php: * MAIN::: -- requires /abstract/cs_content.abstract.class.php -- extends cs_contentAbstract * __construct(): -- calls parent::__construct() -- removed things involving version & cs_globalFunctions (in parent constructor). * cdup(): -- replaced reference of "this->gf" to "this->gfObj" * cd(): -- replaced reference of "this->gf" to "this->gfObj" * filename2absolute(): -- replaced reference of "this->gf" to "this->gfObj" * rename(): -- replaced reference of "this->gf" to "this->gfObj" * resolve_path_with_dots(): -- replaced reference of "this->gf" to "this->gfObj" * check_chroot(): -- replaced reference of "this->gf" to "this->gfObj" * movefile(): -- replaced reference of "this->gf" to "this->gfObj" /cs_genericPageClass.php: * MAIN::: -- requires /abstract/cs_content.abstract.class.php -- extends cs_contentAbstract * __construct(): -- calls parent::__construct() -- removed things involving version & cs_globalFunctions (in parent constructor). /cs_phpDB.php: * MAIN::: -- requires /abstract/cs_content.abstract.class.php -- extends cs_contentAbstract * __construct(): -- calls parent::__construct() -- removed things involving version & cs_globalFunctions (in parent constructor). /cs_sessionClass.php: * MAIN::: -- requires /abstract/cs_content.abstract.class.php -- extends cs_contentAbstract * __construct(): -- calls parent::__construct() -- removed things involving version & cs_globalFunctions (in parent constructor). /cs_siteConfig.class.php: * MAIN::: -- requires /abstract/cs_content.abstract.class.php -- extends cs_contentAbstract * __construct(): -- calls parent::__construct() -- removed things involving version & cs_globalFunctions (in parent constructor). * parse_config(): -- replaced reference of "this->gf" to "this->gfObj" /cs_tabsClass.php: * MAIN::: -- requires /abstract/cs_content.abstract.class.php -- extends cs_contentAbstract * __construct(): -- calls parent::__construct() -- removed things involving version & cs_globalFunctions (in parent constructor). /abstract/cs_content.abstract.class.php [NEW]: * class that holds all duplicated functionality across libraries. * NOTE::: passing boolean false to the constructor avoids including or creating the cs_globalFunctions{} object. Modified Paths: -------------- trunk/1.0/contentSystemClass.php trunk/1.0/cs_bbCodeParser.class.php trunk/1.0/cs_fileSystemClass.php trunk/1.0/cs_genericPageClass.php trunk/1.0/cs_phpDB.php trunk/1.0/cs_sessionClass.php trunk/1.0/cs_siteConfig.class.php trunk/1.0/cs_tabsClass.php Added Paths: ----------- trunk/1.0/abstract/cs_content.abstract.class.php Added: trunk/1.0/abstract/cs_content.abstract.class.php =================================================================== --- trunk/1.0/abstract/cs_content.abstract.class.php (rev 0) +++ trunk/1.0/abstract/cs_content.abstract.class.php 2009-01-29 20:18:41 UTC (rev 331) @@ -0,0 +1,36 @@ +<?php +/* + * Created on Jan 29, 2009 + * + * FILE INFORMATION: + * + * $HeadURL$ + * $Id$ + * $LastChangedDate$ + * $LastChangedBy$ + * $LastChangedRevision$ + */ + +require_once(dirname(__FILE__) ."/../../cs-versionparse/cs_version.abstract.class.php"); + + +abstract class cs_contentAbstract extends cs_versionAbstract { + + //------------------------------------------------------------------------- + function __construct($makeGfObj=true) { + $this->set_version_file_location(dirname(__FILE__) . '/../VERSION'); + $this->get_version(); + $this->get_project(); + + if($makeGfObj === true) { + //make a cs_globalFunctions{} object. + require_once(dirname(__FILE__) ."/../cs_globalFunctions.php"); + $this->gfObj = new cs_globalFunctions(); + } + }//end __construct() + //------------------------------------------------------------------------- + + + +} +?> \ No newline at end of file Property changes on: trunk/1.0/abstract/cs_content.abstract.class.php ___________________________________________________________________ Added: svn:keywords + HeadURL Id LastChangedDate LastChangedBy LastChangedRevision Date Revision Author Modified: trunk/1.0/contentSystemClass.php =================================================================== --- trunk/1.0/contentSystemClass.php 2009-01-29 19:44:01 UTC (rev 330) +++ trunk/1.0/contentSystemClass.php 2009-01-29 20:18:41 UTC (rev 331) @@ -70,14 +70,13 @@ $GLOBALS['SITE_ROOT'] = str_replace("/public_html", "", $GLOBALS['SITE_ROOT']); } -require_once(dirname(__FILE__) ."/cs_globalFunctions.php"); +require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); require_once(dirname(__FILE__) ."/cs_fileSystemClass.php"); require_once(dirname(__FILE__) ."/cs_sessionClass.php"); require_once(dirname(__FILE__) ."/cs_genericPageClass.php"); require_once(dirname(__FILE__) ."/cs_tabsClass.php"); -require_once(dirname(__FILE__) ."/../cs-versionparse/cs_version.abstract.class.php"); -class contentSystem extends cs_versionAbstract { +class contentSystem extends cs_contentAbstract { protected $baseDir = NULL; //base directory for templates & includes. protected $section = NULL; //section string, derived from the URL. @@ -109,11 +108,7 @@ $this->isTest = TRUE; } else { - $this->set_version_file_location(dirname(__FILE__) . '/VERSION'); - $this->get_version(); - $this->get_project(); - //make a cs_globalFunctions{} object. - $this->gfObj = new cs_globalFunctions(); + parent::__construct(); //setup the section stuff... $repArr = array($_SERVER['SCRIPT_NAME'], "/"); Modified: trunk/1.0/cs_bbCodeParser.class.php =================================================================== --- trunk/1.0/cs_bbCodeParser.class.php 2009-01-29 19:44:01 UTC (rev 330) +++ trunk/1.0/cs_bbCodeParser.class.php 2009-01-29 20:18:41 UTC (rev 331) @@ -18,9 +18,9 @@ * been converted. */ -require_once(dirname(__FILE__) ."/../cs-versionparse/cs_version.abstract.class.php"); +require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); -class cs_bbCodeParser extends cs_versionAbstract { +class cs_bbCodeParser extends cs_contentAbstract { /** Array containing all the codes & how to parse them. */ private $bbCodeData = NULL; @@ -30,7 +30,7 @@ * Setup internal structures. */ function __construct() { - $this->set_version_file_location(dirname(__FILE__) . '/VERSION'); + parent::__construct(false); # Which BBCode is accepted here $this->bbCodeData = array( 'bold' => array( Modified: trunk/1.0/cs_fileSystemClass.php =================================================================== --- trunk/1.0/cs_fileSystemClass.php 2009-01-29 19:44:01 UTC (rev 330) +++ trunk/1.0/cs_fileSystemClass.php 2009-01-29 20:18:41 UTC (rev 331) @@ -9,10 +9,9 @@ * $LastChangedRevision$ */ -require_once(dirname(__FILE__) ."/cs_globalFunctions.php"); -require_once(dirname(__FILE__) ."/../cs-versionparse/cs_version.abstract.class.php"); +require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); -class cs_fileSystemClass extends cs_versionAbstract { +class cs_fileSystemClass extends cs_contentAbstract { public $root; //actual root directory. public $cwd; //current directory; relative to $this->root @@ -20,7 +19,6 @@ public $dh; //directory handle. public $fh; //file handle. public $filename; //filename currently being used. - public $gf; //cs_globalFunctions{} object. public $lineNum = NULL; @@ -29,7 +27,6 @@ * The constructor. */ public function __construct($rootDir=NULL, $cwd=NULL, $initialMode=NULL) { - $this->set_version_file_location(dirname(__FILE__) . '/VERSION'); //set the root directory that we'll be using; this is considered just like "/" in // linux. Directories above it are considered non-existent. if(($rootDir) AND (is_dir($rootDir))) { @@ -43,7 +40,8 @@ exit("UNUSEABLE ROOT: $rootDir"); } - $this->gf = new cs_globalFunctions(); + parent::__construct(); + $this->root = $this->resolve_path_with_dots($this->root); //set the CURRENT working directory... this should be a RELATIVE path to $this->root. @@ -84,8 +82,8 @@ } $myParts = explode('/', $myCwd); array_pop($myParts); - $myCwd = $this->gf->string_from_array($myParts, NULL, '/'); - $realCwd = $this->gf->create_list($this->root, $myCwd, '/'); + $myCwd = $this->gfObj->string_from_array($myParts, NULL, '/'); + $realCwd = $this->gfObj->create_list($this->root, $myCwd, '/'); if(file_exists($realCwd)) { $retval = TRUE; $this->realcwd = $realCwd; @@ -119,7 +117,7 @@ $retval = 1; } elseif(is_dir($this->realcwd .'/'. $newDir)) { //relative path... - $this->cwd = $this->gf->create_list($this->cwd, $newDir, '/'); + $this->cwd = $this->gfObj->create_list($this->cwd, $newDir, '/'); $this->realcwd .= '/'. $newDir; $retval = 1; } else { @@ -426,7 +424,7 @@ } if(!$this->check_chroot($retval, FALSE)) { - $this->gf->debug_print(func_get_args()); + $this->gfObj->debug_print(func_get_args()); throw new exception(__METHOD__ .": file is outside of allowed directory (". $retval .")"); } @@ -602,7 +600,7 @@ */ public function rename($currentFilename, $newFilename) { if($newFilename == $currentFilename) { - $this->gf->debug_print(func_get_args()); + $this->gfObj->debug_print(func_get_args()); throw new exception(__METHOD__ .": renaming file to same name"); } @@ -612,7 +610,7 @@ if($this->compare_open_filename($newFilename)) { //renaming a different file to our currently open file... - $this->gf->debug_print(func_get_args()); + $this->gfObj->debug_print(func_get_args()); throw new exception(__METHOD__ .": renaming another file (". $currentFilename .") to the currently open filename (". $newFilename .")"); } else { @@ -805,7 +803,7 @@ } } - $retval = $this->gf->string_from_array($finalPieces, NULL, '/'); + $retval = $this->gfObj->string_from_array($finalPieces, NULL, '/'); if($isAbsolute) { $retval = '/'. $retval; } @@ -844,10 +842,10 @@ $pathDir = $pathPieces[$index]; if($pathDir != $dirName) { $retval = FALSE; - $this->gf->debug_print(__METHOD__ .": failed... tmp=(". $tmp ."), dirName=(". $dirName .")"); + $this->gfObj->debug_print(__METHOD__ .": failed... tmp=(". $tmp ."), dirName=(". $dirName .")"); break; } - $tmp = $this->gf->create_list($tmp, $dirName, '/'); + $tmp = $this->gfObj->create_list($tmp, $dirName, '/'); } return($retval); @@ -884,7 +882,7 @@ $retval = rename($filename, $destination); } else { - $this->gf->debug_print(__METHOD__ .":: ". $this->check_chroot($destination),1); + $this->gfObj->debug_print(__METHOD__ .":: ". $this->check_chroot($destination),1); throw new exception(__METHOD__ .':: destination is not in the directory path (from=['. $filename .'], to=['. $destination .']'); } } Modified: trunk/1.0/cs_genericPageClass.php =================================================================== --- trunk/1.0/cs_genericPageClass.php 2009-01-29 19:44:01 UTC (rev 330) +++ trunk/1.0/cs_genericPageClass.php 2009-01-29 20:18:41 UTC (rev 331) @@ -8,9 +8,9 @@ * $LastChangedRevision$ */ require_once(dirname(__FILE__) ."/template.inc"); -require_once(dirname(__FILE__) ."/../cs-versionparse/cs_version.abstract.class.php"); +require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); -class cs_genericPage extends cs_versionAbstract { +class cs_genericPage extends cs_contentAbstract { var $templateObj; //template object to parse the pages var $templateVars = array(); //our copy of the global templateVars var $mainTemplate; //the default layout of the site @@ -32,13 +32,14 @@ //handle some configuration. $this->allowRedirect = $allowRedirect; + //initialize stuff from our parent... + parent::__construct(); + //initialize some internal stuff. - $this->set_version_file_location(dirname(__FILE__) . '/VERSION'); $this->initialize_locals($mainTemplateFile); //if they need to be logged-in... $this->check_login($restrictedAccess); - $this->gfObj = new cs_globalFunctions; if(!defined('CS-CONTENT_SESSION_NAME')) { define("CS-CONTENT_SESSION_NAME", ini_get('session.name')); Modified: trunk/1.0/cs_phpDB.php =================================================================== --- trunk/1.0/cs_phpDB.php 2009-01-29 19:44:01 UTC (rev 330) +++ trunk/1.0/cs_phpDB.php 2009-01-29 20:18:41 UTC (rev 331) @@ -24,17 +24,16 @@ // /////////////////////// -require_once(dirname(__FILE__) ."/../cs-versionparse/cs_version.abstract.class.php"); +require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); require_once(dirname(__FILE__) ."/abstract/cs_phpDB.abstract.class.php"); -class cs_phpDB extends cs_versionAbstract { +class cs_phpDB extends cs_contentAbstract { private $dbLayerObj; private $dbType; //========================================================================= public function __construct($type='pgsql') { - $this->set_version_file_location(dirname(__FILE__) . '/VERSION'); if(strlen($type)) { @@ -43,12 +42,8 @@ $this->dbLayerObj = new $className; $this->dbType = $type; - $this->gfObj = new cs_globalFunctions; + parent::__construct(); - if(defined('DEBUGPRINTOPT')) { - $this->gfObj->debugPrintOpt = DEBUGPRINTOPT; - } - $this->isInitialized = TRUE; } else { Modified: trunk/1.0/cs_sessionClass.php =================================================================== --- trunk/1.0/cs_sessionClass.php 2009-01-29 19:44:01 UTC (rev 330) +++ trunk/1.0/cs_sessionClass.php 2009-01-29 20:18:41 UTC (rev 331) @@ -10,7 +10,7 @@ require_once(dirname(__FILE__) ."/../cs-versionparse/cs_version.abstract.class.php"); -class cs_session extends cs_versionAbstract { +class cs_session extends cs_contentAbstract { protected $db; public $uid; @@ -26,7 +26,7 @@ * used as the session name. */ function __construct($createSession=1) { - $this->set_version_file_location(dirname(__FILE__) . '/VERSION'); + parent::__construct(false); if($createSession) { if(!is_null($createSession) && strlen($createSession) && !is_numeric($createSession)) { session_name($createSession); Modified: trunk/1.0/cs_siteConfig.class.php =================================================================== --- trunk/1.0/cs_siteConfig.class.php 2009-01-29 19:44:01 UTC (rev 330) +++ trunk/1.0/cs_siteConfig.class.php 2009-01-29 20:18:41 UTC (rev 331) @@ -15,12 +15,12 @@ * */ -require_once(dirname(__FILE__) .'/cs_globalFunctions.php'); +require_once(dirname(__FILE__) .'/abstract/cs_content.abstract.class.php'); require_once(dirname(__FILE__) .'/cs_fileSystemClass.php'); require_once(dirname(__FILE__). '/../cs-phpxml/cs_phpxmlParser.class.php'); require_once(dirname(__FILE__) .'/../cs-phpxml/cs_phpxmlBuilder.class.php'); -class cs_siteConfig { +class cs_siteConfig extends cs_contentAbstract { /** XMLParser{} object, for reading XML config file. */ private $xmlReader; @@ -77,8 +77,7 @@ $section = strtoupper($section); $this->setVarPrefix=$setVarPrefix; - $this->gf = new cs_globalFunctions; - $this->gf->debugPrintOpt=1; + parent::__construct(); if(strlen($configFileLocation) && file_exists($configFileLocation)) { @@ -181,8 +180,8 @@ $itemValue = $itemValue['value']; if(preg_match("/{/", $itemValue)) { $origVal = $itemValue; - $itemValue = $this->gf->mini_parser($itemValue, $specialVars, '{', '}'); - $itemValue = $this->gf->mini_parser($itemValue, $parseThis, '{', '}'); + $itemValue = $this->gfObj->mini_parser($itemValue, $specialVars, '{', '}'); + $itemValue = $this->gfObj->mini_parser($itemValue, $parseThis, '{', '}'); $itemValue = preg_replace("/[\/]{2,}/", "/", $itemValue); } Modified: trunk/1.0/cs_tabsClass.php =================================================================== --- trunk/1.0/cs_tabsClass.php 2009-01-29 19:44:01 UTC (rev 330) +++ trunk/1.0/cs_tabsClass.php 2009-01-29 20:18:41 UTC (rev 331) @@ -4,7 +4,10 @@ * */ -class cs_tabs extends cs_versionAbstract { +require_once(dirname(__FILE__) .'/abstract/cs_content.abstract.class.php'); + + +class cs_tabs extends cs_contentAbstract { private $tabsArr; private $selectedTab; @@ -25,7 +28,7 @@ * @param $templateVar (str,optional) What template var to find the tab blockrows in. */ public function __construct(cs_genericPage $csPageObj, $templateVar="tabs") { - $this->set_version_file_location(dirname(__FILE__) . '/VERSION'); + parent::__construct(false); if(is_null($csPageObj) || !is_object($csPageObj) || get_class($csPageObj) !== 'cs_genericPage') { //can't continue without that! throw new exception("cs_tabs::__construct(): cannot load without cs_genericPage{} object (". get_class($csPageObj) .")"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-01-29 20:40:24
|
Revision: 332 http://cs-content.svn.sourceforge.net/cs-content/?rev=332&view=rev Author: crazedsanity Date: 2009-01-29 20:40:16 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Renamed "contentSystemClass.php" to contentSystem.class.php (standardized CS naming convention). Modified Paths: -------------- trunk/1.0/sample_files/public_html/content Added Paths: ----------- trunk/1.0/contentSystem.class.php Removed Paths: ------------- trunk/1.0/contentSystemClass.php Copied: trunk/1.0/contentSystem.class.php (from rev 331, trunk/1.0/contentSystemClass.php) =================================================================== --- trunk/1.0/contentSystem.class.php (rev 0) +++ trunk/1.0/contentSystem.class.php 2009-01-29 20:40:16 UTC (rev 332) @@ -0,0 +1,854 @@ +<?php +/* + * FILE INFORMATION: + * $HeadURL$ + * $Id$ + * $LastChangedDate$ + * $LastChangedRevision$ + * $LastChangedBy$ + * + * HOW THE SYSTEM WORKS::: + * TEMPLATE FILES: + * Automatically loads templates based on the URL, and optionally includes scripts in the includes directory. + * + * MAIN SECTION: + * For the main section, i.e. "/content", it requires a template in a directory of that name beneath + * /templates, with a file called "index.content.tmpl"... i.e. /templatee/content/index.content.tmpl. + * SUB SECTIONS: + * For any subsection to be valid, i.e. "/content/members", it must have an associated template, i.e. + * "/templates/content/members.content.tmpl". If a subdirectory with an index.content.tmpl file exists, it will + * be used instead of the file in the sub directory (i.e. "/templates/content/members/index.content.tmpl"). + * SUB SECTION TEMPLATE INHERITANCE: + * All pages load the base set of "shared" templates, which are in the form "<section>.shared.tmpl" in + * the root of the templates directory. + * + * Shared files within each directory, in the form "<section>.shared.tmpl", will be loaded for ANY subsection. + * + * For any subsection, it inherits a previous section's templates in the following manner (any "content" + * templates are ignored for inheritance, as they're require for page load). + * /content ---> /templates/content/index.*.tmpl + * + * /content/members |--> /templates/content/index.*.tmpl + * `--> /templates/content/members.*.tmpl + * + * /content/members/test |--> /templates/content/index.*.tmpl + * |--> /templates/content/members.*.tmpl + * |--> /templates/content/members/index.*.tmpl + * `--> /templates/content/members/test.*.tmpl + * AUTOMATIC INCLUDES: + * Much in the same way templates are included, so are scripts, from the /includes directory, though the logic + * is decidedly simpler: all scripts must have the extension of ".inc", and must have either the section's name + * as the first part of the filename, or "shared". Shared scripts will be loaded for ALL subsections. + * + * INCLUDES INHERITANCE: + * The template inheritance scheme is as laid-out below. The content system will go as far into the + * includes directory as it can for the given section, regardless of if any intermediate files are missing. + * + * It is important to note that the content system will NOT regard a section as valid if there are include + * scripts but no templates. + * + * /content |--> /includes/shared.inc + * `--> /includes/content.inc + * + * /content/members |--> /includes/shared.inc + * |--> /includes/content.inc + * |--> /includes/content/shared.inc + * `--> /includes/content/members.inc + * + * /content/members/test |--> /includes/shared.inc + * |--> /includes/content.inc + * |--> /includes/content/shared.inc + * |--> /includes/content/members.inc + * |--> /includes/content/members/shared.inc + * |--> /includes/content/members/test.inc + */ + +//TODO: remove this terrible little hack. +if(!isset($GLOBALS['SITE_ROOT'])) { + //define where our scripts are located. + $GLOBALS['SITE_ROOT'] = $_SERVER['DOCUMENT_ROOT']; + $GLOBALS['SITE_ROOT'] = str_replace("/public_html", "", $GLOBALS['SITE_ROOT']); +} + +require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); +require_once(dirname(__FILE__) ."/cs_fileSystemClass.php"); +require_once(dirname(__FILE__) ."/cs_sessionClass.php"); +require_once(dirname(__FILE__) ."/cs_genericPageClass.php"); +require_once(dirname(__FILE__) ."/cs_tabsClass.php"); + +class contentSystem extends cs_contentAbstract { + + protected $baseDir = NULL; //base directory for templates & includes. + protected $section = NULL; //section string, derived from the URL. + protected $sectionArr = array(); //array of items, for figuring out where templates & includes are. + protected $fileSystemObj = NULL; //the object used to access the filesystem. + protected $ignoredList = array( //array of files & folders that are implicitely ignored. + 'file' => array('.htaccess'), + 'dir' => array('.svn','CVS' + ) + ); + protected $templateList = array(); + protected $includesList = array(); + public $templateObj = NULL; + protected $gfObj = NULL; + protected $tabs = NULL; + + protected $finalSection; + + private $isValid=FALSE; + private $reason=NULL; + + //------------------------------------------------------------------------ + /** + * The CONSTRUCTOR. Duh. + */ + public function __construct($testOnly=FALSE) { + if($testOnly === 'unit_test') { + //It's just a test, don't do anything we might regret later. + $this->isTest = TRUE; + } + else { + parent::__construct(); + + //setup the section stuff... + $repArr = array($_SERVER['SCRIPT_NAME'], "/"); + $_SERVER['REQUEST_URI'] = ereg_replace('^/', "", $_SERVER['REQUEST_URI']); + + //figure out the section & subsection stuff. + $this->section = $this->clean_url($_SERVER['REQUEST_URI']); + + $this->initialize_locals(); + } + }//end __construct() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Creates internal objects & prepares for later usage. + */ + private function initialize_locals() { + //build the templating engine: this may cause an immediate redirect, if they need to be logged-in. + //TODO: find a way to define this on a per-page basis. Possibly have templateObj->check_login() + // run during the "finish" stage... probably using GenericPage{}->check_login(). + $this->templateObj = new cs_genericPage(FALSE, "main.shared.tmpl"); + + //setup some default template vars. + $this->templateObj->add_template_var('date', date('m-d-Y')); + $this->templateObj->add_template_var('time', date('H:i:s')); + + $myUrl = '/'; + if(strlen($this->section) && $this->section !== 0) { + $myUrl = '/'. $this->section; + } + $this->templateObj->add_template_var('CURRENT_URL', $myUrl); + + //create a fileSystem object. + $this->fileSystemObj = new cs_fileSystemClass(); + + //create a tabs object, in case they want to load tabs on the page. + $this->tabs = new cs_tabs($this->templateObj); + + //check versions, make sure they're all the same. + $myVersion = $this->get_version(); + if($this->templateObj->get_version() !== $myVersion) { + throw new exception(__METHOD__ .": ". get_class($this->templateObj) ." has mismatched version (". $this->templateObj->get_version() ." does not equal ". $myVersion .")"); + } + if($this->fileSystemObj->get_version() !== $myVersion) { + throw new exception(__METHOD__ .": ". get_class($this->fileSystemObj) ." has mismatched version (". $this->fileSystemObj->get_version() ." does not equal ". $myVersion .")"); + } + if($this->gfObj->get_version() !== $myVersion) { + throw new exception(__METHOD__ .": ". get_class($this->gfObj) ." has mismatched version (". $this->gfObj->get_version() ." does not equal ". $myVersion .")"); + } + if($this->tabs->get_version() !== $myVersion) { + throw new exception(__METHOD__ .": ". get_class($this->tabs) ." has mismatched version (". $this->tabs->get_version() ." does not equal ". $myVersion .")"); + } + + //split apart the section so we can do stuff with it later. + $this->parse_section(); + + //get ready for when we have to load templates & such. + $this->prepare(); + }//end initialize_locals() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + private function get_template_dirs() { + if(is_array($this->sectionArr)) { + $this->fileSystemObj->cd("/templates/". $this->baseDir); + $retval = array(); + $retval[] = $this->fileSystemObj->cwd; + foreach($this->sectionArr as $index=>$name) { + if($this->fileSystemObj->cd($name)) { + $retval[] = $this->fileSystemObj->cwd; + } + else { + break; + } + } + } + else { + throw new exception(__METHOD__ .": section array is invalid"); + } + + return($retval); + }//end get_template_dirs() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Call this to require that users accessing the given URL are authenticated; + * if they're not, this will cause them to be redirected to another URL + * (generally, so they can login). + */ + public function force_authentication($redirectToUrl, $destinationArg='loginDestination') { + if(is_object($this->session) && method_exists($this->session, 'is_authenticated')) { + if(strlen($redirectToUrl)) { + $cleanedRedirect = $this->clean_url($redirectToUrl); + if($this->section != $cleanedRedirect) { + if(!$this->session->is_authenticated()) { + //run the redirect. + if(strlen($destinationArg)) { + $redirectToUrl .= '?'. $destinationArg .'=/'. urlencode($_SERVER['REQUEST_URI']); + } + $this->gfObj->conditional_header($redirectToUrl, TRUE); + } + } + else { + throw new exception(__METHOD__ .": redirect url (". $redirectToUrl .") matches current URL"); + } + } + else { + throw new exception(__METHOD__ .": failed to provide proper redirection URL"); + } + } + else { + throw new exception(__METHOD__ .": cannot force authentication (missing method)"); + } + }//end force_authentication() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Used to determine if contentSystem{} should handle creating the session. + */ + public function handle_session(&$sessionObj=NULL) { + if(is_object($sessionObj)) { + //they want us to use a different class... fine. + $this->session = $sessionObj; + } + else { + //use our own session handler. + $this->session = new cs_session; + } + + if(!method_exists($this->session, 'is_authenticated')) { + throw new exception(__METHOD__ .": session class ('". get_class($this->session) ."') is missing method is_authenticated()"); + } + }//end handle_session() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Rips apart the "section" string, setting $this->section and $this->sectionArr. + */ + private function parse_section() { + if($this->section === 0 || is_null($this->section) || !strlen($this->section)) { + $this->section = "content/index"; + } + $myArr = split('/', $this->section); + + //if we've got something in the array, keep going. + if(is_array($myArr) && count($myArr) && ($myArr[0] !== 0)) { + $this->baseDir = array_shift($myArr); + $this->sectionArr = $myArr; + } + }//end parse_section() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Removes all the crap from the url, so we can figure out what section we + * need to load templates & includes for. + */ + private function clean_url($section=NULL) { + if(!strlen($section) && strlen($this->section)) { + //if argument wasn't given, use internal pointer. + $section = $this->section; + } + + //make sure we've still got something valid to work with. + if(!strlen($section)) { + //TODO: remove the extra return statement (should only be one at the bottom of the method). + return(NULL); + } + else { + //check the string to make sure it doesn't begin or end with a "/" + if($section[0] == '/') { + $section = substr($section, 1, strlen($section)); + } + + //check the last char for a "/"... + if($section[strlen($section) -1] == '/') { + //last char is a '/'... kill it. + $section = substr($section, 0, strlen($section) -1); + } + + //if we've been sent a query, kill it off the string... + if(preg_match('/\?/', $section)) { + $section = split('\?', $section); + $section = $section[0]; + } + + if(ereg("\.", $section)) { + //disregard file extensions, but keep everything else... + // i.e. "index.php/yermom.html" becomes "index/yermom" + $tArr = split('/', $section); + foreach($tArr as $tSecName) { + $temp = split("\.", $tSecName); + if(strlen($temp[0]) > 1) { + $tSecName = $temp[0]; + } + $tSection = $this->gfObj->create_list($tSection, $tSecName, '/'); + } + $section = $tSection; + } + } + + return($section); + }//end clean_url() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Retrieves the list of templates & includes in preparation for later work. + */ + private function prepare() { + //attempt to load any includes... + if($this->fileSystemObj->cd('/includes')) { + $this->load_includes(); + } + $foundIncludes = count($this->includesList); + + //cd() in to the templates directory. + $cdResult = $this->fileSystemObj->cd('/templates'); + $validatePageRes = $this->validate_page(); + if($foundIncludes || ($cdResult && $validatePageRes)) { + + //okay, get template directories & start loading + $tmplDirs = $this->get_template_dirs(); + + $this->load_shared_templates(); + foreach($tmplDirs as $myPath) { + //load shared templates. + $this->load_shared_templates($myPath); + } + + //load templates for the main section. + $this->load_main_templates(); + + //load templates for the page. + $this->load_page_templates(); + + //now cd() all the way back. + $this->fileSystemObj->cd('/'); + } + else { + //couldn't find the templates directory, and no includes... it's dead. + $this->die_gracefully(__METHOD__ .": unable to find the templates directory, or non-valid page [". $this->validate_page() ."]"); + } + }//end prepare() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Ensures the page we're on would actually load, so other methods don't have to do + * so much extra checking. + */ + private function validate_page() { + $valid = FALSE; + //if we've got a non-basedir page, (instead of "/whatever", we have "/whatever/x"), see + // if there are templates that make it good... or just check the base template. + if((count($this->sectionArr) > 0) && !((count($this->sectionArr) == 1) && ($this->sectionArr[0] == 'index'))) { + //got more than just a baseDir url... see if the template is good. + $finalLink = $this->gfObj->string_from_array($this->sectionArr, NULL, '/'); + $this->fileSystemObj->cd($this->baseDir); + $mySectionArr = $this->sectionArr; + $finalSection = array_pop($mySectionArr); + $this->finalSection = $finalSection; + if(count($mySectionArr) > 0) { + foreach($mySectionArr as $dir) { + if(!$this->fileSystemObj->cd($dir)) { + break; + } + } + } + + //check for the file & the directory... + $indexFilename = $finalSection ."/index.content.tmpl"; + if(!strlen($finalSection)) { + $indexFilename = 'index.content.tmpl'; + } + + $lsDir = $this->fileSystemObj->ls($indexFilename); + $lsDirVals = array_values($lsDir); + $lsFile = $this->fileSystemObj->ls("$finalSection.content.tmpl"); + + if(is_array(array_values($lsFile)) && is_array($lsFile[$finalSection .".content.tmpl"])) { + //it's the file ("{finalSection}.content.tmpl", like "mySection.content.tmpl") + $myIndex = $finalSection .".content.tmpl"; + } + elseif(is_array(array_values($lsDir)) && (is_array($lsDir[$indexFilename]))) { + $myIndex = $indexFilename; + } + else { + //nothin' doin'. + $myIndex = NULL; + } + + //check the index file for validity... this is kind of a dirty hack... but it works. + $checkMe = $this->fileSystemObj->ls($myIndex); + if(!is_array($checkMe[$myIndex])) { + unset($myIndex); + } + + if(isset($myIndex)) { + $valid = TRUE; + $this->fileSystemObj->cd('/templates'); + } + else { + $this->reason = __METHOD__ .": couldn't find page template for ". $this->section; + } + } + else { + //if the baseDir is "help", this would try to use "/help/index.content.tmpl" + $myFile = $this->baseDir .'/index.content.tmpl'; + $sectionLsData = $this->fileSystemObj->ls($myFile); + + //if the baseDir is "help", this would try to use "/help.content.tmpl" + $sectionFile = $this->baseDir .'.content.tmpl'; + $lsData = $this->fileSystemObj->ls(); + + if(isset($lsData[$sectionFile]) && is_array($lsData[$sectionFile])) { + $valid = TRUE; + $this->finalSection = $this->baseDir; + } + elseif(isset($sectionLsData[$myFile]) && $sectionLsData[$myFile]['type'] == 'file') { + //we're good. + $valid = TRUE; + $this->finalSection = $this->baseDir; + } + else { + $this->reason = __METHOD__ .": couldn't find base template."; + } + } + $this->isValid = $valid; + + return($valid); + }//end validate_page() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Loads the templates for the current page (performs template inheritance, too). + */ + private function load_page_templates() { + //should already be in the proper directory, start looping through sectionArr, + // looking for templates. + $mySectionArr = $this->sectionArr; + + $finalSection = $this->sectionArr[(count($this->sectionArr) -1)]; + foreach($mySectionArr as $index=>$value) { + $tmplList = $this->arrange_directory_contents('name', 'section'); + if(isset($tmplList[$value])) { + foreach($tmplList[$value] as $mySection=>$myTmpl) { + // + $this->templateList[$mySection] = $myTmpl; + } + } + if(!$this->fileSystemObj->cd($value)) { + break; + } + } + + //load the final template(s). + $finalTmplList = $this->arrange_directory_contents('name', 'section'); + if(isset($finalTmplList[$finalSection])) { + foreach($finalTmplList[$finalSection] as $mySection => $myTmpl) { + $this->templateList[$mySection] = $myTmpl; + } + } + elseif(is_array($finalTmplList)) { + foreach($finalTmplList as $mySection => $subArr) { + foreach($subArr as $internalSection => $myTmpl) { + $this->templateList[$mySection] = $myTmpl; + } + } + } + if($this->fileSystemObj->cd($finalSection)) { + //load the index stuff. + $tmplList = $this->arrange_directory_contents('name', 'section'); + if(isset($tmplList['index'])) { + foreach($tmplList['index'] as $mySection => $myTmpl) { + $this->templateList[$mySection] = $myTmpl; + } + } + if(isset($tmplList[$this->baseDir]['content'])) { + //load template for the main page (if $this->baseDir == "help", this would load "/help.content.tmpl" as content) + $this->templateList['content'] = $tmplList[$this->baseDir]['content']; + } + } + }//end load_page_templates() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * loads templates for the main section they're on. + */ + private function load_main_templates() { + $this->fileSystemObj->cd('/templates'); + //check to see if the present section is valid. + $this->fileSystemObj->cd($this->baseDir); + $dirContents = $this->arrange_directory_contents('name', 'section'); + if(is_array($dirContents)) { + foreach($dirContents as $mySection => $subArr) { + foreach($subArr as $subIndex=>$templateFilename) { + $this->templateList[$mySection] = $templateFilename; + } + } + } + }//end load_main_templates() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Loads any shared templates: these can be overwritten later. + */ + private function load_shared_templates($path=NULL) { + + if(!is_null($path)) { + $this->fileSystemObj->cd($path); + } + else { + $this->fileSystemObj->cd('/templates'); + } + + //pull a list of the files. + $dirContents = $this->arrange_directory_contents(); + if(count($dirContents['shared'])) { + + foreach($dirContents['shared'] as $section => $template) { + $this->templateList[$section] = $template; + } + } + }//end load_shared_templates() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Pulls a list of files in the current directory, & arranges them by section & + * name, or vice-versa. + */ + private function arrange_directory_contents($primaryIndex='section', $secondaryIndex='name') { + $directoryInfo = $this->fileSystemObj->ls(); + $arrangedArr = array(); + if(is_array($directoryInfo)) { + foreach($directoryInfo as $index=>$data) { + $myType = $data['type']; + if(($myType == 'file') && !in_array($index, $this->ignoredList[$myType])) { + $filename = $this->gfObj->create_list($this->fileSystemObj->cwd, $index, '/'); + $filename = preg_replace('/^\/templates/', '', $filename); + $filename = preg_replace('/^\/\//', '/', $filename); + //call another method to rip the filename apart properly, then arrange things as needed. + $pieces = $this->parse_filename($index); + $myPriIndex = $pieces[$primaryIndex]; + $mySecIndex = $pieces[$secondaryIndex]; + if(strlen($myPriIndex) && strlen($mySecIndex)) { + //only load if it's got BOTH parts of the filename. + $arrangedArr[$myPriIndex][$mySecIndex] = $filename; + } + } + } + } + + return($arrangedArr); + }//end arrange_directory_contents() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Takes a filename (string) and breaks it down into the "type", "section", and + * "name". I.E. for the filename "test.content.tmpl", type=tmpl, section="content", + * and "name"=test. + * TODO: set a way to define how the filenames are setup, so filenames can be "name.section.type" or "section.name.type". + */ + private function parse_filename($filename) { + //break it into it's various parts. + $myParts = explode('.', $filename); + $retval = array(); + $count = count($myParts); + if($count >= 3) { + //"type" is the last element of the array, and "section" is the second-to-last. + $type = array_pop($myParts); + + //define what types of files that are accepted: if it's not one of them, don't bother. + $acceptedTypes = array("tmpl"); + if(in_array($type, $acceptedTypes)) { + $section = array_pop($myParts); + + //just in case we want to allow templates with "."'s in them, rip off the + // last two parts, and use what's left as the name. + $stripThis = '.'. $section .'\.'. $type .'$'; + $name = preg_replace('/'. $stripThis .'/', '', $filename); + + $retval = array( + 'name' => $name, + 'section' => $section, + 'type' => $type + ); + } + } + + return($retval); + }//end parse_filename() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Finds all scripts in the /inlcudes directory, & adds them to the includesList array. + */ + private function load_includes() { + + //first load includes for the base directory. + $this->load_dir_includes($this->baseDir); + + //okay, now loop through $this->sectionArr & see if we can include anything else. + if(($this->fileSystemObj->cd($this->baseDir)) && is_array($this->sectionArr) && count($this->sectionArr) > 0) { + + foreach($this->sectionArr as $mySection) { + //Run includes. + $this->load_dir_includes($mySection); + + //attempt to cd() into the next directory, or die if we can't. + if(!$this->fileSystemObj->cd($mySection)) { + //no dice. Break the loop. + break; + } + } + } + + }//end load_includes() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Attempts to add a shared include & the given section's include file: used + * solely by load_includes(). + */ + private function load_dir_includes($section) { + $lsData = $this->fileSystemObj->ls(); + + //attempt to load the shared includes file. + if(isset($lsData['shared.inc']) && $lsData['shared.inc']['type'] == 'file') { + $this->includesList[] = $this->fileSystemObj->realcwd .'/shared.inc'; + } + + //attempt to load the section's includes file. + $myFile = $section .'.inc'; + if(isset($lsData[$myFile]) && $lsData[$myFile]['type'] == 'file') { + $this->includesList[] = $this->fileSystemObj->realcwd .'/'. $myFile; + } + + if(isset($lsData[$section]) && !count($this->sectionArr)) { + $this->fileSystemObj->cd($section); + $lsData = $this->fileSystemObj->ls(); + if(isset($lsData['index.inc'])) { + $this->includesList[] = $this->fileSystemObj->realcwd .'/index.inc'; + } + } + }//end load_dir_includes() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Called when something is broken. + */ + private function die_gracefully($details=NULL) { + header('HTTP/1.0 404 Not Found'); + if($this->templateObj->template_file_exists('system/404.shared.tmpl')) { + //Simple "Page Not Found" error... show 'em. + $this->templateObj->add_template_var('main', $this->templateObj->file_to_string('system/404.shared.tmpl')); + $this->templateObj->add_template_var('details', $details); + $this->templateObj->add_template_var('datetime', date('m-d-Y H:i:s')); + $this->templateObj->print_page(); + exit; + } + else { + //TODO: make it *actually* die gracefully... the way it works now looks more like puke than grace. + throw new exception(__METHOD__ .": Couldn't find 404 template, plus additional error... \nDETAILS::: $details" . + "\nREASON::: ". $this->reason); + } + }//end die_gracefully() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * The super-magical method that includes files & finalizes things using + * the given templating engine. + * NOTE: the local variable "$page" is made so that the included scripts + * can make calls to the templating engine, just like they used to. It's + * AWESOME. + */ + function finish() { + //Avoid problems when REGISTER_GLOBALS is on... + $badUrlVars = array('page', 'this'); + foreach($badUrlVars as $badVarName) { + unset($_GET[$badVarName], $_POST[$badVarName]); + } + + $page =& $this->templateObj; + if(is_object($this->session)) { + $page->session =& $this->session; + } + + + //if we loaded an index, but there is no "content", then move 'em around so we have content. + if(isset($this->templateList['index']) && !isset($this->templateList['content'])) { + $this->templateList['content'] = $this->templateList['index']; + unset($this->templateList['index']); + } + + foreach($this->templateList as $mySection => $myTmpl) { + $myTmpl = preg_replace("/\/\//", "/", $myTmpl); + $page->add_template_file($mySection, $myTmpl); + } + unset($mySection); + unset($myTmpl); + + //make the "final section" available to scripts. + $finalSection = $this->finalSection; + $sectionArr = $this->sectionArr; + array_unshift($sectionArr, $this->baseDir); + $finalURL = $this->gfObj->string_from_array($sectionArr, NULL, '/'); + + //now include the includes scripts, if there are any. + if(is_array($this->includesList) && count($this->includesList)) { + try { + foreach($this->includesList as $myInternalIndex=>$myInternalScriptName) { + $this->myLastInclude = $myInternalScriptName; + include_once($this->myLastInclude); + } + } + catch(exception $e) { + $myRoot = preg_replace('/\//', '\\\/', $this->fileSystemObj->root); + $displayableInclude = preg_replace('/^'. $myRoot .'/', '', $this->myLastInclude); + $this->templateObj->set_message_wrapper(array( + 'title' => "Fatal Error", + 'message' => __METHOD__ .": A fatal error occurred while processing <b>". + $displayableInclude ."</b>:<BR>\n<b>ERROR</b>: ". $e->getMessage(), + 'type' => "fatal" + )); + + //try to pass the error on to the user's exception handler, if there is one. + if(function_exists('exception_handler')) { + exception_handler($e); + } + } + unset($myInternalIndex); + unset($myInternalScriptName); + } + + if(is_bool($this->templateObj->allow_invalid_urls() === TRUE) && $this->isValid === FALSE) { + $this->isValid = $this->templateObj->allow_invalid_urls(); + } + + if($this->isValid === TRUE) { + $page->print_page(); + } + else { + $this->die_gracefully($this->reason); + } + }//end finish() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Method for accessing the protected $this->sectionArr array. + */ + public function get_sectionArr() { + //give 'em what they want. + return($this->sectionArr); + }//end get_sectionArr() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Method for accessing the protected member $this->finalSection. + */ + public function get_finalSection() { + //give 'em what they want. + return($this->finalSection); + }//end get_finalSection() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * Method for accessing "baseDir", only referenced as the base section. + */ + public function get_baseSection() { + return($this->baseDir); + }//end get_baseSection() + //------------------------------------------------------------------------ + + + + //------------------------------------------------------------------------ + /** + * The destructor... does nothing, right now. + */ + public function __destruct() { + }//end __destruct() + //------------------------------------------------------------------------ + + +}//end contentSystem{} +?> Property changes on: trunk/1.0/contentSystem.class.php ___________________________________________________________________ Added: svn:keywords + HeadURL Id LastChangedBy LastChangedDate LastChangedRevision Added: svn:mergeinfo + Added: svn:eol-style + native Deleted: trunk/1.0/contentSystemClass.php =================================================================== --- trunk/1.0/contentSystemClass.php 2009-01-29 20:18:41 UTC (rev 331) +++ trunk/1.0/contentSystemClass.php 2009-01-29 20:40:16 UTC (rev 332) @@ -1,854 +0,0 @@ -<?php -/* - * FILE INFORMATION: - * $HeadURL$ - * $Id$ - * $LastChangedDate$ - * $LastChangedRevision$ - * $LastChangedBy$ - * - * HOW THE SYSTEM WORKS::: - * TEMPLATE FILES: - * Automatically loads templates based on the URL, and optionally includes scripts in the includes directory. - * - * MAIN SECTION: - * For the main section, i.e. "/content", it requires a template in a directory of that name beneath - * /templates, with a file called "index.content.tmpl"... i.e. /templatee/content/index.content.tmpl. - * SUB SECTIONS: - * For any subsection to be valid, i.e. "/content/members", it must have an associated template, i.e. - * "/templates/content/members.content.tmpl". If a subdirectory with an index.content.tmpl file exists, it will - * be used instead of the file in the sub directory (i.e. "/templates/content/members/index.content.tmpl"). - * SUB SECTION TEMPLATE INHERITANCE: - * All pages load the base set of "shared" templates, which are in the form "<section>.shared.tmpl" in - * the root of the templates directory. - * - * Shared files within each directory, in the form "<section>.shared.tmpl", will be loaded for ANY subsection. - * - * For any subsection, it inherits a previous section's templates in the following manner (any "content" - * templates are ignored for inheritance, as they're require for page load). - * /content ---> /templates/content/index.*.tmpl - * - * /content/members |--> /templates/content/index.*.tmpl - * `--> /templates/content/members.*.tmpl - * - * /content/members/test |--> /templates/content/index.*.tmpl - * |--> /templates/content/members.*.tmpl - * |--> /templates/content/members/index.*.tmpl - * `--> /templates/content/members/test.*.tmpl - * AUTOMATIC INCLUDES: - * Much in the same way templates are included, so are scripts, from the /includes directory, though the logic - * is decidedly simpler: all scripts must have the extension of ".inc", and must have either the section's name - * as the first part of the filename, or "shared". Shared scripts will be loaded for ALL subsections. - * - * INCLUDES INHERITANCE: - * The template inheritance scheme is as laid-out below. The content system will go as far into the - * includes directory as it can for the given section, regardless of if any intermediate files are missing. - * - * It is important to note that the content system will NOT regard a section as valid if there are include - * scripts but no templates. - * - * /content |--> /includes/shared.inc - * `--> /includes/content.inc - * - * /content/members |--> /includes/shared.inc - * |--> /includes/content.inc - * |--> /includes/content/shared.inc - * `--> /includes/content/members.inc - * - * /content/members/test |--> /includes/shared.inc - * |--> /includes/content.inc - * |--> /includes/content/shared.inc - * |--> /includes/content/members.inc - * |--> /includes/content/members/shared.inc - * |--> /includes/content/members/test.inc - */ - -//TODO: remove this terrible little hack. -if(!isset($GLOBALS['SITE_ROOT'])) { - //define where our scripts are located. - $GLOBALS['SITE_ROOT'] = $_SERVER['DOCUMENT_ROOT']; - $GLOBALS['SITE_ROOT'] = str_replace("/public_html", "", $GLOBALS['SITE_ROOT']); -} - -require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); -require_once(dirname(__FILE__) ."/cs_fileSystemClass.php"); -require_once(dirname(__FILE__) ."/cs_sessionClass.php"); -require_once(dirname(__FILE__) ."/cs_genericPageClass.php"); -require_once(dirname(__FILE__) ."/cs_tabsClass.php"); - -class contentSystem extends cs_contentAbstract { - - protected $baseDir = NULL; //base directory for templates & includes. - protected $section = NULL; //section string, derived from the URL. - protected $sectionArr = array(); //array of items, for figuring out where templates & includes are. - protected $fileSystemObj = NULL; //the object used to access the filesystem. - protected $ignoredList = array( //array of files & folders that are implicitely ignored. - 'file' => array('.htaccess'), - 'dir' => array('.svn','CVS' - ) - ); - protected $templateList = array(); - protected $includesList = array(); - public $templateObj = NULL; - protected $gfObj = NULL; - protected $tabs = NULL; - - protected $finalSection; - - private $isValid=FALSE; - private $reason=NULL; - - //------------------------------------------------------------------------ - /** - * The CONSTRUCTOR. Duh. - */ - public function __construct($testOnly=FALSE) { - if($testOnly === 'unit_test') { - //It's just a test, don't do anything we might regret later. - $this->isTest = TRUE; - } - else { - parent::__construct(); - - //setup the section stuff... - $repArr = array($_SERVER['SCRIPT_NAME'], "/"); - $_SERVER['REQUEST_URI'] = ereg_replace('^/', "", $_SERVER['REQUEST_URI']); - - //figure out the section & subsection stuff. - $this->section = $this->clean_url($_SERVER['REQUEST_URI']); - - $this->initialize_locals(); - } - }//end __construct() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * Creates internal objects & prepares for later usage. - */ - private function initialize_locals() { - //build the templating engine: this may cause an immediate redirect, if they need to be logged-in. - //TODO: find a way to define this on a per-page basis. Possibly have templateObj->check_login() - // run during the "finish" stage... probably using GenericPage{}->check_login(). - $this->templateObj = new cs_genericPage(FALSE, "main.shared.tmpl"); - - //setup some default template vars. - $this->templateObj->add_template_var('date', date('m-d-Y')); - $this->templateObj->add_template_var('time', date('H:i:s')); - - $myUrl = '/'; - if(strlen($this->section) && $this->section !== 0) { - $myUrl = '/'. $this->section; - } - $this->templateObj->add_template_var('CURRENT_URL', $myUrl); - - //create a fileSystem object. - $this->fileSystemObj = new cs_fileSystemClass(); - - //create a tabs object, in case they want to load tabs on the page. - $this->tabs = new cs_tabs($this->templateObj); - - //check versions, make sure they're all the same. - $myVersion = $this->get_version(); - if($this->templateObj->get_version() !== $myVersion) { - throw new exception(__METHOD__ .": ". get_class($this->templateObj) ." has mismatched version (". $this->templateObj->get_version() ." does not equal ". $myVersion .")"); - } - if($this->fileSystemObj->get_version() !== $myVersion) { - throw new exception(__METHOD__ .": ". get_class($this->fileSystemObj) ." has mismatched version (". $this->fileSystemObj->get_version() ." does not equal ". $myVersion .")"); - } - if($this->gfObj->get_version() !== $myVersion) { - throw new exception(__METHOD__ .": ". get_class($this->gfObj) ." has mismatched version (". $this->gfObj->get_version() ." does not equal ". $myVersion .")"); - } - if($this->tabs->get_version() !== $myVersion) { - throw new exception(__METHOD__ .": ". get_class($this->tabs) ." has mismatched version (". $this->tabs->get_version() ." does not equal ". $myVersion .")"); - } - - //split apart the section so we can do stuff with it later. - $this->parse_section(); - - //get ready for when we have to load templates & such. - $this->prepare(); - }//end initialize_locals() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - private function get_template_dirs() { - if(is_array($this->sectionArr)) { - $this->fileSystemObj->cd("/templates/". $this->baseDir); - $retval = array(); - $retval[] = $this->fileSystemObj->cwd; - foreach($this->sectionArr as $index=>$name) { - if($this->fileSystemObj->cd($name)) { - $retval[] = $this->fileSystemObj->cwd; - } - else { - break; - } - } - } - else { - throw new exception(__METHOD__ .": section array is invalid"); - } - - return($retval); - }//end get_template_dirs() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * Call this to require that users accessing the given URL are authenticated; - * if they're not, this will cause them to be redirected to another URL - * (generally, so they can login). - */ - public function force_authentication($redirectToUrl, $destinationArg='loginDestination') { - if(is_object($this->session) && method_exists($this->session, 'is_authenticated')) { - if(strlen($redirectToUrl)) { - $cleanedRedirect = $this->clean_url($redirectToUrl); - if($this->section != $cleanedRedirect) { - if(!$this->session->is_authenticated()) { - //run the redirect. - if(strlen($destinationArg)) { - $redirectToUrl .= '?'. $destinationArg .'=/'. urlencode($_SERVER['REQUEST_URI']); - } - $this->gfObj->conditional_header($redirectToUrl, TRUE); - } - } - else { - throw new exception(__METHOD__ .": redirect url (". $redirectToUrl .") matches current URL"); - } - } - else { - throw new exception(__METHOD__ .": failed to provide proper redirection URL"); - } - } - else { - throw new exception(__METHOD__ .": cannot force authentication (missing method)"); - } - }//end force_authentication() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * Used to determine if contentSystem{} should handle creating the session. - */ - public function handle_session(&$sessionObj=NULL) { - if(is_object($sessionObj)) { - //they want us to use a different class... fine. - $this->session = $sessionObj; - } - else { - //use our own session handler. - $this->session = new cs_session; - } - - if(!method_exists($this->session, 'is_authenticated')) { - throw new exception(__METHOD__ .": session class ('". get_class($this->session) ."') is missing method is_authenticated()"); - } - }//end handle_session() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * Rips apart the "section" string, setting $this->section and $this->sectionArr. - */ - private function parse_section() { - if($this->section === 0 || is_null($this->section) || !strlen($this->section)) { - $this->section = "content/index"; - } - $myArr = split('/', $this->section); - - //if we've got something in the array, keep going. - if(is_array($myArr) && count($myArr) && ($myArr[0] !== 0)) { - $this->baseDir = array_shift($myArr); - $this->sectionArr = $myArr; - } - }//end parse_section() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * Removes all the crap from the url, so we can figure out what section we - * need to load templates & includes for. - */ - private function clean_url($section=NULL) { - if(!strlen($section) && strlen($this->section)) { - //if argument wasn't given, use internal pointer. - $section = $this->section; - } - - //make sure we've still got something valid to work with. - if(!strlen($section)) { - //TODO: remove the extra return statement (should only be one at the bottom of the method). - return(NULL); - } - else { - //check the string to make sure it doesn't begin or end with a "/" - if($section[0] == '/') { - $section = substr($section, 1, strlen($section)); - } - - //check the last char for a "/"... - if($section[strlen($section) -1] == '/') { - //last char is a '/'... kill it. - $section = substr($section, 0, strlen($section) -1); - } - - //if we've been sent a query, kill it off the string... - if(preg_match('/\?/', $section)) { - $section = split('\?', $section); - $section = $section[0]; - } - - if(ereg("\.", $section)) { - //disregard file extensions, but keep everything else... - // i.e. "index.php/yermom.html" becomes "index/yermom" - $tArr = split('/', $section); - foreach($tArr as $tSecName) { - $temp = split("\.", $tSecName); - if(strlen($temp[0]) > 1) { - $tSecName = $temp[0]; - } - $tSection = $this->gfObj->create_list($tSection, $tSecName, '/'); - } - $section = $tSection; - } - } - - return($section); - }//end clean_url() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * Retrieves the list of templates & includes in preparation for later work. - */ - private function prepare() { - //attempt to load any includes... - if($this->fileSystemObj->cd('/includes')) { - $this->load_includes(); - } - $foundIncludes = count($this->includesList); - - //cd() in to the templates directory. - $cdResult = $this->fileSystemObj->cd('/templates'); - $validatePageRes = $this->validate_page(); - if($foundIncludes || ($cdResult && $validatePageRes)) { - - //okay, get template directories & start loading - $tmplDirs = $this->get_template_dirs(); - - $this->load_shared_templates(); - foreach($tmplDirs as $myPath) { - //load shared templates. - $this->load_shared_templates($myPath); - } - - //load templates for the main section. - $this->load_main_templates(); - - //load templates for the page. - $this->load_page_templates(); - - //now cd() all the way back. - $this->fileSystemObj->cd('/'); - } - else { - //couldn't find the templates directory, and no includes... it's dead. - $this->die_gracefully(__METHOD__ .": unable to find the templates directory, or non-valid page [". $this->validate_page() ."]"); - } - }//end prepare() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * Ensures the page we're on would actually load, so other methods don't have to do - * so much extra checking. - */ - private function validate_page() { - $valid = FALSE; - //if we've got a non-basedir page, (instead of "/whatever", we have "/whatever/x"), see - // if there are templates that make it good... or just check the base template. - if((count($this->sectionArr) > 0) && !((count($this->sectionArr) == 1) && ($this->sectionArr[0] == 'index'))) { - //got more than just a baseDir url... see if the template is good. - $finalLink = $this->gfObj->string_from_array($this->sectionArr, NULL, '/'); - $this->fileSystemObj->cd($this->baseDir); - $mySectionArr = $this->sectionArr; - $finalSection = array_pop($mySectionArr); - $this->finalSection = $finalSection; - if(count($mySectionArr) > 0) { - foreach($mySectionArr as $dir) { - if(!$this->fileSystemObj->cd($dir)) { - break; - } - } - } - - //check for the file & the directory... - $indexFilename = $finalSection ."/index.content.tmpl"; - if(!strlen($finalSection)) { - $indexFilename = 'index.content.tmpl'; - } - - $lsDir = $this->fileSystemObj->ls($indexFilename); - $lsDirVals = array_values($lsDir); - $lsFile = $this->fileSystemObj->ls("$finalSection.content.tmpl"); - - if(is_array(array_values($lsFile)) && is_array($lsFile[$finalSection .".content.tmpl"])) { - //it's the file ("{finalSection}.content.tmpl", like "mySection.content.tmpl") - $myIndex = $finalSection .".content.tmpl"; - } - elseif(is_array(array_values($lsDir)) && (is_array($lsDir[$indexFilename]))) { - $myIndex = $indexFilename; - } - else { - //nothin' doin'. - $myIndex = NULL; - } - - //check the index file for validity... this is kind of a dirty hack... but it works. - $checkMe = $this->fileSystemObj->ls($myIndex); - if(!is_array($checkMe[$myIndex])) { - unset($myIndex); - } - - if(isset($myIndex)) { - $valid = TRUE; - $this->fileSystemObj->cd('/templates'); - } - else { - $this->reason = __METHOD__ .": couldn't find page template for ". $this->section; - } - } - else { - //if the baseDir is "help", this would try to use "/help/index.content.tmpl" - $myFile = $this->baseDir .'/index.content.tmpl'; - $sectionLsData = $this->fileSystemObj->ls($myFile); - - //if the baseDir is "help", this would try to use "/help.content.tmpl" - $sectionFile = $this->baseDir .'.content.tmpl'; - $lsData = $this->fileSystemObj->ls(); - - if(isset($lsData[$sectionFile]) && is_array($lsData[$sectionFile])) { - $valid = TRUE; - $this->finalSection = $this->baseDir; - } - elseif(isset($sectionLsData[$myFile]) && $sectionLsData[$myFile]['type'] == 'file') { - //we're good. - $valid = TRUE; - $this->finalSection = $this->baseDir; - } - else { - $this->reason = __METHOD__ .": couldn't find base template."; - } - } - $this->isValid = $valid; - - return($valid); - }//end validate_page() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * Loads the templates for the current page (performs template inheritance, too). - */ - private function load_page_templates() { - //should already be in the proper directory, start looping through sectionArr, - // looking for templates. - $mySectionArr = $this->sectionArr; - - $finalSection = $this->sectionArr[(count($this->sectionArr) -1)]; - foreach($mySectionArr as $index=>$value) { - $tmplList = $this->arrange_directory_contents('name', 'section'); - if(isset($tmplList[$value])) { - foreach($tmplList[$value] as $mySection=>$myTmpl) { - // - $this->templateList[$mySection] = $myTmpl; - } - } - if(!$this->fileSystemObj->cd($value)) { - break; - } - } - - //load the final template(s). - $finalTmplList = $this->arrange_directory_contents('name', 'section'); - if(isset($finalTmplList[$finalSection])) { - foreach($finalTmplList[$finalSection] as $mySection => $myTmpl) { - $this->templateList[$mySection] = $myTmpl; - } - } - elseif(is_array($finalTmplList)) { - foreach($finalTmplList as $mySection => $subArr) { - foreach($subArr as $internalSection => $myTmpl) { - $this->templateList[$mySection] = $myTmpl; - } - } - } - if($this->fileSystemObj->cd($finalSection)) { - //load the index stuff. - $tmplList = $this->arrange_directory_contents('name', 'section'); - if(isset($tmplList['index'])) { - foreach($tmplList['index'] as $mySection => $myTmpl) { - $this->templateList[$mySection] = $myTmpl; - } - } - if(isset($tmplList[$this->baseDir]['content'])) { - //load template for the main page (if $this->baseDir == "help", this would load "/help.content.tmpl" as content) - $this->templateList['content'] = $tmplList[$this->baseDir]['content']; - } - } - }//end load_page_templates() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * loads templates for the main section they're on. - */ - private function load_main_templates() { - $this->fileSystemObj->cd('/templates'); - //check to see if the present section is valid. - $this->fileSystemObj->cd($this->baseDir); - $dirContents = $this->arrange_directory_contents('name', 'section'); - if(is_array($dirContents)) { - foreach($dirContents as $mySection => $subArr) { - foreach($subArr as $subIndex=>$templateFilename) { - $this->templateList[$mySection] = $templateFilename; - } - } - } - }//end load_main_templates() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * Loads any shared templates: these can be overwritten later. - */ - private function load_shared_templates($path=NULL) { - - if(!is_null($path)) { - $this->fileSystemObj->cd($path); - } - else { - $this->fileSystemObj->cd('/templates'); - } - - //pull a list of the files. - $dirContents = $this->arrange_directory_contents(); - if(count($dirContents['shared'])) { - - foreach($dirContents['shared'] as $section => $template) { - $this->templateList[$section] = $template; - } - } - }//end load_shared_templates() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * Pulls a list of files in the current directory, & arranges them by section & - * name, or vice-versa. - */ - private function arrange_directory_contents($primaryIndex='section', $secondaryIndex='name') { - $directoryInfo = $this->fileSystemObj->ls(); - $arrangedArr = array(); - if(is_array($directoryInfo)) { - foreach($directoryInfo as $index=>$data) { - $myType = $data['type']; - if(($myType == 'file') && !in_array($index, $this->ignoredList[$myType])) { - $filename = $this->gfObj->create_list($this->fileSystemObj->cwd, $index, '/'); - $filename = preg_replace('/^\/templates/', '', $filename); - $filename = preg_replace('/^\/\//', '/', $filename); - //call another method to rip the filename apart properly, then arrange things as needed. - $pieces = $this->parse_filename($index); - $myPriIndex = $pieces[$primaryIndex]; - $mySecIndex = $pieces[$secondaryIndex]; - if(strlen($myPriIndex) && strlen($mySecIndex)) { - //only load if it's got BOTH parts of the filename. - $arrangedArr[$myPriIndex][$mySecIndex] = $filename; - } - } - } - } - - return($arrangedArr); - }//end arrange_directory_contents() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * Takes a filename (string) and breaks it down into the "type", "section", and - * "name". I.E. for the filename "test.content.tmpl", type=tmpl, section="content", - * and "name"=test. - * TODO: set a way to define how the filenames are setup, so filenames can be "name.section.type" or "section.name.type". - */ - private function parse_filename($filename) { - //break it into it's various parts. - $myParts = explode('.', $filename); - $retval = array(); - $count = count($myParts); - if($count >= 3) { - //"type" is the last element of the array, and "section" is the second-to-last. - $type = array_pop($myParts); - - //define what types of files that are accepted: if it's not one of them, don't bother. - $acceptedTypes = array("tmpl"); - if(in_array($type, $acceptedTypes)) { - $section = array_pop($myParts); - - //just in case we want to allow templates with "."'s in them, rip off the - // last two parts, and use what's left as the name. - $stripThis = '.'. $section .'\.'. $type .'$'; - $name = preg_replace('/'. $stripThis .'/', '', $filename); - - $retval = array( - 'name' => $name, - 'section' => $section, - 'type' => $type - ); - } - } - - return($retval); - }//end parse_filename() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * Finds all scripts in the /inlcudes directory, & adds them to the includesList array. - */ - private function load_includes() { - - //first load includes for the base directory. - $this->load_dir_includes($this->baseDir); - - //okay, now loop through $this->sectionArr & see if we can include anything else. - if(($this->fileSystemObj->cd($this->baseDir)) && is_array($this->sectionArr) && count($this->sectionArr) > 0) { - - foreach($this->sectionArr as $mySection) { - //Run includes. - $this->load_dir_includes($mySection); - - //attempt to cd() into the next directory, or die if we can't. - if(!$this->fileSystemObj->cd($mySection)) { - //no dice. Break the loop. - break; - } - } - } - - }//end load_includes() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * Attempts to add a shared include & the given section's include file: used - * solely by load_includes(). - */ - private function load_dir_includes($section) { - $lsData = $this->fileSystemObj->ls(); - - //attempt to load the shared includes file. - if(isset($lsData['shared.inc']) && $lsData['shared.inc']['type'] == 'file') { - $this->includesList[] = $this->fileSystemObj->realcwd .'/shared.inc'; - } - - //attempt to load the section's includes file. - $myFile = $section .'.inc'; - if(isset($lsData[$myFile]) && $lsData[$myFile]['type'] == 'file') { - $this->includesList[] = $this->fileSystemObj->realcwd .'/'. $myFile; - } - - if(isset($lsData[$section]) && !count($this->sectionArr)) { - $this->fileSystemObj->cd($section); - $lsData = $this->fileSystemObj->ls(); - if(isset($lsData['index.inc'])) { - $this->includesList[] = $this->fileSystemObj->realcwd .'/index.inc'; - } - } - }//end load_dir_includes() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * Called when something is broken. - */ - private function die_gracefully($details=NULL) { - header('HTTP/1.0 404 Not Found'); - if($this->templateObj->template_file_exists('system/404.shared.tmpl')) { - //Simple "Page Not Found" error... show 'em. - $this->templateObj->add_template_var('main', $this->templateObj->file_to_string('system/404.shared.tmpl')); - $this->templateObj->add_template_var('details', $details); - $this->templateObj->add_template_var('datetime', date('m-d-Y H:i:s')); - $this->templateObj->print_page(); - exit; - } - else { - //TODO: make it *actually* die gracefully... the way it works now looks more like puke than grace. - throw new exception(__METHOD__ .": Couldn't find 404 template, plus additional error... \nDETAILS::: $details" . - "\nREASON::: ". $this->reason); - } - }//end die_gracefully() - //------------------------------------------------------------------------ - - - - //------------------------------------------------------------------------ - /** - * The super-magical method that includes files & finalizes things using - * the given templating engine. - * NOTE: the local variable "$page" is made so that the included scripts - * can make calls to the templating engine, just like they used to. It's - * AWESOME. - */ - function finish() { - //Avoid problems when REGISTER_GLOBALS is on... - $badUrlVars = array('page', 'this'); - foreach($badUrlVars as $badVarName) { - unset($_GET[$badVarName], $_POST[$badVarName]); - } - - $page =& $this->templateObj; - if(is_object($this->session)) { - $page->session =& $this->session; - } - - - //if we loaded an index, but there is no "content", then move 'em around so we have content. - if(isset($this->templateList['index']) && !isset($this->templateList['content'])) { - $this->templateList['content'] = $this->templateList['index']; - unset($this->templateList['index']); - } - - foreach($this->templateList as $mySection => $myTmpl) { - $myTmpl = preg_replace("/\/\//", "/", $myTmpl); - $page->add_template_file($mySection, $myTmpl); - } - unset($mySection); - unset($myTmpl); - - //make the "final section" available to scripts. - $finalSection =... [truncated message content] |
From: <cra...@us...> - 2009-01-29 21:04:23
|
Revision: 333 http://cs-content.svn.sourceforge.net/cs-content/?rev=333&view=rev Author: crazedsanity Date: 2009-01-29 21:04:18 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Rename cs_fileSystemClass.php to cs_fileSystem.class.php & change name to match. /contentSystem.class.php: * MAIN::: -- fix require_once() statement to point to proper file * __construct(): -- use new class name cs_fileSystem{} /cs_fileSystem.class.php [RENAMED FROM cs_fileSystemClass.php] /cs_fileSystemClass.php [RENAMED TO cs_fileSystem.class.php] /cs_siteConfig.class.php: * MAIN::: -- fix require_once statement for cs_fileSystem. -- update comments to reference appropriate class name. * __construct(): -- update references to use new class name. Modified Paths: -------------- trunk/1.0/contentSystem.class.php trunk/1.0/cs_siteConfig.class.php Added Paths: ----------- trunk/1.0/cs_fileSystem.class.php Removed Paths: ------------- trunk/1.0/cs_fileSystemClass.php Modified: trunk/1.0/contentSystem.class.php =================================================================== --- trunk/1.0/contentSystem.class.php 2009-01-29 20:40:16 UTC (rev 332) +++ trunk/1.0/contentSystem.class.php 2009-01-29 21:04:18 UTC (rev 333) @@ -71,7 +71,7 @@ } require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); -require_once(dirname(__FILE__) ."/cs_fileSystemClass.php"); +require_once(dirname(__FILE__) ."/cs_fileSystem.class.php"); require_once(dirname(__FILE__) ."/cs_sessionClass.php"); require_once(dirname(__FILE__) ."/cs_genericPageClass.php"); require_once(dirname(__FILE__) ."/cs_tabsClass.php"); @@ -145,7 +145,7 @@ $this->templateObj->add_template_var('CURRENT_URL', $myUrl); //create a fileSystem object. - $this->fileSystemObj = new cs_fileSystemClass(); + $this->fileSystemObj = new cs_fileSystem(); //create a tabs object, in case they want to load tabs on the page. $this->tabs = new cs_tabs($this->templateObj); Copied: trunk/1.0/cs_fileSystem.class.php (from rev 331, trunk/1.0/cs_fileSystemClass.php) =================================================================== --- trunk/1.0/cs_fileSystem.class.php (rev 0) +++ trunk/1.0/cs_fileSystem.class.php 2009-01-29 21:04:18 UTC (rev 333) @@ -0,0 +1,969 @@ +<?php + +/* + * FILE INFORMATION: + * $HeadURL$ + * $Id$ + * $LastChangedDate$ + * $LastChangedBy$ + * $LastChangedRevision$ + */ + +require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); + +class cs_fileSystem extends cs_contentAbstract { + + public $root; //actual root directory. + public $cwd; //current directory; relative to $this->root + public $realcwd; //$this->root .'/'. $this->cwd + public $dh; //directory handle. + public $fh; //file handle. + public $filename; //filename currently being used. + public $lineNum = NULL; + + + //======================================================================================== + /** + * The constructor. + */ + public function __construct($rootDir=NULL, $cwd=NULL, $initialMode=NULL) { + //set the root directory that we'll be using; this is considered just like "/" in + // linux. Directories above it are considered non-existent. + if(($rootDir) AND (is_dir($rootDir))) { + // yup... use it. + $this->root = $rootDir; + } elseif(($GLOBALS['SITE_ROOT']) AND (is_dir($GLOBALS['SITE_ROOT']))) { + //not set, but SITE_ROOT is... use it. + $this->root = $GLOBALS['SITE_ROOT']; + } else { + //nothing useable... die. + exit("UNUSEABLE ROOT: $rootDir"); + } + + parent::__construct(); + + $this->root = $this->resolve_path_with_dots($this->root); + + //set the CURRENT working directory... this should be a RELATIVE path to $this->root. + if(($cwd) AND (is_dir($rootDir .'/'. $cwd)) AND (!ereg($this->root, $cwd))) { + //looks good. Use it. + $this->cwd = $cwd; + $this->realcwd = $this->root .'/'. $cwd; + } else { + //no dice. Use the root. + $this->cwd = '/'; + $this->realcwd = $this->root ; + } + chdir($this->realcwd); + + //check for the initialMode... + $useableModes = array('r', 'r+', 'w', 'w+', 'a', 'a+', 'x', 'x+'); + if(($initialMode) AND (in_array($initialMode, $useableModes))) { + // + $this->mode = $initialMode; + } else { + //define the DEFAULT mode. + $this->mode = "r+"; + } + + }//end __construct() + //======================================================================================== + + + + //======================================================================================== + public function cdup() { + $retval = FALSE; + //easy way to go "up" a directory (just like doing "cd .." in linux) + if(strlen($this->cwd) > 1) { + $myCwd = preg_replace('/\/$/', '', $this->cwd); + if(!preg_match('/^\//', $myCwd)) { + $myCwd = '/'. $myCwd; + } + $myParts = explode('/', $myCwd); + array_pop($myParts); + $myCwd = $this->gfObj->string_from_array($myParts, NULL, '/'); + $realCwd = $this->gfObj->create_list($this->root, $myCwd, '/'); + if(file_exists($realCwd)) { + $retval = TRUE; + $this->realcwd = $realCwd; + $this->cwd = '/'. $myCwd; + } + } + + return($retval); + }//end cdup() + //======================================================================================== + + + + //======================================================================================== + /** + * Think of the linux version of "cd": we're changing the current directory. + * + * @param $newDir (str) dir to change to, either absolutte or relative. + * + * @return 0 (FAIL) unable to change directory + * @return 1 (PASS) success. + */ + public function cd($newDir) { + + //check to see if it's a relative path or not... + //NOTE: non-relative paths should NOT include $this->root. + if((preg_match('/^\//', $newDir)) AND (is_dir($this->root .'/'. $newDir))) { + //not a relative path. + $this->cwd = '/'. $newDir; + $this->realcwd = $this->root . $newDir; + $retval = 1; + } elseif(is_dir($this->realcwd .'/'. $newDir)) { + //relative path... + $this->cwd = $this->gfObj->create_list($this->cwd, $newDir, '/'); + $this->realcwd .= '/'. $newDir; + $retval = 1; + } else { + //bad. + $retval = 0; + } + $this->cwd = preg_replace('/\/\//', '/', $this->cwd); + $this->realcwd = preg_replace('/\/\//', '/', $this->realcwd); + + return($retval); + }//end cd() + //======================================================================================== + + + //======================================================================================== + /** + * Just like the linux version of the 'ls' command. + */ + public function ls($filename=NULL, $args=NULL) { + + clearstatcache(); + //open the directory for reading. + $this->dh = opendir($this->realcwd); + clearstatcache(); + if(is_string($filename)) { + //check to make sure the file exists. + $tFile=$this->filename2absolute($filename); + if(file_exists($tFile)) { + //it's there... get info about it. + $info = $this->get_fileinfo($tFile); + if($info['type'] == 'dir') { + $oldCwd = $this->cwd; + $oldRealCwd = $this->realcwd; + + $this->cd($filename); + $retval = $this->ls(); + + $this->cwd = $oldCwd; + $this->realcwd = $oldRealCwd; + } + else { + $retval[$filename] = $info; + } + } else { + //stupid! + $retval[$filename] = "FILE NOT FOUND."; + } + } else { + //array if file/directory names to ignore if matched exactly. + $ignoreArr = array("CVS", ".svn", ".", ".."); + while (($file = readdir($this->dh)) !== false) { + if(!in_array($file, $ignoreArr)) { + $tFile = $this->realcwd .'/'. $file; + $tType = filetype($tFile); + $retval[$file] = $this->get_fileinfo($tFile); + if(!$tType) { + debug_print("FILE: $tFile || TYPE: $tType || is_file(): ". is_file($tFile) ."is_dir(): ". is_dir($tFile)); + exit; + } + #debug_print("FILE: $file || $dir". $file); + unset($tType); + } + } + } + #debug_print($retval); + #debug_print(readdir($this->dh)); + return($retval); + }//end ls() + //======================================================================================== + + + //======================================================================================== + /** + * Grabs an array of information for a given file. + */ + public function get_fileinfo($tFile) { + + $retval = array( + "size" => filesize($tFile), + "type" => @filetype($tFile), + "accessed" => fileatime($tFile), + "modified" => filemtime($tFile), + "owner" => $this->my_getuser_group(fileowner($tFile), 'uid'), + "uid" => fileowner($tFile), + "group" => $this->my_getuser_group(filegroup($tFile), 'gid'), + "gid" => filegroup($tFile), + "perms" => $this->translate_perms(fileperms($tFile)), + "perms_num" => substr(sprintf('%o', fileperms($tFile)), -4) + ); + + return($retval); + }//end get_fileinfo() + //======================================================================================== + + + + //======================================================================================== + /** + * Gets the username/groupname of the given uid/gid. + * + * @param $int (int) uid/gid to check. + * @param $type (str) is it a uid or a gid? + * + * @return (string) groupname/username + */ + private function my_getuser_group($int, $type='uid') { + + if($type == 'uid') { + $func = 'posix_getpwuid'; + } elseif($type == 'gid') { + $func = 'posix_getgrgid'; + } else { + $retval = $int; + } + + if(!function_exists($func)) { + throw new exception(__METHOD__ .": required function missing (". $func .")"); + } + $t = $func($int); + return($t['name']); + + }//end my_getpwuid() + //======================================================================================== + + + + //======================================================================================== + /** + * Translates the permissions string (like "0700") into a *nix-style permissions + * string (i.e. "rwx------"). + * + * @param $in_Perms (int) permission number string + * + * @return (string) permissions string. + * + * NOTE: pretty sure I copied this from php.net, though I don't know the owner. If anybody + * can enlighten me, I'd be glad to give them credit. + */ + private function translate_perms($in_Perms) { + $sP = ""; + $sP .= (($in_Perms & 0x0100) ? 'r' : '-') . + (($in_Perms & 0x0080) ? 'w' : '-') . + (($in_Perms & 0x0040) ? (($in_Perms & 0x0800) ? 's' : 'x' ) : + (($in_Perms & 0x0800) ? 'S' : '-')); + // group + $sP .= (($in_Perms & 0x0020) ? 'r' : '-') . + (($in_Perms & 0x0010) ? 'w' : '-') . + (($in_Perms & 0x0008) ? (($in_Perms & 0x0400) ? 's' : 'x' ) : + (($in_Perms & 0x0400) ? 'S' : '-')); + + // world + $sP .= (($in_Perms & 0x0004) ? 'r' : '-') . + (($in_Perms & 0x0002) ? 'w' : '-') . + (($in_Perms & 0x0001) ? (($in_Perms & 0x0200) ? 't' : 'x' ) : + (($in_Perms & 0x0200) ? 'T' : '-')); + return($sP); + }//end translate_perms() + //======================================================================================== + + + //======================================================================================== + /** + * Creates an empty file... think of the linux command "touch". + * + * @param $filename (string) filename to create. + * + * @return 0 (FAIL) unable to create file. + * @return 1 (PASS) file created successfully. + */ + public function create_file($filename, $truncateFile=FALSE) { + + $retval = 0; + $filename = $this->filename2absolute($filename); + $filename = $this->resolve_path_with_dots($filename); + $this->filename = $filename; + + //check to see if the file exists... + if(!file_exists($filename)) { + if($this->is_writable(dirname($filename))) { + //no file. Create it. + $createFileRes = touch($filename); + if($createFileRes) { + $retval = 1; + } + else { + throw new exception(__METHOD__ .": invalid return from touch(". $filename ."), return was (". $createFileRes .")"); + } + } + else { + throw new exception(__METHOD__ .": directory (". dirname($filename) .") is not writable"); + } + } + elseif($truncateFile === TRUE) { + $this->truncate_file($filename); + } + else { + throw new exception(__METHOD__ .": file (". $filename .") exists and truncate not set"); + } + return($retval); + }//end create_file() + //======================================================================================== + + + + //======================================================================================== + /** + * Opens a stream/resource handle to use for doing file i/o. + * + * @param $filename (string) filename to open: should be relative to the current dir. + * @param $mode (string) mode to use: consult PHP.net's info for fopen(). + * + * @return 0 (FAIL) unable to open file. + * @return 1 (PASS) file opened successfully. + */ + public function openFile($filename=NULL, $mode="r+") { + clearstatcache(); + if(!strlen($filename) || is_null($filename)) { + $filename = $this->filename; + } + $filename = $this->filename2absolute($filename); + $this->filename = $filename; + + if($this->is_readable($filename)) { + //make sure we've got a mode to use. + + if(!is_string($mode)) { + $mode = "r+"; + } + $this->mode = $mode; + + if(in_array($this->mode, array("r+", "w", "w+", "a", "a+", "x", "x+")) && !$this->is_writable($filename)) { + throw new exception(__METHOD__ .": file is not writable (". $filename .") (". $this->is_writable($filename) ."), mode=(". $this->mode .")"); + } + + //attempt to open a stream to a file... + $this->fh = fopen($this->filename, $this->mode); + if(is_resource($this->fh)) { + //looks like we opened successfully. + $retval = 1; + $this->lineNum = 0; + } else { + //something bad happened. + $retval = 0; + } + } + else { + throw new exception(__METHOD__ .": file is unreadable (". $filename .")"); + } + + return($retval); + }//end openFile() + //======================================================================================== + + + //======================================================================================== + /** + * Write the given contents into the current file or the filename given. + * + * @param $content (str) Content to write into the file. + * @param $filename (str,optional) filename to use. If none given, the current one is used. + * + * @return 0 (FAIL) unable to write content to the file. + * @return (n) (PASS) wrote (n) bytes. + */ + public function write($content, $filename=NULL) { + + //open the file for writing. + if(!$filename) { + $filename= $this->filename; + } + $this->filename = $filename; + + //open the file... + $openResult = $this->openFile($this->filename, $this->mode); + + //looks like we made it... + $retval = fwrite($this->fh, $content, strlen($content)); + + //done... return the result. + return($retval); + }//end write() + //======================================================================================== + + + //======================================================================================== + /** + * Takes the given filename & returns the ABSOLUTE pathname: checks to see if the given + * string already has the absolute path in it. + */ + private function filename2absolute($filename) { + + clearstatcache(); + + $filename = $this->resolve_path_with_dots($filename); + + //see if it starts with a "/"... + if(preg_match("/^\//", $filename)) { + $retval = $filename; + } else { + $retval=$this->realcwd .'/'. $filename; + $retval = $this->resolve_path_with_dots($retval); + #debug_print(__METHOD__ .": realcwd=(". $this->realcwd .")"); + #$this->resolve_path_with_dots($retval); + } + + if(!$this->check_chroot($retval, FALSE)) { + $this->gfObj->debug_print(func_get_args()); + throw new exception(__METHOD__ .": file is outside of allowed directory (". $retval .")"); + } + + return($retval); + + }//end filename2absolute() + //======================================================================================== + + + + //======================================================================================== + /** + * Reads-in the contents of an entire file. + */ + function read($filename, $returnArray=FALSE) { + $myFile = $this->filename2absolute($filename); + if(!file_exists($myFile)) { + throw new exception(__METHOD__ .": file doesn't exist (". $myFile .")"); + } + elseif($this->is_readable($myFile)) { + if($returnArray) { + $data = file($myFile); + } + else { + $data = file_get_contents($myFile); + } + + if($data === FALSE) { + throw new exception(__METHOD__. ": file_get_contents() returned FALSE"); + } + } + else { + throw new exception(__METHOD__. ": File isn't readable (". $myFile .")"); + } + return($data); + }//end read() + //======================================================================================== + + + + //======================================================================================== + public function rm($filename) { + $filename = $this->filename2absolute($filename); + return(unlink($filename)); + }//end rm() + //======================================================================================== + + + + //======================================================================================== + public function rmdir($dirname) { + $dirname = $this->filename2absolute($dirname); + return(rmdir($dirname)); + }//end rm() + //======================================================================================== + + + + //======================================================================================== + /** + * Return the next line for a file. + * + * When the end of the file is found, this method returns FALSE (returning NULL might be + * misconstrued as a blank line). + */ + public function get_next_line($maxLength=NULL, $trimLine=TRUE) { + if(is_resource($this->fh) && get_resource_type($this->fh) == 'stream') { + if(feof($this->fh)) { + $retval = FALSE; + } + else { + if(!is_numeric($maxLength)) { + $retval = @fgets($this->fh); + } + else { + $retval = fgets($this->fh, $maxLength); + } + if($trimLine) { + $retval = trim($retval); + } + + if(is_null($this->lineNum) || !is_numeric($this->lineNum) || $this->lineNum < 0) { + throw new exception(__METHOD__ .": invalid data for lineNum (". $this->lineNum .")"); + } + $this->lineNum++; + } + } + else { + throw new exception(__METHOD__ .": invalid filehandle"); + } + + return($retval); + }//end get_next_line() + //======================================================================================== + + + + //======================================================================================== + public function append_to_file($data, $eolChar="\n") { + $retval = FALSE; + if(is_resource($this->fh)) { + $result = fwrite($this->fh, $data . $eolChar); + if($result === FALSE) { + throw new exception(__METHOD__ .": failed to write data to file"); + } + else { + $this->lineNum++; + $retval = $result; + } + } + else { + throw new exception(__METHOD__ .": invalid filehandle"); + } + + return($retval); + }//end append_to_file() + //======================================================================================== + + + + //======================================================================================== + public function closeFile() { + $retval = FALSE; + if(is_resource($this->fh)) { + fclose($this->fh); + $retval = TRUE; + } + + //reset internal pointers. + $this->filename = NULL; + $this->lineNum = NULL; + + return($retval); + }//end closeFile() + //======================================================================================== + + + + //======================================================================================== + /** + * Compare the given filename to the open filename to see if they match (using this allows + * giving a filename instead of comparing the whole path). + */ + public function compare_open_filename($compareToFilename) { + if(!strlen($compareToFilename) || is_null($compareToFilename)) { + throw new exception(__METHOD__ .": invalid filename to compare"); + } + elseif(!strlen($this->filename)) { + $retval = FALSE; + } + else { + $internalFilename = $this->filename2absolute($this->filename); + $compareToFilename = $this->filename2absolute($compareToFilename); + if($internalFilename == $compareToFilename) { + $retval = TRUE; + } + else { + $retval = FALSE; + } + } + + return($retval); + }//end compare_open_filename() + //======================================================================================== + + + + //======================================================================================== + /** + * Give a file a new name. + * + * TODO: check to make sure both files exist within our root. + */ + public function rename($currentFilename, $newFilename) { + if($newFilename == $currentFilename) { + $this->gfObj->debug_print(func_get_args()); + throw new exception(__METHOD__ .": renaming file to same name"); + } + + if($this->compare_open_filename($currentFilename)) { + $this->closeFile(); + } + + if($this->compare_open_filename($newFilename)) { + //renaming a different file to our currently open file... + $this->gfObj->debug_print(func_get_args()); + throw new exception(__METHOD__ .": renaming another file (". $currentFilename .") to the currently open filename (". $newFilename .")"); + } + else { + + $currentFilename = $this->filename2absolute($currentFilename); + $newFilename = $this->filename2absolute($newFilename); + + if(!$this->is_writable(dirname($newFilename))) { + throw new exception(__METHOD__ .": directory isn't writable... "); + } + $retval = rename($currentFilename, $newFilename); + if($retval !== TRUE) { + throw new exception(__METHOD__ .": failed to rename file (". $retval .")"); + } + } + + return($retval); + + }//end rename() + //======================================================================================== + + + + //======================================================================================== + /** + * Check if the given filename is executable. + */ + public function is_executable($filename) { + $filename = $this->filename2absolute($filename); + $retval = FALSE; + if(strlen($filename)) { + $retval = is_executable($filename); + } + + return($retval); + }//end is_executable() + //======================================================================================== + + + + //======================================================================================== + /** + * Check if the given filename is readable. + */ + public function is_readable($filename) { + $filename = $this->filename2absolute($filename); + $retval = FALSE; + if(strlen($filename)) { + $retval = is_readable($filename); + } + + return($retval); + }//end is_readable() + //======================================================================================== + + + + //======================================================================================== + /** + * Check if the given filename/path is writable + */ + public function is_writable($filenameOrPath) { + $filenameOrPath = $this->filename2absolute($filenameOrPath); + $retval = FALSE; + if(strlen($filenameOrPath)) { + $retval = is_writable($filenameOrPath); + } + + return($retval); + }//end is_writable() + //======================================================================================== + + + + //======================================================================================== + /** + * Determines how many lines are left in the current file. + */ + public function count_remaining_lines() { + if(is_resource($this->fh) && get_resource_type($this->fh) == 'stream') { + $originalLineNum = $this->lineNum; + + $myFilename = $this->filename; + $myNextLine = $this->get_next_line(); + $retval = 0; + while($myNextLine !== FALSE) { + $retval++; + $myNextLine = $this->get_next_line(); + } + + $this->closeFile(); + $this->openFile($myFilename, $this->mode); + + if($originalLineNum > 0) { + while($originalLineNum > $this->lineNum) { + $this->get_next_line(); + } + } + + if($this->lineNum !== $originalLineNum) { + throw new exception(__METHOD__ .": failed to match-up old linenum (". $originalLineNum .") with the current one (". $this->lineNum .")"); + } + } + else { + throw new exception(__METHOD__ .": Invalid filehandle, can't count remaining lines"); + } + + return($retval); + }//end count_remaining_files() + //======================================================================================== + + + + //======================================================================================== + /** + * Moves the cursor to the given line number. + * + * NOTE: remember if you're trying to get line #1 (literally), then you'll + * want to go to line #0, then call get_next_line() to retrieve it... this + * is the traditional logical vs. programatic numbering issue. A.k.a. the + * "off-by-one problem". + */ + public function go_to_line($lineNum) { + $retval = FALSE; + if(is_resource($this->fh) && get_resource_type($this->fh) == 'stream') { + if($this->lineNum > $lineNum) { + //gotta "rewind" the cursor back to the beginning. + rewind($this->fh); + $this->lineNum=0; + } + + if($lineNum == $this->lineNum) { + $retval = TRUE; + } + elseif($this->lineNum < $lineNum) { + while($this->lineNum < $lineNum) { + //don't grab any data, just move the cursor... + $this->get_next_line(); + } + if($this->lineNum == $lineNum) { + $retval = TRUE; + } + else { + throw new exception(__METHOD__ .": couldn't reach the line (". $lineNum ."), failed at (". $this->lineNum .")"); + } + } + else { + throw new exception(__METHOD__ .": internal lineNum (". $this->lineNum .") couldn't be retrieved or reset to (". $lineNum .")"); + } + } + + return($retval); + }//end go_to_line() + //======================================================================================== + + + + //======================================================================================== + /** + * Fix a path that contains "../". + * + * EXAMPLE: changes "/home/user/blah/blah/../../test" into "/home/user/test" + */ + public function resolve_path_with_dots($path) { + + while(preg_match('/\/\//', $path)) { + $path = preg_replace('/\/\//', '/', $path); + } + $retval = $path; + if(strlen($path) && preg_match('/\./', $path)) { + + $isAbsolute = FALSE; + if(preg_match('/^\//', $path)) { + $isAbsolute = TRUE; + $path = preg_replace('/^\//', '', $path); + } + $pieces = explode('/', $path); + + $finalPieces = array(); + for($i=0; $i < count($pieces); $i++) { + $dirName = $pieces[$i]; + if($dirName == '.') { + //do nothing; don't bother appending. + } + elseif($dirName == '..') { + $rippedIndex = array_pop($finalPieces); + } + else { + $finalPieces[] = $dirName; + } + } + + $retval = $this->gfObj->string_from_array($finalPieces, NULL, '/'); + if($isAbsolute) { + $retval = '/'. $retval; + } + } + + return($retval); + }//end resolve_path_with_dots() + //======================================================================================== + + + + //======================================================================================== + private function check_chroot($path, $translatePath=TRUE) { + if($translatePath === TRUE) { + $path = $this->filename2absolute($path); + } + + //now, let's go through the root directory structure, & make sure $path is within that. + $rootPieces = explode('/', $this->root); + $pathPieces = explode('/', $path); + + + if($rootPieces[0] == '') { + array_shift($rootPieces); + } + if($rootPieces[(count($rootPieces) -1)] == '') { + array_pop($rootPieces); + } + if($pathPieces[0] == '') { + array_shift($pathPieces); + } + + $retval = TRUE; + $tmp = ''; + foreach($rootPieces as $index=>$dirName) { + $pathDir = $pathPieces[$index]; + if($pathDir != $dirName) { + $retval = FALSE; + $this->gfObj->debug_print(__METHOD__ .": failed... tmp=(". $tmp ."), dirName=(". $dirName .")"); + break; + } + $tmp = $this->gfObj->create_list($tmp, $dirName, '/'); + } + + return($retval); + }//end check_chroot() + //======================================================================================== + + + + //======================================================================================== + public function copy_file($filename, $destination) { + $retval = FALSE; + if($this->openFile($filename)) { + if($this->check_chroot($destination)) { + //okay, try to copy. + $retval = copy($this->fh, $destination); + } + else { + throw new exception(__METHOD__ .':: destination is not in the directory path'); + } + } + + return($retval); + }//end copy_file() + //======================================================================================== + + + + //======================================================================================== + public function move_file($filename, $destination) { + $retval = FALSE; + if($this->is_readable($filename)) { + if($this->check_chroot($destination)) { + //do the move. + $retval = rename($filename, $destination); + } + else { + $this->gfObj->debug_print(__METHOD__ .":: ". $this->check_chroot($destination),1); + throw new exception(__METHOD__ .':: destination is not in the directory path (from=['. $filename .'], to=['. $destination .']'); + } + } + + return($retval); + }//end move_file() + //======================================================================================== + + + + //======================================================================================== + public function mkdir($name, $mode=0777) { + if(!is_numeric($mode) || strlen($mode) != 4) { + $mode = 0777; + } + $retval = NULL; + if(!is_null($name) && strlen($name)) { + $name = $this->filename2absolute($name); + if($this->check_chroot($name)) { + $retval = mkdir($name, $mode); + chmod($name, $mode); + } + else { + throw new exception(__METHOD__ .': ('. $name .') isn\'t within chroot'); + } + } + else { + cs_debug_backtrace(1); + throw new exception(__METHOD__ .': invalid data: ('. $name .')'); + } + + return($retval); + }//end mkdir() + //======================================================================================== + + + + //======================================================================================== + public function truncate_file($filename) { + if($this->is_writable($filename)) { + if($this->openFile($filename)) { + $retval = ftruncate($this->fh,0); + $this->closeFile(); + } + else { + throw new exception(__METHOD__ .": unable to open specified file (". $filename .")"); + } + } + else { + throw new exception(__METHOD__ .": Cannot truncate, file (". $filename .") is not writable"); + } + + return($retval); + }//end truncate_file() + //======================================================================================== + + + + //======================================================================================== + public function go_to_last_line() { + if(is_resource($this->fh) && get_resource_type($this->fh) == 'stream') { + if(feof($this->fh)) { + $retval = TRUE; + } + else { + //NOTE::: fseek() doesn't update the file pointer in $this->fh, so we have to use fgets(), which seems faster anyway. + while(!feof($this->fh)) { + fgets($this->fh); + $this->lineNum++; + } + $retval = TRUE; + } + } + else { + throw new exception(__METHOD__ .": invalid filehandle"); + } + + return($retval); + }//end go_to_last_line() + //======================================================================================== + + +}//end cs_filesystemClass{} +?> Property changes on: trunk/1.0/cs_fileSystem.class.php ___________________________________________________________________ Added: svn:keywords + HeadURL Id LastChangedBy LastChangedDate LastChangedRevision Added: svn:mergeinfo + Added: svn:eol-style + native Deleted: trunk/1.0/cs_fileSystemClass.php =================================================================== --- trunk/1.0/cs_fileSystemClass.php 2009-01-29 20:40:16 UTC (rev 332) +++ trunk/1.0/cs_fileSystemClass.php 2009-01-29 21:04:18 UTC (rev 333) @@ -1,969 +0,0 @@ -<?php - -/* - * FILE INFORMATION: - * $HeadURL$ - * $Id$ - * $LastChangedDate$ - * $LastChangedBy$ - * $LastChangedRevision$ - */ - -require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); - -class cs_fileSystemClass extends cs_contentAbstract { - - public $root; //actual root directory. - public $cwd; //current directory; relative to $this->root - public $realcwd; //$this->root .'/'. $this->cwd - public $dh; //directory handle. - public $fh; //file handle. - public $filename; //filename currently being used. - public $lineNum = NULL; - - - //======================================================================================== - /** - * The constructor. - */ - public function __construct($rootDir=NULL, $cwd=NULL, $initialMode=NULL) { - //set the root directory that we'll be using; this is considered just like "/" in - // linux. Directories above it are considered non-existent. - if(($rootDir) AND (is_dir($rootDir))) { - // yup... use it. - $this->root = $rootDir; - } elseif(($GLOBALS['SITE_ROOT']) AND (is_dir($GLOBALS['SITE_ROOT']))) { - //not set, but SITE_ROOT is... use it. - $this->root = $GLOBALS['SITE_ROOT']; - } else { - //nothing useable... die. - exit("UNUSEABLE ROOT: $rootDir"); - } - - parent::__construct(); - - $this->root = $this->resolve_path_with_dots($this->root); - - //set the CURRENT working directory... this should be a RELATIVE path to $this->root. - if(($cwd) AND (is_dir($rootDir .'/'. $cwd)) AND (!ereg($this->root, $cwd))) { - //looks good. Use it. - $this->cwd = $cwd; - $this->realcwd = $this->root .'/'. $cwd; - } else { - //no dice. Use the root. - $this->cwd = '/'; - $this->realcwd = $this->root ; - } - chdir($this->realcwd); - - //check for the initialMode... - $useableModes = array('r', 'r+', 'w', 'w+', 'a', 'a+', 'x', 'x+'); - if(($initialMode) AND (in_array($initialMode, $useableModes))) { - // - $this->mode = $initialMode; - } else { - //define the DEFAULT mode. - $this->mode = "r+"; - } - - }//end __construct() - //======================================================================================== - - - - //======================================================================================== - public function cdup() { - $retval = FALSE; - //easy way to go "up" a directory (just like doing "cd .." in linux) - if(strlen($this->cwd) > 1) { - $myCwd = preg_replace('/\/$/', '', $this->cwd); - if(!preg_match('/^\//', $myCwd)) { - $myCwd = '/'. $myCwd; - } - $myParts = explode('/', $myCwd); - array_pop($myParts); - $myCwd = $this->gfObj->string_from_array($myParts, NULL, '/'); - $realCwd = $this->gfObj->create_list($this->root, $myCwd, '/'); - if(file_exists($realCwd)) { - $retval = TRUE; - $this->realcwd = $realCwd; - $this->cwd = '/'. $myCwd; - } - } - - return($retval); - }//end cdup() - //======================================================================================== - - - - //======================================================================================== - /** - * Think of the linux version of "cd": we're changing the current directory. - * - * @param $newDir (str) dir to change to, either absolutte or relative. - * - * @return 0 (FAIL) unable to change directory - * @return 1 (PASS) success. - */ - public function cd($newDir) { - - //check to see if it's a relative path or not... - //NOTE: non-relative paths should NOT include $this->root. - if((preg_match('/^\//', $newDir)) AND (is_dir($this->root .'/'. $newDir))) { - //not a relative path. - $this->cwd = '/'. $newDir; - $this->realcwd = $this->root . $newDir; - $retval = 1; - } elseif(is_dir($this->realcwd .'/'. $newDir)) { - //relative path... - $this->cwd = $this->gfObj->create_list($this->cwd, $newDir, '/'); - $this->realcwd .= '/'. $newDir; - $retval = 1; - } else { - //bad. - $retval = 0; - } - $this->cwd = preg_replace('/\/\//', '/', $this->cwd); - $this->realcwd = preg_replace('/\/\//', '/', $this->realcwd); - - return($retval); - }//end cd() - //======================================================================================== - - - //======================================================================================== - /** - * Just like the linux version of the 'ls' command. - */ - public function ls($filename=NULL, $args=NULL) { - - clearstatcache(); - //open the directory for reading. - $this->dh = opendir($this->realcwd); - clearstatcache(); - if(is_string($filename)) { - //check to make sure the file exists. - $tFile=$this->filename2absolute($filename); - if(file_exists($tFile)) { - //it's there... get info about it. - $info = $this->get_fileinfo($tFile); - if($info['type'] == 'dir') { - $oldCwd = $this->cwd; - $oldRealCwd = $this->realcwd; - - $this->cd($filename); - $retval = $this->ls(); - - $this->cwd = $oldCwd; - $this->realcwd = $oldRealCwd; - } - else { - $retval[$filename] = $info; - } - } else { - //stupid! - $retval[$filename] = "FILE NOT FOUND."; - } - } else { - //array if file/directory names to ignore if matched exactly. - $ignoreArr = array("CVS", ".svn", ".", ".."); - while (($file = readdir($this->dh)) !== false) { - if(!in_array($file, $ignoreArr)) { - $tFile = $this->realcwd .'/'. $file; - $tType = filetype($tFile); - $retval[$file] = $this->get_fileinfo($tFile); - if(!$tType) { - debug_print("FILE: $tFile || TYPE: $tType || is_file(): ". is_file($tFile) ."is_dir(): ". is_dir($tFile)); - exit; - } - #debug_print("FILE: $file || $dir". $file); - unset($tType); - } - } - } - #debug_print($retval); - #debug_print(readdir($this->dh)); - return($retval); - }//end ls() - //======================================================================================== - - - //======================================================================================== - /** - * Grabs an array of information for a given file. - */ - public function get_fileinfo($tFile) { - - $retval = array( - "size" => filesize($tFile), - "type" => @filetype($tFile), - "accessed" => fileatime($tFile), - "modified" => filemtime($tFile), - "owner" => $this->my_getuser_group(fileowner($tFile), 'uid'), - "uid" => fileowner($tFile), - "group" => $this->my_getuser_group(filegroup($tFile), 'gid'), - "gid" => filegroup($tFile), - "perms" => $this->translate_perms(fileperms($tFile)), - "perms_num" => substr(sprintf('%o', fileperms($tFile)), -4) - ); - - return($retval); - }//end get_fileinfo() - //======================================================================================== - - - - //======================================================================================== - /** - * Gets the username/groupname of the given uid/gid. - * - * @param $int (int) uid/gid to check. - * @param $type (str) is it a uid or a gid? - * - * @return (string) groupname/username - */ - private function my_getuser_group($int, $type='uid') { - - if($type == 'uid') { - $func = 'posix_getpwuid'; - } elseif($type == 'gid') { - $func = 'posix_getgrgid'; - } else { - $retval = $int; - } - - if(!function_exists($func)) { - throw new exception(__METHOD__ .": required function missing (". $func .")"); - } - $t = $func($int); - return($t['name']); - - }//end my_getpwuid() - //======================================================================================== - - - - //======================================================================================== - /** - * Translates the permissions string (like "0700") into a *nix-style permissions - * string (i.e. "rwx------"). - * - * @param $in_Perms (int) permission number string - * - * @return (string) permissions string. - * - * NOTE: pretty sure I copied this from php.net, though I don't know the owner. If anybody - * can enlighten me, I'd be glad to give them credit. - */ - private function translate_perms($in_Perms) { - $sP = ""; - $sP .= (($in_Perms & 0x0100) ? 'r' : '-') . - (($in_Perms & 0x0080) ? 'w' : '-') . - (($in_Perms & 0x0040) ? (($in_Perms & 0x0800) ? 's' : 'x' ) : - (($in_Perms & 0x0800) ? 'S' : '-')); - // group - $sP .= (($in_Perms & 0x0020) ? 'r' : '-') . - (($in_Perms & 0x0010) ? 'w' : '-') . - (($in_Perms & 0x0008) ? (($in_Perms & 0x0400) ? 's' : 'x' ) : - (($in_Perms & 0x0400) ? 'S' : '-')); - - // world - $sP .= (($in_Perms & 0x0004) ? 'r' : '-') . - (($in_Perms & 0x0002) ? 'w' : '-') . - (($in_Perms & 0x0001) ? (($in_Perms & 0x0200) ? 't' : 'x' ) : - (($in_Perms & 0x0200) ? 'T' : '-')); - return($sP); - }//end translate_perms() - //======================================================================================== - - - //======================================================================================== - /** - * Creates an empty file... think of the linux command "touch". - * - * @param $filename (string) filename to create. - * - * @return 0 (FAIL) unable to create file. - * @return 1 (PASS) file created successfully. - */ - public function create_file($filename, $truncateFile=FALSE) { - - $retval = 0; - $filename = $this->filename2absolute($filename); - $filename = $this->resolve_path_with_dots($filename); - $this->filename = $filename; - - //check to see if the file exists... - if(!file_exists($filename)) { - if($this->is_writable(dirname($filename))) { - //no file. Create it. - $createFileRes = touch($filename); - if($createFileRes) { - $retval = 1; - } - else { - throw new exception(__METHOD__ .": invalid return from touch(". $filename ."), return was (". $createFileRes .")"); - } - } - else { - throw new exception(__METHOD__ .": directory (". dirname($filename) .") is not writable"); - } - } - elseif($truncateFile === TRUE) { - $this->truncate_file($filename); - } - else { - throw new exception(__METHOD__ .": file (". $filename .") exists and truncate not set"); - } - return($retval); - }//end create_file() - //======================================================================================== - - - - //======================================================================================== - /** - * Opens a stream/resource handle to use for doing file i/o. - * - * @param $filename (string) filename to open: should be relative to the current dir. - * @param $mode (string) mode to use: consult PHP.net's info for fopen(). - * - * @return 0 (FAIL) unable to open file. - * @return 1 (PASS) file opened successfully. - */ - public function openFile($filename=NULL, $mode="r+") { - clearstatcache(); - if(!strlen($filename) || is_null($filename)) { - $filename = $this->filename; - } - $filename = $this->filename2absolute($filename); - $this->filename = $filename; - - if($this->is_readable($filename)) { - //make sure we've got a mode to use. - - if(!is_string($mode)) { - $mode = "r+"; - } - $this->mode = $mode; - - if(in_array($this->mode, array("r+", "w", "w+", "a", "a+", "x", "x+")) && !$this->is_writable($filename)) { - throw new exception(__METHOD__ .": file is not writable (". $filename .") (". $this->is_writable($filename) ."), mode=(". $this->mode .")"); - } - - //attempt to open a stream to a file... - $this->fh = fopen($this->filename, $this->mode); - if(is_resource($this->fh)) { - //looks like we opened successfully. - $retval = 1; - $this->lineNum = 0; - } else { - //something bad happened. - $retval = 0; - } - } - else { - throw new exception(__METHOD__ .": file is unreadable (". $filename .")"); - } - - return($retval); - }//end openFile() - //======================================================================================== - - - //======================================================================================== - /** - * Write the given contents into the current file or the filename given. - * - * @param $content (str) Content to write into the file. - * @param $filename (str,optional) filename to use. If none given, the current one is used. - * - * @return 0 (FAIL) unable to write content to the file. - * @return (n) (PASS) wrote (n) bytes. - */ - public function write($content, $filename=NULL) { - - //open the file for writing. - if(!$filename) { - $filename= $this->filename; - } - $this->filename = $filename; - - //open the file... - $openResult = $this->openFile($this->filename, $this->mode); - - //looks like we made it... - $retval = fwrite($this->fh, $content, strlen($content)); - - //done... return the result. - return($retval); - }//end write() - //======================================================================================== - - - //======================================================================================== - /** - * Takes the given filename & returns the ABSOLUTE pathname: checks to see if the given - * string already has the absolute path in it. - */ - private function filename2absolute($filename) { - - clearstatcache(); - - $filename = $this->resolve_path_with_dots($filename); - - //see if it starts with a "/"... - if(preg_match("/^\//", $filename)) { - $retval = $filename; - } else { - $retval=$this->realcwd .'/'. $filename; - $retval = $this->resolve_path_with_dots($retval); - #debug_print(__METHOD__ .": realcwd=(". $this->realcwd .")"); - #$this->resolve_path_with_dots($retval); - } - - if(!$this->check_chroot($retval, FALSE)) { - $this->gfObj->debug_print(func_get_args()); - throw new exception(__METHOD__ .": file is outside of allowed directory (". $retval .")"); - } - - return($retval); - - }//end filename2absolute() - //======================================================================================== - - - - //======================================================================================== - /** - * Reads-in the contents of an entire file. - */ - function read($filename, $returnArray=FALSE) { - $myFile = $this->filename2absolute($filename); - if(!file_exists($myFile)) { - throw new exception(__METHOD__ .": file doesn't exist (". $myFile .")"); - } - elseif($this->is_readable($myFile)) { - if($returnArray) { - $data = file($myFile); - } - else { - $data = file_get_contents($myFile); - } - - if($data === FALSE) { - throw new exception(__METHOD__. ": file_get_contents() returned FALSE"); - } - } - else { - throw new exception(__METHOD__. ": File isn't readable (". $myFile .")"); - } - return($data); - }//end read() - //======================================================================================== - - - - //======================================================================================== - public function rm($filename) { - $filename = $this->filename2absolute($filename); - return(unlink($filename)); - }//end rm() - //======================================================================================== - - - - //======================================================================================== - public function rmdir($dirname) { - $dirname = $this->filename2absolute($dirname); - return(rmdir($dirname)); - }//end rm() - //======================================================================================== - - - - //======================================================================================== - /** - * Return the next line for a file. - * - * When the end of the file is found, this method returns FALSE (returning NULL might be - * misconstrued as a blank line). - */ - public function get_next_line($maxLength=NULL, $trimLine=TRUE) { - if(is_resource($this->fh) && get_resource_type($this->fh) == 'stream') { - if(feof($this->fh)) { - $retval = FALSE; - } - else { - if(!is_numeric($maxLength)) { - $retval = @fgets($this->fh); - } - else { - $retval = fgets($this->fh, $maxLength); - } - if($trimLine) { - $retval = trim($retval); - } - - if(is_null($this->lineNum) || !is_numeric($this->lineNum) || $this->lineNum < 0) { - throw new exception(__METHOD__ .": invalid data for lineNum (". $this->lineNum .")"); - } - $this->lineNum++; - } - } - else { - throw new exception(__METHOD__ .": invalid filehandle"); - } - - return($retval); - }//end get_next_line() - //======================================================================================== - - - - //======================================================================================== - public function append_to_file($data, $eolChar="\n") { - $retval = FALSE; - if(is_resource($this->fh)) { - $result = fwrite($this->fh, $data . $eolChar); - if($result === FALSE) { - throw new exception(__METHOD__ .": failed to write data to file"); - } - else { - $this->lineNum++; - $retval = $result; - } - } - else { - throw new exception(__METHOD__ .": invalid filehandle"); - } - - return($retval); - }//end append_to_file() - //======================================================================================== - - - - //======================================================================================== - public function closeFile() { - $retval = FALSE; - if(is_resource($this->fh)) { - fclose($this->fh); - $retval = TRUE; - } - - //reset internal pointers. - $this->filename = NULL; - $this->lineNum = NULL; - - return($retval); - }//end closeFile() - //======================================================================================== - - - - //======================================================================================== - /** - * Compare the given filename to the open filename to see if they match (using this allows - * giving a filename instead of comparing the whole path). - */ - public function compare_open_filename($compareToFilename) { - if(!strlen($compareToFilename) || is_null($compareToFilename)) { - throw new exception(__METHOD__ .": invalid filename to compare"); - } - elseif(!strlen($this->filename)) { - $retval = FALSE; - } - else { - $internalFilename = $this->filename2absolute($this->filename); - $compareToFilename = $this->filename2absolute($compareToFilename); - if($internalFilename == $compareToFilename) { - $retval = TRUE; - } - else { - $retval = FALSE; - } - } - - return($retval); - }//end compare_open_filename() - //======================================================================================== - - - - //======================================================================================== - /** - * Give a file a new name. - * - * TODO: check to make sure both files exist within our root. - */ - public function rename($currentFilename, $newFilename) { - if($newFilename == $currentFilename) { - $this->gfObj->debug_print(func_get_args()); - throw new exception(__METHOD__ .": renaming file to same name"); - } - - if($this->compare_open_filename($currentFilename)) { - $this->closeFile(); - } - - if($this->compare_open_filename($newFilename)) { - //renaming a different file to our currently open file... - $this->gfObj->debug_print(func_get_args()); - throw new exception(__METHOD__ .": renaming another file (". $currentFilename .") to the currently open filename (". $newFilename .")"); - } - else { - - $currentFilename = $this->filename2absolute($currentFilename); - $newFilename = $this->filename2absolute($newFilename); - - if(!$this->is_writable(dirname($newFilename))) { - throw new exception(__METHOD__ .": directory isn't writable... "); - } - $retval = rename($currentFilename, $newFilename); - if($retval !== TRUE) { - throw new exception(__METHOD__ .": failed to rename file (". $retval .")"); - } - } - - return($retval); - - }//end rename() - //======================================================================================== - - - - //======================================================================================== - /** - * Check if the given filename is executable. - */ - public function is_executable($filename) { - $filename = $this->filename2absolute($filename); - $retval = FALSE; - if(strlen($filename)) { - $retval = is_executable($filename); - } - - return($retval); - }//end is_executable() - //======================================================================================== - - - - //======================================================================================== - /** - * Check if the given filename is readable. - */ - public function is_readable($filename) { - $filename = $this->filename2absolute($filename); - $retval = FALSE; - if(strlen($filename)) { - $retval = is_readable($filename); - } - - return($retval); - }//end is_readable() - //======================================================================================== - - - - //======================================================================================== - /** - * Check if the given filename/path is writable - */ - public function is_writable($filenameOrPath) { - $filenameOrPath = $this->filename2absolute($filenameOrPath); - $retval = FALSE; - if(strlen($filenameOrPath)) { - $retval = is_writable($filenameOrPath); - } - - return($retval); - }//end is_writable() - //======================================================================================== - - - - //======================================================================================== - /** - * Determines how many lines are left in the current file. - */ - public function count_remaining_lines() { - if(is_resource($this->fh) && get_resource_type($this->fh) == 'stream') { - $originalLineNum = $this->lineNum; - - $myFilename = $this->filename; - $myNextLine = $this->get_next_line(); - $retval = 0; - while($myNextLine !== FALSE) { - $retval++; - $myNextLine = $this->get_next_line(); - } - - $this->closeFile(); - $this->openFile($myFilename, $this->mode); - - if($originalLineNum > 0) { - while($originalLineNum > $this->lineNum) { - $this->get_next_line(); - } - } - - if($this->lineNum !== $originalLineNum) { - throw new exception(__METHOD__ .": failed to match-up old linenum (". $originalLineNum .") with the current one (". $this->lineNum .")"); - } - } - else { - throw new exception(__METHOD__ .": Invalid filehandle, can't count remaining lines"); - } - - return($retval); - }//end count_remaining_files() - //======================================================================================== - - - - //======================================================================================== - /** - * Moves the cursor to the given line number. - * - * NOTE: remember if you're trying to get line #1 (literally), then you'll - * want to go to line #0, then call get_next_line() to retrieve it... this - * is the traditional logical vs. programatic numbering issue. A.k.a. the - * "off-by-one problem". - */ - public function go_to_line($lineNum) { - $retval = FALSE; - if(is_resource($this->fh) && get_resource_type($this->fh) == 'stream') { - if($this->lineNum > $lineNum) { - //gotta "rewind" the cursor back to the beginning. - rewind($this->fh); - $this->lineNum=0; - } - - if($lineNum == $this->lineNum) { - $retval = TRUE; - } - elseif($this->lineNum < $lineNum) { - while($this->lineNum < $lineNum) { - //don't grab any data, just move the cursor... - $this->get_next_line(); - } - if($this->lineNum == $lineNum) { - $retval = TRUE; - } - else { - throw new exception(__METHOD__ .": couldn't reach the line (". $lineNum ."), failed at (". $this->lineNum .")"); - } - } - else { - throw new exception(__METHOD__ .": internal lineNum (". $this->lineNum .") couldn't be retrieved or reset to (". $lineNum .")"); - } - } - - return($retval); - }//end go_to_line() - //======================================================================================== - - - - //======================================================================================== - /** - * Fix a path that contains "../". - * - * EXAMPLE: changes "/home/user/blah/blah/../../test" into "/home/user/test" - */ - public function resolve_path_with_dots($path) { - - while(preg_match('/\/\//', $path)) { - $path = preg_replace('/\/\//', '/', $path); - } - $retval = $path; - if(strlen($path) && preg_match('/\./', $path)) { - - $isAbsolute = FALSE; - if(preg_match('/^\//', $path)) { - $isAbsolute = TRUE; - $path = preg_replace('/^\//', '', $path); - } - $pieces = explode('/', $path); - - $finalPieces = array(); - for($i=0; $i < count($pieces); $i++) { - $dirName = $pieces[$i]; - if($dirName == '.') { - //do nothing; don't bother appending. - } - elseif($dirName == '..') { - $rippedIndex = array_pop($finalPieces); - } - else { - $finalPieces[] = $dirName; - } - } - - $retval = $this->gfObj->string_from_array($finalPieces, NULL, '/'); - if($isAbsolute) { - $retval = '/'. $retval; - } - } - - return($retval); - }//end resolve_path_with_dots() - //======================================================================================== - - - - //======================================================================================== - private function check_chroot($path, $translatePath=TRUE) { - if($translatePath === TRUE) { - $path = $this->filename2absolute($path); - } - - //now, let's go through the root directory structure, & make sure $path is within that. - $rootPieces = explode('/', $this->root); - $pathPieces = explode('/', $path); - - - if($rootPieces[0] == '') { - array_shift($rootPieces); - } - if($rootPieces[(count($rootPieces) -1)] == '') { - array_pop($rootPieces); - } - if($pathPieces[0] == '') { - array_shift($pathPieces); - } - - $retval = TRUE; - $tmp = ''; - foreach($rootPieces as $index=>$dirName) { - $pathDir = $pathPieces[$index]; - if($pathDir != $dirName) { - $retval = FALSE; - $this->gfObj->debug_print(__METHOD__ .": failed... tmp=(". $tmp ."), dirName=(". $dirName .")"); - break; - } - $tmp = $this->gfObj->create_list($tmp, $dirName, '/'); - } - - return($retval); - }//end check_chroot() - //======================================================================================== - - - - //======================================================================================== - public function copy_file($filename, $destination) { - $retval = FALSE; - if($this->openFile($filename)) { - if($this->check_chroot($destination)) { - //okay, try to copy. - $retval = copy($this->fh, $destination); - } - else { - throw new exception(__METHOD__ .':: destination is not in the directory path'); - } - } - - return($retval); - }//end copy_file() - //======================================================================================== - - - - //======================================================================================== - public function move_file($filename, $destination) { - $retval = FALSE; - if($this->is_readable($filename)) { - if($this->check_chroot($destination)) { - //do the move. - $retval = rename($filename, $destin... [truncated message content] |
From: <cra...@us...> - 2009-01-29 21:15:25
|
Revision: 334 http://cs-content.svn.sourceforge.net/cs-content/?rev=334&view=rev Author: crazedsanity Date: 2009-01-29 21:15:20 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Fix references from cs_genericPageClass.php to cs_genericPage.class.php. /contentSystem.class.php: * MAIN::: -- fix include to point to new cs_genericPage{} class file /cs_genericPage.class.php [RENAMED FROM cs_genericPageClass.php] /cs_genericPageClass.php [RENAMED TO cs_genericPage.class.php] Modified Paths: -------------- trunk/1.0/contentSystem.class.php Added Paths: ----------- trunk/1.0/cs_genericPage.class.php Removed Paths: ------------- trunk/1.0/cs_genericPageClass.php Modified: trunk/1.0/contentSystem.class.php =================================================================== --- trunk/1.0/contentSystem.class.php 2009-01-29 21:04:18 UTC (rev 333) +++ trunk/1.0/contentSystem.class.php 2009-01-29 21:15:20 UTC (rev 334) @@ -73,7 +73,7 @@ require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); require_once(dirname(__FILE__) ."/cs_fileSystem.class.php"); require_once(dirname(__FILE__) ."/cs_sessionClass.php"); -require_once(dirname(__FILE__) ."/cs_genericPageClass.php"); +require_once(dirname(__FILE__) ."/cs_genericPage.class.php"); require_once(dirname(__FILE__) ."/cs_tabsClass.php"); class contentSystem extends cs_contentAbstract { Copied: trunk/1.0/cs_genericPage.class.php (from rev 331, trunk/1.0/cs_genericPageClass.php) =================================================================== --- trunk/1.0/cs_genericPage.class.php (rev 0) +++ trunk/1.0/cs_genericPage.class.php 2009-01-29 21:15:20 UTC (rev 334) @@ -0,0 +1,619 @@ +<?php +/* + * FILE INFORMATION: + * $HeadURL$ + * $Id$ + * $LastChangedDate$ + * $LastChangedBy$ + * $LastChangedRevision$ + */ +require_once(dirname(__FILE__) ."/template.inc"); +require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); + +class cs_genericPage extends cs_contentAbstract { + var $templateObj; //template object to parse the pages + var $templateVars = array(); //our copy of the global templateVars + var $mainTemplate; //the default layout of the site + + private $tmplDir; + private $libDir; + private $siteRoot; + private $allowRedirect; + + private $showEditableLink = FALSE; + + private $allowInvalidUrls=NULL; + + //--------------------------------------------------------------------------------------------- + /** + * The constructor. + */ + public function __construct($restrictedAccess=TRUE, $mainTemplateFile=NULL, $allowRedirect=TRUE) { + //handle some configuration. + $this->allowRedirect = $allowRedirect; + + //initialize stuff from our parent... + parent::__construct(); + + //initialize some internal stuff. + $this->initialize_locals($mainTemplateFile); + + //if they need to be logged-in... + $this->check_login($restrictedAccess); + + if(!defined('CS-CONTENT_SESSION_NAME')) { + define("CS-CONTENT_SESSION_NAME", ini_get('session.name')); + } + + //TODO: if the end page doesn't want to allow the "edit" links, will this still work? + if(defined("CS_CONTENT_MODIFIABLE") && constant("CS_CONTENT_MODIFIABLE") === TRUE) { + $this->showEditableLink = TRUE; + } + }//end __construct() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Initializes some internal objects, variables, and so on. + */ + protected function initialize_locals($mainTemplateFile) { + + //NOTE: this **requires** that the global variable "SITE_ROOT" is already set. + $this->siteRoot = preg_replace('/\/public_html/', '', $_SERVER['DOCUMENT_ROOT']); + $this->tmplDir = $this->siteRoot .'/templates'; + $this->libDir = $this->siteRoot .'/lib'; + + //if there have been some global template vars (or files) set, read 'em in here. + if(is_array($GLOBALS['templateVars']) && count($GLOBALS['templateVars'])) { + foreach($GLOBALS['templateVars'] as $key=>$value) { + $this->add_template_var($key, $value); + } + } + if(isset($GLOBALS['templateFiles']) && is_array($GLOBALS['templateFiles'])) { + foreach($GLOBALS['templateFiles'] as $key => $value) { + $this->templateFiles[$key] = $value; + } + } + unset($GLOBALS['templateVars'], $GLOBALS['templateFiles']); + + //build a new instance of the template library (from PHPLib) + $this->templateObj=new Template($this->tmplDir,"keep"); //initialize a new template parser + + if(preg_match('/^\//', $mainTemplateFile)) { + $mainTemplateFile = $this->tmplDir ."/". $mainTemplateFile; + } + $this->mainTemplate=$mainTemplateFile; //load the default layout + }//end initialize_locals() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Should just check to see if they've authenticated. In reality, this + * just performs blind redirection if $restrictedAccess is set (and if + * redirecting is allowed). + */ + public function check_login($restrictedAccess) { + if($restrictedAccess) { + $myUri = $_SERVER['SCRIPT_NAME']; + $doNotRedirectArr = array('/login.php', '/admin/login.php', '/index.php', '/admin.php', + '/content', '/content/index.php' + ); + $myUrlString=""; + $myGetArr = $_GET; + if(is_array($myGetArr) && count($myGetArr) > 0) { + unset($myGetArr['PHPSESSID']); + unset($myGetArr[CS-CONTENT_SESSION_NAME]); + $myUrlString = string_from_array($myGetArr, NULL, 'url'); + } + + //TODO: make the redirectHere variable dynamic--an argument, methinks. + $redirectHere = '/login.php?destination='. $myUrlString; + + //Not exitting after conditional_header() is... bad, m'kay? + $this->conditional_header($redirectHere, TRUE); + exit; + } + }//end check_login() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Remove all data from the special template var "content" (or optionally another ver). + * + * @param $section (str,optional) defines what template var to wip-out. + * @return (NULL) + */ + public function clear_content($section="content"){ + $this->change_content(" ",$section); + }//end clear_content() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Change the content of a template to the given data. + * + * @param $htmlString (str) data to use. + * @param $section (str,optional) define a different section. + * + * @return (NULL) + */ + public function change_content($htmlString,$section="content"){ + $this->templateVars[$section] = $htmlString; + }//end change_content() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Adds a template file (with the given handle) to be parsed. + * + * TODO: check if $fileName exists before blindly trying to parse it. + */ + public function add_template_file($handleName, $fileName){ + if($this->showEditableLink) { + $prefix = '[<a href="#NULL_page='. $fileName .'"><font color="red"><b>Edit "'. $handleName .'"</b></font></a>]<BR>'; + $this->add_template_var($handleName, $prefix .$this->file_to_string($fileName)); + } + else { + $this->add_template_var($handleName, $this->file_to_string($fileName)); + } + }//end add_template_file() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Adds a value for a template placeholder. + */ + public function add_template_var($varName, $varValue){ + $this->templateVars[$varName]=$varValue; + }//end add_template_var(); + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Rips the given block row name from the parent templateVar, (optionally) replacing it with + * a template var of the same name. + * + * @param $parent (str) name of the templateVar to pull the block row from. + * @param $handle (str) name of the block row to rip from it's parent. + * @param $removeDefs (bool,optional) if evaluated as FALSE, no template var is left in + * place of the block row. + * + * @return (bool false) FAIL: unable to find block row. + * @return (str) PASS: content of the block row. + */ + public function set_block_row($parent, $handle, $removeDefs=0) { + $name = $handle; + $str = $this->templateVars[$parent]; + + $reg = "/<!-- BEGIN $handle -->.+<!-- END $handle -->/sU"; + preg_match_all($reg, $str, $m); + if(!is_string($m[0][0])) { + #exit("set_block_row(): couldn't find '$handle' in var '$parent'"); + $retval = FALSE; + } else { + if($removeDefs) { + $openHandle = "<!-- BEGIN $handle -->"; + $endHandle = "<!-- END $handle -->"; + $m[0][0] = str_replace($openHandle, "", $m[0][0]); + $m[0][0] = str_replace($endHandle, "", $m[0][0]); + } + + $str = preg_replace($reg, "{" . "$name}", $str); + $this->templateVars[$parent] = $str; + $this->templateRows[$name] = $m[0][0]; + $this->add_template_var($name, ""); + $retval = $m[0][0]; + } + return($retval); + }//end set_block_row() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Using the given template, it will replace each index (in $repArr) with it's value: each + * var to be replaced must begin the given begin & end delimiters. + * + * @param $template (str) Data to perform the replacements on. + * @param $repArr (array) Array of name=>value pairs, where name is to be replaced with value. + * @param $b (str,optional) beginning delimiter. + * @param $e (str,optional) ending delimiter. + */ + public function mini_parser($template, $repArr, $b='%%', $e='%%') { + return($this->gfObj->mini_parser($template, $repArr, $b, $e)); + }//end mini_parser() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Processes all template vars & files, etc, to produce the final page. NOTE: it is a wise idea + * for most pages to have this called *last*. + * + * @param $stripUndefVars (bool,optional) Remove all undefined template vars. + * + * @return (str) Final, parsed page. + */ + public function print_page($stripUndefVars=1) { + //Show any available messages. + $this->process_set_message(); + + //Load the default page layout. + $this->templateObj->set_file("main", $this->mainTemplate); + + //load the placeholder names and thier values + $this->templateObj->set_var($this->templateVars); + $this->templateObj->parse("out","main"); //parse the sub-files into the main page + + if($stripUndefVars) { + $numLoops = 0; + while(preg_match_all('/\{.\S+?\}/', $this->templateObj->varvals['out'], $tags) && $numLoops < 50) { + $tags = $tags[0]; + + //TODO: figure out why this works when running it twice. + foreach($tags as $key=>$str) { + $str2 = str_replace("{", "", $str); + $str2 = str_replace("}", "", $str2); + if(!$this->templateVars[$str2] && $stripUndefVars) { + //TODO: set an internal pointer or something to use here, so they can see what was missed. + $this->templateObj->varvals[out] = str_replace($str, '', $this->templateObj->varvals[out]); + } + } + $this->templateObj->parse("out", "out"); + $numLoops++; + } + } + $this->templateObj->pparse("out","out"); //parse the main page + + }//end of print_page() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Handles a message that was set into the session. + */ + public function process_set_message() { + //if there's not a message set, skip. + $errorBox = ""; + if(isset($_SESSION['message']) && is_array($_SESSION['message'])) { + $errorBox = $this->file_to_string("system/message_box.tmpl"); + //let's make sure the "type" value is *lowercase*. + $_SESSION['message']['type'] = strtolower($_SESSION['message']['type']); + + //WARNING::: if you give it the wrong type, it'll STILL be parsed. Otherwise + // this has to match set_message() FAR too closely. And it's a pain. + $_SESSION['message']['messageType'] = $_SESSION['message']['type']; + $errorBox = $this->gfObj->mini_parser($errorBox, $_SESSION['message'], '{', '}'); + if($_SESSION['message']['type'] == "fatal") { + //replace content of the page with our error. + $this->change_content($errorBox); + } else { + //Non-fatal: put it into a template var. + $this->add_template_var("error_msg", $errorBox); + } + + //now that we're done displaying the message, let's get it out of the session (otherwise + // they'll never get past this point). + unset($_SESSION['message']); + } + return($errorBox); + }//end of process_set_message() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Takes a template file, whose root must be within $GLOBALS['TMPLDIR'], pulls it's + * content & returns it. + */ + public function file_to_string($templateFileName) { + $templateFileName = preg_replace('/\/\//', '\/', $templateFileName); + if($this->template_file_exists($templateFileName)) { + $retval = file_get_contents($this->tmplDir .'/'. $templateFileName); + } else { + $this->set_message_wrapper(array( + "title" => 'Template File Error', + "message" => 'Not all templates could be found for this page.', + "type" => 'error' + )); + } + return($retval); + }//end file_to_string() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Checks to see if the given filename exists within the template directory. + */ + public function template_file_exists($file) { + $retval = 0; + //If the string doesn't start with a /, add one + if (strncmp("/",$file,1)) { + //strncmp returns 0 if they match, so we're putting a / on if they don't + $file="/".$file; + } + $filename=$this->tmplDir.$file; + + if(file_exists($filename)) { + $retval = $filename; + } + return($retval); + }//end template_file_exists() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Creates an array in the session, used by the templating system as a way to + * get messages generated by the code into the page, without having to add + * special templates & such each time. + * + * @param $title (str) the title of the message. + * @param $message (str) text beneath the title. + * @param $linkURL (str,optional) URL for the link below the message. + * @param $type (str) notice/status/error/fatal message, indicating + * it's importance. Generally, fatal messages + * cause only the message to be shown. + * @param $linkText (str,optional) text that the link wraps. + * @param $overwriteSame (bool,optional) whether setting a message which has + * the same type as an already set message will + * overwite the previous. More important messages + * always overwrite lesser ones. + * @param $priority (int,optional) specify the message's priority. + * + * @return (bool) Indicates pass (true)/fail (false) + */ + function set_message($title=NULL, $message=NULL, $linkURL=NULL, $type=NULL, $linkText=NULL, $overwriteSame=NULL, $priority=NULL) { + if(!isset($overwriteSame)) { + $overwriteSame = 1; + } + + //defines the importance level of each type of message: the higher the value, the more important it is. + $priorityArr = array( + 'notice' => 10, + 'status' => 20, + 'error' => 30, + 'fatal' => 100 + ); + if(!isset($type) || !isset($priorityArr[$type])) { + //set a default type. + $arrayKeys = array_keys(); + $type = $arrayKeys[0]; + } + + if(strlen($linkURL)) { + if(!strlen($linkText) || is_null($linkText)) { + $linkText = "Link"; + } + $redirectText = '<a href="'. $linkURL .'">'. $linkText .'</a>'; + } + + //Create the array. + $myArr = array( + "title" => $title, + "message" => $message, + "redirect" => $redirectText, + "type" => $type, + "priority" => $priority + + ); + + //make sure the message type is IN the priority array... + if(!in_array($type, array_keys($priorityArr))) { + //invalid type. + $retval = FALSE; + } elseif($_SESSION['message']) { + //there's already a message... check if the new one should overwrite the existing. + if((!$overwriteSame) AND ($priorityArr[$_SESSION['message']['type']] == $priorityArr[$type])) { + //no overwriting. + $retval = FALSE; + } elseif($priorityArr[$_SESSION['message']['type']] <= $priorityArr[$type]) { + // the existing message is less important. Overwrite it. + $_SESSION['message'] = $myArr; + } + } + else { + $_SESSION['message'] = $myArr; + $retval = TRUE; + } + + return($retval); + + } // end of set_message() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + public function set_message_wrapper($array) { + @$this->set_message( + $array['title'], + $array['message'], + $array['linkURL'], + $array['type'], + $array['linkText'], + $array['overwriteSame'], + $array['priority'] + ); + }//end set_message_wrapper() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Performs redirection, provided it is allowed. + */ + function conditional_header($url, $exitAfter=TRUE) { + if($this->allowRedirect) { + //checks to see if headers were sent; if yes: use a meta redirect. + // if no: send header("location") info... + if(headers_sent()) { + //headers sent. Use the meta redirect. + print " + <HTML> + <HEAD> + <TITLE>Redirect Page</TITLE> + <META HTTP-EQUIV='refresh' content='0; URL=$url'> + </HEAD> + <a href=\"$url\"></a> + </HTML> + "; + } + else { + header("location:$url"); + } + + if($exitAfter) { + //redirecting without exitting is bad, m'kay? + exit; + } + } + else { + //TODO: should an exception be thrown, or maybe exit here anyway? + throw new exception(__METHOD__ .": auto redirects not allowed...?"); + } + }//end conditional_header() + //--------------------------------------------------------------------------------------------- + + + //--------------------------------------------------------------------------------------------- + /** + * Given the name of a templateVar (defaults to "content"), this method will retrieve all block + * row definitions in the order that they can safely be ripped-out/set by set_block_row(). + * + * @parm $templateVar (str,optional) templateVar to parse. + * + * @return (array) Contains all rows, complete & incomplete. + * + * NOTE: Layout of array::: + * array( + * incomplete => array( + * begin => array(), + * end => array() + * ), + * ordered => array() + * NOTE2: each "array()" above is a list of block row names. + * NOTE3: failure to parse the template will return a blank array. + * + * + */ + function get_block_row_defs($templateVar="content") { + //cast $retArr as an array, so it's clean. + $retArr = array(); + + //NOTE: the value 31 isn't just a randomly chosen length; it's the minimum + // number of characters to have a block row. EG: "<!-- BEGIN x -->o<!-- END x -->" + $templateContents = $this->templateVars[$templateVar]; + if(strlen($templateContents) >= 31) { + //looks good to me. Run the regex... + $flags = PREG_PATTERN_ORDER; + $reg = "/<!-- BEGIN (.+) -->/"; + preg_match_all($reg, $templateContents, $beginArr, $flags); + $beginArr = $beginArr[1]; + + $endReg = "/<!-- END (.+) -->/"; + preg_match_all($endReg, $templateContents, $endArr, $flags); + $endArr = $endArr[1]; + + //create a part of the array that shows any orphaned "BEGIN" statements (no matching "END" + // statement), and orphaned "END" statements (no matching "BEGIN" statements) + // NOTE::: by doing this, should easily be able to tell if the block rows were defined + // properly or not. + if(count($retArr['incomplete']['begin'] = array_diff($beginArr, $endArr)) > 0) { + //I'm sure there's an easier way to do this, but my head hurts too much when + // I try to do the magic. Maybe I need to put another level in CodeMancer... + foreach($retArr['incomplete']['begin'] as $num=>$val) { + unset($beginArr[$num]); + } + } + if(count($retArr['incomplete']['end'] = array_diff($endArr, $beginArr)) > 0) { + //both of the below foreach's simply pulls undefined vars out of the + // proper arrays, so I don't have to deal with them later. + foreach($retArr['incomplete']['end'] as $num=>$val) { + unset($endArr[$num]); + } + } + + //YAY!!! we've got valid data!!! + //reverse the order of the array, so when the ordered array + // is looped through, all block rows can be pulled. + $retArr['ordered'] = array_reverse($beginArr); + } else { + //nothin' doin'. Return a blank array. + $retArr = array(); + } + + return($retArr); + }//end get_block_row_defs() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + function rip_all_block_rows($templateVar="content", $exceptionArr=array()) { + $rowDefs = $this->get_block_row_defs($templateVar); + + $useTheseBlockRows = $rowDefs['ordered']; + $retval = array(); + if(is_array($useTheseBlockRows)) { + foreach($useTheseBlockRows as $blockRowName) + { + if(!in_array($blockRowName, $exceptionArr)) + { + //remove the block row. + $rowData = $this->set_block_row($templateVar, $blockRowName); + $retval[$blockRowName] = $rowData; + } + } + } + + return($retval); + }//end rip_all_block_rows() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + public function set_all_block_rows($templateVar="content", $exceptionArr=array()) + { + $retval = $this->rip_all_block_rows($templateVar, $exceptionArr); + return($retval); + }//end set_all_block_rows() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + public function allow_invalid_urls($newSetting=NULL) { + if(!is_null($newSetting) && is_bool($newSetting)) { + $this->allowInvalidUrls = $newSetting; + } + return($this->allowInvalidUrls); + }//end allow_invalid_urls() + //--------------------------------------------------------------------------------------------- + +}//end cs_genericPage{} +?> Property changes on: trunk/1.0/cs_genericPage.class.php ___________________________________________________________________ Added: svn:executable + * Added: svn:keywords + HeadURL Id LastChangedBy LastChangedDate LastChangedRevision Added: svn:mergeinfo + Added: svn:eol-style + native Deleted: trunk/1.0/cs_genericPageClass.php =================================================================== --- trunk/1.0/cs_genericPageClass.php 2009-01-29 21:04:18 UTC (rev 333) +++ trunk/1.0/cs_genericPageClass.php 2009-01-29 21:15:20 UTC (rev 334) @@ -1,619 +0,0 @@ -<?php -/* - * FILE INFORMATION: - * $HeadURL$ - * $Id$ - * $LastChangedDate$ - * $LastChangedBy$ - * $LastChangedRevision$ - */ -require_once(dirname(__FILE__) ."/template.inc"); -require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); - -class cs_genericPage extends cs_contentAbstract { - var $templateObj; //template object to parse the pages - var $templateVars = array(); //our copy of the global templateVars - var $mainTemplate; //the default layout of the site - - private $tmplDir; - private $libDir; - private $siteRoot; - private $allowRedirect; - - private $showEditableLink = FALSE; - - private $allowInvalidUrls=NULL; - - //--------------------------------------------------------------------------------------------- - /** - * The constructor. - */ - public function __construct($restrictedAccess=TRUE, $mainTemplateFile=NULL, $allowRedirect=TRUE) { - //handle some configuration. - $this->allowRedirect = $allowRedirect; - - //initialize stuff from our parent... - parent::__construct(); - - //initialize some internal stuff. - $this->initialize_locals($mainTemplateFile); - - //if they need to be logged-in... - $this->check_login($restrictedAccess); - - if(!defined('CS-CONTENT_SESSION_NAME')) { - define("CS-CONTENT_SESSION_NAME", ini_get('session.name')); - } - - //TODO: if the end page doesn't want to allow the "edit" links, will this still work? - if(defined("CS_CONTENT_MODIFIABLE") && constant("CS_CONTENT_MODIFIABLE") === TRUE) { - $this->showEditableLink = TRUE; - } - }//end __construct() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Initializes some internal objects, variables, and so on. - */ - protected function initialize_locals($mainTemplateFile) { - - //NOTE: this **requires** that the global variable "SITE_ROOT" is already set. - $this->siteRoot = preg_replace('/\/public_html/', '', $_SERVER['DOCUMENT_ROOT']); - $this->tmplDir = $this->siteRoot .'/templates'; - $this->libDir = $this->siteRoot .'/lib'; - - //if there have been some global template vars (or files) set, read 'em in here. - if(is_array($GLOBALS['templateVars']) && count($GLOBALS['templateVars'])) { - foreach($GLOBALS['templateVars'] as $key=>$value) { - $this->add_template_var($key, $value); - } - } - if(isset($GLOBALS['templateFiles']) && is_array($GLOBALS['templateFiles'])) { - foreach($GLOBALS['templateFiles'] as $key => $value) { - $this->templateFiles[$key] = $value; - } - } - unset($GLOBALS['templateVars'], $GLOBALS['templateFiles']); - - //build a new instance of the template library (from PHPLib) - $this->templateObj=new Template($this->tmplDir,"keep"); //initialize a new template parser - - if(preg_match('/^\//', $mainTemplateFile)) { - $mainTemplateFile = $this->tmplDir ."/". $mainTemplateFile; - } - $this->mainTemplate=$mainTemplateFile; //load the default layout - }//end initialize_locals() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Should just check to see if they've authenticated. In reality, this - * just performs blind redirection if $restrictedAccess is set (and if - * redirecting is allowed). - */ - public function check_login($restrictedAccess) { - if($restrictedAccess) { - $myUri = $_SERVER['SCRIPT_NAME']; - $doNotRedirectArr = array('/login.php', '/admin/login.php', '/index.php', '/admin.php', - '/content', '/content/index.php' - ); - $myUrlString=""; - $myGetArr = $_GET; - if(is_array($myGetArr) && count($myGetArr) > 0) { - unset($myGetArr['PHPSESSID']); - unset($myGetArr[CS-CONTENT_SESSION_NAME]); - $myUrlString = string_from_array($myGetArr, NULL, 'url'); - } - - //TODO: make the redirectHere variable dynamic--an argument, methinks. - $redirectHere = '/login.php?destination='. $myUrlString; - - //Not exitting after conditional_header() is... bad, m'kay? - $this->conditional_header($redirectHere, TRUE); - exit; - } - }//end check_login() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Remove all data from the special template var "content" (or optionally another ver). - * - * @param $section (str,optional) defines what template var to wip-out. - * @return (NULL) - */ - public function clear_content($section="content"){ - $this->change_content(" ",$section); - }//end clear_content() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Change the content of a template to the given data. - * - * @param $htmlString (str) data to use. - * @param $section (str,optional) define a different section. - * - * @return (NULL) - */ - public function change_content($htmlString,$section="content"){ - $this->templateVars[$section] = $htmlString; - }//end change_content() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Adds a template file (with the given handle) to be parsed. - * - * TODO: check if $fileName exists before blindly trying to parse it. - */ - public function add_template_file($handleName, $fileName){ - if($this->showEditableLink) { - $prefix = '[<a href="#NULL_page='. $fileName .'"><font color="red"><b>Edit "'. $handleName .'"</b></font></a>]<BR>'; - $this->add_template_var($handleName, $prefix .$this->file_to_string($fileName)); - } - else { - $this->add_template_var($handleName, $this->file_to_string($fileName)); - } - }//end add_template_file() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Adds a value for a template placeholder. - */ - public function add_template_var($varName, $varValue){ - $this->templateVars[$varName]=$varValue; - }//end add_template_var(); - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Rips the given block row name from the parent templateVar, (optionally) replacing it with - * a template var of the same name. - * - * @param $parent (str) name of the templateVar to pull the block row from. - * @param $handle (str) name of the block row to rip from it's parent. - * @param $removeDefs (bool,optional) if evaluated as FALSE, no template var is left in - * place of the block row. - * - * @return (bool false) FAIL: unable to find block row. - * @return (str) PASS: content of the block row. - */ - public function set_block_row($parent, $handle, $removeDefs=0) { - $name = $handle; - $str = $this->templateVars[$parent]; - - $reg = "/<!-- BEGIN $handle -->.+<!-- END $handle -->/sU"; - preg_match_all($reg, $str, $m); - if(!is_string($m[0][0])) { - #exit("set_block_row(): couldn't find '$handle' in var '$parent'"); - $retval = FALSE; - } else { - if($removeDefs) { - $openHandle = "<!-- BEGIN $handle -->"; - $endHandle = "<!-- END $handle -->"; - $m[0][0] = str_replace($openHandle, "", $m[0][0]); - $m[0][0] = str_replace($endHandle, "", $m[0][0]); - } - - $str = preg_replace($reg, "{" . "$name}", $str); - $this->templateVars[$parent] = $str; - $this->templateRows[$name] = $m[0][0]; - $this->add_template_var($name, ""); - $retval = $m[0][0]; - } - return($retval); - }//end set_block_row() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Using the given template, it will replace each index (in $repArr) with it's value: each - * var to be replaced must begin the given begin & end delimiters. - * - * @param $template (str) Data to perform the replacements on. - * @param $repArr (array) Array of name=>value pairs, where name is to be replaced with value. - * @param $b (str,optional) beginning delimiter. - * @param $e (str,optional) ending delimiter. - */ - public function mini_parser($template, $repArr, $b='%%', $e='%%') { - return($this->gfObj->mini_parser($template, $repArr, $b, $e)); - }//end mini_parser() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Processes all template vars & files, etc, to produce the final page. NOTE: it is a wise idea - * for most pages to have this called *last*. - * - * @param $stripUndefVars (bool,optional) Remove all undefined template vars. - * - * @return (str) Final, parsed page. - */ - public function print_page($stripUndefVars=1) { - //Show any available messages. - $this->process_set_message(); - - //Load the default page layout. - $this->templateObj->set_file("main", $this->mainTemplate); - - //load the placeholder names and thier values - $this->templateObj->set_var($this->templateVars); - $this->templateObj->parse("out","main"); //parse the sub-files into the main page - - if($stripUndefVars) { - $numLoops = 0; - while(preg_match_all('/\{.\S+?\}/', $this->templateObj->varvals['out'], $tags) && $numLoops < 50) { - $tags = $tags[0]; - - //TODO: figure out why this works when running it twice. - foreach($tags as $key=>$str) { - $str2 = str_replace("{", "", $str); - $str2 = str_replace("}", "", $str2); - if(!$this->templateVars[$str2] && $stripUndefVars) { - //TODO: set an internal pointer or something to use here, so they can see what was missed. - $this->templateObj->varvals[out] = str_replace($str, '', $this->templateObj->varvals[out]); - } - } - $this->templateObj->parse("out", "out"); - $numLoops++; - } - } - $this->templateObj->pparse("out","out"); //parse the main page - - }//end of print_page() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Handles a message that was set into the session. - */ - public function process_set_message() { - //if there's not a message set, skip. - $errorBox = ""; - if(isset($_SESSION['message']) && is_array($_SESSION['message'])) { - $errorBox = $this->file_to_string("system/message_box.tmpl"); - //let's make sure the "type" value is *lowercase*. - $_SESSION['message']['type'] = strtolower($_SESSION['message']['type']); - - //WARNING::: if you give it the wrong type, it'll STILL be parsed. Otherwise - // this has to match set_message() FAR too closely. And it's a pain. - $_SESSION['message']['messageType'] = $_SESSION['message']['type']; - $errorBox = $this->gfObj->mini_parser($errorBox, $_SESSION['message'], '{', '}'); - if($_SESSION['message']['type'] == "fatal") { - //replace content of the page with our error. - $this->change_content($errorBox); - } else { - //Non-fatal: put it into a template var. - $this->add_template_var("error_msg", $errorBox); - } - - //now that we're done displaying the message, let's get it out of the session (otherwise - // they'll never get past this point). - unset($_SESSION['message']); - } - return($errorBox); - }//end of process_set_message() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Takes a template file, whose root must be within $GLOBALS['TMPLDIR'], pulls it's - * content & returns it. - */ - public function file_to_string($templateFileName) { - $templateFileName = preg_replace('/\/\//', '\/', $templateFileName); - if($this->template_file_exists($templateFileName)) { - $retval = file_get_contents($this->tmplDir .'/'. $templateFileName); - } else { - $this->set_message_wrapper(array( - "title" => 'Template File Error', - "message" => 'Not all templates could be found for this page.', - "type" => 'error' - )); - } - return($retval); - }//end file_to_string() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Checks to see if the given filename exists within the template directory. - */ - public function template_file_exists($file) { - $retval = 0; - //If the string doesn't start with a /, add one - if (strncmp("/",$file,1)) { - //strncmp returns 0 if they match, so we're putting a / on if they don't - $file="/".$file; - } - $filename=$this->tmplDir.$file; - - if(file_exists($filename)) { - $retval = $filename; - } - return($retval); - }//end template_file_exists() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Creates an array in the session, used by the templating system as a way to - * get messages generated by the code into the page, without having to add - * special templates & such each time. - * - * @param $title (str) the title of the message. - * @param $message (str) text beneath the title. - * @param $linkURL (str,optional) URL for the link below the message. - * @param $type (str) notice/status/error/fatal message, indicating - * it's importance. Generally, fatal messages - * cause only the message to be shown. - * @param $linkText (str,optional) text that the link wraps. - * @param $overwriteSame (bool,optional) whether setting a message which has - * the same type as an already set message will - * overwite the previous. More important messages - * always overwrite lesser ones. - * @param $priority (int,optional) specify the message's priority. - * - * @return (bool) Indicates pass (true)/fail (false) - */ - function set_message($title=NULL, $message=NULL, $linkURL=NULL, $type=NULL, $linkText=NULL, $overwriteSame=NULL, $priority=NULL) { - if(!isset($overwriteSame)) { - $overwriteSame = 1; - } - - //defines the importance level of each type of message: the higher the value, the more important it is. - $priorityArr = array( - 'notice' => 10, - 'status' => 20, - 'error' => 30, - 'fatal' => 100 - ); - if(!isset($type) || !isset($priorityArr[$type])) { - //set a default type. - $arrayKeys = array_keys(); - $type = $arrayKeys[0]; - } - - if(strlen($linkURL)) { - if(!strlen($linkText) || is_null($linkText)) { - $linkText = "Link"; - } - $redirectText = '<a href="'. $linkURL .'">'. $linkText .'</a>'; - } - - //Create the array. - $myArr = array( - "title" => $title, - "message" => $message, - "redirect" => $redirectText, - "type" => $type, - "priority" => $priority - - ); - - //make sure the message type is IN the priority array... - if(!in_array($type, array_keys($priorityArr))) { - //invalid type. - $retval = FALSE; - } elseif($_SESSION['message']) { - //there's already a message... check if the new one should overwrite the existing. - if((!$overwriteSame) AND ($priorityArr[$_SESSION['message']['type']] == $priorityArr[$type])) { - //no overwriting. - $retval = FALSE; - } elseif($priorityArr[$_SESSION['message']['type']] <= $priorityArr[$type]) { - // the existing message is less important. Overwrite it. - $_SESSION['message'] = $myArr; - } - } - else { - $_SESSION['message'] = $myArr; - $retval = TRUE; - } - - return($retval); - - } // end of set_message() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - public function set_message_wrapper($array) { - @$this->set_message( - $array['title'], - $array['message'], - $array['linkURL'], - $array['type'], - $array['linkText'], - $array['overwriteSame'], - $array['priority'] - ); - }//end set_message_wrapper() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Performs redirection, provided it is allowed. - */ - function conditional_header($url, $exitAfter=TRUE) { - if($this->allowRedirect) { - //checks to see if headers were sent; if yes: use a meta redirect. - // if no: send header("location") info... - if(headers_sent()) { - //headers sent. Use the meta redirect. - print " - <HTML> - <HEAD> - <TITLE>Redirect Page</TITLE> - <META HTTP-EQUIV='refresh' content='0; URL=$url'> - </HEAD> - <a href=\"$url\"></a> - </HTML> - "; - } - else { - header("location:$url"); - } - - if($exitAfter) { - //redirecting without exitting is bad, m'kay? - exit; - } - } - else { - //TODO: should an exception be thrown, or maybe exit here anyway? - throw new exception(__METHOD__ .": auto redirects not allowed...?"); - } - }//end conditional_header() - //--------------------------------------------------------------------------------------------- - - - //--------------------------------------------------------------------------------------------- - /** - * Given the name of a templateVar (defaults to "content"), this method will retrieve all block - * row definitions in the order that they can safely be ripped-out/set by set_block_row(). - * - * @parm $templateVar (str,optional) templateVar to parse. - * - * @return (array) Contains all rows, complete & incomplete. - * - * NOTE: Layout of array::: - * array( - * incomplete => array( - * begin => array(), - * end => array() - * ), - * ordered => array() - * NOTE2: each "array()" above is a list of block row names. - * NOTE3: failure to parse the template will return a blank array. - * - * - */ - function get_block_row_defs($templateVar="content") { - //cast $retArr as an array, so it's clean. - $retArr = array(); - - //NOTE: the value 31 isn't just a randomly chosen length; it's the minimum - // number of characters to have a block row. EG: "<!-- BEGIN x -->o<!-- END x -->" - $templateContents = $this->templateVars[$templateVar]; - if(strlen($templateContents) >= 31) { - //looks good to me. Run the regex... - $flags = PREG_PATTERN_ORDER; - $reg = "/<!-- BEGIN (.+) -->/"; - preg_match_all($reg, $templateContents, $beginArr, $flags); - $beginArr = $beginArr[1]; - - $endReg = "/<!-- END (.+) -->/"; - preg_match_all($endReg, $templateContents, $endArr, $flags); - $endArr = $endArr[1]; - - //create a part of the array that shows any orphaned "BEGIN" statements (no matching "END" - // statement), and orphaned "END" statements (no matching "BEGIN" statements) - // NOTE::: by doing this, should easily be able to tell if the block rows were defined - // properly or not. - if(count($retArr['incomplete']['begin'] = array_diff($beginArr, $endArr)) > 0) { - //I'm sure there's an easier way to do this, but my head hurts too much when - // I try to do the magic. Maybe I need to put another level in CodeMancer... - foreach($retArr['incomplete']['begin'] as $num=>$val) { - unset($beginArr[$num]); - } - } - if(count($retArr['incomplete']['end'] = array_diff($endArr, $beginArr)) > 0) { - //both of the below foreach's simply pulls undefined vars out of the - // proper arrays, so I don't have to deal with them later. - foreach($retArr['incomplete']['end'] as $num=>$val) { - unset($endArr[$num]); - } - } - - //YAY!!! we've got valid data!!! - //reverse the order of the array, so when the ordered array - // is looped through, all block rows can be pulled. - $retArr['ordered'] = array_reverse($beginArr); - } else { - //nothin' doin'. Return a blank array. - $retArr = array(); - } - - return($retArr); - }//end get_block_row_defs() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - function rip_all_block_rows($templateVar="content", $exceptionArr=array()) { - $rowDefs = $this->get_block_row_defs($templateVar); - - $useTheseBlockRows = $rowDefs['ordered']; - $retval = array(); - if(is_array($useTheseBlockRows)) { - foreach($useTheseBlockRows as $blockRowName) - { - if(!in_array($blockRowName, $exceptionArr)) - { - //remove the block row. - $rowData = $this->set_block_row($templateVar, $blockRowName); - $retval[$blockRowName] = $rowData; - } - } - } - - return($retval); - }//end rip_all_block_rows() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - public function set_all_block_rows($templateVar="content", $exceptionArr=array()) - { - $retval = $this->rip_all_block_rows($templateVar, $exceptionArr); - return($retval); - }//end set_all_block_rows() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - public function allow_invalid_urls($newSetting=NULL) { - if(!is_null($newSetting) && is_bool($newSetting)) { - $this->allowInvalidUrls = $newSetting; - } - return($this->allowInvalidUrls); - }//end allow_invalid_urls() - //--------------------------------------------------------------------------------------------- - -}//end cs_genericPage{} -?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-01-29 21:26:02
|
Revision: 335 http://cs-content.svn.sourceforge.net/cs-content/?rev=335&view=rev Author: crazedsanity Date: 2009-01-29 21:25:58 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Renamed cs_globalFunctions.php to cs_globalFunctions.class.php. /cs_globalFunctions.php [RENAMED TO cs_globalFunctions.class.php] /cs_globalFunctions.class.php [RENAMED FROM cs_globalFunctions.php] /abstract/cs_content/abstract.class.php: * fix name of cs_globalFunctions{} file. /sample_files/public_html/index.php: * fix name of cs_globalFunctions{} file. /tests/testOfCSContent.php: * fix name of cs_globalFunctions{} file. Modified Paths: -------------- trunk/1.0/abstract/cs_content.abstract.class.php trunk/1.0/sample_files/public_html/index.php trunk/1.0/tests/testOfCSContent.php Added Paths: ----------- trunk/1.0/cs_globalFunctions.class.php Removed Paths: ------------- trunk/1.0/cs_globalFunctions.php Modified: trunk/1.0/abstract/cs_content.abstract.class.php =================================================================== --- trunk/1.0/abstract/cs_content.abstract.class.php 2009-01-29 21:15:20 UTC (rev 334) +++ trunk/1.0/abstract/cs_content.abstract.class.php 2009-01-29 21:25:58 UTC (rev 335) @@ -24,7 +24,7 @@ if($makeGfObj === true) { //make a cs_globalFunctions{} object. - require_once(dirname(__FILE__) ."/../cs_globalFunctions.php"); + require_once(dirname(__FILE__) ."/../cs_globalFunctions.class.php"); $this->gfObj = new cs_globalFunctions(); } }//end __construct() Copied: trunk/1.0/cs_globalFunctions.class.php (from rev 333, trunk/1.0/cs_globalFunctions.php) =================================================================== --- trunk/1.0/cs_globalFunctions.class.php (rev 0) +++ trunk/1.0/cs_globalFunctions.class.php 2009-01-29 21:25:58 UTC (rev 335) @@ -0,0 +1,844 @@ +<?php + +require_once(dirname(__FILE__) ."/../cs-versionparse/cs_version.abstract.class.php"); + +class cs_globalFunctions extends cs_versionAbstract { + + + /* DEBUG PRINT OPTIONS */ + /** Remove the separator below the output of each debug_print()? */ + public $debugRemoveHr = 0; + public $debugPrintOpt = 0; + + private $forceSqlQuotes=0; + private $oldForceSqlQuotes=0; + + //========================================================================= + public function __construct() { + //These checks have been implemented for pseudo backwards-compatibility + // (internal vars won't change if GLOBAL vars changed). + if(defined('DEBUGREMOVEHR')) { + $this->debugRemoveHr = constant('DEBUGREMOVEHR'); + } + elseif(isset($GLOBALS['DEBUGREMOVEHR'])) { + $this->debugRemoveHr = $GLOBALS['DEBUGREMOVEHR']; + } + + if(defined('DEBUGPRINTOPT')) { + $this->debugPrintOpt = constant('DEBUGPRINTOPT'); + } + elseif(isset($GLOBALS['DEBUGPRINTOPT'])) { + $this->debugPrintOpt = $GLOBALS['DEBUGPRINTOPT']; + } + $this->set_version_file_location(dirname(__FILE__) . '/VERSION'); + }//end __construct() + //========================================================================= + + + + //========================================================================= + public function switch_force_sql_quotes($newSetting) { + if(is_bool($newSetting)) { + if($newSetting === true) { + $newSetting = 1; + } + else { + $newSetting = 1; + } + } + elseif(is_numeric($newSetting)) { + if($newSetting > 0) { + $newSetting = 1; + } + else { + $newSetting = 0; + } + } + else { + throw new exception(__METHOD__ .": invalid new setting (". $newSetting .")"); + } + + if($newSetting !== $this->forceSqlQuotes) { + $this->oldForceSqlQuotes = $this->forceSqlQuotes; + $this->forceSqlQuotes = $newSetting; + $retval = true; + } + else { + $retval = false; + } + + return($retval); + }//end switch_force_sql_quotes() + //========================================================================= + + + + //================================================================================================================ + /** + * Automatically selects either the header() function, or printing meta-refresh data for redirecting a browser. + */ + public function conditional_header($url, $exitAfter=TRUE, $permRedir=FALSE) { + + if(is_array($_SESSION)) { + //do some things to help protect against recursive redirects. + if(isset($_SESSION['__conditional_header__'])) { + $number = $_SESSION['__conditional_header__']['number']; + $lastTime = $_SESSION['__conditional_header__']['last_time']; + if((time() - $lastTime) <= 1 && $number > 5) { + unset($_SESSION['__conditional_header__']); + throw new exception(__METHOD__ .": too many redirects (". $number .") in a short time, last url: (". $url .")"); + } + else { + $_SESSION['__conditional_header__']['number']++; + $_SESSION['__conditional_header__']['last_time'] = time(); + } + } + else { + $_SESSION['__conditional_header__'] = array( + 'last_time' => time(), + 'number' => 0 + ); + } + } + + if(!strlen($url)) { + throw new exception(__METHOD__ .": failed to specify URL (". $url .")"); + } + else { + if(headers_sent()) { + //headers sent. Use the meta redirect. + print " + <HTML> + <HEAD> + <TITLE>Redirect Page</TITLE> + <META HTTP-EQUIV='refresh' content='0; URL=$url'> + </HEAD> + <a href=\"$url\"></a> + </HTML> + "; + } + else { + if($permRedir) { + //NOTE: can't do much for permanent redirects if headers have already been sent. + header("HTTP/1.1 301 Moved Permanently"); + } + header("location:$url"); + } + } + + if($exitAfter) { + exit; + } + }//end conditional_header() + //================================================================================================================ + + + + //================================================================================================================ + /** + * Basically, just a wrapper for create_list(), which returns a list or + * an array of lists, depending upon what was requested. + * + * @param $array <array> list for the array... + * @param $style <str,optional> what "style" it should be returned + * as (select, update, etc). + * @param $separator <str,optional> what separattes key from value: see each + * style for more information. + * @param $cleanString <mixed,optional> clean the values in $array by sending it + * to cleanString(), with this as the second argument. + * @param $removeEmptyVals <bool,optional> If $cleanString is an ARRAY and this + * evaluates as TRUE, indexes of $array whose values have + * a length of 0 will be removed. + */ + public function string_from_array($array,$style=NULL,$separator=NULL, $cleanString=NULL, $removeEmptyVals=FALSE) { + + $retval = NULL; + //precheck... if it's not an array, kill it. + if(!is_array($array)) { + return(NULL); + } + + //make sure $style is valid. + $typesArr = array("insert", "update"); + $style = strtolower($style); + + if(is_array($array)) { + + //if $cleanString is an array, assume it's arrayIndex => cleanStringArg + if(is_array($cleanString) && (!is_null($style) && (strlen($style)))) { + $cleanStringArr = array_intersect_key($cleanString, $array); + if(count($cleanStringArr) > 0 && is_array($cleanStringArr)) { + foreach($cleanStringArr as $myIndex=>$myCleanStringArg) { + if(($removeEmptyVals) && (strlen($array[$myIndex]) == 0)) { + //remove the index. + unset($array[$myIndex]); + } + else { + //now format it properly. + $array[$myIndex] = $this->cleanString($array[$myIndex], $myCleanStringArg); + } + } + } + } + switch($style) { + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + case "insert": + if(!$separator) { + $separator = " VALUES "; + } + //build temporary data... + $tmp = array(); + foreach($array as $key=>$value) { + @$tmp[0] = $this->create_list($tmp[0], $key); + //clean the string, if required. + if($cleanString) { + //make sure it's not full of poo... + $value = $this->cleanString($value, "sql"); + #$value = "'". $value ."'"; + } + if((is_null($value)) OR ($value == "")) { + $value = "NULL"; + } + @$tmp[1] = $this->create_list($tmp[1], $value, ",", 1); + } + + //make the final product. + $retval = "(". $tmp[0] .")" . $separator . "(". $tmp[1] .")"; + + break; + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + case "update": + if(!$separator) { + $separator = "="; + } + //build final product. + foreach($array as $field=>$value) { + $sqlQuotes = 1; + if(($value === "NULL" || $value === NULL) && !$this->forceSqlQuotes) { + $sqlQuotes = 0; + } + if($cleanString && !preg_match('/^\'/',$value)) { + //make sure it doesn't have crap in it... + $value = $this->cleanString($value, "sql",$sqlQuotes); + } + $retval = $this->create_list($retval, $field . $separator . $value); + } + break; + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + case "order": + case "limit": + //for creating the "limit 50 offset 35" part of a query... or at least using that "style". + $separator = " "; + //build final product. + foreach($array as $field=>$value) { + if($cleanString) { + //make sure it doesn't have crap in it... + $value = $this->cleanString($value, "sql", $this->forceSqlQuotes); + $value = "'". $value ."'"; + } + $retval = $this->create_list($retval, $value, ", "); + } + if($style == "order" && !preg_match('/order by/', strtolower($retval))) { + $retval = "ORDER BY ". $retval; + } + break; + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + case "select": + //build final product. + $separator = "="; + foreach($array as $field=>$value) { + + //allow for tricksie things... + /* + * Example: + * string_from_array(array("y"=>3, "x" => array(1,2,3))); + * + * would yield: "y=3 AND (x=1 OR x=2 OR x=3)" + */ + $delimiter = "AND"; + if(is_array($value)) { + //doing tricksie things!!! + $retval = $this->create_list($retval, $field ." IN (". $this->string_from_array($value) .")", + " $delimiter ", $this->forceSqlQuotes); + } + else { + //if there's already an operator ($separator), don't specify one. + if(preg_match('/^[\(<=>]/', $value)) { + $separator = NULL; + } + if($cleanString) { + //make sure it doesn't have crap in it... + $value = $this->cleanString($value, "sql"); + } + if(!is_numeric($value) && isset($separator)) { + $value = "'". $value ."'"; + } + $retval = $this->create_list($retval, $field . $separator . $value, " $delimiter ", $this->forceSqlQuotes); + } + } + break; + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + case "url":{ + //an array like "array('module'='todo','action'='view','ID'=164)" to "module=todo&action=view&ID=164" + if(!$separator) { + $separator = "&"; + } + foreach($array as $field=>$value) { + if($cleanString && !is_array($cleanString)) { + $value = $this->cleanString($value, $cleanString); + } + $retval = $this->create_list($retval, "$field=$value", $separator); + } + } + break; + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + case "text_list":{ + if(is_null($separator)) { + $separator = '='; + } + foreach($array as $field=>$value) { + $retval = $this->create_list($retval, $field . $separator . $value, "\n"); + } + } + break; + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + case "html_list":{ + if(is_null($separator)) { + $separator = '='; + } + foreach($array as $field=>$value) { + $retval = $this->create_list($retval, $field . $separator . $value, "<BR>\n"); + } + } + break; + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + DEFAULT: + if(!$separator) { + $separator = ", "; + } + foreach($array as $field=>$value) { + if($cleanString) { + $value = $this->cleanString($value, $cleanString); + } + $retval = $this->create_list($retval, $value, $separator, $this->forceSqlQuotes); + } + //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + } + } + else { + //not an array. + $retval = NULL; + } + + return($retval); + }//end string_from_array() + //================================================================================================================ + + + + //================================================================================================================ + /** + * Easy way of cleaning data using types/styles of cleaning, with optional quoting. + * + * @param $cleanThis (str) data to be cleaned + * @param $cleanType (str,optional) how to clean the data. + * @param $sqlQuotes (bool,optional) quote the string for SQL + * + * @return (string) Cleaned data. + */ + function cleanString($cleanThis=NULL, $cleanType="all",$sqlQuotes=0) { + $cleanType = strtolower($cleanType); + switch ($cleanType) { + case "none": + //nothing to see here (no cleaning wanted/needed). Move along. + $sqlQuotes = 0; + break; + + case "query": + /* + replace \' with ' + gets rid of evil characters that might lead to SQL injection attacks. + replace line-break characters + */ + $evilChars = array("\$", "%", "~", "*",">", "<", "-", "{", "}", "[", "]", ")", "(", "&", "#", "?", ".", "\,","\/","\\","\"","\|","!","^","+","`","\n","\r"); + $cleanThis = preg_replace("/\|/","",$cleanThis); + $cleanThis = preg_replace("/\'/", "", $cleanThis); + $cleanThis = str_replace($evilChars,"", $cleanThis); + $cleanThis = stripslashes(addslashes($cleanThis)); + break; + + case "sql": + $cleanThis = addslashes(stripslashes($cleanThis)); + break; + + + case "sql_insert": + /* + * This is for descriptive fields, where double quotes don't need to be escaped: in these + * cases, escaping the double-quotes might lead to inserting something that looks different + * than the original, but in fact is identical. + */ + $cleanThis = addslashes(stripslashes($cleanThis)); + $cleanThis = preg_replace('/\\\\"/', '"', $cleanThis); + $cleanThis = preg_replace("/'/", "\\\'", $cleanThis); + + break; + + + case "sql92_insert": + /* + * Just like 'sql_insert', except that single quotes are "delimited" by + * adding another single quote, which works *at least* with postgres & sqlite. + */ + $cleanThis = preg_replace("/'/", "''", $cleanThis); + $cleanThis = preg_replace('/\\\\"/', '"', $cleanThis); + $cleanThis = stripslashes($cleanThis); + $sqlQuotes = 0; + break; + + case "double_quote": + //This will remove all double quotes from a string. + $cleanThis = str_replace('"',"",$cleanThis); + break; + + case "htmlspecial": + /* + This function is useful in preventing user-supplied text from containing HTML markup, such as in a message board or guest book application. + The translations performed are: + '&' (ampersand) becomes '&' + '"' (double quote) becomes '"'. + '<' (less than) becomes '<' + '>' (greater than) becomes '>' + */ + + $cleanThis = htmlspecialchars($cleanThis); + break; + + case "htmlspecial_q": + /* + '&' (ampersand) becomes '&' + '"' (double quote) becomes '"'. + ''' (single quote) becomes '''. + '<' (less than) becomes '<' + '>' (greater than) becomes '> + */ + $cleanThis = htmlspecialchars($cleanThis,ENT_QUOTES); + break; + + case "htmlspecial_nq": + /* + '&' (ampersand) becomes '&' + '<' (less than) becomes '<' + '>' (greater than) becomes '> + */ + $cleanThis = htmlspecialchars($cleanThis,ENT_NOQUOTES); + break; + + case "htmlentity": + /* + Convert all applicable text to its html entity + Will convert double-quotes and leave single-quotes alone + */ + $cleanThis = htmlentities(html_entity_decode($cleanThis)); + break; + + case "htmlentity_plus_brackets": + /* + Just like htmlentity, but also converts "{" and "}" (prevents template + from being incorrectly parse). + Also converts "{" and "}" to their html entity. + */ + $cleanThis = htmlentities(html_entity_decode($cleanThis)); + $cleanThis = str_replace('$', '$', $cleanThis); + $cleanThis = str_replace('{', '{', $cleanThis); + $cleanThis = str_replace('}', '}', $cleanThis); + break; + + case "double_entity": + //Removed double quotes, then calls html_entities on it. + $cleanThis = str_replace('"',"",$cleanThis); + $cleanThis = htmlentities(html_entity_decode($cleanThis)); + break; + + case "meta": + // Returns a version of str with a backslash character (\) before every character that is among these: + // . \\ + * ? [ ^ ] ( $ ) + $cleanThis = quotemeta($cleanThis); + break; + + case "email": + //Remove all characters that aren't allowed in an email address. + $cleanThis = preg_replace("/[^A-Za-z0-9\._@-]/","",$cleanThis); + break; + + case "email_plus_spaces": + //Remove all characters that aren't allowed in an email address. + $cleanThis = preg_replace("/[^A-Za-z0-9\ \._@-]/","",$cleanThis); + break; + + case "phone_fax": + //Remove everything that's not numeric or +()- example: +1 (555)-555-2020 is valid + $cleanThis = preg_replace("/[^0-9-+() ]/","",$cleanThis); + break; + + case "integer": + case "numeric": + //Remove everything that's not numeric. + if(is_null($cleanThis)) { + $cleanThis = "NULL"; + $sqlQuotes = 0; + } + else { + $cleanThis = preg_replace("/[^0-9]/","",$cleanThis); + } + break; + + case "decimal": + case "float": + //same as integer only the decimal point is allowed + $cleanThis = preg_replace("/[^0-9\.]/","",$cleanThis); + break; + + case "name": + case "names": + //allows only things in the "alpha" case and single quotes. + $cleanThis = preg_replace("/[^a-zA-Z']/", "", $cleanThis); + break; + + case "alpha": + //Removes anything that's not English a-zA-Z + $cleanThis = preg_replace("/[^a-zA-Z]/","",$cleanThis); + break; + + case "bool": + case "boolean": + //makes it either T or F (gotta lower the string & only check the first char to ensure accurate results). + $cleanThis = $this->interpret_bool($cleanThis, array('f', 't')); + break; + + case "varchar": + $cleanThis=$this->cleanString($cleanThis,"query"); + $cleanThis="'" . $cleanThis . "'"; + if($cleanThis == "''") { + $cleanThis="NULL"; + } + break; + + case "date": + $cleanThis = preg_replace("/[^0-9\-]/","",$cleanThis); + break; + + case "datetime": + $cleanThis=preg_replace("/[^A-Za-z0-9\/: \-\'\.]/","",$cleanThis); + break; + + case "all": + default: + // 1. Remove all naughty characters we can think of except alphanumeric. + $cleanThis = preg_replace("/[^A-Za-z0-9]/","",$cleanThis); + break; + + } + if($sqlQuotes) { + $cleanThis = "'". $cleanThis ."'"; + } + return $cleanThis; + }//end cleanString() + //================================================================================================================ + + + + + //================================================================================================================ + /** + * Returns a list delimited by the given delimiter. Does the work of checking if the given variable has data + * in it already, that needs to be added to, vs. setting the variable with the new content. + */ + public function create_list($string=NULL, $addThis=NULL, $delimiter=", ", $useSqlQuotes=0) { + if(strlen($string)) { + if($useSqlQuotes && !(preg_match("/^'/", $addThis) && preg_match("/'\$/", $addThis))) { + $addThis = "'". $addThis ."'"; + } + $retVal = $string . $delimiter . $addThis; + } + else { + $retVal = $addThis; + if($useSqlQuotes && !(preg_match("/^'/", $retVal) && preg_match("/'\$/", $retVal))) { + $retVal = "'". $retVal ."'"; + } + } + + return($retVal); + } //end create_list() + //================================================================================================================ + + + + //================================================================================================================ + /** + * A way of printing out human-readable information, especially arrays & objects, either to a web browser or via + * the command line. + * + * @param $input (mixed,optional) data to print/return + * @param $printItForMe (bool,optional) whether it should be printed or just returned. + * + * @return (string) printed data. + */ + public function debug_print($input=NULL, $printItForMe=NULL, $removeHR=NULL) { + if(!is_numeric($removeHR)) { + $removeHR = $this->debugRemoveHr; + } + + if(!is_numeric($printItForMe)) { + $printItForMe = $this->debugPrintOpt; + } + + ob_start(); + print_r($input); + $output = ob_get_contents(); + ob_end_clean(); + + $output = "<pre>$output</pre>"; + + if(!isset($_SERVER['SERVER_PROTOCOL']) || !$_SERVER['SERVER_PROTOCOL']) { + $output = strip_tags($output); + $hrString = "\n***************************************************************\n"; + } + else { + $hrString = "<hr>"; + } + if($removeHR) { + $hrString = NULL;; + } + + if($printItForMe) { + print "$output". $hrString ."\n"; + } + + return($output); + } //end debug_print() + //================================================================================================================ + + + + //================================================================================================================ + function swapValue(&$value, $c1, $c2) { + if(!$value) { + $value = $c1; + } + + + /* choose the next color */ + if($value == "$c1") { + $value = "$c2"; + } + else { + $value = "$c1"; + } + + return($value); + } + //================================================================================================================ + + + + //--------------------------------------------------------------------------------------------- + /** + * Using the given template, it will replace each index (in $repArr) with it's value: each + * var to be replaced must begin the given begin & end delimiters. + * + * @param $template (str) Data to perform the replacements on. + * @param $repArr (array) Array of name=>value pairs, where name is to be replaced with value. + * @param $b (str,optional) beginning delimiter. + * @param $e (str,optional) ending delimiter. + */ + public function mini_parser($template, $repArr, $b='%%', $e='%%') { + if(!isset($b) OR !isset($e)){ + $b="{"; + $e="}"; + } + + foreach($repArr as $key=>$value) { + //run the replacements. + $key = "$b" . $key . "$e"; + $template = str_replace("$key", $value, $template); + } + + return($template); + }//end mini_parser() + //--------------------------------------------------------------------------------------------- + + + //--------------------------------------------------------------------------------------------- + /** + * Takes the given string & truncates it so the final string is the given + * maximum length. Optionally adds a chunk of text to the end, and also + * optionally STRICTLY truncates (non-strict means the endString will be + * added blindly, while strict means the length of the endString will be + * subtracted from the total length, so the final string is EXACTLY the + * given length or shorter). + * + * @param string (str) the string to truncate + * @param $maxLength (int) maximum length for the result. + * @param $endString (str,optional) this is added to the end of the + * truncated string, if it exceeds $maxLength + * @param $strict (bool,optional) if non-strict, the length of + * the return would be $maxLength + length($endString) + */ + function truncate_string($string,$maxLength,$endString="...",$strict=FALSE) { + + //determine if it's even worth truncating. + if(is_string($string) && is_numeric($maxLength) && $maxLength > 0) { + $strLength = strlen($string); + if($strLength <= $maxLength) { + //no need to truncate. + $retval = $string; + } + else { + //actually needs to be truncated... + if($strict) { + $trueMaxLength = $maxLength - strlen($endString); + } + else { + $trueMaxLength = $maxLength; + } + + //rip the first ($trueMaxLength) characters from string, append $endString, and go. + $tmp = substr($string,0,$trueMaxLength); + $retval = $tmp . $endString; + } + } + else { + $retval = $string; + } + + return($retval); + + }//end truncate_string() + //--------------------------------------------------------------------------------------------- + + + + //########################################################################## + public function array_as_option_list(array $data, $checkedValue=NULL, $type="select", $useTemplateString=NULL, array $repArr=NULL) { + $typeArr = array ( + "select" => "selected", + "radio" => "checked", + "checkbox" => "checked" + ); + + $myType = $typeArr[$type]; + if(is_null($useTemplateString)) { + // + $useTemplateString = "\t\t<option value='%%value%%'%%selectedString%%>%%display%%</option>"; + } + + $retval = ""; + foreach($data as $value=>$display) { + //see if it's the value that's been selected. + $selectedString = ""; + if($value == $checkedValue || $display == $checkedValue) { + //yep, it's selected. + $selectedString = " ". $myType; + } + + //create the string. + $myRepArr = array( + 'value' => $value, + 'display' => $display, + 'selectedString' => $selectedString + ); + if(is_array($repArr) && is_array($repArr[$value])) { + //merge the arrays. + $myRepArr = array_merge($repArr[$value], $myRepArr); + } + $addThis = $this->mini_parser($useTemplateString, $myRepArr, "%%", "%%"); + $retval = $this->create_list($retval, $addThis, "\n"); + } + + return($retval); + }//end array_as_option_list() + //########################################################################## + + + + //########################################################################## + public function interpret_bool($interpretThis, array $trueFalseMapper=null) { + $interpretThis = preg_replace('/ /', '', $interpretThis); + if(is_array($trueFalseMapper)) { + if(count($trueFalseMapper) == 2 && isset($trueFalseMapper[0]) && isset($trueFalseMapper[1])) { + $realVals = $trueFalseMapper; + } + else { + throw new exception(__METHOD__ .": invalid true/false map"); + } + } + else { + //set an array that defines what "0" and "1" return. + $realVals = array( + 0 => false, + 1 => true + ); + } + + //now figure out the value to return. + if(is_numeric($interpretThis)) { + settype($interpretThis, 'integer'); + if($interpretThis == '0') { + $index=0; + } + else { + $index=1; + } + } + elseif(is_bool($interpretThis)) { + if($interpretThis == true) { + $index=1; + } + else { + $index=0; + } + } + elseif(preg_match('/^true$/i', $interpretThis) || preg_match('/^false$/', $interpretThis) || preg_match("/^[tf]$/", $interpretThis)) { + if(preg_match('/^true$/i', $interpretThis) || preg_match('/^t$/', $interpretThis)) { + $index=1; + } + else { + $index=0; + } + } + else { + //straight-up PHP if/else evaluation. + if($interpretThis) { + $index=1; + } + else { + $index=0; + } + } + + return($realVals[$index]); + }//end interpret_bool() + //########################################################################## + +}//end cs_globalFunctions{} + +?> Property changes on: trunk/1.0/cs_globalFunctions.class.php ___________________________________________________________________ Added: svn:executable + * Added: svn:mergeinfo + Added: svn:eol-style + native Deleted: trunk/1.0/cs_globalFunctions.php =================================================================== --- trunk/1.0/cs_globalFunctions.php 2009-01-29 21:15:20 UTC (rev 334) +++ trunk/1.0/cs_globalFunctions.php 2009-01-29 21:25:58 UTC (rev 335) @@ -1,844 +0,0 @@ -<?php - -require_once(dirname(__FILE__) ."/../cs-versionparse/cs_version.abstract.class.php"); - -class cs_globalFunctions extends cs_versionAbstract { - - - /* DEBUG PRINT OPTIONS */ - /** Remove the separator below the output of each debug_print()? */ - public $debugRemoveHr = 0; - public $debugPrintOpt = 0; - - private $forceSqlQuotes=0; - private $oldForceSqlQuotes=0; - - //========================================================================= - public function __construct() { - //These checks have been implemented for pseudo backwards-compatibility - // (internal vars won't change if GLOBAL vars changed). - if(defined('DEBUGREMOVEHR')) { - $this->debugRemoveHr = constant('DEBUGREMOVEHR'); - } - elseif(isset($GLOBALS['DEBUGREMOVEHR'])) { - $this->debugRemoveHr = $GLOBALS['DEBUGREMOVEHR']; - } - - if(defined('DEBUGPRINTOPT')) { - $this->debugPrintOpt = constant('DEBUGPRINTOPT'); - } - elseif(isset($GLOBALS['DEBUGPRINTOPT'])) { - $this->debugPrintOpt = $GLOBALS['DEBUGPRINTOPT']; - } - $this->set_version_file_location(dirname(__FILE__) . '/VERSION'); - }//end __construct() - //========================================================================= - - - - //========================================================================= - public function switch_force_sql_quotes($newSetting) { - if(is_bool($newSetting)) { - if($newSetting === true) { - $newSetting = 1; - } - else { - $newSetting = 1; - } - } - elseif(is_numeric($newSetting)) { - if($newSetting > 0) { - $newSetting = 1; - } - else { - $newSetting = 0; - } - } - else { - throw new exception(__METHOD__ .": invalid new setting (". $newSetting .")"); - } - - if($newSetting !== $this->forceSqlQuotes) { - $this->oldForceSqlQuotes = $this->forceSqlQuotes; - $this->forceSqlQuotes = $newSetting; - $retval = true; - } - else { - $retval = false; - } - - return($retval); - }//end switch_force_sql_quotes() - //========================================================================= - - - - //================================================================================================================ - /** - * Automatically selects either the header() function, or printing meta-refresh data for redirecting a browser. - */ - public function conditional_header($url, $exitAfter=TRUE, $permRedir=FALSE) { - - if(is_array($_SESSION)) { - //do some things to help protect against recursive redirects. - if(isset($_SESSION['__conditional_header__'])) { - $number = $_SESSION['__conditional_header__']['number']; - $lastTime = $_SESSION['__conditional_header__']['last_time']; - if((time() - $lastTime) <= 1 && $number > 5) { - unset($_SESSION['__conditional_header__']); - throw new exception(__METHOD__ .": too many redirects (". $number .") in a short time, last url: (". $url .")"); - } - else { - $_SESSION['__conditional_header__']['number']++; - $_SESSION['__conditional_header__']['last_time'] = time(); - } - } - else { - $_SESSION['__conditional_header__'] = array( - 'last_time' => time(), - 'number' => 0 - ); - } - } - - if(!strlen($url)) { - throw new exception(__METHOD__ .": failed to specify URL (". $url .")"); - } - else { - if(headers_sent()) { - //headers sent. Use the meta redirect. - print " - <HTML> - <HEAD> - <TITLE>Redirect Page</TITLE> - <META HTTP-EQUIV='refresh' content='0; URL=$url'> - </HEAD> - <a href=\"$url\"></a> - </HTML> - "; - } - else { - if($permRedir) { - //NOTE: can't do much for permanent redirects if headers have already been sent. - header("HTTP/1.1 301 Moved Permanently"); - } - header("location:$url"); - } - } - - if($exitAfter) { - exit; - } - }//end conditional_header() - //================================================================================================================ - - - - //================================================================================================================ - /** - * Basically, just a wrapper for create_list(), which returns a list or - * an array of lists, depending upon what was requested. - * - * @param $array <array> list for the array... - * @param $style <str,optional> what "style" it should be returned - * as (select, update, etc). - * @param $separator <str,optional> what separattes key from value: see each - * style for more information. - * @param $cleanString <mixed,optional> clean the values in $array by sending it - * to cleanString(), with this as the second argument. - * @param $removeEmptyVals <bool,optional> If $cleanString is an ARRAY and this - * evaluates as TRUE, indexes of $array whose values have - * a length of 0 will be removed. - */ - public function string_from_array($array,$style=NULL,$separator=NULL, $cleanString=NULL, $removeEmptyVals=FALSE) { - - $retval = NULL; - //precheck... if it's not an array, kill it. - if(!is_array($array)) { - return(NULL); - } - - //make sure $style is valid. - $typesArr = array("insert", "update"); - $style = strtolower($style); - - if(is_array($array)) { - - //if $cleanString is an array, assume it's arrayIndex => cleanStringArg - if(is_array($cleanString) && (!is_null($style) && (strlen($style)))) { - $cleanStringArr = array_intersect_key($cleanString, $array); - if(count($cleanStringArr) > 0 && is_array($cleanStringArr)) { - foreach($cleanStringArr as $myIndex=>$myCleanStringArg) { - if(($removeEmptyVals) && (strlen($array[$myIndex]) == 0)) { - //remove the index. - unset($array[$myIndex]); - } - else { - //now format it properly. - $array[$myIndex] = $this->cleanString($array[$myIndex], $myCleanStringArg); - } - } - } - } - switch($style) { - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - case "insert": - if(!$separator) { - $separator = " VALUES "; - } - //build temporary data... - $tmp = array(); - foreach($array as $key=>$value) { - @$tmp[0] = $this->create_list($tmp[0], $key); - //clean the string, if required. - if($cleanString) { - //make sure it's not full of poo... - $value = $this->cleanString($value, "sql"); - #$value = "'". $value ."'"; - } - if((is_null($value)) OR ($value == "")) { - $value = "NULL"; - } - @$tmp[1] = $this->create_list($tmp[1], $value, ",", 1); - } - - //make the final product. - $retval = "(". $tmp[0] .")" . $separator . "(". $tmp[1] .")"; - - break; - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - case "update": - if(!$separator) { - $separator = "="; - } - //build final product. - foreach($array as $field=>$value) { - $sqlQuotes = 1; - if(($value === "NULL" || $value === NULL) && !$this->forceSqlQuotes) { - $sqlQuotes = 0; - } - if($cleanString && !preg_match('/^\'/',$value)) { - //make sure it doesn't have crap in it... - $value = $this->cleanString($value, "sql",$sqlQuotes); - } - $retval = $this->create_list($retval, $field . $separator . $value); - } - break; - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - case "order": - case "limit": - //for creating the "limit 50 offset 35" part of a query... or at least using that "style". - $separator = " "; - //build final product. - foreach($array as $field=>$value) { - if($cleanString) { - //make sure it doesn't have crap in it... - $value = $this->cleanString($value, "sql", $this->forceSqlQuotes); - $value = "'". $value ."'"; - } - $retval = $this->create_list($retval, $value, ", "); - } - if($style == "order" && !preg_match('/order by/', strtolower($retval))) { - $retval = "ORDER BY ". $retval; - } - break; - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - case "select": - //build final product. - $separator = "="; - foreach($array as $field=>$value) { - - //allow for tricksie things... - /* - * Example: - * string_from_array(array("y"=>3, "x" => array(1,2,3))); - * - * would yield: "y=3 AND (x=1 OR x=2 OR x=3)" - */ - $delimiter = "AND"; - if(is_array($value)) { - //doing tricksie things!!! - $retval = $this->create_list($retval, $field ." IN (". $this->string_from_array($value) .")", - " $delimiter ", $this->forceSqlQuotes); - } - else { - //if there's already an operator ($separator), don't specify one. - if(preg_match('/^[\(<=>]/', $value)) { - $separator = NULL; - } - if($cleanString) { - //make sure it doesn't have crap in it... - $value = $this->cleanString($value, "sql"); - } - if(!is_numeric($value) && isset($separator)) { - $value = "'". $value ."'"; - } - $retval = $this->create_list($retval, $field . $separator . $value, " $delimiter ", $this->forceSqlQuotes); - } - } - break; - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - - - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - case "url":{ - //an array like "array('module'='todo','action'='view','ID'=164)" to "module=todo&action=view&ID=164" - if(!$separator) { - $separator = "&"; - } - foreach($array as $field=>$value) { - if($cleanString && !is_array($cleanString)) { - $value = $this->cleanString($value, $cleanString); - } - $retval = $this->create_list($retval, "$field=$value", $separator); - } - } - break; - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - - - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - case "text_list":{ - if(is_null($separator)) { - $separator = '='; - } - foreach($array as $field=>$value) { - $retval = $this->create_list($retval, $field . $separator . $value, "\n"); - } - } - break; - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - - - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - case "html_list":{ - if(is_null($separator)) { - $separator = '='; - } - foreach($array as $field=>$value) { - $retval = $this->create_list($retval, $field . $separator . $value, "<BR>\n"); - } - } - break; - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - - - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - DEFAULT: - if(!$separator) { - $separator = ", "; - } - foreach($array as $field=>$value) { - if($cleanString) { - $value = $this->cleanString($value, $cleanString); - } - $retval = $this->create_list($retval, $value, $separator, $this->forceSqlQuotes); - } - //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - } - } - else { - //not an array. - $retval = NULL; - } - - return($retval); - }//end string_from_array() - //================================================================================================================ - - - - //================================================================================================================ - /** - * Easy way of cleaning data using types/styles of cleaning, with optional quoting. - * - * @param $cleanThis (str) data to be cleaned - * @param $cleanType (str,optional) how to clean the data. - * @param $sqlQuotes (bool,optional) quote the string for SQL - * - * @return (string) Cleaned data. - */ - function cleanString($cleanThis=NULL, $cleanType="all",$sqlQuotes=0) { - $cleanType = strtolower($cleanType); - switch ($cleanType) { - case "none": - //nothing to see here (no cleaning wanted/needed). Move along. - $sqlQuotes = 0; - break; - - case "query": - /* - replace \' with ' - gets rid of evil characters that might lead to SQL injection attacks. - replace line-break characters - */ - $evilChars = array("\$", "%", "~", "*",">", "<", "-", "{", "}", "[", "]", ")", "(", "&", "#", "?", ".", "\,","\/","\\","\"","\|","!","^","+","`","\n","\r"); - $cleanThis = preg_replace("/\|/","",$cleanThis); - $cleanThis = preg_replace("/\'/", "", $cleanThis); - $cleanThis = str_replace($evilChars,"", $cleanThis); - $cleanThis = stripslashes(addslashes($cleanThis)); - break; - - case "sql": - $cleanThis = addslashes(stripslashes($cleanThis)); - break; - - - case "sql_insert": - /* - * This is for descriptive fields, where double quotes don't need to be escaped: in these - * cases, escaping the double-quotes might lead to inserting something that looks different - * than the original, but in fact is identical. - */ - $cleanThis = addslashes(stripslashes($cleanThis)); - $cleanThis = preg_replace('/\\\\"/', '"', $cleanThis); - $cleanThis = preg_replace("/'/", "\\\'", $cleanThis); - - break; - - - case "sql92_insert": - /* - * Just like 'sql_insert', except that single quotes are "delimited" by - * adding another single quote, which works *at least* with postgres & sqlite. - */ - $cleanThis = preg_replace("/'/", "''", $cleanThis); - $cleanThis = preg_replace('/\\\\"/', '"', $cleanThis); - $cleanThis = stripslashes($cleanThis); - $sqlQuotes = 0; - break; - - case "double_quote": - //This will remove all double quotes from a string. - $cleanThis = str_replace('"',"",$cleanThis); - break; - - case "htmlspecial": - /* - This function is useful in preventing user-supplied text from containing HTML markup, such as in a message board or guest book application. - The translations performed are: - '&' (ampersand) becomes '&' - '"' (double quote) becomes '"'. - '<' (less than) becomes '<' - '>' (greater than) becomes '>' - */ - - $cleanThis = htmlspecialchars($cleanThis); - break; - - case "htmlspecial_q": - /* - '&' (ampersand) becomes '&' - '"' (double quote) becomes '"'. - ''' (single quote) becomes '''. - '<' (less than) becomes '<' - '>' (greater than) becomes '> - */ - $cleanThis = htmlspecialchars($cleanThis,ENT_QUOTES); - break; - - case "htmlspecial_nq": - /* - '&' (ampersand) becomes '&' - '<' (less than) becomes '<' - '>' (greater than) becomes '> - */ - $cleanThis = htmlspecialchars($cleanThis,ENT_NOQUOTES); - break; - - case "htmlentity": - /* - Convert all applicable text to its html entity - Will convert double-quotes and leave single-quotes alone - */ - $cleanThis = htmlentities(html_entity_decode($cleanThis)); - break; - - case "htmlentity_plus_brackets": - /* - Just like htmlentity, but also converts "{" and "}" (prevents template - from being incorrectly parse). - Also converts "{" and "}" to their html entity. - */ - $cleanThis = htmlentities(html_entity_decode($cleanThis)); - $cleanThis = str_replace('$', '$', $cleanThis); - $cleanThis = str_replace('{', '{', $cleanThis); - $cleanThis = str_replace('}', '}', $cleanThis); - break; - - case "double_entity": - //Removed double quotes, then calls html_entities on it. - $cleanThis = str_replace('"',"",$cleanThis); - $cleanThis = htmlentities(html_entity_decode($cleanThis)); - break; - - case "meta": - // Returns a version of str with a backslash character (\) before every character that is among these: - // . \\ + * ? [ ^ ] ( $ ) - $cleanThis = quotemeta($cleanThis); - break; - - case "email": - //Remove all characters that aren't allowed in an email address. - $cleanThis = preg_replace("/[^A-Za-z0-9\._@-]/","",$cleanThis); - break; - - case "email_plus_spaces": - //Remove all characters that aren't allowed in an email address. - $cleanThis = preg_replace("/[^A-Za-z0-9\ \._@-]/","",$cleanThis); - break; - - case "phone_fax": - //Remove everything that's not numeric or +()- example: +1 (555)-555-2020 is valid - $cleanThis = preg_replace("/[^0-9-+() ]/","",$cleanThis); - break; - - case "integer": - case "numeric": - //Remove everything that's not numeric. - if(is_null($cleanThis)) { - $cleanThis = "NULL"; - $sqlQuotes = 0; - } - else { - $cleanThis = preg_replace("/[^0-9]/","",$cleanThis); - } - break; - - case "decimal": - case "float": - //same as integer only the decimal point is allowed - $cleanThis = preg_replace("/[^0-9\.]/","",$cleanThis); - break; - - case "name": - case "names": - //allows only things in the "alpha" case and single quotes. - $cleanThis = preg_replace("/[^a-zA-Z']/", "", $cleanThis); - break; - - case "alpha": - //Removes anything that's not English a-zA-Z - $cleanThis = preg_replace("/[^a-zA-Z]/","",$cleanThis); - break; - - case "bool": - case "boolean": - //makes it either T or F (gotta lower the string & only check the first char to ensure accurate results). - $cleanThis = $this->interpret_bool($cleanThis, array('f', 't')); - break; - - case "varchar": - $cleanThis=$this->cleanString($cleanThis,"query"); - $cleanThis="'" . $cleanThis . "'"; - if($cleanThis == "''") { - $cleanThis="NULL"; - } - break; - - case "date": - $cleanThis = preg_replace("/[^0-9\-]/","",$cleanThis); - break; - - case "datetime": - $cleanThis=preg_replace("/[^A-Za-z0-9\/: \-\'\.]/","",$cleanThis); - break; - - case "all": - default: - // 1. Remove all naughty characters we can think of except alphanumeric. - $cleanThis = preg_replace("/[^A-Za-z0-9]/","",$cleanThis); - break; - - } - if($sqlQuotes) { - $cleanThis = "'". $cleanThis ."'"; - } - return $cleanThis; - }//end cleanString() - //================================================================================================================ - - - - - //================================================================================================================ - /** - * Returns a list delimited by the given delimiter. Does the work of checking if the given variable has data - * in it already, that needs to be added to, vs. setting the variable with the new content. - */ - public function create_list($string=NULL, $addThis=NULL, $delimiter=", ", $useSqlQuotes=0) { - if(strlen($string)) { - if($useSqlQuotes && !(preg_match("/^'/", $addThis) && preg_match("/'\$/", $addThis))) { - $addThis = "'". $addThis ."'"; - } - $retVal = $string . $delimiter . $addThis; - } - else { - $retVal = $addThis; - if($useSqlQuotes && !(preg_match("/^'/", $retVal) && preg_match("/'\$/", $retVal))) { - $retVal = "'". $retVal ."'"; - } - } - - return($retVal); - } //end create_list() - //================================================================================================================ - - - - //================================================================================================================ - /** - * A way of printing out human-readable information, especially arrays & objects, either to a web browser or via - * the command line. - * - * @param $input (mixed,optional) data to print/return - * @param $printItForMe (bool,optional) whether it should be printed or just returned. - * - * @return (string) printed data. - */ - public function debug_print($input=NULL, $printItForMe=NULL, $removeHR=NULL) { - if(!is_numeric($removeHR)) { - $removeHR = $this->debugRemoveHr; - } - - if(!is_numeric($printItForMe)) { - $printItForMe = $this->debugPrintOpt; - } - - ob_start(); - print_r($input); - $output = ob_get_contents(); - ob_end_clean(); - - $output = "<pre>$output</pre>"; - - if(!isset($_SERVER['SERVER_PROTOCOL']) || !$_SERVER['SERVER_PROTOCOL']) { - $output = strip_tags($output); - $hrString = "\n***************************************************************\n"; - } - else { - $hrString = "<hr>"; - } - if($removeHR) { - $hrString = NULL;; - } - - if($printItForMe) { - print "$output". $hrString ."\n"; - } - - return($output); - } //end debug_print() - //================================================================================================================ - - - - //================================================================================================================ - function swapValue(&$value, $c1, $c2) { - if(!$value) { - $value = $c1; - } - - - /* choose the next color */ - if($value == "$c1") { - $value = "$c2"; - } - else { - $value = "$c1"; - } - - return($value); - } - //================================================================================================================ - - - - //--------------------------------------------------------------------------------------------- - /** - * Using the given template, it will replace each index (in $repArr) with it's value: each - * var to be replaced must begin the given begin & end delimiters. - * - * @param $template (str) Data to perform the replacements on. - * @param $repArr (array) Array of name=>value pairs, where name is to be replaced with value. - * @param $b (str,optional) beginning delimiter. - * @param $e (str,optional) ending delimiter. - */ - public function mini_parser($template, $repArr, $b='%%', $e='%%') { - if(!isset($b) OR !isset($e)){ - $b="{"; - $e="}"; - } - - foreach($repArr as $key=>$value) { - //run the replacements. - $key = "$b" . $key . "$e"; - $template = str_replace("$key", $value, $template); - } - - return($template); - }//end mini_parser() - //--------------------------------------------------------------------------------------------- - - - //--------------------------------------------------------------------------------------------- - /** - * Takes the given string & truncates it so the final string is the given - * maximum length. Optionally adds a chunk of text to the end, and also - * optionally STRICTLY truncates (non-strict means the endString will be - * added blindly, while strict means the length of the endString will be - * subtracted from the total length, so the final string is EXACTLY the - * given length or shorter). - * - * @param string (str) the string to truncate - * @param $maxLength (int) maximum length for the result. - * @param $endString (str,optional) this is added to the end of the - * truncated string, if it exceeds $maxLength - * @param $strict (bool,optional) if non-strict, the length of - * the return would be $maxLength + length($endString) - */ - function truncate_string($string,$maxLength,$endString="...",$strict=FALSE) { - - //determine if it's even worth truncating. - if(is_string($string) && is_numeric($maxLength) && $maxLength > 0) { - $strLength = strlen($string); - if($strLength <= $maxLength) { - //no need to truncate. - $retval = $string; - } - else { - //actually needs to be truncated... - if($strict) { - $trueMaxLength = $maxLength - strlen($endString); - } - else { - $trueMaxLength = $maxLength; - } - - //rip the first ($trueMaxLength) characters from string, append $endString, and go. - $tmp = substr($string,0,$trueMaxLength); - $retval = $tmp . $endString; - } - } - else { - $retval = $string; - } - - return($retval); - - }//end truncate_string() - //--------------------------------------------------------------------------------------------- - - - - //########################################################################## - public function array_as_option_list(array $data, $checkedValue=NULL, $type="select", $useTemplateString=NULL, array $repArr=NULL) { - $typeArr = array ( - "select" => "selected", - "radio" => "checked", - "checkbox" => "checked" - ); - - $myType = $typeArr[$type]; - if(is_null($useTemplateString)) { - // - $useTemplateString = "\t\t<option value='%%value%%'%%selectedString%%>%%display%%</option>"; - } - - $retval = ""; - foreach($data as $value=>$display) { - //see if it's the value that's been selected. - $selectedString = ""; - if($value == $checkedValue || $display == $checkedValue) { - //yep, it's selected. - $selectedString = " ". $myType; - } - - //create the string. - $myRepArr = array( - 'value' => $value, - 'display' => $display, - 'selectedString' => $selectedString - ); - if(is_array($repArr) && is_array($repArr[$value])) { - //merge the arrays. - $myRepArr = array_merge($repArr[$value], $myRepArr); - } - $addThis = $this->mini_parser($useTemplateString, $myRepArr, "%%", "%%"); - $retval = $this->create_list($retval, $addThis, "\n"); - } - - return($retval); - }//end array_as_option_list() - //########################################################################## - - - - //########################################################################## - public function interpret_bool($interpretThis, array $trueFalseMapper=null) { - $interpretThis = preg_replace('/ /', '', $interpretThis); - if(is_array($trueFalseMapper)) { - if(count($trueFalseMapper) == 2 && isset($trueFalseMapper[0]) && isset($trueFalseMapper[1])) { - $realVals = $trueFalseMapper; - } - else { - throw new exception(__METHOD__ .": invalid true/false map"); - } - } - else { - //set an array that defines what "0" and "1" return. - $realVals = array( - 0 => false, - 1 => true - ); - } - - //now figure out the value to return. - if(is_numeric($interpretThis)) { - settype($interpretThis, 'integer'); - if($interpretThis == '0') { - $index=0; - } - else { - $index=1; - } - } - elseif(is_bool($interpretThis)) { - if($interpretThis == true) { - $index=1; - } - else { - $index=0; - } - } - elseif(preg_match('/^true$/i', $interpretThis) || preg_match('/^false$/', $interpretThis) || preg_match("/^[tf]$/", $interpretThis)) { - if(preg_match('/^true$/i', $interpretThis) || preg_match('/^t$/', $interpretThis)) { - $index=1; - } - else { - $index=0; - } - } - else { - //straight-up PHP if/else evaluation. - if($interpretThis) { - $index=1; - } - else { - $index=0; - } - } - - return($realVals[$index]); - }//end interpret_bool() - //########################################################################## - -}//end cs_globalFunctions{} - -?> Modified: trunk/1.0/sample_files/public_html/index.php =================================================================== --- trunk/1.0/sample_files/public_html/index.php 2009-01-29 21:15:20 UTC (rev 334) +++ trunk/1.0/sample_files/public_html/index.php 2009-01-29 21:25:58 UTC (rev 335) @@ -1,6 +1,6 @@ <?php -require(dirname(__FILE__) . "/../lib/cs-content/cs_globalFunctions.php"); +require(dirname(__FILE__) . "/../lib/cs-content/cs_globalFunctions.class.php"); $gf = new cs_globalFunctions; $gf->conditional_header("/content/index.php"); exit; Modified: trunk/1.0/tests/testOfCSContent.php =================================================================== --- trunk/1.0/tests/testOfCSContent.php 2009-01-29 21:15:20 UTC (rev 334) +++ trunk/1.0/tests/testOfCSContent.php 2009-01-29 21:25:58 UTC (rev 335) @@ -19,7 +19,7 @@ //------------------------------------------------------------------------- function __construct() { - require_once(dirname(__FILE__) .'/../cs_globalFunctions.php'); + require_once(dirname(__FILE__) .'/../cs_globalFunctions.class.php'); require_once(dirname(__FILE__) .'/../cs_siteConfig.class.php'); $this->gf = new cs_globalFunctions; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-01-29 22:49:19
|
Revision: 337 http://cs-content.svn.sourceforge.net/cs-content/?rev=337&view=rev Author: crazedsanity Date: 2009-01-29 21:48:59 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Renamed cs_tabsClass.php to cs_tabs.class.php /contentSystem.class.php: * MAIN::: -- fixed require_once() path to cs_tabs{} /cs_tabs.class.php [RENAMED FROM cs_tabsClass.php] /cs_tabsClass.php [RENAMED TO cs_tabs.class.php] Modified Paths: -------------- trunk/1.0/contentSystem.class.php Added Paths: ----------- trunk/1.0/cs_tabs.class.php Removed Paths: ------------- trunk/1.0/cs_tabsClass.php Modified: trunk/1.0/contentSystem.class.php =================================================================== --- trunk/1.0/contentSystem.class.php 2009-01-29 21:45:51 UTC (rev 336) +++ trunk/1.0/contentSystem.class.php 2009-01-29 21:48:59 UTC (rev 337) @@ -74,7 +74,7 @@ require_once(dirname(__FILE__) ."/cs_fileSystem.class.php"); require_once(dirname(__FILE__) ."/cs_sessionClass.php"); require_once(dirname(__FILE__) ."/cs_genericPage.class.php"); -require_once(dirname(__FILE__) ."/cs_tabsClass.php"); +require_once(dirname(__FILE__) ."/cs_tabs.class.php"); class contentSystem extends cs_contentAbstract { Copied: trunk/1.0/cs_tabs.class.php (from rev 333, trunk/1.0/cs_tabsClass.php) =================================================================== --- trunk/1.0/cs_tabs.class.php (rev 0) +++ trunk/1.0/cs_tabs.class.php 2009-01-29 21:48:59 UTC (rev 337) @@ -0,0 +1,152 @@ +<?php +/* + * Created on Jan 9, 2007 + * + */ + +require_once(dirname(__FILE__) .'/abstract/cs_content.abstract.class.php'); + + +class cs_tabs extends cs_contentAbstract { + private $tabsArr; + private $selectedTab; + + private $csPageObj; + private $templateVar; + + /** Block row with the "selected" tab */ + private $selectedTabContent; + + /** Block row with the "unselected" tab */ + private $unselectedTabContent; + + //--------------------------------------------------------------------------------------------- + /** + * Build the object, and parses the given template. Tabs must be added & selected manually. + * + * @param $csPageObj (object) Instance of the class "cs_genericPage". + * @param $templateVar (str,optional) What template var to find the tab blockrows in. + */ + public function __construct(cs_genericPage $csPageObj, $templateVar="tabs") { + parent::__construct(false); + if(is_null($csPageObj) || !is_object($csPageObj) || get_class($csPageObj) !== 'cs_genericPage') { + //can't continue without that! + throw new exception("cs_tabs::__construct(): cannot load without cs_genericPage{} object (". get_class($csPageObj) .")"); + } + else { + //set it as a member. + $this->csPageObj = $csPageObj; + } + + + if(is_null($templateVar) || strlen($templateVar) < 3) { + //no template name? AHH!!! + throw new exception("cs_tabs::__construct(): failed to specify proper template file"); + } + else { + //set the internal var. + $this->templateVar = $templateVar; + } + }//end __construct() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Loads & parses the given tabs template. Requires that the given template has "selected_tab" + * and "unselected_tab" block row definitions. + * + * @param (void) + * @return (void) + */ + private function load_tabs_template() { + //now let's parse it for the proper block rows. + $blockRows = $this->csPageObj->rip_all_block_rows($this->templateVar); + + if(count($blockRows) < 2 || !isset($blockRows['selected_tab']) || !isset($blockRows['unselected_tab'])) { + //not enough blocks, or they're not properly named. + throw new exception("cs_tabs::load_tabs_template(): failed to retrieve the required block rows"); + } + else { + //got the rows. Yay! + $this->selectedTabContent = $blockRows['selected_tab']; + $this->unselectedTabContent = $blockRows['unselected_tab']; + } + }//end load_tabs_template() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + public function add_tab_array(array $tabs) { + $retval = 0; + foreach($tabs as $name=>$url) { + //call an internal method to do it. + $retval += $this->add_tab($name, $url); + } + + return($retval); + }//end add_tab_array() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Sets the given tab as selected, provided it exists. + * + * @param $tabName (str) Sets this tab as selected. + * @return (void) + */ + public function select_tab($tabName) { + $this->selectedTab = $tabName; + }//end select_tab() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + public function add_tab($tabName, $url) { + //add it to an array. + $this->tabsArr[$tabName] = $url; + }//end add_tab() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Call this to add the parsed tabs into the page. + */ + public function display_tabs() { + if(is_array($this->tabsArr) && count($this->tabsArr)) { + $this->load_tabs_template(); + $finalString = ""; + //loop through the array. + foreach($this->tabsArr as $tabName=>$url) { + $useTabContent = $this->unselectedTabContent; + if(strtolower($tabName) === strtolower($this->selectedTab)) { + //it's selected. + $useTabContent = $this->selectedTabContent; + } + $parseThis = array( + 'title' => $tabName, + 'url' => $url + ); + $finalString .= $this->csPageObj->mini_parser($useTabContent, $parseThis, '%%', '%%'); + } + + //now parse it onto the page. + $this->csPageObj->add_template_var($this->templateVar, $finalString); + } + else { + //something bombed. + throw new exception("cs_tabs::display_tabs(): no tabs to add"); + } + + }//end display_tabs() + //--------------------------------------------------------------------------------------------- + +} +?> Property changes on: trunk/1.0/cs_tabs.class.php ___________________________________________________________________ Added: svn:mergeinfo + Added: svn:eol-style + native Deleted: trunk/1.0/cs_tabsClass.php =================================================================== --- trunk/1.0/cs_tabsClass.php 2009-01-29 21:45:51 UTC (rev 336) +++ trunk/1.0/cs_tabsClass.php 2009-01-29 21:48:59 UTC (rev 337) @@ -1,152 +0,0 @@ -<?php -/* - * Created on Jan 9, 2007 - * - */ - -require_once(dirname(__FILE__) .'/abstract/cs_content.abstract.class.php'); - - -class cs_tabs extends cs_contentAbstract { - private $tabsArr; - private $selectedTab; - - private $csPageObj; - private $templateVar; - - /** Block row with the "selected" tab */ - private $selectedTabContent; - - /** Block row with the "unselected" tab */ - private $unselectedTabContent; - - //--------------------------------------------------------------------------------------------- - /** - * Build the object, and parses the given template. Tabs must be added & selected manually. - * - * @param $csPageObj (object) Instance of the class "cs_genericPage". - * @param $templateVar (str,optional) What template var to find the tab blockrows in. - */ - public function __construct(cs_genericPage $csPageObj, $templateVar="tabs") { - parent::__construct(false); - if(is_null($csPageObj) || !is_object($csPageObj) || get_class($csPageObj) !== 'cs_genericPage') { - //can't continue without that! - throw new exception("cs_tabs::__construct(): cannot load without cs_genericPage{} object (". get_class($csPageObj) .")"); - } - else { - //set it as a member. - $this->csPageObj = $csPageObj; - } - - - if(is_null($templateVar) || strlen($templateVar) < 3) { - //no template name? AHH!!! - throw new exception("cs_tabs::__construct(): failed to specify proper template file"); - } - else { - //set the internal var. - $this->templateVar = $templateVar; - } - }//end __construct() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Loads & parses the given tabs template. Requires that the given template has "selected_tab" - * and "unselected_tab" block row definitions. - * - * @param (void) - * @return (void) - */ - private function load_tabs_template() { - //now let's parse it for the proper block rows. - $blockRows = $this->csPageObj->rip_all_block_rows($this->templateVar); - - if(count($blockRows) < 2 || !isset($blockRows['selected_tab']) || !isset($blockRows['unselected_tab'])) { - //not enough blocks, or they're not properly named. - throw new exception("cs_tabs::load_tabs_template(): failed to retrieve the required block rows"); - } - else { - //got the rows. Yay! - $this->selectedTabContent = $blockRows['selected_tab']; - $this->unselectedTabContent = $blockRows['unselected_tab']; - } - }//end load_tabs_template() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - public function add_tab_array(array $tabs) { - $retval = 0; - foreach($tabs as $name=>$url) { - //call an internal method to do it. - $retval += $this->add_tab($name, $url); - } - - return($retval); - }//end add_tab_array() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Sets the given tab as selected, provided it exists. - * - * @param $tabName (str) Sets this tab as selected. - * @return (void) - */ - public function select_tab($tabName) { - $this->selectedTab = $tabName; - }//end select_tab() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - public function add_tab($tabName, $url) { - //add it to an array. - $this->tabsArr[$tabName] = $url; - }//end add_tab() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Call this to add the parsed tabs into the page. - */ - public function display_tabs() { - if(is_array($this->tabsArr) && count($this->tabsArr)) { - $this->load_tabs_template(); - $finalString = ""; - //loop through the array. - foreach($this->tabsArr as $tabName=>$url) { - $useTabContent = $this->unselectedTabContent; - if(strtolower($tabName) === strtolower($this->selectedTab)) { - //it's selected. - $useTabContent = $this->selectedTabContent; - } - $parseThis = array( - 'title' => $tabName, - 'url' => $url - ); - $finalString .= $this->csPageObj->mini_parser($useTabContent, $parseThis, '%%', '%%'); - } - - //now parse it onto the page. - $this->csPageObj->add_template_var($this->templateVar, $finalString); - } - else { - //something bombed. - throw new exception("cs_tabs::display_tabs(): no tabs to add"); - } - - }//end display_tabs() - //--------------------------------------------------------------------------------------------- - -} -?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-01-29 22:49:43
|
Revision: 336 http://cs-content.svn.sourceforge.net/cs-content/?rev=336&view=rev Author: crazedsanity Date: 2009-01-29 21:45:51 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Renamed cs_phpDB.php to cs_phpDB.class.php. /cs_phpDB.class.php [RENAMED FROM cs_phpDB.php] /cs_phpDB.php [RENAMED TO cs_phpDB.class.php] Added Paths: ----------- trunk/1.0/cs_phpDB.class.php Removed Paths: ------------- trunk/1.0/cs_phpDB.php Copied: trunk/1.0/cs_phpDB.class.php (from rev 333, trunk/1.0/cs_phpDB.php) =================================================================== --- trunk/1.0/cs_phpDB.class.php (rev 0) +++ trunk/1.0/cs_phpDB.class.php 2009-01-29 21:45:51 UTC (rev 336) @@ -0,0 +1,74 @@ +<?php + +/* + * A class for generic PostgreSQL database access. + * + * SVN INFORMATION::: + * SVN Signature:::::::: $Id$ + * Last Committted Date: $Date$ + * Last Committed Path:: $HeadURL$ + * + */ + +/////////////////////// +// ORIGINATION INFO: +// Author: Trevin Chow (with contributions from Lee Pang, wle...@ho...) +// Email: t1...@ma... +// Date: February 21, 2000 +// Last Updated: August 14, 2001 +// +// Description: +// Abstracts both the php function calls and the server information to POSTGRES +// databases. Utilizes class variables to maintain connection information such +// as number of rows, result id of last operation, etc. +// +/////////////////////// + +require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); +require_once(dirname(__FILE__) ."/abstract/cs_phpDB.abstract.class.php"); + +class cs_phpDB extends cs_contentAbstract { + + private $dbLayerObj; + private $dbType; + + //========================================================================= + public function __construct($type='pgsql') { + + if(strlen($type)) { + + require_once(dirname(__FILE__) .'/db_types/'. __CLASS__ .'__'. $type .'.class.php'); + $className = __CLASS__ .'__'. $type; + $this->dbLayerObj = new $className; + $this->dbType = $type; + + parent::__construct(); + + $this->isInitialized = TRUE; + } + else { + throw new exception(__METHOD__ .": failed to give a type (". $type .")"); + } + }//end __construct() + //========================================================================= + + + + //========================================================================= + /** + * Magic method to call methods within the database abstraction layer ($this->dbLayerObj). + */ + public function __call($methodName, $args) { + if(method_exists($this->dbLayerObj, $methodName)) { + $retval = call_user_func_array(array($this->dbLayerObj, $methodName), $args); + } + else { + throw new exception(__METHOD__ .': unsupported method ('. $methodName .') for database of type ('. $this->dbType .')'); + } + return($retval); + }//end __call() + //========================================================================= + +} // end class phpDB + +?> Property changes on: trunk/1.0/cs_phpDB.class.php ___________________________________________________________________ Added: svn:eol + native Added: svn:keywords + Id HeadURL Date Revision Author Added: svn:mergeinfo + Added: svn:eol-style + native Deleted: trunk/1.0/cs_phpDB.php =================================================================== --- trunk/1.0/cs_phpDB.php 2009-01-29 21:25:58 UTC (rev 335) +++ trunk/1.0/cs_phpDB.php 2009-01-29 21:45:51 UTC (rev 336) @@ -1,74 +0,0 @@ -<?php - -/* - * A class for generic PostgreSQL database access. - * - * SVN INFORMATION::: - * SVN Signature:::::::: $Id$ - * Last Committted Date: $Date$ - * Last Committed Path:: $HeadURL$ - * - */ - -/////////////////////// -// ORIGINATION INFO: -// Author: Trevin Chow (with contributions from Lee Pang, wle...@ho...) -// Email: t1...@ma... -// Date: February 21, 2000 -// Last Updated: August 14, 2001 -// -// Description: -// Abstracts both the php function calls and the server information to POSTGRES -// databases. Utilizes class variables to maintain connection information such -// as number of rows, result id of last operation, etc. -// -/////////////////////// - -require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); -require_once(dirname(__FILE__) ."/abstract/cs_phpDB.abstract.class.php"); - -class cs_phpDB extends cs_contentAbstract { - - private $dbLayerObj; - private $dbType; - - //========================================================================= - public function __construct($type='pgsql') { - - if(strlen($type)) { - - require_once(dirname(__FILE__) .'/db_types/'. __CLASS__ .'__'. $type .'.class.php'); - $className = __CLASS__ .'__'. $type; - $this->dbLayerObj = new $className; - $this->dbType = $type; - - parent::__construct(); - - $this->isInitialized = TRUE; - } - else { - throw new exception(__METHOD__ .": failed to give a type (". $type .")"); - } - }//end __construct() - //========================================================================= - - - - //========================================================================= - /** - * Magic method to call methods within the database abstraction layer ($this->dbLayerObj). - */ - public function __call($methodName, $args) { - if(method_exists($this->dbLayerObj, $methodName)) { - $retval = call_user_func_array(array($this->dbLayerObj, $methodName), $args); - } - else { - throw new exception(__METHOD__ .': unsupported method ('. $methodName .') for database of type ('. $this->dbType .')'); - } - return($retval); - }//end __call() - //========================================================================= - -} // end class phpDB - -?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-01-29 22:49:35
|
Revision: 338 http://cs-content.svn.sourceforge.net/cs-content/?rev=338&view=rev Author: crazedsanity Date: 2009-01-29 21:50:55 +0000 (Thu, 29 Jan 2009) Log Message: ----------- Move the "template.inc" file into a "required" folder to cleanup the main directory just a bit. Modified Paths: -------------- trunk/1.0/cs_genericPage.class.php Added Paths: ----------- trunk/1.0/required/ trunk/1.0/required/template.inc Removed Paths: ------------- trunk/1.0/template.inc Modified: trunk/1.0/cs_genericPage.class.php =================================================================== --- trunk/1.0/cs_genericPage.class.php 2009-01-29 21:48:59 UTC (rev 337) +++ trunk/1.0/cs_genericPage.class.php 2009-01-29 21:50:55 UTC (rev 338) @@ -7,7 +7,7 @@ * $LastChangedBy$ * $LastChangedRevision$ */ -require_once(dirname(__FILE__) ."/template.inc"); +require_once(dirname(__FILE__) ."/required/template.inc"); require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); class cs_genericPage extends cs_contentAbstract { Copied: trunk/1.0/required/template.inc (from rev 333, trunk/1.0/template.inc) =================================================================== --- trunk/1.0/required/template.inc (rev 0) +++ trunk/1.0/required/template.inc 2009-01-29 21:50:55 UTC (rev 338) @@ -0,0 +1,1013 @@ +<?php +/* + * Session Management for PHP3 + * + * (C) Copyright 1999-2000 NetUSE GmbH + * Kristian Koehntopp + * + * $Id: template.inc,v 1.15 2004/07/23 20:36:29 layne_weathers Exp $ + * + */ + +/* + * Change log since version 7.2c + * + * Bug fixes to version 7.2c compiled by Richard Archer <rh...@ju...>: + * (credits given to first person to post a diff to phplib mailing list) + * + * Normalised all comments and whitespace (rha) + * replaced "$handle" with "$varname" and "$h" with "$v" throughout (from phplib-devel) + * added braces around all one-line if statements in: get_undefined, loadfile and halt (rha) + * set_var was missing two sets of braces (rha) + * added a couple of "return true" statements (rha) + * set_unknowns had "keep" as default instead of "remove" (from phplib-devel) + * set_file failed to check for empty strings if passed an array of filenames (phplib-devel) + * remove @ from call to preg_replace in subst -- report errors if there are any (NickM) + * set_block unnecessarily required a newline in the template file (Marc Tardif) + * pparse now calls this->finish to replace undefined vars (Layne Weathers) + * get_var now checks for unset varnames (NickM & rha) + * get_var when passed an array used the array key instead of the value (rha) + * get_vars now uses a call to get_var rather than this->varvals to prevent undefined var warning (rha) + * in finish, the replacement string referenced an unset variable (rha) + * loadfile would try to load a file if the varval had been set to "" (rha) + * in get_undefined, only match non-whitespace in variable tags as in finish (Layne Weathers & rha) + * more elegant fix to the problem of subst stripping '$n', '\n' and '\\' strings (rha) + * parse uses get_var to obtain return value (Jordi via SF) + * set_block reports an error if the block could not be extracted (rha) + * filename is now windows-pathname aware (krabu @ SF) + * + * + * Changes in functionality which go beyond bug fixes: + * + * added ability for filename comments to be output (from phplib-users layne) + * changed debug handling so set, get and internals can be tracked separately (rha) + * added debug statements throughout to track most function calls (rha) + * debug output contained raw HTML -- is now escaped with htmlentities (rha) + * Alter regex in set_block to remove more whitespace around BEGIN/END tags to improve HTML layout (rha) + * Add "append" option to set_var, works just like append in parse (dale at linuxwebpro.com, rha) + * Altered parse so that append is honored if passed an array (Brian) + * Converted comments and documentation to phpdoc style (rha) + * Added clear_var to set the value of variables to "" (rha) + * Added unset_var to usset variables (rha) + * + */ + +/** + * The template class allows you to keep your HTML code in some external files + * which are completely free of PHP code, but contain replacement fields. + * The class provides you with functions which can fill in the replacement fields + * with arbitrary strings. These strings can become very large, e.g. entire tables. + * + * Note: If you think that this is like FastTemplates, read carefully. It isn't. + */ + +class Template +{ + /** + * Serialization helper, the name of this class. + * + * @var string + * @access public + */ + var $classname = "Template"; + + /** + * Determines how much debugging output Template will produce. + * This is a bitwise mask of available debug levels: + * 0 = no debugging + * 1 = debug variable assignments + * 2 = debug calls to get variable + * 4 = debug internals (outputs all function calls with parameters). + * + * Note: setting $this->debug = true will enable debugging of variable + * assignments only which is the same behaviour as versions up to release 7.2d. + * + * @var int + * @access public + */ + var $debug = false; + + /** + * Determines whether Template outputs filename comments. + * false = no filename outputs + * true = HTML comments (e.g. <!-- START FILE $filename -->) placed in output + * + * @var int + * @access public + */ + var $filename_comments = false; + + /** + * Determines the regular expression used to find unknown variable tags. + * "loose" = traditional match all curly braces with no whitespace between + * "strict" = adopts PHP's variable naming rules + * ("loose" has a nasty habit of deleting JavaScript RegEx components) + * (should future major version releases of PHPLib default this "strict"?) + * + * @var string + * @access public + */ + var $unknown_regexp = "loose"; + + /** + * The base directory from which template files are loaded. + * + * @var string + * @access private + * @see set_root + */ + var $root = "."; + + /** + * A hash of strings forming a translation table which translates variable names + * into names of files containing the variable content. + * $file[varname] = "filename"; + * + * @var array + * @access private + * @see set_file + */ + var $file = array(); + + /** + * A hash of strings forming a translation table which translates variable names + * into regular expressions for themselves. + * $varkeys[varname] = "/varname/" + * + * @var array + * @access private + * @see set_var + */ + var $varkeys = array(); + + /** + * A hash of strings forming a translation table which translates variable names + * into values for their respective varkeys. + * $varvals[varname] = "value" + * + * @var array + * @access private + * @see set_var + */ + var $varvals = array(); + + /** + * Determines how to output variable tags with no assigned value in templates. + * + * @var string + * @access private + * @see set_unknowns + */ + var $unknowns = "remove"; + + /** + * Determines how Template handles error conditions. + * "yes" = the error is reported, then execution is halted + * "report" = the error is reported, then execution continues by returning "false" + * "no" = errors are silently ignored, and execution resumes reporting "false" + * + * @var string + * @access public + * @see halt + */ + var $halt_on_error = "yes"; + + /** + * The last error message is retained in this variable. + * + * @var string + * @access public + * @see halt + */ + var $last_error = ""; + + /****************************************************************************** + * Class constructor. May be called with two optional parameters. + * The first parameter sets the template directory the second parameter + * sets the policy regarding handling of unknown variables. + * + * usage: Template([string $root = "."], [string $unknowns = "remove"]) + * + * @param $root path to template directory + * @param $string what to do with undefined variables + * @see set_root + * @see set_unknowns + * @access public + * @return void + */ + function Template($root = ".", $unknowns = "remove") { + if ($this->debug & 4) { + echo "<p><b>Template:</b> root = $root, unknowns = $unknowns</p>\n"; + } + $this->set_root($root); + $this->set_unknowns($unknowns); + } + + + /****************************************************************************** + * Checks that $root is a valid directory and if so sets this directory as the + * base directory from which templates are loaded by storing the value in + * $this->root. Relative filenames are prepended with the path in $this->root. + * + * Returns true on success, false on error. + * + * usage: set_root(string $root) + * + * @param $root string containing new template directory + * @see root + * @access public + * @return boolean + */ + function set_root($root) { + if(ereg('/$', $root)) { + $root = substr($root, 0, -1); + } + if ($this->debug & 4) { + echo "<p><b>set_root:</b> root = $root</p>\n"; + } + if (!is_dir($root)) { + $this->halt("set_root: $root is not a directory."); + return false; + } + + $this->root = $root; + return true; + } + + + /****************************************************************************** + * Sets the policy for dealing with unresolved variable names. + * + * unknowns defines what to do with undefined template variables + * "remove" = remove undefined variables + * "comment" = replace undefined variables with comments + * "keep" = keep undefined variables + * + * Note: "comment" can cause unexpected results when the variable tag is embedded + * inside an HTML tag, for example a tag which is expected to be replaced with a URL. + * + * usage: set_unknowns(string $unknowns) + * + * @param $unknowns new value for unknowns + * @see unknowns + * @access public + * @return void + */ + function set_unknowns($unknowns = "remove") { + if ($this->debug & 4) { + echo "<p><b>unknowns:</b> unknowns = $unknowns</p>\n"; + } + $this->unknowns = $unknowns; + } + + + /****************************************************************************** + * Defines a filename for the initial value of a variable. + * + * It may be passed either a varname and a file name as two strings or + * a hash of strings with the key being the varname and the value + * being the file name. + * + * The new mappings are stored in the array $this->file. + * The files are not loaded yet, but only when needed. + * + * Returns true on success, false on error. + * + * usage: set_file(array $filelist = (string $varname => string $filename)) + * or + * usage: set_file(string $varname, string $filename) + * + * @param $varname either a string containing a varname or a hash of varname/file name pairs. + * @param $filename if varname is a string this is the filename otherwise filename is not required + * @access public + * @return boolean + */ + function set_file($varname, $filename = "") { + if (!is_array($varname)) { + if ($this->debug & 4) { + echo "<p><b>set_file:</b> (with scalar) varname = $varname, filename = $filename</p>\n"; + } + if ($filename == "") { + $this->halt("set_file: For varname $varname filename is empty."); + return false; + } + $this->file[$varname] = $this->filename($filename); + } else { + reset($varname); + while (list($v, $f) = each($varname)) { + if ($this->debug & 4) { + echo "<p><b>set_file:</b> (with array) varname = $v, filename = $f</p>\n"; + } + if ($f == "") { + $this->halt("set_file: For varname $v filename is empty."); + return false; + } + $this->file[$v] = $this->filename($f); + } + } + return true; + } + + + /****************************************************************************** + * A variable $parent may contain a variable block defined by: + * <!-- BEGIN $varname --> content <!-- END $varname -->. This function removes + * that block from $parent and replaces it with a variable reference named $name. + * The block is inserted into the varkeys and varvals hashes. If $name is + * omitted, it is assumed to be the same as $varname. + * + * Blocks may be nested but care must be taken to extract the blocks in order + * from the innermost block to the outermost block. + * + * Returns true on success, false on error. + * + * usage: set_block(string $parent, string $varname, [string $name = ""]) + * + * @param $parent a string containing the name of the parent variable + * @param $varname a string containing the name of the block to be extracted + * @param $name the name of the variable in which to store the block + * @access public + * @return boolean + */ + function set_block($parent, $varname, $name = "") { + if ($this->debug & 4) { + echo "<p><b>set_block:</b> parent = $parent, varname = $varname, name = $name</p>\n"; + } + if (!$this->loadfile($parent)) { + $this->halt("set_block: unable to load $parent."); + return false; + } + if ($name == "") { + $name = $varname; + } + + $str = $this->get_var($parent); + $reg = "/[ \t]*<!--\s+BEGIN $varname\s+-->\s*?\n?(\s*.*?\n?)\s*<!--\s+END $varname\s+-->\s*?\n?/sm"; + preg_match_all($reg, $str, $m); + if (!isset($m[1][0])) { + $this->halt("set_block: unable to set block $varname."); + return false; + } + $str = preg_replace($reg, "{" . $name . "}", $str); + $this->set_var($varname, $m[1][0]); + $this->set_var($parent, $str); + return true; + } + + + /****************************************************************************** + * This functions sets the value of a variable. + * + * It may be called with either a varname and a value as two strings or an + * an associative array with the key being the varname and the value being + * the new variable value. + * + * The function inserts the new value of the variable into the $varkeys and + * $varvals hashes. It is not necessary for a variable to exist in these hashes + * before calling this function. + * + * An optional third parameter allows the value for each varname to be appended + * to the existing variable instead of replacing it. The default is to replace. + * This feature was introduced after the 7.2d release. + * + * + * usage: set_var(string $varname, [string $value = ""], [boolean $append = false]) + * or + * usage: set_var(array $varname = (string $varname => string $value), [mixed $dummy_var], [boolean $append = false]) + * + * @param $varname either a string containing a varname or a hash of varname/value pairs. + * @param $value if $varname is a string this contains the new value for the variable otherwise this parameter is ignored + * @param $append if true, the value is appended to the variable's existing value + * @access public + * @return void + */ + function set_var($varname, $value = "", $append = false) { + if (!is_array($varname)) { + if (!empty($varname)) { + if ($this->debug & 1) { + printf("<b>set_var:</b> (with scalar) <b>%s</b> = '%s'<br>\n", $varname, htmlentities($value)); + } + $this->varkeys[$varname] = "/".$this->varname($varname)."/"; + if ($append && isset($this->varvals[$varname])) { + $this->varvals[$varname] .= $value; + } else { + $this->varvals[$varname] = $value; + } + } + } else { + reset($varname); + while (list($k, $v) = each($varname)) { + if (!empty($k)) { + if ($this->debug & 1) { + printf("<b>set_var:</b> (with array) <b>%s</b> = '%s'<br>\n", $k, htmlentities($v)); + } + $this->varkeys[$k] = "/".$this->varname($k)."/"; + if ($append && isset($this->varvals[$k])) { + $this->varvals[$k] .= $v; + } else { + $this->varvals[$k] = $v; + } + } + } + } + } + + + /****************************************************************************** + * This functions clears the value of a variable. + * + * It may be called with either a varname as a string or an array with the + * values being the varnames to be cleared. + * + * The function sets the value of the variable in the $varkeys and $varvals + * hashes to "". It is not necessary for a variable to exist in these hashes + * before calling this function. + * + * + * usage: clear_var(string $varname) + * or + * usage: clear_var(array $varname = (string $varname)) + * + * @param $varname either a string containing a varname or an array of varnames. + * @access public + * @return void + */ + function clear_var($varname) { + if (!is_array($varname)) { + if (!empty($varname)) { + if ($this->debug & 1) { + printf("<b>clear_var:</b> (with scalar) <b>%s</b><br>\n", $varname); + } + $this->set_var($varname, ""); + } + } else { + reset($varname); + while (list($k, $v) = each($varname)) { + if (!empty($v)) { + if ($this->debug & 1) { + printf("<b>clear_var:</b> (with array) <b>%s</b><br>\n", $v); + } + $this->set_var($v, ""); + } + } + } + } + + + /****************************************************************************** + * This functions unsets a variable completely. + * + * It may be called with either a varname as a string or an array with the + * values being the varnames to be cleared. + * + * The function removes the variable from the $varkeys and $varvals hashes. + * It is not necessary for a variable to exist in these hashes before calling + * this function. + * + * + * usage: unset_var(string $varname) + * or + * usage: unset_var(array $varname = (string $varname)) + * + * @param $varname either a string containing a varname or an array of varnames. + * @access public + * @return void + */ + function unset_var($varname) { + if (!is_array($varname)) { + if (!empty($varname)) { + if ($this->debug & 1) { + printf("<b>unset_var:</b> (with scalar) <b>%s</b><br>\n", $varname); + } + unset($this->varkeys[$varname]); + unset($this->varvals[$varname]); + } + } else { + reset($varname); + while (list($k, $v) = each($varname)) { + if (!empty($v)) { + if ($this->debug & 1) { + printf("<b>unset_var:</b> (with array) <b>%s</b><br>\n", $v); + } + unset($this->varkeys[$v]); + unset($this->varvals[$v]); + } + } + } + } + + + /****************************************************************************** + * This function fills in all the variables contained within the variable named + * $varname. The resulting value is returned as the function result and the + * original value of the variable varname is not changed. The resulting string + * is not "finished", that is, the unresolved variable name policy has not been + * applied yet. + * + * Returns: the value of the variable $varname with all variables substituted. + * + * usage: subst(string $varname) + * + * @param $varname the name of the variable within which variables are to be substituted + * @access public + * @return string + */ + function subst($varname) { + $varvals_quoted = array(); + if ($this->debug & 4) { + echo "<p><b>subst:</b> varname = $varname</p>\n"; + } + if (!$this->loadfile($varname)) { + $this->halt("subst: unable to load $varname."); + return false; + } + + // quote the replacement strings to prevent bogus stripping of special chars + reset($this->varvals); + while (list($k, $v) = each($this->varvals)) { + $varvals_quoted[$k] = preg_replace(array('/\\\\/', '/\$/'), array('\\\\\\\\', '\\\\$'), $v); + } + + $str = $this->get_var($varname); + $str = preg_replace($this->varkeys, $varvals_quoted, $str); + return $str; + } + + + /****************************************************************************** + * This is shorthand for print $this->subst($varname). See subst for further + * details. + * + * Returns: always returns false. + * + * usage: psubst(string $varname) + * + * @param $varname the name of the variable within which variables are to be substituted + * @access public + * @return false + * @see subst + */ + function psubst($varname) { + if ($this->debug & 4) { + echo "<p><b>psubst:</b> varname = $varname</p>\n"; + } + print $this->subst($varname); + + return false; + } + + + /****************************************************************************** + * The function substitutes the values of all defined variables in the variable + * named $varname and stores or appends the result in the variable named $target. + * + * It may be called with either a target and a varname as two strings or a + * target as a string and an array of variable names in varname. + * + * The function inserts the new value of the variable into the $varkeys and + * $varvals hashes. It is not necessary for a variable to exist in these hashes + * before calling this function. + * + * An optional third parameter allows the value for each varname to be appended + * to the existing target variable instead of replacing it. The default is to + * replace. + * + * If $target and $varname are both strings, the substituted value of the + * variable $varname is inserted into or appended to $target. + * + * If $handle is an array of variable names the variables named by $handle are + * sequentially substituted and the result of each substitution step is + * inserted into or appended to in $target. The resulting substitution is + * available in the variable named by $target, as is each intermediate step + * for the next $varname in sequence. Note that while it is possible, it + * is only rarely desirable to call this function with an array of varnames + * and with $append = true. This append feature was introduced after the 7.2d + * release. + * + * Returns: the last value assigned to $target. + * + * usage: parse(string $target, string $varname, [boolean $append]) + * or + * usage: parse(string $target, array $varname = (string $varname), [boolean $append]) + * + * @param $target a string containing the name of the variable into which substituted $varnames are to be stored + * @param $varname if a string, the name the name of the variable to substitute or if an array a list of variables to be substituted + * @param $append if true, the substituted variables are appended to $target otherwise the existing value of $target is replaced + * @access public + * @return string + * @see subst + */ + function parse($target, $varname, $append = false) { + if (!is_array($varname)) { + if ($this->debug & 4) { + echo "<p><b>parse:</b> (with scalar) target = $target, varname = $varname, append = $append</p>\n"; + } + $str = $this->subst($varname); + if ($append) { + $this->set_var($target, $this->get_var($target) . $str); + } else { + $this->set_var($target, $str); + } + } else { + reset($varname); + while (list($i, $v) = each($varname)) { + if ($this->debug & 4) { + echo "<p><b>parse:</b> (with array) target = $target, i = $i, varname = $v, append = $append</p>\n"; + } + $str = $this->subst($v); + if ($append) { + $this->set_var($target, $this->get_var($target) . $str); + } else { + $this->set_var($target, $str); + } + } + } + + if ($this->debug & 4) { + echo "<p><b>parse:</b> completed</p>\n"; + } + return $this->get_var($target); + } + + + /****************************************************************************** + * This is shorthand for print $this->parse(...) and is functionally identical. + * See parse for further details. + * + * Returns: always returns false. + * + * usage: pparse(string $target, string $varname, [boolean $append]) + * or + * usage: pparse(string $target, array $varname = (string $varname), [boolean $append]) + * + * @param $target a string containing the name of the variable into which substituted $varnames are to be stored + * @param $varname if a string, the name the name of the variable to substitute or if an array a list of variables to be substituted + * @param $append if true, the substituted variables are appended to $target otherwise the existing value of $target is replaced + * @access public + * @return false + * @see parse + */ + function pparse($target, $varname, $append = false) { + if ($this->debug & 4) { + echo "<p><b>pparse:</b> passing parameters to parse...</p>\n"; + } + print $this->finish($this->parse($target, $varname, $append)); + return false; + } + + + /****************************************************************************** + * This function returns an associative array of all defined variables with the + * name as the key and the value of the variable as the value. + * + * This is mostly useful for debugging. Also note that $this->debug can be used + * to echo all variable assignments as they occur and to trace execution. + * + * Returns: a hash of all defined variable values keyed by their names. + * + * usage: get_vars() + * + * @access public + * @return array + * @see $debug + */ + function get_vars() { + if ($this->debug & 4) { + echo "<p><b>get_vars:</b> constructing array of vars...</p>\n"; + } + reset($this->varkeys); + while (list($k, $v) = each($this->varkeys)) { + $result[$k] = $this->get_var($k); + } + return $result; + } + + + /****************************************************************************** + * This function returns the value of the variable named by $varname. + * If $varname references a file and that file has not been loaded yet, the + * variable will be reported as empty. + * + * When called with an array of variable names this function will return a a + * hash of variable values keyed by their names. + * + * Returns: a string or an array containing the value of $varname. + * + * usage: get_var(string $varname) + * or + * usage: get_var(array $varname) + * + * @param $varname if a string, the name the name of the variable to get the value of, or if an array a list of variables to return the value of + * @access public + * @return string or array + */ + function get_var($varname) { + if (!is_array($varname)) { + if (isset($this->varvals[$varname])) { + $str = $this->varvals[$varname]; + } else { + $str = ""; + } + if ($this->debug & 2) { + printf ("<b>get_var</b> (with scalar) <b>%s</b> = '%s'<br>\n", $varname, htmlentities($str)); + } + return $str; + } else { + reset($varname); + while (list($k, $v) = each($varname)) { + if (isset($this->varvals[$v])) { + $str = $this->varvals[$v]; + } else { + $str = ""; + } + if ($this->debug & 2) { + printf ("<b>get_var:</b> (with array) <b>%s</b> = '%s'<br>\n", $v, htmlentities($str)); + } + $result[$v] = $str; + } + return $result; + } + } + + + /****************************************************************************** + * This function returns a hash of unresolved variable names in $varname, keyed + * by their names (that is, the hash has the form $a[$name] = $name). + * + * Returns: a hash of varname/varname pairs or false on error. + * + * usage: get_undefined(string $varname) + * + * @param $varname a string containing the name the name of the variable to scan for unresolved variables + * @access public + * @return array + */ + function get_undefined($varname) { + if ($this->debug & 4) { + echo "<p><b>get_undefined:</b> varname = $varname</p>\n"; + } + if (!$this->loadfile($varname)) { + $this->halt("get_undefined: unable to load $varname."); + return false; + } + + preg_match_all( + (("loose" == $this->unknown_regexp) ? "/{([^ \t\r\n}]+)}/" : "/{([_a-zA-Z]\\w+)}/"), + $this->get_var($varname), + $m); + $m = $m[1]; + if (!is_array($m)) { + return false; + } + + reset($m); + while (list($k, $v) = each($m)) { + if (!isset($this->varkeys[$v])) { + if ($this->debug & 4) { + echo "<p><b>get_undefined:</b> undefined: $v</p>\n"; + } + $result[$v] = $v; + } + } + + if (count($result)) { + return $result; + } else { + return false; + } + } + + + /****************************************************************************** + * This function returns the finished version of $str. That is, the policy + * regarding unresolved variable names will be applied to $str. + * + * Returns: a finished string derived from $str and $this->unknowns. + * + * usage: finish(string $str) + * + * @param $str a string to which to apply the unresolved variable policy + * @access public + * @return string + * @see set_unknowns + */ + function finish($str) { + switch ($this->unknowns) { + case "keep": + break; + + case "remove": + $str = preg_replace( + (("loose" == $this->unknown_regexp) ? "/{([^ \t\r\n}]+)}/" : "/{([_a-zA-Z]\\w+)}/"), + "", + $str); + break; + + case "comment": + $str = preg_replace( + (("loose" == $this->unknown_regexp) ? "/{([^ \t\r\n}]+)}/" : "/{([_a-zA-Z]\\w+)}/"), + "<!-- Template variable \\1 undefined -->", + $str); + break; + } + + return $str; + } + + + /****************************************************************************** + * This function prints the finished version of the value of the variable named + * by $varname. That is, the policy regarding unresolved variable names will be + * applied to the variable $varname then it will be printed. + * + * usage: p(string $varname) + * + * @param $varname a string containing the name of the variable to finish and print + * @access public + * @return void + * @see set_unknowns + * @see finish + */ + function p($varname) { + print $this->finish($this->get_var($varname)); + } + + + /****************************************************************************** + * This function returns the finished version of the value of the variable named + * by $varname. That is, the policy regarding unresolved variable names will be + * applied to the variable $varname and the result returned. + * + * Returns: a finished string derived from the variable $varname. + * + * usage: get(string $varname) + * + * @param $varname a string containing the name of the variable to finish + * @access public + * @return void + * @see set_unknowns + * @see finish + */ + function get($varname) { + return $this->finish($this->get_var($varname)); + } + + + /****************************************************************************** + * When called with a relative pathname, this function will return the pathname + * with $this->root prepended. Absolute pathnames are returned unchanged. + * + * Returns: a string containing an absolute pathname. + * + * usage: filename(string $filename) + * + * @param $filename a string containing a filename + * @access private + * @return string + * @see set_root + */ + function filename($filename) { + if ($this->debug & 4) { + echo "<p><b>filename:</b> filename = $filename</p>\n"; + } + if (substr($filename, 0, 1) != "/" + && substr($filename, 0, 1) != "\\" + && substr($filename, 1, 2) != ":\\" + && substr($filename, 1, 2) != ":/" + ) { + $filename = $this->root."/".$filename; + } + + if (!file_exists($filename)) { + $this->halt("filename: file $filename does not exist."); + } + return $filename; + } + + + /****************************************************************************** + * This function will construct a regexp for a given variable name with any + * special chars quoted. + * + * Returns: a string containing an escaped variable name. + * + * usage: varname(string $varname) + * + * @param $varname a string containing a variable name + * @access private + * @return string + */ + function varname($varname) { + return preg_quote("{" . $varname . "}"); + } + + + /****************************************************************************** + * If a variable's value is undefined and the variable has a filename stored in + * $this->file[$varname] then the backing file will be loaded and the file's + * contents will be assigned as the variable's value. + * + * Note that the behaviour of this function changed slightly after the 7.2d + * release. Where previously a variable was reloaded from file if the value + * was empty, now this is not done. This allows a variable to be loaded then + * set to "", and also prevents attempts to load empty variables. Files are + * now only loaded if $this->varvals[$varname] is unset. + * + * Returns: true on success, false on error. + * + * usage: loadfile(string $varname) + * + * @param $varname a string containing the name of a variable to load + * @access private + * @return boolean + * @see set_file + */ + function loadfile($varname) { + if ($this->debug & 4) { + echo "<p><b>loadfile:</b> varname = $varname</p>\n"; + } + + if (!isset($this->file[$varname])) { + // $varname does not reference a file so return + if ($this->debug & 4) { + echo "<p><b>loadfile:</b> varname $varname does not reference a file</p>\n"; + } + return true; + } + + if (isset($this->varvals[$varname])) { + // will only be unset if varname was created with set_file and has never been loaded + // $varname has already been loaded so return + if ($this->debug & 4) { + echo "<p><b>loadfile:</b> varname $varname is already loaded</p>\n"; + } + return true; + } + $filename = $this->file[$varname]; + + /* use @file here to avoid leaking filesystem information if there is an error */ + $str = implode("", @file($filename)); + if (empty($str)) { + $this->halt("loadfile: While loading $varname, $filename does not exist or is empty."); + return false; + } + + if ($this->filename_comments) { + $str = "<!-- START FILE $filename -->\n$str<!-- END FILE $filename -->\n"; + } + if ($this->debug & 4) { + printf("<b>loadfile:</b> loaded $filename into $varname<br>\n"); + } + $this->set_var($varname, $str); + + return true; + } + + + /****************************************************************************** + * This function is called whenever an error occurs and will handle the error + * according to the policy defined in $this->halt_on_error. Additionally the + * error message will be saved in $this->last_error. + * + * Returns: always returns false. + * + * usage: halt(string $msg) + * + * @param $msg a string containing an error message + * @access private + * @return void + * @see $halt_on_error + */ + function halt($msg) { + $this->last_error = $msg; + + if ($this->halt_on_error != "no") { + $this->haltmsg($msg); + } + + if ($this->halt_on_error == "yes") { + die("<b>Halted.</b>"); + } + + return false; + } + + + /****************************************************************************** + * This function prints an error message. + * It can be overridden by your subclass of Template. It will be called with an + * error message to display. + * + * usage: haltmsg(string $msg) + * + * @param $msg a string containing the error message to display + * @access public + * @return void + * @see halt + */ + function haltmsg($msg) { + printf("<b>Template Error:</b> %s<br>\n", $msg); + } + +} +?> Property changes on: trunk/1.0/required/template.inc ___________________________________________________________________ Added: svn:mergeinfo + Added: svn:eol-style + native Deleted: trunk/1.0/template.inc =================================================================== --- trunk/1.0/template.inc 2009-01-29 21:48:59 UTC (rev 337) +++ trunk/1.0/template.inc 2009-01-29 21:50:55 UTC (rev 338) @@ -1,1013 +0,0 @@ -<?php -/* - * Session Management for PHP3 - * - * (C) Copyright 1999-2000 NetUSE GmbH - * Kristian Koehntopp - * - * $Id: template.inc,v 1.15 2004/07/23 20:36:29 layne_weathers Exp $ - * - */ - -/* - * Change log since version 7.2c - * - * Bug fixes to version 7.2c compiled by Richard Archer <rh...@ju...>: - * (credits given to first person to post a diff to phplib mailing list) - * - * Normalised all comments and whitespace (rha) - * replaced "$handle" with "$varname" and "$h" with "$v" throughout (from phplib-devel) - * added braces around all one-line if statements in: get_undefined, loadfile and halt (rha) - * set_var was missing two sets of braces (rha) - * added a couple of "return true" statements (rha) - * set_unknowns had "keep" as default instead of "remove" (from phplib-devel) - * set_file failed to check for empty strings if passed an array of filenames (phplib-devel) - * remove @ from call to preg_replace in subst -- report errors if there are any (NickM) - * set_block unnecessarily required a newline in the template file (Marc Tardif) - * pparse now calls this->finish to replace undefined vars (Layne Weathers) - * get_var now checks for unset varnames (NickM & rha) - * get_var when passed an array used the array key instead of the value (rha) - * get_vars now uses a call to get_var rather than this->varvals to prevent undefined var warning (rha) - * in finish, the replacement string referenced an unset variable (rha) - * loadfile would try to load a file if the varval had been set to "" (rha) - * in get_undefined, only match non-whitespace in variable tags as in finish (Layne Weathers & rha) - * more elegant fix to the problem of subst stripping '$n', '\n' and '\\' strings (rha) - * parse uses get_var to obtain return value (Jordi via SF) - * set_block reports an error if the block could not be extracted (rha) - * filename is now windows-pathname aware (krabu @ SF) - * - * - * Changes in functionality which go beyond bug fixes: - * - * added ability for filename comments to be output (from phplib-users layne) - * changed debug handling so set, get and internals can be tracked separately (rha) - * added debug statements throughout to track most function calls (rha) - * debug output contained raw HTML -- is now escaped with htmlentities (rha) - * Alter regex in set_block to remove more whitespace around BEGIN/END tags to improve HTML layout (rha) - * Add "append" option to set_var, works just like append in parse (dale at linuxwebpro.com, rha) - * Altered parse so that append is honored if passed an array (Brian) - * Converted comments and documentation to phpdoc style (rha) - * Added clear_var to set the value of variables to "" (rha) - * Added unset_var to usset variables (rha) - * - */ - -/** - * The template class allows you to keep your HTML code in some external files - * which are completely free of PHP code, but contain replacement fields. - * The class provides you with functions which can fill in the replacement fields - * with arbitrary strings. These strings can become very large, e.g. entire tables. - * - * Note: If you think that this is like FastTemplates, read carefully. It isn't. - */ - -class Template -{ - /** - * Serialization helper, the name of this class. - * - * @var string - * @access public - */ - var $classname = "Template"; - - /** - * Determines how much debugging output Template will produce. - * This is a bitwise mask of available debug levels: - * 0 = no debugging - * 1 = debug variable assignments - * 2 = debug calls to get variable - * 4 = debug internals (outputs all function calls with parameters). - * - * Note: setting $this->debug = true will enable debugging of variable - * assignments only which is the same behaviour as versions up to release 7.2d. - * - * @var int - * @access public - */ - var $debug = false; - - /** - * Determines whether Template outputs filename comments. - * false = no filename outputs - * true = HTML comments (e.g. <!-- START FILE $filename -->) placed in output - * - * @var int - * @access public - */ - var $filename_comments = false; - - /** - * Determines the regular expression used to find unknown variable tags. - * "loose" = traditional match all curly braces with no whitespace between - * "strict" = adopts PHP's variable naming rules - * ("loose" has a nasty habit of deleting JavaScript RegEx components) - * (should future major version releases of PHPLib default this "strict"?) - * - * @var string - * @access public - */ - var $unknown_regexp = "loose"; - - /** - * The base directory from which template files are loaded. - * - * @var string - * @access private - * @see set_root - */ - var $root = "."; - - /** - * A hash of strings forming a translation table which translates variable names - * into names of files containing the variable content. - * $file[varname] = "filename"; - * - * @var array - * @access private - * @see set_file - */ - var $file = array(); - - /** - * A hash of strings forming a translation table which translates variable names - * into regular expressions for themselves. - * $varkeys[varname] = "/varname/" - * - * @var array - * @access private - * @see set_var - */ - var $varkeys = array(); - - /** - * A hash of strings forming a translation table which translates variable names - * into values for their respective varkeys. - * $varvals[varname] = "value" - * - * @var array - * @access private - * @see set_var - */ - var $varvals = array(); - - /** - * Determines how to output variable tags with no assigned value in templates. - * - * @var string - * @access private - * @see set_unknowns - */ - var $unknowns = "remove"; - - /** - * Determines how Template handles error conditions. - * "yes" = the error is reported, then execution is halted - * "report" = the error is reported, then execution continues by returning "false" - * "no" = errors are silently ignored, and execution resumes reporting "false" - * - * @var string - * @access public - * @see halt - */ - var $halt_on_error = "yes"; - - /** - * The last error message is retained in this variable. - * - * @var string - * @access public - * @see halt - */ - var $last_error = ""; - - /****************************************************************************** - * Class constructor. May be called with two optional parameters. - * The first parameter sets the template directory the second parameter - * sets the policy regarding handling of unknown variables. - * - * usage: Template([string $root = "."], [string $unknowns = "remove"]) - * - * @param $root path to template directory - * @param $string what to do with undefined variables - * @see set_root - * @see set_unknowns - * @access public - * @return void - */ - function Template($root = ".", $unknowns = "remove") { - if ($this->debug & 4) { - echo "<p><b>Template:</b> root = $root, unknowns = $unknowns</p>\n"; - } - $this->set_root($root); - $this->set_unknowns($unknowns); - } - - - /****************************************************************************** - * Checks that $root is a valid directory and if so sets this directory as the - * base directory from which templates are loaded by storing the value in - * $this->root. Relative filenames are prepended with the path in $this->root. - * - * Returns true on success, false on error. - * - * usage: set_root(string $root) - * - * @param $root string containing new template directory - * @see root - * @access public - * @return boolean - */ - function set_root($root) { - if(ereg('/$', $root)) { - $root = substr($root, 0, -1); - } - if ($this->debug & 4) { - echo "<p><b>set_root:</b> root = $root</p>\n"; - } - if (!is_dir($root)) { - $this->halt("set_root: $root is not a directory."); - return false; - } - - $this->root = $root; - return true; - } - - - /****************************************************************************** - * Sets the policy for dealing with unresolved variable names. - * - * unknowns defines what to do with undefined template variables - * "remove" = remove undefined variables - * "comment" = replace undefined variables with comments - * "keep" = keep undefined variables - * - * Note: "comment" can cause unexpected results when the variable tag is embedded - * inside an HTML tag, for example a tag which is expected to be replaced with a URL. - * - * usage: set_unknowns(string $unknowns) - * - * @param $unknowns new value for unknowns - * @see unknowns - * @access public - * @return void - */ - function set_unknowns($unknowns = "remove") { - if ($this->debug & 4) { - echo "<p><b>unknowns:</b> unknowns = $unknowns</p>\n"; - } - $this->unknowns = $unknowns; - } - - - /****************************************************************************** - * Defines a filename for the initial value of a variable. - * - * It may be passed either a varname and a file name as two strings or - * a hash of strings with the key being the varname and the value - * being the file name. - * - * The new mappings are stored in the array $this->file. - * The files are not loaded yet, but only when needed. - * - * Returns true on success, false on error. - * - * usage: set_file(array $filelist = (string $varname => string $filename)) - * or - * usage: set_file(string $varname, string $filename) - * - * @param $varname either a string containing a varname or a hash of varname/file name pairs. - * @param $filename if varname is a string this is the filename otherwise filename is not required - * @access public - * @return boolean - */ - function set_file($varname, $filename = "") { - if (!is_array($varname)) { - if ($this->debug & 4) { - echo "<p><b>set_file:</b> (with scalar) varname = $varname, filename = $filename</p>\n"; - } - if ($filename == "") { - $this->halt("set_file: For varname $varname filename is empty."); - return false; - } - $this->file[$varname] = $this->filename($filename); - } else { - reset($varname); - while (list($v, $f) = each($varname)) { - if ($this->debug & 4) { - echo "<p><b>set_file:</b> (with array) varname = $v, filename = $f</p>\n"; - } - if ($f == "") { - $this->halt("set_file: For varname $v filename is empty."); - return false; - } - $this->file[$v] = $this->filename($f); - } - } - return true; - } - - - /****************************************************************************** - * A variable $parent may contain a variable block defined by: - * <!-- BEGIN $varname --> content <!-- END $varname -->. This function removes - * that block from $parent and replaces it with a variable reference named $name. - * The block is inserted into the varkeys and varvals hashes. If $name is - * omitted, it is assumed to be the same as $varname. - * - * Blocks may be nested but care must be taken to extract the blocks in order - * from the innermost block to the outermost block. - * - * Returns true on success, false on error. - * - * usage: set_block(string $parent, string $varname, [string $name = ""]) - * - * @param $parent a string containing the name of the parent variable - * @param $varname a string containing the name of the block to be extracted - * @param $name the name of the variable in which to store the block - * @access public - * @return boolean - */ - function set_block($parent, $varname, $name = "") { - if ($this->debug & 4) { - echo "<p><b>set_block:</b> parent = $parent, varname = $varname, name = $name</p>\n"; - } - if (!$this->loadfile($parent)) { - $this->halt("set_block: unable to load $parent."); - return false; - } - if ($name == "") { - $name = $varname; - } - - $str = $this->get_var($parent); - $reg = "/[ \t]*<!--\s+BEGIN $varname\s+-->\s*?\n?(\s*.*?\n?)\s*<!--\s+END $varname\s+-->\s*?\n?/sm"; - preg_match_all($reg, $str, $m); - if (!isset($m[1][0])) { - $this->halt("set_block: unable to set block $varname."); - return false; - } - $str = preg_replace($reg, "{" . $name . "}", $str); - $this->set_var($varname, $m[1][0]); - $this->set_var($parent, $str); - return true; - } - - - /****************************************************************************** - * This functions sets the value of a variable. - * - * It may be called with either a varname and a value as two strings or an - * an associative array with the key being the varname and the value being - * the new variable value. - * - * The function inserts the new value of the variable into the $varkeys and - * $varvals hashes. It is not necessary for a variable to exist in these hashes - * before calling this function. - * - * An optional third parameter allows the value for each varname to be appended - * to the existing variable instead of replacing it. The default is to replace. - * This feature was introduced after the 7.2d release. - * - * - * usage: set_var(string $varname, [string $value = ""], [boolean $append = false]) - * or - * usage: set_var(array $varname = (string $varname => string $value), [mixed $dummy_var], [boolean $append = false]) - * - * @param $varname either a string containing a varname or a hash of varname/value pairs. - * @param $value if $varname is a string this contains the new value for the variable otherwise this parameter is ignored - * @param $append if true, the value is appended to the variable's existing value - * @access public - * @return void - */ - function set_var($varname, $value = "", $append = false) { - if (!is_array($varname)) { - if (!empty($varname)) { - if ($this->debug & 1) { - printf("<b>set_var:</b> (with scalar) <b>%s</b> = '%s'<br>\n", $varname, htmlentities($value)); - } - $this->varkeys[$varname] = "/".$this->varname($varname)."/"; - if ($append && isset($this->varvals[$varname])) { - $this->varvals[$varname] .= $value; - } else { - $this->varvals[$varname] = $value; - } - } - } else { - reset($varname); - while (list($k, $v) = each($varname)) { - if (!empty($k)) { - if ($this->debug & 1) { - printf("<b>set_var:</b> (with array) <b>%s</b> = '%s'<br>\n", $k, htmlentities($v)); - } - $this->varkeys[$k] = "/".$this->varname($k)."/"; - if ($append && isset($this->varvals[$k])) { - $this->varvals[$k] .= $v; - } else { - $this->varvals[$k] = $v; - } - } - } - } - } - - - /****************************************************************************** - * This functions clears the value of a variable. - * - * It may be called with either a varname as a string or an array with the - * values being the varnames to be cleared. - * - * The function sets the value of the variable in the $varkeys and $varvals - * hashes to "". It is not necessary for a variable to exist in these hashes - * before calling this function. - * - * - * usage: clear_var(string $varname) - * or - * usage: clear_var(array $varname = (string $varname)) - * - * @param $varname either a string containing a varname or an array of varnames. - * @access public - * @return void - */ - function clear_var($varname) { - if (!is_array($varname)) { - if (!empty($varname)) { - if ($this->debug & 1) { - printf("<b>clear_var:</b> (with scalar) <b>%s</b><br>\n", $varname); - } - $this->set_var($varname, ""); - } - } else { - reset($varname); - while (list($k, $v) = each($varname)) { - if (!empty($v)) { - if ($this->debug & 1) { - printf("<b>clear_var:</b> (with array) <b>%s</b><br>\n", $v); - } - $this->set_var($v, ""); - } - } - } - } - - - /****************************************************************************** - * This functions unsets a variable completely. - * - * It may be called with either a varname as a string or an array with the - * values being the varnames to be cleared. - * - * The function removes the variable from the $varkeys and $varvals hashes. - * It is not necessary for a variable to exist in these hashes before calling - * this function. - * - * - * usage: unset_var(string $varname) - * or - * usage: unset_var(array $varname = (string $varname)) - * - * @param $varname either a string containing a varname or an array of varnames. - * @access public - * @return void - */ - function unset_var($varname) { - if (!is_array($varname)) { - if (!empty($varname)) { - if ($this->debug & 1) { - printf("<b>unset_var:</b> (with scalar) <b>%s</b><br>\n", $varname); - } - unset($this->varkeys[$varname]); - unset($this->varvals[$varname]); - } - } else { - reset($varname); - while (list($k, $v) = each($varname)) { - if (!empty($v)) { - if ($this->debug & 1) { - printf("<b>unset_var:</b> (with array) <b>%s</b><br>\n", $v); - } - unset($this->varkeys[$v]); - unset($this->varvals[$v]); - } - } - } - } - - - /****************************************************************************** - * This function fills in all the variables contained within the variable named - * $varname. The resulting value is returned as the function result and the - * original value of the variable varname is not changed. The resulting string - * is not "finished", that is, the unresolved variable name policy has not been - * applied yet. - * - * Returns: the value of the variable $varname with all variables substituted. - * - * usage: subst(string $varname) - * - * @param $varname the name of the variable within which variables are to be substituted - * @access public - * @return string - */ - function subst($varname) { - $varvals_quoted = array(); - if ($this->debug & 4) { - echo "<p><b>subst:</b> varname = $varname</p>\n"; - } - if (!$this->loadfile($varname)) { - $this->halt("subst: unable to load $varname."); - return false; - } - - // quote the replacement strings to prevent bogus stripping of special chars - reset($this->varvals); - while (list($k, $v) = each($this->varvals)) { - $varvals_quoted[$k] = preg_replace(array('/\\\\/', '/\$/'), array('\\\\\\\\', '\\\\$'), $v); - } - - $str = $this->get_var($varname); - $str = preg_replace($this->varkeys, $varvals_quoted, $str); - return $str; - } - - - /****************************************************************************** - * This is shorthand for print $this->subst($varname). See subst for further - * details. - * - * Returns: always returns false. - * - * usage: psubst(string $varname) - * - * @param $varname the name of the variable within which variables are to be substituted - * @access public - * @return false - * @see subst - */ - function psubst($varname) { - if ($this->debug & 4) { - echo "<p><b>psubst:</b> varname = $varname</p>\n"; - } - print $this->subst($varname); - - return false; - } - - - /****************************************************************************** - * The function substitutes the values of all defined variables in the variable - * named $varname and stores or appends the result in the variable named $target. - * - * It may be called with either a target and a varname as two strings or a - * target as a string and an array of variable names in varname. - * - * The function inserts the new value of the variable into the $varkeys and - * $varvals hashes. It is not necessary for a variable to exist in these hashes - * before calling this function. - * - * An optional third parameter allows the value for each varname to be appended - * to the existing target variable instead of replacing it. The default is to - * replace. - * - * If $target and $varname are both strings, the substituted value of the - * variable $varname is inserted into or appended to $target. - * - * If $handle is an array of variable names the variables named by $handle are - * sequentially substituted and the result of each substitution step is - * inserted into or appended to in $target. The resulting substitution is - * available in the variable named by $target, as is each intermediate step - * for the next $varname in sequence. Note that while it is possible, it - * is only rarely desirable to call this function with an array of varnames - * and with $append = true. This append feature was introduced after the 7.2d - * release. - * - * Returns: the last value assigned to $target. - * - * usage: parse(string $target, string $varname, [boolean $append]) - * or - * usage: parse(string $target, array $varname = (string $varname), [boolean $append]) - * - * @param $target a string containing the name of the variable into which substituted $varnames are to be stored - * @param $varname if a string, the name the name of the variable to substitute or if an array a list of variables to be substituted - * @param $append if true, the substituted variables are appended to $target otherwise the existing value of $target is replaced - * @access public - * @return string - * @see subst - */ - function parse($target, $varname, $append = false) { - if (!is_array($varname)) { - if ($this->debug & 4) { - echo "<p><b>parse:</b> (with scalar) target = $target, varname = $varname, append = $append</p>\n"; - } - $str = $this->subst($varname); - if ($append) { - $this->set_var($target, $this->get_var($target) . $str); - } else { - $this->set_var($target, $str); - } - } else { - reset($varname); - while (list($i, $v) = each($varname)) { - if ($this->debug & 4) { - echo "<p><b>parse:</b> (with array) target = $target, i = $i, varname = $v, append = $append</p>\n"; - } - $str = $this->subst($v); - if ($append) { - $this->set_var($target, $this->get_var($target) . $str); - } else { - $this->set_var($target, $str); - } - } - } - - if ($this->debug & 4) { - echo "<p><b>parse:</b> completed</p>\n"; - } - return $this->get_var($target); - } - - - /****************************************************************************** - * This is shorthand for print $this->parse(...) and is functionally identical. - * See parse for further details. - * - * Returns: always returns false. - * - * usage: pparse(string... [truncated message content] |
From: <cra...@us...> - 2009-02-03 19:06:54
|
Revision: 344 http://cs-content.svn.sourceforge.net/cs-content/?rev=344&view=rev Author: crazedsanity Date: 2009-02-03 19:06:49 +0000 (Tue, 03 Feb 2009) Log Message: ----------- Minor problems with version parser & including proper files. /contentSystem.class.php: * __construct(): -- call parent::__construct() to ensure version parse stuff is always available... /cs_sessionClass.php: * MAIN::: -- require cs_content.abstract.class.php Modified Paths: -------------- trunk/1.0/contentSystem.class.php trunk/1.0/cs_sessionClass.php Modified: trunk/1.0/contentSystem.class.php =================================================================== --- trunk/1.0/contentSystem.class.php 2009-02-01 18:12:51 UTC (rev 343) +++ trunk/1.0/contentSystem.class.php 2009-02-03 19:06:49 UTC (rev 344) @@ -103,12 +103,12 @@ * The CONSTRUCTOR. Duh. */ public function __construct($testOnly=FALSE) { + parent::__construct(); if($testOnly === 'unit_test') { //It's just a test, don't do anything we might regret later. $this->isTest = TRUE; } else { - parent::__construct(); //setup the section stuff... $repArr = array($_SERVER['SCRIPT_NAME'], "/"); Modified: trunk/1.0/cs_sessionClass.php =================================================================== --- trunk/1.0/cs_sessionClass.php 2009-02-01 18:12:51 UTC (rev 343) +++ trunk/1.0/cs_sessionClass.php 2009-02-03 19:06:49 UTC (rev 344) @@ -8,6 +8,7 @@ * $LastChangedRevision$ */ +require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); require_once(dirname(__FILE__) ."/../cs-versionparse/cs_version.abstract.class.php"); class cs_session extends cs_contentAbstract { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-02-03 19:08:54
|
Revision: 345 http://cs-content.svn.sourceforge.net/cs-content/?rev=345&view=rev Author: crazedsanity Date: 2009-02-03 19:08:45 +0000 (Tue, 03 Feb 2009) Log Message: ----------- Rename cs_sessionClass.php to cs_session.class.php to match naming conventions. Added Paths: ----------- trunk/1.0/cs_session.class.php Removed Paths: ------------- trunk/1.0/cs_sessionClass.php Copied: trunk/1.0/cs_session.class.php (from rev 344, trunk/1.0/cs_sessionClass.php) =================================================================== --- trunk/1.0/cs_session.class.php (rev 0) +++ trunk/1.0/cs_session.class.php 2009-02-03 19:08:45 UTC (rev 345) @@ -0,0 +1,145 @@ +<?php +/* + * FILE INFORMATION: + * $HeadURL$ + * $Id$ + * $LastChangedDate$ + * $LastChangedBy$ + * $LastChangedRevision$ + */ + +require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); +require_once(dirname(__FILE__) ."/../cs-versionparse/cs_version.abstract.class.php"); + +class cs_session extends cs_contentAbstract { + + protected $db; + public $uid; + public $sid; + public $sid_check = 1; + + //--------------------------------------------------------------------------------------------- + /** + * The constructor. + * + * @param $createSession (mixed,optional) determines if a session will be started or not; if + * this parameter is non-null and non-numeric, the value will be + * used as the session name. + */ + function __construct($createSession=1) { + parent::__construct(false); + if($createSession) { + if(!is_null($createSession) && strlen($createSession) && !is_numeric($createSession)) { + session_name($createSession); + } + + //now actually create the session. + session_start(); + } + + //check if there's a uid in the session already. + //TODO: need a setting somewhere that says what the name of this var should be, + // instead of always forcing "uid". + $this->uid = 0; + if($_SESSION['uid']) { + $this->uid = $_SESSION['uid']; + } + + //grab what our session_id is... + $this->sid = session_id(); + + }//end __construct() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Required method, so passing the object to contentSystem::handle_session() + * will work properly. + * + * @param (none) + * + * @return FALSE FAIL: user is not authenticated (hard-coded this way). + */ + public function is_authenticated() { + return(FALSE); + }//end is_authenticated() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Retrieve data for an existing cookie. + * + * @param $name (string) Name of cookie to retrieve value for. + * + * @return NULL FAIL (?): cookie doesn't exist or has NULL value. + * @return (string) PASS: value of cookie. + */ + public function get_cookie($name) { + $retval = NULL; + if(isset($_COOKIE) && $_COOKIE[$name]) { + $retval = $_COOKIE[$name]; + } + return($retval); + }//end get_cookie() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Create a new cookie. + * + * @param $name (string) Name of cookie + * @param $value (string) value of cookie + * @param $expiration (string/number) unix timestamp or value for strtotime(). + */ + public function create_cookie($name, $value, $expiration=NULL) { + + $expTime = NULL; + if(!is_null($expiration)) { + if(is_numeric($expiration)) { + $expTime = $expiration; + } + elseif(preg_match('/ /', $expiration)) { + $expTime = strtotime($expiration); + } + else { + throw new exception(__METHOD__ .": invalid timestamp given (". $expiration .")"); + } + } + + $retval = setcookie($name, $value, $expTime, '/'); + return($retval); + + }//end create_cookie() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + /** + * Destroy (expire) an existing cookie. + * + * @param $name (string) Name of cookie to destroy + * + * @return FALSE FAIL: no cookie by that name. + * @return TRUE PASS: cookie destroyed. + */ + public function drop_cookie($name) { + $retval = FALSE; + if(isset($_COOKIE[$name])) { + setcookie($name, $_COOKIE[$name], time() -10000, '/'); + unset($_COOKIE[$name]); + $retval = TRUE; + } + return($retval); + }//end drop_cookie() + //--------------------------------------------------------------------------------------------- + + +}//end cs_session{} +?> \ No newline at end of file Property changes on: trunk/1.0/cs_session.class.php ___________________________________________________________________ Added: svn:executable + * Added: svn:keywords + HeadURL Id LastChangedBy LastChangedDate LastChangedRevision Added: svn:mergeinfo + Added: svn:eol-style + native Deleted: trunk/1.0/cs_sessionClass.php =================================================================== --- trunk/1.0/cs_sessionClass.php 2009-02-03 19:06:49 UTC (rev 344) +++ trunk/1.0/cs_sessionClass.php 2009-02-03 19:08:45 UTC (rev 345) @@ -1,145 +0,0 @@ -<?php -/* - * FILE INFORMATION: - * $HeadURL$ - * $Id$ - * $LastChangedDate$ - * $LastChangedBy$ - * $LastChangedRevision$ - */ - -require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); -require_once(dirname(__FILE__) ."/../cs-versionparse/cs_version.abstract.class.php"); - -class cs_session extends cs_contentAbstract { - - protected $db; - public $uid; - public $sid; - public $sid_check = 1; - - //--------------------------------------------------------------------------------------------- - /** - * The constructor. - * - * @param $createSession (mixed,optional) determines if a session will be started or not; if - * this parameter is non-null and non-numeric, the value will be - * used as the session name. - */ - function __construct($createSession=1) { - parent::__construct(false); - if($createSession) { - if(!is_null($createSession) && strlen($createSession) && !is_numeric($createSession)) { - session_name($createSession); - } - - //now actually create the session. - session_start(); - } - - //check if there's a uid in the session already. - //TODO: need a setting somewhere that says what the name of this var should be, - // instead of always forcing "uid". - $this->uid = 0; - if($_SESSION['uid']) { - $this->uid = $_SESSION['uid']; - } - - //grab what our session_id is... - $this->sid = session_id(); - - }//end __construct() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Required method, so passing the object to contentSystem::handle_session() - * will work properly. - * - * @param (none) - * - * @return FALSE FAIL: user is not authenticated (hard-coded this way). - */ - public function is_authenticated() { - return(FALSE); - }//end is_authenticated() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Retrieve data for an existing cookie. - * - * @param $name (string) Name of cookie to retrieve value for. - * - * @return NULL FAIL (?): cookie doesn't exist or has NULL value. - * @return (string) PASS: value of cookie. - */ - public function get_cookie($name) { - $retval = NULL; - if(isset($_COOKIE) && $_COOKIE[$name]) { - $retval = $_COOKIE[$name]; - } - return($retval); - }//end get_cookie() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Create a new cookie. - * - * @param $name (string) Name of cookie - * @param $value (string) value of cookie - * @param $expiration (string/number) unix timestamp or value for strtotime(). - */ - public function create_cookie($name, $value, $expiration=NULL) { - - $expTime = NULL; - if(!is_null($expiration)) { - if(is_numeric($expiration)) { - $expTime = $expiration; - } - elseif(preg_match('/ /', $expiration)) { - $expTime = strtotime($expiration); - } - else { - throw new exception(__METHOD__ .": invalid timestamp given (". $expiration .")"); - } - } - - $retval = setcookie($name, $value, $expTime, '/'); - return($retval); - - }//end create_cookie() - //--------------------------------------------------------------------------------------------- - - - - //--------------------------------------------------------------------------------------------- - /** - * Destroy (expire) an existing cookie. - * - * @param $name (string) Name of cookie to destroy - * - * @return FALSE FAIL: no cookie by that name. - * @return TRUE PASS: cookie destroyed. - */ - public function drop_cookie($name) { - $retval = FALSE; - if(isset($_COOKIE[$name])) { - setcookie($name, $_COOKIE[$name], time() -10000, '/'); - unset($_COOKIE[$name]); - $retval = TRUE; - } - return($retval); - }//end drop_cookie() - //--------------------------------------------------------------------------------------------- - - -}//end cs_session{} -?> \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-02-03 19:22:59
|
Revision: 346 http://cs-content.svn.sourceforge.net/cs-content/?rev=346&view=rev Author: crazedsanity Date: 2009-02-03 19:22:53 +0000 (Tue, 03 Feb 2009) Log Message: ----------- Fix location of cs_session from last commit. Modified Paths: -------------- trunk/1.0/VERSION trunk/1.0/contentSystem.class.php Modified: trunk/1.0/VERSION =================================================================== --- trunk/1.0/VERSION 2009-02-03 19:08:45 UTC (rev 345) +++ trunk/1.0/VERSION 2009-02-03 19:22:53 UTC (rev 346) @@ -1,5 +1,5 @@ ## Stores the current version of the cs-content system, and it's source. Please do NOT modify this file. -VERSION: 1.0-ALPHA3 +VERSION: 1.0-ALPHA4 PROJECT: cs-content $HeadURL$ \ No newline at end of file Modified: trunk/1.0/contentSystem.class.php =================================================================== --- trunk/1.0/contentSystem.class.php 2009-02-03 19:08:45 UTC (rev 345) +++ trunk/1.0/contentSystem.class.php 2009-02-03 19:22:53 UTC (rev 346) @@ -72,7 +72,7 @@ require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); require_once(dirname(__FILE__) ."/cs_fileSystem.class.php"); -require_once(dirname(__FILE__) ."/cs_sessionClass.php"); +require_once(dirname(__FILE__) ."/cs_session.class.php"); require_once(dirname(__FILE__) ."/cs_genericPage.class.php"); require_once(dirname(__FILE__) ."/cs_tabs.class.php"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-02-04 16:34:34
|
Revision: 350 http://cs-content.svn.sourceforge.net/cs-content/?rev=350&view=rev Author: crazedsanity Date: 2009-02-04 16:34:29 +0000 (Wed, 04 Feb 2009) Log Message: ----------- Unit testing for template/block row handling & minor feature additions. /cs_genericPage.class.php: * MAIN::: -- changed "var" definitions into public definitions -- added "unhandledVars=array()" definition * initialize_locals(): -- creates the tmplDir from the dirname of the "mainTemplateFile", if it is determinable (i.e. if a path to the template was given). -- siteRoot is taken from tmplDir after dropping the ending "/templates" part off the path. NOTE::: siteRoot should eventually be able to be set independently of the tmplDir... this should eventually use constants as default values. -- tmplDir is only appended to the mainTemplateFile var when it does NOT begin with a slash (old logic was inverted). * set_block_row(): -- allows blank block rows to be removed instead of requiring at least one character between definitions (get_block_row_defs() would see the row, but it would not get removed). * print_page(): -- updates internal "unhandledVars" array when stripping undefined vars (mostly for debugging purposes) * get_block_row_defs(): -- changed minimum length of block row from 31 to 30 to match changes in set_block_row() method. * return_printed_page() [NEW]: -- uses output buffering when calling print_page() to return the page without printing it. * strip_undef_template_vars() [NEW]: -- strip undefined vars from a part of the page & return that parsed section (does not affect given section directly). /tests/testOfCSContent.php: * __construct(): -- create internal cs_globalFunctions{} object, turn debug printing on. * test_genericPage() [NEW]: -- runs some basic tests to make sure that template parsing & block row handling all work exactly as they are expected to. /tests/files/: * added some files to support unit tests for cs_genericPage{}. Modified Paths: -------------- trunk/1.0/cs_genericPage.class.php trunk/1.0/tests/testOfCSContent.php Added Paths: ----------- trunk/1.0/tests/files/gptest_all-together.txt trunk/1.0/tests/files/templates/ trunk/1.0/tests/files/templates/content.shared.tmpl trunk/1.0/tests/files/templates/footer.shared.tmpl trunk/1.0/tests/files/templates/infobar.shared.tmpl trunk/1.0/tests/files/templates/main.shared.tmpl trunk/1.0/tests/files/templates/menubar.shared.tmpl trunk/1.0/tests/files/templates/title.shared.tmpl Property Changed: ---------------- trunk/1.0/tests/files/sampleConfig.xml Modified: trunk/1.0/cs_genericPage.class.php =================================================================== --- trunk/1.0/cs_genericPage.class.php 2009-02-03 19:28:36 UTC (rev 349) +++ trunk/1.0/cs_genericPage.class.php 2009-02-04 16:34:29 UTC (rev 350) @@ -11,9 +11,10 @@ require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); class cs_genericPage extends cs_contentAbstract { - var $templateObj; //template object to parse the pages - var $templateVars = array(); //our copy of the global templateVars - var $mainTemplate; //the default layout of the site + public $templateObj; //template object to parse the pages + public $templateVars = array(); //our copy of the global templateVars + public $mainTemplate; //the default layout of the site + public $unhandledVars=array(); private $tmplDir; private $libDir; @@ -60,9 +61,15 @@ */ protected function initialize_locals($mainTemplateFile) { - //NOTE: this **requires** that the global variable "SITE_ROOT" is already set. - $this->siteRoot = preg_replace('/\/public_html/', '', $_SERVER['DOCUMENT_ROOT']); - $this->tmplDir = $this->siteRoot .'/templates'; + + if(strlen(dirname($mainTemplateFile)) && dirname($mainTemplateFile) !== '/') { + $this->tmplDir = dirname($mainTemplateFile); + } + else { + //NOTE: this **requires** that the global variable "SITE_ROOT" is already set. + $this->siteRoot = preg_replace('/\/public_html/', '', $_SERVER['DOCUMENT_ROOT']); + } + $this->siteRoot = preg_replace('/\/templates$/', '', $this->tmplDir); $this->libDir = $this->siteRoot .'/lib'; //if there have been some global template vars (or files) set, read 'em in here. @@ -81,7 +88,7 @@ //build a new instance of the template library (from PHPLib) $this->templateObj=new Template($this->tmplDir,"keep"); //initialize a new template parser - if(preg_match('/^\//', $mainTemplateFile)) { + if(!preg_match('/^\//', $mainTemplateFile)) { $mainTemplateFile = $this->tmplDir ."/". $mainTemplateFile; } $this->mainTemplate=$mainTemplateFile; //load the default layout @@ -199,7 +206,7 @@ $name = $handle; $str = $this->templateVars[$parent]; - $reg = "/<!-- BEGIN $handle -->.+<!-- END $handle -->/sU"; + $reg = "/<!-- BEGIN $handle -->(.+){0,}<!-- END $handle -->/sU"; preg_match_all($reg, $str, $m); if(!is_string($m[0][0])) { #exit("set_block_row(): couldn't find '$handle' in var '$parent'"); @@ -273,6 +280,7 @@ if(!$this->templateVars[$str2] && $stripUndefVars) { //TODO: set an internal pointer or something to use here, so they can see what was missed. $this->templateObj->varvals[out] = str_replace($str, '', $this->templateObj->varvals[out]); + $this->unhandledVars[$str2]++; } } $this->templateObj->parse("out", "out"); @@ -524,10 +532,10 @@ //cast $retArr as an array, so it's clean. $retArr = array(); - //NOTE: the value 31 isn't just a randomly chosen length; it's the minimum - // number of characters to have a block row. EG: "<!-- BEGIN x -->o<!-- END x -->" + //NOTE: the value 30 isn't just a randomly chosen length; it's the minimum + // number of characters to have a block row. EG: "<!-- BEGIN x --><!-- END x -->" $templateContents = $this->templateVars[$templateVar]; - if(strlen($templateContents) >= 31) { + if(strlen($templateContents) >= 30) { //looks good to me. Run the regex... $flags = PREG_PATTERN_ORDER; $reg = "/<!-- BEGIN (\S{1,}) -->/"; @@ -615,6 +623,48 @@ return($this->allowInvalidUrls); }//end allow_invalid_urls() //--------------------------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + public function return_printed_page($stripUndefVars=1) { + ob_start(); + $this->print_page($stripUndefVars); + $retval = ob_get_contents(); + ob_end_clean(); + return($retval); + }//end return_printed_page() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + public function strip_undef_template_vars($section='content') { + $numLoops = 0; + if(isset($this->templateVars[$section])) { + $templateContents = $this->templateVars[$section]; + while(preg_match_all('/\{.\S+?\}/', $templateContents, $tags) && $numLoops < 50) { + $tags = $tags[0]; + + //TODO: figure out why this works when running it twice. + foreach($tags as $key=>$str) { + $str2 = str_replace("{", "", $str); + $str2 = str_replace("}", "", $str2); + if(!$this->templateVars[$str2]) { + //TODO: set an internal pointer or something to use here, so they can see what was missed. + $templateContents = str_replace($str, '', $templateContents); + } + } + $this->templateObj->parse("out", "out"); + $numLoops++; + } + } + else { + throw new exception(__METHOD__ .": section (". $section .") does not exist"); + } + return($templateContents); + //------------------------------------------------------------------------- + } }//end cs_genericPage{} ?> Added: trunk/1.0/tests/files/gptest_all-together.txt =================================================================== --- trunk/1.0/tests/files/gptest_all-together.txt (rev 0) +++ trunk/1.0/tests/files/gptest_all-together.txt 2009-02-04 16:34:29 UTC (rev 350) @@ -0,0 +1,19 @@ +<html> + <head> + <title>This is the title.</title> + </head> +<body> + <table>This is the infobar.</table> + --- the menubar (DATE: 2009-01-01) +---+++ CONTENT STARTS HERE +++--- + <!-- BEGIN blockRow1 -->This is a TEST<!-- comment --><!-- END blockRow1 --> + <!-- BEGIN blockRow2 -->This + is second blockRow<!-- END blockRow2 --> +<!-- BEGIN blockRow3 --> Some data In here... + <!-- END blockRow3 --> + + <!-- BEGIN blockRow4 --><!-- END blockRow4 --> +---+++ CONTENT ENDS HERE +++--- + --- the footer. +</body> +</html> \ No newline at end of file Property changes on: trunk/1.0/tests/files/gptest_all-together.txt ___________________________________________________________________ Added: svn:eol-style + native Property changes on: trunk/1.0/tests/files/sampleConfig.xml ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/1.0/tests/files/templates/content.shared.tmpl =================================================================== --- trunk/1.0/tests/files/templates/content.shared.tmpl (rev 0) +++ trunk/1.0/tests/files/templates/content.shared.tmpl 2009-02-04 16:34:29 UTC (rev 350) @@ -0,0 +1,8 @@ + + <!-- BEGIN blockRow1 -->This is a TEST<!-- comment --><!-- END blockRow1 --> + <!-- BEGIN blockRow2 -->This + is second blockRow<!-- END blockRow2 --> +<!-- BEGIN blockRow{blockRowTestVal} --> Some data In here... + <!-- END blockRow3 --> + + <!-- BEGIN blockRow4 --><!-- END blockRow4 -->{invisibleTemplateVar} Property changes on: trunk/1.0/tests/files/templates/content.shared.tmpl ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/1.0/tests/files/templates/footer.shared.tmpl =================================================================== --- trunk/1.0/tests/files/templates/footer.shared.tmpl (rev 0) +++ trunk/1.0/tests/files/templates/footer.shared.tmpl 2009-02-04 16:34:29 UTC (rev 350) @@ -0,0 +1 @@ +--- the footer. \ No newline at end of file Property changes on: trunk/1.0/tests/files/templates/footer.shared.tmpl ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/1.0/tests/files/templates/infobar.shared.tmpl =================================================================== --- trunk/1.0/tests/files/templates/infobar.shared.tmpl (rev 0) +++ trunk/1.0/tests/files/templates/infobar.shared.tmpl 2009-02-04 16:34:29 UTC (rev 350) @@ -0,0 +1 @@ +<table>This is the infobar.</table> \ No newline at end of file Property changes on: trunk/1.0/tests/files/templates/infobar.shared.tmpl ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/1.0/tests/files/templates/main.shared.tmpl =================================================================== --- trunk/1.0/tests/files/templates/main.shared.tmpl (rev 0) +++ trunk/1.0/tests/files/templates/main.shared.tmpl 2009-02-04 16:34:29 UTC (rev 350) @@ -0,0 +1,11 @@ +<html> + <head> + <title>{title}</title> + </head> +<body> + {infobar} + {menubar} +---+++ CONTENT STARTS HERE +++---{content}---+++ CONTENT ENDS HERE +++--- + {footer} +</body> +</html> \ No newline at end of file Property changes on: trunk/1.0/tests/files/templates/main.shared.tmpl ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/1.0/tests/files/templates/menubar.shared.tmpl =================================================================== --- trunk/1.0/tests/files/templates/menubar.shared.tmpl (rev 0) +++ trunk/1.0/tests/files/templates/menubar.shared.tmpl 2009-02-04 16:34:29 UTC (rev 350) @@ -0,0 +1 @@ +--- the menubar (DATE: {date}) \ No newline at end of file Property changes on: trunk/1.0/tests/files/templates/menubar.shared.tmpl ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/1.0/tests/files/templates/title.shared.tmpl =================================================================== --- trunk/1.0/tests/files/templates/title.shared.tmpl (rev 0) +++ trunk/1.0/tests/files/templates/title.shared.tmpl 2009-02-04 16:34:29 UTC (rev 350) @@ -0,0 +1 @@ +This is the title. \ No newline at end of file Property changes on: trunk/1.0/tests/files/templates/title.shared.tmpl ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/1.0/tests/testOfCSContent.php =================================================================== --- trunk/1.0/tests/testOfCSContent.php 2009-02-03 19:28:36 UTC (rev 349) +++ trunk/1.0/tests/testOfCSContent.php 2009-02-04 16:34:29 UTC (rev 350) @@ -22,8 +22,8 @@ require_once(dirname(__FILE__) .'/../cs_globalFunctions.class.php'); require_once(dirname(__FILE__) .'/../cs_siteConfig.class.php'); - $this->gf = new cs_globalFunctions; - $this->gf->debugPrintOpt=1; + $this->gfObj = new cs_globalFunctions; + $this->gfObj->debugPrintOpt=1; }//end __construct() //------------------------------------------------------------------------- @@ -245,6 +245,63 @@ + //------------------------------------------------------------------------- + function test_genericPage() { + $filesDir = dirname(__FILE__) .'/files'; + + $page = new cs_genericPage(false, $filesDir .'/templates/main.shared.tmpl', false); + $fs = new cs_fileSystem($filesDir .'/templates'); + + $lsData = $fs->ls(); + + foreach($lsData as $index=>$value) { + $filenameBits = explode('.', $index); + $page->add_template_var($filenameBits[0], $page->file_to_string($index)); + } + + $page->add_template_var('blockRowTestVal', 3); + $page->add_template_var('date', '2009-01-01'); + + $checkThis = $page->return_printed_page(); + + + $this->assertEqual($checkThis, file_get_contents($filesDir .'/gptest_all-together.txt')); + + //now let's rip all the template rows out & add them back in. + $rowDefs = $page->get_block_row_defs('content'); + $rippedRows = $page->rip_all_block_rows('content'); + + $this->assertEqual($rowDefs['ordered'], array_keys($rippedRows)); + $remainingRows = $page->rip_all_block_rows('content'); + $this->assertEqual(array(), $remainingRows, "ERROR: some block rows exist after ripping: ". + $this->gfObj->string_from_array(array_keys($remainingRows), 'null', ',')); + + + foreach($rippedRows as $name=>$data) { + $page->add_template_var($name, $data); + } + $checkThis2 = $page->return_printed_page(); + + $this->assertEqual($checkThis, $checkThis2); + + $checkThis = $page->return_printed_page(0); + $this->assertTrue(preg_match('/\{.\S+?\}/', $checkThis)); + + //clone the page object so we can change stuff & not affect the original. + $page2 = clone $page; + unset($page2->templateVars); + $this->assertNotEqual($page->templateVars, $page2->templateVars); + $page2 = clone $page; + + $this->assertNotEqual($page2->templateVars['content'], $page2->strip_undef_template_vars('content')); + $this->assertNotEqual($page2->templateVars['content'], $page2->strip_undef_template_vars('content')); + $page2->templateVars['content'] = $page2->strip_undef_template_vars('content'); + $this->assertEqual($page->return_printed_page(1), $page2->return_printed_page(1)); + }//end test_genericPage + //------------------------------------------------------------------------- + + + }//end TestOfCSContent //============================================================================= ?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-02-04 17:31:25
|
Revision: 356 http://cs-content.svn.sourceforge.net/cs-content/?rev=356&view=rev Author: crazedsanity Date: 2009-02-04 17:31:19 +0000 (Wed, 04 Feb 2009) Log Message: ----------- Changes for stripping undefined template vars. NOTE::: these changes are mostly for ease in processing when returning parsed templates via ajax (CS-Project does this). /cs_genericPage.class.php: * strip_undef_template_vars(): -- ARG CHANGE: RENAMED ARG: #1 (now is "$templateContents") -- template contents must be fed to it instead of specifying a section to retrieve template contents. * strip_undef_template_vars_from_section() [NEW]: -- similar to what strip_undef_template_vars() was previously, except it this one applies changes to that section before returning the new value. /tests/testOfCSContent.php: * test_genericPage(): -- updated tests to handle change in the two methods listed above. Modified Paths: -------------- trunk/1.0/cs_genericPage.class.php trunk/1.0/tests/testOfCSContent.php Modified: trunk/1.0/cs_genericPage.class.php =================================================================== --- trunk/1.0/cs_genericPage.class.php 2009-02-04 17:04:36 UTC (rev 355) +++ trunk/1.0/cs_genericPage.class.php 2009-02-04 17:31:19 UTC (rev 356) @@ -640,32 +640,40 @@ //------------------------------------------------------------------------- - public function strip_undef_template_vars($section='content') { + public function strip_undef_template_vars($templateContents) { $numLoops = 0; - if(isset($this->templateVars[$section])) { - $templateContents = $this->templateVars[$section]; - while(preg_match_all('/\{.\S+?\}/', $templateContents, $tags) && $numLoops < 50) { - $tags = $tags[0]; - - //TODO: figure out why this works when running it twice. - foreach($tags as $key=>$str) { - $str2 = str_replace("{", "", $str); - $str2 = str_replace("}", "", $str2); - if(!$this->templateVars[$str2]) { - //TODO: set an internal pointer or something to use here, so they can see what was missed. - $templateContents = str_replace($str, '', $templateContents); - } + while(preg_match_all('/\{.\S+?\}/', $templateContents, $tags) && $numLoops < 50) { + $tags = $tags[0]; + + //TODO: figure out why this works when running it twice. + foreach($tags as $key=>$str) { + $str2 = str_replace("{", "", $str); + $str2 = str_replace("}", "", $str2); + if(!$this->templateVars[$str2]) { + //TODO: set an internal pointer or something to use here, so they can see what was missed. + $templateContents = str_replace($str, '', $templateContents); } - $this->templateObj->parse("out", "out"); - $numLoops++; } + $numLoops++; } + return($templateContents); + }//end strip_undef_template_vars() + //------------------------------------------------------------------------- + + + + //------------------------------------------------------------------------- + public function strip_undef_template_vars_from_section($section='content') { + if(isset($this->templateVars[$section])) { + $this->templateVars[$section] = $this->strip_undef_template_vars($this->templateVars[$section]); + } else { throw new exception(__METHOD__ .": section (". $section .") does not exist"); } - return($templateContents); + + return($this->templateVars[$section]); + }//strip_undef_template_vars_from_section() //------------------------------------------------------------------------- - } }//end cs_genericPage{} ?> Modified: trunk/1.0/tests/testOfCSContent.php =================================================================== --- trunk/1.0/tests/testOfCSContent.php 2009-02-04 17:04:36 UTC (rev 355) +++ trunk/1.0/tests/testOfCSContent.php 2009-02-04 17:31:19 UTC (rev 356) @@ -293,9 +293,9 @@ $this->assertNotEqual($page->templateVars, $page2->templateVars); $page2 = clone $page; - $this->assertNotEqual($page2->templateVars['content'], $page2->strip_undef_template_vars('content')); - $this->assertNotEqual($page2->templateVars['content'], $page2->strip_undef_template_vars('content')); - $page2->templateVars['content'] = $page2->strip_undef_template_vars('content'); + $this->assertNotEqual($page2->templateVars['content'], $page2->strip_undef_template_vars($page2->templateVars['content'])); + $this->assertNotEqual($page2->templateVars['content'], $page2->strip_undef_template_vars($page2->templateVars['content'])); + $page2->templateVars['content'] = $page2->strip_undef_template_vars($page2->templateVars['content']); $this->assertEqual($page->return_printed_page(1), $page2->return_printed_page(1)); }//end test_genericPage //------------------------------------------------------------------------- This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-02-05 23:22:12
|
Revision: 357 http://cs-content.svn.sourceforge.net/cs-content/?rev=357&view=rev Author: crazedsanity Date: 2009-02-05 22:04:03 +0000 (Thu, 05 Feb 2009) Log Message: ----------- Fix issue about not including all include files (#246). NOTE::: a good way to test it is to create a script that will run the content system (see example file included with project), then build levels of includes in the appropriate includes directory; each level should have index.inc, shared.inc, and {pwd}.inc. That helps find problems with handling includes at the root level and at much deeper levels. Testing URLs that go beyond the level of includes is also a great idea. /contentSystem.class.php: * __parse_section(): -- added a TODO about arbitrarily handling "/" as "content/index" * load_includes(): -- special handling in the case that the last section is called "index" -- after it's done, attempt to load a final index.inc and shared.inc using new method "add_include()" * load_dir_includes(): -- use new method "add_include()" instead of manually doing so. * add_include() [NEW]: -- adds items to the internal includesList array. /cs_genericPage.class.php: * initialize_locals(): -- strip multiple "/" characters to avoid logic problems. -- strip leading "/" if there is only one "/" in the string (#247) Modified Paths: -------------- trunk/1.0/contentSystem.class.php trunk/1.0/cs_genericPage.class.php Modified: trunk/1.0/contentSystem.class.php =================================================================== --- trunk/1.0/contentSystem.class.php 2009-02-04 17:31:19 UTC (rev 356) +++ trunk/1.0/contentSystem.class.php 2009-02-05 22:04:03 UTC (rev 357) @@ -262,6 +262,8 @@ * Rips apart the "section" string, setting $this->section and $this->sectionArr. */ private function parse_section() { + + //TODO::: this should be an OPTIONAL THING as to how to handle "/" (i.e. CSCONTENT_HANDLE_ROOTURL='content/index') if($this->section === 0 || is_null($this->section) || !strlen($this->section)) { $this->section = "content/index"; } @@ -652,7 +654,16 @@ //okay, now loop through $this->sectionArr & see if we can include anything else. if(($this->fileSystemObj->cd($this->baseDir)) && is_array($this->sectionArr) && count($this->sectionArr) > 0) { - foreach($this->sectionArr as $mySection) { + + //if the last item in the array is "index", disregard it... + $loopThis = $this->sectionArr; + $lastSection = $this->sectionArr[(count($this->sectionArr) -1)]; + if($lastSection == 'index') { + array_pop($loopThis); + } + + + foreach($loopThis as $mySection) { //Run includes. $this->load_dir_includes($mySection); @@ -664,6 +675,14 @@ } } + //include the final shared & index files. + $lsData = $this->fileSystemObj->ls(); + if(isset($lsData['shared.inc']) && is_array($lsData['shared.inc'])) { + $this->add_include('shared.inc'); + } + if(isset($lsData['index.inc']) && is_array($lsData['index.inc'])) { + $this->add_include('index.inc'); + } }//end load_includes() //------------------------------------------------------------------------ @@ -679,13 +698,13 @@ //attempt to load the shared includes file. if(isset($lsData['shared.inc']) && $lsData['shared.inc']['type'] == 'file') { - $this->includesList[] = $this->fileSystemObj->realcwd .'/shared.inc'; + $this->add_include('shared.inc'); } //attempt to load the section's includes file. $myFile = $section .'.inc'; if(isset($lsData[$myFile]) && $lsData[$myFile]['type'] == 'file') { - $this->includesList[] = $this->fileSystemObj->realcwd .'/'. $myFile; + $this->add_include($myFile); } if(isset($lsData[$section]) && !count($this->sectionArr)) { @@ -850,5 +869,25 @@ //------------------------------------------------------------------------ + + //------------------------------------------------------------------------ + private final function add_include($file) { + $myFile = $this->fileSystemObj->realcwd .'/'. $file; + if(array_search($myFile, $this->includesList)) { + $this->gfObj->debug_print("<h1><font color='red'>". __METHOD__ .": file (". $myFile .") already exists... </h1>". cs_debug_backtrace(0) ."</font>"); + #exit; + } + else { + $this->includesList[] = $myFile; + + $this->gfObj->debug_print("<h1>". __METHOD__ .": included (". $myFile .")</h1>"); + } + if($file == 'index.inc') { + cs_debug_backtrace(1); + } + }//end add_include() + //------------------------------------------------------------------------ + + }//end contentSystem{} ?> Modified: trunk/1.0/cs_genericPage.class.php =================================================================== --- trunk/1.0/cs_genericPage.class.php 2009-02-04 17:31:19 UTC (rev 356) +++ trunk/1.0/cs_genericPage.class.php 2009-02-05 22:04:03 UTC (rev 357) @@ -61,7 +61,13 @@ */ protected function initialize_locals($mainTemplateFile) { + //replace multiple slashes with a single one to avoid confusing other logic... + $mainTemplateFile = preg_replace('/(\/){2,}/', '/', $mainTemplateFile); + if(preg_match('/\//', $mainTemplateFile) == 1 && preg_match('/^/', $mainTemplateFile)) { + $mainTemplateFile = preg_replace('/^\//', '', $mainTemplateFile); + } + if(strlen(dirname($mainTemplateFile)) && dirname($mainTemplateFile) !== '/' && !preg_match('/^\./', dirname($mainTemplateFile))) { $this->tmplDir = dirname($mainTemplateFile); $this->siteRoot = preg_replace('/\/templates$/', '', $this->tmplDir); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-03-02 17:55:00
|
Revision: 366 http://cs-content.svn.sourceforge.net/cs-content/?rev=366&view=rev Author: crazedsanity Date: 2009-03-02 17:54:58 +0000 (Mon, 02 Mar 2009) Log Message: ----------- Set default section, handle request for relative filename w/leading slash. /contentSystem.class.php: * parse_section(): -- if we encounter "/" as the section, potentially use "DEFAULT_SECTION" constant value instead of hard-coding "/content/index" (see #43) /cs_fileSystem.class.php: * filename2absolute(): -- special handling of requested file with single leading slash (treated as a file within the relative path instead of absolute). -- removed some extraneous commented-out debugging code. /tests/testOfCSContent.php: * __construct(): -- set constant for files directory * test_siteConfig(): -- use constant for files directory * test_genericPage(): -- use constant for files directory * test_cs_fileSystem() [NEW]: -- test handling of files with leading slashes, multiple slashes, dots, etc. to ensure it works as expected. -- this could stand to have many more tests... Modified Paths: -------------- trunk/1.0/contentSystem.class.php trunk/1.0/cs_fileSystem.class.php trunk/1.0/tests/testOfCSContent.php Modified: trunk/1.0/contentSystem.class.php =================================================================== --- trunk/1.0/contentSystem.class.php 2009-02-10 17:40:03 UTC (rev 365) +++ trunk/1.0/contentSystem.class.php 2009-03-02 17:54:58 UTC (rev 366) @@ -264,8 +264,8 @@ private function parse_section() { //TODO::: this should be an OPTIONAL THING as to how to handle "/" (i.e. CSCONTENT_HANDLE_ROOTURL='content/index') - if($this->section === 0 || is_null($this->section) || !strlen($this->section)) { - $this->section = "content/index"; + if(($this->section === 0 || is_null($this->section) || !strlen($this->section)) && defined('DEFAULT_SECTION')) { + $this->section = constant('DEFAULT_SECTION'); } $myArr = split('/', $this->section); Modified: trunk/1.0/cs_fileSystem.class.php =================================================================== --- trunk/1.0/cs_fileSystem.class.php 2009-02-10 17:40:03 UTC (rev 365) +++ trunk/1.0/cs_fileSystem.class.php 2009-03-02 17:54:58 UTC (rev 366) @@ -413,14 +413,19 @@ $filename = $this->resolve_path_with_dots($filename); - //see if it starts with a "/"... + //If it's a single filename beginning with a slash, strip the slash. + $x = array(); + $numSlashes = preg_match_all('/\//', $filename, $x); + if(preg_match('/^\/[\w]/', $filename) && !preg_match('/^\/\./', $filename) && $numSlashes == 1) { + $filename = preg_replace('/^\//', '', $filename); + } + + if(preg_match("/^\//", $filename)) { $retval = $filename; } else { $retval=$this->realcwd .'/'. $filename; $retval = $this->resolve_path_with_dots($retval); - #debug_print(__METHOD__ .": realcwd=(". $this->realcwd .")"); - #$this->resolve_path_with_dots($retval); } if(!$this->check_chroot($retval, FALSE)) { Modified: trunk/1.0/tests/testOfCSContent.php =================================================================== --- trunk/1.0/tests/testOfCSContent.php 2009-02-10 17:40:03 UTC (rev 365) +++ trunk/1.0/tests/testOfCSContent.php 2009-03-02 17:54:58 UTC (rev 366) @@ -24,6 +24,9 @@ $this->gfObj = new cs_globalFunctions; $this->gfObj->debugPrintOpt=1; + + $filesDir = dirname(__FILE__) ."/files"; + define('TEST_FILESDIR', $filesDir); }//end __construct() //------------------------------------------------------------------------- @@ -179,7 +182,7 @@ //------------------------------------------------------------------------- public function test_siteConfig() { - $configFile = dirname(__FILE__) .'/files/sampleConfig.xml'; + $configFile = constant('TEST_FILESDIR') .'/sampleConfig.xml'; $varPrefix = preg_replace("/:/", "_", __METHOD__ ."-"); $sc = new cs_siteConfig($configFile, 'main', $varPrefix); @@ -247,7 +250,7 @@ //------------------------------------------------------------------------- function test_genericPage() { - $filesDir = dirname(__FILE__) .'/files'; + $filesDir = constant('TEST_FILESDIR'); $page = new cs_genericPage(false, $filesDir .'/templates/main.shared.tmpl', false); $fs = new cs_fileSystem($filesDir .'/templates'); @@ -302,6 +305,36 @@ + //------------------------------------------------------------------------- + function test_cs_fileSystem() { + $fs = new cs_fileSystem(constant('TEST_FILESDIR')); + + $list = array( + 'slashTest' => array('/sampleConfig.xml', 'sampleConfig.xml'), + 'slashtest2' => array('/templates/content.shared.tmpl', 'templates/content.shared.tmpl'), + 'pathWithDots' => array('templates/.././sampleConfig.xml', '/templates/.././sampleConfig.xml'), + 'multiSlashes' => array('////sampleConfig.xml', '///sampleConfig.xml', '/templates///////content.shared.tmpl/../templates/content.shared.tmpl') + ); + + foreach($list as $testName=>$files) { + foreach($files as $filename) { + $gotException=false; + try { + $data = $fs->ls('/sampleConfig.xml'); + } + catch(exception $e) { + $gotException=true; + } + + $this->assertFalse($gotException, "Failed test '". $testName ."'"); + } + } + + }//end test_cs_fileSystem() + //------------------------------------------------------------------------- + + + }//end TestOfCSContent //============================================================================= ?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-04-22 20:43:44
|
Revision: 371 http://cs-content.svn.sourceforge.net/cs-content/?rev=371&view=rev Author: crazedsanity Date: 2009-04-22 20:43:39 +0000 (Wed, 22 Apr 2009) Log Message: ----------- Fixes for PHP warnings, update for "htdocs" public folder, setting to stop printing from an include. contentSystem.class.php: * finish(): -- only print if templateObj->printOnFinish is true... helps for embedded XML interfaces. cs_genericPage.class.php: * MAIN::: -- new public var, $printOnFinish=true * initialize_locals(): -- also remove "htdocs" from siteRoot. * set_block_row(): -- minor change to fix PHP warnings about referencing non-existent array indexes. * print_page(): -- another minor change to fix PHP warnings on array indexes -- add quotes in reference to 'out' array index to avoid PHP warnings about constants. * rip_all_block_rows(): -- superficial change to avoid PHP warnings if no array was returned. Modified Paths: -------------- trunk/1.0/contentSystem.class.php trunk/1.0/cs_genericPage.class.php Modified: trunk/1.0/contentSystem.class.php =================================================================== --- trunk/1.0/contentSystem.class.php 2009-03-27 14:56:04 UTC (rev 370) +++ trunk/1.0/contentSystem.class.php 2009-04-22 20:43:39 UTC (rev 371) @@ -793,7 +793,9 @@ } if($this->isValid === TRUE) { - $page->print_page(); + if($this->templateObj->printOnFinish === true) { + $page->print_page(); + } } else { $this->die_gracefully($this->reason); Modified: trunk/1.0/cs_genericPage.class.php =================================================================== --- trunk/1.0/cs_genericPage.class.php 2009-03-27 14:56:04 UTC (rev 370) +++ trunk/1.0/cs_genericPage.class.php 2009-04-22 20:43:39 UTC (rev 371) @@ -15,6 +15,7 @@ public $templateVars = array(); //our copy of the global templateVars public $mainTemplate; //the default layout of the site public $unhandledVars=array(); + public $printOnFinish=true; private $tmplDir; private $libDir; @@ -78,6 +79,7 @@ else { //NOTE: this **requires** that the global variable "SITE_ROOT" is already set. $this->siteRoot = preg_replace('/\/public_html/', '', $_SERVER['DOCUMENT_ROOT']); + $this->siteRoot = preg_replace('/\/htdocs/', '', $_SERVER['DOCUMENT_ROOT']); $this->tmplDir = $this->siteRoot .'/templates'; } $this->libDir = $this->siteRoot .'/lib'; @@ -218,7 +220,7 @@ $reg = "/<!-- BEGIN $handle -->(.+){0,}<!-- END $handle -->/sU"; preg_match_all($reg, $str, $m); - if(!is_string($m[0][0])) { + if(!is_array($m) || !isset($m[0][0]) || !is_string($m[0][0])) { #exit("set_block_row(): couldn't find '$handle' in var '$parent'"); $retval = FALSE; } else { @@ -287,9 +289,9 @@ foreach($tags as $key=>$str) { $str2 = str_replace("{", "", $str); $str2 = str_replace("}", "", $str2); - if(!$this->templateVars[$str2] && $stripUndefVars) { + if(!isset($this->templateVars[$str2]) && $stripUndefVars) { //TODO: set an internal pointer or something to use here, so they can see what was missed. - $this->templateObj->varvals[out] = str_replace($str, '', $this->templateObj->varvals[out]); + $this->templateObj->varvals['out'] = str_replace($str, '', $this->templateObj->varvals['out']); $this->unhandledVars[$str2]++; } } @@ -594,17 +596,20 @@ function rip_all_block_rows($templateVar="content", $exceptionArr=array()) { $rowDefs = $this->get_block_row_defs($templateVar); - $useTheseBlockRows = $rowDefs['ordered']; $retval = array(); - if(is_array($useTheseBlockRows)) { - foreach($useTheseBlockRows as $blockRowName) - { - if(!in_array($blockRowName, $exceptionArr)) + + if(is_array($rowDefs) && isset($rowDefs['ordered'])) { + $useTheseBlockRows = $rowDefs['ordered']; + if(is_array($useTheseBlockRows)) { + foreach($useTheseBlockRows as $blockRowName) { - //remove the block row. - $rowData = $this->set_block_row($templateVar, $blockRowName); - $retval[$blockRowName] = $rowData; + if(!in_array($blockRowName, $exceptionArr)) + { + //remove the block row. + $rowData = $this->set_block_row($templateVar, $blockRowName); + $retval[$blockRowName] = $rowData; + } } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-05-01 19:24:50
|
Revision: 373 http://cs-content.svn.sourceforge.net/cs-content/?rev=373&view=rev Author: crazedsanity Date: 2009-05-01 19:24:49 +0000 (Fri, 01 May 2009) Log Message: ----------- Ability to inject vars into pages, make section retrieval case insensitive. /contentSystem.class.php: * MAIN::: -- new (private) var, $injectVars=array() * finish(): -- checks if the internal injectVars array has things in it, and will then inject variables with the given names into the local scope. * inject_var() [NEW]: -- inject a variable into the local scope. /cs_siteConfig.class.php: * get_section(): -- upper-case the given section so no errors are thrown when they ask for a lowercase section. Modified Paths: -------------- trunk/1.0/contentSystem.class.php trunk/1.0/cs_siteConfig.class.php Modified: trunk/1.0/contentSystem.class.php =================================================================== --- trunk/1.0/contentSystem.class.php 2009-05-01 13:42:14 UTC (rev 372) +++ trunk/1.0/contentSystem.class.php 2009-05-01 19:24:49 UTC (rev 373) @@ -103,6 +103,8 @@ private $isValid=FALSE; private $reason=NULL; + private $injectVars=array(); + //------------------------------------------------------------------------ /** * The CONSTRUCTOR. Duh. @@ -737,6 +739,19 @@ } $page =& $this->templateObj; + + if(is_array($this->injectVars) && count($this->injectVars)) { + $definedVars = get_defined_vars(); + foreach($this->injectVars as $myVarName=>$myVarVal) { + if(!isset($definedVars[$myVarName])) { + $$myVarName = $myVarVal; + } + else { + throw new exception(__METHOD__ .": attempt to inject already defined var '". $myVarName ."'"); + } + } + } + if(is_object($this->session)) { $page->session =& $this->session; } @@ -863,5 +878,13 @@ //------------------------------------------------------------------------ + + //------------------------------------------------------------------------ + public function inject_var($varName, $value) { + $this->injectVars[$varName] = $value; + }//end inject_var() + //------------------------------------------------------------------------ + + }//end contentSystem{} ?> Modified: trunk/1.0/cs_siteConfig.class.php =================================================================== --- trunk/1.0/cs_siteConfig.class.php 2009-05-01 13:42:14 UTC (rev 372) +++ trunk/1.0/cs_siteConfig.class.php 2009-05-01 19:24:49 UTC (rev 373) @@ -234,6 +234,7 @@ */ public function get_section($section) { if($this->isInitialized === true) { + $section = strtoupper($section); $data = $this->a2p->get_data($section); if(is_array($data) && count($data) && $data['type'] == 'open') { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-05-06 19:30:08
|
Revision: 376 http://cs-content.svn.sourceforge.net/cs-content/?rev=376&view=rev Author: crazedsanity Date: 2009-05-06 19:30:00 +0000 (Wed, 06 May 2009) Log Message: ----------- Fix PHP warning, allow multiple tab block rows in same set. NOTE::: cs_tabs needs some work to make it more useful and extensible. There is some stuff to figure out with logic in load_tabs_template()--i.e. is it still needed? NOTE2:: cs_tabs will overwrite existing tabs of the same name, so duplicate tab names aren't allowed... but some apps may require this. Consider adding. /cs_genericPage.class.php: * get_block_row_defs(): -- check if the index in templateVars is set before evaluating it. /cs_tabs.class.php: * MAIN::: -- set tabsArray as an array() initially. -- NEW (private) VAR: $defaultSuffix='tab' (default if no block row suffix is given (i.e. this would require "selected_tab" and "unselected_tab" to function). * load_tabs_template(): -- no longer throws exceptions, nor does it do any real checking -- rips block rows for the given template var... * add_tab_array(): -- ARG CHANGE: NEW ARG: #2 ($useSuffix=null) -- passes $useSuffix when calling add_tab(). * add_tab(): -- ARG CHANGE: NEW ARG: #2 ($useSuffix=null) -- Now stores an array of data instead of just the text of the url: the "suffix" denotes what block row to use, allowing multiple different rows to be used within the same tab set. -- if no suffix is given, the default suffix is used. * display_tabs(): -- code to automatically select a tab if one is not already selected. -- instead of parsing a known templateRow (block row), it is dynamic, determined based on the "suffix" given. -- can throw an exception if referenced templateRow doesn't exist. -- updated exception (when no tabs present) to use magic __METHOD__ constant. * tab_exists() [NEW]: -- method to determine if the named tab exists or not. * rename_tab() [NEW]: -- rename existing tab to a new one. -- throws an exception if the named tab doesn't exist or if a tab named as the new name already exists. Modified Paths: -------------- trunk/1.0/cs_genericPage.class.php trunk/1.0/cs_tabs.class.php Modified: trunk/1.0/cs_genericPage.class.php =================================================================== --- trunk/1.0/cs_genericPage.class.php 2009-05-06 15:23:00 UTC (rev 375) +++ trunk/1.0/cs_genericPage.class.php 2009-05-06 19:30:00 UTC (rev 376) @@ -551,8 +551,8 @@ //NOTE: the value 30 isn't just a randomly chosen length; it's the minimum // number of characters to have a block row. EG: "<!-- BEGIN x --><!-- END x -->" - $templateContents = $this->templateVars[$templateVar]; - if(strlen($templateContents) >= 30) { + if(isset($this->templateVars[$templateVar]) && strlen($this->templateVars[$templateVar]) >= 30) { + $templateContents = $this->templateVars[$templateVar]; //looks good to me. Run the regex... $flags = PREG_PATTERN_ORDER; $reg = "/<!-- BEGIN (\S{1,}) -->/"; Modified: trunk/1.0/cs_tabs.class.php =================================================================== --- trunk/1.0/cs_tabs.class.php 2009-05-06 15:23:00 UTC (rev 375) +++ trunk/1.0/cs_tabs.class.php 2009-05-06 19:30:00 UTC (rev 376) @@ -8,18 +8,15 @@ class cs_tabs extends cs_contentAbstract { - private $tabsArr; + private $tabsArr=array(); private $selectedTab; private $csPageObj; private $templateVar; - /** Block row with the "selected" tab */ - private $selectedTabContent; + /** This is the default suffix to use when none is given during the add_tab() call. */ + private $defaultSuffix='tab'; - /** Block row with the "unselected" tab */ - private $unselectedTabContent; - //--------------------------------------------------------------------------------------------- /** * Build the object, and parses the given template. Tabs must be added & selected manually. @@ -64,26 +61,21 @@ //now let's parse it for the proper block rows. $blockRows = $this->csPageObj->rip_all_block_rows($this->templateVar); - if(count($blockRows) < 2 || !isset($blockRows['selected_tab']) || !isset($blockRows['unselected_tab'])) { - //not enough blocks, or they're not properly named. - throw new exception("cs_tabs::load_tabs_template(): failed to retrieve the required block rows"); - } - else { - //got the rows. Yay! - $this->selectedTabContent = $blockRows['selected_tab']; - $this->unselectedTabContent = $blockRows['unselected_tab']; - } + #if(count($blockRows) < 2) { + # //not enough blocks, or they're not properly named. + # throw new exception("cs_tabs::load_tabs_template(): failed to retrieve the required block rows"); + #} }//end load_tabs_template() //--------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------- - public function add_tab_array(array $tabs) { + public function add_tab_array(array $tabs, $useSuffix=null) { $retval = 0; foreach($tabs as $name=>$url) { //call an internal method to do it. - $retval += $this->add_tab($name, $url); + $retval += $this->add_tab($name, $url, $useSuffix); } return($retval); @@ -107,9 +99,18 @@ //--------------------------------------------------------------------------------------------- - public function add_tab($tabName, $url) { + public function add_tab($tabName, $url, $useSuffix=null) { + + //set the default suffix. + if(is_null($useSuffix)) { + $useSuffix = $this->defaultSuffix; + } + //add it to an array. - $this->tabsArr[$tabName] = $url; + $this->tabsArr[$tabName] = array( + 'url' => $url, + 'suffix' => $useSuffix + ); }//end add_tab() //--------------------------------------------------------------------------------------------- @@ -120,16 +121,33 @@ * Call this to add the parsed tabs into the page. */ public function display_tabs() { + + if(!strlen($this->selectedTab)) { + $keys = array_keys($this->tabsArr); + $this->select_tab($keys[0]); + } + if(is_array($this->tabsArr) && count($this->tabsArr)) { $this->load_tabs_template(); $finalString = ""; //loop through the array. - foreach($this->tabsArr as $tabName=>$url) { - $useTabContent = $this->unselectedTabContent; - if(strtolower($tabName) === strtolower($this->selectedTab)) { - //it's selected. - $useTabContent = $this->selectedTabContent; + foreach($this->tabsArr as $tabName=>$tabData) { + + $url = $tabData['url']; + $suffix = $tabData['suffix']; + + $blockRowName = 'unselected_'. $suffix; + if(strtolower($tabName) == strtolower($this->selectedTab)) { + $blockRowName = 'selected_'. $suffix; } + + if(isset($this->csPageObj->templateRows[$blockRowName])) { + $useTabContent = $this->csPageObj->templateRows[$blockRowName]; + } + else { + throw new exception(__METHOD__ ."(): failed to load block row (". $blockRowName .") for tab (". $tabName .")". $this->csPageObj->gfObj->debug_print($this->csPageObj->templateRows,0)); + } + $parseThis = array( 'title' => $tabName, 'url' => $url @@ -142,11 +160,40 @@ } else { //something bombed. - throw new exception("cs_tabs::display_tabs(): no tabs to add"); + throw new exception(__METHOD__ ."(): no tabs to add"); } }//end display_tabs() //--------------------------------------------------------------------------------------------- + + //--------------------------------------------------------------------------------------------- + /** + * Determine if the given named tab exists (returns boolean true/false) + */ + public function tab_exists($tabName) { + $retval = false; + if(isset($this->tabsArr[$tabName])) { + $retval = true; + } + return($retval); + }//end tab_exists() + //--------------------------------------------------------------------------------------------- + + + + //--------------------------------------------------------------------------------------------- + public function rename_tab($tabName, $newTabName) { + if($this->tab_exists($tabName) && !$this->tab_exists($newTabName)) { + $tabContents = $this->tabsArr[$tabName]; + unset($this->tabsArr[$tabName]); + $this->tabsArr[$newTabName] = $tabContents; + } + else { + throw new exception(__METHOD__ .": tried to rename non-existent tab (". $tabName .") to (". $newTabName .")"); + } + }//end rename_tab(); + //--------------------------------------------------------------------------------------------- + } ?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-06-01 14:50:02
|
Revision: 386 http://cs-content.svn.sourceforge.net/cs-content/?rev=386&view=rev Author: crazedsanity Date: 2009-06-01 14:49:57 +0000 (Mon, 01 Jun 2009) Log Message: ----------- Remove old code & consolidate how errors are displayed + use of APPURL. /contentSystem.class.php: * MAIN::: -- removed code that set SITE_ROOT globally * initialize_locals(): -- add APPURL as a template var, if it is defined. * die_gracefully(): -- push the message_box template into the 404 template and set all of the required template vars to make it appear properly. * add_template(): -- removed some debug_print() statements. /sample_files/: * updated 404 template to have a template var to stuff message_box into * update system templates to prefix image refs with {APPURL} * removed old templates (construction & db_error) * added images required to display message box. Modified Paths: -------------- trunk/1.0/contentSystem.class.php trunk/1.0/sample_files/templates/system/404.shared.tmpl trunk/1.0/sample_files/templates/system/message_box.tmpl Added Paths: ----------- trunk/1.0/sample_files/public_html/images/ trunk/1.0/sample_files/public_html/images/clear.gif trunk/1.0/sample_files/public_html/images/frame/ trunk/1.0/sample_files/public_html/images/frame/crn-white-bl.gif trunk/1.0/sample_files/public_html/images/frame/crn-white-br.gif trunk/1.0/sample_files/public_html/images/frame/crn-white-tl.gif trunk/1.0/sample_files/public_html/images/frame/crn-white-tr.gif Removed Paths: ------------- trunk/1.0/sample_files/templates/system/construction.content.tmpl trunk/1.0/sample_files/templates/system/db_error.tmpl Modified: trunk/1.0/contentSystem.class.php =================================================================== --- trunk/1.0/contentSystem.class.php 2009-05-31 21:22:05 UTC (rev 385) +++ trunk/1.0/contentSystem.class.php 2009-06-01 14:49:57 UTC (rev 386) @@ -63,13 +63,6 @@ * |--> /includes/content/members/test.inc */ -//TODO: remove this terrible little hack. -if(!isset($GLOBALS['SITE_ROOT'])) { - //define where our scripts are located. - $GLOBALS['SITE_ROOT'] = $_SERVER['DOCUMENT_ROOT']; - $GLOBALS['SITE_ROOT'] = str_replace("/public_html", "", $GLOBALS['SITE_ROOT']); -} - require_once(dirname(__FILE__) ."/abstract/cs_content.abstract.class.php"); require_once(dirname(__FILE__) ."/cs_fileSystem.class.php"); require_once(dirname(__FILE__) ."/cs_session.class.php"); @@ -157,6 +150,11 @@ } $this->templateObj->add_template_var('CURRENT_URL', $myUrl); + if(defined('APPURL')) { + //set the APPURL as a template var. + $this->templateObj->add_template_var('APPURL', constant('APPURL')); + } + //create a fileSystem object for templates. $tmplBaseDir = constant('SITE_ROOT') .'/templates'; if(defined('TMPLDIR')) { @@ -730,7 +728,14 @@ if($this->templateObj->template_file_exists('system/404.shared.tmpl')) { //Simple "Page Not Found" error... show 'em. $this->templateObj->add_template_var('main', $this->templateObj->file_to_string('system/404.shared.tmpl')); - $this->templateObj->add_template_var('details', $details); + + //add the message box & required template vars to display the error. + $this->templateObj->add_template_var('content', $this->templateObj->file_to_string('system/message_box.tmpl')); + $this->templateObj->add_template_var('messageType', 'fatal'); + $this->templateObj->add_template_var('title', "Fatal Error"); + $this->templateObj->add_template_var('message', $details); + + $this->templateObj->add_template_var('datetime', date('m-d-Y H:i:s')); $this->templateObj->print_page(); exit; @@ -917,12 +922,9 @@ //------------------------------------------------------------------------ private final function add_template($var, $file, $allowIndex=false) { if($var == 'index' && $allowIndex !== true) { - $this->gfObj->debug_print(__METHOD__ .": set index invalidly (". $file ."), IGNORED"); + //$this->gfObj->debug_print(__METHOD__ .": set index invalidly (". $file ."), IGNORED"); } else { - if(isset($this->templateList[$var])) { - $this->gfObj->debug_print(__METHOD__ .": REPLACING [". $var ."], was (". $this->templateList[$var] .") with (". $file .")"); - } $this->templateList[$var] = $file; } }//end add_template() Added: trunk/1.0/sample_files/public_html/images/clear.gif =================================================================== (Binary files differ) Property changes on: trunk/1.0/sample_files/public_html/images/clear.gif ___________________________________________________________________ Added: svn:executable + * Added: svn:mime-type + application/octet-stream Added: trunk/1.0/sample_files/public_html/images/frame/crn-white-bl.gif =================================================================== (Binary files differ) Property changes on: trunk/1.0/sample_files/public_html/images/frame/crn-white-bl.gif ___________________________________________________________________ Added: svn:executable + * Added: svn:mime-type + application/octet-stream Added: trunk/1.0/sample_files/public_html/images/frame/crn-white-br.gif =================================================================== (Binary files differ) Property changes on: trunk/1.0/sample_files/public_html/images/frame/crn-white-br.gif ___________________________________________________________________ Added: svn:executable + * Added: svn:mime-type + application/octet-stream Added: trunk/1.0/sample_files/public_html/images/frame/crn-white-tl.gif =================================================================== (Binary files differ) Property changes on: trunk/1.0/sample_files/public_html/images/frame/crn-white-tl.gif ___________________________________________________________________ Added: svn:executable + * Added: svn:mime-type + application/octet-stream Added: trunk/1.0/sample_files/public_html/images/frame/crn-white-tr.gif =================================================================== (Binary files differ) Property changes on: trunk/1.0/sample_files/public_html/images/frame/crn-white-tr.gif ___________________________________________________________________ Added: svn:executable + * Added: svn:mime-type + application/octet-stream Modified: trunk/1.0/sample_files/templates/system/404.shared.tmpl =================================================================== --- trunk/1.0/sample_files/templates/system/404.shared.tmpl 2009-05-31 21:22:05 UTC (rev 385) +++ trunk/1.0/sample_files/templates/system/404.shared.tmpl 2009-06-01 14:49:57 UTC (rev 386) @@ -8,37 +8,11 @@ --> <head> <title>Page Not Found</title> + <link rel="stylesheet" href="{APPURL}/css/site.css" type="text/css"> </head> <body> <table border=0 cellpadding=0 cellspacing=0 width="90%" align="center"> -<tr> - <!-- This row should span across the page. --> - - <td align="center" colspan=2> - <table border="0" cellpadding="0" cellspacing="0" width="100%"> -<tbody><tr> - <td align="center"> - <font face="Georgia, Times New Roman, Times, serif"> - - <table border="0" cellpadding="3" cellspacing="0"> - <tbody><tr valign="middle"> - <td align="right">Crazed</td> - <td align="center" width="118"><img border="0" src="http://crazedsanity.com/images/dev-logo.gif?from=404" width="118" width="67"></td> - - <td align="left">Sanity</td> - </tr> - <tr> - <td colspan="3" align="center"> - Dot Com<BR> - </td> - </tr> - </tbody></table> - - </font> - - </td> -</tr> <TR> <td align="center"><!-- This row should span across the page. --><hr>{datetime}<hr></td> </TR> @@ -46,23 +20,14 @@ <tr> <td> - Could not find the page requested...<BR> - <pre>{details}</pre> + + + {content} + + + </td> </tr> - <tr> - <td> - <hr> - <div align="center"><font size="1"> - All content is ©opyright CrazedSanity - Computer Industries, 1998-2007. <br> - CrazedSanity Computer Industries is a division of <a href="http://unlimited.buzzkill.org">BuzzKill - Productions Unlimited ®</a>. All rights reserved. <br> - For questions, to report a problem, or to offer your first born child as a sacrifice, - contact <a href="mailto:webmaster@127.0.0.1">webmaster -at- crazedsanity.com</a>.</font><br><br> -</div> - </td> - </tr> - </table> +</table> </body> </html> Deleted: trunk/1.0/sample_files/templates/system/construction.content.tmpl =================================================================== --- trunk/1.0/sample_files/templates/system/construction.content.tmpl 2009-05-31 21:22:05 UTC (rev 385) +++ trunk/1.0/sample_files/templates/system/construction.content.tmpl 2009-06-01 14:49:57 UTC (rev 386) @@ -1,5 +0,0 @@ -<!-- BEGIN content/construction_content.tmpl --> - <h2 align="center"><br> - <br> - It's broken. I don't know what to say, dude. -<!-- END content/construction_content.tmpl --> Deleted: trunk/1.0/sample_files/templates/system/db_error.tmpl =================================================================== --- trunk/1.0/sample_files/templates/system/db_error.tmpl 2009-05-31 21:22:05 UTC (rev 385) +++ trunk/1.0/sample_files/templates/system/db_error.tmpl 2009-06-01 14:49:57 UTC (rev 386) @@ -1,47 +0,0 @@ -<HTML> -<HEAD> - <title>CS - Database Error</title> -</HEAD> - -<body bgcolor="white" text="#666666" link="#006666" vlink="#006666" alink="#006666"> -<table border="0" cellpadding="0" cellspacing="0" width="100%"> -<tr><!-- This row should span across the page. --> - <td align="center"> - <font face="Georgia, Times New Roman, Times, serif"> - <table border=0 cellpadding=3 cellspacing=0> - <TR> - <td>Crazed</td> - <td><img border=0 src="/images/dev-logo.gif"></td> - <td>Sanity</td> - </TR> - <TR> - <td colspan="3" align="center">Dot Com</td> - </TR> - </table> - </font> - </td> -</tr> -</table> - -<hr> - -<!-- BEGIN excuse --> -<p align="center">If you're seeing this, that means something pretty bad is happening.<BR> -Either the database went down, or bigger issues have arisen.<BR> -Check back in awhile.</p> -<!-- END excuse --> - - - <hr> -<center> -<font size="1"> - <a href="http://www.buzzkill.org/"><img src="/images/Bk3dtext.jpg" width="474" height="40" border="0"></a><BR> -All content is copyright CrazedSanity.com, 1998-2004. <br> - CrazedSanity.com is a division of -<a href="http://www.buzzkill.org/">BuzzKill Productions Unlimited ®</a>. All rights reserved. <br> - For questions or to report a problem, contact -<a href="?emailLink=spam" onClick="invalidEmailLink('webmaster')">webmaster at crazedsanity dot com</a>.</font> -</center> -</body> - -</HTML> Modified: trunk/1.0/sample_files/templates/system/message_box.tmpl =================================================================== --- trunk/1.0/sample_files/templates/system/message_box.tmpl 2009-05-31 21:22:05 UTC (rev 385) +++ trunk/1.0/sample_files/templates/system/message_box.tmpl 2009-06-01 14:49:57 UTC (rev 386) @@ -1,12 +1,12 @@ -<!-- *** MESSAGE_BOX START (/help/messages/message_box.tmpl) *** --> +<!-- *** MESSAGE_BOX START (/system/message_box.tmpl) *** --> <table border="0" cellspacing="0" cellpadding="0" align="center"> <tr> - <td class="{messageType}" width="5" align="left" valign="top"><img src="/images/frame/crn-white-tl.gif" width="5" height="5" alt=""></td> - <td class="{messageType}"><img src="/images/clear.gif" width="20" height="2" alt=""></td> - <td class="{messageType}" width="5" align="right" valign="top"><img src="/images/frame/crn-white-tr.gif" width="5" height="5" alt=""></td> + <td class="{messageType}" width="5" align="left" valign="top"><img src="{APPURL}/images/frame/crn-white-tl.gif" width="5" height="5" alt=""></td> + <td class="{messageType}"><img src="{APPURL}/images/clear.gif" width="20" height="2" alt=""></td> + <td class="{messageType}" width="5" align="right" valign="top"><img src="{APPURL}/images/frame/crn-white-tr.gif" width="5" height="5" alt=""></td> </tr> <tr> - <td class="{messageType}" width="5"><img src="/images/clear.gif" width="5" height="20" alt=""></td> + <td class="{messageType}" width="5"><img src="{APPURL}/images/clear.gif" width="5" height="20" alt=""></td> <td> <table class="{messageType}" width="100%" border="0" cellspacing="0" cellpadding="5"> <tr> @@ -16,14 +16,14 @@ {redirect} </td> </tr> </table></td> - <td class="{messageType}" width="5"><img src="/images/clear.gif" width="5" height="20" alt=""></td> + <td class="{messageType}" width="5"><img src="{APPURL}/images/clear.gif" width="5" height="20" alt=""></td> </tr> <tr> - <td class="{messageType}" width="5" align="left" valign="bottom"><img src="/images/frame/crn-white-bl.gif" width="5" height="5" alt=""></td> - <td class="{messageType}"><img src="/images/clear.gif" width="20" height="2" alt=""></td> - <td class="{messageType}" width="5" align="right" valign="bottom"><img src="/images/frame/crn-white-br.gif" width="5" height="5" alt=""></td> + <td class="{messageType}" width="5" align="left" valign="bottom"><img src="{APPURL}/images/frame/crn-white-bl.gif" width="5" height="5" alt=""></td> + <td class="{messageType}"><img src="{APPURL}/images/clear.gif" width="20" height="2" alt=""></td> + <td class="{messageType}" width="5" align="right" valign="bottom"><img src="{APPURL}/images/frame/crn-white-br.gif" width="5" height="5" alt=""></td> </tr> </table> <br> -<!-- *** MESSAGE_BOX END (/help/messages/message_box.tmpl) *** --> +<!-- *** MESSAGE_BOX END (/system/message_box.tmpl) *** --> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-06-17 15:19:04
|
Revision: 395 http://cs-content.svn.sourceforge.net/cs-content/?rev=395&view=rev Author: crazedsanity Date: 2009-06-17 15:19:02 +0000 (Wed, 17 Jun 2009) Log Message: ----------- Minor changes to pgsql layer for compatibility with mysql layer. NOTE::: the main cs_phpDB class should have an abstract class or interface that defines all methods, etc that are required to work with it, so there is an easy-to-identify standard list of method names. /cs_phpDB.class.php: * get_dbtype() [NEW]: -- returns internal private $dbType /db_types/cs_phpDB__pgsql.class.php: * lastID() [NEW]: -- for compatibility with mysql calls (except this requires the sequence name; without some fancy SQL to retrieve the given sequence, any code attempting to use this will have to be coded around PostgreSQL so it knows the sequence name, or will have to build its schema so the sequence name is known and ALWAYS pass it). Modified Paths: -------------- trunk/1.0/cs_phpDB.class.php trunk/1.0/db_types/cs_phpDB__pgsql.class.php Modified: trunk/1.0/cs_phpDB.class.php =================================================================== --- trunk/1.0/cs_phpDB.class.php 2009-06-12 13:52:39 UTC (rev 394) +++ trunk/1.0/cs_phpDB.class.php 2009-06-17 15:19:02 UTC (rev 395) @@ -69,6 +69,14 @@ }//end __call() //========================================================================= + + + //========================================================================= + public function get_dbtype() { + return($this->dbType); + }//end get_dbtype() + //========================================================================= + } // end class phpDB ?> Modified: trunk/1.0/db_types/cs_phpDB__pgsql.class.php =================================================================== --- trunk/1.0/db_types/cs_phpDB__pgsql.class.php 2009-06-12 13:52:39 UTC (rev 394) +++ trunk/1.0/db_types/cs_phpDB__pgsql.class.php 2009-06-17 15:19:02 UTC (rev 395) @@ -1115,6 +1115,22 @@ + //========================================================================= + public function lastID($sequence) { + + if(strlen($sequence)) { + $retval = $this->get_currval($sequence); + } + else { + throw new exception(__METHOD__ .": no sequence specified (required by ". $this->dbType .")"); + } + + return($retval); + }//end lastID() + //========================================================================= + + + } // end class phpDB ?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-06-25 21:32:23
|
Revision: 398 http://cs-content.svn.sourceforge.net/cs-content/?rev=398&view=rev Author: crazedsanity Date: 2009-06-25 21:32:15 +0000 (Thu, 25 Jun 2009) Log Message: ----------- Add simple methods for querying & running inserts + fix mysql lastID(). /cs_phpDB.class.php: * run_query() [NEW]: -- runs a query and returns data in the requested array format. * run_insert() [NEW]: -- runs an insert & returns the newly-created ID. /db_types/cs_phpDB__mysql.class.php: * lastID(): -- use internal connectionID to avoid problems with multiple connections. -- fix comment (referred to "lastOID()") Modified Paths: -------------- trunk/1.0/cs_phpDB.class.php trunk/1.0/db_types/cs_phpDB__mysql.class.php Modified: trunk/1.0/cs_phpDB.class.php =================================================================== --- trunk/1.0/cs_phpDB.class.php 2009-06-25 19:01:25 UTC (rev 397) +++ trunk/1.0/cs_phpDB.class.php 2009-06-25 21:32:15 UTC (rev 398) @@ -77,6 +77,80 @@ }//end get_dbtype() //========================================================================= + + + //========================================================================= + /** + * Performs queries which require results. Passing $indexField returns a + * complex array indexed from that field; passing $valueField will change + * it to a name=>value formatted array. + * + * NOTE:: when using an index field, be sure it is guaranteed to be unique, + * i.e. it is a primary key! If duplicates are found, the database class + * will throw an exception! + */ + public function run_query($sql, $indexField=null, $valueField=null) { + + $retval = array(); + + //length must be 19 as that's about the shortest valid SQL: "select * from table" + if(strlen($sql) >= 19) { + $this->exec($sql); + + $numRows = $this->numRows(); + $dbError = $this->errorMsg(); + if($numRows > 0 && !strlen($dbError)) { + if(strlen($indexField) && (is_null($valueField) || !strlen($valueField))) { + //return a complex array based on a given field. + $retval = $this->farray_fieldnames($indexField, null, 0); + } + elseif(strlen($indexField) && strlen($valueField)) { + //return an array as name=>value pairs. + $retval = $this->farray_nvp($indexField, $valueField); + } + else { + $retval = $this->farray_fieldnames(); + } + } + elseif($numRows == 0 && !strlen($dbError)) { + $retval = false; + } + else { + $this->exception_handler(__METHOD__ .": no rows (". $numRows .") or dbError::: ". $dbError ."<BR>\nSQL::: ". $sql); + } + } + else { + $this->exception_handler(__METHOD__ .": invalid length SQL (". $sql .")"); + } + + return($retval); + }//end run_query() + //========================================================================= + + + + //========================================================================= + /** + * Handles performing the insert statement & returning the last inserted ID. + */ + protected function run_insert($sql) { + + $this->exec($sql); + + if($this->numAffected() == 1 && !strlen($this->errorMsg())) { + //retrieve the ID just created. + $retval = $this->lastID(); + } + else { + //something broke... + $this->exception_handler(__METHOD__ .": failed to insert, rows=(". $this->numRows .")... " + ."ERROR::: ". $this->errorMsg() ."\n -- SQL:::: ". $sql); + } + + return($retval); + }//end run_insert() + //========================================================================= + } // end class phpDB ?> Modified: trunk/1.0/db_types/cs_phpDB__mysql.class.php =================================================================== --- trunk/1.0/db_types/cs_phpDB__mysql.class.php 2009-06-25 19:01:25 UTC (rev 397) +++ trunk/1.0/db_types/cs_phpDB__mysql.class.php 2009-06-25 21:32:15 UTC (rev 398) @@ -692,9 +692,9 @@ * get last ID of last INSERT statement */ function lastID() { - $retval = mysql_insert_id(); + $retval = mysql_insert_id($this->connectionID); return($retval); - }//end lastOID() + }//end lastID() //========================================================================= This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-07-06 16:02:29
|
Revision: 403 http://cs-content.svn.sourceforge.net/cs-content/?rev=403&view=rev Author: crazedsanity Date: 2009-07-06 16:02:22 +0000 (Mon, 06 Jul 2009) Log Message: ----------- Minor fixes for update logic. /cs_phpDB.class.php: * run_update() [NEW]: -- simple logic for running updates ("run_query()" fails, since it attempts to call numRows() without checking numAffected()). /db_types/cs_phpDB__mysql.class.php: * numRows(): -- return 0 if the result is null OR if it isn't a valid resource. Modified Paths: -------------- trunk/1.0/cs_phpDB.class.php trunk/1.0/db_types/cs_phpDB__mysql.class.php Modified: trunk/1.0/cs_phpDB.class.php =================================================================== --- trunk/1.0/cs_phpDB.class.php 2009-07-02 15:40:51 UTC (rev 402) +++ trunk/1.0/cs_phpDB.class.php 2009-07-06 16:02:22 UTC (rev 403) @@ -151,6 +151,25 @@ }//end run_insert() //========================================================================= + + + //========================================================================= + /** + * Performs the update & returns how many rows were affected. + */ + public function run_update($sql, $zeroIsOk=false) { + $this->exec($sql); + + $numAffected = $this->numAffected(); + + if($numAffected==0 && $zeroIsOk == false) { + throw new exception(__METHOD__ .": no rows updated (". $numAffected .")"); + } + + return($numAffected); + }//end run_update() + //========================================================================= + } // end class phpDB ?> Modified: trunk/1.0/db_types/cs_phpDB__mysql.class.php =================================================================== --- trunk/1.0/db_types/cs_phpDB__mysql.class.php 2009-07-02 15:40:51 UTC (rev 402) +++ trunk/1.0/db_types/cs_phpDB__mysql.class.php 2009-07-06 16:02:22 UTC (rev 403) @@ -635,11 +635,10 @@ * Returns the number of rows in a result (from a SELECT query). */ function numRows() { - if ($this->result == null) { + if ($this->result == null || !is_resource($this->result)) { $retval = 0; } else { - //TODO: implement MySQL version.. $this->numrows = mysql_num_rows($this->result); $retval = $this->numrows; } @@ -670,7 +669,6 @@ $retval = 0; } else { - //TODO: implement MySQL version.. $retval = mysql_num_fields($this->result); } return($retval); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <cra...@us...> - 2009-07-20 18:58:32
|
Revision: 408 http://cs-content.svn.sourceforge.net/cs-content/?rev=408&view=rev Author: crazedsanity Date: 2009-07-20 18:58:30 +0000 (Mon, 20 Jul 2009) Log Message: ----------- Add information for debugging, MySQL transactions shouldn't autocommit. /cs_phpDB.class.php: * run_update(): -- add the SQL string to the error so it can be debugged. /db_types/cs_phpDB__mysql.class.php: * beginTrans(): -- turn off autocommit so changes aren't committed unless the transaction actually succeeded. Modified Paths: -------------- trunk/1.0/cs_phpDB.class.php trunk/1.0/db_types/cs_phpDB__mysql.class.php Modified: trunk/1.0/cs_phpDB.class.php =================================================================== --- trunk/1.0/cs_phpDB.class.php 2009-07-20 05:26:02 UTC (rev 407) +++ trunk/1.0/cs_phpDB.class.php 2009-07-20 18:58:30 UTC (rev 408) @@ -31,6 +31,7 @@ private $dbLayerObj; private $dbType; + public $connectParams = array(); //========================================================================= public function __construct($type='pgsql') { @@ -60,6 +61,10 @@ */ public function __call($methodName, $args) { if(method_exists($this->dbLayerObj, $methodName)) { + if($methodName == 'connect' && is_array($args[0])) { + //capture the connection parameters. + $this->connectParams = $args[0]; + } $retval = call_user_func_array(array($this->dbLayerObj, $methodName), $args); } else { @@ -133,13 +138,13 @@ /** * Handles performing the insert statement & returning the last inserted ID. */ - public function run_insert($sql, $sequence='null') { + public function run_insert($sql) { $this->exec($sql); if($this->numAffected() == 1 && !strlen($this->errorMsg())) { //retrieve the ID just created. - $retval = $this->lastID($sequence); + $retval = $this->lastID(); } else { //something broke... @@ -163,13 +168,26 @@ $numAffected = $this->numAffected(); if($numAffected==0 && $zeroIsOk == false) { - throw new exception(__METHOD__ .": no rows updated (". $numAffected .")"); + throw new exception(__METHOD__ .": no rows updated (". $numAffected ."), SQL::: ". $sql); } return($numAffected); }//end run_update() //========================================================================= + + + //========================================================================= + public function reconnect() { + if(is_array($this->connectParams) && count($this->connectParams)) { + $this->dbLayerObj->connect($this->connectParams, true); + } + else { + throw new exception(__METHOD__ .": no connection parameters stored"); + } + }//end reconnect() + //========================================================================= + } // end class phpDB ?> Modified: trunk/1.0/db_types/cs_phpDB__mysql.class.php =================================================================== --- trunk/1.0/db_types/cs_phpDB__mysql.class.php 2009-07-20 05:26:02 UTC (rev 407) +++ trunk/1.0/db_types/cs_phpDB__mysql.class.php 2009-07-20 18:58:30 UTC (rev 408) @@ -793,7 +793,7 @@ //========================================================================= public function beginTrans() { - $this->exec('BEGIN'); + $this->exec('BEGIN;SET autocommit=0;'); return(true); }//end beginTrans() //========================================================================= This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |