GitHub Access Token became invalid

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

getAutoInstallAdminInstallInfo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) NAVER <http://www.navercorp.com> */
3
4
/**
5
 * Model class of the autoinstall module
6
 * @author NAVER ([email protected])
7
 */
8
class autoinstallAdminModel extends autoinstall
9
{
10
11
	var $layout_category_srl = 18322954;
12
	var $mobile_layout_category_srl = 18994172;
13
	var $module_skin_category_srl = 18322943;
14
	var $module_mobile_skin_category_srl = 18994170;
15
16
	/**
17
	 * Pre process parameters
18
	 */
19
	function preProcParam(&$order_target, &$order_type, &$page)
20
	{
21
		$order_target_array = array('newest' => 1, 'download' => 1, 'popular' => 1);
22
		if(!isset($order_target_array[$order_target]))
23
		{
24
			$order_target = 'newest';
25
		}
26
27
		$order_type_array = array('asc' => 1, 'desc' => 1);
28
		if(!isset($order_type_array[$order_type]))
29
		{
30
			$order_type = 'desc';
31
		}
32
33
		$page = (int) $page;
34
		if($page < 1)
35
		{
36
			$page = 1;
37
		}
38
	}
39
40
	/**
41
	 * Return list of package that can have instance
42
	 */
43
	function getAutoinstallAdminMenuPackageList()
44
	{
45
		$search_keyword = Context::get('search_keyword');
46
		$order_target = Context::get('order_target');
47
		$order_type = Context::get('order_type');
48
		$page = Context::get('page');
49
50
		$this->preProcParam($order_target, $order_type, $page);
51
		$this->getPackageList('menu', $order_target, $order_type, $page, $search_keyword);
52
	}
53
54
	/**
55
	 * Return list of layout package
56
	 */
57
	function getAutoinstallAdminLayoutPackageList()
58
	{
59
		$search_keyword = Context::get('search_keyword');
60
		$order_target = Context::get('order_target');
61
		$order_type = Context::get('order_type');
62
		$page = Context::get('page');
63
64
		$type_array = array('M' => 1, 'P' => 1);
65
		$type = Context::get('type');
66
		if(!isset($type_array[$type]))
67
		{
68
			$type = 'P';
69
		}
70
71
		if($type == 'P')
72
		{
73
			$category_srl = $this->layout_category_srl;
74
		}
75
		else
76
		{
77
			$category_srl = $this->mobile_layout_category_srl;
78
		}
79
80
		$this->preProcParam($order_target, $order_type, $page);
81
		$this->getPackageList('layout', $order_target, $order_type, $page, $search_keyword, $category_srl);
82
	}
83
84
	/**
85
	 * Return list of module skin package
86
	 */
87
	function getAutoinstallAdminSkinPackageList()
88
	{
89
		Context::setRequestMethod('JSON');
90
		$search_keyword = Context::get('search_keyword');
91
		$order_target = Context::get('order_target');
92
		$order_type = Context::get('order_type');
93
		$page = Context::get('page');
94
		$parent_program = Context::get('parent_program');
95
96
		$type_array = array('M' => 1, 'P' => 1);
97
		$type = Context::get('type');
98
		if(!isset($type_array[$type]))
99
		{
100
			$type = 'P';
101
		}
102
103
		if($type == 'P')
104
		{
105
			$category_srl = $this->module_skin_category_srl;
106
		}
107
		else
108
		{
109
			$category_srl = $this->module_mobile_skin_category_srl;
110
		}
111
112
		$this->preProcParam($order_target, $order_type, $page);
113
		$this->getPackageList('skin', $order_target, $order_type, $page, $search_keyword, $category_srl, $parent_program);
114
	}
115
116
	/**
117
	 * Get Package List
118
	 */
119
	function getPackageList($type, $order_target = 'newest', $order_type = 'desc', $page = '1', $search_keyword = NULL, $category_srl = NULL, $parent_program = NULL)
120
	{
121
		if($type == 'menu')
122
		{
123
			$params["act"] = "getResourceapiMenuPackageList";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = 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...
124
		}
125
		elseif($type == 'skin')
126
		{
127
			$params["act"] = "getResourceapiSkinPackageList";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = 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...
128
			$params['parent_program'] = $parent_program;
129
		}
130
		else
131
		{
132
			$params["act"] = "getResourceapiPackagelist";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = 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...
133
		}
134
135
		$oAdminView = getAdminView('autoinstall');
136
		$params["order_target"] = $order_target;
137
		$params["order_type"] = $order_type;
138
		$params["page"] = $page;
139
140
		if($category_srl)
141
		{
142
			$params["category_srl"] = $category_srl;
143
		}
144
145
		if($search_keyword)
146
		{
147
			$params["search_keyword"] = $search_keyword;
148
		}
149
150
		$xmlDoc = XmlGenerater::getXmlDoc($params);
151
		if($xmlDoc && $xmlDoc->response->packagelist->item)
152
		{
153
			$item_list = $oAdminView->rearranges($xmlDoc->response->packagelist->item);
154
			$this->add('item_list', $item_list);
155
			$array = array('total_count', 'total_page', 'cur_page', 'page_count', 'first_page', 'last_page');
156
			$page_nav = $oAdminView->rearrange($xmlDoc->response->page_navigation, $array);
157
			$page_navigation = new PageHandler($page_nav->total_count, $page_nav->total_page, $page_nav->cur_page, 5);
158
			$this->add('page_navigation', $page_navigation);
159
		}
160
	}
161
162
	/**
163
	 * Get is authed ftp
164
	 */
165
	function getAutoinstallAdminIsAuthed()
166
	{
167
		$oAdminModel = getAdminModel('autoinstall');
168
		$package = $oAdminModel->getInstallInfo(Context::get('package_srl'));
169
		
170
		$is_authed = 0;
0 ignored issues
show
Unused Code introduced by
$is_authed 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...
171
		$output = $oAdminModel->checkUseDirectModuleInstall($package);
172
		if($output->toBool()==TRUE)
173
		{
174
			$is_authed = 1;
175
		}
176
		else
177
		{
178
			$ftp_info = Context::getFTPInfo();
179
			if(!$ftp_info->ftp_root_path)
180
			{
181
				$is_authed = -1;
182
			}
183
			else
184
			{
185
				$is_authed = (int) isset($_SESSION['ftp_password']);
186
			}
187
		}
188
		
189
		$this->add('is_authed', $is_authed);
190
	}
191
192
	/**
193
	 * Returns list of need update
194
	 */
195
	public function getNeedUpdateList()
196
	{
197
		$oModel = getModel('autoinstall');
198
		$output = executeQueryArray('autoinstall.getNeedUpdate');
199
		if(!is_array($output->data))
200
		{
201
			return NULL;
202
		}
203
204
		$result = array();
205
		$xml = new XmlParser();
206
		foreach($output->data as $package)
207
		{
208
			$packageSrl = $package->package_srl;
0 ignored issues
show
Unused Code introduced by
$packageSrl 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...
209
210
			$packageInfo = new stdClass();
211
			$packageInfo->currentVersion = $package->current_version;
212
			$packageInfo->version = $package->version;
213
			$packageInfo->type = $oModel->getTypeFromPath($package->path);
214
			$packageInfo->url = $oModel->getUpdateUrlByPackageSrl($package->package_srl);
215
216
			if($packageInfo->type == 'core')
217
			{
218
				$title = 'XpressEngine';
219
			}
220
			else
221
			{
222
				$configFile = $oModel->getConfigFilePath($packageInfo->type);
223
				$xmlDoc = $xml->loadXmlFile(FileHandler::getRealPath($package->path) . $configFile);
224
225
				if($xmlDoc)
226
				{
227
					$type = $packageInfo->type;
228
					if($type == "drcomponent")
229
					{
230
						$type = "component";
231
					}
232
					if($type == "style" || $type == "m.skin")
233
					{
234
						$type = "skin";
235
					}
236
					if($type == "m.layout")
237
					{
238
						$type = "layout";
239
					}
240
					$title = $xmlDoc->{$type}->title->body;
241
				}
242
				else
243
				{
244
					$pathInfo = explode('/', $package->path);
245
					$title = $pathInfo[count($pathInfo) - 1];
246
				}
247
			}
248
			$packageInfo->title = $title;
249
250
			$result[] = $packageInfo;
251
		}
252
253
		return $result;
254
	}
255
256
	/**
257
	 * Get install info
258
	 *
259
	 * @param int $packageSrl Package sequence to get info
260
	 * @return stdClass install info
261
	 */
262
	public function getInstallInfo($packageSrl)
263
	{
264
		$params["act"] = "getResourceapiInstallInfo";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = 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...
265
		$params["package_srl"] = $packageSrl;
266
		$xmlDoc = XmlGenerater::getXmlDoc($params);
267
		$oModel = getModel('autoinstall');
268
269
		$targetpackages = array();
270
		if($xmlDoc)
271
		{
272
			$xmlPackage = $xmlDoc->response->package;
273
			$package = new stdClass();
274
			$package->package_srl = $xmlPackage->package_srl->body;
275
			$package->title = $xmlPackage->title->body;
276
			$package->package_description = $xmlPackage->package_description->body;
277
			$package->version = $xmlPackage->version->body;
278
			$package->path = $xmlPackage->path->body;
279
			if($xmlPackage->depends)
280
			{
281
				if(!is_array($xmlPackage->depends->item))
282
				{
283
					$xmlPackage->depends->item = array($xmlPackage->depends->item);
284
				}
285
286
				$package->depends = array();
287
				foreach($xmlPackage->depends->item as $item)
288
				{
289
					$dep_item = new stdClass();
290
					$dep_item->package_srl = $item->package_srl->body;
291
					$dep_item->title = $item->title->body;
292
					$dep_item->version = $item->version->body;
293
					$dep_item->path = $item->path->body;
294
					$package->depends[] = $dep_item;
295
					$targetpackages[$dep_item->package_srl] = 1;
296
				}
297
298
				$packages = $oModel->getInstalledPackages(array_keys($targetpackages));
299
				$package->deplist = "";
300
				foreach($package->depends as $key => $dep)
301
				{
302
					if(!$packages[$dep->package_srl])
303
					{
304
						$package->depends[$key]->installed = FALSE;
305
						$package->package_srl .= "," . $dep->package_srl;
306
					}
307
					else
308
					{
309
						$package->depends[$key]->installed = TRUE;
310
						$package->depends[$key]->cur_version = $packages[$dep->package_srl]->current_version;
311
						if(version_compare($dep->version, $packages[$dep->package_srl]->current_version, ">"))
312
						{
313
							$package->depends[$key]->need_update = TRUE;
314
							$package->package_srl .= "," . $dep->package_srl;
315
316
							if($dep->path === '.')
317
							{
318
								$package->contain_core = TRUE;
319
								$package->contain_core_version = $dep->version;
320
							}
321
						}
322
						else
323
						{
324
							$package->need_update = FALSE;
325
						}
326
					}
327
				}
328
			}
329
330
			$installedPackage = $oModel->getInstalledPackage($packageSrl);
331
			if($installedPackage)
332
			{
333
				$package->installed = TRUE;
334
				$package->cur_version = $installedPackage->current_version;
335
				$package->need_update = version_compare($package->version, $installedPackage->current_version, ">");
336
			}
337
338
			if($package->path === '.')
339
			{
340
				$package->contain_core = TRUE;
341
				$package->contain_core_version = $package->version;
342
			}
343
		}
344
345
		return $package;
0 ignored issues
show
Bug introduced by
The variable $package 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...
346
	}
347
348
	/**
349
	 * get install info (act)
350
	 */
351
	public function getAutoInstallAdminInstallInfo()
352
	{
353
		$packageSrl = Context::get('package_srl');
354
		if(!$packageSrl)
355
		{
356
			return new Object(-1, 'msg_invalid_request');
357
		}
358
359
		$package = $this->getInstallInfo($packageSrl);
360
		$this->add('package', $package);
361
	}
362
363
	public function checkUseDirectModuleInstall($package)
364
	{
365
		$directModuleInstall = TRUE;
366
		$arrUnwritableDir = array();
367
		$output = $this->isWritableDir($package->path);
368 View Code Duplication
		if($output->toBool()==FALSE)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
369
		{
370
			$directModuleInstall = FALSE;
371
			$arrUnwritableDir[] = $output->get('path');
372
		}
373
374
		foreach($package->depends as $dep)
375
		{
376
			$output = $this->isWritableDir($dep->path);
377 View Code Duplication
			if($output->toBool()==FALSE)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
378
			{
379
				$directModuleInstall = FALSE;
380
				$arrUnwritableDir[] = $output->get('path');
381
			}
382
		}
383
384
		if($directModuleInstall==FALSE)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
385
		{
386
			$output = new Object(-1, 'msg_direct_inall_invalid');
387
			$output->add('path', $arrUnwritableDir);
388
			return $output;
389
		}
390
391
		return new Object();
392
	}
393
394
	public function isWritableDir($path)
395
	{
396
		$path_list = explode('/', dirname($path));
397
		$real_path = './';
398
399
		while($path_list)
0 ignored issues
show
Bug Best Practice introduced by
The expression $path_list 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...
400
		{
401
			$check_path = realpath($real_path . implode('/', $path_list));
402
			if(FileHandler::isDir($check_path))
0 ignored issues
show
Bug Best Practice introduced by
The expression \FileHandler::isDir($check_path) of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
403
			{
404
				break;
405
			}
406
			array_pop($path_list);
407
		}
408
409
		if(FileHandler::isWritableDir($check_path)==FALSE)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
410
		{
411
			$output = new Object(-1, 'msg_unwritable_directory');
412
			$output->add('path', FileHandler::getRealPath($check_path));
0 ignored issues
show
Bug introduced by
The variable $check_path 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...
413
			return $output;
414
		}
415
		return new Object();
416
	}
417
418
}
419
/* End of file autoinstall.admin.model.php */
420
/* Location: ./modules/autoinstall/autoinstall.admin.model.php */
421