Beispiel #1
0
 public function run()
 {
     $filename = Hymn_Client::$fileName;
     if (!file_exists($filename)) {
         throw new RuntimeException('File "' . $filename . '" is missing');
     }
     $config = json_decode(file_get_contents($filename));
     if (is_null($config)) {
         throw new RuntimeException('Configuration file "' . $filename . '" is not valid JSON');
     }
     $key = $this->client->arguments->getArgument(0);
     if (!strlen(trim($key))) {
         throw new InvalidArgumentException('Missing first argument "key" is missing');
     }
     $key = $this->client->arguments->getArgument(0);
     $value = $this->client->arguments->getArgument(1);
     $parts = explode(".", $key);
     if (count($parts) === 3) {
         if (!isset($config->{$parts[0]})) {
             $config->{$parts[0]} = (object) array();
         }
         if (!isset($config->{$parts[0]}->{$parts[1]})) {
             $config->{$parts[0]}->{$parts[1]} = (object) array();
         }
         if (!isset($config->{$parts[0]}->{$parts[1]}->{$parts[2]})) {
             $config->{$parts[0]}->{$parts[1]}->{$parts[2]} = NULL;
         }
         $current = $config->{$parts[0]}->{$parts[1]}->{$parts[2]};
     } else {
         if (count($parts) === 2) {
             if (!isset($config->{$parts[0]})) {
                 $config->{$parts[0]} = (object) array();
             }
             if (!isset($config->{$parts[0]}->{$parts[1]})) {
                 $config->{$parts[0]}->{$parts[1]} = NULL;
             }
             $current = $config->{$parts[0]}->{$parts[1]};
         } else {
             throw new InvalidArgumentException('Invalid key - must be of syntax "path.(subpath.)key"');
         }
     }
     if (!strlen(trim($value))) {
         $value = trim(Hymn_Client::getInput("Value for '" . $key . "'", $current, array(), FALSE));
     }
     if (preg_match('/^".*"$/', $value)) {
         $value = substr($value, 1, -1);
     }
     //		if( $current === $value )
     //			throw new RuntimeException( 'No change made' );
     if (count($parts) === 3) {
         $config->{$parts[0]}->{$parts[1]}->{$parts[2]} = $value;
     } else {
         if (count($parts) === 2) {
             $config->{$parts[0]}->{$parts[1]} = $value;
         }
     }
     file_put_contents($filename, json_encode($config, JSON_PRETTY_PRINT));
     clearstatcache();
 }
Beispiel #2
0
 public function run()
 {
     $config = $this->client->getConfig();
     if (!isset($config->database)) {
         $config->database = (object) array();
     }
     $dba = $config->database;
     $dba->driver = isset($dba->driver) ? $dba->driver : "mysql";
     $dba->host = isset($dba->host) ? $dba->host : "localhost";
     $dba->port = isset($dba->port) ? $dba->port : "3306";
     $dba->name = isset($dba->name) ? $dba->name : NULL;
     $dba->prefix = isset($dba->prefix) ? $dba->prefix : NULL;
     $dba->username = isset($dba->username) ? $dba->username : NULL;
     $dba->password = isset($dba->password) ? $dba->password : NULL;
     $drivers = PDO::getAvailableDrivers();
     $questions = array((object) array('key' => 'driver', 'label' => "- PDO Driver", 'options' => $drivers), (object) array('key' => 'host', 'label' => "- Server Host"), (object) array('key' => 'port', 'label' => "- Server Port"), (object) array('key' => 'name', 'label' => "- Database Name"), (object) array('key' => 'username', 'label' => "- Username"), (object) array('key' => 'password', 'label' => "- Password"), (object) array('key' => 'prefix', 'label' => "- Table Prefix"));
     $connectable = FALSE;
     do {
         //  do in loop
         foreach ($questions as $question) {
             //  iterate questions
             $default = $dba->{$question->key};
             //  shortcut default
             $options = isset($question->options) ? $question->options : array();
             //  realize options
             $input = Hymn_Client::getInput($question->label, $default, $options, FALSE);
             //  ask for value
             $dba->{$question->key} = $input;
             //  assign given value
         }
         $dsn = $dba->driver . ":host=" . $dba->host . ";port=" . $dba->port . ";dbname=" . $dba->name;
         //  render PDO DSN
         try {
             //  try to connect database
             if ($dbc = @new PDO($dsn, $dba->username, $dba->password)) {
                 //  connection can be established
                 $result = $dbc->query("SHOW TABLES");
                 //  query for tables
                 if (is_object($result) && is_array($result->fetchAll())) {
                     //  query has been successful
                     $connectable = TRUE;
                 }
                 //  note connectability for loop break
             }
             if (!$connectable) {
                 //  still not connectable
                 Hymn_Client::out('Database connection failed');
             }
             //  show error message
         } catch (Exception $e) {
             //  catch all exceptions
             Hymn_Client::out('Database connection error: ' . $e->getMessage());
         }
     } while (!$connectable);
     //  repeat until connectable
     $json = json_decode(file_get_contents(Hymn_Client::$fileName));
     $json->database = $dba;
     file_put_contents(Hymn_Client::$fileName, json_encode($json, JSON_PRETTY_PRINT));
 }
