class-ini

Gesponsorde koppelingen

PHP script bestanden

  1. class-ini

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
// INI Files parser.
// Created for: Fatal1ty bot.
// Copyright Remco "MisterGT" Pander.
// Mail: [email protected]

$ini = new ini('Fatal1ty/bot.ini');
$ini -> setFileName('Fatal1ty/test.ini');
$var = $ini -> get('variable', 'sectie');
$ini -> set('variable', 'waarde', 'sectie');
$ini -> write();

class ini
{
    // File name.
    public $file_name = 'file.ini';
    
    // File contents.
    public $contents = null;
    
    // Parsed.
    public $variables = array();
    
    // Constructor function.
    public function __construct($file_name = null)
    {

        # File name given?
        if($file_name == null)
        {

            return;
        }

        
        # Set the file name.
        $this -> file_name = $file_name;
        
        # File does exists?
        if(!file_exists($file_name))
        {

            return;
        }

        
        # Parse the file.
        $this -> parse();
    }

    
    // Set a new filename.
    public function setFileName($name)
    {

        $this -> file_name = $name;
        
        return $this -> file_name;
    }

    
    // Parse a ini file.
    public function parse()
    {

        # Make sure the file exists.
        if(!isset($this -> file_name) || !file_exists($this -> file_name))
        {

            return false;
        }

        
        # Fetch the file contents.
        $this -> contents = file_get_contents($this -> file_name);
        
        # Check if everything still went fine..
        if($this -> contents == false)
        {

            return false;
        }

                
        # Split the contents up in newlines.
        $lines = explode("\n", $this -> contents);
        
        # Loop trought the lines.
        foreach($lines as $line)
        {

            $line = trim($line);
            
            # Empty line
            if($line == '')
            {

                continue;
            }

            
            # Comment.
            if(substr($line, 0, 1) == ';')
            {

                continue;
            }

            
            # Section.
            if(substr($line, 0, 1) == '[' && substr($line, - 1) == ']')
            {

                $this -> section = substr($line, 1, -1);
                $this -> variables[$this -> section] = array();
                
                continue;
            }

            
            # Variable.
            $exploded = explode('=', $line);
            
            # Trim
            foreach($exploded as $i => $variable)
            {

                $exploded[$i] = trim($variable);
            }

            
            $variable = $exploded[0];
            $answer   = $exploded[1];
            
            # If it already exists, make an array.
            if(isset($this -> variables[$this -> section][$variable]))
            {

                # Already an array?
                if(is_array($this -> variables[$this -> section][$variable]))
                {

                    $this -> variables[$this -> section][$variable][] = $answer;
                }
else{
                    $answerCurrent = $this -> variables[$this -> section][$variable];
                    $this -> variables[$this -> section][$variable] = array($answerCurrent, $answer);
                }
            }
else{
                $this -> variables[$this -> section][$variable] = $answer;
            }
        }

        
        return count($this -> variables);
    }

    
    // Write a ini file.
    public function write()
    {

        $output = null;
        
        # Loop trought the sections.
        foreach($this -> variables as $section => $variables)
        {

            $output .= "[".$section."]\n";
            
            # Loop trought the variables.
            foreach($this -> variables[$section] as $variable => $answer)
            {

                # Array?
                if(is_array($this -> variables[$section][$variable]))
                {

                    # Loop (again)
                    foreach($this -> variables[$section][$variable] as $answerX)
                    {

                        $output .= $variable." = ".$answerX."\n";
                    }
                }
else{
                    $output .= $variable." = ".$answer."\n";
                }
            }
        }

        
        # Write output.
        file_put_contents($this -> file_name, $output);        
    }

    
    // Get the answer from a variable.
    public function get($variable, $section)
    {

        if(!isset($this -> variables[$section][$variable]))
        {

            return null;
        }

        
        return $this -> variables[$section][$variable];
    }

    
    // Change a variable.
    public function set($variable, $new, $section)
    {

        $this -> variables[$section][$variable] = $new;
    }

    
    // Remove a variable.
    public function destroy($variable, $section)
    {

        if(isset($this -> variables[$section][$variable]))
        {

            unset($this -> variables[$section][$variable]);
        }

        
        return true;
    }
}

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.