Projekt

Obecné

Profil

Stáhnout (25.3 KB) Statistiky
| Větev: | Tag: | Revize:
1
#!/usr/bin/php -q
2
<?php
3
/* SVN FILE: $Id: acl.php 5117 2007-05-18 16:46:55Z phpnut $ */
4
/**
5
 * Short description for file.
6
 *
7
 * Long description for file
8
 *
9
 * PHP versions 4 and 5
10
 *
11
 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
12
 * Copyright 2005-2007, Cake Software Foundation, Inc.
13
 *								1785 E. Sahara Avenue, Suite 490-204
14
 *								Las Vegas, Nevada 89104
15
 *
16
 * Licensed under The MIT License
17
 * Redistributions of files must retain the above copyright notice.
18
 *
19
 * @filesource
20
 * @copyright		Copyright 2005-2007, Cake Software Foundation, Inc.
21
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
22
 * @package			cake
23
 * @subpackage		cake.cake.scripts
24
 * @since			CakePHP(tm) v 0.10.0.1232
25
 * @version			$Revision: 5117 $
26
 * @modifiedby		$LastChangedBy: phpnut $
27
 * @lastmodified	$Date: 2007-05-18 11:46:55 -0500 (Fri, 18 May 2007) $
28
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
29
 */
30
/**
31
 * Enter description here...
32
 */
33
	define ('DS', DIRECTORY_SEPARATOR);
34
	if (function_exists('ini_set')) {
35
		ini_set('display_errors', '1');
36
		ini_set('error_reporting', '7');
37
		ini_set('max_execution_time',0);
38
	}
39

    
40
	$app = 'app';
41
	$core = null;
42
	$root = dirname(dirname(dirname(__FILE__)));
43
	$here = $argv[0];
44
	$dataSource = 'default';
45
	$unset = array();
46
	for ($i = 1; $i < count($argv); $i++) {
47
		// Process command-line modifiers here
48
		switch (strtolower($argv[$i])) {
49
			case '-app':
50
				$app = $argv[$i + 1];
51
				$unset[$i] = $argv[$i];
52
				$unset[$i + 1] = $argv[$i + 1];
53
			break;
54
			case '-core':
55
				$core = $argv[$i + 1];
56
				$unset[$i] = $argv[$i];
57
				$unset[$i + 1] = $argv[$i + 1];
58
			break;
59
			case '-root':
60
				$root = $argv[$i + 1];
61
				$unset[$i] = $argv[$i];
62
				$unset[$i + 1] = $argv[$i + 1];
63
			break;
64
			case '-datasource':
65
				$dataSource = $argv[$i + 1];
66
				$unset[$i] = $argv[$i];
67
				$unset[$i + 1] = $argv[$i + 1];
68
			break;
69
		}
70
	}
71

    
72
	if (strlen($app) && $app[0] == DS) {
73
		$cnt = substr_count($root, DS);
74
		$app = str_repeat('..' . DS, $cnt) . $app;
75
	}
76
	define ('ROOT', $root.DS);
77
	define ('APP_DIR', $app);
78
	define ('DEBUG', 1);;
79
	define('CAKE_CORE_INCLUDE_PATH', ROOT);
80
	define('DATASOURCE', $dataSource);
81

    
82
	if(function_exists('ini_set')) {
83
		ini_set('include_path',ini_get('include_path').
84
			PATH_SEPARATOR.CAKE_CORE_INCLUDE_PATH.DS.
85
			PATH_SEPARATOR.CORE_PATH.DS.
86
			PATH_SEPARATOR.ROOT.DS.APP_DIR.DS.
87
			PATH_SEPARATOR.APP_DIR.DS.
88
			PATH_SEPARATOR.APP_PATH);
89
		define('APP_PATH', null);
90
		define('CORE_PATH', null);
91
	} else {
92
		define('APP_PATH', ROOT . DS . APP_DIR . DS);
93
		define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
94
	}
95

    
96
	require ('cake'.DS.'basics.php');
97
	require ('cake'.DS.'config'.DS.'paths.php');
98
	require (CONFIGS.'core.php');
99
	uses ('object', 'configure', 'neat_array', 'session', 'security', 'inflector', 'model'.DS.'connection_manager',
100
			'model'.DS.'datasources'.DS.'dbo_source', 'model'.DS.'model');
