4images Modifications / Modifikationen > Plugins

[MOD-Plugin] - SQL Patches - Add SQL file in ACP (for patch and MODs)

(1/3) > >>

thunderstrike:
Hi,

I code new MOD-Plugin for ACP. Admin add SQL file in form and submit. SQL file is use for SQL command.
After upload file, file is delete auto.

So, if install MOD from someone and need to add SQL command - no more need for phpmyadmin. You can use from ACP. ;)
Note: Always use 4images_ for prefix. No matter if is no same for gallery you use (in publish MOD) - MOD is detect for right prefix use. This is test in 4images v1.74. ;)

// Step 1

In includes/constants.php file,

find:


--- Quote ---define('DATABASE_DIR', 'data/database');

--- End quote ---

add after:


--- Code: ---define('SQL_PATCHES_DIR', 'data/sql_patches');

--- End code ---

// Step 1.1

In data folder, create folder: sql_patches . Set CHMOD 777 for write.

// Step 2

In lang/english/admin.php file,

find:


--- Quote ---$lang['save_settings_success'] = "Settings saved";

--- End quote ---

add after:


--- Code: ---//-----------------------------------------------------
//--- SQL Patches -------------------------------------
//-----------------------------------------------------
$lang['sql_patches_title'] = "SQL Patches";
$lang['sql_patches_could_not_load_message'] = "Could not load: ";
$lang['sql_patches_success_install'] = "<font color=\"green\">Success !</font> - <b>{success_install_file}</b> file has just been added into the database.";
$lang['sql_patches_select_sql_file'] = "<b>Select SQL file:</b> ";
$lang['sql_patches_go_back'] = "<span class=\"smalltext\">[ Go back ]</a>";
$lang['sql_patches_backup_database_message'] = "<span class=\"smalltext\">Use <b>Backup database</b> link first !</span>";
$lang['sql_patches_expert_add_command'] = "<span class=\"smalltext\">Add SQL command here:</span>";
$lang['sql_patches_expert_success_install'] = "<font color=\"green\">Done !</font>";
$lang['sql_patches_expert_mode_link'] = "[ Expert mode ]";

--- End code ---

// Step 3

In admin/plugins folder, create file: sql_patches.php .

Add:


--- Code: ---<?php // PLUGIN_TITLE: SQL Patches

$nozip = 1;
define('IN_CP', 1);
define('ROOT_PATH', "./../../");
require(ROOT_PATH.'admin/admin_global.php');

show_admin_header();

if ($action == "") {
    $action = "show_sql_patches";
}