Beispiel #3
0
 public function run()
 {
     $key = $this->client->arguments->getArgument(0);
     $value = $this->client->arguments->getArgument(1);
     if (!strlen(trim($key))) {
         throw new InvalidArgumentException('Missing first argument "key" is missing');
     }
     $editor = new Hymn_Tool_BaseConfigEditor("config/config.ini");
     if (!$editor->hasProperty($key, FALSE)) {
         throw new InvalidArgumentException('Base config key "' . $key . '" is missing');
     }
     $current = $editor->getProperty($key, FALSE);
     if (!strlen(trim($value))) {
         $value = trim(Hymn_Client::getInput("Value for '" . $key . "'", $current, array(), FALSE));
     }
     $editor->setProperty($key, $value);
     clearstatcache();
 }
Beispiel #4
0
 public function run()
 {
     $filename = Hymn_Client::$fileName;
     #		if( !file_exists( $filename ) )
     #			throw new RuntimeException( 'File "'.$filename.'" is missing' );
     #		$config	= json_decode( file_get_contents( $filename ) );
     #		if( is_null( $config ) )
     #			throw new RuntimeException( 'Configuration file "'.$filename.'" is not valid JSON' );
     $key = $this->client->arguments->getArgument(0);
     if (!strlen(trim($key))) {
         throw new InvalidArgumentException('First argument "key" is missing');
     }
     $key = $this->client->arguments->getArgument(0);
     $value = $this->client->arguments->getArgument(1);
     $parts = explode(".", $key);
     $module = array_shift($parts);
     if (!$parts) {
         throw new InvalidArgumentException('Invalid key - must be of syntax "Module_Name.(section.)key"');
     }
     $configKey = join(".", $parts);
     if (!isset($config->modules->{$module})) {
         $config->modules->{$module} = (object) array();
     }
     if (!isset($config->modules->{$module}->config)) {
         $config->modules->{$module}->config = (object) array();
     }
     if (!isset($config->modules->{$module}->config->{$configKey})) {
         $config->modules->{$module}->config->{$configKey} = NULL;
     }
     $current = $config->modules->{$module}->config->{$configKey};
     if (!strlen(trim($value))) {
         $value = trim(Hymn_Client::getInput('Value for "' . $module . ':' . $configKey . '"', $current, array(), FALSE));
     }
     if (preg_match('/^".*"$/', $value)) {
         $value = substr($value, 1, -1);
     }
     //		if( $current === $value )
     //			throw new RuntimeException( 'No change made' );
     $config->modules->{$module}->config->{$configKey} = $value;
     file_put_contents($filename, json_encode($config, JSON_PRETTY_PRINT));
     clearstatcache();
 }
Beispiel #5
0
 public function run()
 {
     if (!Hymn_Command_Database_Test::test($this->client)) {
         return Hymn_Client::out("Database can NOT be connected.");
     }
     $force = $this->client->arguments->getOption('force');
     $verbose = $this->client->arguments->getOption('verbose');
     $quiet = $this->client->arguments->getOption('quiet');
     $prefix = $this->client->getDatabaseConfiguration('prefix');
     $dbc = $this->client->getDatabase();
     $result = $dbc->query("SHOW TABLES" . ($prefix ? " LIKE '" . $prefix . "%'" : ""));
     $tables = $result->fetchAll();
     if (!$tables) {
         if (!$quiet) {
             Hymn_Client::out("Database is empty");
         }
         return;
     }
     if (!$force) {
         if ($quiet) {
             return Hymn_Client::out("Quiet mode needs force mode (-f|--force)");
         }
         Hymn_Client::out("Database tables:");
         foreach ($tables as $table) {
             Hymn_Client::out("- " . $table[0]);
         }
         $answer = Hymn_Client::getInput("Do you really want to drop these tables?", NULL, array("y", "n"));
         if ($answer !== "y") {
             return;
         }
     }
     foreach ($tables as $table) {
         if (!$quiet && $verbose) {
             Hymn_Client::out("- Drop table '" . $table[0] . "'");
         }
         $dbc->query("DROP TABLE " . $table[0]);
     }
     if (!$quiet) {
         Hymn_Client::out("Database cleared");
     }
 }