101
	require(CAKE.'app_model.php');
102
	uses ('controller'.DS.'components'.DS.'acl', 'controller'.DS.'components'.DS.'dbacl'.DS.'models'.DS.'aclnode',
103
			'controller'.DS.'components'.DS.'dbacl'.DS.'models'.DS.'aco', 'controller'.DS.'components'.DS.'dbacl'.DS.'models'.DS.'acoaction',
104
			'controller'.DS.'components'.DS.'dbacl'.DS.'models'.DS.'aro');
105
	//Get and format args: first arg is the name of the script.
106
	$serverArgs = $argv;
107
	if(!empty($unset)) {
108
		$serverArgs = array_values(array_diff($argv, $unset));
109
	}
110

    
111
	$wasted = array_shift($serverArgs);
112
	$command = array_shift($serverArgs);
113
	$args = $serverArgs;
114
	$aclCLI = new AclCLI ($command, $args);
115
/**
116
 * @package		cake
117
 * @subpackage	cake.cake.scritps
118
 */
119
class AclCLI {
120
/**
121
 * Enter description here...
122
 *
123
 * @var unknown_type
124
 */
125
	var $stdin;
126
/**
127
 * Enter description here...
128
 *
129
 * @var unknown_type
130
 */
131
	var $stdout;
132
/**
133
 * Enter description here...
134
 *
135
 * @var unknown_type
136
 */
137
	var $stderr;
138
/**
139
 * Enter description here...
140
 *
141
 * @var unknown_type
142
 */
143
	var $acl;
144
/**
145
 * Enter description here...
146
 *
147
 * @var unknown_type
148
 */
149
	var $args;
150
/**
151
 * Enter description here...
152
 *
153
 * @var unknown_type
154
 */
155
	var $dataSource = 'default';
156
/**
157
 * Enter description here...
158
 *
159
 * @param unknown_type $command
160
 * @param unknown_type $args
161
 * @return AclCLI
162
 */
163
	function AclCLI($command, $args) {
164
		$this->__construct($command, $args);
165
	}
166
/**
167
 * Enter description here...
168
 *
169
 * @param unknown_type $command
170
 * @param unknown_type $args
171
 */
172
	function __construct ($command, $args) {
173
		$this->stdin = fopen('php://stdin', 'r');
174
		$this->stdout = fopen('php://stdout', 'w');
175
		$this->stderr = fopen('php://stderr', 'w');
176

    
177
		if (ACL_CLASSNAME != 'DB_ACL'){
178
			$out = "--------------------------------------------------\n";
179
			$out .= "Error: Your current Cake configuration is set to \n";
180
			$out .= "an ACL implementation other than DB. Please change \n";
181
			$out .= "your core config to reflect your decision to use \n";
182
			$out .= "DB_ACL before attempting to use this script.\n";
183
			$out .= "--------------------------------------------------\n";
184
			$out .= "Current ACL Classname: " . ACL_CLASSNAME . "\n";
185
			$out .= "--------------------------------------------------\n";
186
			fwrite($this->stderr, $out);
187
			exit();
188
		}
189

    
190
		if(!in_array($command, array('help'))) {
191
			if(!file_exists(CONFIGS.'database.php')) {
192
				$this->stdout('');
193
				$this->stdout('Your database configuration was not found.');
194
				$this->stdout('Take a moment to create one:');
195
				$this->doDbConfig();
196
			}
197
			require_once (CONFIGS.'database.php');
198

    
199
			if(!in_array($command, array('initdb'))) {
200
				$this->dataSource = DATASOURCE;
201
				$this->Acl = new AclComponent();
202
				$this->args = $args;
203
				$this->db =& ConnectionManager::getDataSource($this->dataSource);
204
			}
205
		}
206

    
207
		switch ($command) {
208
			case 'create':
209
				$this->create();
210
			break;
211
			case 'delete':
212
				$this->delete();
213
			break;
214
			case 'setParent':
215
				$this->setParent();
216
			break;
217
			case 'getPath':
218
				$this->getPath();
219
			break;
220
			case 'grant':
221
				$this->grant();
222
			break;
223
			case 'deny':
224
				$this->deny();
225
			break;
226
			case 'inherit':
227
				$this->inherit();
228
			break;
229
			case 'view':
230
				$this->view();
231
			break;
232
			case 'initdb':
233
				$this->initdb();
234
			break;
235
			case 'upgrade':
236
				$this->upgradedb();
237
			break;
238
			case 'help':
239
				$this->help();
240
			break;
241
			default:
242
				fwrite($this->stderr, "Unknown ACL command '$command'.\nFor usage, try 'php acl.php help'.\n\n");
243
			break;
244
		}
245
	}
246
/**
247
 * Enter description here...
248
 *
249
 */
250
	function create() {
251
		$this->checkArgNumber(4, 'create');
252
		$this->checkNodeType();
253
		extract($this->__dataVars());
254

    
255
		$parent = (is_numeric($this->args[2])) ? intval($this->args[2]) : $this->args[2];
256
		if(!$this->Acl->{$class}->create(intval($this->args[1]), $parent, $this->args[3])){
257
			$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'.");
258
		}
259
		$this->stdout("New $class '".$this->args[3]."' created.\n\n");
260
	}
261
/**
262
 * Enter description here...
263
 *
264
 */
265
	function delete() {
266
		$this->checkArgNumber(2, 'delete');
267
		$this->checkNodeType();
268
		extract($this->__dataVars());
269
		if(!$this->Acl->{$class}->delete($this->args[1])) {
270
			$this->displayError("Node Not Deleted", "There was an error deleting the ".$class.". Check that the node exists.\n");
271
		}
272
		$this->stdout("{$class} deleted.\n\n");
273
	}
274

    
275
/**
276
 * Enter description here...
277
 *
278
 */
279
	function setParent() {
280
		$this->checkArgNumber(3, 'setParent');
281
		$this->checkNodeType();
282
		extract($this->__dataVars());
283
		if (!$this->Acl->{$class}->setParent($this->args[2], $this->args[1])){
284
			$this->stdout("Error in setting new parent. Please make sure the parent node exists, and is not a descendant of the node specified.\n");
285
		} else {
286
			$this->stdout("Node parent set to ".$this->args[2]."\n\n");
287
		}
288
	}
289
/**
290
 * Enter description here...
291
 *
292
 */
293
	function getPath() {
294
		$this->checkArgNumber(2, 'getPath');
295
		$this->checkNodeType();
296
		extract($this->__dataVars());
297
		$id = (is_numeric($this->args[2])) ? intval($this->args[1]) : $this->args[1];
298
		$nodes = $this->Acl->{$class}->getPath($id);
299
		if (empty($nodes)) {
300
			$this->displayError("Supplied Node '".$this->args[1]."' not found", "No tree returned.");
301
		}
302
		for ($i = 0; $i < count($nodes); $i++) {
303
			$this->stdout(str_repeat('  ', $i) . "[" . $nodes[$i][$class]['id'] . "]" . $nodes[$i][$class]['alias'] . "\n");
304
		}
305
	}
306
/**
307
 * Enter description here...
308
 *
309
 */
310
	function grant() {
311
		$this->checkArgNumber(3, 'grant');
312
		//add existence checks for nodes involved
313
		$aro = $this->args[0];
314
		if(is_numeric($aro)) {
315
			$aro = intval($aro);
316
		}
317
		$aco = $this->args[1];
318
		if(is_numeric($aco)) {
319
			$aco = intval($aco);
320
		}
321
		if($this->Acl->allow($aro, $aco, $this->args[2])) {
322
			$this->stdout("Permission granted.\n");
323
		} else {
324
			$this->stdout("Permission could not be granted.\n");
325
		}
326
	}
327
/**
328
 * Enter description here...
329
 *
330
 */
331
	function deny() {
332
		$this->checkArgNumber(3, 'deny');
333
		//add existence checks for nodes involved
334
		$aro = (is_numeric($this->args[0])) ? intval($this->args[0]) : $this->args[0];
335
		$aco = (is_numeric($this->args[1])) ? intval($this->args[1]) : $this->args[1];
336
		$aro = $this->args[0];
337
		if(is_numeric($aro)) {
338
			$aro = intval($aro);
339
		}
340
		$aco = $this->args[1];
341
		if(is_numeric($aco)) {
342
			$aco = intval($aco);
343
		}
344
		if($this->Acl->deny($aro, $aco, $this->args[2])) {
345
			$this->stdout("Permission denied.\n");
346
		} else {
347
			$this->stdout("Permission could not be denied.\n");
348
		}
349
	}
350
/**
351
 * Enter description here...
352
 *
353
 */
354
	function inherit() {
355
		$this->checkArgNumber(3, 'inherit');
356
		$aro = $this->args[0];
357
		if(is_numeric($aro)) {
358
			$aro = intval($aro);
359
		}
360
		$aco = $this->args[1];
361
		if(is_numeric($aco)) {
362
			$aco = intval($aco);
363
		}
364
		if($this->Acl->inherit($aro, $aco, $this->args[2])) {
365
			$this->stdout("Permission inherited.\n");
366
		} else {
367
			$this->stdout("Permission could not be inherited.\n");
368
		}
369
	}
370
/**
371
 * Enter description here...
372
 *
373
 */
374
	function view() {
375
		$this->checkArgNumber(1, 'view');
376
		$this->checkNodeType();
377
		extract($this->__dataVars());
378
		if (!is_null($this->args[1])) {
379
			$conditions = $this->Acl->{$class}->_resolveID($this->args[1]);
380
		} else {
381
			$conditions = null;
382
		}
383
		$nodes = $this->Acl->{$class}->findAll($conditions, null, 'lft ASC');
384
		if (empty($nodes)) {
385
			$this->displayError($this->args[1]." not found", "No tree returned.");
386
		}
387
		$right = array();
388

    
389
		$this->stdout($class . " tree:\n");
390
		$this->stdout("------------------------------------------------\n");
391

    
392
		for($i = 0; $i < count($nodes); $i++){
393
			if (count($right) > 0){
394
				while ($right[count($right)-1] < $nodes[$i][$class]['rght']){
395
					if ($right[count($right)-1]){
396
						array_pop($right);
397
					} else {
398
						break;
399
					}
400
				}
401
			}
402
			$this->stdout(str_repeat('  ',count($right)) . "[" . $nodes[$i][$class]['id'] . "]" . $nodes[$i][$class]['alias']."\n");
403
			$right[] = $nodes[$i][$class]['rght'];
404
		}
405
		$this->stdout("------------------------------------------------\n");
406
	}
407
/**
408
 * Enter description here...
409
 *
410
 */
411
	function initdb() {
412
		$db =& ConnectionManager::getDataSource($this->dataSource);
413
		$this->stdout("Initializing Database...\n");
414
		$this->stdout("Creating access control objects table (acos)...\n");
415
		$sql = " CREATE TABLE ".$db->fullTableName('acos')." (
416
				".$db->name('id')." ".$db->column($db->columns['primary_key']).",
417
				".$db->name('object_id')." ".$db->column($db->columns['integer'])." default NULL,
418
				".$db->name('alias')." ".$db->column($db->columns['string'])." NOT NULL default '',
419
				".$db->name('lft')." ".$db->column($db->columns['integer'])." default NULL,
420
				".$db->name('rght')." ".$db->column($db->columns['integer'])." default NULL,
421
				PRIMARY KEY  (".$db->name('id').")
422
				);";
423
		if ($db->query($sql) === false) {
424
			die("Error: " . $db->lastError() . "\n\n");
425
		}
426

    
427
		$this->stdout("Creating access request objects table (aros)...\n");
428
		$sql2 = "CREATE TABLE ".$db->fullTableName('aros')." (
429
				".$db->name('id')." ".$db->column($db->columns['primary_key']).",
430
				".$db->name('foreign_key')." ".$db->column($db->columns['integer'])." default NULL,
431
				".$db->name('alias')." ".$db->column($db->columns['string'])." NOT NULL default '',
432
				".$db->name('lft')." ".$db->column($db->columns['integer'])." default NULL,
433
				".$db->name('rght')." ".$db->column($db->columns['integer'])." default NULL,
434
				PRIMARY KEY  (".$db->name('id').")
435
				);";
