EveBB /include/poll.php

Language PHP Lines 755
MD5 Hash 8ae68a7a5355ac1d67e168ab8fa5b3de
Repository https://github.com/Dratone/EveBB.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
<?php
/**
 * Copyright (C) 2011 Visman (visman@inbox.ru)
 * based on code by kg (kg@as-planned.com)
 * Poll Mod for FluxBB, written by As-Planned.com
 * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
 */

if (!defined('PUN'))
	exit;

if (file_exists(PUN_ROOT.'lang/'.$pun_user['language'].'/poll.php'))
	require PUN_ROOT.'lang/'.$pun_user['language'].'/poll.php';
else
	require PUN_ROOT.'lang/English/poll.php';

// вывод сообщений *************************************************************
function poll_mess($mess, $ques = '', $vote = '')
{
	global $lang_poll;

	if (empty($ques))
		$m = $lang_poll[$mess];
	else if (!empty($ques) && empty($vote))
		$m = sprintf($lang_poll[$mess], $ques);
	else
		$m = sprintf($lang_poll[$mess], $ques, $vote);

	message($lang_poll['Err poll'].'<h2>'.$m.'</h2>');
}

// получение данных из формы ***************************************************
function poll_post($var, $default = null)
{
	return isset($_POST[$var]) ? $_POST[$var] : $default;
}

// запрещено ли редактировать **************************************************
function poll_noedit($tid)
{
	global $is_admmod, $pun_config;

	$top = poll_topic($tid);
	return (!$is_admmod && ($top[0] == 2 || ($top[0] == 1 && $pun_config['o_poll_time'] != 0 && (time() - $top[1] > 60 * $pun_config['o_poll_time']))));
}

// доступ запрещен *************************************************************
function poll_bad()
{
	global $pun_user, $pun_config;

	return ($pun_user['is_guest'] || $pun_config['o_poll_enabled'] != '1');
}

// может ли голосовать юзер ****************************************************
function poll_can_vote($tid, $uid)
{
	global $db, $pun_user;

	if ($pun_user['is_guest']) return false;
	
	$result = $db->query('SELECT 1 FROM '.$db->prefix.'poll_voted WHERE tid='.$tid.' AND uid='.$uid) or error('Unable to fetch poll voted info', __FILE__, __LINE__, $db->error());
	return ($db->num_rows($result) == 0);
}

// получение информации по опросу **********************************************
function poll_info($tid, $uid = NULL)
{
	global $db;

	if ($tid == 0) return null;
	
	if (file_exists(FORUM_CACHE_DIR.'polls/'.$tid.'.php'))
		include FORUM_CACHE_DIR.'polls/'.$tid.'.php';
		
	if (!isset($kol))
	{
		$result = $db->query('SELECT question, field, choice, votes FROM '.$db->prefix.'poll WHERE tid='.$tid.' ORDER BY question, field') or error('Unable to fetch poll info', __FILE__, __LINE__, $db->error());

	  $kol = 0;
	  $questions = $type = $choices = $votes = array();
		while ($cur = $db->fetch_assoc($result))
		{
			$kol = $cur['question'];
			if ($cur['field'] == 0)
			{
				$questions[$kol] = $cur['choice'];
				$type[$kol] = $cur['votes'];
				$choices[$kol] = array();
				$votes[$kol] = array();
			}
			else
			{
				$choices[$kol][$cur['field']] = $cur['choice'];
				$votes[$kol][$cur['field']] = $cur['votes'];
			}
		}

		if ($kol == 0) return null;
	
		$rez = array(
			'questions' => $questions,
			'choices' => $choices,
			'votes' => $votes,
			'type' => $type,
			);

		if (!is_dir(FORUM_CACHE_DIR.'polls/'))
			mkdir(FORUM_CACHE_DIR.'polls', 0755);

		$fh = @fopen(FORUM_CACHE_DIR.'polls/'.$tid.'.php', 'wb');
		if (!$fh)
			error('Unable to write configuration cache file to cache(/polls) directory. Please make sure PHP has write access to this directory.', __FILE__, __LINE__);

		fwrite($fh, '<?php'."\n\n".'$kol = '.$kol.';'."\n\n".'$rez = '.var_export($rez, true).';'."\n\n".'?'.'>');

		fclose($fh);

		if (function_exists('apc_delete_file'))
			@apc_delete_file(FORUM_CACHE_DIR.'polls/'.$tid.'.php');
	}
	
	if ($kol == 0) return null;

	$rez['canVote'] = (is_null($uid)) ? false : poll_can_vote($tid, $uid);
	$rez['isGuest'] = (is_null($uid) || $uid > 1) ? false : true;

	return $rez;
}