if ($action == "exec_sql_patches") {
    
    if (isset($HTTP_POST_FILES['sql_file']['tmp_name'])) {
        $HTTP_POST_FILES['sql_file']['tmp_name'] = (stripslashes(trim($HTTP_POST_FILES['sql_file']['tmp_name'])));                
    }   
        
        if (!isset($HTTP_POST_FILES['sql_file']['tmp_name']) || empty($HTTP_POST_FILES['sql_file']['tmp_name'])) {            
            $action = "show_sql_patches";
        }
        
        if (!preg_match("/\.sql$/", $HTTP_POST_FILES['sql_file']['name'])) {
            unlink ($HTTP_POST_FILES['sql_file']['tmp_name']);
            unset ($HTTP_POST_FILES['sql_file']['tmp_name']);
            $action = "show_sql_patches";
        }
        
        if (isset($HTTP_POST_FILES['sql_file']['tmp_name']) && !empty($HTTP_POST_FILES['sql_file']['tmp_name']) && $HTTP_POST_FILES['sql_file']['error'] == 0 && preg_match("/\.sql$/", $HTTP_POST_FILES['sql_file']['name'])) {
            
            if (@file_exists($HTTP_POST_FILES['sql_file']['tmp_name'])) {
                @move_uploaded_file($HTTP_POST_FILES['sql_file']['tmp_name'], ROOT_PATH . SQL_PATCHES_DIR . "/" . $HTTP_POST_FILES['sql_file']['name']);
            }
            
            include(ROOT_PATH.'includes/db_utils.php');
            
            $db_file = ROOT_PATH . SQL_PATCHES_DIR . "/" . get_file_name(basename($HTTP_POST_FILES['sql_file']['name']));
            $cont = @fread(@fopen($db_file, 'r'), @filesize($db_file));
            if (empty($cont)) {
                $error_log[] = $lang['sql_patches_could_not_load_message'] . $db_file;
            }
            
            $flag = false;
            if (empty($error_log)) {            
                $cont = preg_replace('/4images_/', $table_prefix, $cont);
                $pieces = split_sql_dump($cont);
                for ($i = 0; $i < sizeof($pieces); $i++) {
                    $sql = trim($pieces[$i]);
                    if (!empty($sql) and $sql[0] != "#") {
                        if (!$site_db->query($sql)) {
                            $error_log[] = $sql;
                            $flag = false;
                        } else {
                            $flag = true;                            
                        }
                    }            
                }
            }
        
        if ($flag == true) {
            ?>
            <table border="0" width="100%" cellpadding="0" cellspacing="0">
            <tr>
            <td width="100%">    
            <table border="0" width="100%" cellpadding="0" cellspacing="0">
            <tr>
            <td width="100%" class="tableheader" />&nbsp;<?php echo $lang['sql_patches_title']; ?></td>
            </tr>
            <tr>
            <td width="100%" class="tablerow" /><a href="<?php echo $site_sess->url("sql_patches.php"); ?>" class="link" /><?php echo $lang['sql_patches_go_back']; ?></a></td>
            </tr>
            <tr>
            <td width="100%" class="tablerow" />&nbsp;</td>
            </tr>
            <tr>
            <td width="100%" align="center" class="tablerow" /><?php echo str_replace("{success_install_file}", format_text(trim($HTTP_POST_FILES['sql_file']['name']), 2), $lang['sql_patches_success_install']); ?></td>
            </tr>
            <tr>    
            <td width="100%" class="tablerow" />&nbsp;</td>
            </tr>
            <tr>
            <td width="100%" class="tableheader" />&nbsp;</td>
            </tr>    
            </table>    
            </td>            
            </tr>
            </table>
            <?php
            @unlink(ROOT_PATH . SQL_PATCHES_DIR . $HTTP_POST_FILES['sql_file']['name']);
        
        } elseif ($flag == false) {
            
            if (isset($error_log) && is_array($error_log) && !empty($error_log)) {
                foreach ($error_log as $key => $val) {
                    echo $val . "<br />";
                }
                $action = "show_sql_patches";
            }            
        }        
    }
}

