Browse Source

shells added

m 14 years ago
parent
commit
9c82c0e716
5 changed files with 731 additions and 0 deletions
  1. 298 0
      vendors/shells/cc.php
  2. 157 0
      vendors/shells/notes.php
  3. 71 0
      vendors/shells/pwd.php
  4. 76 0
      vendors/shells/pwd_reset.php
  5. 129 0
      vendors/shells/user.php

+ 298 - 0
vendors/shells/cc.php

@@ -0,0 +1,298 @@
+<?php
+
+App::import('Core', 'Folder');
+App::import('Core', 'File');
+
+if (!defined('LF')) {
+	define('LF', "\r\n"); # windows compatible as default
+}
+
+/**
+ * Code Completion
+ * 2009-12-26 ms
+ */
+class CcShell extends Shell {
+	var $uses = array();
+
+	protected $plugins = null;
+	protected $content = '';
+
+	function main() {
+		$this->out('Code Completion Dump - customized for PHPDesigner');
+
+		//TODO: ask for version (1.2 etc - defaults to 1.3!)
+
+		$this->filename = APP.'code_completion__.php';
+
+		# get classes
+		$this->models();
+		$this->behaviors();
+		
+		$this->components();
+		$this->helpers();
+		//TODO: behaviors
+
+		# write to file
+		$this->_dump();
+
+		$this->out('...done');
+	}
+
+	/**
+	 * @deprecated
+	 * now use: Configure::listObjects()
+	 */
+	function __getFiles($folder) {
+		$handle = new Folder($folder);
+		$handleFiles = $handle->read(true, true);
+		$files = $handleFiles[1];
+		foreach ($files as $key => $file) {
+			$file = extractPathInfo('file', $file);
+
+			if (mb_strrpos($file, '_') === mb_strlen($file) - 1) { # ending with _ like test_.php
+				unset($files[$key]);
+			} else {
+				$files[$key] = Inflector::camelize($file);
+			}
+		}
+		return $files;
+	}
+
+
+	public function _getFiles($type) {
+    $files = App::objects($type, null, false);
+    
+    //$paths = (array)App::path($type.'s');
+    //$libFiles = App::objects($type, $paths[0] . 'lib' . DS, false);
+
+    if (!isset($this->plugins)) {
+    	$this->plugins = App::objects('plugin');
+    }
+    
+    if (!empty($this->plugins)) {
+      foreach ($this->plugins as $plugin) {
+      	$path = App::pluginPath($plugin);
+      	if ($type == 'helper') {
+      		$path .= 'views' . DS;
+      	} elseif ($type == 'components') {
+      		$path .= 'controllers' . DS;
+      	} elseif ($type == 'behavior') {
+      		$path .= 'models' . DS;
+      	} elseif ($type == 'datasources') {
+      		$path .= 'models' . DS;
+      	} 
+				$path .= $type.'s' . DS;
+      	
+				$pluginFiles = App::objects($type, $path, false);
+				if (!empty($pluginFiles)) {
+				    foreach ($pluginFiles as $t) {
+				        $files[] = $t;
+				    }
+				}
+      }
+    }
+    $files = array_unique($files);
+
+		$appIndex = array_search('App', $files);
+		if ($appIndex !== false) {
+			unset($files[$appIndex]);
+		}
+
+		# no test/tmp files etc (helper.test.php or helper.OLD.php)
+    foreach ($files as $key => $file) {
+			if (strpos($file, '.') !== false || !preg_match('/^[\da-zA-Z_]+$/', $file)) {
+				unset($files[$key]);
+			}
+		}
+    return $files;
+	}
+
+
+	function models() {
+		$files = $this->_getFiles('model');
+
+		$content = LF.'<?php'.LF;
+		$content .= '/*** model start ***/'.LF;
+		$content .= 'class AppModel extends Model {'.LF;
+		if (!empty($files)) {
+			$content .= $this->_prepModels($files);
+		}
+		
+		$content .= '}'.LF;
+		$content .= '/*** model end ***/'.LF;
+		$content .= '?>';
+
+		$this->content .= $content;
+	}
+	
+	function behaviors() {
+		$files = $this->_getFiles('behavior');
+
+		$content = LF.'<?php'.LF;
+		$content .= '/*** behavior start ***/'.LF;
+		$content .= 'class AppModel extends Model {'.LF;
+		if (!empty($files)) {
+			$content .= $this->_prepBehaviors($files);
+		}
+		$content .= '}'.LF;
+		$content .= '/*** behavior end ***/'.LF;
+		$content .= '?>';
+		
+		$content .= '/*** model start ***/'.LF;
+		
+		$this->content .= $content;
+	}
+	
+	function components() {
+		$files = $this->_getFiles('component');
+
+		$content = LF.'<?php'.LF;
+		$content .= '/*** component start ***/'.LF;
+		$content .= 'class AppController extends Controller {'.LF;
+		if (!empty($files)) {
+			$content .= $this->_prepComponents($files);
+		}
+		$content .= '}'.LF;
+		$content .= '/*** component end ***/'.LF;
+		$content .= '?>';
+
+		$this->content .= $content;
+	}
+
+	function helpers() {
+		$files = $this->_getFiles('helper');
+		$content = LF.'<?php'.LF;
+		$content .= '/*** helper start ***/'.LF;
+		$content .= 'class AppHelper extends Helper {'.LF;
+		if (!empty($files)) {
+			$content .= $this->_prepHelpers($files);
+		}
+		$content .= '}'.LF;
+		$content .= '/*** helper end ***/'.LF;
+		$content .= '?>';
+
+		$this->content .= $content;
+	}
+
+	function _prepModels($files) {
+		$res = '';
+		foreach ($files as $name) {
+			$res .= '
+	/**
+	* '.$name.'
+	*
+	* @var '.$name.'
+	*/
+	public $'.$name.';
+'.LF;
+		}
+
+		$res .= '	function __construct() {';
+
+		foreach ($files as $name) {
+			$res .= '
+		$this->'.$name.' = new '.$name.'();';
+		}
+
+		$res .= LF.'	}'.LF;
+		return $res;
+	}
+	
+	function _prepBehaviors($files) {
+		$res = '';
+		foreach ($files as $name) {
+			$res .= '
+	/**
+	* '.$name.'Behavior
+	*
+	* @var '.$name.'Behavior
+	*/
+	public $'.$name.';
+'.LF;
+		}
+
+		$res .= '	function __construct() {';
+
+		foreach ($files as $name) {
+			$res .= '
+		$this->'.$name.' = new '.$name.'Behavior();';
+		}
+
+		$res .= LF.'	}'.LF;
+		return $res;
+	}
+
+	function _prepComponents($files) {
+		$res = '';
+		foreach ($files as $name) {
+			$res .= '
+	/**
+	* '.$name.'Component
+	*
+	* @var '.$name.'Component
+	*/
+	public $'.$name.';
+'.LF;
+		}
+
+		$res .= '	function __construct() {';
+
+		foreach ($files as $name) {
+			$res .= '
+		$this->'.$name.' = new '.$name.'Component();';
+		}
+
+		$res .= LF.'	}'.LF;
+		return $res;
+	}
+
+	function _prepHelpers($files) {
+		# new ones
+		$res = '';
+
+		foreach ($files as $name) {
+			$res .= '
+	/**
+	* '.$name.'Helper
+	*
+	* @var '.$name.'Helper
+	*/
+	public $'.$name.';
+'.LF;
+		}
+
+		$res .= '	function __construct() {';
+
+		foreach ($files as $name) {
+			$res .= '
+		$this->'.$name.' = new '.$name.'Helper();';
+		}
+
+		/*
+		foreach ($files as $name) {
+		$res .= '
+		$'.lcfirst($name).' = new '.$name.'Helper();
+		';
+		}
+		$res .= LF;
+		*/
+
+		$res .= LF.'	}'.LF;
+
+		return $res;
+	}
+
+
+	function _dump() {
+		$file = new File($this->filename, true);
+
+		$content = '<?php exit();'.LF;
+		$content .= '//Add in some helpers so the code assist works much better'.LF;
+		$content .= '//Printed: '.date('d.m.Y, H:i:s').LF;
+		$content .= '?>'.LF;
+		$content .= $this->content;
+		return $file->write($content);
+	}
+}
+
+

