forked from phpbb/support-toolkit
/
srt_generator.php
627 lines (556 loc) · 15.8 KB
/
srt_generator.php
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<?php
/**
*
* @package Support Toolkit - Support Request Generator
* @version $Id$
* @copyright (c) 2009 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
exit;
}
class srt_generator
{
/**
* @var Integer The current step
* @access public
*/
var $step = 0;
/**
* The data array contains all data required to build the SRT questions,
* for each question the following keys can be used
* <code>
* 'name' => Required, the identifier that is used for everything
* (field names/langauge entries/etc)
* 'type' => Required, the type of the input field. (dropdown ||
* text || texteara || boolean)
* 'options' => Only used when 'type' = dropdown. If set these
* values are used as options for the dropdown box, if
* not set the options will be red from the language files.
* 'p_callback' => php callback. This function/method is called before
* the question is added to the template. If the function
* returns everything else than the boolean `false`
* that value will be used as answer. If it returns
* false (strict check) the question shall be displayed.
* 'hide' => This question is only asked but will not be used
* when compiling the SRT.
* 'depends' => This question is only asked/handled if the given question
* was answered with "true", this currently only supports
* a boolean check.
* </code>
*
* @var Array The data that is used to generate the questions in the template
* The array is prefilled with the main data which is filtered later
* @access private
*/
var $_data = array();
/**
* This method is always called, so we "abuse"
* it for this tool to setup some stuff we need
* later on.
*/
function tool_active()
{
global $config;
// This tool relies on the cache, if the user has the `NULL`
// cache enabled he can't use this!
global $acm_type;
if ($acm_type == 'null')
{
return 'SRT_NO_CACHE';
}
// Set the step
$this->step = request_var('step', 0);
if ($this->step < 0 || $this->step > 4 || !empty($_POST['reset']))
{
$this->step = 0;
}
// Step one is something different, don't need any more
if ($this->step == 0)
{
return true;
}
// Fill the data array
$this->_data = array(
'step2' => array(
array(
'name' => 'phpbb_version',
'type' => 'dropdown',
'options' => array(
'phpBB ' . $config['version'],
'phpBB ' . PHPBB_VERSION,
),
'p_callback' => array($this, '_prefill_phpbb_version'),
),
array(
'name' => 'board_url',
'type' => 'text',
'p_callback' => 'generate_board_url',
),
array(
'name' => 'dbms',
'type' => 'text',
'p_callback' => array($this, '_prefill_dbms'),
),
array(
'name' => 'php',
'type' => 'text',
'p_callback' => PHP_VERSION,
),
array(
'name' => 'host_name',
'type' => 'text',
),
array(
'name' => 'install_type',
'type' => 'dropdown',
),
array(
'name' => 'inst_converse',
'type' => 'dropdown',
),
array(
'name' => 'mods_installed',
'type' => 'boolean',
'p_callback' => array($this, '_prefill_has_mods_installed'),
'hide' => true,
),
array(
'name' => 'registration_req',
'type' => 'boolean',
'hide' => true,
),
),
'step3' => array(
array(
'name' => 'installed_mods',
'type' => 'textarea',
'depends' => 'mods_installed',
'p_callback' => array($this, '_prefill_installed_mods'),
),
array(
'name' => 'installed_styles',
'type' => 'textarea',
'p_callback' => array($this, '_prefill_installed_styles'),
),
array(
'name' => 'installed_languages',
'type' => 'textarea',
'p_callback' => array($this, '_prefill_installed_languages'),
),
array(
'name' => 'xp_level',
'type' => 'dropdown',
),
array(
'name' => 'test_username',
'type' => 'text',
'depends' => 'registration_req',
),
array(
'name' => 'test_password',
'type' => 'text',
'depends' => 'registration_req',
),
array(
'name' => 'problem_started',
'type' => 'textarea',
),
array(
'name' => 'problem_description',
'type' => 'textarea',
),
),
);
// Have to return here otherwise shit breaks
return true;
}
/**
* Generate all the Q/A pages
*/
function display_options()
{
global $cache, $template, $user;
// Step 0 is easy
if ($this->step == 0)
{
return 'SRT_GENERATOR_LANDING';
}
// Step 1 only needs a single template var set
if ($this->step == 1)
{
$template->assign_var('S_STEP1', true);
}
// Step 4 builds the result
elseif ($this->step == 4)
{
$this->_build_srt();
}
// Got to do a bit more
else
{
// Fetch the cached results, might need it here
$_previous_data = $cache->get('_stk_srt_generator');
// Run through the questions
$_prefilled = array();
foreach ($this->_data["step{$this->step}"] as $question)
{
// Some questions are only asked when certain answers are given earlier
if (isset($question['depends']))
{
// Only support booleans atm
if (empty($_previous_data[$question['depends']]))
{
continue;
}
}
// First call the prefill if there is one
$_p_callback_result = false;
if (isset($question['p_callback']) && is_callable($question['p_callback']))
{
$_p_callback_result = call_user_func($question['p_callback']);
}
elseif (!empty($question['p_callback']))
{
$_p_callback_result = $question['p_callback'];
}
// If there is a prefill result use that as answer
// Handle MODs a bit different (ugly :/)
if ($_p_callback_result !== false && $question['name'] != 'installed_mods')
{
$_prefilled[$question['name']] = $_p_callback_result;
}
// Show the question in the template
else
{
// If this is a dropdown generate the options.
if ($question['type'] == 'dropdown')
{
// Options can be passed ether by an array in the data list or are generated from the language file
$options = (!empty($question['options'])) ? $question['options'] : array();
$this->_format_options($options, $question['name']);
}
$template->assign_block_vars('questionrow', array(
'EXPLAIN' => (!empty($user->lang['SRT_QUESTIONS_EXPLAIN']["step{$this->step}"][$question['name']])) ? $user->lang['SRT_QUESTIONS_EXPLAIN']["step{$this->step}"][$question['name']] : '',
'NAME' => $question['name'],
'OPTIONS' => ($question['type'] == 'dropdown') ? $options : '',
'PREFILL' => $_p_callback_result,
'QUESTION' => $user->lang['SRT_QUESTIONS']["step{$this->step}"][$question['name']],
'TYPE' => $question['type'],
));
}
}
}
$template->assign_vars(array(
'S_PREFILL' => (!empty($_prefilled)) ? htmlspecialchars(serialize($_prefilled)) : '',
'U_ACTION' => append_sid(STK_INDEX, array('c' => 'main', 't' => 'srt_generator', 'step' => $this->step)),
));
// Spit out teh page
page_header(user_lang('SRT_GENERATOR'));
$template->set_filenames(array(
'body' => 'tools/srt_generator.html',
));
page_footer();
}
/**
* Handle a submit
*/
function run_tool()
{
global $cache, $user;
// Step 0 is a special place to be, only available for special people
// this user isn't special so kick him to the user spot
if ($this->step == 0)
{
// Distory the previous data
$cache->destroy('_stk_srt_generator');
}
// In the first step only have to trigger errors
else if ($this->step == 1)
{
global $template;
// Disable the back link
$template->assign_var('U_BACK_TOOL', false);
if (!empty($_POST['srt']['mod_related']) && $_POST['srt']['mod_related'] == '1')
{
trigger_error('STEP1_MOD_ERROR');
}
if (!empty($_POST['srt']['hacked']) && $_POST['srt']['hacked'] == '1')
{
trigger_error('STEP1_HACKED_ERROR');
}
}
// Handle the submitted stuf
else
{
$_cached_data = $cache->get('_stk_srt_generator');
$_prefilled = unserialize(htmlspecialchars_decode(request_var('prefill', '', true)));
// $_cached_data can be "false"
if ($_cached_data === false)
{
$_cached_data = array();
}
// Run through the questions and fetch the answers
$_answers = array();
foreach ($this->_data["step{$this->step}"] as $question)
{
// From the prefill?
if (isset($_prefilled[$question['name']]))
{
$_answers[$question['name']] = $_prefilled[$question['name']];
}
else
{
$_answers[$question['name']] = request_var($question['name'], '', true);
}
// For some types some aditional stuff is needed here
switch ($question['type'])
{
case 'boolean' :
// When fetched from the form these are "yes" or "no",
// convert to booleans
if (in_array($_answers[$question['name']], array('yes', 'no')))
{
$_answers[$question['name']] = ($_answers[$question['name']] == 'yes') ? true : false;
}
break;
case 'dropdown' :
// The correct one
if (!empty($question['options']))
{
$_answers[$question['name']] = $question['options'][$_answers[$question['name']]];
}
else
{
$_answers[$question['name']] = $user->lang['SRT_DROPDOWN_OPTIONS']["step{$this->step}"][$question['name']][$_answers[$question['name']]];
}
break;
}
}
// Place back in the cache
$_cached_data = array_merge($_cached_data, $_answers);
$cache->put('_stk_srt_generator', $_cached_data);
}
// Next one pleaze
redirect(append_sid(STK_INDEX, array('c' => 'main', 't' => 'srt_generator', 'step' => ++$this->step)));
}
/**
* Build the SRT and output the result
*
* @return void
* @access private
*/
function _build_srt()
{
global $cache, $template, $user;
$_template = array();
// Get the submitted data
$_answers = $cache->get('_stk_srt_generator');
if ($_answers === false)
{
// Shouldn't happening in normal operation
trigger_error('COULDNT_LOAD_SRT_ANSWERS');
}
// Header
$_template[] = '[size=115][color=#368AD2][b]Support Request Template[/b][/color][/size]<br />';
// Walk through the data
foreach ($this->_data as $step => $questions)
{
// Do teh questions
foreach ($questions as $question)
{
// Hidden?
if (!empty($question['hide']))
{
continue;
}
$question_string = $user->lang['SRT_QUESTIONS'][$step][$question['name']];
// No answer, easy :)
if (empty($_answers[$question['name']]))
{
$_template[] = "[color=#cc6600][b]{$question_string}[/b][/color]: [i]No answer given[/i]";
continue;
}
$_template[] = "[color=#cc6600][b]{$question_string}[/b][/color]: {$_answers[$question['name']]}";
}
}
// Footer
$_template[] = '<br />[size=60]Generated by Support Toolkit SRT Generator.[/size]';
// Output
$user->add_lang('viewtopic');
$template->assign_var('COMPILED_TEMPLATE', nl2br(implode('<br />', $_template)));
}
/**
* This method build the options for dropdown boxes, if the $options paramater
* contains a non-empty array the data within this array will be used for the
* options. If the array is empty the options will be fetched from the language
* files.
*
* @param Array $options The array that will be used/filled with the
* options for the dropdown box
* @param String $name The name of the question, this is used to
* lookup the correct strings in the language
* files.
* @return void
* @access private
*/
function _format_options(&$options, $name = '')
{
global $user;
$_option_list = array();
if (!empty($options))
{
foreach ($options as $_key => $_value)
{
$_option_list[] = "<option value='{$_key}'>{$_value}</option>";
}
}
else
{
// If there is no language entry for this list return an empty array
if (!empty($user->lang['SRT_DROPDOWN_OPTIONS']["step{$this->step}"][$name]))
{
foreach ($user->lang['SRT_DROPDOWN_OPTIONS']["step{$this->step}"][$name] as $_key => $_value)
{
$_option_list[] = "<option value='{$_key}'>{$_value}</option>";
}
}
}
// Set the return
$options = implode('', $_option_list);
}
/**
* Some prefill methods
* @Note, due to strict checking if a prefill should return "false" you'll
* have to return the *string*, not the boolean!
*/
/**
* Just a wrapper to get the DBAL layer
*/
function _prefill_dbms()
{
global $db;
return $db->sql_server_info();
}
/**
* Try to determine whether the user has MODs installed. This can be prefilled
* if the user has AutoMOD installed *AND* he has installed at least one MOD
* with it
*
* @return Boolean True _only_ when the user has installed at least one
* MOD through AutoMOD, otherwise false.
*/
function _prefill_has_mods_installed()
{
if ((@include PHPBB_ROOT_PATH . 'includes/functions_mods.' . PHP_EXT) !== false)
{
// See whether there is something installed
global $db;
$sql = 'SELECT mod_name
FROM ' . MODS_TABLE;
$result = $db->sql_query_limit($sql, 1);
$mod = $db->sql_fetchfield('mod_name', false, $result);
$db->sql_freeresult($result);
if ($mod !== false)
{
return true;
}
}
return false;
}
/**
* Fetch all the installed languages from the database
*
* @return String All the languages installed on this board
* @access private
*/
function _prefill_installed_languages()
{
global $db;
$_languages = array();
$sql = 'SELECT lang_english_name
FROM ' . LANG_TABLE . '
ORDER BY lang_english_name';
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$_languages[] = $row['lang_english_name'];
}
$db->sql_freeresult($result);
return implode("\n", $_languages);
}
/**
* If the user has AutoMOD installed prefill the "Mods installed" field
* with all MODs know by AutoMOD.
*
* @return BooleanString False if no AutoMOD was found or when no
* MODs where installed with it. Otherwise all the
* MODs that where found in the db
* @access private
*/
function _prefill_installed_mods()
{
global $db;
if ((@include PHPBB_ROOT_PATH . 'includes/functions_mods.' . PHP_EXT) !== false)
{
$_mods = array();
$sql = 'SELECT mod_name, mod_version
FROM ' . MODS_TABLE;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$_mods[] = "{$row['mod_name']} ({$row['mod_version']})";
}
$db->sql_freeresult($result);
if (!empty($_mods))
{
return implode("\n", $_mods);
}
}
return false;
}
/**
* Fetch all the installed styles from the database
*
* @return Boolean|String False if for some reason no styles could be
* red from the database. Otherwise the data
* @access private
*/
function _prefill_installed_styles()
{
global $db;
$_styles = array();
$sql = 'SELECT style_name, style_active
FROM ' . STYLES_TABLE;
$result = $db->sql_query($sql);
while ($row = $db->sql_fetchrow($result))
{
$_styles[] = $row['style_name'] . (($row['style_active'] == 0) ? ' [i](not active)[/i]' : '');
}
$db->sql_freeresult($result);
return implode("\n", $_styles);
}
/**
* Test whether we can rely on the board to give us the version number
* This test fails if $config['version'] and PHPBB_VERSION differ.
*
* @return Boolean|String False if the version isn't relyable, otherwise
* the version number.
* @access private
*/
function _prefill_phpbb_version()
{
global $config;
if (version_compare(strtolower($config['version']), strtolower(PHPBB_VERSION), 'eq')) // use strtolower as my local php installation seems to think that x.y.z-PL1 != x.y.z-pl1
{
// Always point to the first option!
return 0;
}
return false;
}
}