436
		if ($db->query($sql2) === false) {
437
			die("Error: " . $db->lastError() . "\n\n");
438
		}
439

    
440
		$this->stdout("Creating relationships table (aros_acos)...\n");
441
		$sql3 = "CREATE TABLE ".$db->fullTableName('aros_acos')." (
442
				".$db->name('id')." ".$db->column($db->columns['primary_key']).",
443
				".$db->name('aro_id')." ".$db->column($db->columns['integer'])." default NULL,
444
				".$db->name('aco_id')." ".$db->column($db->columns['integer'])." default NULL,
445
				".$db->name('_create')." ".$db->column($db->columns['integer'])." NOT NULL default '0',
446
				".$db->name('_read')." ".$db->column($db->columns['integer'])." NOT NULL default '0',
447
				".$db->name('_update')." ".$db->column($db->columns['integer'])." NOT NULL default '0',
448
				".$db->name('_delete')." ".$db->column($db->columns['integer'])." NOT NULL default '0',
449
				PRIMARY KEY  (".$db->name('id').")
450
				);";
451
		if ($db->query($sql3) === false) {
452
			die("Error: " . $db->lastError() . "\n\n");
453
		}
454

    
455
		$this->stdout("\nDone.\n");
456
	}
457

    
458
/**
459
 * Enter description here...
460
 *
461
 */