+ 157 - 0
vendors/shells/notes.php

@@ -0,0 +1,157 @@
+<?php
+/**
+ * The NotesTask is a source-annotations extractor task for bake2, that allows you to add FIXME, OPTIMIZE,
+ * and TODO comments to your source code that can then be extracted in concert with this task
+ *
+ * PHP versions 4 and 5
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @filesource
+ * @copyright		Copyright 2006-2007, Joel Moss
+ * @link				http://joelmoss.info
+ * @since			CakePHP(tm) v 1.2
+ * @version			$Version: 1.0 $
+ * @modifiedby		$LastChangedBy: joelmoss $
+ * @lastmodified	$Date: 2007-02-27 (Tues, 27 Feb 2007) $
+ * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
+ * 
+ * 
+ * @Changelog
+ * 
+ * v 1.0
+ *  [+] Initial code offering
+ *  
+ * 
+ * LOTS OF PROBLEMS - until working...
+ * 2009-05-07 ms
+ */
+
+App::import('Core','Folder');
+App::import('Core','File');
+
+class NotesShell extends Shell {
+  var $notes = array();
+  var $type = null;
+  var $dirs = array(
+    'config',
+    'controllers',
+    'models',
+    'plugins',
+  );
+  
+	function main($params = null) {
+		$this->welcome();
+		
+		if (isset($params[0])) {
+			if ($params[0] == 'todo') {
+  				$this->type = 'TODO';
+  			} elseif ($params[0] == 'fixme') {
+  				$this->type = 'FIXME';
+  			} elseif ($params[0] == 'optimise' || $params[0] == 'optimize') {
+  				$this->type = 'OPTIMIZE';
+  			} elseif ($params[0] == 'help') {
+  				$this->help();
+  			}
+		}
+
+		$this->read();
+		foreach ($this->dirs as $d) {
+			$this->read($d, true);
+		}
+		foreach ($this->notes as $file => $types) {
+			$this->out("$file:");
+			$this->out('');
+			foreach ($types as $type => $notes)
+			{
+			  foreach ($notes as $ln => $note)
+			  {
+			    $this->out("   * [$ln] [$type] $note");
+			  }
+			}
+			$this->out('');
+		}
+		$this->hr();
+  }
+    
+  function read($dir = null, $recursive = false) {
+    $notes = array();
+    $path = CORE_PATH.APP_PATH.$dir;
+		
+    $folder = new Folder(APP_PATH.$dir);
+    $fold = $recursive ? $folder->findRecursive('.*\.php') : $folder->find('.*\.php');
+    foreach ($fold as $file) {
+      $file = $recursive ? $file : $path.$file;
+      $file_path = r(CORE_PATH.APP_PATH, '', $file);
+      
+      $handle = new File($file_path);
+      $content = $handle->read();
+      $lines = explode(PHP_EOL, $content);
+      //$lines = file($file);
+      $ln = 1;
+      if (!empty($lines)) {
+	      foreach ($lines as $line) {
+	      	if ((is_null($this->type) || $this->type == 'TODO') &&
+	      	     preg_match("/[#\*\\/\\/]\s*TODO\s*(.*)/", $line, $match)) {
+	      	  $this->notes[$file_path]['TODO'][$ln] = $match[1];
+	      	}
+	      	if ((is_null($this->type) || $this->type == 'OPTIMIZE') &&
+	      	     preg_match("/[#\*\\/\\/]\s*OPTIMIZE|OPTIMISE\s*(.*)/", $line, $match)) {
+	      	  $this->notes[$file_path]['OPTIMIZE'][$ln] = $match[1];
+	      	}
+	      	if ((is_null($this->type) || $this->type == 'FIXME') &&
+	      	     preg_match("/[#\*\\/\\/]\s*FIXME|BUG\s*(.*)/", $line, $match)) {
+	      	  $this->notes[$file_path]['FIXME'][$ln] = $match[1];
+	      	}
+	      	$ln++;
+	      }
+      }
+    }
+    return $this->notes;
+  }
+  
+  function help() {
+    $this->out("This task allows you to add");
+    $this->out("FIXME/BUG, OPTIMIZE, and TODO comments to your source, e.g.:");    
+    $this->out("# FIXME: blablub");
+    $this->out("");
+    $this->out("code that can then be extracted in concert with bake2 notes (shows all), bake2");
+    $this->out("notes fixme, bake2 notes optimize and bake2 notes todo.");
+    $this->out("Usage: bake notes [todo|optimize|fixme]");
+    $this->hr();
+    exit;
+  }
+  
+  /*
+  function out($str='', $newline=true)
+  {
+    $nl = $newline ? "\n" : "";
+    echo "  $str$nl";
+  }
+  function hr()
+  {
+    echo "\n  ----------------------------------------------------------------------------\n";
+  }
+  function err($str)
+  {
+    $this->out('');
+    $this->out('');
+    $this->out($str);
+    $this->out('');
+    $this->out('');
+    exit;
+  }
+  */
+  
+  function welcome()
+  {
+    $this->out('');
+    $this->hr();
+    $this->out('-- Notes --');
+    $this->hr();
+    $this->out('');
+  }
+  
+}
+