if ($action == "exec_sql_expert_patches") {
    
    if (isset($HTTP_POST_VARS['add_sql_expert_patches'])) {
        $add_sql_expert_patches = (isset($HTTP_POST_VARS['add_sql_expert_patches'])) ? un_htmlspecialchars(trim((string)$HTTP_POST_VARS['add_sql_expert_patches'])) : "";
    } else {
        $add_sql_expert_patches = "";
    }
    
    if (!isset($add_sql_expert_patches) || empty($add_sql_expert_patches)) {
        $action = "show_sql_expert_patches";
    }
    
    if (isset($add_sql_expert_patches) && !empty($add_sql_expert_patches)) {        
        
        if (!function_exists('check_sql_queries')) {
            function check_sql_queries($add_sql_expert_patches) {
                global $site_db, $table_prefix, $valid, $split_sql_queries;  
                if (!isset($split_sql_queries)) {
                    $ids = trim($add_sql_expert_patches);                    
                    $ids = preg_replace("/[\n\r]/is", " ", $ids);
                    //$ids = str_replace(",", " ", $ids);
                    $ids = preg_quote($ids);
                    $ids = str_replace('/', '\\/', $ids);                    
                    $split_sql_queries = preg_split("/\s+/", $ids);
                }
                $valid = true;
                foreach ($split_sql_queries as $key => $val) {            
                    if ($val == "CREATE" || $val == "ALTER" || $val == "DROP" || $val == "DELETE" || $val == "UPDATE" || $val == "REPLACE" || $val == "INSERT") {
                        $val = preg_replace("/4images_/", $table_prefix, $val);                                                                      
                        if (!$site_db->query(stripslashes(stripslashes($ids)))) {
                            $valid = false;
                            break;
                        }
                    }                    
                }        
            }
        }
        
        check_sql_queries($add_sql_expert_patches);
        
        if ($valid == true) {
            ?>
            <table border="0" width="100%" cellpadding="0" cellspacing="0">
            <tr>
            <td width="100%">    
            <table border="0" width="100%" cellpadding="0" cellspacing="0">
            <tr>
            <td width="100%" class="tableheader" />&nbsp;<?php echo $lang['sql_patches_title']; ?></td>
            </tr>
            <tr>
            <td width="100%" class="tablerow" /><a href="<?php echo $site_sess->url("sql_patches.php"); ?>" class="link" /><?php echo $lang['sql_patches_go_back']; ?></a></td>
            </tr>
            <tr>
            <td width="100%" class="tablerow" />&nbsp;</td>
            </tr>
            <tr>
            <td width="100%" align="center" class="tablerow" /><?php echo $lang['sql_patches_expert_success_install']; ?></td>
            </tr>
            <tr>    
            <td width="100%" class="tablerow" />&nbsp;</td>
            </tr>
            <tr>
            <td width="100%" class="tableheader" />&nbsp;</td>
            </tr>    
            </table>    
            </td>            
            </tr>
            </table>
            <?php            
        
        } elseif ($valid == false) {
            $action = "show_sql_expert_patches";            
        }
    }
}

if ($action == "show_sql_expert_patches") {    
    ?>
    <table border="0" width="100%" cellpadding="0" cellspacing="0">
    <tr>
    <td width="100%">
    <form action="<?php echo $site_sess->url("sql_patches.php"); ?>" method="post">
    <input type="hidden" name="action" value="exec_sql_expert_patches">
    <table border="0" width="100%" cellpadding="0" cellspacing="0">
    <tr>
    <td width="100%" class="tableheader" />&nbsp;<?php echo $lang['sql_patches_title']; ?></td>
    </tr>
    <tr>
    <td width="100%" class="tablerow" /><a href="<?php echo $site_sess->url("sql_patches.php"); ?>" class="link" /><?php echo $lang['sql_patches_go_back']; ?></a></td>
    </tr>
    </table>
    <table border="0" width="100%" cellpadding="0" cellspacing="0">
    <tr>
    <td width="100%" align="center" class="tablerow" />&nbsp;</td>
    </tr>
    </table>
    <table border="0" width="100%" cellpadding="0" cellspacing="0">
    <tr>
    <td width="100%" align="center" class="tablerow" />
    <?php echo $lang['sql_patches_expert_add_command']; ?><br />
    <textarea name="add_sql_expert_patches" rows="20" cols="60" class="textarea" /></textarea><br /><br />
    <input type="submit" name="submit" value="<?php echo $lang['submit']; ?>" class="button" />
    </form>    
    </td>
    </tr>
    <tr>
    <td width="100%" align="center" class="tablerow" />&nbsp;</td>
    </tr>
    <tr>
    <td width="100%" align="center" class="tablerow" /><?php echo $lang['sql_patches_backup_database_message']; ?></td>
    </tr>
    <tr>
    <td width="100%" align="center" class="tablerow" />&nbsp;</td>
    </tr>
    <tr>
    <td width="100%" align="center" class="tableheader" />&nbsp;</td>
    </tr>    
    </table>
    </form>
    </td>
    </tr>
    </table>
    <?php    
}

