00001 #!/usr/bin/php -q
00002 <?php
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 define ('DS', DIRECTORY_SEPARATOR);
00034 if (function_exists('ini_set')) {
00035 ini_set('display_errors', '1');
00036 ini_set('error_reporting', '7');
00037 ini_set('max_execution_time',0);
00038 }
00039
00040 $app = 'app';
00041 $core = null;
00042 $root = dirname(dirname(dirname(__FILE__)));
00043 $here = $argv[0];
00044 $dataSource = 'default';
00045 $unset = array();
00046 for ($i = 1; $i < count($argv); $i++) {
00047
00048 switch (strtolower($argv[$i])) {
00049 case '-app':
00050 $app = $argv[$i + 1];
00051 $unset[$i] = $argv[$i];
00052 $unset[$i + 1] = $argv[$i + 1];
00053 break;
00054 case '-core':
00055 $core = $argv[$i + 1];
00056 $unset[$i] = $argv[$i];
00057 $unset[$i + 1] = $argv[$i + 1];
00058 break;
00059 case '-root':
00060 $root = $argv[$i + 1];
00061 $unset[$i] = $argv[$i];
00062 $unset[$i + 1] = $argv[$i + 1];
00063 break;
00064 case '-datasource':
00065 $dataSource = $argv[$i + 1];
00066 $unset[$i] = $argv[$i];
00067 $unset[$i + 1] = $argv[$i + 1];
00068 break;
00069 }
00070 }
00071
00072 if (strlen($app) && $app[0] == DS) {
00073 $cnt = substr_count($root, DS);
00074 $app = str_repeat('..' . DS, $cnt) . $app;
00075 }
00076 define ('ROOT', $root.DS);
00077 define ('APP_DIR', $app);
00078 define ('DEBUG', 1);;
00079 define('CAKE_CORE_INCLUDE_PATH', ROOT);
00080 define('DATASOURCE', $dataSource);
00081
00082 if (function_exists('ini_set')) {
00083 ini_set('include_path',ini_get('include_path').
00084 PATH_SEPARATOR.CAKE_CORE_INCLUDE_PATH.DS.
00085 PATH_SEPARATOR.CORE_PATH.DS.
00086 PATH_SEPARATOR.ROOT.DS.APP_DIR.DS.
00087 PATH_SEPARATOR.APP_DIR.DS.
00088 PATH_SEPARATOR.APP_PATH);
00089 define('APP_PATH', null);
00090 define('CORE_PATH', null);
00091 } else {
00092 define('APP_PATH', ROOT . DS . APP_DIR . DS);
00093 define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
00094 }
00095
00096 require ('cake'.DS.'basics.php');
00097 require ('cake'.DS.'config'.DS.'paths.php');
00098 require (CONFIGS.'core.php');
00099 uses ('object', 'configure', 'neat_array', 'session', 'security', 'inflector', 'model'.DS.'connection_manager',
00100 'model'.DS.'datasources'.DS.'dbo_source', 'model'.DS.'model');
00101 require(CAKE.'app_model.php');
00102 uses ('controller'.DS.'components'.DS.'acl', 'controller'.DS.'components'.DS.'dbacl'.DS.'models'.DS.'aclnode',
00103 'controller'.DS.'components'.DS.'dbacl'.DS.'models'.DS.'aco', 'controller'.DS.'components'.DS.'dbacl'.DS.'models'.DS.'acoaction',
00104 'controller'.DS.'components'.DS.'dbacl'.DS.'models'.DS.'aro');
00105
00106 $serverArgs = $argv;
00107 if (!empty($unset)) {
00108 $serverArgs = array_values(array_diff($argv, $unset));
00109 }
00110
00111 $wasted = array_shift($serverArgs);
00112 $command = array_shift($serverArgs);
00113 $args = $serverArgs;
00114 $aclCLI = new AclCLI ($command, $args);
00115
00116
00117
00118
00119 class AclCLI {
00120
00121
00122
00123
00124
00125 var $stdin;
00126
00127
00128
00129
00130
00131 var $stdout;
00132
00133
00134
00135
00136
00137 var $stderr;
00138
00139
00140
00141
00142
00143 var $acl;
00144
00145
00146
00147
00148
00149 var $args;
00150
00151
00152
00153
00154
00155 var $dataSource = 'default';
00156
00157
00158
00159
00160
00161
00162
00163 function AclCLI($command, $args) {
00164 $this->__construct($command, $args);
00165 }
00166
00167
00168
00169
00170
00171
00172 function __construct ($command, $args) {
00173 $this->stdin = fopen('php:
00174 $this->stdout = fopen('php:
00175 $this->stderr = fopen('php:
00176
00177 if (ACL_CLASSNAME != 'DB_ACL') {
00178 $out = "--------------------------------------------------\n";
00179 $out .= "Error: Your current Cake configuration is set to \n";
00180 $out .= "an ACL implementation other than DB. Please change \n";
00181 $out .= "your core config to reflect your decision to use \n";
00182 $out .= "DB_ACL before attempting to use this script.\n";
00183 $out .= "--------------------------------------------------\n";
00184 $out .= "Current ACL Classname: " . ACL_CLASSNAME . "\n";
00185 $out .= "--------------------------------------------------\n";
00186 fwrite($this->stderr, $out);
00187 exit();
00188 }
00189
00190 if (!in_array($command, array('help'))) {
00191 if (!file_exists(CONFIGS.'database.php')) {
00192 $this->stdout('');
00193 $this->stdout('Your database configuration was not found.');
00194 $this->stdout('Take a moment to create one:');
00195 $this->doDbConfig();
00196 }
00197 require_once (CONFIGS.'database.php');
00198
00199 if (!in_array($command, array('initdb'))) {
00200 $this->dataSource = DATASOURCE;
00201 $this->Acl = new AclComponent();
00202 $this->args = $args;
00203 $this->db =& ConnectionManager::getDataSource($this->dataSource);
00204 }
00205 }
00206
00207 switch ($command) {
00208 case 'create':
00209 $this->create();
00210 break;
00211 case 'delete':
00212 $this->delete();
00213 break;
00214 case 'setParent':
00215 $this->setParent();
00216 break;
00217 case 'getPath':
00218 $this->getPath();
00219 break;
00220 case 'grant':
00221 $this->grant();
00222 break;
00223 case 'deny':
00224 $this->deny();
00225 break;
00226 case 'inherit':
00227 $this->inherit();
00228 break;
00229 case 'view':
00230 $this->view();
00231 break;
00232 case 'initdb':
00233 $this->initdb();
00234 break;
00235 case 'upgrade':
00236 $this->upgradedb();
00237 break;
00238 case 'help':
00239 $this->help();
00240 break;
00241 default:
00242 fwrite($this->stderr, "Unknown ACL command '$command'.\nFor usage, try 'php acl.php help'.\n\n");
00243 break;
00244 }
00245 }
00246
00247
00248
00249
00250 function create() {
00251 $this->checkArgNumber(4, 'create');
00252 $this->checkNodeType();
00253 extract($this->__dataVars());
00254
00255 $parent = (is_numeric($this->args[2])) ? intval($this->args[2]) : $this->args[2];
00256 if (!$this->Acl->{$class}->create(intval($this->args[1]), $parent, $this->args[3])) {
00257 $this->displayError("Parent Node Not Found", "There was an error creating the ".$class.", probably couldn't find the parent node.\n If you wish to create a new root node, specify the <parent_id> as '0'.");
00258 }
00259 $this->stdout("New $class '".$this->args[3]."' created.\n\n");
00260 }
00261
00262
00263
00264
00265 function delete() {
00266 $this->checkArgNumber(2, 'delete');
00267 $this->checkNodeType();
00268 extract($this->__dataVars());
00269 if (!$this->Acl->{$class}->delete($this->args[1])) {
00270 $this->displayError("Node Not Deleted", "There was an error deleting the ".$class.". Check that the node exists.\n");
00271 }
00272 $this->stdout("{$class} deleted.\n\n");
00273 }
00274
00275
00276
00277
00278
00279 function setParent() {
00280 $this->checkArgNumber(3, 'setParent');
00281 $this->checkNodeType();
00282 extract($this->__dataVars());
00283 if (!$this->Acl->{$class}->setParent($this->args[2], $this->args[1])) {
00284 $this->stdout("Error in setting new parent. Please make sure the parent node exists, and is not a descendant of the node specified.\n");
00285 } else {
00286 $this->stdout("Node parent set to ".$this->args[2]."\n\n");
00287 }
00288 }
00289
00290
00291
00292
00293 function getPath() {
00294 $this->checkArgNumber(2, 'getPath');
00295 $this->checkNodeType();
00296 extract($this->__dataVars());
00297 $id = (is_numeric($this->args[2])) ? intval($this->args[1]) : $this->args[1];
00298 $nodes = $this->Acl->{$class}->getPath($id);
00299 if (empty($nodes)) {
00300 $this->displayError("Supplied Node '".$this->args[1]."' not found", "No tree returned.");
00301 }
00302 for ($i = 0; $i < count($nodes); $i++) {
00303 $this->stdout(str_repeat(' ', $i) . "[" . $nodes[$i][$class]['id'] . "]" . $nodes[$i][$class]['alias'] . "\n");
00304 }
00305 }
00306
00307
00308
00309
00310 function grant() {
00311 $this->checkArgNumber(3, 'grant');
00312
00313 $aro = $this->args[0];
00314 if (is_numeric($aro)) {
00315 $aro = intval($aro);
00316 }
00317 $aco = $this->args[1];
00318 if (is_numeric($aco)) {
00319 $aco = intval($aco);
00320 }
00321 if ($this->Acl->allow($aro, $aco, $this->args[2])) {
00322 $this->stdout("Permission granted.\n");
00323 } else {
00324 $this->stdout("Permission could not be granted.\n");
00325 }
00326 }
00327
00328
00329
00330
00331 function deny() {
00332 $this->checkArgNumber(3, 'deny');
00333
00334 $aro = (is_numeric($this->args[0])) ? intval($this->args[0]) : $this->args[0];
00335 $aco = (is_numeric($this->args[1])) ? intval($this->args[1]) : $this->args[1];
00336 $aro = $this->args[0];
00337 if (is_numeric($aro)) {
00338 $aro = intval($aro);
00339 }
00340 $aco = $this->args[1];
00341 if (is_numeric($aco)) {
00342 $aco = intval($aco);
00343 }
00344 if ($this->Acl->deny($aro, $aco, $this->args[2])) {
00345 $this->stdout("Permission denied.\n");
00346 } else {
00347 $this->stdout("Permission could not be denied.\n");
00348 }
00349 }
00350
00351
00352
00353
00354 function inherit() {
00355 $this->checkArgNumber(3, 'inherit');
00356 $aro = $this->args[0];
00357 if (is_numeric($aro)) {
00358 $aro = intval($aro);
00359 }
00360 $aco = $this->args[1];
00361 if (is_numeric($aco)) {
00362 $aco = intval($aco);
00363 }
00364 if ($this->Acl->inherit($aro, $aco, $this->args[2])) {
00365 $this->stdout("Permission inherited.\n");
00366 } else {
00367 $this->stdout("Permission could not be inherited.\n");
00368 }
00369 }
00370
00371
00372
00373
00374 function view() {
00375 $this->checkArgNumber(1, 'view');
00376 $this->checkNodeType();
00377 extract($this->__dataVars());
00378 if (!is_null($this->args[1])) {
00379 $conditions = $this->Acl->{$class}->_resolveID($this->args[1]);
00380 } else {
00381 $conditions = null;
00382 }
00383 $nodes = $this->Acl->{$class}->findAll($conditions, null, 'lft ASC');
00384 if (empty($nodes)) {
00385 $this->displayError($this->args[1]." not found", "No tree returned.");
00386 }
00387 $right = array();
00388
00389 $this->stdout($class . " tree:\n");
00390 $this->stdout("------------------------------------------------\n");
00391
00392 for ($i = 0; $i < count($nodes); $i++) {
00393 if (count($right) > 0) {
00394 while ($right[count($right)-1] < $nodes[$i][$class]['rght']) {
00395 if ($right[count($right)-1]) {
00396 array_pop($right);
00397 } else {
00398 break;
00399 }
00400 }
00401 }
00402 $this->stdout(str_repeat(' ',count($right)) . "[" . $nodes[$i][$class]['id'] . "]" . $nodes[$i][$class]['alias']."\n");
00403 $right[] = $nodes[$i][$class]['rght'];
00404 }
00405 $this->stdout("------------------------------------------------\n");
00406 }
00407
00408
00409
00410
00411 function initdb() {
00412 $db =& ConnectionManager::getDataSource($this->dataSource);
00413 $this->stdout("Initializing Database...\n");
00414 $this->stdout("Creating access control objects table (acos)...\n");
00415 $sql = " CREATE TABLE ".$db->fullTableName('acos')." (
00416 ".$db->name('id')." ".$db->column($db->columns['primary_key']).",
00417 ".$db->name('object_id')." ".$db->column($db->columns['integer'])." default NULL,
00418 ".$db->name('alias')." ".$db->column($db->columns['string'])." NOT NULL default '',
00419 ".$db->name('lft')." ".$db->column($db->columns['integer'])." default NULL,
00420 ".$db->name('rght')." ".$db->column($db->columns['integer'])." default NULL,
00421 PRIMARY KEY (".$db->name('id').")
00422 );";
00423 if ($db->query($sql) === false) {
00424 die("Error: " . $db->lastError() . "\n\n");
00425 }
00426
00427 $this->stdout("Creating access request objects table (aros)...\n");
00428 $sql2 = "CREATE TABLE ".$db->fullTableName('aros')." (
00429 ".$db->name('id')." ".$db->column($db->columns['primary_key']).",
00430 ".$db->name('foreign_key')." ".$db->column($db->columns['integer'])." default NULL,
00431 ".$db->name('alias')." ".$db->column($db->columns['string'])." NOT NULL default '',
00432 ".$db->name('lft')." ".$db->column($db->columns['integer'])." default NULL,
00433 ".$db->name('rght')." ".$db->column($db->columns['integer'])." default NULL,
00434 PRIMARY KEY (".$db->name('id').")
00435 );";
00436 if ($db->query($sql2) === false) {
00437 die("Error: " . $db->lastError() . "\n\n");
00438 }
00439
00440 $this->stdout("Creating relationships table (aros_acos)...\n");
00441 $sql3 = "CREATE TABLE ".$db->fullTableName('aros_acos')." (
00442 ".$db->name('id')." ".$db->column($db->columns['primary_key']).",
00443 ".$db->name('aro_id')." ".$db->column($db->columns['integer'])." default NULL,
00444 ".$db->name('aco_id')." ".$db->column($db->columns['integer'])." default NULL,
00445 ".$db->name('_create')." ".$db->column($db->columns['integer'])." NOT NULL default '0',
00446 ".$db->name('_read')." ".$db->column($db->columns['integer'])." NOT NULL default '0',
00447 ".$db->name('_update')." ".$db->column($db->columns['integer'])." NOT NULL default '0',
00448 ".$db->name('_delete')." ".$db->column($db->columns['integer'])." NOT NULL default '0',
00449 PRIMARY KEY (".$db->name('id').")
00450 );";
00451 if ($db->query($sql3) === false) {
00452 die("Error: " . $db->lastError() . "\n\n");
00453 }
00454
00455 $this->stdout("\nDone.\n");
00456 }
00457
00458
00459
00460
00461
00462 function upgradedb() {
00463 $db =& ConnectionManager::getDataSource($this->dataSource);
00464 $this->stdout("Initializing Database...\n");
00465 $this->stdout("Upgrading table (aros)...\n");
00466 $sql = "ALTER TABLE ".$db->fullTableName('aros')."
00467 CHANGE ".$db->name('user_id')."
00468 ".$db->name('foreign_key')."
00469 INT( 10 ) UNSIGNED NULL DEFAULT NULL;";
00470 $sql .= "ALTER TABLE " . $db->name('aros_acos') . " CHANGE " . $db->name('_create')
00471 . " " . $db->name('_create') . " CHAR(2) NOT NULL DEFAULT '0';";
00472 $sql .= "ALTER TABLE " . $db->name('aros_acos') . " CHANGE " . $db->name('_update')
00473 . " " . $db->name('_update') . " CHAR(2) NOT NULL DEFAULT '0';";
00474 $sql .= "ALTER TABLE " . $db->name('aros_acos') . " CHANGE " . $db->name('_read')
00475 . " " . $db->name('_read') . " CHAR(2) NOT NULL DEFAULT '0';";
00476 $sql .= "ALTER TABLE " . $db->name('aros_acos') . " CHANGE " . $db->name('_delete')
00477 . " " . $db->name('_delete') . " CHAR(2) NOT NULL DEFAULT '0';";
00478 if ($db->query($sql) === false) {
00479 die("Error: " . $db->lastError() . "\n\n");
00480 }
00481 $this->stdout("\nDatabase upgrade is complete.\n");
00482 }
00483
00484
00485
00486
00487
00488 function help() {
00489 $out = "Usage: php acl.php <command> <arg1> <arg2>...\n";
00490 $out .= "-----------------------------------------------\n";
00491 $out .= "Commands:\n";
00492 $out .= "\n";
00493 $out .= "\tcreate aro|aco <link_id> <parent_id> <alias>\n";
00494 $out .= "\t\tCreates a new ACL object under the parent specified by <parent_id>, an id/alias (see\n";
00495 $out .= "\t\t'view'). The link_id allows you to link a user object to Cake's\n";
00496 $out .= "\t\tACL structures. The alias parameter allows you to address your object\n";
00497 $out .= "\t\tusing a non-integer ID. Example: \"\$php acl.php create aro 57 0 John\"\n";
00498 $out .= "\t\twould create a new ARO object at the root of the tree, linked to 57\n";
00499 $out .= "\t\tin your users table, with an internal alias 'John'.";
00500 $out .= "\n";
00501 $out .= "\n";
00502 $out .= "\tdelete aro|aco <id>\n";
00503 $out .= "\t\tDeletes the ACL object with the specified ID (see 'view').\n";
00504 $out .= "\n";
00505 $out .= "\n";
00506 $out .= "\tsetParent aro|aco <id> <parent_id>\n";
00507 $out .= "\t\tUsed to set the parent of the ACL object specified by <id> to the ID\n";
00508 $out .= "\t\tspecified by <parent_id>.\n";
00509 $out .= "\n";
00510 $out .= "\n";
00511 $out .= "\tgetPath aro|aco <id>\n";
00512 $out .= "\t\tReturns the path to the ACL object specified by <id>. This command is\n";
00513 $out .= "\t\tis useful in determining the inhertiance of permissions for a certain\n";
00514 $out .= "\t\tobject in the tree.\n";
00515 $out .= "\n";
00516 $out .= "\n";
00517 $out .= "\tgrant <Aro.alias|Aro.foreign_key> <Aco.alias|Aco.object_id> <aco_action>\n";
00518 $out .= "\t\tUse this command to grant ACL permissions. Once executed, the ARO\n";
00519 $out .= "\t\tspecified (and its children, if any) will have ALLOW access to the\n";
00520 $out .= "\t\tspecified ACO action (and the ACO's children, if any).\n";
00521 $out .= "\t\tIf an integer is passed permissions will be granted based on the foreign_key or object_id.\n";
00522 $out .= "\n";
00523 $out .= "\n";
00524 $out .= "\tdeny <Aro.alias|Aro.foreign_key> <Aco.alias|Aco.object_id> <aco_action>\n";
00525 $out .= "\t\tUse this command to deny ACL permissions. Once executed, the ARO\n";
00526 $out .= "\t\tspecified (and its children, if any) will have DENY access to the\n";
00527 $out .= "\t\tspecified ACO action (and the ACO's children, if any).\n";
00528 $out .= "\t\tIf an integer is passed permissions will be denied based on the foreign_key or object_id.\n";
00529 $out .= "\n";
00530 $out .= "\n";
00531 $out .= "\tinherit <Aro.alias|Aro.foreign_key> <Aco.alias|Aco.object_id> <aco_action>\n";
00532 $out .= "\t\tUse this command to force a child ARO object to inherit its\n";
00533 $out .= "\t\tpermissions settings from its parent.\n";
00534 $out .= "\t\tIf an integer is passed permissions will be inherited based on the foreign_key or object_id.\n";
00535 $out .= "\n";
00536 $out .= "\n";
00537 $out .= "\tview aro|aco [id]\n";
00538 $out .= "\t\tThe view command will return the ARO or ACO tree. The optional\n";
00539 $out .= "\t\tid/alias parameter allows you to return only a portion of the requested\n";
00540 $out .= "\t\ttree.\n";
00541 $out .= "\n";
00542 $out .= "\n";
00543 $out .= "\tinitdb\n";
00544 $out .= "\t\tUse this command to create the database tables needed to use DB ACL.\n";
00545 $out .= "\n";
00546 $out .= "\n";
00547 $out .= "\thelp\n";
00548 $out .= "\t\tDisplays this help message.\n";
00549 $out .= "\n";
00550 $out .= "\n";
00551 $this->stdout($out);
00552 }
00553
00554
00555
00556
00557
00558
00559 function displayError($title, $msg) {
00560 $out = "\n";
00561 $out .= "Error: $title\n";
00562 $out .= "$msg\n";
00563 $out .= "\n";
00564 $this->stdout($out);
00565 exit();
00566 }
00567
00568
00569
00570
00571
00572
00573
00574 function checkArgNumber($expectedNum, $command) {
00575 if (count($this->args) < $expectedNum) {
00576 $this->displayError('Wrong number of parameters: '.count($this->args), 'Please type \'php acl.php help\' for help on usage of the '.$command.' command.');
00577 }
00578 }
00579
00580
00581
00582
00583 function checkNodeType() {
00584 if ($this->args[0] != 'aco' && $this->args[0] != 'aro') {
00585 $this->displayError("Missing/Unknown node type: '".$this->args[0]."'", 'Please specify which ACL object type you wish to create.');
00586 }
00587 }
00588
00589
00590
00591
00592
00593
00594
00595 function nodeExists($type, $id) {
00596
00597 extract($this->__dataVars($type));
00598 $conditions = $this->Acl->{$class}->_resolveID($id);
00599 $possibility = $this->Acl->{$class}->findAll($conditions);
00600 return $possibility;
00601 }
00602
00603
00604
00605
00606
00607
00608
00609 function __dataVars($type = null) {
00610 if ($type == null) {
00611 $type = $this->args[0];
00612 }
00613
00614 $vars = array();
00615 $class = ucwords($type);
00616 $vars['secondary_id'] = ($class == 'aro' ? 'foreign_key' : 'object_id');
00617 $vars['data_name'] = $type;
00618 $vars['table_name'] = $type . 's';
00619 $vars['class'] = $class;
00620 return $vars;
00621 }
00622
00623
00624
00625
00626 function doDbConfig() {
00627 $this->hr();
00628 $this->stdout('Database Configuration:');
00629 $this->hr();
00630
00631 $driver = '';
00632
00633 while ($driver == '') {
00634 $driver = $this->getInput('What database driver would you like to use?', array('mysql','mysqli','mssql','sqlite','postgres', 'odbc'), 'mysql');
00635 if ($driver == '') {
00636 $this->stdout('The database driver supplied was empty. Please supply a database driver.');
00637 }
00638 }
00639
00640 switch($driver) {
00641 case 'mysql':
00642 $connect = 'mysql_connect';
00643 break;
00644 case 'mysqli':
00645 $connect = 'mysqli_connect';
00646 break;
00647 case 'mssql':
00648 $connect = 'mssql_connect';
00649 break;
00650 case 'sqlite':
00651 $connect = 'sqlite_open';
00652 break;
00653 case 'postgres':
00654 $connect = 'pg_connect';
00655 break;
00656 case 'odbc':
00657 $connect = 'odbc_connect';
00658 break;
00659 default:
00660 $this->stdout('The connection parameter could not be set.');
00661 break;
00662 }
00663
00664 $host = '';
00665
00666 while ($host == '') {
00667 $host = $this->getInput('What is the hostname for the database server?', null, 'localhost');
00668 if ($host == '') {
00669 $this->stdout('The host name you supplied was empty. Please supply a hostname.');
00670 }
00671 }
00672 $login = '';
00673
00674 while ($login == '') {
00675 $login = $this->getInput('What is the database username?', null, 'root');
00676
00677 if ($login == '') {
00678 $this->stdout('The database username you supplied was empty. Please try again.');
00679 }
00680 }
00681 $password = '';
00682 $blankPassword = false;
00683
00684 while ($password == '' && $blankPassword == false) {
00685 $password = $this->getInput('What is the database password?');
00686 if ($password == '') {
00687 $blank = $this->getInput('The password you supplied was empty. Use an empty password?', array('y', 'n'), 'n');
00688 if ($blank == 'y')
00689 {
00690 $blankPassword = true;
00691 }
00692 }
00693 }
00694 $database = '';
00695
00696 while ($database == '') {
00697 $database = $this->getInput('What is the name of the database you will be using?', null, 'cake');
00698
00699 if ($database == '') {
00700 $this->stdout('The database name you supplied was empty. Please try again.');
00701 }
00702 }
00703
00704 $prefix = '';
00705
00706 while ($prefix == '') {
00707 $prefix = $this->getInput('Enter a table prefix?', null, 'n');
00708 }
00709 if (low($prefix) == 'n') {
00710 $prefix = '';
00711 }
00712
00713 $this->stdout('');
00714 $this->hr();
00715 $this->stdout('The following database configuration will be created:');
00716 $this->hr();
00717 $this->stdout("Driver: $driver");
00718 $this->stdout("Connection: $connect");
00719 $this->stdout("Host: $host");
00720 $this->stdout("User: $login");
00721 $this->stdout("Pass: " . str_repeat('*', strlen($password)));
00722 $this->stdout("Database: $database");
00723 $this->stdout("Table prefix: $prefix");
00724 $this->hr();
00725 $looksGood = $this->getInput('Look okay?', array('y', 'n'), 'y');
00726
00727 if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
00728 $this->bakeDbConfig($driver, $connect, $host, $login, $password, $database, $prefix);
00729 } else {
00730 $this->stdout('Bake Aborted.');
00731 }
00732 }
00733
00734
00735
00736
00737
00738
00739
00740
00741 function bakeDbConfig( $driver, $connect, $host, $login, $password, $database, $prefix) {
00742 $out = "<?php\n";
00743 $out .= "class DATABASE_CONFIG {\n\n";
00744 $out .= "\tvar \$default = array(\n";
00745 $out .= "\t\t'driver' => '{$driver}',\n";
00746 $out .= "\t\t'connect' => '{$connect}',\n";
00747 $out .= "\t\t'host' => '{$host}',\n";
00748 $out .= "\t\t'login' => '{$login}',\n";
00749 $out .= "\t\t'password' => '{$password}',\n";
00750 $out .= "\t\t'database' => '{$database}', \n";
00751 $out .= "\t\t'prefix' => '{$prefix}' \n";
00752 $out .= "\t);\n";
00753 $out .= "}\n";
00754 $out .= "?>";
00755 $filename = CONFIGS.'database.php';
00756 $this->__createFile($filename, $out);
00757 }
00758
00759
00760
00761
00762
00763
00764
00765
00766 function getInput($prompt, $options = null, $default = null) {
00767 if (!is_array($options)) {
00768 $print_options = '';
00769 } else {
00770 $print_options = '(' . implode('/', $options) . ')';
00771 }
00772
00773 if ($default == null) {
00774 $this->stdout('');
00775 $this->stdout($prompt . " $print_options \n" . '> ', false);
00776 } else {
00777 $this->stdout('');
00778 $this->stdout($prompt . " $print_options \n" . "[$default] > ", false);
00779 }
00780 $result = trim(fgets($this->stdin));
00781
00782 if ($default != null && empty($result)) {
00783 return $default;
00784 } else {
00785 return $result;
00786 }
00787 }
00788
00789
00790
00791
00792
00793
00794 function stdout($string, $newline = true) {
00795 if ($newline) {
00796 fwrite($this->stdout, $string . "\n");
00797 } else {
00798 fwrite($this->stdout, $string);
00799 }
00800 }
00801
00802
00803
00804
00805
00806 function stderr($string) {
00807 fwrite($this->stderr, $string);
00808 }
00809
00810
00811
00812
00813 function hr() {
00814 $this->stdout('---------------------------------------------------------------');
00815 }
00816
00817
00818
00819
00820
00821
00822
00823 function __createFile ($path, $contents) {
00824 $path = str_replace('
00825 echo "\nCreating file $path\n";
00826 if (is_file($path) && $this->interactive === true) {
00827 fwrite($this->stdout, "File exists, overwrite?" . " {$path} (y/n/q):");
00828 $key = trim(fgets($this->stdin));
00829
00830 if ($key=='q') {
00831 fwrite($this->stdout, "Quitting.\n");
00832 exit;
00833 } elseif ($key == 'a') {
00834 $this->dont_ask = true;
00835 } elseif ($key == 'y') {
00836 } else {
00837 fwrite($this->stdout, "Skip" . " {$path}\n");
00838 return false;
00839 }
00840 }
00841
00842 if ($f = fopen($path, 'w')) {
00843 fwrite($f, $contents);
00844 fclose($f);
00845 fwrite($this->stdout, "Wrote" . "{$path}\n");
00846 return true;
00847 } else {
00848 fwrite($this->stderr, "Error! Could not write to" . " {$path}.\n");
00849 return false;
00850 }
00851 }
00852 }
00853 ?>