+ 71 - 0
vendors/shells/pwd.php

@@ -0,0 +1,71 @@
+<?php
+
+class PwdShell extends Shell {
+	var $tasks = array();
+	//var $uses = array('User');
+	
+	var $Auth = null;	
+	
+	function main() {
+		$components = array('AuthExt', 'Auth');
+		
+		foreach ($components as $component) {
+			if (App::import('Component', $component)) {
+				$component .='Component';
+				$this->Auth = new $component();
+				break;
+			}
+		}
+		if (!is_object($this->Auth)) {
+			$this->out('No Auth Component found');
+			die();
+		}
+		
+		$this->out('Using: '.get_class($this->Auth));
+		
+		while (empty($pwToHash) || mb_strlen($pwToHash) < 2) {
+			$pwToHash = $this->in(__('Password to Hash (2 characters at least)', true));
+		}
+				
+		$pw = $this->Auth->password($pwToHash);
+		$this->hr();
+		echo $pw;	
+	}
+	
+	
+	function hash() {
+		if (!empty($this->args[0]) && in_array(strtolower($this->args[0]), hash_algos())) {
+			$type = strtolower($this->args[0]);
+		} else {
+			# prompt for one
+			$type = $this->in(__('Hash Type', true), array_combine(array_values(hash_algos()), array_values(hash_algos())), 'sha1');
+		}
+		$pwd = '123';
+		$this->hr();
+		echo hash($type, $pwd);
+	}
+	
+	function compare() {
+		$algos = hash_algos();
+		$data = "hello";
+		foreach ($algos as $v) { 
+			$res = hash($v, $data, false); 
+			$r = str_split($res, 50);
+			printf("%-12s %3d  %s\n", $v, strlen($res), array_shift($r));
+			while (!empty($r)) { 
+				printf("                  %s\n", array_shift($r));
+			} 
+		} 
+	}
+	
+	function help() {
+		$this->out('-- Hash Passwort with Auth(Ext) Component --');
+		$this->out('-- cake pwd');
+		$this->out('---- using the salt of the core.php (!)');
+		$this->out('-- cake pwd hash [method]');
+		$this->out('---- for custom hashing of pwd strings (method name optional)');
+		$this->out('-- cake pwd compare');
+		$this->out('---- to list all available methods and their lenghts');
+	}
+}
+