462
	function upgradedb() {
463
		$db =& ConnectionManager::getDataSource($this->dataSource);
464
		$this->stdout("Initializing Database...\n");
465
		$this->stdout("Upgrading table (aros)...\n");
466
		$sql = "ALTER TABLE ".$db->fullTableName('aros')."
467
				CHANGE ".$db->name('user_id')."
468
				".$db->name('foreign_key')."
469
				INT( 10 ) UNSIGNED NULL DEFAULT NULL;";
470
		$sql .= "ALTER TABLE " . $db->name('aros_acos') . " CHANGE " . $db->name('_create')
471
				. " " . $db->name('_create') . " CHAR(2) NOT NULL DEFAULT '0';";
472
		$sql .= "ALTER TABLE " . $db->name('aros_acos') . " CHANGE " . $db->name('_update')
473
				. " " . $db->name('_update') . " CHAR(2) NOT NULL DEFAULT '0';";
474
		$sql .= "ALTER TABLE " . $db->name('aros_acos') . " CHANGE " . $db->name('_read')
475
				. " " . $db->name('_read') . " CHAR(2) NOT NULL DEFAULT '0';";
476
		$sql .= "ALTER TABLE " . $db->name('aros_acos') . " CHANGE " . $db->name('_delete')
477
				. " " . $db->name('_delete') . " CHAR(2) NOT NULL DEFAULT '0';";
478
		if ($db->query($sql) === false) {
479
			die("Error: " . $db->lastError() . "\n\n");
480
		}
481
		$this->stdout("\nDatabase upgrade is complete.\n");
482
	}
