/** * Support method for changeConfig; act on a single file. * * @param string $configName Configuration to modify. * @param array $settings Settings to change. * @param bool $replace Should we replace the existing config entirely * (as opposed to extending it with new settings)? * * @return void */ protected function changeConfigFile($configName, $settings, $replace = false) { $file = $configName . '.ini'; $local = ConfigLocator::getLocalConfigPath($file, null, true); if (file_exists($local)) { // File exists? Make a backup! copy($local, $local . '.bak'); } else { // File doesn't exist? Make a baseline version. copy(ConfigLocator::getBaseConfigPath($file), $local); } // If we're replacing the existing file, wipe it out now: if ($replace) { file_put_contents($local, ''); } $writer = new ConfigWriter($local); foreach ($settings as $section => $contents) { foreach ($contents as $key => $value) { $writer->set($section, $key, $value); } } $writer->save(); }
/** * Save a modified configuration file. * * @param string $filename Name of config file to write (contents will be * pulled from current state of object properties). * * @throws FileAccessException * @return void */ protected function saveModifiedConfig($filename) { if (null === $this->newDir) { // skip write if no destination return; } // If we're doing an in-place upgrade, and the source file is empty, // there is no point in upgrading anything (the file doesn't exist). if (empty($this->oldConfigs[$filename]) && $this->inPlaceUpgrade) { // Special case: if we set up custom permissions, we need to // write the file even if it didn't previously exist. if (!$this->permissionsModified || $filename !== 'permissions.ini') { return; } } // If target file already exists, back it up: $outfile = $this->newDir . '/' . $filename; $bakfile = $outfile . '.bak.' . time(); if (!copy($outfile, $bakfile)) { throw new FileAccessException("Error: Could not copy {$outfile} to {$bakfile}."); } $writer = new ConfigWriter($outfile, $this->newConfigs[$filename], $this->comments[$filename]); if (!$writer->save()) { throw new FileAccessException("Error: Problem writing to {$outfile}."); } }
/** * Convert hash algorithms * Expected parameters: oldmethod:oldkey (or none) newmethod:newkey * * @return \Zend\Console\Response */ public function switchdbhashAction() { // Validate command line arguments: $argv = $this->consoleOpts->getRemainingArgs(); if (count($argv) < 1) { Console::writeLine('Expected parameters: newmethod [newkey]'); return $this->getFailureResponse(); } // Pull existing encryption settings from the configuration: $config = $this->getConfig(); if (!isset($config->Authentication->encrypt_ils_password) || !isset($config->Authentication->ils_encryption_key) || !$config->Authentication->encrypt_ils_password) { $oldhash = 'none'; $oldkey = null; } else { $oldhash = isset($config->Authentication->ils_encryption_algo) ? $config->Authentication->ils_encryption_algo : 'blowfish'; $oldkey = $config->Authentication->ils_encryption_key; } // Pull new encryption settings from arguments: $newhash = $argv[0]; $newkey = isset($argv[1]) ? $argv[1] : $oldkey; // No key specified AND no key on file = fatal error: if ($newkey === null) { Console::writeLine('Please specify a key as the second parameter.'); return $this->getFailureResponse(); } // If no changes were requested, abort early: if ($oldkey == $newkey && $oldhash == $newhash) { Console::writeLine('No changes requested -- no action needed.'); return $this->getSuccessResponse(); } // Initialize Mcrypt first, so we can catch any illegal algorithms before // making any changes: try { if ($oldhash != 'none') { $oldCrypt = new Mcrypt(['algorithm' => $oldhash]); } $newCrypt = new Mcrypt(['algorithm' => $newhash]); } catch (\Exception $e) { Console::writeLine($e->getMessage()); return $this->getFailureResponse(); } // Next update the config file, so if we are unable to write the file, // we don't go ahead and make unwanted changes to the database: $configPath = ConfigLocator::getLocalConfigPath('config.ini', null, true); Console::writeLine("\tUpdating {$configPath}..."); $writer = new ConfigWriter($configPath); $writer->set('Authentication', 'encrypt_ils_password', true); $writer->set('Authentication', 'ils_encryption_algo', $newhash); $writer->set('Authentication', 'ils_encryption_key', $newkey); if (!$writer->save()) { Console::writeLine("\tWrite failed!"); return $this->getFailureResponse(); } // Now do the database rewrite: $userTable = $this->getServiceLocator()->get('VuFind\\DbTablePluginManager')->get('User'); $users = $userTable->select(function ($select) { $select->where->isNotNull('cat_username'); }); Console::writeLine("\tConverting hashes for " . count($users) . ' user(s).'); foreach ($users as $row) { $pass = null; if ($oldhash != 'none' && isset($row['cat_pass_enc'])) { $oldcipher = new BlockCipher($oldCrypt); $oldcipher->setKey($oldkey); $pass = $oldcipher->decrypt($row['cat_pass_enc']); } else { $pass = $row['cat_password']; } $newcipher = new BlockCipher($newCrypt); $newcipher->setKey($newkey); $row['cat_password'] = null; $row['cat_pass_enc'] = $newcipher->encrypt($pass); $row->save(); } // If we got this far, all went well! Console::writeLine("\tFinished."); return $this->getSuccessResponse(); }
/** * Disable auto-configuration. * * @return mixed */ public function doneAction() { $config = ConfigLocator::getLocalConfigPath('config.ini', null, true); $writer = new ConfigWriter($config); $writer->set('System', 'autoConfigure', 0); if (!$writer->save()) { return $this->forwardTo('Install', 'fixbasicconfig'); } return $this->createViewModel(['configDir' => dirname($config)]); }
/** * Save a modified configuration file. * * @param string $filename Name of config file to write (contents will be * pulled from current state of object properties). * * @throws FileAccessException * @return void */ protected function saveModifiedConfig($filename) { $outfile = $this->newDir . '/' . $filename; $writer = new ConfigWriter($outfile, $this->newConfigs[$filename], $this->comments[$filename]); if (!$writer->save()) { throw new FileAccessException("Error: Problem writing to {$outfile}."); } }