+ 76 - 0
vendors/shells/pwd_reset.php

@@ -0,0 +1,76 @@
+<?php
+
+# enhancement for plugin user model
+if (!defined('CLASS_USER')) {
+	define('CLASS_USER', 'User');
+}
+
+/**
+ * reset user passwords
+ * 2011-08-01 ms
+ */
+class PwdResetShell extends Shell {
+	var $tasks = array();
+	//var $uses = array('User');
+
+	var $Auth = null;
+
+	/**
+	 * reset all pwds to a simply pwd (for local development)
+	 * 2011-08-01 ms
+	 */
+	function main() {
+		$components = array('AuthExt', 'Auth');
+		foreach ($components as $component) {
+			if (App::import('Component', $component)) {
+				$component .='Component';
+				$this->Auth = new $component();
+				break;
+			}
+		}
+		if (!is_object($this->Auth)) {
+			$this->out('No Auth Component found');
+			die();
+		}
+
+		$this->out('Using: '.get_class($this->Auth).' (Abort with STRG+C)');
+
+
+		if (!empty($this->args[0]) && mb_strlen($this->args[0]) >= 2) {
+			$pwToHash = $this->args[0];
+		}
+		while (empty($pwToHash) || mb_strlen($pwToHash) < 2) {
+			$pwToHash = $this->in(__('Password to Hash (2 characters at least)', true));
+		}
+		$this->hr();
+		$this->out('pwd:');
+		$this->out($pwToHash);
+		$pw = $this->Auth->password($pwToHash);
+		$this->hr();
+		$this->out('hash:');
+		$this->out($pw);
+
+		$this->hr();
+		$this->out('resetting...');
+
+		$this->User = ClassRegistry::init(CLASS_USER);
+		if (!$this->User->hasField('password')) {
+			$this->error(CLASS_USER.' model doesnt have a password field!');
+		}
+		
+		if (method_exists($this->User, 'escapeValue')) {
+			$newPwd = $this->User->escapeValue($pw);
+		} else {
+			$newPwd = '\''.$pw.'\'';
+		}
+		$this->User->recursive = -1;
+		$this->User->updateAll(array('password'=>$newPwd), array('password !='=>$pw));
+		$count = $this->User->getAffectedRows();
+		$this->out($count.' pwds resetted - DONE');
+	}
+
+
+	function help() {
+		$this->out('-- Hash and Reset all user passwords with Auth(Ext) Component --');
+	}
+}