if ($action == "show_sql_patches") {
    ?>
    <table border="0" width="100%" cellpadding="0" cellspacing="0">
    <tr>
    <td width="100%">
    <form action="<?php echo $site_sess->url("sql_patches.php"); ?>" method="post" enctype="multipart/form-data">
    <input type="hidden" name="action" value="exec_sql_patches">
    <table border="0" width="100%" cellpadding="0" cellspacing="0">
    <tr>
    <td width="100%" class="tableheader" />&nbsp;<?php echo $lang['sql_patches_title']; ?></td>
    </tr>
    </table>
    <table border="0" width="100%" cellpadding="0" cellspacing="0">
    <tr>
    <td width="100%" align="right" class="tablerow" /><a href="<?php echo $site_sess->url("sql_patches.php?action=show_sql_expert_patches"); ?>" class="link" /><?php echo $lang['sql_patches_expert_mode_link']; ?></a>&nbsp;</td>
    </tr>
    </table>
    <table border="0" width="100%" cellpadding="0" cellspacing="0">
    <tr>
    <td width="100%" align="center" class="tablerow" />&nbsp;</td>
    </tr>
    </table>
    <table border="0" width="100%" cellpadding="0" cellspacing="0">
    <tr>
    <td width="100%" align="center" class="tablerow" /><?php echo $lang['sql_patches_select_sql_file']; ?><input type="file" name="sql_file" class="input" />&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" value="<?php echo $lang['submit']; ?>" class="button" /></td>
    </tr>
    <tr>
    <td width="100%" align="center" class="tablerow" />&nbsp;</td>
    </tr>
    <tr>
    <td width="100%" align="center" class="tablerow" /><?php echo $lang['sql_patches_backup_database_message']; ?></td>
    </tr>
    <tr>
    <td width="100%" align="center" class="tablerow" />&nbsp;</td>
    </tr>
    <tr>
    <td width="100%" align="center" class="tableheader" />&nbsp;</td>
    </tr>    
    </table>
    </form>
    </td>
    </tr>
    </table>
    <?php    
}

show_admin_footer();
?>

--- End code ---

// Step 4

In includes/db_utils.php file,

find:


--- Quote ---if (preg_match("/^CREATE/i", $line)) {

--- End quote ---

replace:


--- Code: ---if (preg_match("/^CREATE/i", $line) || preg_match("/^ALTER/i", $line) || preg_match("/^INSERT/i", $line) || preg_match("/^UPDATE/i", $line) || preg_match("/^REPLACE/i", $line) || preg_match("/^DELETE/i", $line) || preg_match("/^DROP/i", $line)) {

--- End code ---

// Step 5

Install this:

http://www.4homepages.de/forum/index.php?topic=19287.0

From step 3 - find:


--- Quote ---if (!isset($add_sql_expert_patches) || empty($add_sql_expert_patches)) {
    $action = "show_sql_expert_patches";
}

--- End quote ---

add after:


--- Code: ---if (function_exists('check_post_comment_valid_chars') && !check_post_comment_valid_chars($add_sql_expert_patches)) {
    $action = "show_sql_expert_patches";
}

--- End code ---

Go in ACP - > SQL Patches link (in plugins tab) - and

Tala ! 8)

Again - is test in 4images v1.74 .

Nicky:
hi thunderstrike,
nice mod

but whatfor is "browse" for file when file have to be in folder /data/sql_patches/  ?

--- Quote ---Could not load: ./../../data/sql_patches/test.sql

--- End quote ---

moving thread to releases.

thunderstrike:
I no can create problem now but see before...

If have problem with path :

1 - You need edit: ROOT_PATH in file.
2 - You need set CHMOD 777 to sql_patches folder in data.

Is how I fix ...

3 - You move this MOD to release but no sticky (same for other MOD I post :!:)

Nicky:
okay...
on a linux machine that works fine.
on the windows machine error will be produced.. but no matter..

your post is now sticked, like all others..

thunderstrike:
Ok - I check for windows problem with path later.

Navigation

[0] Message Index

[#] Next page

Go to full version