GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — develop (#1929)
by
unknown
11:46
created

menuAdminController::getHomeMenuCacheFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) NAVER <http://www.navercorp.com> */
3
/**
4
 * menuAdminController class
5
 * admin controller class of the menu module
6
 *
7
 * @author NAVER ([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');
0 ignored issues
show
Bug introduced by
The property module_path cannot be accessed from this context as it is declared private in class ModuleObject.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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 Object 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 Object
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 Object(-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 Object();
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|Object
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 Object(-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 Object(-1, 'msg_cannot_delete_homemenu');
291
		}
292
293
		$output = $this->deleteMenu($menu_srl);
294
		if(!$output->toBool())
295
		{
296
			return new Object(-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 Object
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 Object(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 Object(-1, 'msg_invalid_request');
397
		}
398
399
		$this->_setMenuSrl($request->parent_srl, $request->menu_srl);
400
		if(!$request->menu_srl)
401
		{
402
			return new Object(-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 Object($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->expand = $request->menu_expand;
465
			$args->is_shortcut = $request->is_shortcut;
466
			$args->url = $request->shortcut_target;
467
468
			if(!$args->open_window) $args->open_window = 'N';
469
			if(!$args->expand) $args->expand = 'N';
470
			if(!$args->is_shortcut) $args->is_shortcut = 'Y';
471
472
			if($request->menu_name_key) $args->name = $request->menu_name_key;
473
			else $args->name = $request->menu_name;
474
		}
475
		// type is module short cut
476
		else if(is_numeric($request->shortcut_target))
477
		{
478
			// Get original information
479
			$oMenuAdminModel = getAdminModel('menu');
480
			$itemInfo = $oMenuAdminModel->getMenuItemInfo($request->shortcut_target);
481
			if(!$itemInfo->menu_item_srl)
482
			{
483
				return new Object(-1, 'msg_invalid_request');
484
			}
485
			unset($itemInfo->normal_btn, $itemInfo->hover_btn, $itemInfo->active_btn);
486
487
			$args = $itemInfo;
488
			if(count($args->group_srls) == 0)
489
			{
490
				unset($args->group_srls);
491
			}
492
			$args->menu_srl = $request->menu_srl;
493
			$args->name = $request->menu_name;
494
			$args->parent_srl = $request->parent_srl;
495
			$args->is_shortcut = $request->is_shortcut;
496
		}
497
		// empty target shortcut
498
		else
499
		{
500
			$args = new stdClass();
501
			$args->menu_srl = $request->menu_srl;
502
			$args->name = $request->menu_name;
503
			$args->parent_srl = $request->parent_srl;
504
			$args->is_shortcut = $request->is_shortcut;
505
			$args->url = '#';
506
		}
507
508
		if($request->menu_desc) $args->desc = $request->menu_desc;
509
		else $args->desc = '';
510
511
		$args->menu_item_srl = getNextSequence();
512
		$args->listorder = -1*$args->menu_item_srl;
513
		$output = executeQuery('menu.insertMenuItem', $args);
514
		if(!$output->toBool()) return $output;
515
516
		$oDB->commit();
517
518
		$this->add('menu_item_srl', $args->menu_item_srl);
519
		$this->setMessage('success_registed', 'info');
520
	}
521
522
	private function _insertMenu(&$request, $isProc)
523
	{
524
		$oDB = DB::getInstance();
525
		$oDB->begin();
526
527
		// set menu variable
528
		$args = new stdClass();
529
		$args->menu_srl = $request->menu_srl;
530
		$args->parent_srl = $request->parent_srl;
531
		$args->open_window = $request->menu_open_window;
532
		$args->expand = $request->menu_expand;
533
		$args->expand = $request->menu_expand;
534
		$args->is_shortcut = $request->is_shortcut;
535
536
		if(!$args->open_window) $args->open_window = 'N';
537
		if(!$args->expand) $args->expand = 'N';
538
		if(!$args->is_shortcut) $args->is_shortcut = 'N';
539
540
		if($request->menu_name_key) $args->name = $request->menu_name_key;
541
		else $args->name = $request->menu_name;
542
543
		if($request->menu_desc) $args->desc = $request->menu_desc;
544
		else $args->desc = '';
545
546
		if($request->module_id && strncasecmp('http', $request->module_id, 4) === 0)
547
		{
548
			return new Object(-1, 'msg_invalid_request');
549
		}
550
551
		// when menu copy, module already copied
552
		if($isProc)
553
		{
554
			$result = $this->_insertModule($request, $args);
555
			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...
556
			{
557
				return new Object(-1, $result->message);
558
			}
559
		}
560
561
		// if setting button variables, set argument button variables for db insert. but not upload in this method
562
		if($request->normal_btn) $args->normal_btn = $request->normal_btn;
563
		if($request->hover_btn) $args->hover_btn = $request->hover_btn;
564
		if($request->active_btn) $args->active_btn = $request->active_btn;
565
566
		if(!$request->module_id)
567
		{
568
			return new Object(-1, 'msg_invalid_request');
569
		}
570
571
		$args->url = $request->module_id;
572
		$args->menu_item_srl = getNextSequence();
573
		$args->listorder = -1*$args->menu_item_srl;
574
		$output = executeQuery('menu.insertMenuItem', $args);
575
		if(!$output->toBool()) return $output;
576
577
		$oDB->commit();
578
579
		$this->add('menu_item_srl', $args->menu_item_srl);
580
		$this->setMessage('success_registed', 'info');
581
	}
582
583
	/**
584
	 * insert module by men create value
585
	 * @request value of client request
586
	 * @args value for menu create
587
	 * @return bool result of create module
588
	 */
589
	private function _insertModule(&$request, &$args)
590
	{
591
		$cmArgs = new stdClass();
592
		switch ($request->module_type)
593
		{
594
			case 'WIDGET' :
595
			case 'ARTICLE' :
596
			case 'OUTSIDE' :
597
				$cmArgs->module = 'page';
598
				$cmArgs->page_type = $request->module_type;
599
				break;
600
			default:
601
				$cmArgs->module = $request->module_type;
602
				unset($cmArgs->page_type);
603
		}
604
605
		//module create
606
		$site_module_info = Context::get('site_module_info');
607
		$cmArgs->site_srl = (int)$site_module_info->site_srl;
608
		$cmArgs->browser_title = $args->name;
609
		$cmArgs->menu_srl = $request->menu_srl;
610
		$cmArgs->layout_srl = -1;
611
		$cmArgs->mlayout_srl = -1;
612
		$cmArgs->is_skin_fix = 'N';
613
		$cmArgs->is_mskin_fix = 'N';
614
615
		if(Mobile::isMobileEnabled() === true)
616
		{
617
			$cmArgs->use_mobile = 'Y';
618
		}
619
620
		// if mid is empty, auto create mid
621
		if(!$request->module_id)
622
		{
623
			$randomMid = $this->_makeRandomMid();
624
			$request->module_id = $cmArgs->module.'_'.$randomMid;
625
		}
626
		$cmArgs->mid = $request->module_id;
627
628
		// check already created module instance
629
		$oModuleModel = getModel('module');
630
		$output = $oModuleModel->getModuleInfoByMid($request->module_id);
631
		if($output->module_srl)
632
		{
633
			return new Object(-1, 'msg_module_name_exists');
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \Object(-1, 'msg_module_name_exists'); (Object) 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...
634
		}
635
636
		$oModuleController = getController('module');
637
		$output = $oModuleController->insertModule($cmArgs);
638
639
		return $output;
640
	}
641
642
	/**
643
	 * Update an item to the menu, simple version
644
	 * @return void
645
	 */
646
	public function procMenuAdminUpdateItem()
