]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - Classes/class-unrealconf.php
Add a `Plugins` overview card
[irc/unrealircd/unrealircd-webpanel.git] / Classes / class-unrealconf.php
1 <?php
2
3 require_once("../inc/common.php");
4
5 class Conf
6 {
7 static $settings_short = [];
8 static $settings_temp = [];
9 static $settings = [];
10 function __construct($filename, &$error)
11 {
12 if (!is_file($filename))
13 {
14 $error[] = "Not a valid configuration file: $filename";
15 }
16 else
17 {
18 $file = file_get_contents($filename);
19 $file = preg_replace('/\/\*(.|\s)*?\*\//', '', $file);
20 //$file = preg_replace('/\/\/(.|\s)*?\n/', '', $file);
21 $file = str_replace("\t"," ",$file);
22 $file = str_replace("\n\n","\n",$file);
23 $file = str_replace("\n", " ",$file);
24 for(; strstr($file," ");)
25 $file = str_replace(" "," ",$file);
26 $config = $this->parse_config($file, $error);
27 }
28 }
29
30 private function parse_config($string, &$error)
31 {
32 $tok = split($string);
33 $blockstring = "";
34 $full = "";
35 foreach($tok as $str)
36 {
37 $str = trim($str);
38 if ($str[0] == '#' || substr($str,0,2) == "//")
39 {
40 var_dump($str);
41 continue;
42 }
43 if (!strcmp($str,"{") && mb_substr($blockstring,-2,2) !== "::")
44 strcat($blockstring,"::");
45
46 elseif (!strcmp($str,"}"))
47 {
48 $split = split($blockstring,"::");
49 if (BadPtr($split[sizeof($split) - 1]))
50 unset($split[sizeof($split) - 1]);
51 unset($split[sizeof($split) - 1]);
52 $blockstring = glue($split,"::");
53 if (!BadPtr($blockstring))
54 {
55 strcat($blockstring,"::");
56 }
57 }
58 // if we found a value and it's time to go to the next one
59 elseif (!BadPtr($str) && $str[strlen($str) - 1] == ";")
60 {
61 if (substr_count($str,"\"") != 1)
62 strcat($blockstring, "::".rtrim($str,";")); // finish off our item
63 else strcat($blockstring, " ".rtrim($str,";"));
64 strcat($full,str_replace(["::::", "\""],["::", ""],$blockstring)."\n"); // add the full line to our $full variable
65
66 /* rejig the blockstring */
67 $split = split($blockstring,"::");
68 if (BadPtr($split[sizeof($split) - 1]))
69 unset($split[sizeof($split) - 1]);
70 unset($split[sizeof($split) - 1]);
71 unset($split[sizeof($split) - 1]);
72 $blockstring = glue($split,"::");
73 if (!BadPtr($blockstring))
74 {
75 rtrim($blockstring,":");
76 strcat($blockstring,"::");
77 }
78 }
79
80 else
81 { if (!BadPtr($blockstring) && mb_substr($blockstring,-2,2) !== "::")
82 strcat($blockstring," ");
83 strcat($blockstring,$str);
84 }
85 }
86
87 $full = split($full,"\n");
88 echo highlight_string(var_export($full, true));
89 $long = [];
90
91 foreach($full as $config_item)
92 {
93 $arr = &$long;
94 self::$settings_short[] = $config_item;
95 $tok = split($config_item,"::");
96 for ($i = 0; $i <= count($tok); $i++)
97 {
98 if (isset($tok[$i + 2]))
99 $arr = &$arr[$tok[$i]];
100
101 elseif (isset($tok[$i + 1]) && isset($tok[$i - 1]))
102 $arr[$tok[$i]] = $tok[$i + 1];
103
104 elseif (isset($tok[$i + 1]))
105 $arr[$tok[$i]][] = $tok[$i + 1];
106 }
107 }
108 self::$settings_temp = $long;
109 $cf = &self::$settings_temp;
110
111 if (!empty($error))
112 {
113 self::$settings_temp = [];
114 return false;
115 }
116 $arr = ['cfg' => $cf, 'err' => &$error];
117
118
119 if (!empty($error))
120 {
121 self::$settings_temp = [];
122 return false;
123 }
124 self::$settings = self::$settings_temp;
125 self::$settings_temp = [];
126
127 echo highlight_string(var_export(self::$settings, true));
128 }
129 function parse2()
130 {
131 $configFile = 'unrealircd.conf';
132
133 $config = array();
134
135 if (file_exists($configFile)) {
136 $lines = file($configFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
137 foreach ($lines as $line)
138 {
139 $config[] = trim($line);
140 }
141 }
142 echo highlight_string(var_export($config, true));
143 return $config;
144 }
145 }
146
147 $errors = [];
148 new Conf("unrealircd.conf", $errors);