483

    
484
/**
485
 * Enter description here...
486
 *
487
 */
488
	function help() {
489
		$out = "Usage: php acl.php <command> <arg1> <arg2>...\n";
490
		$out .= "-----------------------------------------------\n";
491
		$out .= "Commands:\n";
492
		$out .= "\n";
493
		$out .= "\tcreate aro|aco <link_id> <parent_id> <alias>\n";
494
		$out .= "\t\tCreates a new ACL object under the parent specified by <parent_id>, an id/alias (see\n";
495
		$out .= "\t\t'view'). The link_id allows you to link a user object to Cake's\n";
496
		$out .= "\t\tACL structures. The alias parameter allows you to address your object\n";
497
		$out .= "\t\tusing a non-integer ID. Example: \"\$php acl.php create aro 57 0 John\"\n";
498
		$out .= "\t\twould create a new ARO object at the root of the tree, linked to 57\n";
499
		$out .= "\t\tin your users table, with an internal alias 'John'.";
500
		$out .= "\n";
501
		$out .= "\n";
502
		$out .= "\tdelete aro|aco <id>\n";
503
		$out .= "\t\tDeletes the ACL object with the specified ID (see 'view').\n";
504
		$out .= "\n";
505
		$out .= "\n";
506
		$out .= "\tsetParent aro|aco <id> <parent_id>\n";
507
		$out .= "\t\tUsed to set the parent of the ACL object specified by <id> to the ID\n";
508
		$out .= "\t\tspecified by <parent_id>.\n";
509
		$out .= "\n";
510
		$out .= "\n";
511
		$out .= "\tgetPath aro|aco <id>\n";
512
		$out .= "\t\tReturns the path to the ACL object specified by <id>. This command is\n";
513
		$out .= "\t\tis useful in determining the inhertiance of permissions for a certain\n";
514
		$out .= "\t\tobject in the tree.\n";
515
		$out .= "\n";
516
		$out .= "\n";
517
		$out .= "\tgrant <Aro.alias|Aro.foreign_key> <Aco.alias|Aco.object_id> <aco_action>\n";
518
		$out .= "\t\tUse this command to grant ACL permissions. Once executed, the ARO\n";
519
		$out .= "\t\tspecified (and its children, if any) will have ALLOW access to the\n";
520
		$out .= "\t\tspecified ACO action (and the ACO's children, if any).\n";
521
		$out .= "\t\tIf an integer is passed permissions will be granted based on the foreign_key or object_id.\n";
522
		$out .= "\n";
523
		$out .= "\n";
524
		$out .= "\tdeny <Aro.alias|Aro.foreign_key> <Aco.alias|Aco.object_id> <aco_action>\n";
525
		$out .= "\t\tUse this command to deny ACL permissions. Once executed, the ARO\n";
526
		$out .= "\t\tspecified (and its children, if any) will have DENY access to the\n";
527
		$out .= "\t\tspecified ACO action (and the ACO's children, if any).\n";
528
		$out .= "\t\tIf an integer is passed permissions will be denied based on the foreign_key or object_id.\n";
529
		$out .= "\n";
530
		$out .= "\n";
531
		$out .= "\tinherit <Aro.alias|Aro.foreign_key> <Aco.alias|Aco.object_id> <aco_action>\n";
532
		$out .= "\t\tUse this command to force a child ARO object to inherit its\n";
533
		$out .= "\t\tpermissions settings from its parent.\n";
534
		$out .= "\t\tIf an integer is passed permissions will be inherited based on the foreign_key or object_id.\n";
535
		$out .= "\n";
536
		$out .= "\n";
537
		$out .= "\tview aro|aco [id]\n";
538
		$out .= "\t\tThe view command will return the ARO or ACO tree. The optional\n";
539
		$out .= "\t\tid/alias parameter allows you to return only a portion of the requested\n";
540
		$out .= "\t\ttree.\n";
541
		$out .= "\n";
542
		$out .= "\n";
543
		$out .= "\tinitdb\n";
544
		$out .= "\t\tUse this command to create the database tables needed to use DB ACL.\n";
545
		$out .= "\n";
546
		$out .= "\n";
547
		$out .= "\thelp\n";
548
		$out .= "\t\tDisplays this help message.\n";
549
		$out .= "\n";
550
		$out .= "\n";
551
		$this->stdout($out);
552
	}
