acl.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Short description for file.
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
  11. * Copyright 2005-2007, Cake Software Foundation, Inc.
  12. * 1785 E. Sahara Avenue, Suite 490-204
  13. * Las Vegas, Nevada 89104
  14. *
  15. * Licensed under The MIT License
  16. * Redistributions of files must retain the above copyright notice.
  17. *
  18. * @filesource
  19. * @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
  20. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  21. * @package cake
  22. * @subpackage cake.cake.scripts
  23. * @since CakePHP(tm) v 0.10.0.1232
  24. * @version $Revision$
  25. * @modifiedby $LastChangedBy$
  26. * @lastmodified $Date$
  27. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  28. */
  29. uses (
  30. 'object', 'configure', 'set', 'session', 'security',
  31. 'inflector', 'model'.DS.'connection_manager',
  32. 'model'.DS.'datasources'.DS.'dbo_source', 'model'.DS.'model'
  33. );
  34. require_once(CAKE.'app_model.php');
  35. uses ('controller'.DS.'components'.DS.'acl', 'model'.DS.'db_acl');
  36. /**
  37. * @package cake
  38. * @subpackage cake.cake.scripts
  39. */
  40. class Acl extends ConsoleScript {
  41. /**
  42. * Enter description here...
  43. *
  44. * @var unknown_type
  45. */
  46. var $acl;
  47. /**
  48. * Enter description here...
  49. *
  50. * @var unknown_type
  51. */
  52. var $args;
  53. /**
  54. * Enter description here...
  55. *
  56. * @var unknown_type
  57. */
  58. var $dataSource = 'default';
  59. /**
  60. * Enter description here...
  61. *
  62. * @param unknown_type $command
  63. * @param unknown_type $args
  64. */
  65. function main () {
  66. /*if (function_exists('ini_set')) {
  67. ini_set('display_errors', '1');
  68. ini_set('error_reporting', '7');
  69. ini_set('max_execution_time',0);
  70. }*/
  71. $this->dataSource = 'default';
  72. if (isset($this->params['datasource'])) {
  73. $this->dataSource = $this->params['datasource'];
  74. }
  75. define('DATASOURCE', $this->dataSource);
  76. if (ACL_CLASSNAME != 'DB_ACL') {
  77. $out = "--------------------------------------------------\n";
  78. $out .= "Error: Your current Cake configuration is set to \n";
  79. $out .= "an ACL implementation other than DB. Please change \n";
  80. $out .= "your core config to reflect your decision to use \n";
  81. $out .= "DB_ACL before attempting to use this script.\n";
  82. $out .= "--------------------------------------------------\n";
  83. $out .= "Current ACL Classname: " . ACL_CLASSNAME . "\n";
  84. $out .= "--------------------------------------------------\n";
  85. $this->err($out);
  86. exit();
  87. }
  88. pr($this);
  89. die();
  90. if(!in_array($command, array('help'))) {
  91. if(!file_exists(CONFIGS.'database.php')) {
  92. $this->out('');
  93. $this->out('Your database configuration was not found.');
  94. $this->out('Take a moment to create one:');
  95. $this->doDbConfig();
  96. }
  97. require_once (CONFIGS.'database.php');
  98. if(!in_array($command, array('initdb'))) {
  99. $this->dataSource = DATASOURCE;
  100. $this->Acl = new AclComponent();
  101. $this->db =& ConnectionManager::getDataSource($this->dataSource);
  102. }
  103. }
  104. switch ($command) {
  105. case 'create':
  106. $this->create();
  107. break;
  108. case 'delete':
  109. $this->delete();
  110. break;
  111. case 'setParent':
  112. $this->setParent();
  113. break;
  114. case 'getPath':
  115. $this->getPath();
  116. break;
  117. case 'grant':
  118. $this->grant();
  119. break;
  120. case 'deny':
  121. $this->deny();
  122. break;
  123. case 'inherit':
  124. $this->inherit();
  125. break;
  126. case 'view':
  127. $this->view();
  128. break;
  129. case 'initdb':
  130. $this->initdb();
  131. break;
  132. case 'upgrade':
  133. $this->upgradedb();
  134. break;
  135. case 'help':
  136. $this->help();
  137. break;
  138. default:
  139. $this->err("Unknown ACL command '$command'.\nFor usage, try 'php acl.php help'.\n\n");
  140. break;
  141. }
  142. }
  143. /**
  144. * Enter description here...
  145. *
  146. */
  147. function create() {
  148. $this->checkArgNumber(4, 'create');
  149. $this->checkNodeType();
  150. extract($this->__dataVars());
  151. if (preg_match('/^([\w]+)\.(.*)$/', $this->args[2], $matches)) {
  152. pr($matches);
  153. die();
  154. } else {
  155. $parent = $this->args[2];
  156. }
  157. if(!$this->Acl->{$class}->create(intval($this->args[1]), $parent, $this->args[3])){
  158. $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'.");
  159. }
  160. $this->out("New $class '".$this->args[3]."' created.\n\n");
  161. }
  162. /**
  163. * Enter description here...
  164. *
  165. */
  166. function delete() {
  167. $this->checkArgNumber(2, 'delete');
  168. $this->checkNodeType();
  169. extract($this->__dataVars());
  170. if(!$this->Acl->{$class}->delete($this->args[1])) {
  171. $this->displayError("Node Not Deleted", "There was an error deleting the ".$class.". Check that the node exists.\n");
  172. }
  173. $this->out("{$class} deleted.\n\n");
  174. }
  175. /**
  176. * Enter description here...
  177. *
  178. */
  179. function setParent() {
  180. $this->checkArgNumber(3, 'setParent');
  181. $this->checkNodeType();
  182. extract($this->__dataVars());
  183. if (!$this->Acl->{$class}->setParent($this->args[2], $this->args[1])){
  184. $this->out("Error in setting new parent. Please make sure the parent node exists, and is not a descendant of the node specified.\n");
  185. } else {
  186. $this->out("Node parent set to ".$this->args[2]."\n\n");
  187. }
  188. }
  189. /**
  190. * Enter description here...
  191. *
  192. */
  193. function getPath() {
  194. $this->checkArgNumber(2, 'getPath');
  195. $this->checkNodeType();
  196. extract($this->__dataVars());
  197. $id = (is_numeric($this->args[2])) ? intval($this->args[1]) : $this->args[1];
  198. $nodes = $this->Acl->{$class}->getPath($id);
  199. if (empty($nodes)) {
  200. $this->displayError("Supplied Node '".$this->args[1]."' not found", "No tree returned.");
  201. }
  202. for ($i = 0; $i < count($nodes); $i++) {
  203. $this->out(str_repeat(' ', $i) . "[" . $nodes[$i][$class]['id'] . "]" . $nodes[$i][$class]['alias'] . "\n");
  204. }
  205. }
  206. /**
  207. * Enter description here...
  208. *
  209. */
  210. function grant() {
  211. $this->checkArgNumber(3, 'grant');
  212. //add existence checks for nodes involved
  213. $aro = (is_numeric($this->args[0])) ? intval($this->args[0]) : $this->args[0];
  214. $aco = (is_numeric($this->args[1])) ? intval($this->args[1]) : $this->args[1];
  215. $this->Acl->allow($aro, $aco, $this->args[2]);
  216. $this->out("Permission granted.\n");
  217. }
  218. /**
  219. * Enter description here...
  220. *
  221. */
  222. function deny() {
  223. $this->checkArgNumber(3, 'deny');
  224. //add existence checks for nodes involved
  225. $aro = (is_numeric($this->args[0])) ? intval($this->args[0]) : $this->args[0];
  226. $aco = (is_numeric($this->args[1])) ? intval($this->args[1]) : $this->args[1];
  227. $this->Acl->deny($aro, $aco, $this->args[2]);
  228. $this->out("Requested permission successfully denied.\n");
  229. }
  230. /**
  231. * Enter description here...
  232. *
  233. */
  234. function inherit() {
  235. $this->checkArgNumber(3, 'inherit');
  236. $aro = (is_numeric($this->args[0])) ? intval($this->args[0]) : $this->args[0];
  237. $aco = (is_numeric($this->args[1])) ? intval($this->args[1]) : $this->args[1];
  238. $this->Acl->inherit($aro, $aco, $this->args[2]);
  239. $this->out("Requested permission successfully inherited.\n");
  240. }
  241. /**
  242. * Enter description here...
  243. *
  244. */
  245. function view() {
  246. $this->checkArgNumber(1, 'view');
  247. $this->checkNodeType();
  248. extract($this->__dataVars());
  249. if (!is_null($this->args[1])) {
  250. $conditions = $this->Acl->{$class}->_resolveID($this->args[1]);
  251. } else {
  252. $conditions = null;
  253. }
  254. $nodes = $this->Acl->{$class}->findAll($conditions, null, 'lft ASC');
  255. if (empty($nodes)) {
  256. $this->displayError($this->args[1]." not found", "No tree returned.");
  257. }
  258. $right = array();
  259. $this->out($class . " tree:");
  260. $this->hr(true);
  261. for($i = 0; $i < count($nodes); $i++){
  262. if (count($right) > 0){
  263. while ($right[count($right)-1] < $nodes[$i][$class]['rght']){
  264. if ($right[count($right)-1]){
  265. array_pop($right);
  266. } else {
  267. break;
  268. }
  269. }
  270. }
  271. $this->out(str_repeat(' ',count($right)) . "[" . $nodes[$i][$class]['id'] . "]" . $nodes[$i][$class]['alias']."\n");
  272. $right[] = $nodes[$i][$class]['rght'];
  273. }
  274. $this->hr(true);
  275. }
  276. /**
  277. * Enter description here...
  278. *
  279. */
  280. function initdb() {
  281. $db =& ConnectionManager::getDataSource($this->dataSource);
  282. $this->out("Initializing Database...\n");
  283. $this->out("Creating access control objects table (acos)...\n");
  284. $sql = " CREATE TABLE ".$db->fullTableName('acos')." (
  285. ".$db->name('id')." ".$db->column($db->columns['primary_key']).",
  286. ".$db->name('object_id')." ".$db->column($db->columns['integer'])." default NULL,
  287. ".$db->name('alias')." ".$db->column($db->columns['string'])." NOT NULL default '',
  288. ".$db->name('lft')." ".$db->column($db->columns['integer'])." default NULL,
  289. ".$db->name('rght')." ".$db->column($db->columns['integer'])." default NULL,
  290. PRIMARY KEY (".$db->name('id').")
  291. );";
  292. if ($db->query($sql) === false) {
  293. die("Error: " . $db->lastError() . "\n\n");
  294. }
  295. $this->out("Creating access request objects table (aros)...\n");
  296. $sql2 = "CREATE TABLE ".$db->fullTableName('aros')." (
  297. ".$db->name('id')." ".$db->column($db->columns['primary_key']).",
  298. ".$db->name('foreign_key')." ".$db->column($db->columns['integer'])." default NULL,
  299. ".$db->name('alias')." ".$db->column($db->columns['string'])." NOT NULL default '',
  300. ".$db->name('lft')." ".$db->column($db->columns['integer'])." default NULL,
  301. ".$db->name('rght')." ".$db->column($db->columns['integer'])." default NULL,
  302. PRIMARY KEY (".$db->name('id').")
  303. );";
  304. if ($db->query($sql2) === false) {
  305. die("Error: " . $db->lastError() . "\n\n");
  306. }
  307. $this->out("Creating relationships table (aros_acos)...\n");
  308. $sql3 = "CREATE TABLE ".$db->fullTableName('aros_acos')." (
  309. ".$db->name('id')." ".$db->column($db->columns['primary_key']).",
  310. ".$db->name('aro_id')." ".$db->column($db->columns['integer'])." default NULL,
  311. ".$db->name('aco_id')." ".$db->column($db->columns['integer'])." default NULL,
  312. ".$db->name('_create')." ".$db->column($db->columns['integer'])." NOT NULL default '0',
  313. ".$db->name('_read')." ".$db->column($db->columns['integer'])." NOT NULL default '0',
  314. ".$db->name('_update')." ".$db->column($db->columns['integer'])." NOT NULL default '0',
  315. ".$db->name('_delete')." ".$db->column($db->columns['integer'])." NOT NULL default '0',
  316. PRIMARY KEY (".$db->name('id').")
  317. );";
  318. if ($db->query($sql3) === false) {
  319. die("Error: " . $db->lastError() . "\n\n");
  320. }
  321. $this->out("\nDone.\n");
  322. }
  323. /**
  324. * Enter description here...
  325. *
  326. */
  327. function upgradedb() {
  328. $db =& ConnectionManager::getDataSource($this->dataSource);
  329. $this->out("Initializing Database...\n");
  330. $this->out("Upgrading table (aros)...\n");
  331. $sql = "ALTER TABLE ".$db->fullTableName('aros')."
  332. CHANGE ".$db->name('user_id')."
  333. ".$db->name('foreign_key')."
  334. INT( 10 ) UNSIGNED NULL DEFAULT NULL;";
  335. $sql .= "ALTER TABLE " . $db->name('aros_acos') . " CHANGE " . $db->name('_create')
  336. . " " . $db->name('_create') . " CHAR(2) NOT NULL DEFAULT '0';";
  337. $sql .= "ALTER TABLE " . $db->name('aros_acos') . " CHANGE " . $db->name('_update')
  338. . " " . $db->name('_update') . " CHAR(2) NOT NULL DEFAULT '0';";
  339. $sql .= "ALTER TABLE " . $db->name('aros_acos') . " CHANGE " . $db->name('_read')
  340. . " " . $db->name('_read') . " CHAR(2) NOT NULL DEFAULT '0';";
  341. $sql .= "ALTER TABLE " . $db->name('aros_acos') . " CHANGE " . $db->name('_delete')
  342. . " " . $db->name('_delete') . " CHAR(2) NOT NULL DEFAULT '0';";
  343. if ($db->query($sql) === false) {
  344. die("Error: " . $db->lastError() . "\n\n");
  345. }
  346. $this->out("\nDatabase upgrade is complete.\n");
  347. }
  348. /**
  349. * Enter description here...
  350. *
  351. */
  352. function help() {
  353. $out = "Usage: php acl.php <command> <arg1> <arg2>...\n";
  354. $out .= "-----------------------------------------------\n";
  355. $out .= "Commands:\n";
  356. $out .= "\n";
  357. $out .= "\tcreate aro|aco <link_id> <parent_id> <alias>\n";
  358. $out .= "\t\tCreates a new ACL object under the parent specified by <parent_id>, an id/alias (see\n";
  359. $out .= "\t\t'view'). The link_id allows you to link a user object to Cake's\n";
  360. $out .= "\t\tACL structures. The alias parameter allows you to address your object\n";
  361. $out .= "\t\tusing a non-integer ID. Example: \"\$php acl.php create aro 57 0 John\"\n";
  362. $out .= "\t\twould create a new ARO object at the root of the tree, linked to 57\n";
  363. $out .= "\t\tin your users table, with an internal alias 'John'.";
  364. $out .= "\n";
  365. $out .= "\n";
  366. $out .= "\tdelete aro|aco <id>\n";
  367. $out .= "\t\tDeletes the ACL object with the specified ID (see 'view').\n";
  368. $out .= "\n";
  369. $out .= "\n";
  370. $out .= "\tsetParent aro|aco <id> <parent_id>\n";
  371. $out .= "\t\tUsed to set the parent of the ACL object specified by <id> to the ID\n";
  372. $out .= "\t\tspecified by <parent_id>.\n";
  373. $out .= "\n";
  374. $out .= "\n";
  375. $out .= "\tgetPath aro|aco <id>\n";
  376. $out .= "\t\tReturns the path to the ACL object specified by <id>. This command is\n";
  377. $out .= "\t\tis useful in determining the inhertiance of permissions for a certain\n";
  378. $out .= "\t\tobject in the tree.\n";
  379. $out .= "\n";
  380. $out .= "\n";
  381. $out .= "\tgrant <aro_id> <aco_id> <aco_action>\n";
  382. $out .= "\t\tUse this command to grant ACL permissions. Once executed, the ARO\n";
  383. $out .= "\t\tspecified (and its children, if any) will have ALLOW access to the\n";
  384. $out .= "\t\tspecified ACO action (and the ACO's children, if any).\n";
  385. $out .= "\n";
  386. $out .= "\n";
  387. $out .= "\tdeny <aro_id> <aco_id> <aco_action>\n";
  388. $out .= "\t\tUse this command to deny ACL permissions. Once executed, the ARO\n";
  389. $out .= "\t\tspecified (and its children, if any) will have DENY access to the\n";
  390. $out .= "\t\tspecified ACO action (and the ACO's children, if any).\n";
  391. $out .= "\n";
  392. $out .= "\n";
  393. $out .= "\tinherit <aro_id> <aco_id> <aco_action> \n";
  394. $out .= "\t\tUse this command to force a child ARO object to inherit its\n";
  395. $out .= "\t\tpermissions settings from its parent.\n";
  396. $out .= "\n";
  397. $out .= "\n";
  398. $out .= "\tview aro|aco [id]\n";
  399. $out .= "\t\tThe view command will return the ARO or ACO tree. The optional\n";
  400. $out .= "\t\tid/alias parameter allows you to return only a portion of the requested\n";
  401. $out .= "\t\ttree.\n";
  402. $out .= "\n";
  403. $out .= "\n";
  404. $out .= "\tinitdb\n";
  405. $out .= "\t\tUse this command to create the database tables needed to use DB ACL.\n";
  406. $out .= "\n";
  407. $out .= "\n";
  408. $out .= "\thelp\n";
  409. $out .= "\t\tDisplays this help message.\n";
  410. $out .= "\n";
  411. $out .= "\n";
  412. $this->out($out);
  413. }
  414. /**
  415. * Enter description here...
  416. *
  417. * @param unknown_type $title
  418. * @param unknown_type $msg
  419. */
  420. function displayError($title, $msg) {
  421. $out = "\n";
  422. $out .= "Error: $title\n";
  423. $out .= "$msg\n";
  424. $out .= "\n";
  425. $this->out($out);
  426. exit();
  427. }
  428. /**
  429. * Enter description here...
  430. *
  431. * @param unknown_type $expectedNum
  432. * @param unknown_type $command
  433. */
  434. function checkArgNumber($expectedNum, $command) {
  435. if (count($this->args) < $expectedNum) {
  436. $this->displayError('Wrong number of parameters: '.count($this->args), 'Please type \'php acl.php help\' for help on usage of the '.$command.' command.');
  437. }
  438. }
  439. /**
  440. * Enter description here...
  441. *
  442. */
  443. function checkNodeType() {
  444. if ($this->args[0] != 'aco' && $this->args[0] != 'aro') {
  445. $this->displayError("Missing/Unknown node type: '".$this->args[0]."'", 'Please specify which ACL object type you wish to create.');
  446. }
  447. }
  448. /**
  449. * Enter description here...
  450. *
  451. * @param unknown_type $type
  452. * @param unknown_type $id
  453. * @return unknown
  454. */
  455. function nodeExists($type, $id) {
  456. //$this->out("Check to see if $type with ID = $id exists...\n");
  457. extract($this->__dataVars($type));
  458. $conditions = $this->Acl->{$class}->_resolveID($id);
  459. $possibility = $this->Acl->{$class}->findAll($conditions);
  460. return $possibility;
  461. }
  462. /**
  463. * Enter description here...
  464. *
  465. * @param unknown_type $type
  466. * @return unknown
  467. */
  468. function __dataVars($type = null) {
  469. if ($type == null) {
  470. $type = $this->args[0];
  471. }
  472. $vars = array();
  473. $class = ucwords($type);
  474. $vars['secondary_id'] = ($class == 'aro' ? 'foreign_key' : 'object_id');
  475. $vars['data_name'] = $type;
  476. $vars['table_name'] = $type . 's';
  477. $vars['class'] = $class;
  478. return $vars;
  479. }
  480. /**
  481. * Database configuration setup.
  482. *
  483. */
  484. function doDbConfig() {
  485. $this->hr(true);
  486. $this->out('Database Configuration:');
  487. $this->hr(true);
  488. $driver = '';
  489. while ($driver == '') {
  490. $driver = $this->getInput('What database driver would you like to use?', array('mysql','mysqli','mssql','sqlite','postgres', 'odbc'), 'mysql');
  491. if ($driver == '') {
  492. $this->out('The database driver supplied was empty. Please supply a database driver.');
  493. }
  494. }
  495. switch($driver) {
  496. case 'mysql':
  497. $connect = 'mysql_connect';
  498. break;
  499. case 'mysqli':
  500. $connect = 'mysqli_connect';
  501. break;
  502. case 'mssql':
  503. $connect = 'mssql_connect';
  504. break;
  505. case 'sqlite':
  506. $connect = 'sqlite_open';
  507. break;
  508. case 'postgres':
  509. $connect = 'pg_connect';
  510. break;
  511. case 'odbc':
  512. $connect = 'odbc_connect';
  513. break;
  514. default:
  515. $this->out('The connection parameter could not be set.');
  516. break;
  517. }
  518. $host = '';
  519. while ($host == '') {
  520. $host = $this->getInput('What is the hostname for the database server?', null, 'localhost');
  521. if ($host == '') {
  522. $this->out('The host name you supplied was empty. Please supply a hostname.');
  523. }
  524. }
  525. $login = '';
  526. while ($login == '') {
  527. $login = $this->getInput('What is the database username?', null, 'root');
  528. if ($login == '') {
  529. $this->out('The database username you supplied was empty. Please try again.');
  530. }
  531. }
  532. $password = '';
  533. $blankPassword = false;
  534. while ($password == '' && $blankPassword == false) {
  535. $password = $this->getInput('What is the database password?');
  536. if ($password == '') {
  537. $blank = $this->getInput('The password you supplied was empty. Use an empty password?', array('y', 'n'), 'n');
  538. if($blank == 'y')
  539. {
  540. $blankPassword = true;
  541. }
  542. }
  543. }
  544. $database = '';
  545. while ($database == '') {
  546. $database = $this->getInput('What is the name of the database you will be using?', null, 'cake');
  547. if ($database == '') {
  548. $this->out('The database name you supplied was empty. Please try again.');
  549. }
  550. }
  551. $prefix = '';
  552. while ($prefix == '') {
  553. $prefix = $this->getInput('Enter a table prefix?', null, 'n');
  554. }
  555. if(low($prefix) == 'n') {
  556. $prefix = '';
  557. }
  558. $this->hr(true);
  559. $this->out('The following database configuration will be created:');
  560. $this->hr(true);
  561. $this->out("Driver: $driver");
  562. $this->out("Connection: $connect");
  563. $this->out("Host: $host");
  564. $this->out("User: $login");
  565. $this->out("Pass: " . str_repeat('*', strlen($password)));
  566. $this->out("Database: $database");
  567. $this->out("Table prefix: $prefix");
  568. $this->hr(true);
  569. $looksGood = $this->getInput('Look okay?', array('y', 'n'), 'y');
  570. if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
  571. $this->bakeDbConfig($driver, $connect, $host, $login, $password, $database, $prefix);
  572. } else {
  573. $this->out('Bake Aborted.');
  574. }
  575. }
  576. /**
  577. * Creates a database configuration file for Bake.
  578. *
  579. * @param string $host
  580. * @param string $login
  581. * @param string $password
  582. * @param string $database
  583. */
  584. function bakeDbConfig( $driver, $connect, $host, $login, $password, $database, $prefix) {
  585. $out = "<?php\n";
  586. $out .= "class DATABASE_CONFIG {\n\n";
  587. $out .= "\tvar \$default = array(\n";
  588. $out .= "\t\t'driver' => '{$driver}',\n";
  589. $out .= "\t\t'connect' => '{$connect}',\n";
  590. $out .= "\t\t'host' => '{$host}',\n";
  591. $out .= "\t\t'login' => '{$login}',\n";
  592. $out .= "\t\t'password' => '{$password}',\n";
  593. $out .= "\t\t'database' => '{$database}', \n";
  594. $out .= "\t\t'prefix' => '{$prefix}' \n";
  595. $out .= "\t);\n";
  596. $out .= "}\n";
  597. $out .= "?>";
  598. $filename = CONFIGS.'database.php';
  599. $this->__createFile($filename, $out);
  600. }
  601. /**
  602. * Prompts the user for input, and returns it.
  603. *
  604. * @param string $prompt Prompt text.
  605. * @param mixed $options Array or string of options.
  606. * @param string $default Default input value.
  607. * @return Either the default value, or the user-provided input.
  608. */
  609. function getInput($prompt, $options = null, $default = null) {
  610. if (!is_array($options)) {
  611. $print_options = '';
  612. } else {
  613. $print_options = '(' . implode('/', $options) . ')';
  614. }
  615. if($default == null) {
  616. $this->out('');
  617. $this->out($prompt . " $print_options \n" . '> ', false);
  618. } else {
  619. $this->out('');
  620. $this->out($prompt . " $print_options \n" . "[$default] > ", false);
  621. }
  622. $result = trim(fgets($this->stdin));
  623. if($default != null && empty($result)) {
  624. return $default;
  625. } else {
  626. return $result;
  627. }
  628. }
  629. }
  630. ?>