/setup/setup.php
PHP | 175 lines | 134 code | 16 blank | 25 comment | 25 complexity | e7551a3cff3b3311357262fa7236e626 MD5 | raw file
Possible License(s): LGPL-2.1
- #!/usr/bin/env php
- <?php
- echo <<< WELCOME
- Welcome to the WYF Framework
- ============================
- This setup guide would help you get up and running with the WYF framework
- as fast as possible.
- WELCOME;
- $name = get_response("What is the name of your application", null, null, true);
- $host = get_response("Where is your application's database hosted", 'localhost', null, true);
- $port = get_response("What is the port of this database", '5432', null, true);
- $username = get_response("What is the database username", null, null, true);
- $password = get_response("What is the password for the database");
- $database = get_response("What is the name of your application's database (please ensure that the database exists)", null, null, true);
- $home = get_response("Where is your application residing", getcwd(), null, null, true) . "/";
- $prefix = get_response("What is the prefix of your application (Enter 'no prefix' if you do not want a prefix)", basename($home));
- echo "\nSetting up the application ...\n";
- mkdir2($home . 'app');
- mkdir2($home . 'app/cache');
- mkdir2($home . 'app/cache/code');
- mkdir2($home . 'app/cache/menus');
- mkdir2($home . 'app/cache/template_compile');
- mkdir2($home . 'app/logs');
- mkdir2($home . 'app/modules');
- mkdir2($home . 'app/modules/system');
- mkdir2($home . 'app/temp');
- mkdir2($home . 'app/themes');
- mkdir2($home . 'app/uploads');
- echo shell_exec("cp -r lib/setup/factory/* app");
- echo shell_exec("cp -r lib/setup/htaccess .htaccess");
- $system = <<< SYSTEM
- <?php
- \$redirect_path = "lib/modules/system";
- \$package_name = "System";
- \$package_path = "system";
- \$package_schema = "system";
- SYSTEM;
- file_put_contents($home . 'app/modules/system/package_redirect.php', $system);
- $index = <<< "INDEX"
- <?php
- /**
- * Generated by WYF setup script.
- *
- */
- require "lib/entry.php";
- INDEX;
- file_put_contents($home . 'index.php', $index);
- $config = <<< "CONFIG"
- <?php
- \$selected = "main";
- \$config = array(
- 'home' => "$home",
- 'prefix' => "/$prefix",
- 'name' => "$name",
- 'db' => array(
- "main" => array(
- 'driver' => 'postgresql',
- 'user' => '$username',
- 'host' => '$host',
- 'password' => '$password',
- 'name' => '$database',
- 'port' => '$port'
- )
- ),
- 'cache' => array(
- 'method' => 'file',
- 'models' => true
- ),
- 'audit_trails' => false,
- 'theme' => 'default'
- );
- CONFIG;
- file_put_contents($home . 'app/config.php', $config);
- file_put_contents($home . 'app/includes.php', "<?php\n");
- file_put_contents($home . 'app/bootstrap.php', "<?php\n");
- require "lib/wyf_bootstrap.php";
- echo "\nSetting up the database ...\n";
- Db::query(file_get_contents("lib/setup/schema.sql"));
- $username = get_response("Enter a name for the superuser account", 'super', null, true);
- $email = get_response('Provide your email address', null, null, true);
- Db::query("INSERT INTO system.roles(role_id, role_name) VALUES(1, 'Super User')");
- Db::query(
- sprintf(
- "INSERT INTO system.users
- (user_name, password, role_id, first_name, last_name, user_status, email)
- VALUES
- ('%s', '%s', 1, 'Super', 'User', 2, '%s')",
- Db::escape($username),
- Db::escape($username),
- Db::escape($email)
- )
- );
- echo "\nDone! Happy programming ;)\n\n";
- /**
- * A function for getting answers to questions from users interractively.
- * @param $question The question you want to ask
- * @param $answers An array of possible answers that this function should validate
- * @param $default The default answer this function should assume for the user.
- * @param $notNull Is the answer required
- */
- function get_response($question, $default=null, $answers=null, $notNull = false)
- {
- echo $question;
- if(is_array($answers))
- {
- if(count($answers) > 0) echo " (" . implode("/", $answers) . ")";
- }
-
- echo " [$default]: ";
- $response = str_replace(array("\n", "\r"),array("",""),fgets(STDIN));
- if($response == "" && $notNull === true && $default == '')
- {
- echo "A value is required.\n";
- return get_response($question, $answers, $default, $notNull);
- }
- else if($response == "" && $notNull === true && $default != '')
- {
- return $default;
- }
- else if($response == "")
- {
- return $default;
- }
- else
- {
- if(count($answers) == 0)
- {
- return $response;
- }
- foreach($answers as $answer)
- {
- if(strtolower($answer) == strtolower($response))
- {
- return strtolower($answer);
- }
- }
- echo "Please provide a valid answer.\n";
- return get_response($question, $answers, $default, $notNull);
- }
- }
- function mkdir2($path)
- {
- echo("Creating directory $path\n");
- if(!\is_writable(dirname($path)))
- {
- fputs(STDERR, "You do not have permissions to create the $path directory\n");
- $path = false;
- }
- else if(\is_dir($path))
- {
- echo ("Directory $path already exists. I will skip creating it ...\n");
- }
- else
- {
- mkdir($path);
- }
- return $path;
- }