Permalink
Browse files

Initial commit

  • Loading branch information...
mcrittenden
mcrittenden committed Feb 12, 2010
0 parents commit 2b60efe6f7f92d9ceef01762518ff6adb448d7b3
Showing with 352 additions and 0 deletions.
  1. +22 −0 LICENSE.txt
  2. +28 −0 README.txt
  3. +4 −0 assets/install.sql
  4. +57 −0 config/database.php
  5. +79 −0 includes/core_class.php
  6. +47 −0 includes/database_class.php
  7. +115 −0 index.php
@@ -0,0 +1,22 @@
+------------------
+ MIT LICENSE
+------------------
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
@@ -0,0 +1,28 @@
+CI Installer is a staring point for giving your downloadable CodeIgniter
+app an installer.
+
+If you're making a CI app that is designed to be downloaded and self-hosted,
+then you'll need an easy way for the user to get the DB structure and config
+in place. That's what this is for.
+
+NOTE: This is to be used as a starting point. You will have to customize it a
+little to make it perfect for your app, and you might have to dig into the code
+a little bit. It's pretty simple, so if you have a basic understanding of PHP
+you shouldn't have trouble.
+
+General Instructions:
+- Download CI Installer, rename the folder to 'install' and put it in the root
+directory of your CI install (as a sibling to the /system folder).
+- Make a SQL dump of your desired database structure and data and paste it into
+assets/install.sql
+- Open up index.php and change "Your App" in the <title> to your app's name.
+- If you move your CI application folder to a sibling of your system folder
+instead of a child (this is common), then do a find/replace to replace
+'system/application' with 'application' in all the files.
+- In index.php, around line 37 change 'welcome' to the URL of the page
+(in CodeIgniter) that you want the user to be redirected to after installing.
+- Now try visiting http://yoursite.com/install and see how it goes.
+
+If you have problems or have recommendations, please file an issue at
+http://bitbucket.org/mcrittenden/ci-installer/
+
@@ -0,0 +1,4 @@
+--
+-- Paste your SQL dump into this file
+--
+
@@ -0,0 +1,57 @@
+<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
+/*
+| -------------------------------------------------------------------
+| DATABASE CONNECTIVITY SETTINGS
+| -------------------------------------------------------------------
+| This file will contain the settings needed to access your database.
+|
+| For complete instructions please consult the "Database Connection"
+| page of the User Guide.
+|
+| -------------------------------------------------------------------
+| EXPLANATION OF VARIABLES
+| -------------------------------------------------------------------
+|
+| ['hostname'] The hostname of your database server.
+| ['username'] The username used to connect to the database
+| ['password'] The password used to connect to the database
+| ['database'] The name of the database you want to connect to
+| ['dbdriver'] The database type. ie: mysql. Currently supported:
+ mysql, mysqli, postgre, odbc, mssql, sqlite, oci8
+| ['dbprefix'] You can add an optional prefix, which will be added
+| to the table name when using the Active Record class
+| ['pconnect'] TRUE/FALSE - Whether to use a persistent connection
+| ['db_debug'] TRUE/FALSE - Whether database errors should be displayed.
+| ['cache_on'] TRUE/FALSE - Enables/disables query caching
+| ['cachedir'] The path to the folder where cache files should be stored
+| ['char_set'] The character set used in communicating with the database
+| ['dbcollat'] The character collation used in communicating with the database
+|
+| The $active_group variable lets you choose which connection group to
+| make active. By default there is only one group (the "default" group).
+|
+| The $active_record variables lets you determine whether or not to load
+| the active record class
+*/
+
+// The following values will probably need to be changed.
+$db['default']['username'] = "%USERNAME%";
+$db['default']['password'] = "%PASSWORD%";
+$db['default']['database'] = "%DATABASE%";
+
+// The following values can probably stay the same.
+$db['default']['hostname'] = "%HOSTNAME%";
+$db['default']['dbdriver'] = "mysql";
+$db['default']['dbprefix'] = "";
+$db['default']['pconnect'] = TRUE;
+$db['default']['db_debug'] = TRUE;
+$db['default']['cache_on'] = FALSE;
+$db['default']['cachedir'] = "";
+$db['default']['char_set'] = "utf8";
+$db['default']['dbcollat'] = "utf8_general_ci";
+
+$active_group = "default";
+$active_record = TRUE;
+
+/* End of file database.php */
+/* Location: ./system/application/config/database.php */
@@ -0,0 +1,79 @@
+<?php
+
+class Core {
+
+ // Function to validate the post data
+ function validate_post($data)
+ {
+ // Counter variable
+ $counter = 0;
+
+ // Validate the hostname
+ if(isset($data['hostname']) AND !empty($data['hostname'])) {
+ $counter++;
+ }
+ // Validate the username
+ if(isset($data['username']) AND !empty($data['username'])) {
+ $counter++;
+ }
+ // Validate the password
+ if(isset($data['password']) AND !empty($data['password'])) {
+ // pass
+ }
+ // Validate the database
+ if(isset($data['database']) AND !empty($data['database'])) {
+ $counter++;
+ }
+
+ // Check if all the required fields have been entered
+ if($counter == '3') {
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+
+ // Function to show an error
+ function show_message($type,$message) {
+ return $message;
+ }
+
+ // Function to write the config file
+ function write_config($data) {
+
+ // Config path
+ $template_path = 'config/database.php';
+ $output_path = '../system/application/config/database.php';
+
+ // Open the file
+ $database_file = file_get_contents($template_path);
+
+ $new = str_replace("%HOSTNAME%",$data['hostname'],$database_file);
+ $new = str_replace("%USERNAME%",$data['username'],$new);
+ $new = str_replace("%PASSWORD%",$data['password'],$new);
+ $new = str_replace("%DATABASE%",$data['database'],$new);
+
+ // Write the new database.php file
+ $handle = fopen($output_path,'w+');
+
+ // Chmod the file, in case the user forgot
+ @chmod($output_path,0777);
+
+ // Verify file permissions
+ if(is_writable($output_path)) {
+
+ // Write the file
+ if(fwrite($handle,$new)) {
+ return true;
+ } else {
+ return false;
+ }
+
+ } else {
+ return false;
+ }
+ }
+}
+?>
+
@@ -0,0 +1,47 @@
+<?php
+
+class Database {
+
+ // Function to the database and tables and fill them with the default data
+ function create_database($data)
+ {
+ // Connect to the database
+ $mysqli = new mysqli($data['hostname'],$data['username'],$data['password'],'');
+
+ // Check for errors
+ if(mysqli_connect_errno())
+ return false;
+
+ // Create the prepared statement
+ $mysqli->query("CREATE DATABASE IF NOT EXISTS ".$data['database']);
+
+ // Close the connection
+ $mysqli->close();
+
+ return true;
+ }
+
+ // Function to create the tables and fill them with the default data
+ function create_tables($data)
+ {
+ // Connect to the database
+ $mysqli = new mysqli($data['hostname'],$data['username'],$data['password'],$data['database']);
+
+ // Check for errors
+ if(mysqli_connect_errno())
+ return false;
+
+ // Open the default SQL file
+ $query = file_get_contents('assets/install.sql');
+
+ // Execute a multi query
+ $mysqli->multi_query($query);
+
+ // Close the connection
+ $mysqli->close();
+
+ return true;
+ }
+}
+?>
+
115 index.php
@@ -0,0 +1,115 @@
+<?php
+
+error_reporting(E_NONE);
+
+$db_config_path = '../system/application/config/database.php';
+
+// Only load the classes in case the user submitted the form
+if($_POST) {
+
+ // Load the classes and create the new objects
+ require_once('includes/core_class.php');
+ require_once('includes/database_class.php');
+
+ $core = new Core();
+ $database = new Database();
+
+
+ // Validate the post data
+ if($core->validate_post($_POST) == true)
+ {
+
+ // First create the database, then create tables, then write config file
+ if($database->create_database($_POST) == false) {
+ $message = $core->show_message('error',"The database could not be created, please verify your settings.");
+ } else if ($database->create_tables($_POST) == false) {
+ $message = $core->show_message('error',"The database tables could not be created, please verify your settings.");
+ } else if ($core->write_config($_POST) == false) {
+ $message = $core->show_message('error',"The database configuration file could not be written, please chmod /application/config/database.php file to 777");
+ }
+
+ // If no errors, redirect to registration page
+ if(!isset($message)) {
+ $redir = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
+ $redir .= "://".$_SERVER['HTTP_HOST'];
+ $redir .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
+ $redir = str_replace('install/','',$redir);
+ header( 'Location: ' . $redir . 'welcome' ) ;
+ }
+
+ }
+ else {
+ $message = $core->show_message('error','Not all fields have been filled in correctly. The host, username, password, and database name are required.');
+ }
+}
+
+?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+
+ <title>Install | Your App</title>
+
+ <style type="text/css">
+ body {
+ font-size: 75%;
+ font-family: Helvetica,Arial,sans-serif;
+ width: 300px;
+ margin: 0 auto;
+ }
+ input, label {
+ display: block;
+ font-size: 18px;
+ margin: 0;
+ padding: 0;
+ }
+ label {
+ margin-top: 20px;
+ }
+ input.input_text {
+ width: 270px;
+ }
+ input#submit {
+ margin: 25px auto 0;
+ font-size: 25px;
+ }
+ fieldset {
+ padding: 15px;
+ }
+ legend {
+ font-size: 18px;
+ font-weight: bold;
+ }
+ .error {
+ background: #ffd1d1;
+ border: 1px solid #ff5858;
+ padding: 4px;
+ }
+ </style>
+ </head>
+ <body>
+
+ <h1>Install</h1>
+ <?php if(is_writable($db_config_path)):?>
+
+ <?php if(isset($message)) {echo '<p class="error">' . $message . '</p>';}?>
+
+ <form id="install_form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
+ <fieldset>
+ <legend>Database settings</legend>
+ <label for="hostname">Hostname</label><input type="text" id="hostname" value="localhost" class="input_text" name="hostname" />
+ <label for="username">Username</label><input type="text" id="username" class="input_text" name="username" />
+ <label for="password">Password</label><input type="password" id="password" class="input_text" name="password" />
+ <label for="database">Database Name</label><input type="text" id="database" class="input_text" name="database" />
+ <input type="submit" value="Install" id="submit" />
+ </fieldset>
+ </form>
+
+ <?php else: ?>
+ <p class="error">Please make the /system/application/config/database.php file writable. <strong>Example</strong>:<br /><br /><code>chmod 777 system/application/config/database.php</code></p>
+ <?php endif; ?>
+
+ </body>
+</html>
+

0 comments on commit 2b60efe

Please sign in to comment.