Beispiel #6
0
 public function run()
 {
     $config = $this->client->getConfig();
     if (!isset($config->sources)) {
         $config->sources = (object) array();
     }
     $data = (object) array('key' => 'Local_Modules', 'type' => 'folder', 'path' => NULL, 'title' => NULL);
     $questions = array((object) array('key' => 'type', 'label' => "- Source type", 'options' => array("folder")), (object) array('key' => 'path', 'label' => "- Source path"), (object) array('key' => 'key', 'label' => "- Source ID"), (object) array('key' => 'title', 'label' => "- Source description"));
     $connectable = FALSE;
     do {
         foreach ($questions as $question) {
             //  iterate questions
             $default = $data->{$question->key};
             //  shortcut default value
             $options = isset($question->options) ? $question->options : array();
             //  realize options
             $input = Hymn_Client::getInput($question->label, $default, $options, FALSE);
             //  ask for value
             $data->{$question->key} = $input;
             //  assign given value
         }
         if (isset($config->sources->{$data->key})) {
             Hymn_Client::out('Error: Source with ID "' . $data->key . '" is already registered.');
         } else {
             if ($data->type === "folder" && !file_exists($data->path)) {
                 Hymn_Client::out('Error: Path to module library source is not existing.');
             } else {
                 $connectable = TRUE;
             }
         }
         //  note connectability for loop break
     } while (!$connectable);
     //  repeat until connectable
     $data->title = $data->title ? $data->title : $data->key;
     $config->sources->{$data->key} = (object) array('active' => TRUE, 'title' => $data->title, 'type' => $data->type, 'path' => $data->path);
     $json = json_decode(file_get_contents(Hymn_Client::$fileName));
     $json->sources = $config->sources;
     file_put_contents(Hymn_Client::$fileName, json_encode($json, JSON_PRETTY_PRINT));
 }
Beispiel #7
0
 public function run()
 {
     $data = array();
     Hymn_Client::out("Please enter application information:");
     $title = Hymn_Client::getInput("- Application title", "My Project", NULL, FALSE);
     $uri = Hymn_Client::getInput("- Folder Path", getcwd() . '/', NULL, FALSE);
     $protocol = Hymn_Client::getInput("- HTTP Protocol", "http://", NULL, FALSE);
     $host = Hymn_Client::getInput("- HTTP Host", "example.com", NULL, FALSE);
     $path = Hymn_Client::getInput("- HTTP Path", "/", NULL, FALSE);
     $data['application'] = (object) array('title' => $title, 'url' => $protocol . $host . "/" . ltrim($path, "/"), 'uri' => $uri);
     $data['library'] = (object) array();
     $data['sources'] = (object) array();
     $data['modules'] = (object) array();
     Hymn_Client::out("");
     Hymn_Client::out("Please enter database information:");
     $data['database'] = (object) array('driver' => Hymn_Client::getInput("- PDO Driver", "mysql", NULL, FALSE), 'host' => Hymn_Client::getInput("- Host", "localhost", NULL, FALSE), 'port' => Hymn_Client::getInput("- Port", "3306", NULL, FALSE), 'username' => Hymn_Client::getInput("- Username", "my_db_user", NULL, FALSE), 'password' => Hymn_Client::getInput("- Password", "my_db_password", NULL, FALSE), 'name' => Hymn_Client::getInput("- Name", "my_db_name", NULL, FALSE), 'prefix' => Hymn_Client::getInput("- Table Prefix", NULL, NULL, FALSE));
     Hymn_Client::out("");
     Hymn_Client::out("Please enter system information:");
     $data['system'] = (object) array('user' => Hymn_Client::getInput("- System User", get_current_user(), NULL, FALSE), 'group' => Hymn_Client::getInput("- System Group", "www-data", NULL, FALSE));
     file_put_contents(Hymn_Client::$fileName, json_encode($data, JSON_PRETTY_PRINT));
     Hymn_Client::out("Configuration file " . Hymn_Client::$fileName . " has been created.");
 }