553
/**
554
 * Enter description here...
555
 *
556
 * @param unknown_type $title
557
 * @param unknown_type $msg
558
 */
559
	function displayError($title, $msg) {
560
		$out = "\n";
561
		$out .= "Error: $title\n";
562
		$out .= "$msg\n";
563
		$out .= "\n";
564
		$this->stdout($out);
565
		exit();
566
	}
567

    
568
/**
569
 * Enter description here...
570
 *
571
 * @param unknown_type $expectedNum
572
 * @param unknown_type $command
573
 */
574
	function checkArgNumber($expectedNum, $command) {
575
		if (count($this->args) < $expectedNum) {
576
			$this->displayError('Wrong number of parameters: '.count($this->args), 'Please type \'php acl.php help\' for help on usage of the '.$command.' command.');
577
		}
578
	}
579
/**
580
 * Enter description here...
581
 *
582
 */
583
	function checkNodeType() {
584
		if ($this->args[0] != 'aco' && $this->args[0] != 'aro') {
585
			$this->displayError("Missing/Unknown node type: '".$this->args[0]."'", 'Please specify which ACL object type you wish to create.');
586
		}
587
	}
588
/**
589
 * Enter description here...
590
 *
591
 * @param unknown_type $type
592
 * @param unknown_type $id
593
 * @return unknown
594
 */
595
	function nodeExists($type, $id) {
596
		//$this->stdout("Check to see if $type with ID = $id exists...\n");
597
		extract($this->__dataVars($type));
598
		$conditions = $this->Acl->{$class}->_resolveID($id);
599
		$possibility = $this->Acl->{$class}->findAll($conditions);
600
		return $possibility;
601
	}
