Merge pull request #1191 from TheSerapher/issue-1181

Issue 1181
This commit is contained in:
Sebastian Grewe 2014-01-02 02:08:19 -08:00
commit 5c5db8dc93
11 changed files with 124 additions and 12 deletions

View File

@ -10,7 +10,7 @@
PHP_BIN=$( which php )
# List of cruns to execute
CRONS="findblock.php proportional_payout.php pplns_payout.php pps_payout.php blockupdate.php payouts.php tickerupdate.php notifications.php statistics.php archive_cleanup.php"
CRONS="findblock.php proportional_payout.php pplns_payout.php pps_payout.php blockupdate.php payouts.php tickerupdate.php notifications.php statistics.php token_cleanup.php archive_cleanup.php"
# Output additional runtime information
VERBOSE="0"

View File

@ -10,7 +10,7 @@
PHP_BIN=$( which php )
# List of cruns to execute
CRONS="tickerupdate.php notifications.php archive_cleanup.php"
CRONS="tickerupdate.php notifications.php token_cleanup.php archive_cleanup.php"
# Output additional runtime information
VERBOSE="0"

39
cronjobs/token_cleanup.php Executable file
View File

@ -0,0 +1,39 @@
#!/usr/bin/php
<?php
/*
Copyright:: 2013, Sebastian Grewe
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Change to working directory
chdir(dirname(__FILE__));
// Include all settings and classes
require_once('shared.inc.php');
// Cleanup old expired tokens
if ($oToken->cleanupTokens()) {
$oToken->deleted == 0 ? $log->logDebug('Did not find any expired tokens') : $log->logInfo('Deleted ' . $oToken->deleted . ' expired tokens');
} else {
$log->logError('Failed to delete expired tokens: ' . $oToken->getCronError());
// Treat as critical since tokens like password resets will never expire
$monitoring->endCronjob($cron_name, 'E0074', 1, true, true);
}
// Cron cleanup and monitoring
require_once('cron_end.inc.php');
?>

View File

@ -116,17 +116,36 @@ class Base {
}
/**
* Get an element as an associated array
* Fetch all entries as an assoc array from a table
* This should, in general, not be used but sometimes it's just easier
* @param none
* @return array Assoc array of all rows found in table
**/
protected function getAllAssoc($value, $field='id', $type='i') {
public function getAllAssoc() {
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("SELECT * FROM $this->table");
if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result())
return $result->fetch_all(MYSQLI_ASSOC);
return $this->sqlError();
}
/**
* Get a single row as an assoc array
* @param value string Value to search for
* @param field string Column to search for
* @param type string Type of value
* @return array Resulting row
**/
protected function getSingleAssoc($value, $field='id', $type='i') {
$this->debug->append("STA " . __METHOD__, 4);
$stmt = $this->mysqli->prepare("SELECT * FROM $this->table WHERE $field = ? LIMIT 1");
if ($this->checkStmt($stmt) && $stmt->bind_param($type, $value) && $stmt->execute() && $result = $stmt->get_result())
return $result->fetch_assoc();
return false;
}
/**
* Get a single row from the table
* Get a single value from a row matching the query specified
* @param value string Value to search for
* @param search Return column to search for
* @param field string Search column

View File

@ -27,7 +27,7 @@ class Share Extends Base {
* @return array Share data
**/
public function getShareById($id) {
return $this->getAllAssoc($id);
return $this->getSingleAssoc($id);
}
/**

View File

@ -54,6 +54,35 @@ class Token Extends Base {
return true;
return $this->sqlError();
}
/**
* Cleanup token table of expired tokens
* @param none
* @return bool
**/
public function cleanupTokens() {
// Get all tokens that have an expiration set
if (!$aTokenTypes = $this->tokentype->getAllExpirations()) {
// Verbose error for crons since this should not happen
$this->setCronMessage('Failed to fetch tokens with expiration times: ' . $this->tokentype->getCronError());
return false;
}
$failed = $this->deleted = 0;
foreach ($aTokenTypes as $aTokenType) {
$stmt = $this->mysqli->prepare("DELETE FROM $this->table WHERE (NOW() - time) > ? AND type = ?");
if (! ($this->checkStmt($stmt) && $stmt->bind_param('ii', $aTokenType['expiration'], $aTokenType['id']) && $stmt->execute())) {
$failed++;
} else {
$this->deleted += $stmt->affected_rows;
}
}
if ($failed > 0) {
$this->setCronMessage('Failed to delete ' . $failed . ' token types from ' . $this->table . ' table');
return false;
}
return true;
}
}
$oToken = new Token();

View File

@ -15,6 +15,27 @@ class Token_Type Extends Base {
public function getTypeId($strName) {
return $this->getSingle($strName, 'id', 'name', 's');
}
/**
* Return expiration time for token type
* @param id int Token ID
* @param time int Time in seconds for expiration
**/
public function getExpiration($id) {
return $this->getSingle($id, 'expiration', 'id', 'i');
}
/**
* Fetch all tokens that have an expiration set
* @param none
* @return array Tokens with expiration times set
**/
public function getAllExpirations() {
$stmt = $this->mysqli->prepare("SELECT * FROM $this->table WHERE expiration > 0");
if ($this->checkStmt($stmt) && $stmt->execute() && $result = $stmt->get_result())
return $result->fetch_all(MYSQLI_ASSOC);
return $this->sqlError();
}
}
$tokentype = new Token_Type();

View File

@ -70,5 +70,6 @@ $aErrorCodes['E0063'] = 'Upstream share already assigned to previous block';
$aErrorCodes['E0064'] = 'Failed to create transaction record';
$aErrorCodes['E0065'] = 'Remaining balance is greater than 0';
$aErrorCodes['E0072'] = 'Worker names must be alphanumeric';
$aErrorCodes['E0073'] = 'Worker name is too long; try entering a shorter name'
$aErrorCodes['E0073'] = 'Worker name is too long; try entering a shorter name';
$aErrorCodes['E0074'] = 'Failed deleting expired tokens';
?>

View File

@ -10,7 +10,7 @@ if (!$user->isAuthenticated() || !$user->isAdmin($_SESSION['USERDATA']['id'])) {
}
// Default crons to monitor
$aCrons = array('statistics','payouts','archive_cleanup','blockupdate','findblock','notifications','tickerupdate');
$aCrons = array('statistics','payouts','token_cleanup','archive_cleanup','blockupdate','findblock','notifications','tickerupdate');
// Special cases, only add them if activated
switch ($config['payout_system']) {

View File

@ -189,14 +189,15 @@ CREATE TABLE IF NOT EXISTS `tokens` (
CREATE TABLE IF NOT EXISTS `token_types` (
`id` tinyint(4) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
`expiration` INT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `token_types` (`id`, `name`) VALUES
(1, 'password_reset'),
(2, 'confirm_email'),
(3, 'invitation');
INSERT INTO `token_types` (`id`, `name`, `expiration`) VALUES
(1, 'password_reset', 3600),
(2, 'confirm_email', 0),
(3, 'invitation', 0);
CREATE TABLE IF NOT EXISTS `transactions` (
`id` int(255) NOT NULL AUTO_INCREMENT,

View File

@ -0,0 +1,2 @@
ALTER TABLE `token_types` ADD `expiration` INT NULL DEFAULT '0';
UPDATE `token_types` SET `expiration` = 3600 WHERE `id` = 1;