2009-06-18 01:14:40 +02:00
|
|
|
<?php
|
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\Versioning\Versioned;
|
|
|
|
use SilverStripe\ORM\DatabaseAdmin;
|
|
|
|
|
2009-06-18 01:14:40 +02:00
|
|
|
/**
|
2014-08-06 06:29:52 +02:00
|
|
|
* Base class for development tools.
|
2016-03-08 21:50:18 +01:00
|
|
|
*
|
2014-08-06 06:29:52 +02:00
|
|
|
* Configured in framework/_config/dev.yml, with the config key registeredControllers being
|
|
|
|
* used to generate the list of links for /dev.
|
2009-06-18 01:14:40 +02:00
|
|
|
*
|
|
|
|
* @todo documentation for how to add new unit tests and tasks
|
2014-08-06 06:29:52 +02:00
|
|
|
* @todo do we need buildDefaults and generatesecuretoken? if so, register in the list
|
|
|
|
* @todo cleanup errors() it's not even an allowed action, so can go
|
|
|
|
* @todo cleanup index() html building
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-06-18 01:14:40 +02:00
|
|
|
* @subpackage dev
|
|
|
|
*/
|
|
|
|
class DevelopmentAdmin extends Controller {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $url_handlers = array(
|
2009-06-18 01:14:40 +02:00
|
|
|
'' => 'index',
|
2010-10-15 01:57:02 +02:00
|
|
|
'build/defaults' => 'buildDefaults',
|
2014-08-06 06:29:52 +02:00
|
|
|
'generatesecuretoken' => 'generatesecuretoken',
|
|
|
|
'$Action' => 'runRegisteredController',
|
2009-06-18 01:14:40 +02:00
|
|
|
);
|
2016-03-08 21:50:18 +01:00
|
|
|
|
|
|
|
private static $allowed_actions = array(
|
|
|
|
'index',
|
2013-11-07 23:09:46 +01:00
|
|
|
'buildDefaults',
|
2014-08-06 06:29:52 +02:00
|
|
|
'runRegisteredController',
|
|
|
|
'generatesecuretoken',
|
2012-12-08 12:20:20 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-02-23 14:46:00 +01:00
|
|
|
protected function init() {
|
2009-06-18 01:14:40 +02:00
|
|
|
parent::init();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:43:30 +02:00
|
|
|
// Special case for dev/build: Defer permission checks to DatabaseAdmin->init() (see #4957)
|
2016-02-17 05:30:51 +01:00
|
|
|
$requestedDevBuild = (stripos($this->getRequest()->getURL(), 'dev/build') === 0)
|
|
|
|
&& (stripos($this->getRequest()->getURL(), 'dev/build/defaults') === false);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-18 01:14:40 +02:00
|
|
|
// We allow access to this controller regardless of live-status or ADMIN permission only
|
|
|
|
// if on CLI. Access to this controller is always allowed in "dev-mode", or of the user is ADMIN.
|
2010-10-15 05:43:30 +02:00
|
|
|
$canAccess = (
|
2014-08-15 08:53:05 +02:00
|
|
|
$requestedDevBuild
|
|
|
|
|| Director::isDev()
|
|
|
|
|| Director::is_cli()
|
2010-10-15 05:43:30 +02:00
|
|
|
// Its important that we don't run this check if dev/build was requested
|
|
|
|
|| Permission::check("ADMIN")
|
|
|
|
);
|
|
|
|
if(!$canAccess) return Security::permissionFailure($this);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-18 01:14:40 +02:00
|
|
|
// check for valid url mapping
|
|
|
|
// lacking this information can cause really nasty bugs,
|
|
|
|
// e.g. when running Director::test() from a FunctionalTest instance
|
|
|
|
global $_FILE_TO_URL_MAPPING;
|
|
|
|
if(Director::is_cli()) {
|
|
|
|
if(isset($_FILE_TO_URL_MAPPING)) {
|
2010-04-13 01:15:04 +02:00
|
|
|
$fullPath = $testPath = BASE_PATH;
|
2009-06-26 04:46:14 +02:00
|
|
|
while($testPath && $testPath != "/" && !preg_match('/^[A-Z]:\\\\$/', $testPath)) {
|
2009-06-18 01:14:40 +02:00
|
|
|
$matched = false;
|
|
|
|
if(isset($_FILE_TO_URL_MAPPING[$testPath])) {
|
|
|
|
$matched = true;
|
2012-12-08 12:20:20 +01:00
|
|
|
break;
|
2009-06-18 01:14:40 +02:00
|
|
|
}
|
|
|
|
$testPath = dirname($testPath);
|
|
|
|
}
|
|
|
|
if(!$matched) {
|
|
|
|
echo 'Warning: You probably want to define '.
|
|
|
|
'an entry in $_FILE_TO_URL_MAPPING that covers "' . Director::baseFolder() . '"' . "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
echo 'Warning: You probably want to define $_FILE_TO_URL_MAPPING in '.
|
2012-09-26 23:34:00 +02:00
|
|
|
'your _ss_environment.php as instructed on the "sake" page of the doc.silverstripe.org wiki'."\n";
|
2009-06-18 01:14:40 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-01 11:50:49 +01:00
|
|
|
// Backwards compat: Default to "draft" stage, which is important
|
|
|
|
// for tasks like dev/build which call DataObject->requireDefaultRecords(),
|
|
|
|
// but also for other administrative tasks which have assumptions about the default stage.
|
2016-03-02 06:18:10 +01:00
|
|
|
Versioned::set_stage(Versioned::DRAFT);
|
2009-06-18 01:14:40 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function index() {
|
2009-06-18 01:14:40 +02:00
|
|
|
// Web mode
|
|
|
|
if(!Director::is_cli()) {
|
2012-04-04 16:59:30 +02:00
|
|
|
$renderer = DebugView::create();
|
2009-06-18 01:14:40 +02:00
|
|
|
$renderer->writeHeader();
|
2012-03-24 04:38:57 +01:00
|
|
|
$renderer->writeInfo("SilverStripe Development Tools", Director::absoluteBaseURL());
|
2009-06-18 01:14:40 +02:00
|
|
|
$base = Director::baseURL();
|
|
|
|
|
|
|
|
echo '<div class="options"><ul>';
|
2012-06-29 06:52:29 +02:00
|
|
|
$evenOdd = "odd";
|
2014-08-06 06:29:52 +02:00
|
|
|
foreach(self::get_links() as $action => $description) {
|
2012-09-26 23:34:00 +02:00
|
|
|
echo "<li class=\"$evenOdd\"><a href=\"{$base}dev/$action\"><b>/dev/$action:</b>"
|
|
|
|
. " $description</a></li>\n";
|
2012-06-29 06:52:29 +02:00
|
|
|
$evenOdd = ($evenOdd == "odd") ? "even" : "odd";
|
2009-06-18 01:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$renderer->writeFooter();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-18 01:14:40 +02:00
|
|
|
// CLI mode
|
|
|
|
} else {
|
2012-03-24 04:38:57 +01:00
|
|
|
echo "SILVERSTRIPE DEVELOPMENT TOOLS\n--------------------------\n\n";
|
2009-06-18 01:14:40 +02:00
|
|
|
echo "You can execute any of the following commands:\n\n";
|
2014-08-06 06:29:52 +02:00
|
|
|
foreach(self::get_links() as $action => $description) {
|
2009-06-18 01:14:40 +02:00
|
|
|
echo " sake dev/$action: $description\n";
|
|
|
|
}
|
|
|
|
echo "\n\n";
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-08-06 06:29:52 +02:00
|
|
|
public function runRegisteredController(SS_HTTPRequest $request){
|
|
|
|
$controllerClass = null;
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2014-08-06 06:29:52 +02:00
|
|
|
$baseUrlPart = $request->param('Action');
|
|
|
|
$reg = Config::inst()->get(__CLASS__, 'registered_controllers');
|
|
|
|
if(isset($reg[$baseUrlPart])){
|
|
|
|
$controllerClass = $reg[$baseUrlPart]['controller'];
|
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2014-08-06 06:29:52 +02:00
|
|
|
if($controllerClass && class_exists($controllerClass)){
|
|
|
|
return $controllerClass::create();
|
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2014-08-06 06:29:52 +02:00
|
|
|
$msg = 'Error: no controller registered in '.__CLASS__.' for: '.$request->param('Action');
|
|
|
|
if(Director::is_cli()){
|
|
|
|
// in CLI we cant use httpError because of a bug with stuff being in the output already, see DevAdminControllerTest
|
|
|
|
throw new Exception($msg);
|
|
|
|
}else{
|
|
|
|
$this->httpError(500, $msg);
|
|
|
|
}
|
2009-06-18 01:14:40 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-03-08 21:50:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-06 06:29:52 +02:00
|
|
|
/*
|
|
|
|
* Internal methods
|
|
|
|
*/
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-08-06 06:29:52 +02:00
|
|
|
/**
|
|
|
|
* @return array of url => description
|
|
|
|
*/
|
|
|
|
protected static function get_links(){
|
|
|
|
$links = array();
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2014-08-06 06:29:52 +02:00
|
|
|
$reg = Config::inst()->get(__CLASS__, 'registered_controllers');
|
|
|
|
foreach($reg as $registeredController){
|
|
|
|
foreach($registeredController['links'] as $url => $desc){
|
|
|
|
$links[$url] = $desc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $links;
|
2009-06-18 01:14:40 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-08-06 06:29:52 +02:00
|
|
|
protected function getRegisteredController($baseUrlPart){
|
|
|
|
$reg = Config::inst()->get(__CLASS__, 'registered_controllers');
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2014-08-06 06:29:52 +02:00
|
|
|
if(isset($reg[$baseUrlPart])){
|
|
|
|
$controllerClass = $reg[$baseUrlPart]['controller'];
|
|
|
|
return $controllerClass;
|
2009-06-18 01:14:40 +02:00
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2014-08-06 06:29:52 +02:00
|
|
|
return null;
|
2009-06-18 01:14:40 +02:00
|
|
|
}
|
2016-03-08 21:50:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-06 06:29:52 +02:00
|
|
|
/*
|
|
|
|
* Unregistered (hidden) actions
|
|
|
|
*/
|
2010-10-15 01:57:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build the default data, calling requireDefaultRecords on all
|
|
|
|
* DataObject classes
|
|
|
|
* Should match the $url_handlers rule:
|
|
|
|
* 'build/defaults' => 'buildDefaults',
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function buildDefaults() {
|
2012-04-04 16:59:30 +02:00
|
|
|
$da = DatabaseAdmin::create();
|
2010-10-15 01:57:02 +02:00
|
|
|
|
|
|
|
if (!Director::is_cli()) {
|
2012-04-04 16:59:30 +02:00
|
|
|
$renderer = DebugView::create();
|
2010-10-15 01:57:02 +02:00
|
|
|
$renderer->writeHeader();
|
|
|
|
$renderer->writeInfo("Defaults Builder", Director::absoluteBaseURL());
|
|
|
|
echo "<div style=\"margin: 0 2em\">";
|
|
|
|
}
|
|
|
|
|
|
|
|
$da->buildDefaults();
|
|
|
|
|
|
|
|
if (!Director::is_cli()) {
|
|
|
|
echo "</div>";
|
|
|
|
$renderer->writeFooter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-06 16:25:45 +01:00
|
|
|
/**
|
|
|
|
* Generate a secure token which can be used as a crypto key.
|
|
|
|
* Returns the token and suggests PHP configuration to set it.
|
|
|
|
*/
|
|
|
|
public function generatesecuretoken() {
|
|
|
|
$generator = Injector::inst()->create('RandomGenerator');
|
|
|
|
$token = $generator->randomToken('sha1');
|
2014-05-02 01:52:47 +02:00
|
|
|
$body = <<<TXT
|
|
|
|
Generated new token. Please add the following code to your YAML configuration:
|
2012-12-06 16:25:45 +01:00
|
|
|
|
2014-05-02 01:52:47 +02:00
|
|
|
Security:
|
|
|
|
token: $token
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-05-02 01:52:47 +02:00
|
|
|
TXT;
|
|
|
|
$response = new SS_HTTPResponse($body);
|
|
|
|
return $response->addHeader('Content-Type', 'text/plain');
|
2012-12-06 16:25:45 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function errors() {
|
2012-05-23 11:50:02 +02:00
|
|
|
$this->redirect("Debug_");
|
2009-06-18 01:14:40 +02:00
|
|
|
}
|
|
|
|
}
|