summaryrefslogtreecommitdiff
path: root/include/ini.h
blob: 8745fef1986f5d76410402cf379b1208213ef38b (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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
 *  Ini.h
 *
 *  Class that can be instantiated in the get_module() startup function to
 *  define settings from the php.ini file that are supported.
 *  
 *  @copyright 2014 Copernica BV
 */

/**
 *  Forward declarations
 */
struct _zend_ini_entry;

/**
 *  Set up namespace
 */
namespace Php {

/**
 *  Class IniValue designed for extracting values from ini entries.
 */
class IniValue
{
public:
    /**
     *  Constructors for floating point values
     * 
     *  @param  name        Name of the php.ini variable
     *  @param  isorig      Is the original value
     */
    IniValue(const char *name, const bool isorig) : _name(name), _isorig(isorig) {}

    /**
     *  Cast to a number
     *  @return int32_t
     */
    operator int16_t () const
    {
        return (int16_t)numericValue();
    }

    /**
     *  Cast to a number
     *  @return int32_t
     */
    operator int32_t () const
    {
        return (int32_t)numericValue();
    }

    /**
     *  Cast to a number
     *  @return uint64_t
     */
    operator int64_t () const
    {
        return numericValue();
    }
    
    /**
     *  Cast to a boolean
     *  @return boolean
     */
    operator bool () const
    {
        return (bool)numericValue();
    }
    
    /**
     *  Cast to a string
     *  @return string
     */
    operator std::string () const
    {
        return rawValue();
    }
    
    /**
     *  Cast to byte array
     *  @return const char *
     */
    operator const char * () const
    {
        return rawValue();
    }
    
    /**
     *  Cast to a floating point
     *  @return double
     */
    operator double() const;


private:

    
    /**
     *  Retrieve the value as number
     *
     *  We force this to be a int64_t because we assume that most
     *  servers run 64 bits nowadays, and because we use int32_t, int64_t
     *  almost everywhere, instead of 'long' and on OSX neither of
     *  these intxx_t types is defined as 'long'...
     *
     *  @return int64_t
     */
    int64_t numericValue() const;

    /**
     *  Get access to the raw buffer for read operationrs.
     *  @return const char *
     */
    const char *rawValue() const;
    
    /**
     *  ini entry name
     *  @var    std::string
     */
    std::string _name;

    /**
     *  Is the orig value?
     *  @var    bool
     */
    bool _isorig = false;
};

/**
 *  Custom output stream operator
 *  @param  stream
 *  @param  ini_val
 *  @return ostream
 */
std::ostream &operator<<(std::ostream &stream, const IniValue &ini_val);


/**
 *  Class definition
 */
class Ini
{
public:
    /**
     *  Supported place-types for ini setting
     * 
     *  The possible settings for where the configuration can be changed are:
     *  PHP_INI_USER, PHP_INI_PERDIR, PHP_INI_SYSTEM and PHP_INI_ALL
     * 
     *  Usually you would choose where the setting can be changed based on how 
     *  it is used. For example if you want to access the setting during RINIT 
     *  stage then you would want PHP_INI_PERDIR because the setting would have 
     *  no use after RINIT.
     */
    enum Place : int {
        User      = (1<<0),                         // ZEND_INI_USER   (1<<0)
        Perdir    = (1<<1),                         // ZEND_INI_PERDIR (1<<1)
        System    = (1<<2),                         // ZEND_INI_SYSTEM (1<<2)
        All       = (1<<0) | (1<<1) | (1<<2)        // ZEND_INI_ALL  (ZEND_INI_USER|ZEND_INI_PERDIR|ZEND_INI_SYSTEM)
    };

    /**
     *  Constructors for string values
     * 
     *  @param  name        Name of the php.ini variable
     *  @param  value       Default value
     *  @param  orig        Original value (if the user resets the variable, it is set back to this value)
     *  @param  place       Place where the ini setting can be changed
     */
    Ini(const char *name, const char *value, const char *orig, const Place place = Place::All) :
        _name(name), _value(value), _orig(orig), _place(place) {}

    Ini(const char *name, const char *value, const Place place = Place::All) :
        _name(name), _value(value), _orig_empty(true), _place(place) {}

    /**
     *  Constructors for bool values
     * 
     *  @param  name        Name of the php.ini variable
     *  @param  value       Default value
     *  @param  orig        Original value (if the user resets the variable, it is set back to this value)
     *  @param  place       Place where the ini setting can be changed
     */
    Ini(const char *name,const  bool value, const bool orig, const Place place = Place::All) :
        _name(name), _value(bool2str(value)), _orig(bool2str(orig)), _place(place) {}

    Ini(const char *name, const bool value, const Place place = Place::All) :
        _name(name), _value(bool2str(value)), _orig_empty(true), _place(place) {}

    /**
     *  Constructors for integer values
     * 
     *  @param  name        Name of the php.ini variable
     *  @param  value       Default value
     *  @param  orig        Original value (if the user resets the variable, it is set back to this value)
     *  @param  place       Place where the ini setting can be changed
     */
    Ini(const char *name, const int16_t value, const int16_t orig, const Place place = Place::All) :
        _name(name), _value(std::to_string(value)), _orig(std::to_string(orig)), _place(place) {}

    Ini(const char *name, const int16_t value, const Place place = Place::All) :
        _name(name), _value(std::to_string(value)), _orig_empty(true), _place(place) {}

    Ini(const char *name, const int32_t value, const int32_t orig, const Place place = Place::All) :
        _name(name), _value(std::to_string(value)), _orig(std::to_string(orig)), _place(place) {}

    Ini(const char *name, const int32_t value, const Place place = Place::All) :
        _name(name), _value(std::to_string(value)), _orig_empty(true), _place(place) {}

    Ini(const char *name, const int64_t value, const int64_t orig, const Place place = Place::All) :
        _name(name), _value(std::to_string(value)), _orig(std::to_string(orig)), _place(place) {}

    Ini(const char *name, const int64_t value, const Place place = Place::All) :
        _name(name), _value(std::to_string(value)), _orig_empty(true), _place(place) {}

    /**
     *  Constructors for floating point values
     * 
     *  @param  name        Name of the php.ini variable
     *  @param  value       Default value
     *  @param  orig        Original value (if the user resets the variable, it is set back to this value)
     *  @param  place       Place where the ini setting can be changed
     */
    Ini(const char *name, const double value, const double orig, const Place place = Place::All) :
        _name(name), _value(std::to_string(value)), _orig(std::to_string(orig)), _place(place) {}

    Ini(const char *name, const double value, const Place place = Place::All) :
        _name(name), _value(std::to_string(value)), _orig_empty(true), _place(place) {}


    /**
     *  Filling ini_entries
     *  @param  ini_entry
     *  @param  module_number
     */
    void fill(struct _zend_ini_entry *ini_entry, int module_number);

    
    static IniValue get(const char* name)
    {
        return IniValue(name, false);
    }

    static IniValue get_orig(const char* name)
    {
        return IniValue(name, true);
    }


private:
    /**
     *  Helper function to convert a boolean to a string
     *  @param  value
     *  @return string
     */
    static constexpr const char* bool2str(const bool value)
    {
        // cast to a string
        return (  static_cast<bool>(value) ? "On" : "Off");
    }
    
    /**
     *  ini entry name
     *  @var    std::string
     */
    std::string _name;

    /**
     *  ini entry value
     *  @var    std::string
     */
    std::string _value;

    /**
     *  ini entry original value
     *  @var    std::string
     */
    std::string _orig;

    /**
     *  Is the orig value set or empty?
     *  @var    bool
     */
    bool _orig_empty = false;
    
    /**
     *  Place where the configuration can be changed
     *  @var    Place
     */
    Place _place;


};

/**
 *  End of namespace
 */
}