Permalink
Browse files

Fixes a bug that assumed the wrong cookie prefix when building cookies.

  • Loading branch information...
1 parent 7fb7f98 commit 8a3d56593451497685b2cba5456d85098158750f @jimsafley jimsafley committed Oct 12, 2011
View
@@ -49,9 +49,6 @@ Here's a basic configuration:
// URL to the MediaWiki installation API.
define('MEDIAWIKI_API_URL', '');
- // Name of the MediaWiki database.
- define('MEDIAWIKI_DB_NAME', '');
-
// Set the include path to Zend and Scripto libraries.
set_include_path(get_include_path()
. PATH_SEPARATOR . ZEND_PATH
@@ -61,8 +58,7 @@ Here's a basic configuration:
require_once 'Scripto.php';
require_once 'Scripto/Adapter/Example.php';
$scripto = new Scripto(new Scripto_Adapter_Example,
- array('api_url' => MEDIAWIKI_API_URL,
- 'db_name' => MEDIAWIKI_DB_NAME));
+ array('api_url' => MEDIAWIKI_API_URL));
// Set the current document object.
$doc = $scripto->getDocument($_REQUEST['documentId']);
@@ -14,8 +14,7 @@
// Set the Document object.
require_once 'Scripto.php';
-$scripto = new Scripto($adapter, array('api_url' => MEDIAWIKI_API_URL,
- 'db_name' => MEDIAWIKI_DB_NAME));
+$scripto = new Scripto($adapter, array('api_url' => MEDIAWIKI_API_URL));
$doc = $scripto->getDocument($documentId);
// Must set the current page first.
@@ -14,8 +14,7 @@
// Set the Document object.
require_once 'Scripto.php';
-$scripto = new Scripto($adapter, array('api_url' => MEDIAWIKI_API_URL,
- 'db_name' => MEDIAWIKI_DB_NAME));
+$scripto = new Scripto($adapter, array('api_url' => MEDIAWIKI_API_URL));
$doc = $scripto->getDocument($documentId);
// Must set the current page first.
@@ -14,8 +14,7 @@
// Set the Document object.
require_once 'Scripto.php';
-$scripto = new Scripto($adapter, array('api_url' => MEDIAWIKI_API_URL,
- 'db_name' => MEDIAWIKI_DB_NAME));
+$scripto = new Scripto($adapter, array('api_url' => MEDIAWIKI_API_URL));
$doc = $scripto->getDocument($documentId);
// Must set the current page first.
@@ -10,9 +10,6 @@ define('SCRIPTO_PATH', '');
// URL to the MediaWiki installation API.
define('MEDIAWIKI_API_URL', '');
-// Name of the MediaWiki database.
-define('MEDIAWIKI_DB_NAME', '');
-
// Set the include path to Zend and Scripto libraries.
set_include_path(get_include_path()
. PATH_SEPARATOR . ZEND_PATH
View
@@ -51,7 +51,6 @@ class Scripto
* @param array|Scripto_Service_MediaWiki $mediawiki If an array:
* <ul>
* <li>$mediawiki['api_url']: required; the MediaWiki API URL</li>
- * <li>$mediawiki['db_name']: required; the MediaWiki database name</li>
* <li>$mediawiki['pass_cookies']: optional pass cookies to the web
* browser via API client</li>
* </ul>
@@ -64,15 +63,12 @@ public function __construct(Scripto_Adapter_Interface $adapter, $mediawiki)
// Set the MediaWiki service.
if ($mediawiki instanceof Scripto_Service_MediaWiki) {
$this->_mediawiki = $mediawiki;
- } else if (is_array($mediawiki)
- && array_key_exists('api_url', $mediawiki)
- && array_key_exists('db_name', $mediawiki)) {
+ } else if (is_array($mediawiki) && array_key_exists('api_url', $mediawiki)) {
if (!isset($mediawiki['pass_cookies'])) {
$mediawiki['pass_cookies'] = true;
}
$this->_mediawiki = new Scripto_Service_MediaWiki($mediawiki['api_url'],
- $mediawiki['db_name'],
(bool) $mediawiki['pass_cookies']);
} else {
throw new Scripto_Exception('The provided mediawiki parameter is invalid.');
@@ -29,15 +29,14 @@ class Scripto_Service_MediaWiki extends Zend_Service_Abstract
const COOKIE_PREFIX = 'scripto_';
/**
- * @var string The MediaWiki database name, used to namespace Scripto/
- * MediaWiki cookies.
+ * @var bool Pass Scripto cookies to the web browser.
*/
- protected $_dbName;
+ protected $_passCookies;
/**
- * @var bool Pass Scripto cookies to the web browser.
+ * @var string The cookie prefix set by MediaWiki during login.
*/
- protected $_passCookies;
+ protected $_mediawikiCookiePrefix;
/**
* @var array Scripto/MediaWiki cookie name suffixes.
@@ -104,28 +103,32 @@ class Scripto_Service_MediaWiki extends Zend_Service_Abstract
*
* @link http://www.mediawiki.org/wiki/API:Main_page
* @param string $apiUrl The URL to the MediaWiki API.
- * @param string $dbName The name of the MediaWiki database.
* @param bool $passCookies Pass cookies to the web browser.
*/
- public function __construct($apiUrl, $dbName, $passCookies = true)
+ public function __construct($apiUrl, $passCookies = true)
{
- $this->_dbName = (string) $dbName;
$this->_passCookies = (bool) $passCookies;
// Set the HTTP client for the MediaWiki API .
self::getHttpClient()->setUri($apiUrl)
->setConfig(array('keepalive' => true))
->setCookieJar();
- // If MediaWiki API authentication cookies are being passed, get them
- // from the browser and add them to the HTTP client cookie jar. Doing so
- // maintains state between browser requests.
- if ($this->_passCookies) {
+ // Set the cookie prefix that was set by MediaWiki during login.
+ if (isset($_COOKIE[self::COOKIE_PREFIX . 'cookieprefix'])) {
+ $this->_mediawikiCookiePrefix = $_COOKIE[self::COOKIE_PREFIX . 'cookieprefix'];
+ }
+
+ // If MediaWiki API authentication cookies are being passed and the
+ // MediaWiki cookieprefix is set, get the cookies from the browser and
+ // add them to the HTTP client cookie jar. Doing so maintains state
+ // between browser requests.
+ if ($this->_passCookies && $this->_mediawikiCookiePrefix) {
require_once 'Zend/Http/Cookie.php';
foreach ($this->_cookieSuffixes as $cookieSuffix) {
- $cookieName = self::COOKIE_PREFIX . $this->_dbName . $cookieSuffix;
+ $cookieName = self::COOKIE_PREFIX . $this->_mediawikiCookiePrefix . $cookieSuffix;
if (array_key_exists($cookieName, $_COOKIE)) {
- $cookie = new Zend_Http_Cookie($this->_dbName . $cookieSuffix,
+ $cookie = new Zend_Http_Cookie($this->_mediawikiCookiePrefix . $cookieSuffix,
$_COOKIE[$cookieName],
self::getHttpClient()->getUri()->getHost());
self::getHttpClient()->getCookieJar()->addCookie($cookie);
@@ -538,6 +541,14 @@ public function login($lgname, $lgpassword)
// Process a successful login.
if ('Success' == $response['login']['result']) {
if ($this->_passCookies) {
+
+ // Persist the MediaWiki cookie prefix in the browser. Set to
+ // expire in 30 days, the same as MediaWiki cookies.
+ setcookie(self::COOKIE_PREFIX . 'cookieprefix',
+ $response['login']['cookieprefix'],
+ time() + 60 * 60 * 24 * 30,
+ '/');
+
// Persist MediaWiki authentication cookies in the browser.
foreach (self::getHttpClient()->getCookieJar()->getAllCookies() as $cookie) {
setcookie(self::COOKIE_PREFIX . $cookie->getName(),
@@ -579,10 +590,11 @@ public function logout()
// Reset the cookie jar.
self::getHttpClient()->getCookieJar()->reset();
- if ($this->_passCookies) {
+ if ($this->_passCookies && $this->_mediawikiCookiePrefix) {
// Delete the MediaWiki authentication cookies from the browser.
+ setcookie(self::COOKIE_PREFIX . 'cookieprefix', false, 0, '/');
foreach ($this->_cookieSuffixes as $cookieSuffix) {
- $cookieName = self::COOKIE_PREFIX . $this->_dbName . $cookieSuffix;
+ $cookieName = self::COOKIE_PREFIX . $this->_mediawikiCookiePrefix . $cookieSuffix;
if (array_key_exists($cookieName, $_COOKIE)) {
setcookie($cookieName, false, 0, '/');
}

0 comments on commit 8a3d565

Please sign in to comment.