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 ( d80144...902697 )
by gyeong-won
12:23
created

pollController::triggerInsertCommentPoll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) NAVER <http://www.navercorp.com> */
3
/**
4
 * @class  pollController
5
 * @author NAVER ([email protected])
6
 * @brief Controller class for poll module
7
 */
8
class pollController extends poll
9
{
10
	/**
11
	 * @brief Initialization
12
	 */
13
	function init()
14
	{
15
	}
16
17
	/**
18
	 * @brief after a qeustion is created in the popup window, register the question during the save time
19
	 */
20
	function procPollInsert()
21
	{
22
		$stop_date = Context::get('stop_date');
23
		if($stop_date < date('Ymd'))
24
		{
25
			$stop_date = date('YmdHis', $_SERVER['REQUEST_TIME']+60*60*24*365);
26
		}
27
28
		$logged_info = Context::get('logged_info');
29
		$vars = Context::getRequestVars();
30
		$args = new stdClass;
31
		$tmp_args = array();
32
33
		unset($vars->_filter);
34
		unset($vars->error_return_url);
35
		unset($vars->stop_date);
36
37
		foreach($vars as $key => $val)
38
		{
39
			if(stripos($key, 'tidx'))
40
			{
41
				continue;
42
			}
43
44
			$tmp_arr = explode('_', $key);
45
46
			$poll_index = $tmp_arr[1];
47
			if(!$poll_index)
48
			{
49
				continue;
50
			}
51
52
			if(!trim($val))
53
			{
54
				continue;
55
			}
56
57
			if($tmp_args[$poll_index] == NULL)
58
			{
59
				$tmp_args[$poll_index] = new stdClass;
60
			}
61
62
			if(!is_array($tmp_args[$poll_index]->item))
63
			{
64
				$tmp_args[$poll_index]->item = array();
65
			}
66
67
			if($logged_info->is_admin != 'Y')
68
			{
69
				$val = htmlspecialchars($val, ENT_COMPAT | ENT_HTML401, 'UTF-8', false);
70
			}
71
72
			switch($tmp_arr[0])
73
			{
74
				case 'title':
75
					$tmp_args[$poll_index]->title = $val;
76
					break;
77
				case 'checkcount':
78
					$tmp_args[$poll_index]->checkcount = $val;
79
					break;
80
				case 'item':
81
					$tmp_args[$poll_index]->item[] = $val;
82
					break;
83
			}
84
		}
85
86
		foreach($tmp_args as $key => $val)
87
		{
88
			if(!$val->checkcount)
89
			{
90
				$val->checkcount = 1;
91
			}
92
93
			if($val->title && count($val->item))
94
			{
95
				$args->poll[] = $val;
96
			}
97
		}
98
99
		if(!count($args->poll)) return new Object(-1, 'cmd_null_item');
100
101
		$args->stop_date = $stop_date;
102
103
		// Configure the variables
104
		$poll_srl = getNextSequence();
105
		$member_srl = $logged_info->member_srl?$logged_info->member_srl:0;
106
107
		$oDB = &DB::getInstance();
108
		$oDB->begin();
109
110
		// Register the poll
111
		$poll_args = new stdClass;
112
		$poll_args->poll_srl = $poll_srl;
113
		$poll_args->member_srl = $member_srl;
114
		$poll_args->list_order = $poll_srl*-1;
115
		$poll_args->stop_date = $args->stop_date;
116
		$poll_args->poll_count = 0;
117
		$output = executeQuery('poll.insertPoll', $poll_args);
118
		if(!$output->toBool())
119
		{
120
			$oDB->rollback();
121
			return $output;
122
		}
123
124
		// Individual poll registration
125
		foreach($args->poll as $key => $val)
126
		{
127
			$title_args = new stdClass;
128
			$title_args->poll_srl = $poll_srl;
129
			$title_args->poll_index_srl = getNextSequence();
130
			$title_args->title = $val->title;
131
			$title_args->checkcount = $val->checkcount;
132
			$title_args->poll_count = 0;
133
			$title_args->list_order = $title_args->poll_index_srl * -1;
134
			$title_args->member_srl = $member_srl;
135
			$title_args->upload_target_srl = $upload_target_srl;
0 ignored issues
show
Bug introduced by
The variable $upload_target_srl 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...
136
			$output = executeQuery('poll.insertPollTitle', $title_args);
137
			if(!$output->toBool())
138
			{
139
				$oDB->rollback();
140
				return $output;
141
			}
142
143
			// Add the individual survey items
144
			foreach($val->item as $k => $v)
145
			{
146
				$item_args = new stdClass;
147
				$item_args->poll_srl = $poll_srl;
148
				$item_args->poll_index_srl = $title_args->poll_index_srl;
149
				$item_args->title = $v;
150
				$item_args->poll_count = 0;
151
				$item_args->upload_target_srl = $upload_target_srl;
152
				$output = executeQuery('poll.insertPollItem', $item_args);
153
				if(!$output->toBool())
154
				{
155
					$oDB->rollback();
156
					return $output;
157
				}
158
			}
159
		}
160
161
		$oDB->commit();
162
163
		$this->add('poll_srl', $poll_srl);
164
		$this->setMessage('success_registed');
165
	}
166
167
	/**
168
	 * @brief Accept the poll
169
	 */
170
	function procPoll()
171
	{
172
		$poll_srl = Context::get('poll_srl');
173
		$poll_srl_indexes = Context::get('poll_srl_indexes');
174
		$tmp_item_srls = explode(',',$poll_srl_indexes);
175
		for($i=0;$i<count($tmp_item_srls);$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...
176
		{
177
			$srl = (int)trim($tmp_item_srls[$i]);
178
			if(!$srl) continue;
179
			$item_srls[] = $srl;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$item_srls was never initialized. Although not strictly required by PHP, it is generally a good practice to add $item_srls = 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...
180
		}
181
182
		// If there is no response item, display an error
183
		if(!count($item_srls)) return new Object(-1, 'msg_check_poll_item');
0 ignored issues
show
Bug introduced by
The variable $item_srls 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...
184
		// Make sure is the poll has already been taken
185
		$oPollModel = getModel('poll');
186
		if($oPollModel->isPolled($poll_srl)) return new Object(-1, 'msg_already_poll');
187
188
		$oDB = &DB::getInstance();
189
		$oDB->begin();
190
191
		$args = new stdClass;
192
		$args->poll_srl = $poll_srl;
193
		// Update all poll responses related to the post
194
		$output = executeQuery('poll.updatePoll', $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...
195
		$output = executeQuery('poll.updatePollTitle', $args);
196
		if(!$output->toBool())
197
		{
198
			$oDB->rollback();
199
			return $output;
200
		}
201
		// Record each polls selected items
202
		$args->poll_item_srl = implode(',',$item_srls);
203
		$output = executeQuery('poll.updatePollItems', $args);
204
		if(!$output->toBool())
205
		{
206
			$oDB->rollback();
207
			return $output;
208
		}
209
		// Log the respondent's information
210
		$log_args = new stdClass;
211
		$log_args->poll_srl = $poll_srl;
212
213
		$logged_info = Context::get('logged_info');
214
		$member_srl = $logged_info->member_srl?$logged_info->member_srl:0;
215
216
		$log_args->member_srl = $member_srl;
217
		$log_args->ipaddress = $_SERVER['REMOTE_ADDR'];
218
		$output = executeQuery('poll.insertPollLog', $log_args);
219
		if(!$output->toBool())
220
		{
221
			$oDB->rollback();
222
			return $output;
223
		}
224
225
		$oDB->commit();
226
227
		$skin = Context::get('skin');
228 View Code Duplication
		if(!$skin || !is_dir(_XE_PATH_ . 'modules/poll/skins/'.$skin)) $skin = 'default';
229
		// Get tpl
230
		$tpl = $oPollModel->getPollHtml($poll_srl, '', $skin);
231
232
		$this->add('poll_srl', $poll_srl);
233
		$this->add('tpl',$tpl);
234
		$this->setMessage('success_poll');
235
236
		$returnUrl = Context::get('success_return_url') ? Context::get('success_return_url') : getNotEncodedUrl('', 'module', 'admin', 'act', 'dispPollAdminConfig');
237
		$this->setRedirectUrl($returnUrl);
238
	}
239
240
	/**
241
	 * @brief Preview the results
242
	 */
243
	function procPollViewResult()
244
	{
245
		$poll_srl = Context::get('poll_srl');
246
247
		$skin = Context::get('skin');
248 View Code Duplication
		if(!$skin || !is_dir(_XE_PATH_ . 'modules/poll/skins/'.$skin)) $skin = 'default';
249
250
		$oPollModel = getModel('poll');
251
		$tpl = $oPollModel->getPollResultHtml($poll_srl, $skin);
252
253
		$this->add('poll_srl', $poll_srl);
254
		$this->add('tpl',$tpl);
255
	}
256
257
	/**
258
	 * @brief poll list
259
	 */
260
	function procPollGetList()
261
	{
262
		if(!Context::get('is_logged')) return new Object(-1,'msg_not_permitted');
263
		$pollSrls = Context::get('poll_srls');
264
		if($pollSrls) $pollSrlList = explode(',', $pollSrls);
265
266
		global $lang;
267
		if(count($pollSrlList) > 0)
268
		{
269
			$oPollAdminModel = getAdminModel('poll');
270
			$args = new stdClass;
271
			$args->pollIndexSrlList = $pollSrlList;
0 ignored issues
show
Bug introduced by
The variable $pollSrlList 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...
272
			$output = $oPollAdminModel->getPollListWithMember($args);
273
			$pollList = $output->data;
274
275
			if(is_array($pollList))
276
			{
277
				foreach($pollList AS $key=>$value)
278
				{
279
					if($value->checkcount == 1) $value->checkName = $lang->single_check;
280
					else $value->checkName = $lang->multi_check;
281
				}
282
			}
283
		}
284
		else
285
		{
286
			$pollList = array();
287
			$this->setMessage($lang->no_documents);
288
		}
289
290
		$this->add('poll_list', $pollList);
291
	}
292
293
	/**
294
	 * @brief A poll synchronization trigger when a new post is registered
295
	 */
296
	function triggerInsertDocumentPoll(&$obj)
297
	{
298
		$this->syncPoll($obj->document_srl, $obj->content);
299
		return new Object();
300
	}
301
302
	/**
303
	 * @brief A poll synchronization trigger when a new comment is registered
304
	 */
305
	function triggerInsertCommentPoll(&$obj)
306
	{
307
		$this->syncPoll($obj->comment_srl, $obj->content);
308
		return new Object();
309
	}
310
311
	/**
312
	 * @brief A poll synchronization trigger when a post is updated
313
	 */
314
	function triggerUpdateDocumentPoll(&$obj)
315
	{
316
		$this->syncPoll($obj->document_srl, $obj->content);
317
		return new Object();
318
	}
319
320
	/**
321
	 * @brief A poll synchronization trigger when a comment is updated
322
	 */
323
	function triggerUpdateCommentPoll(&$obj)
324
	{
325
		$this->syncPoll($obj->comment_srl, $obj->content);
326
		return new Object();
327
	}
328
329
	/**
330
	 * @brief A poll deletion trigger when a post is removed
331
	 */
332 View Code Duplication
	function triggerDeleteDocumentPoll(&$obj)
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...
333
	{
334
		$document_srl = $obj->document_srl;
335
		if(!$document_srl) return new Object();
336
		// Get the poll
337
		$args = new stdClass();
338
		$args->upload_target_srl = $document_srl;
339
		$output = executeQuery('poll.getPollByTargetSrl', $args);
340
		if(!$output->data) return new Object();
341
342
		$poll_srl = $output->data->poll_srl;
343
		if(!$poll_srl) return new Object();
344
345
		$args->poll_srl = $poll_srl;
346
347
		$output = executeQuery('poll.deletePoll', $args);
348
		if(!$output->toBool()) return $output;
349
350
		$output = executeQuery('poll.deletePollItem', $args);
351
		if(!$output->toBool()) return $output;
352
353
		$output = executeQuery('poll.deletePollTitle', $args);
354
		if(!$output->toBool()) return $output;
355
356
		$output = executeQuery('poll.deletePollLog', $args);
357
		if(!$output->toBool()) return $output;
358
359
		return new Object();
360
	}
361
362
	/**
363
	 * @brief A poll deletion trigger when a comment is removed
364
	 */
365 View Code Duplication
	function triggerDeleteCommentPoll(&$obj)
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...
366
	{
367
		$comment_srl = $obj->comment_srl;
368
		if(!$comment_srl) return new Object();
369
		// Get the poll
370
		$args = new stdClass();
371
		$args->upload_target_srl = $comment_srl;
372
		$output = executeQuery('poll.getPollByTargetSrl', $args);
373
		if(!$output->data) return new Object();
374
375
		$poll_srl = $output->data->poll_srl;
376
		if(!$poll_srl) return new Object();
377
378
		$args->poll_srl = $poll_srl;
379
380
		$output = executeQuery('poll.deletePoll', $args);
381
		if(!$output->toBool()) return $output;
382
383
		$output = executeQuery('poll.deletePollItem', $args);
384
		if(!$output->toBool()) return $output;
385
386
		$output = executeQuery('poll.deletePollTitle', $args);
387
		if(!$output->toBool()) return $output;
388
389
		$output = executeQuery('poll.deletePollLog', $args);
390
		if(!$output->toBool()) return $output;
391
392
		return new Object();
393
	}
394
395
	/**
396
	 * @brief As post content's poll is obtained, synchronize the poll using the document number
397
	 */
398
	function syncPoll($upload_target_srl, $content)
399
	{
400
		$match_cnt = preg_match_all('!<img([^\>]*)poll_srl=(["\']?)([0-9]*)(["\']?)([^\>]*?)\>!is',$content, $matches);
401
		for($i=0;$i<$match_cnt;$i++)
402
		{
403
			$poll_srl = $matches[3][$i];
404
405
			$args = new stdClass;
406
			$args->poll_srl = $poll_srl;
407
			$output = executeQuery('poll.getPoll', $args);
408
			$poll = $output->data;
409
410
			if($poll->upload_target_srl) continue;
411
412
			$args->upload_target_srl = $upload_target_srl;
413
			$output = executeQuery('poll.updatePollTarget', $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...
414
			$output = executeQuery('poll.updatePollTitleTarget', $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...
415
			$output = executeQuery('poll.updatePollItemTarget', $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...
416
		}
417
	}
418
}
419
/* End of file poll.controller.php */
420
/* Location: ./modules/poll/poll.controller.php */
421