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.

menuAdminController   F
last analyzed

Complexity

Total Complexity 366

Size/Duplication

Total Lines 2265
Duplicated Lines 8.79 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 0
Metric Value
dl 199
loc 2265
rs 0.8
c 0
b 0
f 0
wmc 366
lcom 2
cbo 7

49 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
A __construct() 0 3 1
A procMenuAdminInsert() 0 14 3
A addMenu() 0 18 2
A linkAllModuleInstancesToSitemap() 0 18 5
A getUnlinkedMenu() 0 34 5
B updateLinkModule() 0 54 10
A procMenuAdminUpdate() 15 15 3
B procMenuAdminDelete() 0 53 9
C deleteMenu() 9 71 12
B procMenuAdminInsertItem() 0 42 8
A _setMenuSrl() 0 21 3
C _insertShortcut() 0 70 12
F _insertMenu() 0 60 15
B _insertModule() 0 52 7
D procMenuAdminUpdateItem() 0 96 16
C procMenuAdminButtonUpload() 0 51 11
A updateMenuItem() 0 8 1
A _updateMenuItem() 0 6 1
A procMenuAdminDeleteItem() 0 22 3
C deleteItem() 9 72 10
A _checkHomeMenuInOriginMenu() 0 15 6
C _deleteMenuItem() 9 56 11
A _recursiveDeleteMenuItem() 0 16 4
C procMenuAdminMoveItem() 9 71 11
B _recursiveUpdateMenuItem() 6 31 7
A procMenuAdminCopyItem() 9 30 4
A _searchMenu() 0 16 4
C _copyMenu() 0 94 13
A _makeRandomMid() 0 15 3
C procMenuAdminArrangeItem() 0 61 13
A _setParent() 0 17 2
A _recursiveMoveMenuItem() 0 13 2
D moveMenuItem() 6 75 18
A procMenuAdminMakeXmlFile() 14 14 1
B procMenuAdminUploadButton() 0 35 8
A procMenuAdminDeleteButton() 0 10 1
A procMenuAdminAllActList() 6 17 4
B procMenuAdminInsertItemForAdminMenu() 8 72 9
F procMenuAdminUpdateAuth() 0 98 16
C makeXmlFile() 7 111 9
F getXmlTree() 15 89 28
F getPhpCacheCode() 13 123 22
A updateMenuLayout() 0 17 5
F _uploadButton() 40 68 20
A _copyButton() 24 38 4
A _changeMenuItemSrlInButtonPath() 0 8 1
A makeHomemenuCacheFile() 0 7 1
A getHomeMenuCacheFile() 0 4 1

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like menuAdminController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use menuAdminController, and based on these observations, apply Extract Interface, too.

1
<?php
2
/* Copyright (C) XEHub <https://www.xehub.io> */
3
/**
4
 * menuAdminController class
5
 * admin controller class of the menu module
6
 *
7
 * @author XEHub ([email protected])
8
 * @package /modules/menu
9
 * @version 0.1
10
 */