647
	{
648
		$request = Context::getRequestVars();
649
650
		if(!$request->menu_item_srl || !$request->menu_name)
651
		{
652
			return new Object(-1, 'msg_invalid_request');
653
		}
654
655
		// variables set
656
		if($request->menu_open_window != "Y") $request->menu_open_window = "N";
657
		if($request->menu_expand != "Y") $request->menu_expand = "N";
658
659
		// Get original information
660
		$oMenuAdminModel = getAdminModel('menu');
661
		$itemInfo = $oMenuAdminModel->getMenuItemInfo($request->menu_item_srl);
662
		$args = $itemInfo;
663
664
		// if menu type is module, check exists module and update
665
		if($itemInfo->is_shortcut == 'Y')
666
		{
667
			// type is url
668
			if(strncasecmp('http', $request->shortcut_target, 4) === 0 || preg_match('/^(\.\/|\.\.\/|\/).*$/', $request->shortcut_target))
669
			{
670
				$args->url = $request->shortcut_target;
671
			}
672
			// type is module short cut
673
			else if(is_numeric($request->shortcut_target))
674
			{
675
				// Get new original information
676
				$newItemInfo = $oMenuAdminModel->getMenuItemInfo($request->shortcut_target);
677
				if(!$newItemInfo->menu_item_srl)
678
				{
679
					return new Object(-1, 'msg_invalid_request');
680
				}
681
682
				$args->url = $newItemInfo->url;
683
				$args->is_shortcut = 'Y';
684
			}
685
			else
686
			{
687
				$args->url = '#';
688
			}
689
		}
690
		else
691
		{
692
			// check already created module instance
693
			$oModuleModel = getModel('module');
694
			if($request->module_id != $itemInfo->url)
695
			{
696
				$output = $oModuleModel->getModuleInfoByMid($request->module_id);
697
				if($output->module_srl)
698
				{
699
					return new Object(-1, 'msg_module_name_exists');
700
				}
701
			}
702
703
			// if not exist module, return error
704
			$moduleInfo = $oModuleModel->getModuleInfoByMid($itemInfo->url);
705
			if(!$moduleInfo)
706
			{
707
				return new Object(-1, 'msg_invalid_request');
708
			}
709
710
			$moduleInfo->mid = $request->module_id;
711
			if($request->browser_title)
712
			{
713
				$moduleInfo->browser_title = $request->browser_title;
714
			}
715
			$oModuleController = getController('module');
716
			$oModuleController->updateModule($moduleInfo);
717
			$args->url = $request->module_id;
718
		}
719
720
		if($request->menu_name_key)
721
		{
722
			$args->name = $request->menu_name_key;
723
		}
724
		else
725
		{
726
			$args->name = $request->menu_name;
727
		}
728
729
		if($request->menu_desc) $args->desc = $request->menu_desc;
730
		else $args->desc = '';
731
732
		unset($args->group_srls);
733
		$args->open_window = $request->menu_open_window;
734
		$args->expand = $request->menu_expand;
735
		$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...
736
737
		$this->makeXmlFile($args->menu_srl);
738
739
		$this->add('menu_item_srl', $args->menu_item_srl);
740
		$this->setMessage('success_updated', 'info');
741
	}
742
743
	/**
744
	 * upload button
745
	 * @retun void
746
	 */
747
	public function procMenuAdminButtonUpload()
748
	{
749
		$args = Context::getRequestVars();
750
751
		$oMenuAdminModel = getAdminModel('menu');
752
		$item_info = $oMenuAdminModel->getMenuItemInfo($args->menu_item_srl);
753
		$args->menu_srl = $item_info->menu_srl;
754
755
		$btnOutput = $this->_uploadButton($args);
756
757
		if($btnOutput['normal_btn'])
758
		{
759
			$this->add('normal_btn', $btnOutput['normal_btn']);
760
			$item_info->normal_btn = $btnOutput['normal_btn'];
761
		}
762
		if($btnOutput['hover_btn'])
763
		{
764
			$this->add('hover_btn', $btnOutput['hover_btn']);
765
			$item_info->hover_btn = $btnOutput['hover_btn'];
766
		}
767
		if($btnOutput['active_btn'])
768
		{
769
			$this->add('active_btn', $btnOutput['active_btn']);
770
			$item_info->active_btn = $btnOutput['active_btn'];
771
		}
772
773
		// group_srls check
774
		if(count($item_info->group_srls) == 0)
775
		{
776
			unset($item_info->group_srls);
777
		}
778
779
		// Button delete check
780
		if(!$btnOutput['normal_btn'] && $args->isNormalDelete == 'Y')
781
		{
782
			$item_info->normal_btn = '';
783
		}
784
		if(!$btnOutput['hover_btn'] && $args->isHoverDelete == 'Y')
785
		{
786
			$item_info->hover_btn = '';
787
		}
788
		if(!$btnOutput['active_btn'] && $args->isActiveDelete == 'Y')
789
		{
790
			$item_info->active_btn = '';
791
		}
792
793
		$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...
794
795
		// recreate menu cache file
796
		$this->makeXmlFile($args->menu_srl);
797
	}
798
799
	public function updateMenuItem($itemInfo)
800
	{
801
		$output = $this->_updateMenuItem($itemInfo);
802
803
		// recreate menu cache file
804
		$this->makeXmlFile($itemInfo->menu_srl);
805
		return $output;
806
	}
807
808
	public function _updateMenuItem($itemInfo)
809
	{
810
		$output = executeQuery('menu.updateMenuItem', $itemInfo);
811
812
		return $output;
813
	}
814
815
	/**
816
	 * Delete menu item(menu of the menu)
817
	 * @return void|Object
818
	 */
819
	function procMenuAdminDeleteItem()
820
	{
821
		// argument variables
822
		$args = new stdClass();
823
		$args->menu_srl = Context::get('menu_srl');
824
		$args->menu_item_srl = Context::get('menu_item_srl');
825
		$args->is_force = Context::get('is_force');
826
827
		$returnObj = $this->deleteItem($args);
828
		if(is_object($returnObj))
829
		{
830
			$this->setError($returnObj->error);
831
			$this->setMessage($returnObj->message);
832
		}
833
		else
834
		{
835
			$this->setMessage('success_deleted');
836
		}
837
838
		$returnUrl = Context::get('success_return_url') ? Context::get('success_return_url') : getNotEncodedUrl('', 'module', 'admin', 'act', 'dispMenuAdminManagement', 'menu_srl', $args->menu_srl);
839
		$this->setRedirectUrl($returnUrl);
840
	}
841
842
	/**
843
	 * Delete menu item ( Only include BO )
844
	 * @args menu_srl, menu_item_srl, is_force
845
	 * @return void|Object
846
	 */
847
	public function deleteItem($args)
848
	{
849
		$oModuleModel = getModel('module');
850
		$oMenuAdminModel = getAdminModel('menu');
851
852
		// Get original information
853
		$itemInfo = $oMenuAdminModel->getMenuItemInfo($args->menu_item_srl);
854
		$args->menu_srl = $itemInfo->menu_srl;
855
856
		// Display an error that the category cannot be deleted if it has a child node	603
857
		if($args->is_force != 'Y')
858
		{
859
			$output = executeQuery('menu.getChildMenuCount', $args);
860
			if(!$output->toBool()) return $output;
861
			if($output->data->count > 0)
862
			{
863
				return new Object(-1001, 'msg_cannot_delete_for_child');
864
			}
865
		}
866
867
		// Get information of the menu
868
		$menuInfo = $oMenuAdminModel->getMenu($args->menu_srl);
869
		$menu_title = $menuInfo->title;
870
871
		// check admin menu delete
872
		$oAdmin = getClass('admin');
873
		if($menu_title == $oAdmin->getAdminMenuName() && $itemInfo->parent_srl == 0)
874
		{
875
			return $this->stop('msg_cannot_delete_for_admin_topmenu');
876
		}
877
878
		if($itemInfo->parent_srl) $parent_srl = $itemInfo->parent_srl;
879
880
		// get menu properies with child menu
881
		$phpFile = sprintf("./files/cache/menu/%s.php", $args->menu_srl);
882
		$originMenu = NULL;
883
884 View Code Duplication
		if(is_readable(FileHandler::getRealPath($phpFile)))
885
		{
886
			include(FileHandler::getRealPath($phpFile));
887
888
			if(is_array($menu->list))
889
			{
890
				$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...
891
			}
892
		}
893
894
		// check home menu in originMenu
895
		$siteInfo = $oModuleModel->getSiteInfo($menuInfo->site_srl);
896
		$isStartmenuInclude = false;
897
		$this->_checkHomeMenuInOriginMenu($originMenu, $siteInfo->mid, $isStartmenuInclude);
898
		if($isStartmenuInclude)
899
		{
900
			return new Object(-1, 'msg_cannot_delete_homemenu');
901
		}
902
903
		$oDB = DB::getInstance();
904
		$oDB->begin();
905
906
		$this->_recursiveDeleteMenuItem($oDB, $menuInfo, $originMenu);
907
908
		$oDB->commit();
909
910
		// recreate menu cache file
911
		$this->makeXmlFile($args->menu_srl);
912
913
		$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...
914
		$this->add('menu_title', $menu_title);
915
		$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...
916
917
		return new Object(0, 'success_deleted');
918
	}
919
920
	private function _checkHomeMenuInOriginMenu($originMenu, $startMid, &$isStartmenuInclude)
921
	{
922
		if($originMenu['is_shortcut'] != 'Y' && $originMenu['url'] == $startMid)
923
		{
924
			$isStartmenuInclude = true;
925
		}
926
927
		if(!$isStartmenuInclude && is_array($originMenu['list']))
928
		{
929
			foreach($originMenu['list'] AS $key=>$value)
930
			{
931
				$this->_checkHomeMenuInOriginMenu($value, $startMid, $isStartmenuInclude);
932
			}
933
		}
934
	}
935
936
	private function _deleteMenuItem(&$oDB, &$menuInfo, $node)