602

    
603
/**
604
 * Enter description here...
605
 *
606
 * @param unknown_type $type
607
 * @return unknown
608
 */
609
	function __dataVars($type = null) {
610
		if ($type == null) {
611
			$type = $this->args[0];
612
		}
613

    
614
		$vars = array();
615
		$class = ucwords($type);
616
		$vars['secondary_id'] = ($class == 'aro' ? 'foreign_key' : 'object_id');
617
		$vars['data_name'] = $type;
618
		$vars['table_name'] = $type . 's';
619
		$vars['class'] = $class;
620
		return $vars;
621
	}
622
/**
623
 * Database configuration setup.
624
 *
625
 */
626
	function doDbConfig() {
627
		$this->hr();
628
		$this->stdout('Database Configuration:');
629
		$this->hr();
630

    
631
		$driver = '';
632

    
633
		while ($driver == '') {
634
			$driver = $this->getInput('What database driver would you like to use?', array('mysql','mysqli','mssql','sqlite','postgres', 'odbc'), 'mysql');
635
			if ($driver == '') {
636
				$this->stdout('The database driver supplied was empty. Please supply a database driver.');
637
			}
638
		}
639

    
640
		switch($driver) {
641
			case 'mysql':
642
			$connect = 'mysql_connect';
643
			break;
644
			case 'mysqli':
645
			$connect = 'mysqli_connect';
646
			break;
647
			case 'mssql':
648
			$connect = 'mssql_connect';
649
			break;
650
			case 'sqlite':
651
			$connect = 'sqlite_open';
652
			break;
653
			case 'postgres':
654
			$connect = 'pg_connect';
655
			break;
656
			case 'odbc':
657
			$connect = 'odbc_connect';
658
			break;
659
			default:
660
			$this->stdout('The connection parameter could not be set.');
661
			break;
662
		}
663

    
664
		$host = '';
665

    
666
		while ($host == '') {
667
			$host = $this->getInput('What is the hostname for the database server?', null, 'localhost');
668
			if ($host == '') {
669
				$this->stdout('The host name you supplied was empty. Please supply a hostname.');
670
			}
671
		}
672
		$login = '';
673

    
674
		while ($login == '') {
675
			$login = $this->getInput('What is the database username?', null, 'root');
676

    
677
			if ($login == '') {
678
				$this->stdout('The database username you supplied was empty. Please try again.');
679
			}
680
		}
681
		$password = '';
682
		$blankPassword = false;
683

    
684
		while ($password == '' && $blankPassword == false) {
685
			$password = $this->getInput('What is the database password?');
686
			if ($password == '') {
687
				$blank = $this->getInput('The password you supplied was empty. Use an empty password?', array('y', 'n'), 'n');
688
				if($blank == 'y')
689
				{
690
					$blankPassword = true;
691
				}
692
			}
693
		}
694
		$database = '';
695

    
696
		while ($database == '') {
697
			$database = $this->getInput('What is the name of the database you will be using?', null, 'cake');
698

    
699
			if ($database == '')  {
700
				$this->stdout('The database name you supplied was empty. Please try again.');
701
			}
702
		}
703

    
704
		$prefix = '';
705

    
706
		while ($prefix == '') {
707
			$prefix = $this->getInput('Enter a table prefix?', null, 'n');
708
		}
709
		if(low($prefix) == 'n') {
710
			$prefix = '';
711
		}
712

    
713
		$this->stdout('');
714
		$this->hr();
715
		$this->stdout('The following database configuration will be created:');
716
		$this->hr();
717
		$this->stdout("Driver:        $driver");
718
		$this->stdout("Connection:    $connect");
719
		$this->stdout("Host:          $host");
720
		$this->stdout("User:          $login");
721
		$this->stdout("Pass:          " . str_repeat('*', strlen($password)));
722
		$this->stdout("Database:      $database");
723
		$this->stdout("Table prefix:  $prefix");
724
		$this->hr();
725
		$looksGood = $this->getInput('Look okay?', array('y', 'n'), 'y');
726

    
727
		if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
728
			$this->bakeDbConfig($driver, $connect, $host, $login, $password, $database, $prefix);
729
		} else {
730
			$this->stdout('Bake Aborted.');
731
		}
