/** * Allows access to a directory on the server. * * Access to the directory is restricted using a .htaccess file. * * If there is already a .htaccess file in the directory, it is backed up first. * * @param string $directory The name of the directory, relative to PROJECT_ROOT. */ public static function allow_access_to_directory($directory) { $validator = new FileSystem_ExistingDirectoryRelativeToProjectRootValidator(); if ($validator->validate($directory)) { $abs_directory = PROJECT_ROOT . DIRECTORY_SEPARATOR . $directory; $htaccess_file_name = $abs_directory . DIRECTORY_SEPARATOR . '.htaccess'; /* * Back up the old file. */ if (file_exists($htaccess_file_name)) { $back_up_htaccess_file_name = $htaccess_file_name . '_' . date('U'); rename($htaccess_file_name, $back_up_htaccess_file_name); } $copyright_holder = HaddockProjectOrganisation_ProjectInformationHelper::get_copyright_holder(); $date = date('Y-m-d'); if ($fh = fopen($htaccess_file_name, 'w')) { $htaccess_file_content = <<<HTA # Allowing access to {$directory} # (c) {$date}, {$copyright_holder} Order Allow,Deny Allow from all HTA; fwrite($fh, $htaccess_file_content); fclose($fh); } } }
/** * Gets the name of the directory that the user wants to be * the target of the script that implements this class. * * The directory can be set with a command line, if not * then the user is asked. * * @return string The name of the directory. */ protected function get_directory() { $validator = new FileSystem_ExistingDirectoryRelativeToProjectRootValidator(); if ($this->has_arg('directory')) { $directory = $this->get_arg('directory'); try { $validator->validate($directory); } catch (InputValidation_InvalidInputException $e) { fwrite(STDERR, $e->getMessage() . PHP_EOL); exit; } } else { $directory = CLIScripts_UserInterrogationHelper::get_validated_input('Please enter the name of the directory:' . PHP_EOL, $validator); } return $directory; }