Beispiel #8
0
 /**
  *	Configures an installed module by several steps:
  *	1. set version attribtes: install type, source and date
  *	2. look for mandatory but empty config pairs in original module
  *	3. get value for these missing pairs from console if also not set in hymn file
  *	4. combine values from hymn file and console input and apply to module file
  *	@access		public
  *	@param		object		$module			Data object of module to install
  *	@param		boolean		$verbose		Flag: be verbose
  *	@param		boolean		$dry			Flag: dry run move - simulation only
  *	@return		void
  */
 public function configure($module, $verbose = FALSE, $dry = FALSE)
 {
     $source = $module->path . 'module.xml';
     $target = $this->app->uri . 'config/modules/' . $module->id . '.xml';
     if (!$dry) {
         Hymn_Module_Files::createPath(dirname($target));
         @copy($source, $target);
     } else {
         $target = $source;
     }
     $xml = file_get_contents($target);
     $xml = new Hymn_Tool_XmlElement($xml);
     $type = isset($this->app->type) ? $this->app->type : 1;
     $xml->version->setAttribute('install-type', $type);
     $xml->version->setAttribute('install-source', $module->sourceId);
     $xml->version->setAttribute('install-date', date("c"));
     $config = (object) array();
     //  prepare empty hymn module config
     if (isset($this->config->modules->{$module->id}->config)) {
         //  module config is set in hymn file
         $config = $this->config->modules->{$module->id}->config;
     }
     //  get module config from hymn file
     foreach ($xml->config as $nr => $node) {
         //  iterate original module config pairs
         $key = (string) $node['name'];
         //  shortcut config pair key
         if ($module->config[$key]->mandatory == "yes") {
             //  config pair is mandatory
             if ($module->config[$key]->type !== "boolean") {
                 //  ... and not of type boolean
                 if (!strlen(trim($module->config[$key]->value))) {
                     //  ... and has no value
                     if (!isset($config->{$key})) {
                         //  ... and is not set in hymn file
                         $message = "  … configure '" . $key . "'";
                         //  render console input label
                         $values = $module->config[$key]->values;
                         //  get suggested values if set
                         $value = Hymn_Client::getInput($message, NULL, $values, FALSE);
                         //  get new value from console
                         $config->{$key} = $value;
                     }
                 }
             }
         }
         if (isset($config->{$key})) {
             //  a config value has been set
             $dom = dom_import_simplexml($node);
             //  import DOM node of module file
             $dom->nodeValue = $config->{$key};
             //  set new value on DOM node
             if ($verbose && !$this->quiet) {
                 //  verbose mode is on
                 Hymn_Client::out("  … configured " . $key);
             }
             //  inform about configures config pair
         }
     }
     if (!$dry) {
         $xml->saveXml($target);
         //  save changed DOM to module file
         @unlink($this->app->uri . 'config/modules.cache.serial');
         //  remove modules cache file
     }
 }
Beispiel #9
0
 public function setupDatabaseConnection($force = FALSE)
 {
     if ($this->dbc) {
         return;
     }
     //		$this->dbc			= NULL;
     $usesGlobalDbAccess = isset($this->config->database) && $this->config->database;
     $usesDatabaseModule = isset($this->config->modules->Resource_Database->config);
     if ($usesGlobalDbAccess) {
         $this->dba = $this->config->database;
     } else {
         if ($usesDatabaseModule) {
             $config = array();
             foreach ($this->config->modules->Resource_Database->config as $key => $value) {
                 $config[preg_replace("/^access\\./", "", $key)] = $value;
             }
             $this->dba = (object) $config;
         }
     }
     if (empty($this->dba)) {
         if ($force) {
             throw new RuntimeException('Database access needed but not configured');
         }
         return;
     }
     $this->dba->driver = isset($this->dba->driver) ? $this->dba->driver : "mysql";
     $this->dba->host = isset($this->dba->host) ? $this->dba->host : "localhost";
     $this->dba->port = isset($this->dba->port) ? $this->dba->port : "3306";
     $this->dba->name = isset($this->dba->name) ? $this->dba->name : NULL;
     $this->dba->prefix = isset($this->dba->prefix) ? $this->dba->prefix : NULL;
     $this->dba->username = isset($this->dba->username) ? $this->dba->username : NULL;
     $this->dba->password = isset($this->dba->password) ? $this->dba->password : NULL;
     if (!in_array($this->dba->driver, PDO::getAvailableDrivers())) {
         throw new RuntimeException('PDO driver "' . $this->dba->driver . '" is not available');
     }
     while (empty($this->dba->name)) {
         $this->dba->name = Hymn_Client::getInput("Database Name:");
     }
     while (empty($this->dba->username)) {
         $this->dba->username = Hymn_Client::getInput("Database Username:"******"Database Password:"******"Table Prefix:");
     }
     if (!isset($this->config->modules->Resource_Database)) {
         $this->config->modules->Resource_Database = (object) array();
     }
     $this->config->modules->Resource_Database->config = (object) array();
     foreach ($this->dba as $key => $value) {
         $this->config->modules->Resource_Database->config->{"access." . $key} = $value;
     }
     if ($this->isLiveCopy) {
         return;
     }
     if (strtolower($this->dba->driver) !== "mysql") {
         throw new OutOfRangeException('PDO driver "' . $this->dba->driver . '" is not supported at the moment');
     }
     $dsn = $this->dba->driver . ':' . implode(";", array("host=" . $this->dba->host, "port=" . $this->dba->port, "dbname=" . $this->dba->name));
     $this->dbc = new PDO($dsn, $this->dba->username, $this->dba->password);
     $this->dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 }