GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 44a98c...636838 )
by gyeong-won
06:04
created

editorModel::getComponentListCacheKey()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) NAVER <http://www.navercorp.com> */
3
/**
4
 * @class  editorModel
5
 * @author NAVER ([email protected])
6
 * @brief model class of the editor odule
7
 */
8
class editorModel extends editor
9
{
10
	var $loaded_component_list = array();
11
	/**
12
	 * @brief Return the editor
13
	 *
14
	 * Editor internally generates editor_sequence from 1 to 30 for temporary use.
15
	 * That means there is a limitation that more than 30 editors cannot be displayed on a single page.
16
	 *
17
	 * However, editor_sequence can be value from getNextSequence() in case of the modified or the auto-saved for file upload
18
	 *
19
	 */
20
21
	/**
22
	 * @brief Return editor setting for each module
23
	 */
24
	function getEditorConfig($module_srl = null)
25
	{
26
		if(!$GLOBALS['__editor_module_config__'][$module_srl] && $module_srl)
27
		{
28
			// Get trackback settings of the selected module
29
			$oModuleModel = getModel('module');
30
			$GLOBALS['__editor_module_config__'][$module_srl] = $oModuleModel->getModulePartConfig('editor', $module_srl);
31
		}
32
		$editor_config = $GLOBALS['__editor_module_config__'][$module_srl];
33
34
		$oModuleModel = getModel('module');
35
		$editor_default_config = $oModuleModel->getModuleConfig('editor');
36
37
		if(!is_object($editor_config)) $editor_config = new stdClass();
38
39
		if($editor_config->enable_autosave != 'N') $editor_config->enable_autosave = 'Y';
40
		if(!is_array($editor_config->enable_html_grant)) $editor_config->enable_html_grant = array();
41
		if(!is_array($editor_config->enable_comment_html_grant)) $editor_config->enable_comment_html_grant = array();
42
		if(!is_array($editor_config->upload_file_grant)) $editor_config->upload_file_grant = array();
43
		if(!is_array($editor_config->comment_upload_file_grant)) $editor_config->comment_upload_file_grant = array();
44
		if(!is_array($editor_config->enable_default_component_grant)) $editor_config->enable_default_component_grant = array();
45
		if(!is_array($editor_config->enable_comment_default_component_grant)) $editor_config->enable_comment_default_component_grant = array();
46
		if(!is_array($editor_config->enable_component_grant)) $editor_config->enable_component_grant = array();
47
		if(!is_array($editor_config->enable_comment_component_grant)) $editor_config->enable_comment_component_grant= array();
48
49
		if(!$editor_config->editor_height)
50
		{
51
			$editor_config->editor_height = ($editor_default_config->editor_height) ? $editor_default_config->editor_height : 500;
52
		}
53
		if(!$editor_config->comment_editor_height)
54
		{
55
			$editor_config->comment_editor_height = ($editor_default_config->comment_editor_height) ? $editor_default_config->comment_editor_height : 120;
56
		}
57
		if(!$editor_config->editor_skin)
58
		{
59
			$editor_config->editor_skin = ($editor_default_config->editor_skin) ? $editor_default_config->editor_skin : 'ckeditor';
60
		}
61
		if(!$editor_config->comment_editor_skin)
62
		{
63
			$editor_config->comment_editor_skin = ($editor_default_config->comment_editor_skin) ? $editor_default_config->comment_editor_skin : 'ckeditor';
64
		}
65
		if(!$editor_config->content_style)
66
		{
67
			$editor_config->content_style = ($editor_default_config->content_style) ? $editor_default_config->content_style : 'ckeditor_light';
68
		}
69
		if(!$editor_config->content_font && $editor_default_config->content_font)
70
		{
71
			$editor_config->content_font = $editor_default_config->content_font;
72
		}
73
		if(!$editor_config->content_font_size && $editor_default_config->content_font_size)
74
		{
75
			$editor_config->content_font_size = $editor_default_config->content_font_size;
76
		}
77
		if(!$editor_config->sel_editor_colorset && $editor_default_config->sel_editor_colorset)
78
		{
79
			$editor_config->sel_editor_colorset = $editor_default_config->sel_editor_colorset;
80
		}
81
		if(!$editor_config->sel_comment_editor_colorset && $editor_default_config->sel_comment_editor_colorset)
82
		{
83
			$editor_config->sel_comment_editor_colorset = $editor_default_config->sel_comment_editor_colorset;
84
		}
85
		if(!$editor_config->comment_content_style && $editor_default_config->comment_content_style)
86
		{
87
			$editor_config->comment_content_style = $editor_default_config->comment_content_style;
88
		}
89
90
		return $editor_config;
91
	}
92
93
	function loadDrComponents()
94
	{
95
		$drComponentPath = _XE_PATH_ . 'modules/editor/skins/dreditor/drcomponents/';
96
		$drComponentList = FileHandler::readDir($drComponentPath);
97
98
		$oTemplate = &TemplateHandler::getInstance();
99
100
		$drComponentInfo = array();
101
		if($drComponentList)
0 ignored issues
show
Bug Best Practice introduced by
The expression $drComponentList of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
102
		{
103
			foreach($drComponentList as $i => $drComponent)
104
			{
105
				unset($obj);
106
				$obj = $this->getDrComponentXmlInfo($drComponent);
107
				Context::loadLang(sprintf('%s%s/lang/',$drComponentPath,$drComponent));
108
				$path = sprintf('%s%s/tpl/',$drComponentPath,$drComponent);
109
				$obj->html = $oTemplate->compile($path,$drComponent);
110
				$drComponentInfo[$drComponent] = $obj;
111
			}
112
		}
113
		Context::set('drComponentList',$drComponentInfo);
114
	}
115
116
	function getDrComponentXmlInfo($drComponentName)
117
	{
118
		$lang_type = Context::getLangType();
119
		// Get the xml file path of requested component
120
		$component_path = sprintf('%s/skins/dreditor/drcomponents/%s/', $this->module_path, $drComponentName);
121
122
		$xml_file = sprintf('%sinfo.xml', $component_path);
123
		$cache_file = sprintf('./files/cache/editor/dr_%s.%s.php', $drComponentName, $lang_type);
124
		// Return information after including it after cached xml file exists
125 View Code Duplication
		if(file_exists($cache_file) && file_exists($xml_file) && filemtime($cache_file) > filemtime($xml_file))
126
		{
127
			include($cache_file);
128
			return $xml_info;
0 ignored issues
show
Bug introduced by
The variable $xml_info does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
129
		}
130
		// Return after parsing and caching if the cached file does not exist
131
		$oParser = new XmlParser();
132
		$xml_doc = $oParser->loadXmlFile($xml_file);
133
134
		$component_info->component_name = $drComponentName;
0 ignored issues
show
Bug introduced by
The variable $component_info does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
135
		$component_info->title = $xml_doc->component->title->body;
136
		$component_info->description = str_replace('\n', "\n", $xml_doc->component->description->body);
137
		$component_info->version = $xml_doc->component->version->body;
138
		$component_info->date = $xml_doc->component->date->body;
139
		$component_info->homepage = $xml_doc->component->link->body;
140
		$component_info->license = $xml_doc->component->license->body;
141
		$component_info->license_link = $xml_doc->component->license->attrs->link;
142
143
		$buff = '<?php if(!defined("__XE__")) exit(); ';
144
		$buff .= sprintf('$xml_info->component_name = "%s";', $component_info->component_name);
145
		$buff .= sprintf('$xml_info->title = "%s";', $component_info->title);
146
		$buff .= sprintf('$xml_info->description = "%s";', $component_info->description);
147
		$buff .= sprintf('$xml_info->version = "%s";', $component_info->version);
148
		$buff .= sprintf('$xml_info->date = "%s";', $component_info->date);
149
		$buff .= sprintf('$xml_info->homepage = "%s";', $component_info->homepage);
150
		$buff .= sprintf('$xml_info->license = "%s";', $component_info->license);
151
		$buff .= sprintf('$xml_info->license_link = "%s";', $component_info->license_link);
152
153
		// Author information
154 View Code Duplication
		if(!is_array($xml_doc->component->author)) $author_list[] = $xml_doc->component->author;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$author_list was never initialized. Although not strictly required by PHP, it is generally a good practice to add $author_list = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
155
		else $author_list = $xml_doc->component->author;
156
157
		for($i=0; $i < count($author_list); $i++)
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
158
		{
159
			$buff .= sprintf('$xml_info->author['.$i.']->name = "%s";', $author_list[$i]->name->body);
160
			$buff .= sprintf('$xml_info->author['.$i.']->email_address = "%s";', $author_list[$i]->attrs->email_address);
161
			$buff .= sprintf('$xml_info->author['.$i.']->homepage = "%s";', $author_list[$i]->attrs->link);
162
		}
163
164
		// List extra variables (text type only in the editor component)
165
		$extra_vars = $xml_doc->component->extra_vars->var;
166
		if($extra_vars)
167
		{
168
			if(!is_array($extra_vars)) $extra_vars = array($extra_vars);
169
			foreach($extra_vars as $key => $val)
170
			{
171
				unset($obj);
172
				$key = $val->attrs->name;
173
				$title = $val->title->body;
174
				$description = $val->description->body;
175
				$xml_info->extra_vars->{$key}->title = $title;
176
				$xml_info->extra_vars->{$key}->description = $description;
177
178
				$buff .= sprintf('$xml_info->extra_vars->%s->%s = "%s";', $key, 'title', $title);
179
				$buff .= sprintf('$xml_info->extra_vars->%s->%s = "%s";', $key, 'description', $description);
180
			}
181
		}
182
183
		FileHandler::writeFile($cache_file, $buff, "w");
184
185
		unset($xml_info);
186
		include($cache_file);
187
		return $xml_info;
188
	}
189
190
	/**
191
	 * @brief Return the editor template
192
	 * You can call upload_target_srl when modifying content
193
	 * The upload_target_srl is used for a routine to check if an attachment exists
194
	 */
195
	function getEditor($upload_target_srl = 0, $option = null)
196
	{
197
		/**
198
		 * Editor's default options
199
		 */
200
		// Option setting to allow file upload
201
		if($upload_target_srl)
202
		{
203
			$option->editor_sequence = $upload_target_srl;
204
		}
205
		if(!$option->allow_fileupload) $allow_fileupload = false;
206
		else $allow_fileupload = true;
207
		// content_style setting
208
		if(!$option->content_style) $option->content_style = 'ckeditor_light';
209
		Context::set('content_style', $option->content_style);
210
		Context::set('content_style_path', getScriptPath() . ltrim($this->module_path, './') . 'styles/' . $option->content_style);
211
		// Default font setting
212
		Context::set('content_font', addslashes($option->content_font));
213
		Context::set('content_font_size', $option->content_font_size);
214
215
		// Option setting to allow auto-save
216
		if(!$option->enable_autosave) $enable_autosave = false;
217
		elseif(Context::get($option->primary_key_name)) $enable_autosave = false;
218
		else $enable_autosave = true;
219
		// Option setting to allow the default editor component
220
		if(!$option->enable_default_component) $enable_default_component = false;
221
		else $enable_default_component = true;
222
		// Option setting to allow other extended components
223
		if(!$option->enable_component) $enable_component = false;
224
		else $enable_component = true;
225
		// Setting for html-mode
226
		if($option->disable_html) $html_mode = false;
227
		else $html_mode = true;
228
		// Set Height
229
		if(!$option->height) $editor_height = 300;
230
		else $editor_height = $option->height;
231
		if(Mobile::isFromMobilePhone()) {
232
			$editor_height = 150;
233
		}
234
		// Skin Setting
235
		$skin = $option->skin;
236
		if(!$skin) $skin = 'ckeditor';
237
238
		$colorset = $option->colorset;
239
		if(!$colorset) $colorset = 'moono';
240
		Context::set('colorset', $colorset);
241
		Context::set('skin', $skin);
242
		Context::set('module_type', $option->module_type);
243
244
		if($skin=='dreditor')
245
		{
246
			$this->loadDrComponents();
247
		}
248
249
		/**
250
		 * Check the automatic backup feature (do not use if the post is edited)
251
		 */
252
		if($enable_autosave)
253
		{
254
			// Extract auto-saved data
255
			$saved_doc = $this->getSavedDoc($upload_target_srl);
256
			// Context setting auto-saved data
257
			Context::set('saved_doc', $saved_doc);
258
		}
259
		Context::set('enable_autosave', $enable_autosave);
260
261
		/**
262
		 * Extract editor's unique number (in order to display multiple editors on a single page)
263
		 */
264
		if($option->editor_sequence) $editor_sequence = $option->editor_sequence;
265
		else
266
		{
267
			if(!$_SESSION['_editor_sequence_']) $_SESSION['_editor_sequence_'] = 1;
268
			$editor_sequence = $_SESSION['_editor_sequence_'] ++;
269
		}
270
271
		/**
272
		 * Upload setting by using configuration of the file module internally
273
		 */
274
		$files_count = 0;
275
		if($allow_fileupload)
276
		{
277
			$oFileModel = getModel('file');
278
			// Get upload configuration to set on SWFUploader
279
			$file_config = $oFileModel->getUploadConfig();
280
			$file_config->allowed_attach_size = $file_config->allowed_attach_size*1024*1024;
281
			$file_config->allowed_filesize = $file_config->allowed_filesize*1024*1024;
282
283
			Context::set('file_config',$file_config);
284
			// Configure upload status such as file size
285
			$upload_status = $oFileModel->getUploadStatus();
286
			Context::set('upload_status', $upload_status);
287
			// Upload enabled (internally caching)
288
			$oFileController = getController('file');
289
			$oFileController->setUploadInfo($editor_sequence, $upload_target_srl);
290
			// Check if the file already exists
291
			if($upload_target_srl) $files_count = $oFileModel->getFilesCount($upload_target_srl);
292
		}
293
		Context::set('files_count', (int)$files_count);
294
295
		Context::set('allow_fileupload', $allow_fileupload);
296
		// Set editor_sequence value
297
		Context::set('editor_sequence', $editor_sequence);
298
		// Set the document number to upload_target_srl for file attachments
299
		// If a new document, upload_target_srl = 0. The value becomes changed when file attachment is requested
300
		Context::set('upload_target_srl', $upload_target_srl);
301
		// Set the primary key valueof the document or comments
302
		Context::set('editor_primary_key_name', $option->primary_key_name);
303
		// Set content column name to sync contents
304
		Context::set('editor_content_key_name', $option->content_key_name);
305
306
		/**
307
		 * Check editor component
308
		 */
309
		$site_module_info = Context::get('site_module_info');
310
		$site_srl = (int)$site_module_info->site_srl;
311
		if($enable_component)
312
		{
313
			if(!Context::get('component_list'))
314
			{
315
				$component_list = $this->getComponentList(true, $site_srl);
316
				Context::set('component_list', $component_list);
317
			}
318
		}
319
		Context::set('enable_component', $enable_component);
320
		Context::set('enable_default_component', $enable_default_component);
321
322
		/**
323
		 * Variable setting if html_mode is available
324
		 */
325
		Context::set('html_mode', $html_mode);
326
327
		/**
328
		 * Set a height of editor
329
		 */
330
		Context::set('editor_height', $editor_height);
331
		// Check an option whether to start the editor manually
332
		Context::set('editor_manual_start', $option->manual_start);
333
334
		/**
335
		 * Set a skin path to pre-compile the template
336
		 */
337
		$tpl_path = sprintf('%sskins/%s/', $this->module_path, $skin);
338
		$tpl_file = 'editor.html';
339
340
		if(!file_exists($tpl_path.$tpl_file))
341
		{
342
			$skin = 'ckeditor';
343
			$tpl_path = sprintf('%sskins/%s/', $this->module_path, $skin);
344
		}
345
		Context::set('editor_path', $tpl_path);
346
347
		// load editor skin lang
348
		Context::loadLang($tpl_path.'lang');
349
		// Return the compiled result from tpl file
350
		$oTemplate = TemplateHandler::getInstance();
351
		return $oTemplate->compile($tpl_path, $tpl_file);
352
	}
353
354
	/**
355
	 * @brief Return editor template which contains settings of each module
356
	 * Result of getModuleEditor() is as same as getEditor(). But getModuleEditor()uses additional settings of each module to generate an editor
357
	 *
358
	 * 2 types of editors supported; document and comment.
359
	 * 2 types of editors can be used on a single module. For instance each for original post and reply port.
360
	 */
361
	function getModuleEditor($type = 'document', $module_srl, $upload_target_srl, $primary_key_name, $content_key_name)
362
	{
363
		// Get editor settings of the module
364
		$editor_config = $this->getEditorConfig($module_srl);
365
366
		$config = new stdClass();
367
		$config->module_type = $type;
368
369
		// Configurations listed according to a type
370
		if($type == 'document')
371
		{
372
			$config->editor_skin = $editor_config->editor_skin;
373
			$config->content_style = $editor_config->content_style;
374
			$config->content_font = $editor_config->content_font;
375
			$config->content_font_size = $editor_config->content_font_size;
376
			$config->sel_editor_colorset = $editor_config->sel_editor_colorset;
377
			$config->upload_file_grant = $editor_config->upload_file_grant;
378
			$config->enable_default_component_grant = $editor_config->enable_default_component_grant;
379
			$config->enable_component_grant = $editor_config->enable_component_grant;
380
			$config->enable_html_grant = $editor_config->enable_html_grant;
381
			$config->editor_height = $editor_config->editor_height;
382
			$config->enable_autosave = $editor_config->enable_autosave;
383
		}
384
		else
385
		{
386
			$config->editor_skin = $editor_config->comment_editor_skin;
387
			$config->content_style = $editor_config->comment_content_style;
388
			$config->content_font = $editor_config->content_font;
389
			$config->content_font_size = $editor_config->content_font_size;
390
			$config->sel_editor_colorset = $editor_config->sel_comment_editor_colorset;
391
			$config->upload_file_grant = $editor_config->comment_upload_file_grant;
392
			$config->enable_default_component_grant = $editor_config->enable_comment_default_component_grant;
393
			$config->enable_component_grant = $editor_config->enable_comment_component_grant;
394
			$config->enable_html_grant = $editor_config->enable_comment_html_grant;
395
			$config->editor_height = $editor_config->comment_editor_height;
396
			$config->enable_autosave = 'N';
397
		}
398
		// Check a group_list of the currently logged-in user for permission check
399 View Code Duplication
		if(Context::get('is_logged'))
400
		{
401
			$logged_info = Context::get('logged_info');
402
			$group_list = $logged_info->group_list;
403
		}
404
		else
405
		{
406
			$group_list = array();
407
		}
408
		// Pre-set option variables of editor
409
		$option = new stdClass();
410
		$option->module_type = $config->module_type;
411
		$option->skin = $config->editor_skin;
412
		$option->content_style = $config->content_style;
413
		$option->content_font = $config->content_font;
414
		$option->content_font_size = $config->content_font_size;
415
		$option->colorset = $config->sel_editor_colorset;
416
		// Permission check for file upload
417
		$option->allow_fileupload = false;
418 View Code Duplication
		if($logged_info->is_admin=='Y') $option->allow_fileupload = true;
0 ignored issues
show
Bug introduced by
The variable $logged_info does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
419
		elseif(count($config->upload_file_grant))
420
		{
421
			foreach($group_list as $group_srl => $group_info)
422
			{
423
				if(in_array($group_srl, $config->upload_file_grant))
424
				{
425
					$option->allow_fileupload = true;
426
					break;
427
				}
428
			}
429
		}
430
		else $option->allow_fileupload = true;
431
		// Permission check for using default components
432
		$option->enable_default_component = false;
433 View Code Duplication
		if($logged_info->is_admin=='Y') $option->enable_default_component = true;
434
		elseif(count($config->enable_default_component_grant))
435
		{
436
			foreach($group_list as $group_srl => $group_info)
437
			{
438
				if(in_array($group_srl, $config->enable_default_component_grant))
439
				{
440
					$option->enable_default_component = true;
441
					break;
442
				}
443
			}
444
		}
445
		else $option->enable_default_component = true;
446
		// Permisshion check for using extended components
447
		$option->enable_component = false;
448 View Code Duplication
		if($logged_info->is_admin=='Y') $option->enable_component = true;
449
		elseif(count($config->enable_component_grant))
450
		{
451
			foreach($group_list as $group_srl => $group_info)
452
			{
453
				if(in_array($group_srl, $config->enable_component_grant))
454
				{
455
					$option->enable_component = true;
456
					break;
457
				}
458
			}
459
		}
460
		else $option->enable_component = true;
461
		// HTML editing privileges
462
		$enable_html = false;
463 View Code Duplication
		if($logged_info->is_admin=='Y') $enable_html = true;
464
		elseif(count($config->enable_html_grant))
465
		{
466
			foreach($group_list as $group_srl => $group_info)
467
			{
468
				if(in_array($group_srl, $config->enable_html_grant))
469
				{
470
					$enable_html = true;
471
					break;
472
				}
473
			}
474
		}
475
		else $enable_html = true;
476
477
		if($enable_html) $option->disable_html = false;
478
		else $option->disable_html = true;
479
		// Set Height
480
		$option->height = $config->editor_height;
481
		// Set an option for Auto-save
482
		$option->enable_autosave = $config->enable_autosave=='Y'?true:false;
483
		// Other settings
484
		$option->primary_key_name = $primary_key_name;
485
		$option->content_key_name = $content_key_name;
486
487
		return $this->getEditor($upload_target_srl, $option);
488
	}
489
490
	/**
491
	 * @brief Get information which has been auto-saved
492
	 */
493
	function getSavedDoc($upload_target_srl)
494
	{
495
		$auto_save_args = new stdClass();
496
		$auto_save_args->module_srl = Context::get('module_srl');
497
		// Get the current module if module_srl doesn't exist
498
		if(!$auto_save_args->module_srl)
499
		{
500
			$current_module_info = Context::get('current_module_info');
501
			$auto_save_args->module_srl = $current_module_info->module_srl;
502
		}
503
		// Find a document by using member_srl for logged-in user and ipaddress for non-logged user
504 View Code Duplication
		if(Context::get('is_logged'))
505
		{
506
			$logged_info = Context::get('logged_info');
507
			$auto_save_args->member_srl = $logged_info->member_srl;
508
		}
509
		else
510
		{
511
			$auto_save_args->certify_key = $_COOKIE['autosave_certify_key_' . $auto_save_args->module_srl];
512
			// @see https://github.com/xpressengine/xe-core/issues/2208
513
			// 변경 이전에 작성된 게시물 호환성 유지
514
			if(!$auto_save_args->certify_key) $auto_save_args->ipaddress = $_SERVER['REMOTE_ADDR'];
515
		}
516
		// Extract auto-saved data from the DB
517
		$output = executeQuery('editor.getSavedDocument', $auto_save_args);
518
		$saved_doc = $output->data;
519
		// Return null if no result is auto-saved
520
		if(!$saved_doc) return;
521
522
		if($saved_doc->certify_key && !isset($auto_save_args->certify_key))
523
		{
524
			return;
525
		}
526
527
		// Check if the auto-saved document already exists
528
		$oDocumentModel = getModel('document');
529
		$oSaved = $oDocumentModel->getDocument($saved_doc->document_srl);
530
		if($oSaved->isExists()) return;
531
		// Move all the files if the auto-saved data contains document_srl and file
532
		// Then set document_srl to editor_sequence
533
		if($saved_doc->document_srl && $upload_target_srl && !Context::get('document_srl'))
534
		{
535
			$saved_doc->module_srl = $auto_save_args->module_srl;
536
			$oFileController = getController('file');
537
			$oFileController->moveFile($saved_doc->document_srl, $saved_doc->module_srl, $upload_target_srl);
538
		}
539
		else if($upload_target_srl) $saved_doc->document_srl = $upload_target_srl;
540
		// Change auto-saved data
541
		$saved_doc->certify_key = $auto_save_args->certify_key;
542
		$oEditorController = getController('editor');
543
		$oEditorController->deleteSavedDoc(false);
544
		$oEditorController->doSaveDoc($saved_doc);
545
546
		setUserSequence($saved_doc->document_srl);
547
548
		return $saved_doc;
549
	}
550
551
	/**
552
	 * @brief create objects of the component
553
	 */
554
	function getComponentObject($component, $editor_sequence = 0, $site_srl = 0)
555
	{
556
		if(!preg_match('/^[a-zA-Z0-9_-]+$/',$component) || !preg_match('/^[0-9]+$/', $editor_sequence . $site_srl)) return;
557
558
		if(!$this->loaded_component_list[$component][$editor_sequence])
559
		{
560
			// Create an object of the component and execute
561
			$class_path = sprintf('%scomponents/%s/', $this->module_path, $component);
562
			$class_file = sprintf('%s%s.class.php', $class_path, $component);
563 View Code Duplication
			if(!file_exists($class_file)) return new BaseObject(-1, sprintf(Context::getLang('msg_component_is_not_founded'), $component));
564
			// Create an object after loading the class file
565
			require_once($class_file);
566
			$oComponent = new $component($editor_sequence, $class_path);
567
			if(!$oComponent) return new BaseObject(-1, sprintf(Context::getLang('msg_component_is_not_founded'), $component));
568
			// Add configuration information
569
			$component_info = $this->getComponent($component, $site_srl);
570
			$oComponent->setInfo($component_info);
571
			$this->loaded_component_list[$component][$editor_sequence] = $oComponent;
572
		}
573
574
		return $this->loaded_component_list[$component][$editor_sequence];
575
	}
576
577
	/**
578
	 * @brief Return a list of the editor skin
579
	 */
580
	function getEditorSkinList()
581
	{
582
		return FileHandler::readDir('./modules/editor/skins');
583
	}
584
585
	/**
586
	 * @brief Return the cache file name of editor component list
587
	 */
588
	function getCacheFile($filter_enabled= true, $site_srl = 0)
589
	{
590
		$lang = Context::getLangType();
591
		$cache_path = _XE_PATH_.'files/cache/editor/cache/';
592
		FileHandler::makeDir($cache_path);
593
		$cache_file = $cache_path.'component_list.' . $lang .'.';
594
		if($filter_enabled) $cache_file .= 'filter.';
595
		if($site_srl) $cache_file .= $site_srl.'.';
596
		$cache_file .= 'php';
597
		return $cache_file;
598
	}
599
600
	function getComponentListCacheKey($filter_enabled = true, $site_srl = 0)
601
	{
602
		$cache_key = array();
603
		$cache_key[] = Context::getLangType();
604
		if ($filter_enabled) $cache_key[] = 'filter';
605
		if ($site_srl) $cache_key[] = $site_srl;
606
607
		return 'editor.component_list:' . implode('.', $cache_key);
608
	}
609
610
	/**
611
	 * @brief Return a component list (DB Information included)
612
	 */
613
	function getComponentList($filter_enabled = true, $site_srl=0, $from_db=false)
614
	{
615
		$component_list = false;
616
617
		$oCacheHandler = CacheHandler::getInstance('object', null, true);
618
		if($oCacheHandler->isSupport()) {
619
			$cache_key = $this->getComponentListCacheKey(false, $site_srl);
620
			$component_list = $oCacheHandler->get($cache_key);
621
		}
622
623
		if($from_db || $component_list === false)
624
		{
625
			$oEditorController = getController('editor');
626
			$component_list = $oEditorController->makeCache(false, $site_srl);
627
628
		}
629
630
		if(!$component_list) return array();
631
632
		$logged_info = Context::get('logged_info');
633
		if($logged_info && is_array($logged_info->group_list))
634
		{
635
			$group_list = array_keys($logged_info->group_list);
636
		}
637
		else
638
		{
639
			$group_list = array();
640
		}
641
642
		if(count($component_list))
643
		{
644
			foreach($component_list as $key => $val)
645
			{
646
				if(!trim($key)) continue;
647
				if(!is_dir(_XE_PATH_.'modules/editor/components/'.$key))
648
				{
649
					FileHandler::removeFile($cache_file);
0 ignored issues
show
Bug introduced by
The variable $cache_file does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
650
					return $this->getComponentList($filter_enabled, $site_srl);
651
				}
652
				if(!$filter_enabled) continue;
653
				if($val->enabled == "N")
654
				{
655
					unset($component_list->{$key});
656
					continue;
657
				}
658
				if($logged_info->is_admin == "Y" || $logged_info->is_site_admin == "Y") continue;
659
				if($val->target_group)
660
				{
661
					if(!$logged_info)
662
					{
663
						$val->enabled = "N";
664
					}
665
					else
666
					{
667
						$is_granted = false;
668
						foreach($group_list as $group_srl)
669
						{
670
							if(in_array($group_srl, $val->target_group)) $is_granted = true;
671
						}
672
						if(!$is_granted) $val->enabled = "N";
673
					}
674
				}
675
				if($val->enabled != "N" && $val->mid_list)
676
				{
677
					$mid = Context::get('mid');
678
					if(!in_array($mid, $val->mid_list)) $val->enabled = "N";
679
				}
680
				if($val->enabled == "N")
681
				{
682
					unset($component_list->{$key});
683
					continue;
684
				}
685
			}
686
		}
687
		return $component_list;
688
	}
689
690
	/**
691
	 * @brief Get xml and db information of the component
692
	 */
693
	function getComponent($component_name, $site_srl = 0)
694
	{
695
		$args = new stdClass();
696
		$args->component_name = $component_name;
697
698
		if($site_srl)
699
		{
700
			$args->site_srl = $site_srl;
701
			$output = executeQuery('editor.getSiteComponent', $args);
702
		}
703
		else
704
		{
705
			$output = executeQuery('editor.getComponent', $args);
706
		}
707
		$component = $output->data;
708
709
		if(!$output->data) return false;
710
711
		$component_name = $component->component_name;
712
713
		unset($xml_info);
714
		$xml_info = $this->getComponentXmlInfo($component_name);
715
		$xml_info->enabled = $component->enabled;
716
717
		$xml_info->target_group = array();
718
719
		$xml_info->mid_list = array();
720
721 View Code Duplication
		if($component->extra_vars)
722
		{
723
			$extra_vars = unserialize($component->extra_vars);
724
725
			if($extra_vars->target_group)
726
			{
727
				$xml_info->target_group = $extra_vars->target_group;
728
				unset($extra_vars->target_group);
729
			}
730
731
			if($extra_vars->mid_list)
732
			{
733
				$xml_info->mid_list = $extra_vars->mid_list;
734
				unset($extra_vars->mid_list);
735
			}
736
737
			if($xml_info->extra_vars)
738
			{
739
				foreach($xml_info->extra_vars as $key => $val)
740
				{
741
					$xml_info->extra_vars->{$key}->value = $extra_vars->{$key};
742
				}
743
			}
744
		}
745
746
		return $xml_info;
747
	}
748
749
	/**
750
	 * @brief Read xml information of the component
751
	 */
752
	function getComponentXmlInfo($component)
753
	{
754
		$lang_type = Context::getLangType();
755
756
		// Get xml file path of the requested components
757
		$component_path = sprintf('%s/components/%s/', $this->module_path, $component);
758
759
		$xml_file = sprintf('%sinfo.xml', $component_path);
760
		$cache_file = sprintf('./files/cache/editor/%s.%s.php', $component, $lang_type);
761
762
		// Include and return xml file information if cached file exists
763 View Code Duplication
		if(file_exists($cache_file) && file_exists($xml_file) && filemtime($cache_file) > filemtime($xml_file))
764
		{
765
			include($cache_file);
766
767
			return $xml_info;
0 ignored issues
show
Bug introduced by
The variable $xml_info does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
768
		}
769
770
		$oParser = new XmlParser();
771
		$xml_doc = $oParser->loadXmlFile($xml_file);
772
773
		// Component information listed
774
		$component_info = new stdClass;
775
		$component_info->author = array();
776
		$component_info->extra_vars = new stdClass;
777
		$component_info->component_name = $component;
778
		$component_info->title = $xml_doc->component->title->body;
779
780
		if($xml_doc->component->version)
781
		{
782
			$component_info->description = str_replace('\n', "\n", $xml_doc->component->description->body);
783
			$component_info->version = $xml_doc->component->version->body;
784
			$component_info->date = $xml_doc->component->date->body;
785
			$component_info->homepage = $xml_doc->component->link->body;
786
			$component_info->license = $xml_doc->component->license->body;
787
			$component_info->license_link = $xml_doc->component->license->attrs->link;
788
		}
789
		else
790
		{
791
			sscanf($xml_doc->component->author->attrs->date, '%d. %d. %d', $date_obj->y, $date_obj->m, $date_obj->d);
0 ignored issues
show
Bug introduced by
The variable $date_obj does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
792
			$date = sprintf('%04d%02d%02d', $date_obj->y, $date_obj->m, $date_obj->d);
793
794
			$component_info->description = str_replace('\n', "\n", $xml_doc->component->author->description->body);
795
			$component_info->version = $xml_doc->component->attrs->version;
796
			$component_info->date = $date;
797
798
			$component_info->author = array();
799
			$component_info->author[0]->name = $xml_doc->component->author->name->body;
800
			$component_info->author[0]->email_address = $xml_doc->component->author->attrs->email_address;
801
			$component_info->author[0]->homepage = $xml_doc->component->author->attrs->link;
802
		}
803
804
		// Author information
805
		$author_list = array();
806 View Code Duplication
		if(!is_array($xml_doc->component->author)) $author_list[] = $xml_doc->component->author;
807
		else $author_list = $xml_doc->component->author;
808
809
		for($i = 0; $i < count($author_list); $i++)
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
810
		{
811
			$author = new stdClass;
812
			$author->name = $author_list[$i]->name->body;
813
			$author->email_address = $author_list[$i]->attrs->email_address;
814
			$author->homepage = $author_list[$i]->attrs->link;
815
			$component_info->author[] = $author;
816
		}
817
818
		// List extra variables (text type only for editor component)
819
		$extra_vars = $xml_doc->component->extra_vars;
820 View Code Duplication
		if($extra_vars)
821
		{
822
			$extra_var_groups = $extra_vars->group;
823
			if(!$extra_var_groups)
824
			{
825
				$extra_var_groups = $extra_vars;
826
			}
827
			if(!is_array($extra_var_groups))
828
			{
829
				$extra_var_groups = array($extra_var_groups);
830
			}
831
832
			foreach($extra_var_groups as $group)
833
			{
834
				$extra_vars = $group->var;
835
				if(!is_array($group->var))
836
				{
837
					$extra_vars = array($group->var);
838
				}
839
840
				foreach($extra_vars as $key => $val)
841
				{
842
					if(!$val)
843
					{
844
						continue;
845
					}
846
847
					$obj = new stdClass();
848
					if(!$val->attrs)
849
					{
850
						$val->attrs = new stdClass();
851
					}
852
					if(!$val->attrs->type)
853
					{
854
						$val->attrs->type = 'text';
855
					}
856
857
					$obj->group = $group->title->body;
858
					$obj->name = $val->attrs->name;
859
					$obj->title = $val->title->body;
860
					$obj->type = $val->attrs->type;
861
					$obj->description = $val->description->body;
862
					if($obj->name)
863
					{
864
						$obj->value = $extra_vals->{$obj->name};
0 ignored issues
show
Bug introduced by
The variable $extra_vals does not exist. Did you mean $extra_vars?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
865
					}
866
					if(strpos($obj->value, '|@|') != FALSE)
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($obj->value, '|@|') of type integer to the boolean FALSE. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
867
					{
868
						$obj->value = explode('|@|', $obj->value);
869
					}
870
					if($obj->type == 'mid_list' && !is_array($obj->value))
871
					{
872
						$obj->value = array($obj->value);
873
					}
874
875
					// 'Select'type obtained from the option list.
876
					if($val->options && !is_array($val->options))
877
					{
878
						$val->options = array($val->options);
879
					}
880
881
					for($i = 0, $c = count($val->options); $i < $c; $i++)
882
					{
883
						$obj->options[$i] = new stdClass();
884
						$obj->options[$i]->title = $val->options[$i]->title->body;
885
						$obj->options[$i]->value = $val->options[$i]->attrs->value;
886
					}
887
888
					$component_info->extra_vars->{$obj->name} = $obj;
889
				}
890
			}
891
		}
892
893
		$buff = array();
894
		$buff[] = '<?php if(!defined(\'__XE__\')) exit();';
895
		$buff[] = '$xml_info = ' . var_export($component_info, TRUE) . ';';
896
		$buff = str_replace('stdClass::__set_state', '(object)', implode(PHP_EOL, $buff));
897
898
		FileHandler::writeFile($cache_file, $buff, 'w');
899
900
		return $component_info;
901
	}
902
}
903
/* End of file editor.model.php */
904
/* Location: ./modules/editor/editor.model.php */
905