// форма ввода в новой теме ****************************************************
function poll_form_post($tid)
{
	if ($tid == 0)
		poll_form(0);
}

// форма редактирования ********************************************************
function poll_form_edit($tid)
{
	poll_form($tid);
}

// данные по опросу ************************************************************
function poll_topic($tid)
{
	global $cur_post, $cur_topic;
	
	if ($tid == 0)
	{
		$rez = array(0,time(),0,0);
  }
	else if (isset($cur_topic['poll_type']))
	{
		$rez = array($cur_topic['poll_type'], $cur_topic['poll_time'], $cur_topic['poll_term'], $cur_topic['poll_kol']);
		if ($cur_topic['closed'] != '0')
			$rez[0] = 2;
	}
	else if (isset($cur_post['poll_type']))
	{
		$rez = array($cur_post['poll_type'], $cur_post['poll_time'], $cur_post['poll_term'], $cur_post['poll_kol']);
	}
	else
		$rez = array(0,time(),0,0);
		
	return $rez;
}

// форма ввода/редактирования **************************************************
function poll_form($tid)
{
	global $cur_index, $lang_poll, $pun_config, $tpl_main;

	if (poll_bad()) return;

	$top = poll_topic($tid);
	$enabled = ($top[0] > 0);
	$resu = ($top[2] > 1);
	$term = max($top[2],$pun_config['o_poll_term']);
	
	if (poll_noedit($tid))
		$edit = false;
	else
		$edit = true;

	$questions = $type = $choices = array();
	if ($edit && poll_post('form_sent', false))
	{
		$enabled = (poll_post('poll_enabled', 0) == 1);
		$resu = (poll_post('poll_result', 0) == 1);

		$questions = poll_post('poll_question', array());
		$choices = poll_post('poll_choice', array());
		$type = poll_post('poll_type', array());
	}
	else
	{
		$info = poll_info($tid);
		if (!is_null($info))
		{
			$questions = $info['questions'];
			$choices = $info['choices'];
			$type = $info['type'];
		}
  }

	$questions = array_map('pun_trim', $questions);
	$type = array_map('intval', $type);

	if (poll_noedit($tid))
	{
	?>
		<div class="inform">
			<fieldset>
				<legend><?php echo $lang_poll['Form legend'] ?></legend>
				<div class="infldset txtarea">
					<div class="rbox"><label><input disabled="disabled" type="checkbox" id="poll_enabled" name="poll_enabled" value="1" <?php if ($enabled) echo 'checked="checked"'?> /> <?php echo $lang_poll['Form enable'] ?></label></div>
					<div id="poll_input">
<?php
		if ($term > 1)
		{
?>
						<div class="rbox"><label><input disabled="disabled" type="checkbox" name="poll_result"  value="1" <?php if ($resu) echo 'checked="checked"'?> /> <?php printf($lang_poll['Form result'], $term) ?></label></div>
<?php
		}
		$fk = true;
		for ($k = 1; $k <= $pun_config['o_poll_max_ques'] && $fk; $k++)
		{
			$question = (isset($questions[$k])) ? pun_htmlspecialchars($questions[$k]) : '';
			if (empty($question))
			{
				$fk = false;
				break;
			}
			$fi = $fk;
?>
						<div id="poll_number_<?php echo $k ?>">
<?php if ($k > 1) echo "\t\t\t\t\t\t\t<br /><hr><br />\n"; ?>
							<label><?php printf($lang_poll['Form question'], $k) ?><br /><input disabled="disabled" id="poll_ques_<?php echo $k ?>" class="longinput" type="text" name="poll_question[<?php echo $k ?>]" value="<?php echo $question ?>" size="80" maxlength="250" /></label>
							<label><?php echo $lang_poll['Form type'] ?>&nbsp;<input disabled="disabled" type="text" name="poll_type[<?php echo $k ?>]" value="<?php echo ((!isset($type[$k]) || $type[$k]<2) ? '1' : $type[$k]) ?>" size="4" maxlength="2" /></label>
<?php
			for ($i = 1; $i <= $pun_config['o_poll_max_field'] && $fi; $i++)
			{
				$choice = (isset($choices[$k][$i])) ? pun_htmlspecialchars(pun_trim($choices[$k][$i])) : '';
				if (empty($choice))
				{
					$fi = false;
					break;
				}
?>
							<label><?php printf($lang_poll['Form choice'], $i) ?><br /><input disabled="disabled" class="longinput" type="text" name="poll_choice[<?php echo $k ?>][<?php echo $i?>]" value="<?php echo $choice ?>" size="80" maxlength="250" /></label>
<?php
			}
?>
						</div>
<?php
		}
?>
					</div>
				</div>
			</fieldset>
		</div>
<?php

		return;
	}

	?>
		<div class="inform">
			<fieldset>
				<legend><?php echo $lang_poll['Form legend'] ?></legend>
				<div class="infldset txtarea">
					<div class="rbox"><label><input type="checkbox" id="poll_enabled" name="poll_enabled" onclick="ForEnabled();" value="1" <?php if ($enabled) echo 'checked="checked"'?> tabindex="<?php echo $cur_index++ ?>" /> <?php echo $lang_poll['Form enable'] ?></label></div>
					<div id="poll_input">
<?php
	if ($term > 1)
	{
?>
						<div class="rbox"><label><input type="checkbox" name="poll_result"  value="1" <?php if ($resu) echo 'checked="checked"'?> tabindex="<?php echo $cur_index++ ?>" /> <?php printf($lang_poll['Form result'], $term) ?></label></div>
<?php
	}
	$fk = true;
	for ($k = 1; $k <= $pun_config['o_poll_max_ques']; $k++)
	{
		$question = (isset($questions[$k]) && $fk) ? pun_htmlspecialchars($questions[$k]) : '';
?>
						<div id="poll_number_<?php echo $k ?>">
<?php if ($k > 1) echo "\t\t\t\t\t\t\t<br /><hr><br />\n"; ?>
							<label><?php printf($lang_poll['Form question'], $k) ?><br /><input id="poll_ques_<?php echo $k ?>" class="longinput" type="text" name="poll_question[<?php echo $k ?>]" value="<?php echo $question ?>" tabindex="<?php echo $cur_index++ ?>" size="80" maxlength="250" onkeyup="ForQues(<?php echo $k ?>)" /></label>
							<label><?php echo $lang_poll['Form type'] ?>&nbsp;<input type="text" name="poll_type[<?php echo $k ?>]" value="<?php echo ((!isset($type[$k]) || $type[$k]<2) ? '1' : $type[$k]) ?>" tabindex="<?php echo $cur_index++ ?>" size="4" maxlength="2" /></label>
<?php
		if (empty($question))
			$fk = false;
		$fi = $fk;
		
		for ($i = 1; $i <= $pun_config['o_poll_max_field']; $i++)
		{
			$choice = (isset($choices[$k][$i]) && $fi) ? pun_htmlspecialchars(pun_trim($choices[$k][$i])) : '';
?>
							<label><?php printf($lang_poll['Form choice'], $i) ?><br /><input class="longinput" type="text" name="poll_choice[<?php echo $k ?>][<?php echo $i?>]" value="<?php echo $choice ?>" tabindex="<?php echo $cur_index++ ?>" size="80" maxlength="250" onkeyup="ForChoice(<?php echo $k ?>,<?php echo $i?>)" /></label>
<?php
			if (empty($choice))
				$fi = false;
		}
?>
						</div>
<?php
	}
?>
					</div>
				</div>
			</fieldset>
		</div>
<script type="text/javascript">
/* <![CDATA[ */
var max_ques = <?php echo $pun_config['o_poll_max_ques'] ?>;
var max_field = <?php echo $pun_config['o_poll_max_field'] ?>;
function ForEnabled(){var c=document.getElementById('poll_enabled');if(c.checked==true){document.getElementById('poll_input').style.display='';ForQues(1,'')}else{document.getElementById('poll_input').style.display='none'}return false}
function ForChoice(num,field,t){if(num > max_ques || field > max_field){return false}var div=document.getElementById('poll_number_'+num);var i=field+1;if(typeof t != 'undefined'){if(t == 'none'){div.getElementsByTagName('label')[i].style.display='none';ForChoice(num,i,'none')}else{div.getElementsByTagName('label')[i].style.display='';ForChoice(num,field)}}else{var inp=div.getElementsByTagName('label')[i].getElementsByTagName('input')[0];if(inp.value == ""){ForChoice(num, i, 'none')}else{ForChoice(num, i, '')}}return false}
function ForQues(num,t){if(num > max_ques){return false}var div=document.getElementById('poll_number_'+num);if(typeof t != 'undefined'){if(t == 'none'){div.style.display='none';ForQues(num+1,'none')}else{div.style.display='';ForQues(num)}}else{var inp=document.getElementById('poll_ques_'+num);if(inp.value == ""){div.getElementsByTagName('label')[1].style.display='none';ForChoice(num,1,'none');ForQues(num+1,'none')}else{div.getElementsByTagName('label')[1].style.display='';ForChoice(num,1,'');ForQues(num+1,'')}}return false}
/* ]]> */
</script>
<?php
	$tpl_main = str_replace('<body onload="', '<body onload="ForEnabled();', $tpl_main);
	$tpl_main = str_replace('<body>', '<body onload="ForEnabled()">', $tpl_main);
}