11
class menuAdminController extends menu
12
{
13
	/**
14
	 * menu number
15
	 * @var int
16
	 */
17
	var $menuSrl = null;
18
	/**
19
	 * item key list
20
	 * @var array
21
	 */
22
	var $itemKeyList = array();
23
	/**
24
	 * map
25
	 * @var array
26
	 */
27
	var $map = array();
28
	/**
29
	 * checked
30
	 * @var array
31
	 */
32
	var $checked = array();
33
	/**
34
	 * inserted menu item serial number
35
	 * @var array
36
	 */
37
	var $insertedMenuItemSrlList = array();
38
	/**
39
	 * home module's mid
40
	 * @var string
41
	 */
42
	private $homeModuleMid = NULL;
43
	/**
44
	 * home menu cache file
45
	 * @var string
46
	 */
47
	private $homeMenuCacheFile = 'files/cache/menu/homeSitemap.php';
48
49
	/**
50
	 * Initialization
51
	 * @return void
52
	 */
53
	function init()
54
	{
55
		$this->setTemplatePath($this->module_path.'tpl');
56
	}
57
58
	function __construct() {
59
		$this->homeMenuCacheFile = _XE_PATH_ . $this->homeMenuCacheFile;
60
	}
61
62
	/**
63
	 * Add a menu
64
	 * @return void|object
65
	 */
66
	function procMenuAdminInsert()
67
	{
68
		// List variables
69
		$site_module_info = Context::get('site_module_info');
70
71
		$output = $this->addMenu(Context::get('title'), (int)$site_module_info->site_srl);
72
		if(!$output->toBool()) return $output;
73
74
		$this->add('menu_srl', $output->get('menuSrl'));
75
		$this->setMessage('success_registed');
76
77
		$returnUrl = Context::get('success_return_url') ? Context::get('success_return_url') : getNotEncodedUrl('', 'module', 'admin', 'act', 'dispMenuAdminContent');
78
		$this->setRedirectUrl($returnUrl);
79
	}
80
81
	/**
82
	 * Add a menu
83
	 *
84
	 * @param string $title
85
	 * @param int $siteSrl
86
	 * @return BaseObject If success, it contains menuSrl
87
	 */
88
	public function addMenu($title, $siteSrl = 0)
89
	{
90
		$args = new stdClass();
91
		$args->site_srl = $siteSrl;
92
		$args->title = $title;
93
94
		$args->menu_srl = getNextSequence();
95
		$args->listorder = $args->menu_srl * -1;
96
97
		$output = executeQuery('menu.insertMenu', $args);
98
		if(!$output->toBool())
99
		{
100
			return $output;
101
		}
102
103
		$output->add('menuSrl', $args->menu_srl);
104
		return $output;
105
	}
106
107
	function linkAllModuleInstancesToSitemap()
108
	{
109
		$unlinked_modules = false;
110
		$args = new stdClass;
111
		$args->site_srl = 0;
112
		$output = executeQueryArray('module.getNotLinkedModuleBySiteSrl',$args);
113
		if($output->toBool() && $output->data && count($output->data) > 0)
114
		{
115
			$unlinked_modules = $output->data;
116
		}
117
118
		if($unlinked_modules)
119
		{
120
			$unlinked_menu_srl = $this->getUnlinkedMenu();
121
			$output = $this->updateLinkModule($unlinked_modules, $unlinked_menu_srl);
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
122
		}
123
124
	}
125
126
	function getUnlinkedMenu()
127
	{
128
		// 'unlinked' menu 존재여부 확인
129
		$oModuleModel = getModel('module');
130
		$moduleConfig = $oModuleModel->getModuleConfig('menu');
131
132
		if($moduleConfig->unlinked_menu_srl)
133
		{
134
			$menuArgs = new stdClass;
135
			$menuArgs->menu_srl = $moduleConfig->unlinked_menu_srl;
136
			$menuOutput = executeQuery('menu.getMenu', $menuArgs);
137
			if(!$menuOutput->data)
138
			{
139
				unset($moduleConfig->unlinked_menu_srl);
140
			}
141
		}
142
143
		if(!$moduleConfig->unlinked_menu_srl)
144
		{
145
			$output = $this->addMenu('unlinked', 0);
146
			if($output->toBool())
147
			{
148
				$moduleConfig->unlinked_menu_srl = $output->get('menuSrl');
149
				$oModuleController = getController('module');
150
				$oModuleController->updateModuleConfig('menu', $moduleConfig);
151
			}
152
			else
153
			{
154
				return false;
155
			}
156
		}
157
158
		return $moduleConfig->unlinked_menu_srl;
159
	}
160
161
	/**
162
	 * insert menu when not linked module.
163
	 *
164
	 * @param array $moduleInfos
165
	 * @param int $menuSrl
166
	 *
167
	 * @return BaseObject
168
	 */
169
	function updateLinkModule($moduleInfos, $menuSrl)
170
	{
171
		if(!$moduleInfos || !is_array($moduleInfos) || count($moduleInfos) == 0 || $menuSrl == 0)
0 ignored issues
show
Bug Best Practice introduced by
The expression $moduleInfos of type array 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...
172
		{
173
			return new BaseObject(-1, 'msg_invalid_request');
174
		}
175
176
		foreach($moduleInfos as $moduleInfo)
177
		{
178
			// search menu.
179
			$args = new stdClass;
180
			$args->url = $moduleInfo->mid;
181
			$args->site_srl = $moduleInfo->site_srl;
182
			$args->is_shortcut = 'N';
183
184
			$output = executeQuery('menu.getMenuItemByUrl', $args);
185
186
			if($output->toBool() && $output->data)
187
			{
188
				$moduleInfo->menu_srl = $output->data->menu_srl;
189
			}
190
			else
191
			{
192
				// create menu item.
193
				$item_args->menu_srl = $menuSrl;
0 ignored issues
show
Bug introduced by
The variable $item_args 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...
194
				$item_args->url = $moduleInfo->mid;
195
				$item_args->name = $moduleInfo->mid;
196
				$item_args->menu_item_srl = getNextSequence();
197
				$item_args->listorder = -1*$item_args->menu_item_srl;
198
199
				$output = executeQuery('menu.insertMenuItem', $item_args);
200
				if(!$output->toBool())
201
				{
202
					return $output;
203
				}
204
				$moduleInfo->menu_srl = $menuSrl;
205
			}
206
207
			$output = executeQuery('module.updateModule', $moduleInfo);
208
209
			return $output;
210
		}
211
212
		$oCacheHandler = CacheHandler::getInstance('object', null, true);
213
		if($oCacheHandler->isSupport())
214
		{
215
			$oCacheHandler->invalidateGroupKey('site_and_module');
216
		}
217
218
		$oMenuAdminController = getAdminController('menu');
219
		$oMenuAdminController->makeXmlFile($menuSrl);
220
221
		return new BaseObject();
222
	}
223
224
225
226
	/**
227
	 * Change the menu title
228
	 * @return void|object
229
	 */
230 View Code Duplication
	function procMenuAdminUpdate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
231
	{
232
		// List variables
233
		$args = new stdClass();
234
		$args->title = Context::get('title');
235
		$args->menu_srl = Context::get('menu_srl');
236
237
		$output = executeQuery('menu.updateMenu', $args);
238
		if(!$output->toBool()) return $output;
239
240
		$this->setMessage('success_registed');
241
242
		$returnUrl = Context::get('success_return_url') ? Context::get('success_return_url') : getNotEncodedUrl('', 'module', 'admin', 'act', 'dispMenuAdminManagement', 'menu_srl', $args->menu_srl);
243
		$this->setRedirectUrl($returnUrl);
244
	}
245
246
	/**
247
	 * Delete menu process method
248
	 * @return void|BaseObject
249
	 */
250
	function procMenuAdminDelete()
251
	{
252
		$menu_srl = Context::get('menu_srl');
253
254
		$oMenuAdminModel = getAdminModel('menu');
255
		$menuInfo = $oMenuAdminModel->getMenu($menu_srl);
256
257
		$oAdmin = getClass('admin');
258
		if($menuInfo->title == $oAdmin->getAdminMenuName())
259
			return new BaseObject(-1, 'msg_adminmenu_cannot_delete');
260
261
		// get menu properies with child menu
262
		$phpFile = sprintf("./files/cache/menu/%s.php", $menu_srl);
263
		$originMenu = NULL;
264
265
		if(is_readable(FileHandler::getRealPath($phpFile)))
266
		{
267
			include(FileHandler::getRealPath($phpFile));
268
		}
269
270
		// check home menu in originMenu
271
		$oModuleModel = getModel('module');
272
		$siteInfo = $oModuleModel->getSiteInfo($menuInfo->site_srl);
273
274
		$isStartmenuInclude = false;
275
276
		if(is_array($menu->list))
277
		{
278
			foreach($menu->list AS $key=>$value)
0 ignored issues
show
Bug introduced by
The variable $menu 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...
279
			{
280
				$originMenu = $value;
281
				$this->_checkHomeMenuInOriginMenu($originMenu, $siteInfo->mid, $isStartmenuInclude);
282
283
				if($isStartmenuInclude)
284
					break;
285
			}
286
		}
287
288
		if($isStartmenuInclude)
289
		{
290
			return new BaseObject(-1, 'msg_cannot_delete_homemenu');
291
		}
292
293
		$output = $this->deleteMenu($menu_srl);
294
		if(!$output->toBool())
295
		{
296
			return new BaseObject(-1, $output->message);
297
		}
298
299
		$this->setMessage('success_deleted', 'info');
300
		$returnUrl = Context::get('success_return_url') ? Context::get('success_return_url') : getNotEncodedUrl('', 'module', 'admin', 'act', 'dispMenuAdminSiteMap');
301
		$this->setRedirectUrl($returnUrl);
302
	}
303
304
	/**
305
	 * Delete menu
306
	 * Delete menu_item and xml cache files
307
	 * @return BaseObject
308
	 */
309
	function deleteMenu($menu_srl)
310
	{
311
		$oDB = DB::getInstance();
312
		$oDB->begin();
313
314
		$args = new stdClass();
315
		$args->menu_srl = $menu_srl;
316
317
		$oMenuAdminModel = getAdminModel('menu');
318
		$menuInfo = $oMenuAdminModel->getMenu($args->menu_srl);
319
320
		// Delete modules
321
		$output = executeQueryArray('menu.getMenuItems', $args);
322
		if(!$output->toBool())
323
		{
324
			return $output;
325
		}
326
327
		$oModuleController = getController('module');
328
		$oModuleModel = getModel('module');
329
330
		foreach($output->data as $itemInfo)
331
		{
332
			if($itemInfo->is_shortcut != 'Y' && strncasecmp('http', $itemInfo->url, 4) !== 0)
333
			{
334
				$moduleInfo = $oModuleModel->getModuleInfoByMid($itemInfo->url, $menuInfo->site_srl);
335 View Code Duplication
				if($moduleInfo->module_srl)
336
				{
337
					$output = $oModuleController->onlyDeleteModule($moduleInfo->module_srl);
338
					if(!$output->toBool())
339
					{
340
						$oDB->rollback();
341
						return $output;
342
					}
343
				}
344
			}
345
		}
346
347
		// Delete menu items
348
		$output = executeQuery("menu.deleteMenuItems", $args);
349
		if(!$output->toBool())
350
		{
351
			$oDB->rollback();
352
			return $output;
353
		}
354
		// Delete the menu
355
		$output = executeQuery("menu.deleteMenu", $args);
356
		if(!$output->toBool())
357
		{
358
			$oDB->rollback();
359
			return $output;
360
		}
361
362
		// Delete cache files
363
		$cache_list = FileHandler::readDir("./files/cache/menu","",false,true);
364
		if(count($cache_list))
365
		{
366
			foreach($cache_list as $cache_file)
367
			{
368
				$pos = strpos($cache_file, $menu_srl.'.');
369
				if($pos>0)FileHandler::removeFile($cache_file);
370
			}
371
		}
372
		// Delete images of menu buttons
373
		$image_path = sprintf('./files/attach/menu_button/%s', $menu_srl);
374
		FileHandler::removeDir($image_path);
375
376
		$oDB->commit();
377
378
		return new BaseObject(0,'success_deleted');
379
	}
380
381
	/**
382
	 * Add an item to the menu, simple version
383
	 * @return void
384
	 */
385
	public function procMenuAdminInsertItem($request = NULL)
386
	{
387
		$isProc = false;
388
		if(!$request)
389
		{
390
			$isProc = true;
391
			$request = Context::getRequestVars();
392
		}
393
394
		if(!$request->parent_srl || !$request->menu_name)
395
		{
396
			return new BaseObject(-1, 'msg_invalid_request');
397
		}
398
399
		$this->_setMenuSrl($request->parent_srl, $request->menu_srl);
400
		if(!$request->menu_srl)
401
		{
402
			return new BaseObject(-1, 'msg_invalid_request');
403
		}
404
405
		if($request->is_shortcut == 'Y')
406
		{
407
			$result = $this->_insertShortcut($request);
408
		}
409
		else
410
		{
411
			$result = $this->_insertMenu($request, $isProc);
412
		}
413
414
		if($result->error < 0)
415
		{
416
			return new BaseObject($result->error, $result->message);
417
		}
418
419
		// recreate menu cache file
420
		$this->makeXmlFile($request->menu_srl);
421
422
		if(!$isProc)
423
		{
424
			return $this->get('menu_item_srl');
425
		}
426
	}
427
428
	private function _setMenuSrl(&$parent_srl, &$menu_srl)
429
	{
430
		// set menu srl
431
		$oMenuAdminModel = getAdminModel('menu');
432
		$itemInfo = $oMenuAdminModel->getMenuItemInfo($parent_srl);
433
		// parent_srl is parent menu item's srl
434
		if($itemInfo->menu_srl)
435
		{
436
			$menu_srl = $itemInfo->menu_srl;
437
		}
438
		// in this case, parent_srl is menu srl
439
		else
440
		{
441
			$output = $oMenuAdminModel->getMenu($parent_srl);
442
			if($output->menu_srl == $parent_srl)
443
			{
444
				$menu_srl = $output->menu_srl;
445
				$parent_srl = 0;
446
			}
447
		}
448
	}
449
450
	private function _insertShortcut(&$request)
451
	{
452
		$oDB = DB::getInstance();
453
		$oDB->begin();
454
455
		// type is url
456
		if(strncasecmp('http', $request->shortcut_target, 4) === 0 || preg_match('/^(\.\/|\.\.\/|\/).*$/', $request->shortcut_target))
457
		{
458
			// set menu variable
459
			$args = new stdClass();
460
			$args->menu_srl = $request->menu_srl;
461
			$args->parent_srl = $request->parent_srl;
462
			$args->open_window = $request->menu_open_window;
463
			$args->expand = $request->menu_expand;
464
			$args->is_shortcut = $request->is_shortcut;
465
			$args->url = $request->shortcut_target;
466
467
			if(!$args->open_window) $args->open_window = 'N';
468
			if(!$args->expand) $args->expand = 'N';
469
			if(!$args->is_shortcut) $args->is_shortcut = 'Y';
470
471
			if($request->menu_name_key) $args->name = $request->menu_name_key;
472
			else $args->name = $request->menu_name;
473
		}
474
		// type is module short cut
475
		else if(is_numeric($request->shortcut_target))
476
		{
477
			// Get original information
478
			$oMenuAdminModel = getAdminModel('menu');
479
			$itemInfo = $oMenuAdminModel->getMenuItemInfo($request->shortcut_target);
480
			if(!$itemInfo->menu_item_srl)
481
			{
482
				return new BaseObject(-1, 'msg_invalid_request');
483
			}
484
			unset($itemInfo->normal_btn, $itemInfo->hover_btn, $itemInfo->active_btn);
485
486
			$args = $itemInfo;
487
			if(count($args->group_srls) == 0)
488
			{
489
				unset($args->group_srls);
490
			}
491
			$args->menu_srl = $request->menu_srl;
492
			$args->name = $request->menu_name;
493
			$args->parent_srl = $request->parent_srl;
494
			$args->is_shortcut = $request->is_shortcut;
495
		}
496
		// empty target shortcut
497
		else
498
		{
499
			$args = new stdClass();
500
			$args->menu_srl = $request->menu_srl;
501
			$args->name = $request->menu_name;
502
			$args->parent_srl = $request->parent_srl;
503
			$args->is_shortcut = $request->is_shortcut;
504
			$args->url = '#';
505
		}
506
507
		if($request->menu_desc) $args->desc = $request->menu_desc;
508
		else $args->desc = '';
509
510
		$args->menu_item_srl = getNextSequence();
511
		$args->listorder = -1*$args->menu_item_srl;
512
		$output = executeQuery('menu.insertMenuItem', $args);
513
		if(!$output->toBool()) return $output;
514
515
		$oDB->commit();
516
517
		$this->add('menu_item_srl', $args->menu_item_srl);
518
		$this->setMessage('success_registed', 'info');
519
	}
520
521
	private function _insertMenu(&$request, $isProc)
522
	{
523
		$oDB = DB::getInstance();
524
		$oDB->begin();
525
526
		// set menu variable
527
		$args = new stdClass();
528
		$args->menu_srl = $request->menu_srl;
529
		$args->parent_srl = $request->parent_srl;
530
		$args->open_window = $request->menu_open_window;
531
		$args->expand = $request->menu_expand;
532
		$args->expand = $request->menu_expand;
533
		$args->is_shortcut = $request->is_shortcut;
534
535
		if(!$args->open_window) $args->open_window = 'N';
536
		if(!$args->expand) $args->expand = 'N';
537
		if(!$args->is_shortcut) $args->is_shortcut = 'N';
538
539
		if($request->menu_name_key) $args->name = $request->menu_name_key;
540
		else $args->name = $request->menu_name;
541
542
		if($request->menu_desc) $args->desc = $request->menu_desc;
543
		else $args->desc = '';
544
545
		if($request->module_id && strncasecmp('http', $request->module_id, 4) === 0)
546
		{
547
			return new BaseObject(-1, 'msg_invalid_request');
548
		}
549
550
		// when menu copy, module already copied
551
		if($isProc)
552
		{
553
			$result = $this->_insertModule($request, $args);
554
			if(!$result->toBool())
0 ignored issues
show
Bug introduced by
The method toBool cannot be called on $result (of type boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
555
			{
556
				return new BaseObject(-1, $result->message);
557
			}
558
		}
559
560
		// if setting button variables, set argument button variables for db insert. but not upload in this method
561
		if($request->normal_btn) $args->normal_btn = $request->normal_btn;
562
		if($request->hover_btn) $args->hover_btn = $request->hover_btn;
563
		if($request->active_btn) $args->active_btn = $request->active_btn;
564
565
		if(!$request->module_id)
566
		{
567
			return new BaseObject(-1, 'msg_invalid_request');
568
		}
569
570
		$args->url = $request->module_id;
571
		$args->menu_item_srl = getNextSequence();
572
		$args->listorder = -1*$args->menu_item_srl;
573
		$output = executeQuery('menu.insertMenuItem', $args);
574
		if(!$output->toBool()) return $output;
575
576
		$oDB->commit();
577
578
		$this->add('menu_item_srl', $args->menu_item_srl);
579
		$this->setMessage('success_registed', 'info');
580
	}
581
582
	/**
583
	 * insert module by men create value
584
	 * @request value of client request
585
	 * @args value for menu create
586
	 * @return bool result of create module
587
	 */
588
	private function _insertModule(&$request, &$args)
589
	{
590
		$cmArgs = new stdClass();
591
		switch ($request->module_type)
592
		{
593
			case 'WIDGET' :
594
			case 'ARTICLE' :
595
			case 'OUTSIDE' :
596
				$cmArgs->module = 'page';
597
				$cmArgs->page_type = $request->module_type;
598
				break;
599
			default:
600
				$cmArgs->module = $request->module_type;
601
				unset($cmArgs->page_type);
602
		}
603
604
		//module create
605
		$site_module_info = Context::get('site_module_info');
606
		$cmArgs->site_srl = (int)$site_module_info->site_srl;
607
		$cmArgs->browser_title = $args->name;
608
		$cmArgs->menu_srl = $request->menu_srl;
609
		$cmArgs->layout_srl = -1;
610
		$cmArgs->mlayout_srl = -1;
611
		$cmArgs->is_skin_fix = 'N';
612
		$cmArgs->is_mskin_fix = 'N';
613
614
		if(Mobile::isMobileEnabled() === true)
615
		{
616
			$cmArgs->use_mobile = 'Y';
617
		}
618
619
		// if mid is empty, auto create mid
620
		if(!$request->module_id)
621
		{
622
			$randomMid = $this->_makeRandomMid();
623
			$request->module_id = $cmArgs->module.'_'.$randomMid;
624
		}
625
		$cmArgs->mid = $request->module_id;
626
627
		// check already created module instance
628
		$oModuleModel = getModel('module');
629
		$output = $oModuleModel->getModuleInfoByMid($request->module_id);
630
		if($output->module_srl)
631
		{
632
			return new BaseObject(-1, 'msg_module_name_exists');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \BaseObject(-...g_module_name_exists'); (BaseObject) is incompatible with the return type documented by menuAdminController::_insertModule of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
633
		}
634
635
		$oModuleController = getController('module');
636
		$output = $oModuleController->insertModule($cmArgs);
637
638
		return $output;
639
	}
640
641
	/**
642
	 * Update an item to the menu, simple version
643
	 * @return void
644
	 */
645
	public function procMenuAdminUpdateItem()
646
	{
647
		$request = Context::getRequestVars();
648
649
		if(!$request->menu_item_srl || !$request->menu_name)
0 ignored issues
show
Bug introduced by
The property menu_item_srl does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Bug introduced by
The property menu_name does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
650
		{
651
			return new BaseObject(-1, 'msg_invalid_request');
652
		}
653
654
		// variables set
655
		if($request->menu_open_window != "Y") $request->menu_open_window = "N";
0 ignored issues
show
Bug introduced by
The property menu_open_window does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
656
		if($request->menu_expand != "Y") $request->menu_expand = "N";
0 ignored issues
show
Bug introduced by
The property menu_expand does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
657
658
		// Get original information
659
		$oMenuAdminModel = getAdminModel('menu');
660
		$itemInfo = $oMenuAdminModel->getMenuItemInfo($request->menu_item_srl);
661
		$args = $itemInfo;
662
663
		// if menu type is module, check exists module and update
664
		if($itemInfo->is_shortcut == 'Y')
665
		{
666
			// type is url
667
			if(strncasecmp('http', $request->shortcut_target, 4) === 0 || preg_match('/^(\.\/|\.\.\/|\/).*$/', $request->shortcut_target))
668
			{
669
				$args->url = $request->shortcut_target;
0 ignored issues
show
Bug introduced by
The property shortcut_target does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
670
			}
671
			// type is module short cut
672
			else if(is_numeric($request->shortcut_target))
673
			{
674
				// Get new original information
675
				$newItemInfo = $oMenuAdminModel->getMenuItemInfo($request->shortcut_target);
676
				if(!$newItemInfo->menu_item_srl)
677
				{
678
					return new BaseObject(-1, 'msg_invalid_request');
679
				}
680
681
				$args->url = $newItemInfo->url;
682
				$args->is_shortcut = 'Y';
683
			}
684
			else
685
			{
686
				$args->url = '#';
687
			}
688
		}
689
		else
690
		{
691
			// check already created module instance
692
			$oModuleModel = getModel('module');
693
			if($request->module_id != $itemInfo->url)
694
			{
695
				$output = $oModuleModel->getModuleInfoByMid($request->module_id);
0 ignored issues
show
Bug introduced by
The property module_id does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
696
				if($output->module_srl)
697
				{
698
					return new BaseObject(-1, 'msg_module_name_exists');
699
				}
700
			}
701
702
			// if not exist module, return error
703
			$moduleInfo = $oModuleModel->getModuleInfoByMid($itemInfo->url);
704
			if(!$moduleInfo)
705
			{
706
				return new BaseObject(-1, 'msg_invalid_request');
707
			}
708
709
			$moduleInfo->mid = $request->module_id;
710
			if($request->browser_title)
711
			{
712
				$moduleInfo->browser_title = $request->browser_title;
0 ignored issues
show
Bug introduced by
The property browser_title does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
713
			}
714
			$oModuleController = getController('module');
715
			$oModuleController->updateModule($moduleInfo);
716
			$args->url = $request->module_id;
717
		}
718
719
		if($request->menu_name_key)
720
		{
721
			$args->name = $request->menu_name_key;
0 ignored issues
show
Bug introduced by
The property menu_name_key does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
722
		}
723
		else
724
		{
725
			$args->name = $request->menu_name;
726
		}
727
728
		if($request->menu_desc) $args->desc = $request->menu_desc;
0 ignored issues
show
Bug introduced by
The property menu_desc does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
729
		else $args->desc = '';
730
731
		unset($args->group_srls);
732
		$args->open_window = $request->menu_open_window;
733
		$args->expand = $request->menu_expand;
734
		$output = $this->_updateMenuItem($args);
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
735
736
		$this->makeXmlFile($args->menu_srl);
737
738
		$this->add('menu_item_srl', $args->menu_item_srl);
739
		$this->setMessage('success_updated', 'info');
740
	}
741
742
	/**
743
	 * upload button
744
	 * @retun void
745
	 */
746
	public function procMenuAdminButtonUpload()
747
	{
748
		$args = Context::getRequestVars();
749
750
		$oMenuAdminModel = getAdminModel('menu');
751
		$item_info = $oMenuAdminModel->getMenuItemInfo($args->menu_item_srl);
0 ignored issues
show
Bug introduced by
The property menu_item_srl does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
752
		$args->menu_srl = $item_info->menu_srl;
0 ignored issues
show
Bug introduced by
The property menu_srl does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
753
754
		$btnOutput = $this->_uploadButton($args);
755
756
		if($btnOutput['normal_btn'])
757
		{
758
			$this->add('normal_btn', $btnOutput['normal_btn']);
759
			$item_info->normal_btn = $btnOutput['normal_btn'];
760
		}
761
		if($btnOutput['hover_btn'])
762
		{
763
			$this->add('hover_btn', $btnOutput['hover_btn']);
764
			$item_info->hover_btn = $btnOutput['hover_btn'];
765
		}
766
		if($btnOutput['active_btn'])
767
		{
768
			$this->add('active_btn', $btnOutput['active_btn']);
769
			$item_info->active_btn = $btnOutput['active_btn'];
770
		}
771
772
		// group_srls check
773
		if(count($item_info->group_srls) == 0)
774
		{
775
			unset($item_info->group_srls);
776
		}
777
778
		// Button delete check
779
		if(!$btnOutput['normal_btn'] && $args->isNormalDelete == 'Y')
0 ignored issues
show
Bug introduced by
The property isNormalDelete does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
780
		{
781
			$item_info->normal_btn = '';
782
		}
783
		if(!$btnOutput['hover_btn'] && $args->isHoverDelete == 'Y')
0 ignored issues
show
Bug introduced by
The property isHoverDelete does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
784
		{
785
			$item_info->hover_btn = '';
786
		}
787
		if(!$btnOutput['active_btn'] && $args->isActiveDelete == 'Y')
0 ignored issues
show
Bug introduced by
The property isActiveDelete does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
788
		{
789
			$item_info->active_btn = '';
790
		}
791
792
		$output = $this->_updateMenuItem($item_info);
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
793
794
		// recreate menu cache file
795
		$this->makeXmlFile($args->menu_srl);
796
	}
797
798
	public function updateMenuItem($itemInfo)
799
	{
800
		$output = $this->_updateMenuItem($itemInfo);
801
802
		// recreate menu cache file
803
		$this->makeXmlFile($itemInfo->menu_srl);
804
		return $output;
805
	}
806
807
	public function _updateMenuItem($itemInfo)
808
	{
809
		$output = executeQuery('menu.updateMenuItem', $itemInfo);
810
811
		return $output;
812
	}
813
814
	/**
815
	 * Delete menu item(menu of the menu)
816
	 * @return void|BaseObject
817
	 */
818
	function procMenuAdminDeleteItem()
819
	{
820
		// argument variables
821
		$args = new stdClass();
822
		$args->menu_srl = Context::get('menu_srl');
823
		$args->menu_item_srl = Context::get('menu_item_srl');
824
		$args->is_force = Context::get('is_force');
825
826
		$returnObj = $this->deleteItem($args);
827
		if(is_object($returnObj))
828
		{
829
			$this->setError($returnObj->error);
830
			$this->setMessage($returnObj->message);
831
		}
832
		else
833
		{
834
			$this->setMessage('success_deleted');
835
		}
836
837
		$returnUrl = Context::get('success_return_url') ? Context::get('success_return_url') : getNotEncodedUrl('', 'module', 'admin', 'act', 'dispMenuAdminManagement', 'menu_srl', $args->menu_srl);
838
		$this->setRedirectUrl($returnUrl);
839
	}
840
841
	/**
842
	 * Delete menu item ( Only include BO )
843
	 * @args menu_srl, menu_item_srl, is_force
844
	 * @return void|BaseObject
845
	 */
846
	public function deleteItem($args)
847
	{
848
		$oModuleModel = getModel('module');
849
		$oMenuAdminModel = getAdminModel('menu');
850
851
		// Get original information
852
		$itemInfo = $oMenuAdminModel->getMenuItemInfo($args->menu_item_srl);
853
		$args->menu_srl = $itemInfo->menu_srl;
854
855
		// Display an error that the category cannot be deleted if it has a child node	603
856
		if($args->is_force != 'Y')
857
		{
858
			$output = executeQuery('menu.getChildMenuCount', $args);
859
			if(!$output->toBool()) return $output;
860
			if($output->data->count > 0)
861
			{
862
				return new BaseObject(-1001, 'msg_cannot_delete_for_child');
863
			}
864
		}
865
866
		// Get information of the menu
867
		$menuInfo = $oMenuAdminModel->getMenu($args->menu_srl);
868
		$menu_title = $menuInfo->title;
869
870
		// check admin menu delete
871
		$oAdmin = getClass('admin');
872
		if($menu_title == $oAdmin->getAdminMenuName() && $itemInfo->parent_srl == 0)
873
		{
874
			return $this->stop('msg_cannot_delete_for_admin_topmenu');
875
		}
876
877
		if($itemInfo->parent_srl) $parent_srl = $itemInfo->parent_srl;
878
879
		// get menu properies with child menu
880
		$phpFile = sprintf("./files/cache/menu/%s.php", $args->menu_srl);
881
		$originMenu = NULL;
882
883 View Code Duplication
		if(is_readable(FileHandler::getRealPath($phpFile)))
884
		{
885
			include(FileHandler::getRealPath($phpFile));
886
887
			if(is_array($menu->list))
888
			{
889
				$this->_searchMenu($menu->list, $args->menu_item_srl, $originMenu);
0 ignored issues
show
Bug introduced by
The variable $menu 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...
890
			}
891
		}
892
893
		// check home menu in originMenu
894
		$siteInfo = $oModuleModel->getSiteInfo($menuInfo->site_srl);
895
		$isStartmenuInclude = false;
896
		$this->_checkHomeMenuInOriginMenu($originMenu, $siteInfo->mid, $isStartmenuInclude);
897
		if($isStartmenuInclude)
898
		{
899
			return new BaseObject(-1, 'msg_cannot_delete_homemenu');
900
		}
901
902
		$oDB = DB::getInstance();
903
		$oDB->begin();
904
905
		$this->_recursiveDeleteMenuItem($oDB, $menuInfo, $originMenu);
906
907
		$oDB->commit();
908
909
		// recreate menu cache file
910
		$this->makeXmlFile($args->menu_srl);
911
912
		$this->add('xml_file', $xml_file);
0 ignored issues
show
Bug introduced by
The variable $xml_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...
913
		$this->add('menu_title', $menu_title);
914
		$this->add('menu_item_srl', $parent_srl);
0 ignored issues
show
Bug introduced by
The variable $parent_srl 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...
915
916
		return new BaseObject(0, 'success_deleted');
917
	}
918
919
	private function _checkHomeMenuInOriginMenu($originMenu, $startMid, &$isStartmenuInclude)
920
	{
921
		if($originMenu['is_shortcut'] != 'Y' && $originMenu['url'] == $startMid)
922
		{
923
			$isStartmenuInclude = true;
924
		}
925
926
		if(!$isStartmenuInclude && is_array($originMenu['list']))
927
		{
928
			foreach($originMenu['list'] AS $key=>$value)
929
			{
930
				$this->_checkHomeMenuInOriginMenu($value, $startMid, $isStartmenuInclude);
931
			}
932
		}
933
	}
934
935
	private function _deleteMenuItem(&$oDB, &$menuInfo, $node)
936
	{
937
		// Remove from the DB
938
		$args = new stdClass();
939
		$args->menu_srl = $menuSrl;
0 ignored issues
show
Bug introduced by
The variable $menuSrl 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...
940
		$args->menu_item_srl = $node['node_srl'];
941
		$output = executeQuery("menu.deleteMenuItem", $args);
942
		if(!$output->toBool())
943
		{
944
			$oDB->rollback();
945
			return $output;
946
		}
947
948
		// Update the xml file and get its location
949
		$xml_file = $this->makeXmlFile($args->menu_srl);
0 ignored issues
show
Unused Code introduced by
$xml_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
950
		// Delete all of image buttons
951
		if($node['normal_btn']) FileHandler::removeFile($node['normal_btn']);
952
		if($node['hover_btn']) FileHandler::removeFile($node['hover_btn']);
953
		if($node['active_btn']) FileHandler::removeFile($node['active_btn']);
954
955
		// Delete module
956
		if($node['is_shortcut'] != 'Y' && strncasecmp('http', $node['url'], 4) !== 0)
957
		{
958
			$oModuleController = getController('module');
959
			$oModuleModel = getModel('module');
960
961
			// reference menu's url modify
962
			$args->url = $node['url'];
963
			$args->site_srl = $menuInfo->site_srl;
964
			$args->is_shortcut = 'Y';
965
			$output = executeQuery('menu.getMenuItemByUrl', $args);
966
			if($output->data->menu_item_srl)
967
			{
968
				$output->data->url = '';
969
				$referenceItem = $output->data;
970
				$output = $this->_updateMenuItem($referenceItem);
971
				if(!$output->toBool())
972
				{
973
					$oDB->rollback();
974
					return $output;
975
				}
976
			}
977
978
			$moduleInfo = $oModuleModel->getModuleInfoByMid($node['url'], $menuInfo->site_srl);
979 View Code Duplication
			if($moduleInfo->module_srl)
980
			{
981
				$output = $oModuleController->onlyDeleteModule($moduleInfo->module_srl);
982
				if(!$output->toBool())
983
				{
984
					$oDB->rollback();
985
					return $output;
986
				}
987
			}
988
		}
989
		return new BaseObject(0, 'success');
990
	}
991
992
	private function _recursiveDeleteMenuItem(&$oDB, &$menuInfo, $node)
993
	{
994
		$output = $this->_deleteMenuItem($oDB, $menuInfo, $node);
995
		if(!$output->toBool())
996
		{
997
			return new BaseObject(-1, $output->message);
998
		}
999
1000
		if(is_array($node['list']))
1001
		{
1002
			foreach($node['list'] AS $key=>$value)
1003
			{
1004
				$this->_recursiveDeleteMenuItem($oDB, $menuInfo, $value);
1005
			}
1006
		}
1007
	}
1008
1009
	/**
1010
	 * Move menu items
1011
	 * @return void
1012
	 */
1013
	function procMenuAdminMoveItem()
1014
	{
1015
		$mode = Context::get('mode');	//move
1016
		$parent_srl = Context::get('parent_srl');	// Parent menu item serial number
1017
		$source_srl = Context::get('source_srl');	// Same hierarchy's menu item serial number
1018
		$target_srl = Context::get('target_srl');	// Self menu item serial number
1019
1020
		if(!$mode || !$parent_srl || !$target_srl) return new BaseObject(-1,'msg_invalid_request');
1021
1022
		$oMenuAdminModel = getAdminModel('menu');
1023
1024
		// get original menu item info for cache file recreate
1025
		$originalItemInfo = $oMenuAdminModel->getMenuItemInfo($target_srl);
1026
		if(!$originalItemInfo->menu_item_srl)
1027
		{
1028
			return new BaseObject(-1, 'msg_empty_menu_item');
1029
		}
1030
1031
		// get menu properies with child menu
1032
		$phpFile = sprintf(_XE_PATH_ . "files/cache/menu/%s.php", $originalItemInfo->menu_srl);
1033
		$originMenu = NULL;
1034
1035 View Code Duplication
		if(is_readable(FileHandler::getRealPath($phpFile)))
1036
		{
1037
			include(FileHandler::getRealPath($phpFile));
1038
1039
			if(is_array($menu->list))
1040
			{
1041
				$this->_searchMenu($menu->list, $originalItemInfo->menu_item_srl, $originMenu);
0 ignored issues
show
Bug introduced by
The variable $menu 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...
1042
			}
1043
		}
1044
1045
		// get target menu info for move
1046
		$targetMenuItemInfo = $oMenuAdminModel->getMenuItemInfo($parent_srl);
1047
		// if move in same sitemap
1048
		if($targetMenuItemInfo->menu_item_srl)
1049
		{
1050
			$menu_srl = $targetMenuItemInfo->menu_srl;
1051
		}
1052
		// if move to other sitemap
1053
		else
1054
		{
1055
			$targetMenuInfo = $oMenuAdminModel->getMenu($parent_srl);
1056
			$menu_srl = $targetMenuInfo->menu_srl;
1057
			$parent_srl = 0;
1058
		}
1059
1060
		if(!$this->homeModuleMid)
1061
		{
1062
			$oModuleModel = getModel('module');
1063
			$oMenuAdminController = getAdminController('menu');
0 ignored issues
show
Unused Code introduced by
$oMenuAdminController is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1064
			$columnList = array('modules.mid',);
1065
			$output = $oModuleModel->getSiteInfo(0, $columnList);
1066
			if($output->mid)
1067
			{
1068
				$this->homeModuleMid = $output->mid;
1069
			}
1070
		}
1071
1072
		$this->moveMenuItem($menu_srl, $parent_srl, $source_srl, $target_srl, $mode, $originMenu['is_shortcut'], $originMenu['url']);
1073
		if(count($originMenu['list']) > 0)
1074
		{
1075
			$this->_recursiveUpdateMenuItem($originMenu['list'], $menu_srl);
1076
		}
1077
1078
		//recreate original menu
1079
		$xml_file = $this->makeXmlFile($originalItemInfo->menu_srl);
0 ignored issues
show
Unused Code introduced by
$xml_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1080
1081
		//recreate target menu
1082
		$xml_file = $this->makeXmlFile($menu_srl);
0 ignored issues
show
Unused Code introduced by
$xml_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1083
	}
1084
1085
	private function _recursiveUpdateMenuItem($node, $menu_srl)
1086
	{
1087
		if(is_array($node))
1088
		{
1089
			foreach($node AS $key=>$node)
1090
			{
1091
				$args = new stdClass();
1092
				$args->menu_srl = $menu_srl;
1093
				$args->menu_item_srl = $node['node_srl'];
1094
				$output = $this->_updateMenuItem($args);
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1095
1096
				//module's menu_srl move also
1097
				if($node['is_shortcut'] == 'N' && !empty($node['url']))
1098
				{
1099
					$oModuleModel = getModel('module');
1100
					$moduleInfo = $oModuleModel->getModuleInfoByMid($node['url']);
1101 View Code Duplication
					if($menu_srl != $moduleInfo->menu_srl)
1102
					{
1103
						$moduleInfo->menu_srl = $menu_srl;
1104
						$oModuleController = getController('module');
1105
						$output = $oModuleController->updateModule($moduleInfo);
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1106
					}
1107
				}
1108
1109
				if(count($node['list']) > 0)
1110
				{
1111
					$this->_recursiveUpdateMenuItem($node['list'], $menu_srl);
1112
				}
1113
			}
1114
		}
1115
	}
1116
1117
	/**
1118
	 * cop menu item
1119
	 * @return void
1120
	 */
1121
	public function procMenuAdminCopyItem()
1122
	{
1123
		$parentSrl = Context::get('parent_srl');
1124
		$menuItemSrl = Context::get('menu_item_srl');
1125
1126
		$oMenuModel = getAdminModel('menu');
1127
		$itemInfo = $oMenuModel->getMenuItemInfo($menuItemSrl);
1128
		$menuSrl = $itemInfo->menu_srl;
1129
1130
		// get menu properies with child menu
1131
		$phpFile = sprintf(_XE_PATH_ . "files/cache/menu/%s.php", $menuSrl);
1132
		$originMenu = NULL;
1133
1134 View Code Duplication
		if(is_readable(FileHandler::getRealPath($phpFile)))
1135
		{
1136
			include(FileHandler::getRealPath($phpFile));
1137
1138
			if(is_array($menu->list))
1139
			{
1140
				$this->_searchMenu($menu->list, $menuItemSrl, $originMenu);
0 ignored issues
show
Bug introduced by
The variable $menu 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...
1141
			}
1142
		}
1143
1144
		// copy the menu item with recursively
1145
		if(is_array($originMenu))
1146
		{
1147
			$this->_copyMenu($menuSrl, $parentSrl, $originMenu);
1148
		}
1149
		$this->add('insertedMenuItemSrlList', $this->insertedMenuItemSrlList);
1150
	}
1151
1152
	/**
1153
	 * search menu_item in full menu with recursively
1154
	 * @param $menuList menu list
1155
	 * @param $menuItemSrl current menu item serial number
1156
	 * @param $originMenu find result menu
1157
	 * @return void
1158
	 */
1159
	private function _searchMenu(&$menuList, $menuItemSrl, &$originMenu)
1160
	{
1161
		if(array_key_exists($menuItemSrl, $menuList))
1162
		{
1163
			$originMenu = $menuList[$menuItemSrl];
1164
			return;
1165
		}
1166
1167
		foreach($menuList AS $key=>$value)
1168
		{
1169
			if(count($value['list']) > 0)
1170
			{
1171
				$this->_searchMenu($value['list'], $menuItemSrl, $originMenu);
1172
			}
1173
		}
1174
	}
1175
1176
	private function _copyMenu($menuSrl, $parentSrl, &$originMenu)
1177
	{
1178
		$oMenuAdminModel = getAdminModel('menu');
1179
		$menuItemInfo = $oMenuAdminModel->getMenuItemInfo($originMenu['node_srl']);
1180
1181
		// default argument setting
1182
		$args = new stdClass();
1183
		$args->menu_srl = $menuSrl;
1184
		if($parentSrl == 0) $args->parent_srl = $menuSrl;
1185
		else $args->parent_srl = $parentSrl;
1186
		$args->menu_name_key = $originMenu['text'];
1187
		$args->menu_name = $originMenu['text'];
1188
		$args->menu_open_window = $originMenu['open_window'];
1189
		$args->menu_expand = $originMenu['expand'];
1190
		$args->normal_btn = $menuItemInfo->normal_btn;
1191
		$args->hover_btn = $menuItemInfo->hover_btn;
1192
		$args->active_btn = $menuItemInfo->active_btn;
1193
		$args->is_shortcut = $menuItemInfo->is_shortcut;
1194
1195
		$isModuleCopySuccess = false;
1196
		// if menu have a reference of module instance
1197
		if($menuItemInfo->is_shortcut == 'N' && strncasecmp('http', $originMenu['url'], 4) !== 0 )
1198
		{
1199
			$oModuleModel = getModel('module');
1200
			$moduleInfo = $oModuleModel->getModuleInfoByMid($originMenu['url']);
1201
1202
			$args->module_type = $moduleInfo->module;
1203
			$randomMid = $this->_makeRandomMid();
1204
			$args->module_id = $moduleInfo->module.'_'.$randomMid;
1205
			$args->layout_srl = $moduleInfo->layout_srl;
1206
1207
			$oModuleAdminController = getAdminController('module');
1208
			$copyArg = new stdClass();
1209
			$copyArg->module_srl = $moduleInfo->module_srl;
1210
			$copyArg->mid_1 = $args->module_id;
1211
			$copyArg->browser_title_1 = $moduleInfo->browser_title;
1212
			$copyArg->isMenuCreate = FALSE;
1213
			$copiedModuleSrl = $oModuleAdminController->procModuleAdminCopyModule($copyArg);
1214
1215
			$args->module_srl = $copiedModuleSrl;
1216
1217
			if($copiedModuleSrl)
1218
			{
1219
				$isModuleCopySuccess = true;
1220
			}
1221
		}
1222
		// if menu type is shortcut
1223
		else if($menuItemInfo->is_shortcut == 'Y')
1224
		{
1225
			$args->shortcut_target = $originMenu['url'];
1226
			$isModuleCopySuccess = true;
1227
		}
1228
1229
		if($isModuleCopySuccess)
1230
		{
1231
			// if have a group permission
1232
			if($menuItemInfo->group_srls)
1233
			{
1234
				$args->group_srls = $menuItemInfo->group_srls;
1235
			}
1236
1237
			// menu copy
1238
			$output = $this->procMenuAdminInsertItem($args);
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1239
			/*if($output && !$output->toBool())
1240
			{
1241
			return $output;
1242
			}*/
1243
1244
			// if have a button, copy a button image also
1245
			$insertedMenuItemSrl = $this->get('menu_item_srl');
1246
			if($menuItemInfo->normal_btn || $menuItemInfo->hover_btn || $menuItemInfo->active_btn)
1247
			{
1248
				// copy & upate
1249
				$update_item_info = $oMenuAdminModel->getMenuItemInfo($insertedMenuItemSrl);
1250
				$copied_info = $this->_copyButton($insertedMenuItemSrl,$update_item_info->menu_srl, $menuItemInfo);
1251
				if(count($update_item_info->group_srls) == 0)
1252
				{
1253
					unset($update_item_info->group_srls);
1254
				}
1255
				$update_item_info->normal_btn = $copied_info['normal_btn'];
1256
				$update_item_info->hover_btn = $copied_info['hover_btn'];
1257
				$update_item_info->active_btn = $copied_info['active_btn'];
1258
				$output = $this->_updateMenuItem($update_item_info);
0 ignored issues
show
Unused Code introduced by
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1259
			}
1260
			$this->insertedMenuItemSrlList[] = $insertedMenuItemSrl;
1261
		}
1262
1263
		// if have a child menu, copy child menu also
1264
		$childMenu = array_shift($originMenu['list']);
1265
		if(count($childMenu) > 0)
1266
		{
1267
			$this->_copyMenu($menuSrl, $insertedMenuItemSrl, $childMenu);
0 ignored issues
show
Bug introduced by
The variable $insertedMenuItemSrl 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...
1268
		}
1269
	}
1270
1271
	private function _makeRandomMid()
1272
	{
1273
		$time = $_SERVER['REQUEST_TIME'];
1274
		$randomString = "";
1275
		for($i=0;$i<4;$i++)
1276
		{
1277
			$case = rand(0, 1);
1278
			if($case) $doc = rand(65, 90);
1279
			else $doc = rand(97, 122);
1280
1281
			$randomString .= chr($doc);
1282
		}
1283
1284
		return $randomString.substr($time, -2);
1285
	}
1286
1287
	/**
1288
	 * Arrange menu items
1289
	 * @return void|object
1290
	 */
1291
	function procMenuAdminArrangeItem()
1292
	{
1293
		$this->menuSrl = Context::get('menu_srl');
0 ignored issues
show
Documentation Bug introduced by
The property $menuSrl was declared of type integer, but \Context::get('menu_srl') is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
1294
		$args = new stdClass();
1295
		$args->title = Context::get('title');
1296
		$parentKeyList = Context::get('parent_key');
1297
		$this->itemKeyList = Context::get('item_key');
0 ignored issues
show
Documentation Bug introduced by
It seems like \Context::get('item_key') of type string is incompatible with the declared type array of property $itemKeyList.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
1298
1299
		// menu name update
1300
		$args->menu_srl = $this->menuSrl;
1301
		$output = executeQuery('menu.updateMenu', $args);
1302
		if(!$output->toBool()) return $output;
1303
1304
		$this->map = array();
1305
		if(is_array($parentKeyList))
1306
		{
1307
			foreach($parentKeyList as $no=>$srl)
1308
			{
1309
				if($srl === 0) continue;
1310
				if(!is_array($this->map[$srl]))$this->map[$srl] = array();
1311
				$this->map[$srl][] = $no;
1312
			}
1313
		}
1314
1315
		$result = array();
1316
		if(is_array($this->itemKeyList))
1317
		{
1318
			foreach($this->itemKeyList as $srl)
1319
			{
1320
				if(!$this->checked[$srl])
1321
				{
1322
					$target = new stdClass();
1323
					$this->checked[$srl] = 1;
1324
					$target->node = $srl;
1325
					$target->child= array();
1326
1327
					while(count($this->map[$srl]))
1328
					{
1329
						$this->_setParent($srl, array_shift($this->map[$srl]), $target);
1330
					}
1331
					$result[] = $target;
1332
				}
1333
			}
1334
		}
1335
1336
		if(is_array($result))
1337
		{
1338
			$i = 0;
1339
			foreach($result AS $key=>$node)
1340
			{
1341
				$this->moveMenuItem($this->menuSrl, 0, $i, $node->node, 'move');	//move parent node
1342
				$this->_recursiveMoveMenuItem($node);	//move child node
1343
				$i = $node->node;
1344
			}
1345
		}
1346
1347
		$this->setMessage('success_updated', 'info');
1348
1349
		$returnUrl = Context::get('success_return_url') ? Context::get('success_return_url') : getNotEncodedUrl('', 'module', 'admin', 'act', 'dispMenuAdminManagement', 'menu_srl', $args->menu_srl);
1350
		$this->setRedirectUrl($returnUrl);
1351
	}
1352
1353
	/**
1354
	 * Set parent number to child
1355
	 * @param int $parent_srl
1356
	 * @param int $child_index
1357
	 * @param object $target
1358
	 * @return void
1359
	 */
1360
	function _setParent($parent_srl, $child_index, &$target)
1361
	{
1362
		$child_srl = $this->itemKeyList[$child_index];
1363
		$this->checked[$child_srl] = 1;
1364
1365
		$child_node = new stdClass();
1366
		$child_node->node = $child_srl;
1367
		$child_node->parent_node = $parent_srl;
1368
		$child_node->child = array();
1369
		$target->child[] = $child_node;
1370
1371
		while(count($this->map[$child_srl]))
1372
		{
1373
			$this->_setParent($child_srl, array_shift($this->map[$child_srl]), $child_node);
1374
		}
1375
		//return $target;
1376
	}
1377
1378
	/**
1379
	 * move item with sub directory(recursive)
1380
	 * @param object $result
1381
	 * @return void
1382
	 */
1383
	function _recursiveMoveMenuItem($result)
1384
	{
1385
		$i = 0;
1386
		while(count($result->child))
1387
		{
1388
			unset($node);
1389
			$node = array_shift($result->child);
1390
1391
			$this->moveMenuItem($this->menuSrl, $node->parent_node, $i, $node->node, 'move');
1392
			$this->_recursiveMoveMenuItem($node);
1393
			$i = $node->node;
1394
		}
1395
	}
1396
1397
	/**
1398
	 * move menu item
1399
	 * @param int $menu_srl
1400
	 * @param int $parent_srl
1401
	 * @param int $source_srl
1402
	 * @param int $target_srl
1403
	 * @param string $mode 'move' or 'insert'
1404
	 * @return void
1405
	 */
1406
	function moveMenuItem($menu_srl, $parent_srl, $source_srl, $target_srl, $mode, $isShortcut='Y', $url=NULL)
1407
	{
1408
		// Get the original menus
1409
		$oMenuAdminModel = getAdminModel('menu');
1410
1411
		$target_item = $oMenuAdminModel->getMenuItemInfo($target_srl);
1412
		if($target_item->menu_item_srl != $target_srl) return new BaseObject(-1,'msg_invalid_request');
1413
		// Move the menu location(change the order menu appears)
1414
		if($mode == 'move')
1415
		{
1416
			$args = new stdClass();
1417
			$args->parent_srl = $parent_srl;
1418
			$args->menu_srl = $menu_srl;
1419
1420
			if($source_srl)
1421
			{
1422
				$source_item = $oMenuAdminModel->getMenuItemInfo($source_srl);
1423
				if($source_item->menu_item_srl != $source_srl) return new BaseObject(-1,'msg_invalid_request');
1424
				$args->listorder = $source_item->listorder-1;
1425
			}
1426
			else
1427
			{
1428
				$output = executeQuery('menu.getMaxListorder', $args);
1429
				if(!$output->toBool()) return $output;
1430
				$args->listorder = (int)$output->data->listorder;
1431
				if(!$args->listorder) $args->listorder= 0;
1432
			}
1433
			$args->parent_srl = $parent_srl;
1434
			$output = executeQuery('menu.updateMenuItemListorder', $args);
1435
			if(!$output->toBool()) return $output;
1436
1437
			$args->parent_srl = $parent_srl;
1438
			$args->menu_item_srl = $target_srl;
1439
			$output = executeQuery('menu.updateMenuItemNode', $args);
1440
			if(!$output->toBool()) return $output;
1441
1442
			//module's menu_srl move also
1443
			if($isShortcut == 'N' && !empty($url))
1444
			{
1445
				$oModuleModel = getModel('module');
1446
				$moduleInfo = $oModuleModel->getModuleInfoByMid($url);
1447 View Code Duplication
				if($menu_srl != $moduleInfo->menu_srl)
1448
				{
1449
					$moduleInfo->menu_srl = $menu_srl;
1450
					$oModuleController = getController('module');
1451
					$output = $oModuleController->updateModule($moduleInfo);
1452
				}
1453
1454
				// change home menu cache file
1455
				if($url == $this->homeModuleMid)
1456
				{
1457
					if(file_exists($this->homeMenuCacheFile))
1458
					{
1459
						include($this->homeMenuCacheFile);
1460
					}
1461
					if(!$homeMenuSrl || $homeMenuSrl != $menu_srl)
0 ignored issues
show
Bug introduced by
The variable $homeMenuSrl 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...
1462
					{
1463
						$this->makeHomemenuCacheFile($menu_srl);
1464
					}
1465
				}
1466
			}
1467
			// Add a child
1468
		}
1469
		elseif($mode == 'insert')
1470
		{
1471
			$args->menu_item_srl = $target_srl;
0 ignored issues
show
Bug introduced by
The variable $args seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
1472
			$args->parent_srl = $parent_srl;
0 ignored issues
show
Bug introduced by
The variable $args seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
1473
			$args->listorder = -1*getNextSequence();
0 ignored issues
show
Bug introduced by
The variable $args seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
1474
			$output = executeQuery('menu.updateMenuItemNode', $args);
0 ignored issues
show
Bug introduced by
The variable $args seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
1475
			if(!$output->toBool()) return $output;
1476
		}
1477
1478
		$xml_file = $this->makeXmlFile($menu_srl);
1479
		return $xml_file;
1480
	}
1481
1482
	/**
1483
	 * Update xml file
1484
	 * XML file is not often generated after setting menus on the admin page\n
1485
	 * For this occasional cases, manually update was implemented. \n
1486
	 * It looks unnecessary at this moment however no need to eliminate the feature. Just leave it.
1487
	 * @return void
1488
	 */
1489 View Code Duplication
	function procMenuAdminMakeXmlFile()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1490
	{
1491
		// Check input value
1492
		$menu_srl = Context::get('menu_srl');
1493
		// Get information of the menu
1494
		$oMenuAdminModel = getAdminModel('menu');
1495
		$menu_info = $oMenuAdminModel->getMenu($menu_srl);
1496
		$menu_title = $menu_info->title;
1497
		// Re-generate the xml file
1498
		$xml_file = $this->makeXmlFile($menu_srl);
1499
		// Set return value
1500
		$this->add('menu_title',$menu_title);
1501
		$this->add('xml_file',$xml_file);
1502
	}
1503
1504
	/**
1505
	 * Register a menu image button
1506
	 * @return void
1507
	 */
1508
	function procMenuAdminUploadButton()
1509
	{
1510
		$menu_srl = Context::get('menu_srl');
1511
		$menu_item_srl = Context::get('menu_item_srl');
1512
		$target = Context::get('target');
1513
		$target_file = Context::get($target);
1514
		// Error occurs when the target is neither a uploaded file nor a valid file
1515
		if(!$menu_srl || !$menu_item_srl)
1516
		{
1517
			Context::set('error_messge', Context::getLang('msg_invalid_request'));
1518
1519
		}
1520
		else if(!$target_file || !is_uploaded_file($target_file['tmp_name']) || !preg_match('/\.(gif|jpeg|jpg|png)$/i',$target_file['name'])  || !checkUploadedFile($target_file['tmp_name']))
1521
		{
1522
			Context::set('error_messge', Context::getLang('msg_invalid_request'));
1523
		}
1524
1525
		// Move the file to a specific director if the uploaded file meets requirement
1526
		else
1527
		{
1528
			$tmp_arr = explode('.',$target_file['name']);
1529
			$ext = $tmp_arr[count($tmp_arr)-1];
1530
1531
			$path = sprintf('./files/attach/menu_button/%d/', $menu_srl);
1532
			$filename = sprintf('%s%d.%s.%s', $path, $menu_item_srl, $target, $ext);
1533
1534
			if(!is_dir($path)) FileHandler::makeDir($path);
1535
1536
			move_uploaded_file($target_file['tmp_name'], $filename);
1537
			Context::set('filename', $filename);
1538
		}
1539
1540
		$this->setTemplatePath($this->module_path.'tpl');
1541
		$this->setTemplateFile('menu_file_uploaded');
1542
	}
1543
1544
	/**
1545
	 * Remove the menu image button
1546
	 * @return void
1547
	 */
1548
	function procMenuAdminDeleteButton()
1549
	{
1550
		$menu_srl = Context::get('menu_srl');
0 ignored issues
show
Unused Code introduced by
$menu_srl is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1551
		$menu_item_srl = Context::get('menu_item_srl');
0 ignored issues
show
Unused Code introduced by
$menu_item_srl is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1552
		$target = Context::get('target');
1553
		$filename = Context::get('filename');
1554
		FileHandler::removeFile($filename);
1555
1556
		$this->add('target', $target);
1557
	}
1558
1559
	/**
1560
	 * Get all act list for admin menu
1561
	 * @return void
1562
	 */
1563
	function procMenuAdminAllActList()
1564
	{
1565
		$oModuleModel = getModel('module');
1566
		$installed_module_list = $oModuleModel->getModulesXmlInfo();
1567
		if(is_array($installed_module_list))
1568
		{
1569
			$currentLang = Context::getLangType();
0 ignored issues
show
Unused Code introduced by
$currentLang is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1570
			$menuList = array();
1571 View Code Duplication
			foreach($installed_module_list AS $key=>$value)
1572
			{
1573
				$info = $oModuleModel->getModuleActionXml($value->module);
1574
				if($info->menu) $menuList[$value->module] = $info->menu;
1575
				unset($info->menu);
1576
			}
1577
		}
1578
		$this->add('menuList', $menuList);
0 ignored issues
show
Bug introduced by
The variable $menuList 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...
1579
	}
1580
1581
	/**
1582
	 * Get all act list for admin menu
1583
	 * @return void|object
1584
	 */
1585
	function procMenuAdminInsertItemForAdminMenu()
1586
	{
1587
		$requestArgs = Context::getRequestVars();
1588
		$tmpMenuName = explode(':', $requestArgs->menu_name);
0 ignored issues
show
Bug introduced by
The property menu_name does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1589
		$moduleName = $tmpMenuName[0];
1590
		$menuName = $tmpMenuName[1];
1591
1592
		// variable setting
1593
		$logged_info = Context::get('logged_info');
1594
		//$oMenuAdminModel = getAdminModel('menu');
1595
		$oMemberModel = getModel('member');
1596
1597
		//$parentMenuInfo = $oMenuAdminModel->getMenuItemInfo($requestArgs->parent_srl);
1598
		$groupSrlList = $oMemberModel->getMemberGroups($logged_info->member_srl);
1599
1600
		//preg_match('/\{\$lang->menu_gnb\[(.*?)\]\}/i', $parentMenuInfo->name, $m);
1601
		$oModuleModel = getModel('module');
1602
		//$info = $oModuleModel->getModuleInfoXml($moduleName);
1603
		$info = $oModuleModel->getModuleActionXml($moduleName);
1604
1605
		$url = getNotEncodedFullUrl('', 'module', 'admin', 'act', $info->menu->{$menuName}->index);
1606
		if(empty($url)) $url = getNotEncodedFullUrl('', 'module', 'admin', 'act', $info->admin_index_act);
1607
		if(empty($url)) $url = getNotEncodedFullUrl('', 'module', 'admin');
1608
		$dbInfo = Context::getDBInfo();
1609
1610
		$args = new stdClass();
1611
		$args->menu_item_srl = (!$requestArgs->menu_item_srl) ? getNextSequence() : $requestArgs->menu_item_srl;
0 ignored issues
show
Bug introduced by
The property menu_item_srl does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1612
		$args->parent_srl = $requestArgs->parent_srl;
0 ignored issues
show
Bug introduced by
The property parent_srl does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1613
		$args->menu_srl = $requestArgs->menu_srl;
0 ignored issues
show
Bug introduced by
The property menu_srl does not seem to exist in BaseObject.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1614
		$args->name = sprintf('{$lang->menu_gnb_sub[\'%s\']}', $menuName);
1615
		//if now page is https...
1616 View Code Duplication
		if(strpos($url, 'https') !== false)
1617
		{
1618
			$args->url = str_replace('https'.substr($dbInfo->default_url, 4), '', $url);
1619
		}
1620
		else
1621
		{
1622
			$args->url = str_replace($dbInfo->default_url, '', $url);
1623
		}
1624
		$args->open_window = 'N';
1625
		$args->expand = 'N';
1626
		$args->normal_btn = '';
1627
		$args->hover_btn = '';
1628
		$args->active_btn = '';
1629
		$args->group_srls = implode(',', array_keys($groupSrlList));
1630
		$args->listorder = -1*$args->menu_item_srl;
1631
1632
		// Check if already exists
1633
		$oMenuModel = getAdminModel('menu');
1634
		$item_info = $oMenuModel->getMenuItemInfo($args->menu_item_srl);
1635
		// Update if exists
1636
		if($item_info->menu_item_srl == $args->menu_item_srl)
1637
		{
1638
			$output = $this->_updateMenuItem($args);
1639
			if(!$output->toBool()) return $output;
1640
		}
1641
		// Insert if not exist
1642
		else
1643
		{
1644
			$args->listorder = -1*$args->menu_item_srl;
1645
			$output = executeQuery('menu.insertMenuItem', $args);
1646
			if(!$output->toBool()) return $output;
1647
		}
1648
		// Get information of the menu
1649
		$menu_info = $oMenuModel->getMenu($args->menu_srl);
1650
		$menu_title = $menu_info->title;
0 ignored issues
show
Unused Code introduced by
$menu_title is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1651
		// Update the xml file and get its location
1652
		$xml_file = $this->makeXmlFile($args->menu_srl);
0 ignored issues
show
Unused Code introduced by
$xml_file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1653
1654
		$returnUrl = Context::get('success_return_url') ? Context::get('success_return_url') : getNotEncodedUrl('', 'module', 'admin', 'act', 'dispAdminSetup');
1655
		$this->setRedirectUrl($returnUrl);
1656
	}
1657
1658
	/**
1659
	 * Update menu auth (Exposure and Access)
1660
	 * @return void
1661
	 */
1662
	public function procMenuAdminUpdateAuth()
1663
	{
1664
		$menuItemSrl = Context::get('menu_item_srl');
1665
		$exposure = Context::get('exposure');
1666
		$htPerm = Context::get('htPerm');
1667
1668
		$oMenuModel = getAdminModel('menu');
1669
		$itemInfo = $oMenuModel->getMenuItemInfo($menuItemSrl);
1670
		$args = $itemInfo;
1671
1672
		// Menu Exposure update
1673
		// if exposure target is only login user...
1674
		if(!$exposure)
1675
		{
1676
			$args->group_srls = '';
1677
		}
1678
		else
1679
		{
1680
			$exposure = explode(',', $exposure);
1681
			if(in_array($exposure, array('-1','-3')))
1682
			{
1683
				$args->group_srls = $exposure;
1684
			}
1685
1686
			if($exposure) $args->group_srls = implode(',', $exposure);
0 ignored issues
show
Bug Best Practice introduced by
The expression $exposure of type array 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...
1687
		}
1688
1689
		$output = $this->_updateMenuItem($args);
1690
		if(!$output->toBool())
1691
		{
1692
			return $output;
1693
		}
1694
1695
		// Module Access update
1696
		unset($args);
1697
		$oMenuAdminModel = getAdminModel('menu');
1698
		$menuInfo = $oMenuAdminModel->getMenu($itemInfo->menu_srl);
1699
1700
		$oModuleModel = getModel('module');
1701
		$moduleInfo = $oModuleModel->getModuleInfoByMid($itemInfo->url, $menuInfo->site_srl);
1702
		$xml_info = $oModuleModel->getModuleActionXML($moduleInfo->module);
1703
1704
		if($itemInfo->is_shortcut === 'Y')
1705
		{
1706
			$moduleGrnatsArgs = new stdClass;
1707
			$moduleGrnatsArgs->module_srl = $moduleInfo->module_srl;
1708
			$output = executeQueryArray('module.getModuleGrants', $moduleGrnatsArgs);
1709
			if(!$output->data) $output->data = array();
1710
			$moduleGrnats = new stdClass();
1711
			foreach($output->data as $grant)
1712
			{
1713
				$moduleGrnats->{$grant->name}[] = $grant->group_srl;
1714
			}
1715
		}
1716
1717
		$grantList = $xml_info->grant;
1718
		if(!$grantList) $grantList = new stdClass;
1719
1720
		$grantList->access = new stdClass();
1721
		$grantList->access->default = 'guest';
1722
		$grantList->manager = new stdClass();
1723
		$grantList->manager->default = 'manager';
1724
1725
		$grant = new stdClass();
1726
		foreach($grantList AS $grantName=>$grantInfo)
1727
		{
1728
			if($htPerm[$grantName])
1729
			{
1730
				$htPerm[$grantName] = explode(',', $htPerm[$grantName]);
1731
1732
				// users in a particular group
1733
				if(is_array($htPerm[$grantName]))
1734
				{
1735
					$grant->{$grantName} = $htPerm[$grantName];
1736
					continue;
1737
				}
1738
				// -1 = Log-in user only, -2 = site members only, 0 = all users
1739
				else
1740
				{
1741
					$grant->{$grantName}[] = $htPerm[$grantName];
1742
					continue;
1743
				}
1744
			}
1745
			else if($itemInfo->is_shortcut === 'Y')
1746
			{
1747
				if(isset($moduleGrnats) && $moduleGrnats->{$grantName}) $grant->{$grantName} = $moduleGrnats->{$grantName};
1748
			}
1749
		}
1750
1751
		if(count($grant))
1752
		{
1753
			$oModuleController = getController('module');
1754
			$oModuleController->insertModuleGrants($moduleInfo->module_srl, $grant);
1755
		}
1756
1757
		// recreate menu cache file
1758
		$this->makeXmlFile($itemInfo->menu_srl);
1759
	}
1760
1761
	/**
1762
	 * Generate XML file for menu and return its location
1763
	 * @param int $menu_srl
1764
	 * @return string
1765
	 */
1766
	function makeXmlFile($menu_srl)
1767
	{
1768
		// Return if there is no information when creating the xml file
1769
		if(!$menu_srl) return;
1770
		// Get menu informaton
1771
		$args = new stdClass();
1772
		$args->menu_srl = $menu_srl;
1773
		$output = executeQuery('menu.getMenu', $args);
1774
		if(!$output->toBool() || !$output->data) return $output;
1775
		$site_srl = (int)$output->data->site_srl;
1776
1777
		if($site_srl)
1778
		{
1779
			$oModuleModel = getModel('module');
1780
			$columnList = array('sites.domain');
1781
			$site_info = $oModuleModel->getSiteInfo($site_srl, $columnList);
1782
			$domain = $site_info->domain;
1783
		}
1784
		// Get a list of menu items corresponding to menu_srl by listorder
1785
		$args->menu_srl = $menu_srl;
1786
		$args->sort_index = 'listorder';
1787
		$output = executeQuery('menu.getMenuItems', $args);
1788
		if(!$output->toBool()) return;
1789
		// Specify the name of the cache file
1790
		$xml_file = sprintf(_XE_PATH_ . "files/cache/menu/%s.xml.php", $menu_srl);
1791
		$php_file = sprintf(_XE_PATH_ . "files/cache/menu/%s.php", $menu_srl);
1792
		// If no data found, generate an XML file without node data
1793
		$list = $output->data;
1794 View Code Duplication
		if(!$list)
1795
		{
1796
			$xml_buff = "<root />";
1797
			FileHandler::writeFile($xml_file, $xml_buff);
1798
			FileHandler::writeFile($php_file, '<?php if(!defined("__XE__")) exit(); ?>');
1799
			return $xml_file;
1800
		}
1801
		// Change to an array if only a single data is obtained
1802
		if(!is_array($list)) $list = array($list);
1803
		// Create a tree for loop
1804
		$list_count = count($list);
1805
		for($i=0;$i<$list_count;$i++)
1806
		{
1807
			$node = $list[$i];
1808
			$menu_item_srl = $node->menu_item_srl;
1809
			$parent_srl = $node->parent_srl;
1810
1811
			$tree[$parent_srl][$menu_item_srl] = $node;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tree was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tree = 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...
1812
		}
1813
		// A common header to set permissions of the cache file and groups
1814
		$header_script =
1815
			'$lang_type = Context::getLangType(); '.
1816
			'$is_logged = Context::get(\'is_logged\'); '.
1817
			'$logged_info = Context::get(\'logged_info\'); '.
1818
			'$site_srl = '.$site_srl.';'.
1819
			'$site_admin = false;'.
1820
			'if($site_srl) { '.
1821
			'$oModuleModel = getModel(\'module\');'.
1822
			'$site_module_info = $oModuleModel->getSiteInfo($site_srl); '.
1823
			'if($site_module_info) Context::set(\'site_module_info\',$site_module_info);'.
1824
			'else $site_module_info = Context::get(\'site_module_info\');'.
1825
			'$grant = $oModuleModel->getGrant($site_module_info, $logged_info); '.
1826
			'if($grant->manager ==1) $site_admin = true;'.
1827
			'}'.
1828
			'if($is_logged) {'.
1829
			'if($logged_info->is_admin=="Y") $is_admin = true; '.
1830
			'else $is_admin = false; '.
1831
			'$group_srls = array_keys($logged_info->group_list); '.
1832
			'} else { '.
1833
			'$is_admin = false; '.
1834
			'$group_srls = array(); '.
1835
			'}';
1836
		// Create the xml cache file (a separate session is needed for xml cache)
1837
		$xml_buff = sprintf(
1838
			'<?php '.
1839
			'define(\'__XE__\', true); '.
1840
			'require_once(\''.FileHandler::getRealPath('./config/config.inc.php').'\'); '.
1841
			'$oContext = Context::getInstance(); '.
1842
			'$oContext->init(); '.
1843
			'header("Content-Type: text/xml; charset=UTF-8"); '.
1844
			'header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); '.
1845
			'header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); '.
1846
			'header("Cache-Control: no-store, no-cache, must-revalidate"); '.
1847
			'header("Cache-Control: post-check=0, pre-check=0", false); '.
1848
			'header("Pragma: no-cache"); '.
1849
			'%s '.
1850
			'$oContext->close(); '.
1851
			'?>'.
1852
			'<root>%s</root>',
1853
			$header_script,
1854
			$this->getXmlTree($tree[0], $tree, $site_srl, $domain)
0 ignored issues
show
Bug introduced by
The variable $tree 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...
Bug introduced by
The variable $domain 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...
1855
		);
1856
		// Create php cache file
1857
		$php_output = $this->getPhpCacheCode($tree[0], $tree, $site_srl, $domain);
1858
		$php_buff = sprintf(
1859
			'<?php '.
1860
			'if(!defined("__XE__")) exit(); '.
1861
			'$menu = new stdClass();' .
1862
			'%s; '.
1863
			'%s; '.
1864
			'$menu->list = array(%s); '.
1865
			'if(!$is_admin) { recurciveExposureCheck($menu->list); }'.
1866
			'Context::set("included_menu", $menu); '.
1867
			'?>',
1868
			$header_script,
1869
			$php_output['name'],
1870
			$php_output['buff']
1871
		);
1872
		// Save File
1873
		FileHandler::writeFile($xml_file, $xml_buff);
1874
		FileHandler::writeFile($php_file, $php_buff);
1875
		return $xml_file;
1876
	}
1877
1878
	/**
1879
	 * Create xml data recursively looping for array nodes by referencing to parent_srl
1880
	 * menu xml file uses a tag named "node" and this XML configures menus on admin page.
1881
	 * (Implement tree menu by reading the xml file in tree_menu.js)
1882
	 * @param array $source_node
1883
	 * @param array $tree
1884
	 * @param int $site_srl
1885
	 * @param string $domain
1886
	 * @return string
1887
	 */
1888
	function getXmlTree($source_node, $tree, $site_srl, $domain)
1889
	{
1890
		if(!$source_node) return;
0 ignored issues
show
Bug Best Practice introduced by
The expression $source_node of type array 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...
1891
1892
		$oMenuAdminModel = getAdminModel('menu');
1893
1894
		foreach($source_node as $menu_item_srl => $node)
1895
		{
1896
			$child_buff = "";
1897
			// Get data of the child nodes
1898
			if($menu_item_srl&&$tree[$menu_item_srl]) $child_buff = $this->getXmlTree($tree[$menu_item_srl], $tree, $site_srl, $domain);
1899
			// List variables
1900
			$names = $oMenuAdminModel->getMenuItemNames($node->name, $site_srl);
1901
			foreach($names as $key => $val)
1902
			{
1903
				$name_arr_str .= sprintf('"%s"=>%s,',$key, var_export($val, true));
0 ignored issues
show
Bug introduced by
The variable $name_arr_str 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...
1904
			}
1905
			$name_str = sprintf('$_names = array(%s); print $_names[$lang_type];', $name_arr_str);
1906
1907
			$url = str_replace(array('&','"','<','>'),array('&amp;','&quot;','&lt;','&gt;'),$node->url);
1908
			$desc = str_replace(array('&','"',"'"),array('&amp;','&quot;','\\\''),$node->desc);
1909 View Code Duplication
			if(preg_match('/^([0-9a-zA-Z\_\-]+)$/', $node->url))
1910
			{
1911
				$href = "getSiteUrl('$domain', '','mid','$node->url')";
1912
			}
1913
			else $href = var_export($url, true);
1914
			$is_shortcut = ($node->is_shortcut) ? $node->is_shortcut : '';
1915
			$open_window = ($node->open_window) ? $node->open_window : '';
1916
			$expand = ($node->expand) ? $node->expand : '';
1917
1918
			$normal_btn = ($node->normal_btn) ? $node->normal_btn : '';
1919 View Code Duplication
			if($normal_btn && strncasecmp('./files/attach/menu_button', $normal_btn, 26) === 0) $normal_btn = str_replace(array('&','"','<','>'),array('&amp;','&quot;','&lt;','&gt;'),$normal_btn);
1920
			else $normal_btn = '';
1921
			$hover_btn = ($node->hover_btn) ? $node->hover_btn : '';
1922 View Code Duplication
			if($hover_btn && strncasecmp('./files/attach/menu_button', $hover_btn, 26) === 0) $hover_btn = str_replace(array('&','"','<','>'),array('&amp;','&quot;','&lt;','&gt;'),$hover_btn);
1923
			else $hover_btn = '';
1924
			$active_btn = ($node->active_btn) ? $node->active_btn : '';
1925 View Code Duplication
			if($active_btn && strncasecmp('./files/attach/menu_button', $active_btn, 26) === 0) $active_btn = str_replace(array('&','"','<','>'),array('&amp;','&quot;','&lt;','&gt;'),$active_btn);
1926
			else $active_btn = '';
1927
1928
			$group_srls = ($node->group_srls) ? $node->group_srls : '';
1929
1930
			if($normal_btn)
1931
			{
1932
				if($hover_btn) $hover_str = sprintf('onmouseover=&quot;this.src=\'%s\'&quot;', $hover_btn); else $hover_str = '';
1933
				if($active_btn) $active_str = sprintf('onmousedown=&quot;this.src=\'%s\'&quot;', $active_btn); else $active_str = '';
1934
				$link = sprintf('&lt;img src=&quot;%s&quot; onmouseout=&quot;this.src=\'%s\'&quot; alt=&quot;<?php print htmlspecialchars($_names[$lang_type], ENT_COMPAT | ENT_HTML401, \'UTF-8\', false) ?>&quot; %s %s /&gt;', $normal_btn, $normal_btn, $hover_str, $active_str);
1935
			}
1936
			else
1937
			{
1938
				$link = '<?php print $_names[$lang_type]; ?>';
1939
			}
1940
1941
			// If the value of node->group_srls exists
1942 View Code Duplication
			if($group_srls) {
1943
				$group_check_code = sprintf('($is_admin==true||(is_array($group_srls)&&count(array_intersect($group_srls, array(%s))))||($is_logged&&%s))',$group_srls,$group_srls == -1?1:0);
1944
			}
1945
			else
1946
			{
1947
				$group_check_code = "true";
1948
			}
1949
1950
			$attribute = sprintf(
1951
				'node_srl="%d" parent_srl="%d" menu_name_key=%s text="<?php if(%s) { %s }?>" url="<?php print(%s?%s:"")?>" href="<?php print(%s?%s:"")?>" is_shortcut=%s desc=%s open_window=%s expand=%s normal_btn=%s hover_btn=%s active_btn=%s link="<?php if(%s) {?>%s<?php }?>"',
1952
				$menu_item_srl,
1953
				($node->parent_srl) ? $node->parent_srl : '',
1954
				var_export(($node->name) ? $node->name : '', true),
1955
				$group_check_code,
1956
				$name_str,
1957
				$group_check_code,
1958
				var_export($url, true),
1959
				$group_check_code,
1960
				$href,
1961
				var_export($is_shortcut, true),
1962
				var_export($desc, true),
1963
				var_export($open_window, true),
1964
				var_export($expand, true),
1965
				var_export($normal_btn, true),
1966
				var_export($hover_btn, true),
1967
				var_export($active_btn, true),
1968
				$group_check_code,
1969
				$link
1970
			);
1971
1972 View Code Duplication
			if($child_buff) $buff .= sprintf('<node %s>%s</node>', $attribute, $child_buff);
0 ignored issues
show
Bug introduced by
The variable $buff 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...
1973
			else $buff .=  sprintf('<node %s />', $attribute);
1974
		}
1975
		return $buff;
1976
	}
1977
1978
	/**
1979
	 * Return php code converted from nodes in an array
1980
	 * Although xml data can be used for tpl, menu to menu, it needs to use javascript separately
1981
	 * By creating cache file in php and then you can get menu information without DB
1982
	 * This cache includes in ModuleHandler::displayContent() and then Context::set()
1983
	 * @param array $source_node
1984
	 * @param array $tree
1985
	 * @param int $site_srl
1986
	 * @param string $domain
1987
	 * @return array
1988
	 */
1989
	function getPhpCacheCode($source_node, $tree, $site_srl, $domain)
1990
	{
1991
		$output = array("buff"=>"", "url_list"=>array());
1992
		if(!$source_node) return $output;
0 ignored issues
show
Bug Best Practice introduced by
The expression $source_node of type array 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...
1993
1994
		$oMenuAdminModel = getAdminModel('menu');
1995
1996
		foreach($source_node as $menu_item_srl => $node)
1997
		{
1998
			// Get data from child nodes if exist.
1999
			if($menu_item_srl&&$tree[$menu_item_srl]) $child_output = $this->getPhpCacheCode($tree[$menu_item_srl], $tree, $site_srl, $domain);
2000
			else $child_output = array("buff"=>"", "url_list"=>array());
2001
2002
			// List variables
2003
			$names = $oMenuAdminModel->getMenuItemNames($node->name, $site_srl);
2004
			unset($name_arr_str);
2005
			foreach($names as $key => $val)
2006
			{
2007
				if(preg_match('/\{\$lang->menu_gnb(?:_sub)?\[\'([a-zA-Z_]?[a-zA-Z_0-9]*)\'\]\}/', $val) === 1)
2008
				{
2009
					$name_arr_str .= sprintf('"%s"=>"%s",', $key, $val);
0 ignored issues
show
Bug introduced by
The variable $name_arr_str 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...
2010
				}
2011
				else
2012
				{
2013
					$name_arr_str .= sprintf('"%s"=>\'%s\',', $key, str_replace(array('\\','\''), array('\\\\','\\\''), removeHackTag($val)));
2014
				}
2015
			}
2016
			$name_str = sprintf('$_menu_names[%d] = array(%s); %s', $node->menu_item_srl, $name_arr_str, $child_output['name']);
2017
2018
			// If url value is not empty in the current node, put the value into an array url_list
2019
			if($node->url) $child_output['url_list'][] = $node->url;
2020
			$output['url_list'] = array_merge($output['url_list'], $child_output['url_list']);
2021
			// If node->group_srls value exists
2022 View Code Duplication
			if($node->group_srls)$group_check_code = sprintf('($is_admin==true||(is_array($group_srls)&&count(array_intersect($group_srls, array(%s))))||($is_logged && %s))',$node->group_srls,$node->group_srls == -1?1:0);
2023
			else $group_check_code = "true";
2024
2025
			// List variables
2026
			$href = str_replace(array('&','"','<','>'),array('&amp;','&quot;','&lt;','&gt;'),$node->href);
0 ignored issues
show
Unused Code introduced by
$href is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2027
			$url = str_replace(array('&','"','<','>'),array('&amp;','&quot;','&lt;','&gt;'),$node->url);
2028
			$desc = str_replace(array('&','"',"'"),array('&amp;','&quot;','\\\''),$node->desc);
2029 View Code Duplication
			if(preg_match('/^([0-9a-zA-Z\_\-]+)$/i', $node->url))
2030
			{
2031
				$href = "getSiteUrl('$domain', '','mid','$node->url')";
2032
			}
2033
			else $href = var_export($url, true);
2034
			$is_shortcut = $node->is_shortcut;
2035
			$open_window = $node->open_window;
2036
			$normal_btn = str_replace(array('&','"','<','>'),array('&amp;','&quot;','&lt;','&gt;'),$node->normal_btn);
0 ignored issues
show
Unused Code introduced by
$normal_btn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2037
			$hover_btn = str_replace(array('&','"','<','>'),array('&amp;','&quot;','&lt;','&gt;'),$node->hover_btn);
0 ignored issues
show
Unused Code introduced by
$hover_btn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2038
			$active_btn = str_replace(array('&','"','<','>'),array('&amp;','&quot;','&lt;','&gt;'),$node->active_btn);
0 ignored issues
show
Unused Code introduced by
$active_btn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2039
2040
			foreach($child_output['url_list'] as $key =>$val)
2041
			{
2042
				$child_output['url_list'][$key] = addslashes($val);
2043
			}
2044
2045
			$selected = '"'.implode('","',$child_output['url_list']).'"';
2046
			$child_buff = $child_output['buff'];
2047
			$expand = $node->expand;
2048
2049
			$normal_btn = $node->normal_btn;
2050 View Code Duplication
			if($normal_btn && strncasecmp('./files/attach/menu_button', $normal_btn, 26) === 0) $normal_btn = str_replace(array('&','"','<','>'),array('&amp;','&quot;','&lt;','&gt;'),$normal_btn);
2051
			else $normal_btn = '';
2052
2053
			$hover_btn = $node->hover_btn;
2054 View Code Duplication
			if($hover_btn && strncasecmp('./files/attach/menu_button', $hover_btn, 26) === 0) $hover_btn = str_replace(array('&','"','<','>'),array('&amp;','&quot;','&lt;','&gt;'),$hover_btn);
2055
			else $hover_btn = '';
2056
2057
			$active_btn = $node->active_btn;
2058 View Code Duplication
			if($active_btn && strncasecmp('./files/attach/menu_button', $active_btn, 26) === 0) $active_btn = str_replace(array('&','"','<','>'),array('&amp;','&quot;','&lt;','&gt;'),$active_btn);
2059
			else $active_btn = '';
2060
2061
2062
			$group_srls = $node->group_srls;
0 ignored issues
show
Unused Code introduced by
$group_srls is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
2063
2064
			if($normal_btn)
2065
			{
2066
				if($hover_btn) $hover_str = sprintf('onmouseover=\"this.src=\'%s\'\"', $hover_btn); else $hover_str = '';
2067
				if($active_btn) $active_str = sprintf('onmousedown=\"this.src=\'%s\'\"', $active_btn); else $active_str = '';
2068
				$link = sprintf('"<img src=\"%s\" onmouseout=\"this.src=\'%s\'\" alt=\"".$_menu_names[%d][$lang_type]."\" %s %s />"', $normal_btn, $normal_btn, $node->menu_item_srl, $hover_str, $active_str);
2069
				if($active_btn) $link_active = sprintf('"<img src=\"%s\" onmouseout=\"this.src=\'%s\'\" alt=\"".$_menu_names[%d][$lang_type]."\" %s />"', $active_btn, $active_btn, $node->menu_item_srl, $hover_str);
2070
				else $link_active = $link;
2071
			}
2072
			else
2073
			{
2074
				$link_active = $link = sprintf('$_menu_names[%d][$lang_type]', $node->menu_item_srl);
2075
			}
2076
			// Create properties (check if it belongs to the menu node by url_list. It looks a trick but fast and powerful)
2077
			$attribute = sprintf(
2078
				'"node_srl" => %d, "parent_srl" => %d, "menu_name_key" => %s, "isShow" => (%s ? true : false), "text" => (%s ? $_menu_names[%d][$lang_type] : ""), "href" => (%s ? %s : ""), "url" => (%s ? %s : ""), "is_shortcut" => %s, "desc" => %s, "open_window" => %s, "normal_btn" => %s, "hover_btn" => %s, "active_btn" => %s, "selected" => (array(%s) && in_array(Context::get("mid"), array(%s)) ? 1 : 0), "expand" => %s, "list" => array(%s), "link" => (%s ? (array(%s) && in_array(Context::get("mid"), array(%s)) ? %s : %s) : ""),',
2079
				$node->menu_item_srl,
2080
				$node->parent_srl,
2081
				var_export($node->name, true),
2082
				$group_check_code,
2083
				$group_check_code,
2084
				$node->menu_item_srl,
2085
				$group_check_code,
2086
				$href,
2087
				$group_check_code,
2088
				var_export($url, true),
2089
				var_export($is_shortcut, true),
2090
				var_export($desc, true),
2091
				var_export($open_window, true),
2092
				var_export($normal_btn, true),
2093
				var_export($hover_btn, true),
2094
				var_export($active_btn, true),
2095
				$selected,
2096
				$selected,
2097
				var_export($expand, true),
2098
				$child_buff,
2099
				$group_check_code,
2100
				$selected,
2101
				$selected,
2102
				$link_active,
2103
				$link
2104
			);
2105
2106
			// Generate buff data
2107
			$output['buff'] .=  sprintf('%s=>array(%s),', $node->menu_item_srl, $attribute);
2108
			$output['name'] .= $name_str;
2109
		}
2110
		return $output;
2111
	}
2112
2113
	/**
2114
	 * Mapping menu and layout
2115
	 * When setting menu on the layout, map the default layout
2116
	 * @param int $layout_srl
2117
	 * @param array $menu_srl_list
2118
	 */
2119
	function updateMenuLayout($layout_srl, $menu_srl_list)
2120
	{
2121
		if(!count($menu_srl_list)) return;
2122
		// Delete the value of menu_srls
2123
		$args->menu_srls = implode(',',$menu_srl_list);
0 ignored issues
show
Bug introduced by
The variable $args 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...
2124
		$output = executeQuery('menu.deleteMenuLayout', $args);
2125
		if(!$output->toBool()) return $output;
2126
2127
		$args->layout_srl = $layout_srl;
2128
		// Mapping menu_srls, layout_srl
2129
		for($i=0;$i<count($menu_srl_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...
2130
		{
2131
			$args->menu_srl = $menu_srl_list[$i];
2132
			$output = executeQuery('menu.insertMenuLayout', $args);
2133
			if(!$output->toBool()) return $output;
2134
		}
2135
	}
2136
2137
	/**
2138
	 * Register a menu image button
2139
	 * @param object $args
2140
	 * @return array
2141
	 */
2142
	function _uploadButton($args)
2143
	{
2144
		// path setting
2145
		$path = sprintf('./files/attach/menu_button/%d/', $args->menu_srl);
2146
		if($args->menu_normal_btn || $args->menu_hover_btn || $args->menu_active_btn && !is_dir($path))
2147
		{
2148
			FileHandler::makeDir($path);
2149
		}
2150
2151
		if($args->isNormalDelete == 'Y' || $args->isHoverDelete == 'Y' || $args->isActiveDelete == 'Y')
2152
		{
2153
			$oMenuModel = getAdminModel('menu');
2154
			$itemInfo = $oMenuModel->getMenuItemInfo($args->menu_item_srl);
2155
2156
			if($args->isNormalDelete == 'Y' && $itemInfo->normal_btn) FileHandler::removeFile($itemInfo->normal_btn);
2157
			if($args->isHoverDelete == 'Y' && $itemInfo->hover_btn) FileHandler::removeFile($itemInfo->hover_btn);
2158
			if($args->isActiveDelete == 'Y' && $itemInfo->active_btn) FileHandler::removeFile($itemInfo->active_btn);
2159
		}
2160
2161
		$returnArray = array();
2162
		$date = date('YmdHis');
2163
		// normal button
2164 View Code Duplication
		if($args->menu_normal_btn)
2165
		{
2166
			$tmp_arr = explode('.',$args->menu_normal_btn['name']);
2167
			$ext = $tmp_arr[count($tmp_arr)-1];
2168
2169
			$filename = sprintf('%s%d.%s.%s.%s', $path, $args->menu_item_srl, $date, 'menu_normal_btn', $ext);
2170
2171
			if(checkUploadedFile($args->menu_normal_btn['tmp_name']))
2172
			{
2173
				move_uploaded_file ( $args->menu_normal_btn ['tmp_name'], $filename );
2174
				$returnArray ['normal_btn'] = $filename;
2175
			}
2176
		}
2177
2178
		// hover button
2179 View Code Duplication
		if($args->menu_hover_btn)
2180
		{
2181
			$tmp_arr = explode('.',$args->menu_hover_btn['name']);
2182
			$ext = $tmp_arr[count($tmp_arr)-1];
2183
2184
			$filename = sprintf('%s%d.%s.%s.%s', $path, $args->menu_item_srl, $date, 'menu_hover_btn', $ext);
2185
2186
			if(checkUploadedFile($args->menu_hover_btn['tmp_name']))
2187
			{
2188
				move_uploaded_file($args->menu_hover_btn['tmp_name'], $filename);
2189
				$returnArray['hover_btn'] = $filename;
2190
			}
2191
		}
2192
2193
		// active button
2194 View Code Duplication
		if($args->menu_active_btn)
2195
		{
2196
			$tmp_arr = explode('.',$args->menu_active_btn['name']);
2197
			$ext = $tmp_arr[count($tmp_arr)-1];
2198
2199
			$filename = sprintf('%s%d.%s.%s.%s', $path, $args->menu_item_srl, $date, 'menu_active_btn', $ext);
2200
2201
			if(checkUploadedFile($args->menu_active_btn['tmp_name']))
2202
			{
2203
				move_uploaded_file($args->menu_active_btn['tmp_name'], $filename);
2204
				$returnArray['active_btn'] = $filename;
2205
			}
2206
2207
		}
2208
		return $returnArray;
2209
	}
2210
2211
	/**
2212
	 * When copy a menu, button copied also.
2213
	 * @param $args menuItemInfo with button values
2214
	 */
2215
	private function _copyButton($insertedMenuItemSrl, $insertedMenuSrl, &$menuItemInfo)
2216
	{
2217
		$copied_info = array(
2218
			"normal_btn"=>"",
2219
			"hover_btn"=>"",
2220
			"active_btn"=>"",
2221
		);
2222
		//normal_btn
2223 View Code Duplication
		if($menuItemInfo->normal_btn)
2224
		{
2225
			$originFile = FileHandler::getRealPath($menuItemInfo->normal_btn);
2226
			$targetFile = $this->_changeMenuItemSrlInButtonPath($menuItemInfo->normal_btn, $insertedMenuSrl, $insertedMenuItemSrl, 'normal');
2227
2228
			FileHandler::copyFile($originFile, $targetFile);
2229
			$copied_info['normal_btn'] = $targetFile;
2230
		}
2231
2232
		//hover_btn
2233 View Code Duplication
		if($menuItemInfo->hover_btn)
2234
		{
2235
			$originFile = FileHandler::getRealPath($menuItemInfo->hover_btn);
2236
			$targetFile = $this->_changeMenuItemSrlInButtonPath($menuItemInfo->hover_btn, $insertedMenuSrl, $insertedMenuItemSrl, 'hover');
2237
2238
			FileHandler::copyFile($originFile, $targetFile);
2239
			$copied_info['hover_btn'] = $targetFile;
2240
		}
2241
2242
		//active_btn
2243 View Code Duplication
		if($menuItemInfo->active_btn)
2244
		{
2245
			$originFile = FileHandler::getRealPath($menuItemInfo->active_btn);
2246
			$targetFile = $this->_changeMenuItemSrlInButtonPath($menuItemInfo->active_btn, $insertedMenuSrl, $insertedMenuItemSrl, 'active');
2247
2248
			FileHandler::copyFile($originFile, $targetFile);
2249
			$copied_info['active_btn'] = $targetFile;
2250
		}
2251
		return $copied_info;
2252
	}
2253
2254
	private function _changeMenuItemSrlInButtonPath($buttonPath, $menuSrl, $menuItemSrl, $mode)
2255
	{
2256
		$path = sprintf('./files/attach/menu_button/%d/', $menuSrl);
2257
		$tmp_arr = explode('.', $buttonPath);
2258
		$ext = $tmp_arr[count($tmp_arr)-1];
2259
		$date = date("YmdHis");
2260
		return sprintf('%s%d.%s.%s.%s', $path, $menuItemSrl,$date,'menu_'.$mode.'_btn', $ext);
2261
	}
2262
2263
	public function makeHomemenuCacheFile($menuSrl)
2264
	{
2265
		$cacheBuff .= sprintf('<?php if(!defined("__XE__")) exit();');
0 ignored issues
show
Bug introduced by
The variable $cacheBuff 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...
2266
		$cacheBuff .= sprintf('$homeMenuSrl = %d;', $menuSrl);
2267
2268
		FileHandler::writeFile($this->homeMenuCacheFile, $cacheBuff);
2269
	}
2270
2271
	public function getHomeMenuCacheFile()
2272
	{
2273
		return $this->homeMenuCacheFile;
2274
	}
2275
}
2276
/* End of file menu.admin.controller.php */
2277
/* Location: ./modules/menu/menu.admin.controller.php */
2278