937
	{
938
		// Remove from the DB
939
		$args = new stdClass();
940
		$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...
941
		$args->menu_item_srl = $node['node_srl'];
942
		$output = executeQuery("menu.deleteMenuItem", $args);
943
		if(!$output->toBool())
944
		{
945
			$oDB->rollback();
946
			return $output;
947
		}
948
949
		// Update the xml file and get its location
950
		$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...
951
		// Delete all of image buttons
952
		if($node['normal_btn']) FileHandler::removeFile($node['normal_btn']);
953
		if($node['hover_btn']) FileHandler::removeFile($node['hover_btn']);
954
		if($node['active_btn']) FileHandler::removeFile($node['active_btn']);
955
956
		// Delete module
957
		if($node['is_shortcut'] != 'Y' && strncasecmp('http', $node['url'], 4) !== 0)
958
		{
959
			$oModuleController = getController('module');
960
			$oModuleModel = getModel('module');
961
962
			// reference menu's url modify
963
			$args->url = $node['url'];
964
			$args->site_srl = $menuInfo->site_srl;
965
			$args->is_shortcut = 'Y';
966
			$output = executeQuery('menu.getMenuItemByUrl', $args);
967
			if($output->data->menu_item_srl)
968
			{
969
				$output->data->url = '';
970
				$referenceItem = $output->data;
971
				$output = $this->_updateMenuItem($referenceItem);
972
				if(!$output->toBool())
973
				{
974
					$oDB->rollback();
975
					return $output;
976
				}
977
			}
978
979
			$moduleInfo = $oModuleModel->getModuleInfoByMid($node['url'], $menuInfo->site_srl);
980 View Code Duplication
			if($moduleInfo->module_srl)
981
			{
982
				$output = $oModuleController->onlyDeleteModule($moduleInfo->module_srl);
983
				if(!$output->toBool())
984
				{
985
					$oDB->rollback();
986
					return $output;
987
				}
988
			}
989
		}
990
		return new Object(0, 'success');
991
	}
992
993
	private function _recursiveDeleteMenuItem(&$oDB, &$menuInfo, $node)
994
	{
995
		$output = $this->_deleteMenuItem($oDB, $menuInfo, $node);
996
		if(!$output->toBool())
997
		{
998
			return new Object(-1, $output->message);
999
		}
1000
1001
		if(is_array($node['list']))
1002
		{
1003
			foreach($node['list'] AS $key=>$value)
1004
			{
1005
				$this->_recursiveDeleteMenuItem($oDB, $menuInfo, $value);
1006
			}
1007
		}
1008
	}
1009
1010
	/**
1011
	 * Move menu items
1012
	 * @return void
1013
	 */
1014
	function procMenuAdminMoveItem()
1015
	{
1016
		$mode = Context::get('mode');	//move
1017
		$parent_srl = Context::get('parent_srl');	// Parent menu item serial number
1018
		$source_srl = Context::get('source_srl');	// Same hierarchy's menu item serial number
1019
		$target_srl = Context::get('target_srl');	// Self menu item serial number
1020
1021
		if(!$mode || !$parent_srl || !$target_srl) return new Object(-1,'msg_invalid_request');
1022
1023
		$oMenuAdminModel = getAdminModel('menu');
1024
1025
		// get original menu item info for cache file recreate
1026
		$originalItemInfo = $oMenuAdminModel->getMenuItemInfo($target_srl);
1027
		if(!$originalItemInfo->menu_item_srl)
1028
		{
1029
			return new Object(-1, 'msg_empty_menu_item');
1030
		}
1031
1032
		// get menu properies with child menu
1033
		$phpFile = sprintf(_XE_PATH_ . "files/cache/menu/%s.php", $originalItemInfo->menu_srl);
1034
		$originMenu = NULL;
1035
1036 View Code Duplication
		if(is_readable(FileHandler::getRealPath($phpFile)))
1037
		{
1038
			include(FileHandler::getRealPath($phpFile));
1039
1040
			if(is_array($menu->list))
1041
			{
1042
				$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...
1043
			}
1044
		}
1045
1046
		// get target menu info for move
1047
		$targetMenuItemInfo = $oMenuAdminModel->getMenuItemInfo($parent_srl);
1048
		// if move in same sitemap
1049
		if($targetMenuItemInfo->menu_item_srl)
1050
		{
1051
			$menu_srl = $targetMenuItemInfo->menu_srl;
1052
		}
1053
		// if move to other sitemap
1054
		else
1055
		{
1056
			$targetMenuInfo = $oMenuAdminModel->getMenu($parent_srl);
1057
			$menu_srl = $targetMenuInfo->menu_srl;
1058
			$parent_srl = 0;
1059
		}
1060
1061
		if(!$this->homeModuleMid)
1062
		{
1063
			$oModuleModel = getModel('module');
1064
			$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...
1065
			$columnList = array('modules.mid',);
1066
			$output = $oModuleModel->getSiteInfo(0, $columnList);
1067
			if($output->mid)
1068
			{
1069
				$this->homeModuleMid = $output->mid;
1070
			}
1071
		}
1072
1073
		$this->moveMenuItem($menu_srl, $parent_srl, $source_srl, $target_srl, $mode, $originMenu['is_shortcut'], $originMenu['url']);
1074
		if(count($originMenu['list']) > 0)
1075
		{
1076
			$this->_recursiveUpdateMenuItem($originMenu['list'], $menu_srl);
1077
		}
1078
1079
		//recreate original menu
1080
		$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...
1081
1082
		//recreate target menu
1083
		$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...
1084
	}
1085
1086
	private function _recursiveUpdateMenuItem($node, $menu_srl)
1087
	{
1088
		if(is_array($node))
1089
		{
1090
			foreach($node AS $key=>$node)
1091
			{
1092
				$args = new stdClass();
1093
				$args->menu_srl = $menu_srl;
1094
				$args->menu_item_srl = $node['node_srl'];
1095
				$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...
1096
1097
				//module's menu_srl move also
1098
				if($node['is_shortcut'] == 'N' && !empty($node['url']))
1099
				{
1100
					$oModuleModel = getModel('module');
1101
					$moduleInfo = $oModuleModel->getModuleInfoByMid($node['url']);
1102 View Code Duplication
					if($menu_srl != $moduleInfo->menu_srl)
1103
					{
1104
						$moduleInfo->menu_srl = $menu_srl;
1105
						$oModuleController = getController('module');
1106
						$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...
1107
					}
1108
				}
1109
1110
				if(count($node['list']) > 0)
1111
				{
1112
					$this->_recursiveUpdateMenuItem($node['list'], $menu_srl);
1113
				}
1114
			}
1115
		}
1116
	}
1117
1118
	/**
1119
	 * cop menu item
1120
	 * @return void
1121
	 */
1122
	public function procMenuAdminCopyItem()
1123
	{
1124
		$parentSrl = Context::get('parent_srl');
1125
		$menuItemSrl = Context::get('menu_item_srl');
1126
1127
		$oMenuModel = getAdminModel('menu');
1128
		$itemInfo = $oMenuModel->getMenuItemInfo($menuItemSrl);
1129
		$menuSrl = $itemInfo->menu_srl;
1130
1131
		// get menu properies with child menu
1132
		$phpFile = sprintf(_XE_PATH_ . "files/cache/menu/%s.php", $menuSrl);
1133
		$originMenu = NULL;
1134
1135 View Code Duplication
		if(is_readable(FileHandler::getRealPath($phpFile)))
1136
		{
1137
			include(FileHandler::getRealPath($phpFile));
1138
1139
			if(is_array($menu->list))
1140
			{
1141
				$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...
1142
			}
1143
		}
1144
1145
		// copy the menu item with recursively
1146
		if(is_array($originMenu))
1147
		{
1148
			$this->_copyMenu($menuSrl, $parentSrl, $originMenu);
1149
		}
1150
		$this->add('insertedMenuItemSrlList', $this->insertedMenuItemSrlList);
1151
	}
1152
1153
	/**
1154
	 * search menu_item in full menu with recursively
1155
	 * @param $menuList menu list
1156
	 * @param $menuItemSrl current menu item serial number
1157
	 * @param $originMenu find result menu
1158
	 * @return void
1159
	 */
1160
	private function _searchMenu(&$menuList, $menuItemSrl, &$originMenu)
1161
	{
1162
		if(array_key_exists($menuItemSrl, $menuList))
1163
		{
1164
			$originMenu = $menuList[$menuItemSrl];
1165
			return;
1166
		}
1167
1168
		foreach($menuList AS $key=>$value)
1169
		{
1170
			if(count($value['list']) > 0)
1171
			{
1172
				$this->_searchMenu($value['list'], $menuItemSrl, $originMenu);
1173
			}
1174
		}
1175
	}
1176
1177
	private function _copyMenu($menuSrl, $parentSrl, &$originMenu)