// проверяем правильность ******************************************************
function poll_form_validate($tid, &$errors)
{
	global $lang_poll, $pun_config;

	if (poll_bad() || poll_noedit($tid)) return;

	$enabled = (poll_post('poll_enabled', 0) == 1);
	if ($enabled)
	{
		$questions = poll_post('poll_question', array());
		$choices = poll_post('poll_choice', array());
		$type = poll_post('poll_type', array());

		$questions = array_map('pun_trim', $questions);
		$type = array_map('intval', $type);

		$fk = true;
		$kol = 0;
		for ($k = 1; $k <= $pun_config['o_poll_max_ques'] && $fk; $k++)
		{
			$question = (isset($questions[$k]) && $fk) ? $questions[$k] : '';
			if ($question == '')
    		$fk = false;
			else
			{
				$kol++;
    		if (pun_strlen($question) > 250)
					$errors[] = sprintf($lang_poll['Question too long'], $k);

				$koc = 0;
				$fi = $fk;
				for ($i = 1; $i <= $pun_config['o_poll_max_field'] && $fi; $i++)
				{
					$choice = (isset($choices[$k][$i]) && $fi) ? pun_trim($choices[$k][$i]) : '';
					if ($choice == '')
						$fi = false;
					else
					{
						$koc++;
						if (pun_strlen($choice) > 250)
							$errors[] = sprintf($lang_poll['Choice too long'], $k, $i);
					}
				}
				if ($koc < 2)
					$errors[] = sprintf($lang_poll['Not enough choices'], $k);
        else if (!isset($type[$k]) || $type[$k] < 1 || $type[$k] >= $koc)
					$errors[] = sprintf($lang_poll['Max variant'], $k);
			}
		}
		if ($kol == 0)
			$errors[] = $lang_poll['No question'];
	}
}

