Replace "cron.php" with a controller

This commit is contained in:
Dmitriy Simushev 2014-06-03 13:34:08 +00:00
parent 457045d81a
commit 66a68d0f23
7 changed files with 85 additions and 86 deletions

View File

@ -1,53 +0,0 @@
<?php
/*
* Copyright 2005-2014 the original author or authors.
*
* 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.
*/
// Import namespaces and classes of the core
use Mibew\EventDispatcher;
use Mibew\Settings;
// Initialize libraries
require_once(dirname(__FILE__) . '/libs/init.php');
$cron_key = empty($_GET['cron_key']) ? '' : $_GET['cron_key'];
// Check cron security key
if ($cron_key != Settings::get('cron_key')) {
die();
}
// Determine use or not quiet mode
$quiet = isset($_GET['q']);
set_time_limit(0);
// Run cron jobs of the core
calculate_thread_statistics();
calculate_operator_statistics();
calculate_page_statistics();
// Trigger cron event
$dispatcher = EventDispatcher::getInstance();
$dispatcher->triggerEvent('cronRun');
// Update time of last cron run
Settings::set('_last_cron_run', time());
Settings::update();
if (!$quiet) {
// TODO: May be localize it
echo('All cron jobs done.');
}

View File

@ -0,0 +1,68 @@
<?php
/*
* Copyright 2005-2014 the original author or authors.
*
* 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.
*/
namespace Mibew\Controller;
use Mibew\EventDispatcher;
use Mibew\Settings;
use Symfony\Component\HttpFoundation\Request;
/**
* Contains actions which are related with cron.
*/
class CronController extends AbstractController
{
/**
* Runs the cron.
*
* @param Request $request Incoming request.
* @return string Rendered page content.
*/
public function runAction(Request $request)
{
$cron_key = $request->query->get('cron_key', '');
// Check cron security key
if ($cron_key != Settings::get('cron_key')) {
// Return an empty response
return '';
}
// Determine use or not quiet mode
$quiet = $request->query->has('q');
set_time_limit(0);
// Run cron jobs of the core
calculate_thread_statistics();
calculate_operator_statistics();
calculate_page_statistics();
// Trigger cron event
$dispatcher = EventDispatcher::getInstance();
$dispatcher->triggerEvent('cronRun');
// Update time of last cron run
Settings::set('_last_cron_run', time());
Settings::update();
if (!$quiet) {
// TODO: May be localize it
return 'All cron jobs done.';
}
}
}

View File

@ -23,6 +23,7 @@ use Mibew\Style\ChatStyle;
use Mibew\Style\InvitationStyle;
use Mibew\Style\PageStyle;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Contains actions which are related with common system settings.
@ -98,7 +99,11 @@ class CommonController extends AbstractController
$page['invitationStylePreviewPath'] = $this->generateUrl('style_preview', array('type' => 'invitation'));
$page['stored'] = $request->query->has('stored');
$page['enabletracking'] = Settings::get('enabletracking');
$page['cron_path'] = cron_get_uri($params['cron_key']);
$page['cron_path'] = $this->generateUrl(
'cron',
array('cron_key' => $params['cron_key']),
UrlGeneratorInterface::ABSOLUTE_URL
);
$page['title'] = getlocal('settings.title');
$page['menuid'] = 'settings';

View File

@ -20,6 +20,7 @@ namespace Mibew\Controller;
use Mibew\Http\Exception\BadRequestException;
use Mibew\Settings;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Display all statistics-related pages
@ -55,11 +56,16 @@ class StatisticsController extends AbstractController
$page['showbyagent'] = ($statistics_type == self::TYPE_BY_OPERATOR);
$page['showbypage'] = ($statistics_type == self::TYPE_BY_PAGE);
$cron_uri = $this->generateUrl(
'cron',
array('cron_key' => Settings::get('cron_key')),
UrlGeneratorInterface::ABSOLUTE_URL
);
$page['pageDescription'] = getlocal2(
'statistics.description.full',
array(
date_to_text(Settings::get('_last_cron_run')),
cron_get_uri(Settings::get('cron_key')),
$cron_uri,
)
);

View File

@ -1,30 +0,0 @@
<?php
/*
* Copyright 2005-2014 the original author or authors.
*
* 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.
*/
/**
* Generates cron URI
*
* @param string $cron_key Cron security key
* @return string Cron URI
*/
function cron_get_uri($cron_key)
{
$path = get_app_location(true, is_secure_request()) . '/cron.php';
$path .= empty($cron_key) ? '' : '?cron_key=' . $cron_key;
return $path;
}

View File

@ -99,7 +99,6 @@ if (!empty($plugins_list)) {
require_once(MIBEW_FS_ROOT . '/libs/canned.php');
require_once(MIBEW_FS_ROOT . '/libs/captcha.php');
require_once(MIBEW_FS_ROOT . '/libs/chat.php');
require_once(MIBEW_FS_ROOT . '/libs/cron.php');
require_once(MIBEW_FS_ROOT . '/libs/getcode.php');
require_once(MIBEW_FS_ROOT . '/libs/groups.php');
require_once(MIBEW_FS_ROOT . '/libs/invitation.php');

View File

@ -86,6 +86,10 @@ captcha:
path: /captcha
defaults: { _controller: Mibew\Controller\CaptchaController::drawAction }
cron:
path: /cron
defaults: { _controller: Mibew\Controller\CronController::runAction }
license:
path: /license
defaults: { _controller: Mibew\Controller\LicenseController::indexAction }