+ 129 - 0
vendors/shells/user.php

@@ -0,0 +1,129 @@
+<?php
+
+if (!defined('CLASS_USER')) {
+	define('CLASS_USER', 'User');
+}
+
+class UserShell extends Shell {
+	var $tasks = array();
+	var $uses = array(CLASS_USER);
+
+	/*
+	function initialize() {
+		//Configure::write('debug', 2);
+
+		parent::initialize();
+		//$this->User = ClassRegistry::init('User');
+	}
+	*/
+
+	function help() {
+		$this->out('command: cake user');
+	}
+
+
+	//TODO: refactor (smaller sub-parts)
+	function main() {
+		if (App::import('Component', 'AuthExt')) {
+			$this->Auth = new AuthExtComponent();
+		} else {
+			App::import('Component', 'Auth');
+			$this->Auth = new AuthComponent();
+		}
+
+		while (empty($username)) {
+			$username = $this->in(__('Username (2 characters at least)', true));
+		}
+		while (empty($password)) {
+			$password = $this->in(__('Password (2 characters at least)', true));
+		}
+
+		$schema = $this->User->schema();
+
+		if (isset($this->User->Role) && is_object($this->User->Role)) {
+			$roles = $this->User->Role->find('list');
+
+			if (!empty($roles)) {
+				$this->out('');
+				pr($roles);
+			}
+
+			$roleIds = array_keys($roles);
+			while (!empty($roles) && empty($role)) {
+				$role = $this->in(__('Role', true), $roleIds);
+			}
+		} elseif (method_exists($this->User, 'roles')) {
+			$roles = User::roles();
+
+			if (!empty($roles)) {
+				$this->out('');
+				pr ($roles);
+			}
+
+			$roleIds = array_keys($roles);
+			while (!empty($roles) && empty($role)) {
+				$role = $this->in(__('Role', true), $roleIds);
+			}
+		}
+		if (empty($roles)) {
+			$this->out('No Role found (either no table, or no data)');
+			$role = $this->in(__('Please insert a role manually', true));
+		}
+
+		$this->out('');
+		$pwd = $this->Auth->password($password);
+
+		$data = array('User'=>array(
+			'password' => $pwd,
+			'active' => 1
+		));
+		if (!empty($username)) {
+			$data['User']['username'] = $username;
+		}
+		if (!empty($email)) {
+			$data['User']['email'] = $email;
+		}
+		if (!empty($role)) {
+			$data['User']['role_id'] = $role;
+		}
+
+		if (!empty($schema['status']) && method_exists('User', 'statuses')) {
+			$statuses = User::statuses();
+			pr($statuses);
+			while(empty($status)) {
+				$status = $this->in(__('Please insert a status', true), array_keys($statuses));
+			}
+			$data['User']['status'] = $status;
+		}
+
+		if (!empty($schema['email'])) {
+			$provideEmail = $this->in(__('Provide Email? ', true),array('y', 'n'), 'n');
+			if ($provideEmail === 'y') {
+				$email = $this->in(__('Please insert an email', true));
+				$data['User']['email'] = $email;
+			}
+			if (!empty($schema['email_confirmed'])) {
+				$data['User']['email_confirmed'] = 1;
+			}
+		}
+
+
+		$this->out('');
+		pr ($data);
+		$this->out('');
+		$this->out('');
+		$continue = $this->in(__('Continue? ', true),array('y', 'n'), 'n');
+		if ($continue != 'y') {
+			$this->error('Not Executed!');
+		}
+
+		$this->out('');
+		$this->hr();
+		if ($this->User->save($data)) {
+			$this->out('User inserted! ID: '.$this->User->id);
+		} else {
+			$this->error('User could not be inserted ('.print_r($this->User->validationErrors, true).')');
+		}
+	}
+}
+