// удаление кэша опроса ********************************************************
function poll_cache_delete($tid)
{
	if (file_exists(FORUM_CACHE_DIR.'polls/'.$tid.'.php'))
		@unlink(FORUM_CACHE_DIR.'polls/'.$tid.'.php');
}

// удаление опроса *************************************************************
function poll_delete($tid, $flag = false)
{
	global $db;

	$db->query('DELETE FROM '.$db->prefix.'poll WHERE tid='.$tid) or error('Unable to remove poll', __FILE__, __LINE__, $db->error());
	$db->query('DELETE FROM '.$db->prefix.'poll_voted WHERE tid='.$tid) or error('Unable to remove poll_voted', __FILE__, __LINE__, $db->error());
	if ($flag)
		$db->query('UPDATE '.$db->prefix.'topics SET poll_type=0, poll_time=0, poll_term=0, poll_kol=0 WHERE id='.$tid) or error('Unable to update topic', __FILE__, __LINE__, $db->error());

	poll_cache_delete($tid);
}

// сохраняем опрос *************************************************************
function poll_save($tid)
{
	global $db, $pun_config;

	if (poll_bad() || poll_noedit($tid)) return;

	$top = poll_topic($tid);
	$enabled = (poll_post('poll_enabled', 0) == 1);
	if ($enabled)
	{
		$term = 0;
		if (poll_post('poll_result', 0) == 1)
			$term = max($top[2],$pun_config['o_poll_term']);

		$questions = poll_post('poll_question', array());
		$choices = poll_post('poll_choice', array());
		$type = poll_post('poll_type', array());

		$questions = array_map('pun_trim', $questions);
		$type = array_map('intval', $type);

		$cur_ch = array();
		$result = $db->query('SELECT question, field FROM '.$db->prefix.'poll WHERE tid='.$tid.' ORDER BY question, field') or error('Unable to fetch poll info', __FILE__, __LINE__, $db->error());
		while ($ch = $db->fetch_assoc($result))
			$cur_ch[$ch['question']][$ch['field']] = true;

		$fk = true;
		for ($k = 1; $k <= $pun_config['o_poll_max_ques'] && $fk; $k++)
		{
			$question = (isset($questions[$k]) && $fk) ? $questions[$k] : '';
			if ($question == '')
    		$fk = false;
			else
			{
				if (isset($cur_ch[$k][0]))
				{
					$db->query('UPDATE '.$db->prefix.'poll SET choice=\''.$db->escape($question).'\', votes='.$type[$k].' WHERE tid='.$tid.' AND question='.$k.' AND field=0') or error('Unable to update poll question', __FILE__, __LINE__, $db->error());
					unset($cur_ch[$k][0]);
					unset($cur_ch[$k][0]);
				}
				else
					$db->query('INSERT INTO '.$db->prefix.'poll (tid, question, field, choice, votes) VALUES ('.$tid.','.$k.',0,\''.$db->escape($question).'\','.$type[$k].')') or error('Unable to create poll question', __FILE__, __LINE__, $db->error());

				$fi = $fk;
				for ($i = 1; $i <= $pun_config['o_poll_max_field'] && $fi; $i++)
				{
					$choice = (isset($choices[$k][$i]) && $fi) ? pun_trim($choices[$k][$i]) : '';
					if ($choice == '')
						$fi = false;
					else
					{
						if (isset($cur_ch[$k][$i]))
						{
							$db->query('UPDATE '.$db->prefix.'poll SET choice=\''.$db->escape($choice).'\' WHERE tid='.$tid.' AND question='.$k.' AND field='.$i) or error('Unable to update poll choice', __FILE__, __LINE__, $db->error());
							unset($cur_ch[$k][$i]);
							unset($cur_ch[$k][$i]);
						}
						else
							$db->query('INSERT INTO '.$db->prefix.'poll (tid, question, field, choice, votes) VALUES ('.$tid.','.$k.','.$i.',\''.$db->escape($choice).'\',0)') or error('Unable to create poll choice', __FILE__, __LINE__, $db->error());

					}
				}
			}
		}
		$db->query('UPDATE '.$db->prefix.'topics SET poll_type=1, poll_time='.$top[1].', poll_term='.$term.', poll_kol='.$top[3].' WHERE id='.$tid) or error('Unable to update topic', __FILE__, __LINE__, $db->error());

		foreach($cur_ch as $k => $ch)
		{
			foreach($ch as $i => $c)
			{
				$db->query('DELETE FROM '.$db->prefix.'poll  WHERE tid='.$tid.' AND question='.$k.' AND field='.$i) or error('Unable to delete poll choice', __FILE__, __LINE__, $db->error());
			}
		}
		poll_cache_delete($tid);
	}
	else
	{
		if ($top[0] == 0)
			return;
		poll_delete($tid, true);
	}
}

