aboutsummaryrefslogtreecommitdiff
path: root/contrib/conf_merge/merge.php
blob: 6d8bedbaa0883f085c03be1815eebd35ba58c24f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*
 * Project Name: Config File Merge For Mangos/Trinity Server
 * Date: 01.01.2010 inital version (0.0.1a)
 * Author: Paradox
 * Copyright: Paradox
 * Email: iamparadox@netscape.net (paypal email)
 * License: GNU General Public License v2(GPL)
 */
if (!empty($_FILES['File1']) && !empty($_FILES['File2']))
{
    session_id();
    session_start();
    $basedir = "merge";
    $eol = "\r\n";
    if ($_POST['eol'])
        $eol = "\n";
    else
        $eol = "\r\n";
    if (!file_exists($basedir))
        mkdir($basedir);
    if (!file_exists($basedir."/".session_id()))
        mkdir($basedir."/".session_id());
    $upload1 = $basedir."/".session_id()."/".basename($_FILES['File1']['name']);
    $upload2 = $basedir."/".session_id()."/".basename($_FILES['File2']['name']);
    $newconfig = $basedir."/".session_id()."/trinitycore.conf.merged";
    $out_file = fopen($newconfig, w);
    $success = false;
    if (move_uploaded_file($_FILES['File1']['tmp_name'], $upload1))
    {
        $success = true;
    }
    else
    {
        $success = false;
    }
    if (move_uploaded_file($_FILES['File2']['tmp_name'], $upload2))
    {
        $success = true;
    }
    else
    {
        $success = false;
    }

    if ($success)
    {
        $custom_found = false;
        $in_file1 = fopen($upload1,r);
        $in_file2 = fopen($upload2,r);
        $array1 = array();
        $array2 = array();
        $line = trim(fgets($in_file1));
        while (!feof($in_file1))
        {
            if ((substr($line,0,1) != '#' && substr($line,0,1) != ''))
            {
                    list($key, $val) = explode("=",$line);
                    $key = trim($key);
                    $val = trim($val);
                    $array1[$key] = $val;
            }
            $line = trim(fgets($in_file1));
        }
        $line = trim(fgets($in_file2));
        while (!feof($in_file2) && !$custom_found)
        {
            if (substr($line,0,1) != '#' && substr($line,0,1) != '')
            {
                    list($key, $val) = explode("=",$line);
                    $key = trim($key);
                    $val = trim($val);
                    $array2[$key] = $val;
            }
            if (strtolower($line) == "# custom")
               $custom_found = true;
            else
                $line = trim(fgets($in_file2));
        }
        fclose($in_file1);
        foreach($array2 as $k => $v)
        {
            if (array_key_exists($k, $array1))
            {
                $array1[$k] = $v;
                unset($array2[$k]);
            }
        }
        $in_file1 = fopen($upload1,r);
        $line = trim(fgets($in_file1));
        while (!feof($in_file1))
        {
            if (substr($line,0,1) != '#' && substr($line,0,1) != '')
            {
                $array = array();
                while (substr($line,0,1) != '#' && substr($line,0,1) != '')
                {
                    list($key, $val) = explode("=",$line);
                    $key = trim($key);
                    $val = trim($val);
                    $array[$key] = $val;
                    $line = trim(fgets($in_file1));
                }
                foreach($array as $k => $v)
                {
                    if (array_key_exists($k, $array1))
                        fwrite($out_file, $k."=".$array1[$k].$eol);
                    else
                        continue;
                }
                unset($array);
                if (!feof($in_file1))
                    fwrite($out_file, $line.$eol);
            }
            else
                fwrite($out_file, $line.$eol);
            $line = trim(fgets($in_file1));
        }
        if ($custom_found)
        {
            fwrite($out_file, $eol);
            fwrite($out_file, "###############################################################################".$eol);
            fwrite($out_file, "# Custom".$eol);
            $line = trim(fgets($in_file2));
            while (!feof($in_file2))
            {
                fwrite($out_file, $line.$eol);
                $line = trim(fgets($in_file2));
            }
        }
        $first = true;
        foreach($array2 as $k => $v)
        {
            if ($first)
            {
                fwrite($out_file, $eol);
                fwrite($out_file, "###############################################################################".$eol);
                fwrite($out_file, "#      The Following values were removed from the config.".$eol);
                $first = false;
            }
            fwrite($out_file, "#          ".$k."=".$v.$eol);
        }
        unset($array1);
        unset($array2);
        fclose($in_file1);
        fclose($in_file2);
        fclose($out_file);
        unlink($upload1);
        unlink($upload2);

        echo "Process done";
        echo "<br /><a href=".$newconfig.">Click here to retrieve your merged conf</a>";
    }
}
else
{
    echo "An error has occurred";
}
?>