-
Notifications
You must be signed in to change notification settings - Fork 987
/
TemplateConfiguration.php
471 lines (422 loc) · 21.9 KB
/
TemplateConfiguration.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
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* LimeSurvey
* Copyright (C) 2007-2015 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.
*/
/**
* Template Configuration Model
*
* This model retrieves all the data of template configuration from the configuration file
*
* @package LimeSurvey
* @subpackage Backend
*/
class TemplateConfiguration extends CFormModel
{
/** @var string $sTemplateName The template name */
public $sTemplateName='';
/** @var string $iSurveyId The current Survey Id. It can be void. It's use only to retreive the current template of a given survey */
public $iSurveyId='';
/** @var SimpleXMLElement $config Will contain the config.xml*/
public $config;
/**
* @var integer $apiVersion: Version of the LS API when created. Must be private : disallow update
*/
private $apiVersion;
/** @var string $viewPath Path of the views files (php files to replace existing core views) */
public $viewPath;
/** @var string $siteLogo Name of the logo file (like: logo.png) */
public $siteLogo;
/** @var string $filesPath Path of the uploaded files */
public $filesPath;
/**
* @var string[] $cssFramework What framework css is used
* @see getFrameworkPackages()
*/
public $cssFramework;
/** @var stdClass[] $packages Array of package dependencies defined in config.xml*/
public $packages;
/**
* @var string[] $depends List of all dependencies (could be more that just the config.xml packages)
* @see getDependsPackages()
*/
public $depends;
/**
* @var array $otherFiles Array of files in the file directory
* @see setOtherFiles()
*/
public $otherFiles;
public $sTemplateurl;
/** @var Survey $oSurvey The survey object */
public $oSurvey;
/** @var boolean $isStandard Is this template a core one? */
public $isStandard;
/** @var string $path Path of this template */
public $path;
/**
* @var string $hasConfigFile Does it has a config.xml file?
* //TODO why string not boolean ??
*/
public $hasConfigFile='';//
/** @var bool $overwrite_question_views Does it overwrites the question rendering from quanda.php? Must have a valid viewPath too. */
public $overwrite_question_views=false;
/** @var string $xmlFile What xml config file does it use? (config/minimal) */
public $xmlFile;
/**
* This method construct a template object, having all the needed configuration datas.
* It checks if the required template is a core one or a user one.
* If it's a user template, it will check if it's an old 2.0x template to provide default configuration values corresponding to the old template system
* If it's not an old template, it will check if it has a configuration file to load its datas.
* If it's not the case (template probably doesn't exist), it will load the default template configuration
* TODO : more tests should be done, with a call to private function _is_valid_template(), testing not only if it has a config.xml, but also id this file is correct, if it has the needed layout files, if the files refered in css exist, etc.
*
* @param string $sTemplateName the name of the template to load. The string come from the template selector in survey settings
* @param string $iSurveyId the id of the survey. If
* @return $this
*/
public function setTemplateConfiguration($sTemplateName='', $iSurveyId='')
{
// If it's called from template editor, a template name will be provided.
// If it's called for survey taking, a survey id will be provided
if ($sTemplateName == '' && $iSurveyId == '') {
/* Some controller didn't test completely survey id (PrintAnswersController for example), then set to default here */
$sTemplateName=Template::templateNameFilter(Yii::app()->getConfig('defaulttemplate','default'));
//throw new TemplateException("Template needs either template name or survey id");
}
$this->sTemplateName = $sTemplateName;
$this->iSurveyId = (int) $iSurveyId;
if ($sTemplateName=='') {
$this->oSurvey = Survey::model()->findByPk($iSurveyId);
if($this->oSurvey) {
$this->sTemplateName = $this->oSurvey->template;
} else {
$this->sTemplateName = Template::templateNameFilter(App()->getConfig('defaulttemplate','default'));
}
}
// We check if it's a CORE template
$this->isStandard = $this->setIsStandard();
// If the template is standard, its root is based on standardtemplaterootdir, else, it's a user template, its root is based on usertemplaterootdir
$this->path = ($this->isStandard)?Yii::app()->getConfig("standardtemplaterootdir").DIRECTORY_SEPARATOR.$this->sTemplateName:Yii::app()->getConfig("usertemplaterootdir").DIRECTORY_SEPARATOR.$this->sTemplateName;
// If the template directory doesn't exist, it can be that:
// - user deleted a custom theme
// In any case, we just set Default as the template to use
if (!is_dir($this->path)) {
$this->sTemplateName = 'default';
$this->isStandard = true;
$this->path = Yii::app()->getConfig("standardtemplaterootdir").DIRECTORY_SEPARATOR.$this->sTemplateName;
if(!$this->iSurveyId){
setGlobalSetting('defaulttemplate', 'default');
}
}
// If the template don't have a config file (maybe it has been deleted, or whatever),
// then, we load the default template
$this->hasConfigFile = (string) is_file($this->path.DIRECTORY_SEPARATOR.'config.xml');
if (!$this->hasConfigFile) {
$this->path = Yii::app()->getConfig("standardtemplaterootdir").DIRECTORY_SEPARATOR.$this->sTemplateName;
}
$this->xmlFile = $this->path.DIRECTORY_SEPARATOR.'config.xml';
//////////////////////
// Config file loading
$bOldEntityLoaderState = libxml_disable_entity_loader(true); // @see: http://phpsecurity.readthedocs.io/en/latest/Injection-Attacks.html#xml-external-entity-injection
$sXMLConfigFile = file_get_contents( realpath ($this->xmlFile)); // @see: Now that entity loader is disabled, we can't use simplexml_load_file; so we must read the file with file_get_contents and convert it as a string
// Using PHP >= 5.4 then no need to decode encode + need attributes : then other function if needed :https://secure.php.net/manual/en/book.simplexml.php#108688 for example
$this->config = simplexml_load_string($sXMLConfigFile);
// Template configuration
// Ternary operators test if configuration entry exists in the config file (to avoid PHP notice in user custom templates)
$this->apiVersion = (isset($this->config->metadatas->apiVersion)) ? $this->config->metadatas->apiVersion:0;
$this->viewPath = (isset($this->config->engine->viewdirectory)) ? $this->path.DIRECTORY_SEPARATOR.$this->config->engine->viewdirectory.DIRECTORY_SEPARATOR : '';
$this->siteLogo = (isset($this->config->files->logo)) ? $this->config->files->logo->filename : '';
$this->filesPath = (isset($this->config->engine->filesdirectory)) ? $this->path.DIRECTORY_SEPARATOR.$this->config->engine->filesdirectory.DIRECTORY_SEPARATOR : $this->path . '/files/';
$this->cssFramework = (isset($this->config->engine->cssframework)) ? $this->config->engine->cssframework : '';
$this->cssFramework->name = (isset($this->config->engine->cssframework->name)) ? $this->config->engine->cssframework->name : (string)$this->config->engine->cssframework;
$this->packages = (isset($this->config->engine->packages)) ? $this->config->engine->packages : array();
/* Add depend package according to packages */
$this->depends = $this->getDependsPackages();
$this->otherFiles = $this->setOtherFiles();
// Package creation
$this->createTemplatePackage();
libxml_disable_entity_loader($bOldEntityLoaderState); // Put back entity loader to its original state, to avoid contagion to other applications on the server
return $this;
}
/**
* Update the configuration file "last update" node.
* For now, it's called only from template editor
*/
public function actualizeLastUpdate()
{
$date = date("Y-m-d H:i:s");
$config = simplexml_load_file(realpath ($this->xmlFile));
$config->metadatas->last_update = $date;
$config->asXML( realpath ($this->xmlFile) ); // Belt
touch ( $this->path ); // & Suspenders ;-)
}
public function registerAssets()
{
if(!YII_DEBUG || Yii::app()->getConfig('use_asset_manager')){
Yii::app()->clientScript->registerPackage( 'survey-template' );
}else{
$aDepends = $this->getRecursiveDependencies('survey-template');
// CONVERT ALL PACKAGE IN $aDepend to BASE URL instead of PATH
foreach($aDepends as $package){
$aOldPackageDefinition = Yii::app()->clientScript->packages[$package];
// This will overwrite the package definition using a base url instead of a base path
if( array_key_exists('devBaseUrl', $aOldPackageDefinition ) ){
Yii::app()->clientScript->addPackage( $package, array(
'baseUrl' => $aOldPackageDefinition['devBaseUrl'], // Don't use asset manager
'css' => array_key_exists('css', $aOldPackageDefinition)?$aOldPackageDefinition['css']:array(),
'js' => array_key_exists('js', $aOldPackageDefinition)?$aOldPackageDefinition['js']:array(),
'depends' => array_key_exists('depends', $aOldPackageDefinition)?$aOldPackageDefinition['depends']:array(),
) );
}
}
Yii::app()->clientScript->registerPackage( 'survey-template' );
}
}
/**
* Return a list of all the recursive dependencies of a packages
* eg: If a package A depends on B, and B depends on C, getRecursiveDependencies('A') will return {B,C}
*/
public function getRecursiveDependencies($sPackageName)
{
$aPackages = Yii::app()->clientScript->packages;
if ( array_key_exists('depends', $aPackages[$sPackageName]) ){
$aDependencies = $aPackages[$sPackageName]['depends'];
foreach ($aDependencies as $sDpackageName){
if($aPackages[$sPackageName]['depends']){
$aRDependencies = $this->getRecursiveDependencies($sDpackageName); // Recursive call
if (is_array($aRDependencies)){
$aDependencies = array_unique(array_merge($aDependencies, $aRDependencies));
}
}
}
return $aDependencies;
}
}
/**
* Create a package for the asset manager.
* The asset manager will push to tmp/assets/xyxyxy/ the whole template directory (with css, js, files, etc.)
* And it will publish the CSS and the JS defined in config.xml. So CSS can use relative path for pictures.
* The publication of the package itself is done for now in replacements_helper, to respect the old logic of {TEMPLATECSS} replacement keyword
*
* NOTE 1 : To refresh the assets, the base directory of the template must be updated.
*
* NOTE 2: By default, Asset Manager is off when debug mode is on.
* Developers should then think about :
* 1. refreshing their brower's cache (ctrl + F5) to see their changes
* 2. update the config.xml last_update before pushing, to be sure that end users will have the new version
*
*
* For more detail, see :
* http://www.yiiframework.com/doc/api/1.1/CClientScript#addPackage-detail
* http://www.yiiframework.com/doc/api/1.1/YiiBase#setPathOfAlias-detail
*
*/
private function createTemplatePackage()
{
Yii::setPathOfAlias('survey.template.path', $this->path); // The package creation/publication need an alias
Yii::setPathOfAlias('survey.template.viewpath', $this->viewPath);
$aCssFiles = (array)$this->config->files->css->filename; // The CSS files of this template
$aJsFiles = (array)$this->config->files->js->filename; // The JS files of this template
$dir=getLanguageRTL(App()->language) ? 'rtl' : 'ltr';
if (isset($this->config->files->$dir)) {
$aCssFilesDir = isset($this->config->files->$dir->css->filename) ? (array)$this->config->files->$dir->css->filename : array();
$aJsFilesDir = isset($this->config->files->$dir->js->filename) ? (array)$this->config->files->$dir->js->filename : array();
$aCssFiles=array_merge($aCssFiles,$aCssFilesDir);
$aJsFiles=array_merge($aJsFiles,$aJsFilesDir);
}
if (Yii::app()->getConfig('debug') == 0) {
Yii::app()->clientScript->registerScriptFile( Yii::app()->getConfig("generalscripts"). 'deactivatedebug.js', CClientScript::POS_END);
}
// The package "survey-template" will be available from anywhere in the app now.
// To publish it : Yii::app()->clientScript->registerPackage( 'survey-template' );
// It will create the asset directory, and publish the css and js files
if(!YII_DEBUG || Yii::app()->getConfig('use_asset_manager')){
Yii::app()->clientScript->addPackage( 'survey-template', array(
'basePath' => 'survey.template.path', // Use asset manager
'css' => $aCssFiles,
'js' => $aJsFiles,
'depends' => $this->depends,
) );
}else{
$sTemplateurl = $this->getTemplateURL();
Yii::app()->clientScript->addPackage( 'survey-template', array(
'baseUrl' => $sTemplateurl, // Don't use asset manager
'css' => $aCssFiles,
'js' => $aJsFiles,
'depends' => $this->depends,
) );
}
}
/**
* Return the list of ALL files present in the file directory
* @return array
*/
private function setOtherFiles()
{
$otherFiles = array();
if ( file_exists($this->filesPath) && $handle = opendir($this->filesPath)) {
while (false !== ($file = readdir($handle))) {
if($file!='.' && $file!='..') {
if (!is_dir($file)) {
$otherFiles[] = array("name" => $file);
}
}
}
closedir($handle);
}
return $otherFiles;
}
public function getName()
{
return $this->sTemplateName;
}
/**
* @return bool
*/
private function setIsStandard()
{
return Template::isStandardTemplate($this->sTemplateName);
}
/**
* This function returns the complete URL path to a given template name
*
* @param string $sTemplateName
* @return string template url
*/
public function getTemplateURL()
{
if(!isset($this->sTemplateurl)){
$this->sTemplateurl = Template::getTemplateURL($this->sTemplateName);
}
return $this->sTemplateurl;
}
/**
* Get the depends package
* @uses self::@package
* @return string[]
*/
private function getDependsPackages()
{
/* Start by adding cssFramework package */
$packages=$this->getFrameworkPackages();
if(!getLanguageRTL(App()->getLanguage())) {
$packages=array_merge ($packages,$this->getFrameworkPackages('ltr'));
} else {
$packages=array_merge ($packages,$this->getFrameworkPackages('rtl'));
}
/* Core package */
$packages[]='limesurvey-public';
/* template packages */
if(!empty($this->packages->package)) {
$packages=array_merge ($packages,(array)$this->packages->package);
}
/* Adding rtl/tl specific package (see https://bugs.limesurvey.org/view.php?id=11970#c42317 ) */
$dir=getLanguageRTL(App()->language) ? 'rtl' : 'ltr';
if(!empty($this->packages->$dir->package)) {
$packages=array_merge ($packages,(array)$this->packages->$dir->package);
}
return $packages;
}
/**
* Set the framework package
* @param string $dir (rtl|ltr|)
* @use self::@cssFramework
* @return string[] depends for framework
*/
private function getFrameworkPackages($dir="")
{
$framework=isset($this->cssFramework->name)? (string)$this->cssFramework->name : (string)$this->cssFramework;
$framework=$dir ? $framework."-".$dir : $framework;
if(isset(Yii::app()->clientScript->packages[$framework])) {
$frameworkPackages=array();
/* Theming */
if($dir) {
$cssFrameworkCsss=isset($this->cssFramework->$dir->css) ? $this->cssFramework->$dir->css : array();
$cssFrameworkJss=isset($this->cssFramework->$dir->js) ? $this->cssFramework->$dir->js : array();
} else {
$cssFrameworkCsss=isset($this->cssFramework->css) ? $this->cssFramework->css : array();
$cssFrameworkJss=isset($this->cssFramework->js) ? $this->cssFramework->js : array();
}
if(empty($cssFrameworkCsss) && empty($cssFrameworkJss)) {
$frameworkPackages[]=$framework;
} else {
/* Need to create an adapted core framework */
$cssFrameworkPackage=Yii::app()->clientScript->packages[$framework];
/* Need to create an adapted template/theme framework */
$packageCss=array();
$packageJs=array();
/* css file to replace from default package */
$cssDelete=array();
foreach($cssFrameworkCsss as $cssFrameworkCss) {
if(isset($cssFrameworkCss['replace'])) {
$cssDelete[]=$cssFrameworkCss['replace'];
}
if((string)$cssFrameworkCss) {
$packageCss[]=(string)$cssFrameworkCss;
}
}
if(isset($cssFrameworkPackage['css'])) {
$cssFrameworkPackage['css']=array_diff($cssFrameworkPackage['css'],$cssDelete);
}
$jsDelete=array();
foreach($cssFrameworkJss as $cssFrameworkJs) {
if(isset($cssFrameworkJs['replace'])) {
$jsDelete[]=$cssFrameworkJs['replace'];
}
if((string)$cssFrameworkJs) {
$packageJs[]=(string)$cssFrameworkJs;
}
}
if(isset($cssFrameworkPackage['js'])) {
$cssFrameworkPackage['js']=array_diff($cssFrameworkPackage['js'],$cssDelete);
}
/* And now : we add : core package fixed + template/theme package */
Yii::app()->clientScript->packages[$framework]=$cssFrameworkPackage; /* @todo : test if empty css and js : just add depends if yes */
$aDepends=array(
$framework,
);
if (!YII_DEBUG || Yii::app()->getConfig('use_asset_manager')){
Yii::app()->clientScript->addPackage(
$framework.'-template', array(
'basePath' => 'survey.template.path', // basePath: the asset manager will be used
'css' => $packageCss,
'js' => $packageJs,
'depends' => $aDepends,
)
);
}else{
$sTemplateurl = $this->getTemplateURL();
Yii::app()->clientScript->addPackage(
$framework.'-template', array(
'baseUrl' => $sTemplateurl, // Don't use asset manager
'css' => $packageCss,
'js' => $packageJs,
'depends' => $aDepends,
)
);
}
$frameworkPackages[]=$framework.'-template';
}
return $frameworkPackages;
}/*elseif($framework){
throw error ? Only for admin template editor ? disable and reset to default ?
}*/
return array();
}
/**
* get the template API version
* @return integer
*/
public function getApiVersion()
{
return $this->apiVersion;
}
}