1178
	{
1179
		$oMenuAdminModel = getAdminModel('menu');
1180
		$menuItemInfo = $oMenuAdminModel->getMenuItemInfo($originMenu['node_srl']);
1181
1182
		// default argument setting
1183
		$args = new stdClass();
1184
		$args->menu_srl = $menuSrl;
1185
		if($parentSrl == 0) $args->parent_srl = $menuSrl;
1186
		else $args->parent_srl = $parentSrl;
1187
		$args->menu_name_key = $originMenu['text'];
1188
		$args->menu_name = $originMenu['text'];
1189
		$args->menu_open_window = $originMenu['open_window'];
1190
		$args->menu_expand = $originMenu['expand'];
1191
		$args->normal_btn = $menuItemInfo->normal_btn;
1192
		$args->hover_btn = $menuItemInfo->hover_btn;
1193
		$args->active_btn = $menuItemInfo->active_btn;
1194
		$args->is_shortcut = $menuItemInfo->is_shortcut;
1195
1196
		$isModuleCopySuccess = false;
1197
		// if menu have a reference of module instance
1198
		if($menuItemInfo->is_shortcut == 'N' && strncasecmp('http', $originMenu['url'], 4) !== 0 )
1199
		{
1200
			$oModuleModel = getModel('module');
1201
			$moduleInfo = $oModuleModel->getModuleInfoByMid($originMenu['url']);
1202
1203
			$args->module_type = $moduleInfo->module;
1204
			$randomMid = $this->_makeRandomMid();
1205
			$args->module_id = $moduleInfo->module.'_'.$randomMid;
1206
			$args->layout_srl = $moduleInfo->layout_srl;
1207
1208
			$oModuleAdminController = getAdminController('module');
1209
			$copyArg = new stdClass();
1210
			$copyArg->module_srl = $moduleInfo->module_srl;
1211
			$copyArg->mid_1 = $args->module_id;
1212
			$copyArg->browser_title_1 = $moduleInfo->browser_title;
1213
			$copyArg->isMenuCreate = FALSE;
1214
			$copiedModuleSrl = $oModuleAdminController->procModuleAdminCopyModule($copyArg);
1215
1216
			$args->module_srl = $copiedModuleSrl;
1217
1218
			if($copiedModuleSrl)
1219
			{
1220
				$isModuleCopySuccess = true;
1221
			}
1222
		}
1223
		// if menu type is shortcut
1224
		else if($menuItemInfo->is_shortcut == 'Y')
1225
		{
1226
			$args->shortcut_target = $originMenu['url'];
1227
			$isModuleCopySuccess = true;
1228
		}
1229
1230
		if($isModuleCopySuccess)
1231
		{
1232
			// if have a group permission
1233
			if($menuItemInfo->group_srls)
1234
			{
1235
				$args->group_srls = $menuItemInfo->group_srls;
1236
			}
1237
1238
			// menu copy
1239
			$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...
1240
			/*if($output && !$output->toBool())
1241
			{
1242
			return $output;
1243
			}*/
1244
1245
			// if have a button, copy a button image also
1246
			$insertedMenuItemSrl = $this->get('menu_item_srl');
1247
			if($menuItemInfo->normal_btn || $menuItemInfo->hover_btn || $menuItemInfo->active_btn)
1248
			{
1249
				// copy & upate
1250
				$update_item_info = $oMenuAdminModel->getMenuItemInfo($insertedMenuItemSrl);
1251
				$copied_info = $this->_copyButton($insertedMenuItemSrl,$update_item_info->menu_srl, $menuItemInfo);
1252
				if(count($update_item_info->group_srls) == 0)
1253
				{
1254
					unset($update_item_info->group_srls);
1255
				}
1256
				$update_item_info->normal_btn = $copied_info['normal_btn'];
1257
				$update_item_info->hover_btn = $copied_info['hover_btn'];
1258
				$update_item_info->active_btn = $copied_info['active_btn'];
1259
				$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...
1260
			}
1261
			$this->insertedMenuItemSrlList[] = $insertedMenuItemSrl;
1262
		}
1263
1264
		// if have a child menu, copy child menu also
1265
		$childMenu = array_shift($originMenu['list']);
1266
		if(count($childMenu) > 0)
1267
		{
1268
			$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...
1269
		}
1270
	}
1271
1272
	private function _makeRandomMid()
1273
	{
1274
		$time = $_SERVER['REQUEST_TIME'];
1275
		$randomString = "";
1276
		for($i=0;$i<4;$i++)
1277
		{
1278
			$case = rand(0, 1);
1279
			if($case) $doc = rand(65, 90);
1280
			else $doc = rand(97, 122);
1281
1282
			$randomString .= chr($doc);
1283
		}
1284
1285
		return $randomString.substr($time, -2);
1286
	}
1287
1288
	/**
1289
	 * Arrange menu items
1290
	 * @return void|object
1291
	 */
1292
	function procMenuAdminArrangeItem()
1293
	{
1294
		$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...
1295
		$args = new stdClass();
1296
		$args->title = Context::get('title');
1297
		$parentKeyList = Context::get('parent_key');
1298
		$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...
1299
1300
		// menu name update
1301
		$args->menu_srl = $this->menuSrl;
1302
		$output = executeQuery('menu.updateMenu', $args);
1303
		if(!$output->toBool()) return $output;
1304
1305
		$this->map = array();
1306
		if(is_array($parentKeyList))
1307
		{
1308
			foreach($parentKeyList as $no=>$srl)
1309
			{
1310
				if($srl === 0) continue;
1311
				if(!is_array($this->map[$srl]))$this->map[$srl] = array();
1312
				$this->map[$srl][] = $no;
1313
			}
1314
		}
1315
1316
		$result = array();
1317
		if(is_array($this->itemKeyList))
1318
		{
1319
			foreach($this->itemKeyList as $srl)
1320
			{
1321
				if(!$this->checked[$srl])
1322
				{
1323
					$target = new stdClass();
1324
					$this->checked[$srl] = 1;
1325
					$target->node = $srl;
1326
					$target->child= array();
1327
1328
					while(count($this->map[$srl]))
1329
					{
1330
						$this->_setParent($srl, array_shift($this->map[$srl]), $target);
1331
					}
1332
					$result[] = $target;
1333
				}
1334
			}
1335
		}
1336
1337
		if(is_array($result))
1338
		{
1339
			$i = 0;
1340
			foreach($result AS $key=>$node)
1341
			{
1342
				$this->moveMenuItem($this->menuSrl, 0, $i, $node->node, 'move');	//move parent node
1343
				$this->_recursiveMoveMenuItem($node);	//move child node
1344
				$i = $node->node;
1345
			}
1346
		}
1347
1348
		$this->setMessage('success_updated', 'info');
1349
1350
		$returnUrl = Context::get('success_return_url') ? Context::get('success_return_url') : getNotEncodedUrl('', 'module', 'admin', 'act', 'dispMenuAdminManagement', 'menu_srl', $args->menu_srl);
1351
		$this->setRedirectUrl($returnUrl);
1352
	}
1353
1354
	/**
1355
	 * Set parent number to child
1356
	 * @param int $parent_srl
1357
	 * @param int $child_index
1358
	 * @param object $target
1359
	 * @return void
1360
	 */
1361
	function _setParent($parent_srl, $child_index, &$target)
1362
	{
1363
		$child_srl = $this->itemKeyList[$child_index];
1364
		$this->checked[$child_srl] = 1;
1365
1366
		$child_node = new stdClass();
1367
		$child_node->node = $child_srl;
1368
		$child_node->parent_node = $parent_srl;
1369
		$child_node->child = array();
1370
		$target->child[] = $child_node;
1371
1372
		while(count($this->map[$child_srl]))
1373
		{
1374
			$this->_setParent($child_srl, array_shift($this->map[$child_srl]), $child_node);
1375
		}
1376
		//return $target;
1377
	}
1378
1379
	/**
1380
	 * move item with sub directory(recursive)
1381
	 * @param object $result
1382
	 * @return void
1383
	 */
1384
	function _recursiveMoveMenuItem($result)
1385
	{
1386
		$i = 0;
1387
		while(count($result->child))
1388
		{
1389
			unset($node);
1390
			$node = array_shift($result->child);
1391
1392
			$this->moveMenuItem($this->menuSrl, $node->parent_node, $i, $node->node, 'move');
1393
			$this->_recursiveMoveMenuItem($node);
1394
			$i = $node->node;
1395
		}
1396
	}
1397
1398
	/**
1399
	 * move menu item
1400
	 * @param int $menu_srl
1401
	 * @param int $parent_srl
1402
	 * @param int $source_srl
1403
	 * @param int $target_srl
1404
	 * @param string $mode 'move' or 'insert'
1405
	 * @return void
1406
	 */
1407
	function moveMenuItem($menu_srl, $parent_srl, $source_srl, $target_srl, $mode, $isShortcut='Y', $url=NULL)