732
	}
733
/**
734
 * Creates a database configuration file for Bake.
735
 *
736
 * @param string $host
737
 * @param string $login
738
 * @param string $password
739
 * @param string $database
740
 */
741
	function bakeDbConfig( $driver, $connect, $host, $login, $password, $database, $prefix) {
742
		$out = "<?php\n";
743
		$out .= "class DATABASE_CONFIG {\n\n";
744
		$out .= "\tvar \$default = array(\n";
745
		$out .= "\t\t'driver' => '{$driver}',\n";
746
		$out .= "\t\t'connect' => '{$connect}',\n";
747
		$out .= "\t\t'host' => '{$host}',\n";
748
		$out .= "\t\t'login' => '{$login}',\n";
749
		$out .= "\t\t'password' => '{$password}',\n";
750
		$out .= "\t\t'database' => '{$database}', \n";
751
		$out .= "\t\t'prefix' => '{$prefix}' \n";
752
		$out .= "\t);\n";
753
		$out .= "}\n";
754
		$out .= "?>";
755
		$filename = CONFIGS.'database.php';
756
		$this->__createFile($filename, $out);
757
	}
758
/**
759
 * Prompts the user for input, and returns it.
760
 *
761
 * @param string $prompt Prompt text.
762
 * @param mixed $options Array or string of options.
763
 * @param string $default Default input value.
764
 * @return Either the default value, or the user-provided input.
765
 */
766
	function getInput($prompt, $options = null, $default = null) {
767
		if (!is_array($options)) {
768
			$print_options = '';
769
		} else {
770
			$print_options = '(' . implode('/', $options) . ')';
771
		}
772

    
773
		if($default == null) {
774
			$this->stdout('');
775
			$this->stdout($prompt . " $print_options \n" . '> ', false);
776
		} else {
777
			$this->stdout('');
778
			$this->stdout($prompt . " $print_options \n" . "[$default] > ", false);
779
		}
780
		$result = trim(fgets($this->stdin));
781

    
782
		if($default != null && empty($result)) {
783
			return $default;
784
		} else {
785
			return $result;
786
		}
787
	}
788
/**
789
 * Outputs to the stdout filehandle.
790
 *
791
 * @param string $string String to output.
792
 * @param boolean $newline If true, the outputs gets an added newline.
793
 */
794
	function stdout($string, $newline = true) {
795
		if ($newline) {
796
			fwrite($this->stdout, $string . "\n");
797
		} else {
798
			fwrite($this->stdout, $string);
799
		}
800
	}
801
/**
802
 * Outputs to the stderr filehandle.
803
 *
804
 * @param string $string Error text to output.
805
 */
806
	function stderr($string) {
807
		fwrite($this->stderr, $string);
808
	}
809
/**
810
 * Outputs a series of minus characters to the standard output, acts as a visual separator.
811
 *
812
 */
813
	function hr() {
814
		$this->stdout('---------------------------------------------------------------');
815
	}
816
/**
817
 * Creates a file at given path.
818
 *
819
 * @param string $path		Where to put the file.
820
 * @param string $contents Content to put in the file.
821
 * @return Success
822
 */
823
	function __createFile ($path, $contents) {
824
		$path = str_replace('//', '/', $path);
825
		echo "\nCreating file $path\n";
826
		if (is_file($path) && $this->interactive === true) {
827
			fwrite($this->stdout, "File exists, overwrite?" . " {$path} (y/n/q):");
828
			$key = trim(fgets($this->stdin));
829

    
830
			if ($key=='q') {
831
				fwrite($this->stdout, "Quitting.\n");
832
				exit;
833
			} elseif ($key == 'a') {
834
				$this->dont_ask = true;
835
			} elseif ($key == 'y') {
836
			} else {
837
				fwrite($this->stdout, "Skip" . " {$path}\n");
838
				return false;
839
			}
840
		}
841

    
842
		if ($f = fopen($path, 'w')) {
843
			fwrite($f, $contents);
844
			fclose($f);
845
			fwrite($this->stdout, "Wrote" . "{$path}\n");
846
			return true;
847
		} else {
848
			fwrite($this->stderr, "Error! Could not write to" . " {$path}.\n");
849
			return false;
850
		}
851
	}
852
}
853
?>
(1-1/2)