-
Notifications
You must be signed in to change notification settings - Fork 988
/
DemomodeCommand.php
164 lines (148 loc) · 8.58 KB
/
DemomodeCommand.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
<?php
/*
* LimeSurvey (tm)
* Copyright (C) 2011 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*/
class DemomodeCommand extends CConsoleCommand
{
public function run($sArgument)
{
if (isset($sArgument) && isset($sArgument[0]) && $sArgument[0] = 'yes') {
echo "\n###### Restoring installation to demomode #####\n";
echo "|| Resetting Database\n";
$this->_resetDatabase();
echo "|| Resetting Files\n";
$this->_resetFiles();
echo "|| Installing demo surveys\n";
$this->_createDemo();
echo "##### Done recreating demo state #####\n";
} else {
// TODO: a valid error process
echo 'This CLI command wipes a LimeSurvey installation clean (including all user except for the user ID 1 and user-uploaded content). For security reasons this command can only started if you add the parameter \'yes\' to the command line.';
}
}
private function _resetDatabase()
{
Yii::import('application.helpers.common_helper', true);
Yii::import('application.helpers.database_helper', true);
//Truncate most of the tables
$truncatableTables = [
'{{assessments}}', '{{answers}}', '{{answer_l10ns}}', '{{boxes}}', '{{conditions}}', '{{defaultvalues}}', '{{defaultvalue_l10ns}}', '{{labels}}', '{{label_l10ns}}', '{{labelsets}}', '{{groups}}', '{{questions}}', '{{question_l10ns}}', '{{surveys}}', '{{surveys_languagesettings}}', '{{quota}}', '{{quota_members}}', '{{quota_languagesettings}}', '{{question_attributes}}', '{{quota}}', '{{quota_members}}', '{{quota_languagesettings}}', '{{question_attributes}}', '{{user_groups}}', '{{user_in_groups}}', '{{templates}}', '{{template_configuration}}', '{{participants}}', '{{participant_attribute_names}}', '{{participant_attribute_names_lang}}', '{{participant_attribute_values}}', '{{participant_shares}}', '{{settings_user}}', '{{failed_login_attempts}}', '{{saved_control}}', '{{survey_links}}'
];
foreach ($truncatableTables as $table) {
$quotedTable = Yii::app()->db->quoteTableName($table);
$actquery = "truncate table " . $quotedTable;
Yii::app()->db->createCommand($actquery)->execute();
}
//Now delete the basics in all other tables
$actquery = "delete from {{permissions}} where uid<>1";
Yii::app()->db->createCommand($actquery)->execute();
$actquery = "delete from {{users}} where uid<>1";
Yii::app()->db->createCommand($actquery)->execute();
$actquery = "update {{users}} set lang='en'";
Yii::app()->db->createCommand($actquery)->execute();
$actquery = "update {{users}} set lang='auto'";
Yii::app()->db->createCommand($actquery)->execute();
$actquery = "delete from {{settings_global}} where stg_name LIKE 'last_question%'";
Yii::app()->db->createCommand($actquery)->execute();
$actquery = "delete from {{settings_global}} where stg_name LIKE 'last_survey%'";
Yii::app()->db->createCommand($actquery)->execute();
$actquery = "update {{users}} set email = 'test@domain.test', full_name='Administrator'";
Yii::app()->db->createCommand($actquery)->execute();
$actquery = "update {{settings_global}} set stg_value='' where stg_name='googleanalyticsapikey' or stg_name='googleMapsAPIKey' or stg_name='googletranslateapikey' or stg_name='ipInfoDbAPIKey' or stg_name='pdfheadertitle' or stg_name='pdfheaderstring'";
Yii::app()->db->createCommand($actquery)->execute();
$actquery = "update {{settings_global}} set stg_value='test@domain.test' where stg_name='siteadminbounce' or stg_name='siteadminemail'";
Yii::app()->db->createCommand($actquery)->execute();
$actquery = "update {{settings_global}} set stg_value='Administrator' where stg_name='siteadminname'";
Yii::app()->db->createCommand($actquery)->execute();
$actquery = "update {{settings_global}} set stg_value='Sea_Green' where stg_name='admintheme'";
Yii::app()->db->createCommand($actquery)->execute();
$surveyidresult = dbGetTablesLike("tokens%");
foreach ($surveyidresult as $sv) {
Yii::app()->db->createCommand("drop table " . $sv)->execute();
}
$surveyidresult = dbGetTablesLike("old\_%");
foreach ($surveyidresult as $sv) {
Yii::app()->db->createCommand("drop table " . $sv)->execute();
}
$surveyidresult = dbGetTablesLike("survey\_%");
foreach ($surveyidresult as $sv) {
if (strpos($sv, 'survey_links') === false && strpos($sv, 'survey_url_parameters') === false) {
Yii::app()->db->createCommand("drop table " . $sv)->execute();
}
}
//Add the general boxes again
foreach ($templateData = LsDefaultDataSets::getBoxesData() as $boxes) {
Yii::app()->db->createCommand()->insert("{{boxes}}", $boxes);
}
// At last reset the basic themes
foreach ($templateData = LsDefaultDataSets::getTemplatesData() as $template) {
Yii::app()->db->createCommand()->insert("{{templates}}", $template);
}
foreach ($templateConfigurationData = LsDefaultDataSets::getTemplateConfigurationData() as $templateConfiguration) {
Yii::app()->db->createCommand()->insert("{{template_configuration}}", $templateConfiguration);
}
}
private function _resetFiles()
{
$sBaseUploadDir = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'upload';
SureRemoveDir($sBaseUploadDir . DIRECTORY_SEPARATOR . 'surveys', false, ['index.html']);
SureRemoveDir($sBaseUploadDir . DIRECTORY_SEPARATOR . 'global', false, ['index.html']);
SureRemoveDir($sBaseUploadDir . DIRECTORY_SEPARATOR . 'templates', false);
SureRemoveDir($sBaseUploadDir . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . 'survey', false, ['index.html']);
SureRemoveDir($sBaseUploadDir . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . 'question', false);
}
private function _createDemo()
{
Yii::app()->loadHelper('admin/import');
require_once(dirname(dirname(dirname(__FILE__))) . '/application/helpers/replacements_helper.php');
require_once(dirname(dirname(dirname(__FILE__))) . '/application/helpers/expressions/em_manager_helper.php');
require_once(dirname(dirname(dirname(__FILE__))) . '/application/helpers/expressions/em_core_helper.php');
require_once(dirname(dirname(dirname(__FILE__))) . '/application/helpers/admin/activate_helper.php');
Yii::app()->session->add('loginID', 1);
$documentationSurveyPath = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . 'docs' . DIRECTORY_SEPARATOR . 'demosurveys' . DIRECTORY_SEPARATOR;
$aSamplesurveys = scandir($documentationSurveyPath);
$surveysToActivate = [];
foreach ($aSamplesurveys as $sSamplesurvey) {
$result = null;
//Try catch for console application to be able to import surveys
$result = @ XMLImportSurvey($documentationSurveyPath . $sSamplesurvey);
if (in_array($sSamplesurvey, ['ls205_sample_survey_multilingual.lss', 'ls205_randomization_group_test.lss', 'ls205_cascading_array_filter_exclude.lss'])) {
$surveysToActivate[] = $result['newsid'];
}
}
//require_once(__DIR__ . '/../helpers/admin/activate_helper.php');
foreach ($surveysToActivate as $surveyID) {
$survey = \Survey::model()->findByPk($surveyID);
$surveyActivator = new SurveyActivator($survey);
$result = $surveyActivator->activate();
}
}
}
function SureRemoveDir($dir, $DeleteMe, $excludes = [])
{
if (!$dh = @opendir($dir)) {
return;
}
while (false !== ($obj = readdir($dh))) {
if ($obj == '.' || $obj == '..' || in_array($obj, $excludes)) {
continue;
}
if (!@unlink($dir . '/' . $obj)) {
SureRemoveDir($dir . '/' . $obj, true);
}
}
closedir($dh);
if ($DeleteMe) {
if (!@rmdir($dir)) {
echo "Error: could not delete " . $dir;
}
}
}