1408
	{
1409
		// Get the original menus
1410
		$oMenuAdminModel = getAdminModel('menu');
1411
1412
		$target_item = $oMenuAdminModel->getMenuItemInfo($target_srl);
1413
		if($target_item->menu_item_srl != $target_srl) return new Object(-1,'msg_invalid_request');
1414
		// Move the menu location(change the order menu appears)
1415
		if($mode == 'move')
1416
		{
1417
			$args = new stdClass();
1418
			$args->parent_srl = $parent_srl;
1419
			$args->menu_srl = $menu_srl;
1420
1421
			if($source_srl)
1422
			{
1423
				$source_item = $oMenuAdminModel->getMenuItemInfo($source_srl);
1424
				if($source_item->menu_item_srl != $source_srl) return new Object(-1,'msg_invalid_request');
1425
				$args->listorder = $source_item->listorder-1;
1426
			}
1427
			else
1428
			{
1429
				$output = executeQuery('menu.getMaxListorder', $args);
1430
				if(!$output->toBool()) return $output;
1431
				$args->listorder = (int)$output->data->listorder;
1432
				if(!$args->listorder) $args->listorder= 0;
1433
			}
1434
			$args->parent_srl = $parent_srl;
1435
			$output = executeQuery('menu.updateMenuItemListorder', $args);
1436
			if(!$output->toBool()) return $output;
1437
1438
			$args->parent_srl = $parent_srl;
1439
			$args->menu_item_srl = $target_srl;
1440
			$output = executeQuery('menu.updateMenuItemNode', $args);
1441
			if(!$output->toBool()) return $output;
1442
1443
			//module's menu_srl move also
1444
			if($isShortcut == 'N' && !empty($url))
1445
			{
1446
				$oModuleModel = getModel('module');
1447
				$moduleInfo = $oModuleModel->getModuleInfoByMid($url);
1448 View Code Duplication
				if($menu_srl != $moduleInfo->menu_srl)
1449
				{
1450
					$moduleInfo->menu_srl = $menu_srl;
1451
					$oModuleController = getController('module');
1452
					$output = $oModuleController->updateModule($moduleInfo);
1453
				}
1454
1455
				// change home menu cache file
1456
				if($url == $this->homeModuleMid)
1457
				{
1458
					if(file_exists($this->homeMenuCacheFile))
1459
					{
1460
						include($this->homeMenuCacheFile);
1461
					}
1462
					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...
1463
					{
1464
						$this->makeHomemenuCacheFile($menu_srl);
1465
					}
1466
				}
1467
			}
1468
			// Add a child
1469
		}
1470
		elseif($mode == 'insert')
1471
		{
1472
			$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...
1473
			$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...
1474
			$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...
1475
			$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...
1476
			if(!$output->toBool()) return $output;
1477
		}
1478
1479
		$xml_file = $this->makeXmlFile($menu_srl);
1480
		return $xml_file;
1481
	}
1482
1483
	/**
1484
	 * Update xml file
1485
	 * XML file is not often generated after setting menus on the admin page\n
1486
	 * For this occasional cases, manually update was implemented. \n
1487
	 * It looks unnecessary at this moment however no need to eliminate the feature. Just leave it.
1488
	 * @return void
1489
	 */
1490 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...
1491
	{
1492
		// Check input value
1493
		$menu_srl = Context::get('menu_srl');
1494
		// Get information of the menu
1495
		$oMenuAdminModel = getAdminModel('menu');
1496
		$menu_info = $oMenuAdminModel->getMenu($menu_srl);
1497
		$menu_title = $menu_info->title;
1498
		// Re-generate the xml file
1499
		$xml_file = $this->makeXmlFile($menu_srl);
1500
		// Set return value
1501
		$this->add('menu_title',$menu_title);
1502
		$this->add('xml_file',$xml_file);
1503
	}
1504
1505
	/**
1506
	 * Register a menu image button
1507
	 * @return void
1508
	 */
1509
	function procMenuAdminUploadButton()
1510
	{
1511
		$menu_srl = Context::get('menu_srl');
1512
		$menu_item_srl = Context::get('menu_item_srl');
1513
		$target = Context::get('target');
1514
		$target_file = Context::get($target);
1515
		// Error occurs when the target is neither a uploaded file nor a valid file
1516
		if(!$menu_srl || !$menu_item_srl)
1517
		{
1518
			Context::set('error_messge', Context::getLang('msg_invalid_request'));
1519
1520
		}
1521
		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']))
1522
		{
1523
			Context::set('error_messge', Context::getLang('msg_invalid_request'));
1524
		}
1525
1526
		// Move the file to a specific director if the uploaded file meets requirement
1527
		else
1528
		{
1529
			$tmp_arr = explode('.',$target_file['name']);
1530
			$ext = $tmp_arr[count($tmp_arr)-1];
1531
1532
			$path = sprintf('./files/attach/menu_button/%d/', $menu_srl);
1533
			$filename = sprintf('%s%d.%s.%s', $path, $menu_item_srl, $target, $ext);
1534
1535
			if(!is_dir($path)) FileHandler::makeDir($path);
1536
1537
			move_uploaded_file($target_file['tmp_name'], $filename);
1538
			Context::set('filename', $filename);
1539
		}
1540
1541
		$this->setTemplatePath($this->module_path.'tpl');
0 ignored issues
show
Bug introduced by
The property module_path cannot be accessed from this context as it is declared private in class ModuleObject.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
1542
		$this->setTemplateFile('menu_file_uploaded');
1543
	}
1544
1545
	/**
1546
	 * Remove the menu image button
1547
	 * @return void
1548
	 */
1549
	function procMenuAdminDeleteButton()
1550
	{
1551
		$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...
1552
		$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...
1553
		$target = Context::get('target');
1554
		$filename = Context::get('filename');
1555
		FileHandler::removeFile($filename);
1556
1557
		$this->add('target', $target);
1558
	}
1559
1560
	/**
1561
	 * Get all act list for admin menu
1562
	 * @return void
1563
	 */
1564
	function procMenuAdminAllActList()
1565
	{
1566
		$oModuleModel = getModel('module');
1567
		$installed_module_list = $oModuleModel->getModulesXmlInfo();
1568
		if(is_array($installed_module_list))
1569
		{
1570
			$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...
1571
			$menuList = array();
1572
			foreach($installed_module_list AS $key=>$value)
1573
			{
1574
				$info = $oModuleModel->getModuleActionXml($value->module);
1575
				if($info->menu) $menuList[$value->module] = $info->menu;
1576
				unset($info->menu);
1577
			}
1578
		}
1579
		$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...
1580
	}
1581
1582
	/**
1583
	 * Get all act list for admin menu
1584
	 * @return void|object
1585
	 */
1586
	function procMenuAdminInsertItemForAdminMenu()
1587
	{
1588
		$requestArgs = Context::getRequestVars();
1589
		$tmpMenuName = explode(':', $requestArgs->menu_name);
1590
		$moduleName = $tmpMenuName[0];
1591
		$menuName = $tmpMenuName[1];
1592
1593
		// variable setting
1594
		$logged_info = Context::get('logged_info');
1595
		//$oMenuAdminModel = getAdminModel('menu');
1596
		$oMemberModel = getModel('member');
1597
1598
		//$parentMenuInfo = $oMenuAdminModel->getMenuItemInfo($requestArgs->parent_srl);
1599
		$groupSrlList = $oMemberModel->getMemberGroups($logged_info->member_srl);
1600
1601
		//preg_match('/\{\$lang->menu_gnb\[(.*?)\]\}/i', $parentMenuInfo->name, $m);
1602
		$oModuleModel = getModel('module');
1603
		//$info = $oModuleModel->getModuleInfoXml($moduleName);
1604
		$info = $oModuleModel->getModuleActionXml($moduleName);
1605
1606
		$url = getNotEncodedFullUrl('', 'module', 'admin', 'act', $info->menu->{$menuName}->index);
1607
		if(empty($url)) $url = getNotEncodedFullUrl('', 'module', 'admin', 'act', $info->admin_index_act);
1608
		if(empty($url)) $url = getNotEncodedFullUrl('', 'module', 'admin');
1609
		$dbInfo = Context::getDBInfo();
1610
1611
		$args = new stdClass();
1612
		$args->menu_item_srl = (!$requestArgs->menu_item_srl) ? getNextSequence() : $requestArgs->menu_item_srl;
1613
		$args->parent_srl = $requestArgs->parent_srl;
1614
		$args->menu_srl = $requestArgs->menu_srl;
1615
		$args->name = sprintf('{$lang->menu_gnb_sub[\'%s\']}', $menuName);
1616
		//if now page is https...
1617 View Code Duplication
		if(strpos($url, 'https') !== false)
1618
		{
1619
			$args->url = str_replace('https'.substr($dbInfo->default_url, 4), '', $url);
1620
		}
1621
		else
1622
		{
1623
			$args->url = str_replace($dbInfo->default_url, '', $url);
1624
		}
1625
		$args->open_window = 'N';
1626
		$args->expand = 'N';
1627
		$args->normal_btn = '';
1628
		$args->hover_btn = '';
1629
		$args->active_btn = '';
1630
		$args->group_srls = implode(',', array_keys($groupSrlList));
1631
		$args->listorder = -1*$args->menu_item_srl;
1632
1633
		// Check if already exists
1634
		$oMenuModel = getAdminModel('menu');
1635
		$item_info = $oMenuModel->getMenuItemInfo($args->menu_item_srl);
1636
		// Update if exists
1637
		if($item_info->menu_item_srl == $args->menu_item_srl)
1638
		{
1639
			$output = $this->_updateMenuItem($args);
1640
			if(!$output->toBool()) return $output;
1641
		}
1642
		// Insert if not exist
1643
		else
1644
		{
1645
			$args->listorder = -1*$args->menu_item_srl;
1646
			$output = executeQuery('menu.insertMenuItem', $args);
1647
			if(!$output->toBool()) return $output;
1648
		}
1649
		// Get information of the menu
1650
		$menu_info = $oMenuModel->getMenu($args->menu_srl);
1651
		$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...
1652
		// Update the xml file and get its location
1653
		$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...
1654
1655
		$returnUrl = Context::get('success_return_url') ? Context::get('success_return_url') : getNotEncodedUrl('', 'module', 'admin', 'act', 'dispAdminSetup');
1656
		$this->setRedirectUrl($returnUrl);
1657
	}
