1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
<?php
declare(strict_types=1);
namespace SimpleSAML\Module\cron\Controller;
use SimpleSAML\Auth;
use SimpleSAML\Auth\AuthenticationFactory;
use SimpleSAML\Configuration;
use SimpleSAML\Error;
use SimpleSAML\HTTP\RunnableResponse;
use SimpleSAML\Logger;
use SimpleSAML\Module;
use SimpleSAML\Session;
use SimpleSAML\Utils;
use SimpleSAML\XHTML\Template;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
/**
* Controller class for the cron module.
*
* This class serves the different views available in the module.
*
* @package SimpleSAML\Module\cron
*/
class Cron
{
/** @var \SimpleSAML\Configuration */
protected $config;
/** @var \SimpleSAML\Configuration */
protected $cronconfig;
/** @var \SimpleSAML\Session */
protected $session;
/**
* Controller constructor.
*
* It initializes the global configuration and auth source configuration for the controllers implemented here.
*
* @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
* @param \SimpleSAML\Session $session The session to use by the controllers.
*
* @throws \Exception
*/
public function __construct(
Configuration $config,
Session $session
) {
$this->config = $config;
$this->cronconfig = Configuration::getConfig('module_cron.php');
$this->session = $session;
}
/**
* Show cron info.
*
* @return \SimpleSAML\XHTML\Template
* An HTML template or a redirection if we are not authenticated.
*/
public function info()
{
Utils\Auth::requireAdmin();
$key = $this->cronconfig->getValue('key', 'secret');
$tags = $this->cronconfig->getValue('allowed_tags');
$def = [
'weekly' => "22 0 * * 0",
'daily' => "02 0 * * *",
'hourly' => "01 * * * *",
'default' => "XXXXXXXXXX",
];
$urls = [];
if ($this->config->getBoolean('usenewui', false)) {
foreach ($tags as $tag) {
$urls[] = [
'exec_href' => Module::getModuleURL('cron') . '/run/' . $tag . '/' . $key,
'href' => Module::getModuleURL('cron') . '/run/' . $tag . '/' . $key . '/xhtml',
'tag' => $tag,
'int' => (array_key_exists($tag, $def) ? $def[$tag] : $def['default']),
];
}
} else {
// cron.php?key=secret&tag=hourly&output=xhtml
foreach ($tags as $tag) {
$urls[] = [
'exec_href' => Module::getModuleURL('cron/cron.php', ['key' => $key, 'tag' => $tag]),
'href' => Module::getModuleURL('cron/cron.php', ['key' => $key, 'tag' => $tag, 'output' => 'xhtml']),
'tag' => $tag,
'int' => (array_key_exists($tag, $def) ? $def[$tag] : $def['default']),
];
}
}
$t = new Template($this->config, 'cron:croninfo.tpl.php', 'cron:cron');
$t->data['urls'] = $urls;
return $t;
}
/**
* Execute a cronjob.
*
* This controller will start a cron operation
*
* @param string $tag The tag
* @param string $key The secret key
* @param string|null $output The output format, defaulting to xhtml
*
* @return \SimpleSAML\XHTML\Template|\Symfony\Component\HttpFoundation\Response
* An HTML template, a redirect or a "runnable" response.
*
* @throws \SimpleSAML\Error\Exception
*/
public function run($tag, $key, $output)
{
$configKey = $this->cronconfig->getValue('key', 'secret');
if ($key !== $configKey) {
Logger::error('Cron - Wrong key provided. Cron will not run.');
exit;
}
$cron = new \SimpleSAML\Module\cron\Cron();
if ($tag === null || !$cron->isValidTag($tag)) {
Logger::error('Cron - Illegal tag [' . $tag . '].');
exit;
}
$url = Utils\HTTP::getSelfURL();
$time = date(DATE_RFC822);
$croninfo = $cron->runTag($tag);
$summary = $croninfo['summary'];
if ($this->cronconfig->getValue('sendemail', true) && count($summary) > 0) {
$mail = new Utils\EMail('SimpleSAMLphp cron report');
$mail->setData(['url' => $url, 'tag' => $croninfo['tag'], 'summary' => $croninfo['summary']]);
try {
$mail->send();
} catch (\PHPMailer\PHPMailer\Exception $e) {
Logger::warning("Unable to send cron report; " . $e->getMessage());
}
}
if ($output === 'xhtml') {
$t = new Template($this->config, 'cron:croninfo-result.tpl.php', 'cron:cron');
$t->data['tag'] = $croninfo['tag'];
$t->data['time'] = $time;
$t->data['url'] = $url;
$t->data['mail_required'] = isset($mail);
$t->data['mail_sent'] = !isset($e);
$t->data['summary'] = $summary;
return $t;
}
return new Response();
}
}
|