// результат голосования в теме ************************************************
function poll_display_topic($tid, $uid, $p = 0)
{
	global $pun_config;

	if ($pun_config['o_poll_enabled'] != '1') return;

	$top = poll_topic($tid);
	if ($top[0] == 0) return;
		
	$top[4] = $p;
	$info = poll_info($tid, $uid);
	poll_display($tid, $uid, $info, $top);
}

// превью в посте **************************************************************
function poll_display_post($tid, $uid)
{
	global $pun_config;

	if (poll_bad()) return;

	if ($tid > 0 && poll_noedit($tid)) // уже запрещено менять опрос
	{
		$top = poll_topic($tid);
		if ($top[0] == 0) return;

		$info = poll_info($tid, $uid);
	}
	else // а тут опрос еще можно редактировать
	{
		if (poll_post('poll_enabled', 0) != 1) return;
   
		$top = poll_topic($tid);

		$questions = poll_post('poll_question', array());
		$choices = poll_post('poll_choice', array());
		$type = poll_post('poll_type', array());

		$questions = array_map('pun_trim', $questions);
		$type = array_map('intval', $type);

		$q = $ch = array();
		$fk = true;
		for ($k = 1; $k <= $pun_config['o_poll_max_ques'] && $fk; $k++)
		{
			$question = (isset($questions[$k]) && $fk) ? $questions[$k] : '';
			if ($question == '')
	   		$fk = false;
			else
			{
				$q[$k] = $question;
				$ch[$k] = $votes[$k] = array();

				$fi = $fk;
				for ($i = 1; $i <= $pun_config['o_poll_max_field'] && $fi; $i++)
				{
					$choice = (isset($choices[$k][$i]) && $fi) ? pun_trim($choices[$k][$i]) : '';
					if ($choice == '')
						$fi = false;
					else
					{
						$ch[$k][$i] = $choice;
						$votes[$k][$i] = 0;
					}
				}
			}
		}

		$info = array(
			'questions' => $q,
			'choices' => $ch,
			'votes' => $votes,
			'type' => $type,
			'isGuest' => false,
		);
	}

	$top[4] = 0;
	$info['canVote'] = true;
	poll_display($tid, $uid, $info, $top, true);
}