1658
1659
	/**
1660
	 * Update menu auth (Exposure and Access)
1661
	 * @return void
1662
	 */
1663
	public function procMenuAdminUpdateAuth()
1664
	{
1665
		$menuItemSrl = Context::get('menu_item_srl');
1666
		$exposure = Context::get('exposure');
1667
		$htPerm = Context::get('htPerm');
1668
1669
		$oMenuModel = getAdminModel('menu');
1670
		$itemInfo = $oMenuModel->getMenuItemInfo($menuItemSrl);
1671
		$args = $itemInfo;
1672
1673
		// Menu Exposure update
1674
		// if exposure target is only login user...
1675
		if(!$exposure)
1676
		{
1677
			$args->group_srls = '';
1678
		}
1679
		else
1680
		{
1681
			$exposure = explode(',', $exposure);
1682
			if(in_array($exposure, array('-1','-3')))
1683
			{
1684
				$args->group_srls = $exposure;
1685
			}
1686
1687
			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...
1688
		}
1689
1690
		$output = $this->_updateMenuItem($args);
1691
		if(!$output->toBool())
1692
		{
1693
			return $output;
1694
		}
1695
1696
		// Module Access update
1697
		unset($args);
1698
		$oMenuAdminModel = getAdminModel('menu');
1699
		$menuInfo = $oMenuAdminModel->getMenu($itemInfo->menu_srl);
1700
1701
		$oModuleModel = getModel('module');
1702
		$moduleInfo = $oModuleModel->getModuleInfoByMid($itemInfo->url, $menuInfo->site_srl);
1703
1704
		$xml_info = $oModuleModel->getModuleActionXML($moduleInfo->module);
1705
1706
		$grantList = $xml_info->grant;
1707
		if(!$grantList) $grantList = new stdClass;
1708
1709
		$grantList->access = new stdClass();
1710
		$grantList->access->default = 'guest';
1711
		$grantList->manager = new stdClass();
1712
		$grantList->manager->default = 'manager';
1713
1714
		$grant = new stdClass;
1715
		foreach($grantList AS $grantName=>$grantInfo)
1716
		{
1717
			if(!$htPerm[$grantName])
1718
			{
1719
				continue;
1720
			}
1721
1722
			$htPerm[$grantName] = explode(',', $htPerm[$grantName]);
1723
1724
			// users in a particular group
1725
			if(is_array($htPerm[$grantName]))
1726
			{
1727
				$grant->{$grantName} = $htPerm[$grantName];
1728
				continue;
1729
			}
1730
			// -1 = Log-in user only, -2 = site members only, 0 = all users
1731
			else
1732
			{
1733
				$grant->{$grantName}[] = $htPerm[$grantName];
1734
				continue;
1735
			}
1736
			$grant->{$group_srls} = array();
0 ignored issues
show
Unused Code introduced by
$grant->{$group_srls} = array(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
1737
		}
1738
1739
		if(count($grant))
1740
		{
1741
			$oModuleController = getController('module');
1742
			$oModuleController->insertModuleGrants($moduleInfo->module_srl, $grant);
1743
		}
1744
1745
		// recreate menu cache file
1746
		$this->makeXmlFile($itemInfo->menu_srl);
1747
	}
1748
1749
	/**
1750
	 * Generate XML file for menu and return its location
1751
	 * @param int $menu_srl
1752
	 * @return string
1753
	 */
1754
	function makeXmlFile($menu_srl)
1755
	{
1756
		// Return if there is no information when creating the xml file
1757
		if(!$menu_srl) return;
1758
		// Get menu informaton
1759
		$args = new stdClass();
1760
		$args->menu_srl = $menu_srl;
1761
		$output = executeQuery('menu.getMenu', $args);
1762
		if(!$output->toBool() || !$output->data) return $output;
1763
		$site_srl = (int)$output->data->site_srl;
1764
1765
		if($site_srl)
1766
		{
1767
			$oModuleModel = getModel('module');
1768
			$columnList = array('sites.domain');
1769
			$site_info = $oModuleModel->getSiteInfo($site_srl, $columnList);
1770
			$domain = $site_info->domain;
1771
		}
1772
		// Get a list of menu items corresponding to menu_srl by listorder
1773
		$args->menu_srl = $menu_srl;
1774
		$args->sort_index = 'listorder';
1775
		$output = executeQuery('menu.getMenuItems', $args);
1776
		if(!$output->toBool()) return;
1777
		// Specify the name of the cache file
1778
		$xml_file = sprintf(_XE_PATH_ . "files/cache/menu/%s.xml.php", $menu_srl);
1779
		$php_file = sprintf(_XE_PATH_ . "files/cache/menu/%s.php", $menu_srl);
1780
		// If no data found, generate an XML file without node data
1781
		$list = $output->data;
1782 View Code Duplication
		if(!$list)
1783
		{
1784
			$xml_buff = "<root />";
1785
			FileHandler::writeFile($xml_file, $xml_buff);
1786
			FileHandler::writeFile($php_file, '<?php if(!defined("__XE__")) exit(); ?>');
1787
			return $xml_file;
1788
		}
1789
		// Change to an array if only a single data is obtained
1790
		if(!is_array($list)) $list = array($list);
1791
		// Create a tree for loop
1792
		$list_count = count($list);
1793
		for($i=0;$i<$list_count;$i++)
1794
		{
1795
			$node = $list[$i];
1796
			$menu_item_srl = $node->menu_item_srl;
1797
			$parent_srl = $node->parent_srl;
1798
1799
			$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...
1800
		}
1801
		// A common header to set permissions of the cache file and groups
1802
		$header_script =
1803
			'$lang_type = Context::getLangType(); '.
1804
			'$is_logged = Context::get(\'is_logged\'); '.
1805
			'$logged_info = Context::get(\'logged_info\'); '.
1806
			'$site_srl = '.$site_srl.';'.
1807
			'$site_admin = false;'.
1808
			'if($site_srl) { '.
1809
			'$oModuleModel = getModel(\'module\');'.
1810
			'$site_module_info = $oModuleModel->getSiteInfo($site_srl); '.
1811
			'if($site_module_info) Context::set(\'site_module_info\',$site_module_info);'.
1812
			'else $site_module_info = Context::get(\'site_module_info\');'.
1813
			'$grant = $oModuleModel->getGrant($site_module_info, $logged_info); '.
1814
			'if($grant->manager ==1) $site_admin = true;'.
1815
			'}'.
1816
			'if($is_logged) {'.
1817
			'if($logged_info->is_admin=="Y") $is_admin = true; '.
1818
			'else $is_admin = false; '.
1819
			'$group_srls = array_keys($logged_info->group_list); '.
1820
			'} else { '.
1821
			'$is_admin = false; '.
1822
			'$group_srls = array(); '.
1823
			'}';
1824
		// Create the xml cache file (a separate session is needed for xml cache)
1825
		$xml_buff = sprintf(
1826
			'<?php '.
1827
			'define(\'__XE__\', true); '.
1828
			'require_once(\''.FileHandler::getRealPath('./config/config.inc.php').'\'); '.
1829
			'$oContext = Context::getInstance(); '.
1830
			'$oContext->init(); '.
1831
			'header("Content-Type: text/xml; charset=UTF-8"); '.
1832
			'header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); '.
1833
			'header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); '.
1834
			'header("Cache-Control: no-store, no-cache, must-revalidate"); '.
1835
			'header("Cache-Control: post-check=0, pre-check=0", false); '.
1836
			'header("Pragma: no-cache"); '.
1837
			'%s '.
1838
			'$oContext->close(); '.
1839
			'?>'.
1840
			'<root>%s</root>',
1841
			$header_script,
1842
			$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...
1843
		);
1844
		// Create php cache file
1845
		$php_output = $this->getPhpCacheCode($tree[0], $tree, $site_srl, $domain);
1846
		$php_buff = sprintf(
1847
			'<?php '.
1848
			'if(!defined("__XE__")) exit(); '.
1849
			'$menu = new stdClass();' .
1850
			'%s; '.
1851
			'%s; '.
1852
			'$menu->list = array(%s); '.
1853
			'if(!$is_admin) { recurciveExposureCheck($menu->list); }'.
1854
			'Context::set("included_menu", $menu); '.
1855
			'?>',
1856
			$header_script,
1857
			$php_output['name'],
1858
			$php_output['buff']
1859
		);
1860
		// Save File
1861
		FileHandler::writeFile($xml_file, $xml_buff);
1862
		FileHandler::writeFile($php_file, $php_buff);
1863
		return $xml_file;
1864
	}
1865
1866
	/**
1867
	 * Create xml data recursively looping for array nodes by referencing to parent_srl
1868
	 * menu xml file uses a tag named "node" and this XML configures menus on admin page.
1869
	 * (Implement tree menu by reading the xml file in tree_menu.js)
1870
	 * @param array $source_node
1871
	 * @param array $tree
1872
	 * @param int $site_srl
1873
	 * @param string $domain
1874
	 * @return string
1875
	 */
1876
	function getXmlTree($source_node, $tree, $site_srl, $domain)
1877
	{
1878
		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...
1879
1880
		$oMenuAdminModel = getAdminModel('menu');
1881
1882
		foreach($source_node as $menu_item_srl => $node)
1883
		{
1884
			$child_buff = "";
1885
			// Get data of the child nodes
1886
			if($menu_item_srl&&$tree[$menu_item_srl]) $child_buff = $this->getXmlTree($tree[$menu_item_srl], $tree, $site_srl, $domain);
1887
			// List variables
1888
			$names = $oMenuAdminModel->getMenuItemNames($node->name, $site_srl);
1889 View Code Duplication
			foreach($names as $key => $val)
1890
			{
1891
				$name_arr_str .= sprintf('"%s"=>\'%s\',',$key, str_replace(array('\\', '\''), array('\\\\', '\\\''), $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...
1892
			}
1893
			$name_str = sprintf('$_names = array(%s); print $_names[$lang_type];', $name_arr_str);
1894
1895
			$url = str_replace(array('&','"','<','>'),array('&amp;','&quot;','&lt;','&gt;'),$node->url);
1896
			$desc = str_replace(array('&','"',"'"),array('&amp;','&quot;','\\\''),$node->desc);
1897 View Code Duplication
			if(preg_match('/^([0-9a-zA-Z\_\-]+)$/', $node->url))
1898
			{
1899
				$href = "getSiteUrl('$domain', '','mid','$node->url')";
1900
			}
1901
			else $href = sprintf('"%s"', $url);
1902
			$is_shortcut = $node->is_shortcut;
1903
			$open_window = $node->open_window;
1904
			$expand = $node->expand;
1905
1906
			$normal_btn = $node->normal_btn;
1907 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);
1908
			else $normal_btn = '';
1909
			$hover_btn = $node->hover_btn;
1910 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);
1911
			else $hover_btn = '';
1912
			$active_btn = $node->active_btn;
1913 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);
1914
			else $active_btn = '';
1915
1916
			$group_srls = $node->group_srls;
1917
1918
			if($normal_btn)
1919
			{
1920
				if($hover_btn) $hover_str = sprintf('onmouseover=&quot;this.src=\'%s\'&quot;', $hover_btn); else $hover_str = '';
1921
				if($active_btn) $active_str = sprintf('onmousedown=&quot;this.src=\'%s\'&quot;', $active_btn); else $active_str = '';
1922
				$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);
1923
			}
1924
			else
1925
			{
1926
				$link = '<?php print $_names[$lang_type]; ?>';
1927
			}
1928
			// If the value of node->group_srls exists
1929 View Code Duplication
			if($group_srls)$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);
