ThinkPHP installation module for reference only

Source: Internet
Author: User
Tags valid email address

<? Php
/**
* Installation Module
*/
Class IndexAction extends Action {

// Installation Interface
Public function _ initialize (){

// Detect right enter
If ($ _ SESSION ['right _ enter']! = 1 ){
Echo 'the system does not support session! ';
Exit ();
}
}

// License Agreement
Public function index (){

// Determine whether install. lock exists. If yes, the page will jump to the front-end homepage.
$ File = './Public/Install/install. lock ';
If (file_exists ($ file )){
$ This-> redirect ('home/Index/Index ');
}

If ($ _ POST ['startinstall']) {
If ($ _ POST ['Union ']) {
$ _ SESSION ['join'] = 1;
} Else {
$ _ SESSION ['join'] = 0;
}
$ This-> redirect ('Install/Index/check ');
} Else {
$ This-> display ();
}
}

// Check the server environment
Public function check (){

// Determine whether install. lock exists. If yes, the page will jump to the front-end homepage.
$ File = './Public/Install/install. lock ';
If (file_exists ($ file )){
$ This-> redirect ('home/Index/Index ');
}

$ Error = true;
// Check directories and files
$ Check_file = array (
'./Uploads ',
'./Uploads/LocalItems ',
'./Public ',
'./Public/install ',
'./Public/statics ',
'./App/conf ',
'./App/Conf/config. php ',
'./App/Conf/home. php ',
'./App/Conf/home ',
'./App/Conf/Home/config. php ',
'./App/runtime ',
'./App/Runtime/DataBackup ',
);
$ Error_msg = array ();
Foreach ($ check_file as $ file ){
// Check whether the file exists
If (! File_exists ($ file )){
$ Error_msg [] = $ file. 'does not exist! ';
$ Error = false;
Continue;
}
If (is_dir ($ file )){
// Check whether the directory is writable
$ File_test = @ fopen ($ file. '/test.txt', 'w ');
If (! $ File_test ){
$ Error_msg [] = $ file. "cannot be written! ";
$ Error = false;
}
@ Fclose ($ file_test );
@ Unlink ($ file. '/test.txt ');
} Else {
// Check whether the file can be written
If (! Is_writeable ($ file )){
$ Error_msg [] = $ file. "cannot be written! ";
$ Error = false;
}
}
}

// Check whether gd is supported
If (! Function_exists ("gd_info ")){
$ Error_msg [] = "the system does not support gd! ";
$ Error = false;
}

// Check whether curl is supported
If (! Function_exists ("curl_getinfo ")){
$ Error_msg [] = "the system does not support curl! ";
$ Error = false;
}

If (! $ Error ){
$ This-> assign ('error _ msg ', $ error_msg );
$ This-> display ();
} Else {
$ This-> redirect ('Install/Index/setconf ');
}
}

// Enter the configuration information
Public function setconf (){

// Determine whether install. lock exists. If yes, the page will jump to the front-end homepage.
$ File = './Public/Install/install. lock ';
If (file_exists ($ file )){
$ This-> redirect ('home/Index/Index ');
}

// Default configuration information
$ This-> assign ('db _ host', 'localhost ');
$ This-> assign ('db _ port', '123 ');
$ This-> assign ('db _ user', 'root ');
$ This-> assign ('db _ pwd ','');
$ This-> assign ('db _ name', 'jdcms ');
$ This-> assign ('db _ prefix', 'jd _');
$ This-> assign ('user _ name ','');
$ This-> assign ('Password ','');
$ This-> assign ('repassword ','');
$ This-> assign ('email ','');

If (isset ($ _ POST ['edit']) {

Foreach ($ _ POST as $ key => $ val ){
$ This-> assign ($ key, $ val );
}

Extract ($ _ POST );
$ Web_path = trim ($ web_path );
$ Db_host = trim ($ db_host );
$ Db_port = trim ($ db_port );
$ Db_user = trim ($ db_user );
$ Db_pwd = trim ($ db_pwd );
$ Db_name = trim ($ db_name );
$ Db_prefix = trim ($ db_prefix );
$ User_name = trim ($ user_name );
$ Password = trim ($ password );
$ Repassword = trim ($ repassword );
$ Email = trim ($ email );

// Check whether the information is filled
If (! $ Db_host |! $ Db_port |! $ Db_user |! $ Db_name |! $ Db_prefix |! $ User_name |! $ Password |! $ Repassword |! $ Email ){
$ This-> assign ('error _ msg ',' Fill in the complete configuration information! ');
$ This-> display ();
Exit;
}

If ($ user_name = 'admin '){
$ This-> assign ('error _ msg ', 'admin account cannot be set admin ');
$ This-> display ();
Exit;
}

If (strlen ($ password) <6 | strlen ($ password)> 20 ){
$ This-> assign ('error _ msg ', 'password should be between 6-20 bits ');
$ This-> display ();
Exit;
}

// Check whether the two passwords are consistent
If ($ password! = $ Repassword ){
$ This-> assign ('error _ msg ',' the two passwords are inconsistent! ');
$ This-> display ();
Exit;
}

// Check the email format
If (! $ This-> is_email ($ email )){
$ This-> assign ('error _ msg ',' is not a valid email address! ');
$ This-> display ();
Exit;
}

// Check the database connection
$ Conn = @ mysql_connect ($ db_host, $ db_user, $ db_pwd );
If (! $ Conn ){
$ This-> assign ('error _ msg ',' database connection failed. Please check the database configuration information! ');
$ This-> display ();
Exit;
}

// Check whether the database exists. If the database does not exist, create a database.
If (! @ Mysql_select_db ($ db_name )){
$ SQL = "CREATE database'". $ db_name. "'default CHARACTER SET utf8 COLLATE utf8_general_ci ";
Mysql_query ($ SQL );
}

// Check whether the database is successfully created
If (! @ Mysql_select_db ($ db_name )){
$ This-> assign ('error _ msg ',' database creation failed! ');
$ This-> display ();
Exit;
}

$ Login_key = str_rand ();

// Save the configuration to config. php
$ Config = array (
'Web _ path' => $ web_path,
'Login _ key' => $ login_key,
'Db _ host' => $ db_host,
'Db _ port' => $ db_port,
'Db _ user' => $ db_user,
'Db _ pwd' => $ db_pwd,
'Db _ name' => $ db_name,
'Db _ prefix' => $ db_prefix,
);
$ This-> updateconfig ($ config );

// Import the SQL script
$ Sqls = $ this-> get_ SQL ('./Public/SQL/install. SQL ');
$ Conn = @ mysql_connect ($ db_host, $ db_user, $ db_pwd );
Mysql_select_db ($ db_name );

Foreach ($ sqls as $ SQL ){
// Replace the table prefix
$ SQL = str_replace ('jd _ ', $ db_prefix, $ SQL );
$ Result = mysql_query ($ SQL, $ conn );
If (@ mysql_affected_rows ($ conn) <0 ){
$ This-> assign ('error _ msg ', 'table creation failed ');
$ This-> display ();
Exit;
}
}

$ Domain = $ config [cms_domain] = $ config [site_domain] = 'HTTP: // '. $ _ SERVER ['HTTP _ host']. $ web_path;
$ This-> updateconfig ($ config );

// Add an Administrator Account
$ Password = md5 ($ password );
$ Add_time = time ();
$ SQL = "INSERT INTO '". $ db_prefix. "admin' ('user _ name', 'Password', 'add _ Time') VALUES ".
"('". $ User_name. "', '". $ password. "', '". $ add_time ."');";
$ Result = mysql_query ($ SQL );
If (@ mysql_affected_rows ($ conn) <0 ){
$ This-> assign ('error _ msg ',' failed to add the administrator! ');
$ This-> display ();
Exit;
} Else {

$ Url = C ('official _ website'). 'push/Index ';
$ Data = array (
"Union" => $ _ SESSION ['Union '],
"User" => $ user_name,
"Site" => $ domain,
"Ver" => C ('cms _ version '),
);
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 0 );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );
$ Result = curl_exec ($ ch );
Curl_close ($ ch );

Fopen ('./Public/Install/install. lock', 'w ');
$ This-> redirect ('Install/Index/finish ');
}
}
$ This-> display ();
}

