corrade-http-templates – Blame information for rev 2
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
2 | eva | 1 | <?php |
2 | |||
3 | ########################################################################### |
||
4 | ## Copyright (C) Wizardry and Steamworks 2015 - License: GNU GPLv3 ## |
||
5 | ########################################################################### |
||
6 | ## This is a script that creates a Corrade group from a JSON object. ## |
||
7 | ########################################################################### |
||
8 | |||
9 | ########################################################################### |
||
10 | ## GET POST VARIABLES ## |
||
11 | ########################################################################### |
||
12 | |||
13 | if(!isset($_POST['group'])) return -1; |
||
14 | |||
15 | ########################################################################### |
||
16 | ## CONFIGURATION ## |
||
17 | ########################################################################### |
||
18 | |||
19 | # The path to the configuration. |
||
20 | include_once('config.php'); |
||
21 | |||
22 | ########################################################################### |
||
23 | ## INTERNALS ## |
||
24 | ########################################################################### |
||
25 | |||
26 | # This function adds a Corrade group from a JSON object $JSONdata |
||
27 | # The JSON object has the following structure with example data: |
||
28 | # { |
||
29 | # "ChatLog":"logs/groupLog.txt", |
||
30 | # "ChatLogEnabled":false, |
||
31 | # "DatabaseFile":"databases/groupDatabase.db", |
||
32 | # "Workers":5, |
||
33 | # "Notifications":[ |
||
34 | # "alert", |
||
35 | # "typing", |
||
36 | # "invite" |
||
37 | # ], |
||
38 | # "Permissions":[ |
||
39 | # "group", |
||
40 | # "movement" |
||
41 | # ], |
||
42 | # "Name":"Nanny & Baby", |
||
43 | # "UUID":"708cc093-22b9-43e9-9682-6b4f284c75dd", |
||
44 | # "Password":"badmoonrising" |
||
45 | # } |
||
46 | function AddCorradeGroup( |
||
47 | $domDocument, |
||
48 | $JSONdata, |
||
49 | $ALLOWED_PERMISSION = [ |
||
50 | 'movement', |
||
51 | 'economy', |
||
52 | 'land', |
||
53 | 'grooming', |
||
54 | 'inventory', |
||
55 | 'interact', |
||
56 | 'mute', |
||
57 | 'database', |
||
58 | 'notifications', |
||
59 | 'talk', |
||
60 | 'directory', |
||
61 | 'system', |
||
62 | 'friendship', |
||
63 | 'execute', |
||
64 | 'group', |
||
65 | 'filter' |
||
66 | ], |
||
67 | $ALLOWED_NOTIFICATIONS = [ |
||
68 | 'alert', |
||
69 | 'region', |
||
70 | 'group', |
||
71 | 'balance', |
||
72 | 'local', |
||
73 | 'message', |
||
74 | 'notice', |
||
75 | 'dialog', |
||
76 | 'friendship', |
||
77 | 'inventory', |
||
78 | 'permission', |
||
79 | 'lure', |
||
80 | 'effect', |
||
81 | 'collision', |
||
82 | 'crossing', |
||
83 | 'terse', |
||
84 | 'typing', |
||
85 | 'invite', |
||
86 | 'economy', |
||
87 | 'membership', |
||
88 | 'url', |
||
89 | 'ownersay', |
||
90 | 'regionsayto', |
||
91 | 'objectim', |
||
92 | 'rlv', |
||
93 | 'debug', |
||
94 | 'avatars', |
||
95 | 'primitives', |
||
96 | 'control' |
||
97 | ]) { |
||
98 | # Decode JSON |
||
99 | $JSONdata = json_decode($JSONdata); |
||
100 | |||
101 | # First, create a new group element. |
||
102 | $newGroup = $domDocument->createElement('Group'); |
||
103 | |||
104 | # Create the chat-log entry. |
||
105 | if(isset($JSONdata->ChatLog)) { |
||
106 | $chatLog = $domDocument->createElement('ChatLog'); |
||
107 | $chatLog->appendChild( |
||
108 | $domDocument->createTextNode($JSONdata->ChatLog) |
||
109 | ); |
||
110 | $newGroup->appendChild($chatLog); |
||
111 | } |
||
112 | # Create the chat-log enabled entry. |
||
113 | if(isset($JSONdata->ChatLogEnabled)) { |
||
114 | $chatLogEnabled = $domDocument->createElement('ChatLogEnabled'); |
||
115 | $chatLogEnabled->appendChild( |
||
116 | $domDocument->createTextNode($JSONdata->ChatLogEnabled) |
||
117 | ); |
||
118 | $newGroup->appendChild($chatLogEnabled); |
||
119 | } |
||
120 | # Create the database file entry. |
||
121 | if(isset($JSONdata->DatabaseFile)) { |
||
122 | $databaseFile = $domDocument->createElement('DatabaseFile'); |
||
123 | $databaseFile->appendChild( |
||
124 | $domDocument->createTextNode($JSONdata->DatabaseFile) |
||
125 | ); |
||
126 | $newGroup->appendChild($databaseFile); |
||
127 | } |
||
128 | # Create the group name entry. |
||
129 | if(isset($JSONdata->Name)) { |
||
130 | $groupName = $domDocument->createElement('Name'); |
||
131 | $groupName->appendChild( |
||
132 | $domDocument->createTextNode($JSONdata->Name) |
||
133 | ); |
||
134 | $newGroup->appendChild($groupName); |
||
135 | } |
||
136 | # Create the group UUID. |
||
137 | if(isset($JSONdata->UUID)) { |
||
138 | $groupGUID = $domDocument->createElement('Guid'); |
||
139 | $groupGUID->appendChild( |
||
140 | $domDocument->createTextNode($JSONdata->UUID) |
||
141 | ); |
||
142 | $groupUUID = $domDocument->createElement('UUID'); |
||
143 | $groupUUID->appendChild($groupGUID); |
||
144 | $newGroup->appendChild($groupUUID); |
||
145 | } |
||
146 | # Create the group password entry. |
||
147 | if(isset($JSONdata->Password)) { |
||
148 | $groupPassword = $domDocument->createElement('Password'); |
||
149 | $groupPassword->appendChild( |
||
150 | $domDocument->createTextNode($JSONdata->Password) |
||
151 | ); |
||
152 | $newGroup->appendChild($groupPassword); |
||
153 | } |
||
154 | # Create the group workers. |
||
155 | if(isset($JSONdata->Workers)) { |
||
156 | $groupWorkers = $domDocument->createElement('Workers'); |
||
157 | $groupWorkers->appendChild( |
||
158 | $domDocument->createTextNode($JSONdata->Workers) |
||
159 | ); |
||
160 | $newGroup->appendChild($groupWorkers); |
||
161 | } |
||
162 | # Create the group notifications. |
||
163 | if(count($JSONdata->Notifications)) { |
||
164 | $groupNotifications = $domDocument->createElement('Notifications'); |
||
165 | foreach($JSONdata->Notifications as $notification) { |
||
166 | # if the notification is not allowed, skip it |
||
167 | if(!in_array($notification, $ALLOWED_NOTIFICATIONS)) |
||
168 | continue; |
||
169 | $notificationElement = $domDocument-> |
||
170 | createElement('Notifications'); |
||
171 | $notificationElement->appendChild( |
||
172 | $domDocument->createTextNode($notification) |
||
173 | ); |
||
174 | $groupNotifications->appendChild($notificationElement); |
||
175 | } |
||
176 | $newGroup->appendChild($groupNotifications); |
||
177 | } |
||
178 | #Create the group permissions. |
||
179 | if(count($JSONdata->Permissions)) { |
||
180 | $groupPermissions = $domDocument->createElement('Permissions'); |
||
181 | foreach($JSONdata->Permissions as $permission) { |
||
182 | # if the permission is not allowed, skip it |
||
183 | if(!in_array($permission, $ALLOWED_PERMISSION)) |
||
184 | continue; |
||
185 | $permissionElement = $domDocument-> |
||
186 | createElement('Permissions'); |
||
187 | $permissionElement->appendChild( |
||
188 | $domDocument->createTextNode($permission) |
||
189 | ); |
||
190 | $groupPermissions->appendChild($permissionElement); |
||
191 | } |
||
192 | $newGroup->appendChild($groupPermissions); |
||
193 | } |
||
194 | |||
195 | # Finally, add the new group to the groups. |
||
196 | $groups = $domDocument->getElementsByTagName('Groups'); |
||
197 | switch($groups->length != 0) { |
||
198 | case true: # The "Groups" node exists, so just append. |
||
199 | $groups[0]->appendChild($newGroup); |
||
200 | break; |
||
201 | default: # The "Groups" node does not exist, so create it. |
||
202 | $groups = $domDocument->createElement('Groups'); |
||
203 | $groups[0]->appendChild($newGroup); |
||
204 | break; |
||
205 | } |
||
206 | |||
207 | return $domDocument; |
||
208 | } |
||
209 | |||
210 | # This function checks whether a Corrade group by name and UUID exists. |
||
211 | # The JSON must contain "Name" set to the name of the group and "UUID" |
||
212 | # set to the UUID of the group to be searched. |
||
213 | function DoesCorradeGroupExist($domDocument, $JSONdata) { |
||
214 | $JSONdata = json_decode($JSONdata); |
||
215 | $groups = $domDocument->getElementsByTagName('Groups'); |
||
216 | if($groups->length == 0) return false; |
||
217 | foreach($groups[0]->getElementsByTagName('Group') as $group) { |
||
218 | if( |
||
219 | $group->getElementsByTagName('Name')[0]-> nodeValue == |
||
220 | $JSONdata->Name && |
||
221 | $group->getElementsByTagName('UUID')[0]-> |
||
222 | getElementsByTagName('Guid')[0]->nodeValue == |
||
223 | $JSONdata->UUID |
||
224 | ) return true; |
||
225 | } |
||
226 | return false; |
||
227 | } |
||
228 | |||
229 | ########################################################################### |
||
230 | ## DEMO ## |
||
231 | ########################################################################### |
||
232 | |||
233 | #### |
||
234 | # I. Read the configuration file. |
||
235 | $doc = new DomDocument('1.0'); |
||
236 | # This is needed to preserve the indenting of Corrade.ini |
||
237 | $doc->formatOutput = true; |
||
238 | $doc->preserveWhiteSpace = false; |
||
239 | $doc->load('Corrade.ini'); |
||
240 | |||
241 | #### |
||
242 | # II. Create a new group if it does not already exist. |
||
243 | switch(DoesCorradeGroupExist($doc, $_POST['group'])) { |
||
244 | case false: # only add the group if it does not exist |
||
245 | $doc = AddCorradeGroup($doc, $_POST['group']); |
||
246 | break; |
||
247 | default: # the group already exists - panic! |
||
248 | break; |
||
249 | } |
||
250 | |||
251 | #### |
||
252 | # III. Save the configuration file. |
||
253 | $doc->save('Corrade.ini'); |
||
254 | |||
255 | return 0; |
||
256 | |||
257 | |||
258 | ?> |