// отображаем результат голосования ********************************************
function poll_display($tid, $uid, $info, $top, $prev = false)
{

	global $db, $lang_poll, $pun_config, $lang_common;

	if (is_null($info)) return;
	
	$can_vote = ($info['canVote'] && $top[0] != 2 && poll_post('poll_view') == null);
	$can_visi = ((($info['isGuest'] && $pun_config['o_poll_guest'] == '1') || !$info['isGuest']) && $top[2] <= $top[3]);
	$fmess = '';
	if ($prev)
		$fmess = '&nbsp;';
	else if ($top[0] == 2)
		$fmess = $lang_poll['M1'];
	else if ($top[2] > $top[3])
		$fmess = sprintf($lang_poll['M2'], $top[2]);
	else if ($can_visi && $info['isGuest'])
		$fmess = $lang_poll['M3'];
	else if ($info['isGuest'])
		$fmess = $lang_poll['M4'];
	else if (poll_post('poll_view') != null)
		$fmess = '<a href="javascript:history.go(-1)">'.$lang_common['Go back'].'</a>';
	else if (!$can_vote)
		$fmess = $lang_poll['M0'];

	$questions = $info['questions'];
	$choices = $info['choices'];
	$types = $info['type'];
	$votes = $info['votes'];

	if ($can_vote && !$prev)
	{
?>
<div id="poll_form">
<form method="post" action="viewtopic.php?id=<?php echo $tid.($top[4] > 1 ? '&amp;p='.$top[4] : '') ?>">
<?php
	}
	$amax = array();
	
	foreach($questions as $k => $question)
	{
		$choice = $choices[$k];
		$vote = $votes[$k];
		$amax[$k] = count($choice);

		$max = 0;
		foreach ($vote as $v)
		{
			if ($v > $max) $max = $v;
		}
		$maxPercent = ($top[3] == 0 || !$max) ? 1 : 100 * $max / $top[3];
?>
<?php if ($can_vote && !$prev): ?>
	<input type="hidden" name="poll_max[<?php echo $k ?>]" value="<?php echo $amax[$k] ?>" />
<?php endif ?>
	<fieldset class="poll">
		<p><?php echo pun_htmlspecialchars($question) ?></p>
<?php if ($can_vote && $types[$k]>1): ?>
		<div class="poss"><?php printf($lang_poll['Possible choose'], $types[$k]) ?></div>
<?php endif ?>
		<ol>
<?php
		foreach ($choice as $i => $ch)
		{
			if (empty($ch)) continue;

			$percent = ($top[3] == 0) ? 0 : round(100 * $vote[$i] / $top[3],2);
?>
			<li>
<?php
			if ($can_vote)
			{
				if ($types[$k] < 2)
					echo "\t\t\t\t\t".'<label><input type="radio" name="poll_vote['.$k.'][0]" value="'.$i.'" /> '.pun_htmlspecialchars($ch).'</label>';
				else
					echo "\t\t\t\t\t".'<label><input type="checkbox" name="poll_vote['.$k.']['.$i.']" value="1" /> '.pun_htmlspecialchars($ch).'</label>';
			}
			else if ($can_visi)
			{
				echo "\t\t\t\t\t".'<span class="answer">'.pun_htmlspecialchars($ch).'</span><span class="percent">('.$lang_poll['Votes'].$vote[$i].' ['.$percent.'%])</span>';
				echo '<p class="progressbar"><span style="width: '.round(100 * $percent / $maxPercent).'%;"><span>'.$percent.'%</span></span> </p>';
			}
			else
			{
				echo "\t\t\t\t\t".'<span class="answer">'.pun_htmlspecialchars($ch).'</span>';
			}
?>

			</li>
<?php
		}
?>
		</ol>
		<div class="total"><?php printf($lang_poll['Vote total'], $top[3]) ?></div>
	</fieldset>
<?php
	}
	if ($can_vote && !$prev)
	{
		$csrf = pun_hash($tid.(pun_hash($uid.count($questions).implode('0',$types))).get_remote_address().implode('.',$amax));
		foreach ($types as $i => $type)
		{
?>
	<input type="hidden" name="poll_type[<?php echo $i ?>]" value="<?php echo $type ?>" />
<?php
		}
?>
	<input type="hidden" name="poll_ques" value="<?php echo count($questions) ?>" />
	<input type="hidden" name="poll_csrf" value="<?php echo $csrf ?>" />
	<p class="pollbut"><input type="submit" name="poll_submit" value="<?php echo $lang_poll['Vote button'] ?>" /><?php echo (($can_visi && $top[3] > 0) ? '<input type="submit" name="poll_view" value="'.$lang_poll['View'].'" />' : '') ?></p>
</form>
</div>
<?php
	}
	else if (!empty($fmess))
		echo "\t".'<p class="poll_mess">'.$fmess.'</p>'."\n";
}