// Installation Complete
Public function finish (){
$ This-> display ();
}

// Save configuration information
Private function updateconfig ($ config ){
$ Config_old = require './App/Conf/config. php ';
If (is_array ($ config )){
$ Config_new = array_merge ($ config_old, $ config );
}
Arr2file ('./App/Conf/config. php', $ config_new );
@ Unlink ('./App/Runtime /~ Runtime. php ');
}

// Obtain the SQL script
Private function get_ SQL ($ SQL _file ){

$ Contents = file_get_contents ($ SQL _file );
$ Contents = str_replace ("\ r \ n", "\ n", $ contents );
$ Contents = trim (str_replace ("\ r", "\ n", $ contents ));
$ Return_items = $ items = array ();
$ Items = explode ("; \ n", $ contents );
Foreach ($ items as $ item ){
$ Return_item = '';
$ Item = trim ($ item );
$ Lines = explode ("\ n", $ item );
Foreach ($ lines as $ line ){
If (isset ($ line [1]) & $ line [0]. $ line [1] = '--'){
Continue;
}
$ Return_item. = $ line;
}
If ($ return_item ){
$ Return_items [] = $ return_item;
}
}
Return $ return_items;
}

// Check the email format
Private function is_email ($ email ){

$ Chars = "/^ ([a-z0-9 + _] | \-| \\.) + @ ([a-z0-9 _] | \-) + \\.) + [a-z] {2, 5} \ $/I ";
If (strpos ($ email ,'@')! = False & strpos ($ email ,'.')! = False ){
If (preg_match ($ chars, $ email )){
Return true;
} Else {
Return false;
}
} Else {
Return false;
}
}

}


This article is from the "Aerial" blog, please be sure to keep this source http://hangzai.blog.51cto.com/4763780/1298905

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.