1930
			else $group_check_code = "true";
1931
			$attribute = sprintf(
1932
				'node_srl="%s" parent_srl="%s" 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 }?>"',
1933
				$menu_item_srl,
1934
				$node->parent_srl,
1935
				addslashes($node->name),
1936
				$group_check_code,
1937
				$name_str,
1938
				$group_check_code,
1939
				$url,
1940
				$group_check_code,
1941
				$href,
1942
				$is_shortcut,
1943
				$desc,
1944
				$open_window,
1945
				$expand,
1946
				$normal_btn,
1947
				$hover_btn,
1948
				$active_btn,
1949
				$group_check_code,
1950
				$link
1951
			);
1952
1953 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...
1954
			else $buff .=  sprintf('<node %s />', $attribute);
1955
		}
1956
		return $buff;
1957
	}
1958
1959
	/**
1960
	 * Return php code converted from nodes in an array
1961
	 * Although xml data can be used for tpl, menu to menu, it needs to use javascript separately
1962
	 * By creating cache file in php and then you can get menu information without DB
1963
	 * This cache includes in ModuleHandler::displayContent() and then Context::set()
1964
	 * @param array $source_node
1965
	 * @param array $tree
1966
	 * @param int $site_srl
1967
	 * @param string $domain
1968
	 * @return array
1969
	 */
1970
	function getPhpCacheCode($source_node, $tree, $site_srl, $domain)
1971
	{
1972
		$output = array("buff"=>"", "url_list"=>array());
1973
		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...
1974
1975
		$oMenuAdminModel = getAdminModel('menu');
1976
1977
		foreach($source_node as $menu_item_srl => $node)
1978
		{
1979
			// Get data from child nodes if exist.
1980
			if($menu_item_srl&&$tree[$menu_item_srl]) $child_output = $this->getPhpCacheCode($tree[$menu_item_srl], $tree, $site_srl, $domain);
1981
			else $child_output = array("buff"=>"", "url_list"=>array());
1982
			// List variables
1983
			$names = $oMenuAdminModel->getMenuItemNames($node->name, $site_srl);
1984
			unset($name_arr_str);
1985 View Code Duplication
			foreach($names as $key => $val)
1986
			{
1987
				$name_arr_str .= sprintf('"%s"=>"%s",',$key, str_replace(array('\\','"'),array('\\\\','&quot;'),$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...
1988
			}
1989
			$name_str = sprintf('$_menu_names[%d] = array(%s); %s', $node->menu_item_srl, $name_arr_str, $child_output['name']);
1990
			// If url value is not empty in the current node, put the value into an array url_list
1991
			if($node->url) $child_output['url_list'][] = $node->url;
1992
			$output['url_list'] = array_merge($output['url_list'], $child_output['url_list']);
1993
			// If node->group_srls value exists
1994 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);
1995
			else $group_check_code = "true";
1996
			// List variables
1997
			$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...
1998
			$url = str_replace(array('&','"','<','>'),array('&amp;','&quot;','&lt;','&gt;'),$node->url);
1999
			$desc = str_replace(array('&','"',"'"),array('&amp;','&quot;','\\\''),$node->desc);
2000 View Code Duplication
			if(preg_match('/^([0-9a-zA-Z\_\-]+)$/i', $node->url))
2001
			{
2002
				$href = "getSiteUrl('$domain', '','mid','$node->url')";
2003
			}
2004
			else $href = sprintf('"%s"', $url);
2005
			$is_shortcut = $node->is_shortcut;
2006
			$open_window = $node->open_window;
2007
			$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...
2008
			$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...
2009
			$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...
2010
2011
			foreach($child_output['url_list'] as $key =>$val)
2012
			{
2013
				$child_output['url_list'][$key] = addslashes($val);
2014
			}
2015
2016
			$selected = '"'.implode('","',$child_output['url_list']).'"';
2017
			$child_buff = $child_output['buff'];
2018
			$expand = $node->expand;
2019
2020
			$normal_btn = $node->normal_btn;
2021 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);
2022
			else $normal_btn = '';
2023
2024
			$hover_btn = $node->hover_btn;
2025 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);
2026
			else $hover_btn = '';
2027
2028
			$active_btn = $node->active_btn;
2029 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);
2030
			else $active_btn = '';
2031
2032
2033
			$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...
2034
2035
			if($normal_btn)
2036
			{
2037
				if($hover_btn) $hover_str = sprintf('onmouseover=\"this.src=\'%s\'\"', $hover_btn); else $hover_str = '';
2038
				if($active_btn) $active_str = sprintf('onmousedown=\"this.src=\'%s\'\"', $active_btn); else $active_str = '';
2039
				$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);
2040
				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);
2041
				else $link_active = $link;
2042
			}
2043
			else
2044
			{
2045
				$link_active = $link = sprintf('$_menu_names[%d][$lang_type]', $node->menu_item_srl);
2046
			}
