Added logic to NOT start sessions for bots

This commit is contained in:
Josh Sherman 2013-02-20 11:25:04 -05:00
parent 78a9d0dda5
commit b4eb28f945
3 changed files with 106 additions and 94 deletions

View file

@ -19,10 +19,11 @@
* Session Class * Session Class
* *
* Provides session handling via database instead of the file based session * Provides session handling via database instead of the file based session
* handling built into PHP. Using this class requires an array to be defined * handling built into PHP. Using this class requires an array to be
* in place of the boolean true/false (on/off). If simply array(), the * defined in place of the boolean true/false (on/off). If simply array(),
* datasource will default to the value in $config['pickles']['datasource'] and * the datasource will default to the value in
* if the table will default to "sessions". The format is as follows: * $config['pickles']['datasource'] and if the table will default to
* "sessions". The format is as follows:
* *
* $config = array( * $config = array(
* 'pickles' => array( * 'pickles' => array(
@ -33,8 +34,8 @@
* ) * )
* ); * );
* *
* In addition to the configuration variables, a table in your database must * In addition to the configuration variables, a table in your database
* be created. The [MySQL] table schema is as follows: * must be created. The [MySQL] table schema is as follows:
* *
* CREATE TABLE sessions ( * CREATE TABLE sessions (
* id varchar(32) COLLATE utf8_unicode_ci NOT NULL, * id varchar(32) COLLATE utf8_unicode_ci NOT NULL,
@ -44,10 +45,10 @@
* INDEX (expires_at) * INDEX (expires_at)
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; * ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
* *
* Note: The reason for not using a model class was to avoid a naming conflict * Note: The reason for not using a model class was to avoid a naming
* between the Session model and the Session class itself. This will eventually * conflict between the Session model and the Session class itself. This
* be resolved when I abandon full 5.x support and migrate to 5.3+ (assuming * will eventually be resolved when I abandon full 5.x support and migrate
* that ever happens). * to 5.3+ (assuming that ever happens).
*/ */
class Session extends Object class Session extends Object
{ {
@ -74,8 +75,8 @@ class Session extends Object
/** /**
* Time to Live * Time to Live
* *
* The number of seconds the session should remain active. Corresponds to * The number of seconds the session should remain active. Corresponds
* the INI variable session.gc_maxlifetime * to the INI variable session.gc_maxlifetime
* *
* @access private * @access private
* @var integer * @var integer
@ -96,8 +97,8 @@ class Session extends Object
/** /**
* Table * Table
* *
* Name of the database table in the aforementioned datasource that holds * Name of the database table in the aforementioned datasource that
* the session data. The expected schema is defined above. * holds the session data. The expected schema is defined above.
* *
* @access private * @access private
* @var string * @var string
@ -107,8 +108,8 @@ class Session extends Object
/** /**
* Database * Database
* *
* Our database object to interact with the aforementioned datasource and * Our database object to interact with the aforementioned datasource
* table. This object is shared with other PICKLES internals. * and table. This object is shared with other PICKLES internals.
* *
* @access private * @access private
* @var object * @var object
@ -118,10 +119,11 @@ class Session extends Object
/** /**
* Constructor * Constructor
* *
* All of our set up logic for the session in contained here. This object * All of our set up logic for the session in contained here. This
* is initially instantiated from pickles.php and the session callbacks are * object is initially instantiated from pickles.php and the session
* established here. All variables are driven from php.ini and/or the site * callbacks are established here. All variables are driven from
* config. Once configured, the session is started automatically. * php.ini and/or the site config. Once configured, the session is
* started automatically.
*/ */
public function __construct() public function __construct()
{ {
@ -178,7 +180,6 @@ class Session extends Object
{ {
case 'files': case 'files':
ini_set('session.save_handler', 'files'); ini_set('session.save_handler', 'files');
session_start();
break; break;
case 'memcache': case 'memcache':
@ -193,7 +194,6 @@ class Session extends Object
ini_set('session.save_handler', 'memcache'); ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', 'tcp://' . $hostname . ':' . $port . '?persistent=1&weight=1&timeout=1&retry_interval=15'); ini_set('session.save_path', 'tcp://' . $hostname . ':' . $port . '?persistent=1&weight=1&timeout=1&retry_interval=15');
session_start();
break; break;
case 'mysql': case 'mysql':
@ -211,8 +211,6 @@ class Session extends Object
// Initializes the session // Initializes the session
$this->initialize(); $this->initialize();
session_start();
} }
else else
{ {
@ -221,6 +219,13 @@ class Session extends Object
break; break;
} }
if (isset($_SERVER['HTTP_USER_AGENT'])
&& !String::isEmpty($_SERVER['HTTP_USER_AGENT'])
&& !preg_match('/(Baidu|Gigabot|Googlebot|libwww-perl|lwp-trivial|msnbot|SiteUptime|Slurp|WordPress|ZIBB|ZyBorg)/i', $_SERVER['HTTP_USER_AGENT']))
{
session_start();
}
} }
} }
@ -228,9 +233,9 @@ class Session extends Object
* Destructor * Destructor
* *
* Runs garbage collection and closes the session. I'm not sure if the * Runs garbage collection and closes the session. I'm not sure if the
* garbage collection should stay as it could be accomplished via php.ini * garbage collection should stay as it could be accomplished via
* variables. The session_write_close() is present to combat a chicken * php.ini variables. The session_write_close() is present to combat a
* and egg scenario in earlier versions of PHP 5. * chicken and egg scenario in earlier versions of PHP 5.
*/ */
public function __destruct() public function __destruct()
{ {
@ -245,8 +250,8 @@ class Session extends Object
* Initializes the Session * Initializes the Session
* *
* This method exists to combat the fact that calling session_destroy() * This method exists to combat the fact that calling session_destroy()
* also clears out the save handler. Upon destorying a session this method * also clears out the save handler. Upon destorying a session this
* is called again so the save handler is all set. * method is called again so the save handler is all set.
*/ */
public function initialize() public function initialize()
{ {
@ -267,8 +272,8 @@ class Session extends Object
* Opens the Session * Opens the Session
* *
* Since the session is in the database, opens the database connection. * Since the session is in the database, opens the database connection.
* This step isn't really necessary as the Database object is smart enough * This step isn't really necessary as the Database object is smart
* to open itself up upon execute. * enough to open itself up upon execute.
*/ */
public function open() public function open()
{ {
@ -307,7 +312,8 @@ class Session extends Object
/** /**
* Writes the Session * Writes the Session
* *
* When there's changes to the session, writes the data to the database. * When there's changes to the session, writes the data to the
* database.
* *
* @param string $id session ID * @param string $id session ID
* @param string $session serialized session data * @param string $session serialized session data

View file

@ -18,8 +18,8 @@
/** /**
* String Class * String Class
* *
* Just a simple collection of static functions to accomplish some of the more * Just a simple collection of static functions to accomplish some of the
* redundant string related manipulation. * more redundant string related manipulation.
*/ */
class String class String
{ {
@ -69,9 +69,9 @@ class String
/** /**
* Generate Slug * Generate Slug
* *
* Generates a slug from the pass string by lowercasing the string, trimming * Generates a slug from the pass string by lowercasing the string,
* whitespace and converting non-alphanumeric values to dashes. Takes care * trimming whitespace and converting non-alphanumeric values to
* of multiple dashes as well. * dashes. Takes care of multiple dashes as well.
* *
* @static * @static
* @param string $string to be converted to the slug * @param string $string to be converted to the slug
@ -91,10 +91,10 @@ class String
/** /**
* Is Empty * Is Empty
* *
* Checks if a string is empty. You can use the PHP function empty() but * Checks if a string is empty. You can use the PHP function empty()
* that returns true for a string of "0". Last I checked, that's not an * but that returns true for a string of "0". Last I checked, that's
* empty string. PHP's function also doesn't apply trim() to the value * not an empty string. PHP's function also doesn't apply trim() to the
* to ensure it's not just a bunch of spaces. * value to ensure it's not just a bunch of spaces.
* *
* @static * @static
* @param string $value string(s) to be checked * @param string $value string(s) to be checked
@ -119,8 +119,8 @@ class String
/** /**
* Pluralize * Pluralize
* *
* Based on a passed integer, the word will be pluralized. A value of zero * Based on a passed integer, the word will be pluralized. A value of
* will also pluralize the word (e.g. 0 things not 0 thing). * zero will also pluralize the word (e.g. 0 things not 0 thing).
* *
* @static * @static
* @param string $string the word to plurailze * @param string $string the word to plurailze
@ -210,8 +210,8 @@ class String
/** /**
* Truncate * Truncate
* *
* Truncates a string to a specified length and (optionally) adds a span to * Truncates a string to a specified length and (optionally) adds a
* provide a rollover to see the expanded text. * span to provide a rollover to see the expanded text.
* *
* @static * @static
* @param string $string string to truncate * @param string $string string to truncate
@ -242,8 +242,8 @@ class String
/** /**
* Upper Words * Upper Words
* *
* Applies strtolower() and ucwords() to the passed string. The exception * Applies strtolower() and ucwords() to the passed string. The
* being email addresses which are not formatted at all. * exception being email addresses which are not formatted at all.
* *
* @static * @static
* @param string $string string to format * @param string $string string to format

100
jar.php
View file

@ -7416,10 +7416,11 @@ class Security
* Session Class * Session Class
* *
* Provides session handling via database instead of the file based session * Provides session handling via database instead of the file based session
* handling built into PHP. Using this class requires an array to be defined * handling built into PHP. Using this class requires an array to be
* in place of the boolean true/false (on/off). If simply array(), the * defined in place of the boolean true/false (on/off). If simply array(),
* datasource will default to the value in $config['pickles']['datasource'] and * the datasource will default to the value in
* if the table will default to "sessions". The format is as follows: * $config['pickles']['datasource'] and if the table will default to
* "sessions". The format is as follows:
* *
* $config = array( * $config = array(
* 'pickles' => array( * 'pickles' => array(
@ -7430,8 +7431,8 @@ class Security
* ) * )
* ); * );
* *
* In addition to the configuration variables, a table in your database must * In addition to the configuration variables, a table in your database
* be created. The [MySQL] table schema is as follows: * must be created. The [MySQL] table schema is as follows:
* *
* CREATE TABLE sessions ( * CREATE TABLE sessions (
* id varchar(32) COLLATE utf8_unicode_ci NOT NULL, * id varchar(32) COLLATE utf8_unicode_ci NOT NULL,
@ -7441,10 +7442,10 @@ class Security
* INDEX (expires_at) * INDEX (expires_at)
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; * ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
* *
* Note: The reason for not using a model class was to avoid a naming conflict * Note: The reason for not using a model class was to avoid a naming
* between the Session model and the Session class itself. This will eventually * conflict between the Session model and the Session class itself. This
* be resolved when I abandon full 5.x support and migrate to 5.3+ (assuming * will eventually be resolved when I abandon full 5.x support and migrate
* that ever happens). * to 5.3+ (assuming that ever happens).
*/ */
class Session extends Object class Session extends Object
{ {
@ -7471,8 +7472,8 @@ class Session extends Object
/** /**
* Time to Live * Time to Live
* *
* The number of seconds the session should remain active. Corresponds to * The number of seconds the session should remain active. Corresponds
* the INI variable session.gc_maxlifetime * to the INI variable session.gc_maxlifetime
* *
* @access private * @access private
* @var integer * @var integer
@ -7493,8 +7494,8 @@ class Session extends Object
/** /**
* Table * Table
* *
* Name of the database table in the aforementioned datasource that holds * Name of the database table in the aforementioned datasource that
* the session data. The expected schema is defined above. * holds the session data. The expected schema is defined above.
* *
* @access private * @access private
* @var string * @var string
@ -7504,8 +7505,8 @@ class Session extends Object
/** /**
* Database * Database
* *
* Our database object to interact with the aforementioned datasource and * Our database object to interact with the aforementioned datasource
* table. This object is shared with other PICKLES internals. * and table. This object is shared with other PICKLES internals.
* *
* @access private * @access private
* @var object * @var object
@ -7515,10 +7516,11 @@ class Session extends Object
/** /**
* Constructor * Constructor
* *
* All of our set up logic for the session in contained here. This object * All of our set up logic for the session in contained here. This
* is initially instantiated from pickles.php and the session callbacks are * object is initially instantiated from pickles.php and the session
* established here. All variables are driven from php.ini and/or the site * callbacks are established here. All variables are driven from
* config. Once configured, the session is started automatically. * php.ini and/or the site config. Once configured, the session is
* started automatically.
*/ */
public function __construct() public function __construct()
{ {
@ -7575,7 +7577,6 @@ class Session extends Object
{ {
case 'files': case 'files':
ini_set('session.save_handler', 'files'); ini_set('session.save_handler', 'files');
session_start();
break; break;
case 'memcache': case 'memcache':
@ -7590,7 +7591,6 @@ class Session extends Object
ini_set('session.save_handler', 'memcache'); ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', 'tcp://' . $hostname . ':' . $port . '?persistent=1&weight=1&timeout=1&retry_interval=15'); ini_set('session.save_path', 'tcp://' . $hostname . ':' . $port . '?persistent=1&weight=1&timeout=1&retry_interval=15');
session_start();
break; break;
case 'mysql': case 'mysql':
@ -7608,8 +7608,6 @@ class Session extends Object
// Initializes the session // Initializes the session
$this->initialize(); $this->initialize();
session_start();
} }
else else
{ {
@ -7618,6 +7616,13 @@ class Session extends Object
break; break;
} }
if (isset($_SERVER['HTTP_USER_AGENT'])
&& !String::isEmpty($_SERVER['HTTP_USER_AGENT'])
&& !preg_match('/(Baidu|Gigabot|Googlebot|libwww-perl|lwp-trivial|msnbot|SiteUptime|Slurp|WordPress|ZIBB|ZyBorg)/i', $_SERVER['HTTP_USER_AGENT']))
{
session_start();
}
} }
} }
@ -7625,9 +7630,9 @@ class Session extends Object
* Destructor * Destructor
* *
* Runs garbage collection and closes the session. I'm not sure if the * Runs garbage collection and closes the session. I'm not sure if the
* garbage collection should stay as it could be accomplished via php.ini * garbage collection should stay as it could be accomplished via
* variables. The session_write_close() is present to combat a chicken * php.ini variables. The session_write_close() is present to combat a
* and egg scenario in earlier versions of PHP 5. * chicken and egg scenario in earlier versions of PHP 5.
*/ */
public function __destruct() public function __destruct()
{ {
@ -7642,8 +7647,8 @@ class Session extends Object
* Initializes the Session * Initializes the Session
* *
* This method exists to combat the fact that calling session_destroy() * This method exists to combat the fact that calling session_destroy()
* also clears out the save handler. Upon destorying a session this method * also clears out the save handler. Upon destorying a session this
* is called again so the save handler is all set. * method is called again so the save handler is all set.
*/ */
public function initialize() public function initialize()
{ {
@ -7664,8 +7669,8 @@ class Session extends Object
* Opens the Session * Opens the Session
* *
* Since the session is in the database, opens the database connection. * Since the session is in the database, opens the database connection.
* This step isn't really necessary as the Database object is smart enough * This step isn't really necessary as the Database object is smart
* to open itself up upon execute. * enough to open itself up upon execute.
*/ */
public function open() public function open()
{ {
@ -7704,7 +7709,8 @@ class Session extends Object
/** /**
* Writes the Session * Writes the Session
* *
* When there's changes to the session, writes the data to the database. * When there's changes to the session, writes the data to the
* database.
* *
* @param string $id session ID * @param string $id session ID
* @param string $session serialized session data * @param string $session serialized session data
@ -7770,8 +7776,8 @@ class Session extends Object
/** /**
* String Class * String Class
* *
* Just a simple collection of static functions to accomplish some of the more * Just a simple collection of static functions to accomplish some of the
* redundant string related manipulation. * more redundant string related manipulation.
*/ */
class String class String
{ {
@ -7821,9 +7827,9 @@ class String
/** /**
* Generate Slug * Generate Slug
* *
* Generates a slug from the pass string by lowercasing the string, trimming * Generates a slug from the pass string by lowercasing the string,
* whitespace and converting non-alphanumeric values to dashes. Takes care * trimming whitespace and converting non-alphanumeric values to
* of multiple dashes as well. * dashes. Takes care of multiple dashes as well.
* *
* @static * @static
* @param string $string to be converted to the slug * @param string $string to be converted to the slug
@ -7843,10 +7849,10 @@ class String
/** /**
* Is Empty * Is Empty
* *
* Checks if a string is empty. You can use the PHP function empty() but * Checks if a string is empty. You can use the PHP function empty()
* that returns true for a string of "0". Last I checked, that's not an * but that returns true for a string of "0". Last I checked, that's
* empty string. PHP's function also doesn't apply trim() to the value * not an empty string. PHP's function also doesn't apply trim() to the
* to ensure it's not just a bunch of spaces. * value to ensure it's not just a bunch of spaces.
* *
* @static * @static
* @param string $value string(s) to be checked * @param string $value string(s) to be checked
@ -7871,8 +7877,8 @@ class String
/** /**
* Pluralize * Pluralize
* *
* Based on a passed integer, the word will be pluralized. A value of zero * Based on a passed integer, the word will be pluralized. A value of
* will also pluralize the word (e.g. 0 things not 0 thing). * zero will also pluralize the word (e.g. 0 things not 0 thing).
* *
* @static * @static
* @param string $string the word to plurailze * @param string $string the word to plurailze
@ -7962,8 +7968,8 @@ class String
/** /**
* Truncate * Truncate
* *
* Truncates a string to a specified length and (optionally) adds a span to * Truncates a string to a specified length and (optionally) adds a
* provide a rollover to see the expanded text. * span to provide a rollover to see the expanded text.
* *
* @static * @static
* @param string $string string to truncate * @param string $string string to truncate
@ -7994,8 +8000,8 @@ class String
/** /**
* Upper Words * Upper Words
* *
* Applies strtolower() and ucwords() to the passed string. The exception * Applies strtolower() and ucwords() to the passed string. The
* being email addresses which are not formatted at all. * exception being email addresses which are not formatted at all.
* *
* @static * @static
* @param string $string string to format * @param string $string string to format