// голосуем ********************************************************************
function poll_vote($tid, $uid)
{
	global $db;

	if (poll_bad() || !poll_can_vote($tid, $uid)) poll_mess('Err1');

	$csrf = poll_post('poll_csrf');
	$ques = poll_post('poll_ques');
	$type = poll_post('poll_type');
	$votes = poll_post('poll_vote');
	$amax = poll_post('poll_max');

	if (is_null($csrf) || is_null($ques) || is_null($type) || is_null($votes) || is_null($amax)) poll_mess('Err2');

	if (!is_array($type) || !is_array($votes) || !is_array($amax)) poll_mess('Err2');

	$type = array_map('intval', $type);
	$amax = array_map('intval', $amax);
	$ques = intval($ques);
	
	$csrf2 = pun_hash($tid.(pun_hash($uid.$ques.implode('0',$type))).get_remote_address().implode('.',$amax));
	
	if ($csrf2 != $csrf) poll_mess('Err2');

	$kol = 0;
	foreach($votes as $k => $vote)
	{
		if ($k < 1 || $k > $ques) poll_mess('Err3');
		$kol++;
		$kk = 0;
		$vote = array_map('intval', $vote);
		foreach($vote as $i => $vo)
		{
			if ($type[$k] < 2 && $i != 0) poll_mess('Err2');
			if ($type[$k] < 2 && $vo < 1) poll_mess('Err2');
			if ($type[$k] < 2 && $vo > $amax[$k]) poll_mess('Err2');
			if ($type[$k] > 1 && $i == 0) poll_mess('Err2');
			if ($type[$k] > 1 && $i > $amax[$k]) poll_mess('Err2');
			if ($type[$k] > 1 && $vo != 1) poll_mess('Err2');
			$kk++;
		}
		if ($type[$k] < 2 && $kk != 1) poll_mess('Err4', $k);
		if ($type[$k] > 1 && ($kk < 1 || $kk > $type[$k])) poll_mess('Err5', $k, $type[$k]);
	}
	if ($kol != $ques) poll_mess('Err6');

	$arr = array();
	foreach($votes as $k => $vote)
	{
		$vote = array_map('intval', $vote);
		foreach($vote as $i => $vo)
		{
			if ($type[$k] < 2) $j = $vo;
			else $j = $i;
			$arr[] = '(question='.$k.' AND field='.$j.')';
    }
	}

	if (!empty($arr))
		$db->query('UPDATE '.$db->prefix.'poll SET votes=votes+1 WHERE tid='.$tid.' AND ('.implode(' OR ', $arr).')') or error('Unable to update poll choice', __FILE__, __LINE__, $db->error());

	$db->query('INSERT INTO '.$db->prefix.'poll_voted (tid, uid, rez) VALUES ('.$tid.','.$uid.',\''.$db->escape(serialize($votes)).'\')') or error('Unable to save vote', __FILE__, __LINE__, $db->error());
	$db->query('UPDATE '.$db->prefix.'topics SET poll_kol=poll_kol+1 WHERE id='.$tid) or error('Unable to update topic', __FILE__, __LINE__, $db->error());
	
	poll_cache_delete($tid);
}
Back to Top