2047
			// Create properties (check if it belongs to the menu node by url_list. It looks a trick but fast and powerful)
2048
			$attribute = sprintf(
2049
				'"node_srl"=>"%s","parent_srl"=>"%s","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):""),',
2050
				$node->menu_item_srl,
2051
				$node->parent_srl,
2052
				addslashes($node->name),
2053
				$group_check_code,
2054
				$group_check_code,
2055
				$node->menu_item_srl,
2056
				$group_check_code,
2057
				$href,
2058
				$group_check_code,
2059
				$url,
2060
				$is_shortcut,
2061
				$desc,
2062
				$open_window,
2063
				$normal_btn,
2064
				$hover_btn,
2065
				$active_btn,
2066
				$selected,
2067
				$selected,
2068
				$expand,
2069
				$child_buff,
2070
				$group_check_code,
2071
				$selected,
2072
				$selected,
2073
				$link_active,
2074
				$link
2075
			);
2076
2077
			// Generate buff data
2078
			$output['buff'] .=  sprintf('%s=>array(%s),', $node->menu_item_srl, $attribute);
2079
			$output['name'] .= $name_str;
2080
		}
2081
		return $output;
2082
	}
2083
2084
	/**
2085
	 * Mapping menu and layout
2086
	 * When setting menu on the layout, map the default layout
2087
	 * @param int $layout_srl
2088
	 * @param array $menu_srl_list
2089
	 */
2090
	function updateMenuLayout($layout_srl, $menu_srl_list)
2091
	{
2092
		if(!count($menu_srl_list)) return;
2093
		// Delete the value of menu_srls
2094
		$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...
2095
		$output = executeQuery('menu.deleteMenuLayout', $args);
2096
		if(!$output->toBool()) return $output;
2097
2098
		$args->layout_srl = $layout_srl;
2099
		// Mapping menu_srls, layout_srl
2100
		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...
2101
		{
2102
			$args->menu_srl = $menu_srl_list[$i];
2103
			$output = executeQuery('menu.insertMenuLayout', $args);
2104
			if(!$output->toBool()) return $output;
2105
		}
2106
	}
2107
2108
	/**
2109
	 * Register a menu image button
2110
	 * @param object $args
2111
	 * @return array
2112
	 */
2113
	function _uploadButton($args)
2114
	{
2115
		// path setting
2116
		$path = sprintf('./files/attach/menu_button/%d/', $args->menu_srl);
2117
		if($args->menu_normal_btn || $args->menu_hover_btn || $args->menu_active_btn && !is_dir($path))
2118
		{
2119
			FileHandler::makeDir($path);
2120
		}
2121
2122
		if($args->isNormalDelete == 'Y' || $args->isHoverDelete == 'Y' || $args->isActiveDelete == 'Y')
2123
		{
2124
			$oMenuModel = getAdminModel('menu');
2125
			$itemInfo = $oMenuModel->getMenuItemInfo($args->menu_item_srl);
2126
2127
			if($args->isNormalDelete == 'Y' && $itemInfo->normal_btn) FileHandler::removeFile($itemInfo->normal_btn);
2128
			if($args->isHoverDelete == 'Y' && $itemInfo->hover_btn) FileHandler::removeFile($itemInfo->hover_btn);
2129
			if($args->isActiveDelete == 'Y' && $itemInfo->active_btn) FileHandler::removeFile($itemInfo->active_btn);
2130
		}
2131
2132
		$returnArray = array();
2133
		$date = date('YmdHis');
2134
		// normal button
2135 View Code Duplication
		if($args->menu_normal_btn)
2136
		{
2137
			$tmp_arr = explode('.',$args->menu_normal_btn['name']);
2138
			$ext = $tmp_arr[count($tmp_arr)-1];
2139
2140
			$filename = sprintf('%s%d.%s.%s.%s', $path, $args->menu_item_srl, $date, 'menu_normal_btn', $ext);
2141
2142
			if(checkUploadedFile($args->menu_normal_btn['tmp_name']))
2143
			{
2144
				move_uploaded_file ( $args->menu_normal_btn ['tmp_name'], $filename );
2145
				$returnArray ['normal_btn'] = $filename;
2146
			}
2147
		}
2148
2149
		// hover button
2150 View Code Duplication
		if($args->menu_hover_btn)
2151
		{
2152
			$tmp_arr = explode('.',$args->menu_hover_btn['name']);
2153
			$ext = $tmp_arr[count($tmp_arr)-1];
2154
2155
			$filename = sprintf('%s%d.%s.%s.%s', $path, $args->menu_item_srl, $date, 'menu_hover_btn', $ext);
2156
2157
			if(checkUploadedFile($args->menu_hover_btn['tmp_name']))
2158
			{
2159
				move_uploaded_file($args->menu_hover_btn['tmp_name'], $filename);
2160
				$returnArray['hover_btn'] = $filename;
2161
			}
2162
		}
2163
2164
		// active button
2165 View Code Duplication
		if($args->menu_active_btn)
2166
		{
2167
			$tmp_arr = explode('.',$args->menu_active_btn['name']);
2168
			$ext = $tmp_arr[count($tmp_arr)-1];
2169
2170
			$filename = sprintf('%s%d.%s.%s.%s', $path, $args->menu_item_srl, $date, 'menu_active_btn', $ext);
2171
2172
			if(checkUploadedFile($args->menu_active_btn['tmp_name']))
2173
			{
2174
				move_uploaded_file($args->menu_active_btn['tmp_name'], $filename);
2175
				$returnArray['active_btn'] = $filename;
2176
			}
2177
2178
		}
2179
		return $returnArray;
2180
	}
2181
2182
	/**
2183
	 * When copy a menu, button copied also.
2184
	 * @param $args menuItemInfo with button values
2185
	 */
2186
	private function _copyButton($insertedMenuItemSrl, $insertedMenuSrl, &$menuItemInfo)
2187
	{
2188
		$copied_info = array(
2189
			"normal_btn"=>"",
2190
			"hover_btn"=>"",
2191
			"active_btn"=>"",
2192
		);
2193
		//normal_btn
2194 View Code Duplication
		if($menuItemInfo->normal_btn)
2195
		{
2196
			$originFile = FileHandler::getRealPath($menuItemInfo->normal_btn);
2197
			$targetFile = $this->_changeMenuItemSrlInButtonPath($menuItemInfo->normal_btn, $insertedMenuSrl, $insertedMenuItemSrl, 'normal');
2198
2199
			FileHandler::copyFile($originFile, $targetFile);
2200
			$copied_info['normal_btn'] = $targetFile;
2201
		}
2202
2203
		//hover_btn
2204 View Code Duplication
		if($menuItemInfo->hover_btn)
2205
		{
2206
			$originFile = FileHandler::getRealPath($menuItemInfo->hover_btn);
2207
			$targetFile = $this->_changeMenuItemSrlInButtonPath($menuItemInfo->hover_btn, $insertedMenuSrl, $insertedMenuItemSrl, 'hover');
2208
2209
			FileHandler::copyFile($originFile, $targetFile);
2210
			$copied_info['hover_btn'] = $targetFile;
2211
		}
2212
2213
		//active_btn
2214 View Code Duplication
		if($menuItemInfo->active_btn)
2215
		{
2216
			$originFile = FileHandler::getRealPath($menuItemInfo->active_btn);
2217
			$targetFile = $this->_changeMenuItemSrlInButtonPath($menuItemInfo->active_btn, $insertedMenuSrl, $insertedMenuItemSrl, 'active');
2218
2219
			FileHandler::copyFile($originFile, $targetFile);
2220
			$copied_info['active_btn'] = $targetFile;
2221
		}
2222
		return $copied_info;
2223
	}
2224
2225
	private function _changeMenuItemSrlInButtonPath($buttonPath, $menuSrl, $menuItemSrl, $mode)
2226
	{
2227
		$path = sprintf('./files/attach/menu_button/%d/', $menuSrl);
2228
		$tmp_arr = explode('.', $buttonPath);
2229
		$ext = $tmp_arr[count($tmp_arr)-1];
2230
		$date = date("YmdHis");
2231
		return sprintf('%s%d.%s.%s.%s', $path, $menuItemSrl,$date,'menu_'.$mode.'_btn', $ext);
2232
	}
2233
2234
	public function makeHomemenuCacheFile($menuSrl)
2235
	{
2236
		$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...
2237
		$cacheBuff .= sprintf('$homeMenuSrl = %d;', $menuSrl);
2238
2239
		FileHandler::writeFile($this->homeMenuCacheFile, $cacheBuff);
2240
	}
2241
2242
	public function getHomeMenuCacheFile()
2243
	{
2244
		return $this->homeMenuCacheFile;
2245
	}
2246
}
2247
/* End of file menu.admin.controller.php */
2248
/* Location: ./modules/menu/menu.admin.controller.php */
2249