/
functions.inc
2060 lines (1775 loc) · 74.6 KB
/
functions.inc
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// | Polls Plugin 2.1 |
// +---------------------------------------------------------------------------+
// | functions.inc |
// | |
// | This file does two things: 1) it implements the necessary Geeklog Plugin |
// | API method and 2) implements all the common code needed by the Polls |
// | plugin' PHP files. |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2000-2017 by the following authors: |
// | |
// | Authors: Tony Bibbs - tony AT tonybibbs DOT com |
// | Tom Willett - twillett AT users DOT sourceforge DOT net |
// | Blaine Lang - langmail AT sympatico DOT ca |
// | Dirk Haun - dirk AT haun-online DOT de |
// +---------------------------------------------------------------------------+
// | |
// | This program is free software; you can redistribute it and/or |
// | modify it under the terms of the GNU General Public License |
// | as published by the Free Software Foundation; either version 2 |
// | of the License, or (at your option) any later version. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software Foundation, |
// | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
// | |
// +---------------------------------------------------------------------------+
/**
* Implementation of the Plugin API for the Polls plugin
*
* @package Polls
*/
if (stripos($_SERVER['PHP_SELF'], basename(__FILE__)) !== false) {
die('This file can not be used on its own.');
}
$plugin_path = $_CONF['path'] . 'plugins/polls/';
/**
* Language file include
*/
$langfile = $plugin_path . 'language/' . $_CONF['language'] . '.php';
if (file_exists($langfile)) {
require_once $langfile;
} else {
require_once $plugin_path . 'language/english.php';
}
Language::override(array(
'LANG_POLLS', 'LANG25', 'PLG_polls_MESSAGE15', 'PLG_polls_MESSAGE19', 'PLG_polls_MESSAGE20',
));
/**
* Check and see if we need to load the plugin configuration
*/
if (!isset($_PO_CONF['pollsloginrequired'])) {
require_once $_CONF['path_system'] . 'classes/config.class.php';
$po_config = config::get_instance();
$_PO_CONF = $po_config->get_config('polls');
}
// +---------------------------------------------------------------------------+
// | Geeklog Plugin API Implementations |
// +---------------------------------------------------------------------------+
/**
* Returns the items for this plugin that should appear on the main menu
* NOTE: this MUST return the url/value pairs in the following format
* $<arrayname>[<label>] = <url>
*/
function plugin_getmenuitems_polls()
{
global $_CONF, $_PO_CONF, $LANG_POLLS;
if (($_PO_CONF['hidepollsmenu'] == 1) || (COM_isAnonUser() &&
($_CONF['loginrequired'] || $_PO_CONF['pollsloginrequired']))
) {
return false;
}
$menuitems[$LANG_POLLS['polls']] = $_CONF['site_url'] . '/polls/index.php';
return $menuitems;
}
/**
* Poll saves a comment
*
* @param string $title comment title
* @param string $comment comment text
* @param string $id Item id to which $cid belongs
* @param int $pid comment parent
* @param string $postmode 'html' or 'text'
* @return mixed false for failure, HTML string (redirect?) for success
*/
function plugin_savecomment_polls($title, $comment, $id, $pid, $postmode)
{
global $_CONF, $_TABLES, $LANG03;
$retval = '';
$commentcode = DB_getItem($_TABLES['polltopics'], 'commentcode',
"pid = '$id'");
if ($commentcode != 0) {
COM_redirect($_CONF['site_url'] . '/index.php');
}
$ret = CMT_saveComment($title, $comment, $id, $pid, 'polls', $postmode);
if ($ret > 0) { // failure //FIXME: some failures should not return to comment form
$retval .= CMT_commentForm($title, $comment, $id, $pid, 'polls',
$LANG03[14], $postmode);
if (!defined('COMMENT_ON_SAME_PAGE')) {
$retval = COM_createHTMLDocument($retval, array('pagetitle' => $LANG03[1]));
} else {
if (!COMMENT_ON_SAME_PAGE) {
$retval = COM_createHTMLDocument($retval, array('pagetitle' => $LANG03[1]));
}
}
} else { // success
COM_redirect($_CONF['site_url'] . "/polls/index.php?pid=$id");
}
return $retval;
}
/**
* polls: delete a comment
*
* @param int $cid Comment to be deleted
* @param string $id Item id to which $cid belongs
* @return mixed false for failure, HTML string (redirect?) for success
*/
function plugin_deletecomment_polls($cid, $id)
{
global $_CONF, $_TABLES, $_USER;
$retval = '';
$has_editPermissions = SEC_hasRights('polls.edit');
$result = DB_query(
"SELECT owner_id,group_id,perm_owner,perm_group,perm_members,perm_anon FROM {$_TABLES['polltopics']} "
. "WHERE pid = '" . DB_escapeString($id) . "' "
);
$A = DB_fetchArray($result);
if ($has_editPermissions && SEC_hasAccess($A['owner_id'],
$A['group_id'], $A['perm_owner'], $A['perm_group'],
$A['perm_members'], $A['perm_anon']) == 3
) {
CMT_deleteComment($cid, $id, 'polls');
COM_redirect($_CONF['site_url'] . "/polls/index.php?pid=$id&aid=-1");
} else {
COM_errorLog("User {$_USER['username']} (IP: {$_SERVER['REMOTE_ADDR']}) "
. "tried to illegally delete comment $cid from poll $id");
COM_redirect($_CONF['site_url'] . '/index.php');
}
return $retval;
}
/**
* Helper function: count number of polls and total number of votes
*
* @return array(number of polls, number of votes);
*/
function POLLS_countPollsAndVotes()
{
global $_TABLES;
$total_answers = 0;
$result = DB_query("SELECT COUNT(*) AS count FROM {$_TABLES['polltopics']}" . COM_getPermSQL());
$A = DB_fetchArray($result);
$total_polls = $A['count'];
$result = DB_query("SELECT pid FROM {$_TABLES['polltopics']}" . COM_getPermSQL());
$nrows = DB_numRows($result);
if ($nrows > 0) {
$topics = '';
for ($i = 1; $i <= $nrows; $i++) {
$A = DB_fetchArray($result);
if ($i > 1) {
$topics .= ',';
}
$topics .= "'" . DB_escapeString($A['pid']) . "'";
}
$result = DB_query("SELECT SUM(votes) FROM {$_TABLES['pollanswers']} WHERE pid IN ({$topics})");
$A = DB_fetchArray($result, true);
$total_answers = $A[0];
}
return array($total_polls, $total_answers);
}
/**
* Shows the statistics for the Polls plugin on stats.php.
* If $showsitestats is 1 then we are to only print the overall stats in the
* 'site statistics box' otherwise we show the detailed stats
*
* @param int $showsitestats Flag to let us know which stats to get
* @return string
*/
function plugin_showstats_polls($showsitestats)
{
global $_CONF, $_TABLES, $LANG_POLLS;
require_once($_CONF['path_system'] . 'lib-admin.php');
$retval = '';
$result = DB_query("SELECT pid,topic,voters FROM {$_TABLES['polltopics']} WHERE (voters > 0)" . COM_getPermSQL('AND') . " ORDER BY voters DESC LIMIT 10");
$nrows = DB_numRows($result);
if ($nrows > 0) {
$header_arr = array(
array(
'text' => $LANG_POLLS['stats_topics'],
'field' => 'pid',
'header_class' => 'stats-header-title',
),
array(
'text' => $LANG_POLLS['stats_votes'],
'field' => 'voters',
'header_class' => 'stats-header-count',
'field_class' => 'stats-list-count',
),
);
$data_arr = array();
$text_arr = array(
'has_menu' => false,
'title' => $LANG_POLLS['stats_top10'],
'form_url' => $_CONF['site_url'] . '/stats.php',
);
for ($i = 0; $i < $nrows; $i++) {
$A = DB_fetchArray($result);
$url = $_CONF['site_url'] . '/polls/index.php?pid=' . $A['pid']
. '&aid=-1';
$pid = COM_createLink($A['topic'], $url);
$voters = COM_numberFormat($A['voters']);
$data_arr[] = array('pid' => $pid, 'voters' => $voters);
}
$retval .= ADMIN_simpleList('', $header_arr, $text_arr, $data_arr);
} else {
$retval .= COM_startBlock($LANG_POLLS['stats_top10']);
$retval .= $LANG_POLLS['stats_none'];
$retval .= COM_endBlock();
}
return $retval;
}
/**
* New stats plugin API function for proper integration with the site stats
*
* @return array(item text, item count);
*/
function plugin_statssummary_polls()
{
global $LANG_POLLS;
list($total_polls, $total_answers) = POLLS_countPollsAndVotes();
$item_count = COM_numberFormat($total_polls)
. ' (' . COM_numberFormat($total_answers) . ')';
return array($LANG_POLLS['stats_summary'], $item_count);
}
/**
* This will put an option for polls in the command and control block on
* moderation.php
*
* @return array
*/
function plugin_cclabel_polls()
{
global $_CONF, $LANG_POLLS;
if (SEC_hasRights('polls.edit')) {
return array($LANG_POLLS['polls'],
$_CONF['site_admin_url'] . '/plugins/polls/index.php',
plugin_geticon_polls());
}
return array();
}
/**
* returns the administrative option for this plugin
*
* @return array
*/
function plugin_getadminoption_polls()
{
global $_CONF, $_TABLES, $LANG_POLLS;
if (SEC_hasRights('polls.edit')) {
$result = DB_query("SELECT COUNT(*) AS cnt FROM {$_TABLES['polltopics']}" . COM_getPermSQL());
$A = DB_fetchArray($result);
$total_pages = $A['cnt'];
return array($LANG_POLLS['polls'],
$_CONF['site_admin_url'] . '/plugins/polls/index.php',
$total_pages);
} else {
return array();
}
}
/**
* A user is about to be deleted. Update ownership of any polls owned
* by that user or delete them.
*
* @param int $uid User id of deleted user
*/
function plugin_user_delete_polls($uid)
{
global $_TABLES, $_PO_CONF;
if (DB_count($_TABLES['polltopics'], 'owner_id', $uid) == 0) {
// there are no polls owned by this user
return;
}
if ($_PO_CONF['delete_polls'] == 1) {
// delete the polls
$result = DB_query("SELECT pid FROM {$_TABLES['polltopics']} WHERE owner_id = $uid");
$numPolls = DB_numRows($result);
for ($i = 0; $i < $numPolls; $i++) {
list($pid) = DB_fetchArray($result);
DB_delete($_TABLES['pollanswers'], 'pid', DB_escapeString($pid));
DB_delete($_TABLES['pollvoters'], 'pid', DB_escapeString($pid));
DB_delete($_TABLES['comments'], array('sid', 'type'),
array(DB_escapeString($pid), 'polls'));
}
DB_delete($_TABLES['polltopics'], 'owner_id', $uid);
} else {
// assign ownership to a user from the Root group
$rootgroup = DB_getItem($_TABLES['groups'], 'grp_id', "grp_name = 'Root'");
$result = DB_query("SELECT DISTINCT ug_uid FROM {$_TABLES['group_assignments']} WHERE ug_main_grp_id = $rootgroup ORDER BY ug_uid LIMIT 1");
list($rootuser) = DB_fetchArray($result);
DB_query("UPDATE {$_TABLES['polltopics']} SET owner_id = {$rootuser} WHERE owner_id = {$uid}");
}
}
/**
* Return the current version of code.
* Used in the Plugin Editor to show the registered version and code version
*
* @return string
*/
function plugin_chkVersion_polls()
{
global $_CONF;
require_once $_CONF['path'] . 'plugins/polls/autoinstall.php';
$inst_parms = plugin_autoinstall_polls('polls');
return $inst_parms['info']['pi_version'];
}
/**
* Geeklog informs us that we're about to be enabled or disabled
*
* @param boolean $enable true = we're being enabled, false = disabled
* @return void
*/
function plugin_enablestatechange_polls($enable)
{
global $_TABLES;
$is_enabled = $enable ? 1 : 0;
// toggle Poll block
DB_query("UPDATE {$_TABLES['blocks']} SET is_enabled = {$is_enabled} WHERE (type = 'phpblock') AND (phpblockfn = 'phpblock_polls')");
}
/**
* Automatic uninstall function for plugins
*
* @return array
* This code is automatically uninstalling the plugin.
* It passes an array to the core code function that removes
* tables, groups, features and php blocks from the tables.
* Additionally, this code can perform special actions that cannot be
* foreseen by the core code (interactions with other plugins for example)
* @return array
*/
function plugin_autouninstall_polls()
{
$out = array(
/* give the name of the tables, without $_TABLES[] */
'tables' => array('pollanswers', 'polltopics', 'pollvoters', 'pollquestions'),
/* give the full name of the group, as in the db */
'groups' => array('Polls Admin'),
/* give the full name of the feature, as in the db */
'features' => array('polls.edit',
'config.polls.tab_whatsnew',
'config.polls.tab_main',
'config.polls.tab_permissions',
'config.polls.tab_autotag_permissions',
'config.polls.tab_poll_block'),
/* give the full name of the block, including 'phpblock_', etc */
'php_blocks' => array('phpblock_polls'),
/* give all vars with their name */
'vars' => array(),
);
return $out;
}
/**
* Shows a poll form
* Shows an HTML formatted poll for the given topic ID
*
* @param string $pid ID for poll topic
* @param boolean $showall Show only the first question in the poll or all?
* @param int $displaytype Possible values 0 = Normal, 1 = In Block, 2 = autotag
* @param string $order
* @param string $mode
* @param int $page
* @return string HTML Formatted Poll
* @see function COM_pollResults
* @see function COM_showPoll
*/
function POLLS_pollVote($pid, $showall = true, $displaytype = 0, $order = '', $mode = '', $page = 1)
{
global $_CONF, $_TABLES, $_USER, $LANG_POLLS, $LANG01, $LANG25,
$_IMAGE_TYPE;
$retval = '';
$result = DB_query(
"SELECT topic, voters, commentcode, is_open, hideresults, owner_id, group_id, perm_owner, perm_group, perm_anon, perm_members, description FROM {$_TABLES['polltopics']} "
. "WHERE pid = '" . DB_escapeString($pid) . "' " . COM_getPermSQL('AND'));
$ntopics = DB_numRows($result);
if ($ntopics == 0) {
return $retval;
}
$P = DB_fetchArray($result);
if ((!isset($_COOKIE[$pid]) && !POLLS_ipAlreadyVoted($pid)) || ($P['is_open'] == 1)) {
if ($ntopics == 1) {
$questions_sql = "SELECT question, qid, allow_multipleanswers, description FROM {$_TABLES['pollquestions']} "
. "WHERE pid = '" . DB_escapeString($pid) . "' "
. " GROUP BY question, qid, allow_multipleanswers, description "
. " ORDER BY qid ";
$questions = DB_query($questions_sql);
$nquestions = DB_numRows($questions);
if ($nquestions > 0) {
$poll = COM_newTemplate(CTL_plugin_templatePath('polls'));
$poll->set_file(array(
'block' => 'pollblock.thtml',
'comments' => 'pollcomments.thtml',
));
$blocks = array('poll_questions', 'poll_answer');
foreach ($blocks as $block) {
$poll->set_block('block', $block);
}
$poll->set_var('display_type', $displaytype);
$poll->set_var('poll_id', $pid);
$poll->set_var('poll_topic', $P['topic']);
$poll->set_var('poll_topic_description', $P['description']);
$poll->set_var('num_votes', COM_numberFormat($P['voters']));
$poll->set_var('poll_vote_url', $_CONF['site_url']
. '/polls/index.php');
if (($nquestions == 1) || $showall) {
$poll->set_var('lang_vote', $LANG_POLLS['vote']);
} else {
$poll->set_var('lang_vote', $LANG_POLLS['start_poll']);
}
$poll->set_var('lang_voters', $LANG_POLLS['voters']);
$results = '';
if (($P['is_open'] == 0) || ($P['hideresults'] == 0) ||
(($P['hideresults'] == 1) &&
(
SEC_inGroup('Root') ||
(isset($_USER['uid'])
&& ($_USER['uid'] == $P['owner_id']))
))
) {
$results = COM_createLink($LANG_POLLS['results'],
$_CONF['site_url'] . '/polls/index.php?pid=' . $pid
. '&aid=-1');
}
$poll->set_var('poll_results', $results);
$access = SEC_hasAccess($P['owner_id'], $P['group_id'],
$P['perm_owner'], $P['perm_group'],
$P['perm_members'], $P['perm_anon']);
if (($access == 3) && SEC_hasRights('polls.edit')) {
$editlink = COM_createLink($LANG25[27], $_CONF['site_admin_url']
. '/plugins/polls/index.php?mode=edit&pid=' . $pid);
$poll->set_var('edit_link', $editlink);
$editicon = $_CONF['layout_url'] . '/images/edit.' . $_IMAGE_TYPE;
$icon_attr = array('title' => $LANG25[27]);
$editiconhtml = COM_createImage($editicon, $LANG25[27], $icon_attr);
$editlink = COM_createLink($editiconhtml, $_CONF['site_admin_url']
. '/plugins/polls/index.php?mode=edit&pid=' . $pid);
$poll->set_var('edit_icon', $editlink);
}
$aid = Geeklog\Input::fPost('aid', array());
for ($j = 0; $j < $nquestions; $j++) {
$Q = DB_fetchArray($questions);
$poll->set_var('poll_question', $Q['question']);
$poll->set_var('question_id', $j);
$poll->set_var('poll_description', $Q['description']);
if ($Q['allow_multipleanswers'] == 1) {
$poll->set_var('input_type', 'checkbox');
$checked = ' checked';
} else {
$poll->set_var('input_type', 'radio');
$checked = ' checked="checked"';
}
$notification = '';
$filter = '';
if (!$showall) {
$nquestions--;
if ($nquestions > 0) {
$notification = $LANG25[35] . " $nquestions "
. $LANG25[36];
}
$filter = "AND qid='0'";
$nquestions = 1;
} else {
$filter = "AND qid = '{$Q['qid']}'";
$poll->set_var('lang_question_number', " " . ($j + 1) . ":");
}
$answer_sql = "SELECT answer,aid FROM {$_TABLES['pollanswers']} "
. "WHERE pid = '" . DB_escapeString($pid) . "' {$filter} "
. "ORDER BY qid, aid";
$answers = DB_query($answer_sql);
$nanswers = DB_numRows($answers);
for ($i = 0; $i < $nanswers; $i++) {
$A = DB_fetchArray($answers);
if (($j < count($aid)) && ($aid[$j] == $A['aid'])) {
$poll->set_var('selected', ' checked');
}
$poll->set_var('answer_id', $A['aid']);
$poll->set_var('answer_text', $A['answer']);
$poll->parse('poll_answers', 'poll_answer', true);
$poll->clear_var('selected');
}
$poll->parse('poll_questions', 'poll_questions', true);
$poll->clear_var('poll_answers');
}
if ($nquestions > 1) {
$poll->set_var('poll_topic', $LANG25['34'] . " " . $P['topic']);
$poll->set_var('lang_question', $LANG25[31]);
}
$poll->set_var('lang_polltopics', $LANG_POLLS['polltopics']);
$poll->set_var('poll_notification', $notification);
if ($P['commentcode'] >= 0) {
$num_comments = DB_count($_TABLES['comments'],
array('sid', 'type'),
array(DB_escapeString($pid), 'polls')
);
$poll->set_var('num_comments',
COM_numberFormat($num_comments));
$poll->set_var('lang_comments', $LANG01[3]);
$poll->set_var('poll_comments_url', $_CONF['site_url'] .
'/polls/index.php?pid=' . $pid . '#comments');
$poll->parse('poll_comments', 'comments', true);
} else {
$poll->set_var('poll_comments', '');
$poll->set_var('poll_comments_url', '');
}
$retval = $poll->finish($poll->parse('output', 'block')) . LB;
if ($showall && ($P['commentcode'] >= 0 AND $displaytype != 2)) {
$delete_option = (SEC_hasRights('polls.edit') &&
SEC_hasAccess($P['owner_id'], $P['group_id'],
$P['perm_owner'], $P['perm_group'], $P['perm_members'],
$P['perm_anon']) == 3 ? true : false);
require_once $_CONF['path_system'] . 'lib-comment.php';
$retval .= CMT_userComments($pid, $P['topic'], 'polls',
$order, $mode, 0, $page, false,
$delete_option, $P['commentcode']);
}
}
}
} else {
$retval .= POLLS_pollResults($pid, 0, '', '', 1, $displaytype);
}
return $retval;
}
/**
* This shows a poll
* This will determine if a user needs to see the poll form OR the poll result.
*
* @param int $size Size in pixels of poll results
* @param string $pid Poll topic ID to show (optional)
* @param boolean $showall Show only the first question or all
* @param int $displaytype Possible values 0 = Normal, 1 = In Block, 2 = autotag
* @return string HTML formatted string of poll
* @see function COM_pollVote
* @see function COM_pollResults
*/
function POLLS_showPoll($size, $pid = '', $showall = false, $displaytype = 0)
{
global $_TABLES, $_PO_CONF, $LANG_POLLS;
$retval = '';
DB_query("DELETE FROM {$_TABLES['pollvoters']} WHERE date < UNIX_TIMESTAMP() - {$_PO_CONF['polladdresstime']}");
if (!empty($pid)) {
$Q['is_open'] = DB_getItem($_TABLES['polltopics'], 'is_open', "pid = '" . DB_escapeString($pid) . "'");
if (($displaytype == 2) && ($Q['is_open'] == 0)) {
$retval = '<div class="poll-autotag-message">'
. $LANG_POLLS['pollclosed'] . '</div>';
}
if (!isset($_COOKIE['poll-' . $pid]) && !POLLS_ipAlreadyVoted($pid) &&
($Q['is_open'] == 1)
) {
$retval .= POLLS_pollVote($pid, $showall, $displaytype);
} else {
$retval .= POLLS_pollResults($pid, $size, '', '', 1, $displaytype);
}
} else {
$result = DB_query(
"SELECT pid, topic, is_open FROM {$_TABLES['polltopics']} "
. " WHERE display = 1 " . COM_getPermSQL('AND')
. " ORDER BY created DESC"
);
$nrows = DB_numRows($result);
$title = DB_getItem($_TABLES['blocks'], 'title', "name='poll_block'");
if ($nrows > 0) {
for ($i = 1; $i <= $nrows; $i++) {
$Q = DB_fetchArray($result);
$pid = $Q['pid'];
if ($displaytype == 1) { // in the poll block
$showall = false;
} else { // assume we're in polls/index.php
$retval .= COM_startBlock($title);
$showall = true;
}
if (!isset($_COOKIE['poll-' . $pid]) &&
!POLLS_ipAlreadyVoted($pid) && ($Q['is_open'] == 1)
) {
$retval .= POLLS_pollVote($pid, $showall, $displaytype);
} else {
$retval .= POLLS_pollResults($pid, $size, '', '', 1,
$displaytype);
}
if ($displaytype == 1) { // in the poll block
if (($i < $nrows) && !empty($retval)) {
$retval .= '<div class="poll-divider"></div>';
}
} else {
$retval .= COM_endBlock();
}
}
}
}
return $retval;
}
/**
* Saves a user's vote
* Saves the users vote, if allowed for the poll $pid.
* NOTE: all data comes from form post
*
* @param string $pid poll id
* @param array $aid selected answers
* @return string HTML for poll results
*/
function POLLS_pollsave($pid, $aid)
{
global $_TABLES, $LANG_POLLS;
$retval = '';
if (POLLS_ipAlreadyVoted($pid)) {
exit;
}
DB_change($_TABLES['polltopics'], 'voters', "voters + 1", 'pid', DB_escapeString($pid), '', true);
// This call to DB-change will properly supress the insertion of quotes around $value in the sql
foreach ($aid as $qid => $avalueary) {
foreach ($avalueary as $j => $avalue) {
$avalue = COM_applyFilter($avalue, true);
DB_change(
$_TABLES['pollanswers'],
'votes',
"votes + 1",
array('pid', 'qid', 'aid'),
array(DB_escapeString($pid), DB_escapeString($qid), $avalue),
'',
true
);
}
}
// This always does an insert so no need to provide key_field and key_value args
DB_query(
"INSERT INTO {$_TABLES['pollvoters']} (ipaddress, date, pid) "
. "VALUES ('" . DB_escapeString($_SERVER['REMOTE_ADDR']) . "', " . time() . ", '" . DB_escapeString($pid) . "')");
$retval .= COM_showMessageText(
$LANG_POLLS['savedvotemsg'] . ' "'
. DB_getItem($_TABLES['polltopics'], 'topic', "pid = '" . DB_escapeString($pid) . "'") . '"',
$LANG_POLLS['savedvotetitle']
)
. POLLS_pollResults($pid);
return $retval;
}
/**
* Shows the results of a poll
* Shows the poll results for a given poll topic
*
* @param string $pid ID for poll topic to show
* @param int $scale Size in pixels to scale formatted results to
* @param string $order 'ASC' or 'DESC' for Comment ordering (SQL statment ordering)
* @param string $mode Comment Mode possible values 'nocomment', 'flat', 'nested', 'threaded'
* @param int $displaytype Possible values 0 = Normal, 1 = In Block, 2 = autotag
* @see POLLS_pollVote
* @see POLLS_showPoll
* @return string HTML Formated Poll Results
*/
function POLLS_pollResults($pid, $scale = 100, $order = '', $mode = '', $page = 1, $displaytype = 0)
{
global $_CONF, $_TABLES, $_USER, $_IMAGE_TYPE,
$_PO_CONF, $LANG01, $LANG_POLLS, $_COM_VERBOSE, $LANG25;
$retval = '';
$topic_sql = "SELECT topic, voters, is_open, hideresults, commentcode, owner_id, group_id, perm_owner, perm_group, perm_members, perm_anon, description FROM {$_TABLES['polltopics']} "
. "WHERE pid = '" . DB_escapeString($pid) . "' ";
$result = DB_query($topic_sql);
$P = DB_fetchArray($result);
if (SEC_hasAccess($P['owner_id'], $P['group_id'], $P['perm_owner'],
$P['perm_group'], $P['perm_members'], $P['perm_anon']) == 0
) {
return $retval;
}
if ((
$P['hideresults'] == 0
) || (
($P['hideresults'] == 1) && (isset($_USER['uid']) &&
($_USER['uid'] == $P['owner_id']))
) || (
($P['hideresults'] == 1) && (SEC_inGroup('Root'))
)
) {
$retval = '';
} else {
if ($displaytype == 2) {
$retval = '<div class="poll-autotag-message">' . $LANG_POLLS['pollhidden'] . "</div>";
}
return $retval;
}
$ntopic = DB_numRows($result);
if ($ntopic == 1) {
$questions_sql = "SELECT qid,pid,question,description FROM {$_TABLES['pollquestions']} "
. "WHERE pid= '" . DB_escapeString($pid) . "' "
. "ORDER BY qid";
$questions = DB_query($questions_sql);
$nquestions = DB_numRows($questions);
if ($_COM_VERBOSE) {
COM_errorLog("got $questions questions in COM_pollResults", 1);
}
if ($nquestions > 0) {
$poll = COM_newTemplate(CTL_plugin_templatePath('polls'));
$poll->set_file(array(
'result' => 'pollresult.thtml',
'comments' => 'pollcomments.thtml'
));
$blocks = array('poll_question', 'poll_votes_bar', 'poll_votes_num');
foreach ($blocks as $block) {
$poll->set_block('result', $block);
}
$poll->set_var('display_type', $displaytype);
$poll->set_var('poll_topic', $P['topic']);
$poll->set_var('poll_topic_description', $P['description']);
$poll->set_var('poll_id', $pid);
$poll->set_var('num_votes', COM_numberFormat($P['voters']));
$poll->set_var('lang_voters', $LANG_POLLS['voters']);
$access = SEC_hasAccess($P['owner_id'], $P['group_id'],
$P['perm_owner'], $P['perm_group'],
$P['perm_members'], $P['perm_anon']
);
if (($access == 3) && SEC_hasRights('polls.edit')) {
$editlink = COM_createLink($LANG25[27], $_CONF['site_admin_url']
. '/plugins/polls/index.php?mode=edit&pid=' . $pid);
$poll->set_var('edit_link', $editlink);
$editicon = $_CONF['layout_url'] . '/images/edit.' . $_IMAGE_TYPE;
$icon_attr = array('title' => $LANG25[27]);
$editiconhtml = COM_createImage($editicon, $LANG25[27], $icon_attr);
$editlink = COM_createLink($editiconhtml, $_CONF['site_admin_url']
. '/plugins/polls/index.php?mode=edit&pid=' . $pid);
$poll->set_var('edit_icon', $editlink);
}
if ($_PO_CONF['answerorder'] == 'voteorder') {
$answerorder = "votes DESC";
} else {
$answerorder = "aid";
}
for ($j = 0; $j < $nquestions; $j++) {
$poll->set_var('question_id', $j);
$Q = DB_fetchArray($questions);
if ($nquestions >= 1) {
$counter = ($j + 1) . "/$nquestions: ";
}
$poll->set_var('poll_question_text', $counter . $Q['question']);
$poll->set_var('poll_description', $counter . $Q['description']);
$answer_sql = "SELECT votes, answer, remark FROM {$_TABLES['pollanswers']} "
. "WHERE pid = '" . DB_escapeString($pid) . "' AND qid = '" . DB_escapeString($Q['qid']) . "' "
. "ORDER BY {$answerorder}";
$answers = DB_query($answer_sql);
$nanswers = DB_numRows($answers);
$a_summ_sql = "SELECT SUM(votes) as votesumm FROM {$_TABLES['pollanswers']} "
. "WHERE pid = '" . DB_escapeString($pid) . "' AND qid = '" . DB_escapeString($Q['qid']) . "' "
. "GROUP BY qid";
$a_summ = DB_query($a_summ_sql);
$S = DB_fetchArray($a_summ);
if ($_COM_VERBOSE) {
COM_errorLog("got $answers answers in COM_pollResults", 1);
}
for ($i = 1; $i <= $nanswers; $i++) {
$A = DB_fetchArray($answers);
if ($S['votesumm'] == 0) {
$percent = 0;
} else {
$percent = $A['votes'] / $S['votesumm'];
}
$poll->set_var('cssida', 1);
$poll->set_var('cssidb', 2);
$poll->set_var('answer_text', $A['answer']);
$poll->set_var('remark_text', $A['remark']);
$poll->set_var('answer_counter', $i);
$poll->set_var('answer_odd', (($i - 1) % 2));
$poll->set_var('answer_num', COM_numberFormat($A['votes']));
$poll->set_var('answer_percent',
sprintf('%.2f', $percent * 100));
if ($scale < 50) {
$poll->parse('poll_votes', 'poll_votes_num', true);
} else {
$width = (int) ($percent * $scale);
$poll->set_var('bar_width', $width);
$poll->parse('poll_votes', 'poll_votes_bar', true);
}
}
$poll->parse('poll_questions', 'poll_question', true);
$poll->clear_var('poll_votes');
if (($scale < 100) && ($j < 1)) {
$url = $_CONF['site_url'] . "/polls/index.php?pid=$pid";
$poll->set_var('notification', COM_createLink($LANG25[40], $url) . "<br" . XHTML . ">");
break;
}
}
if ($P['commentcode'] >= 0) {
$num_comments = DB_count($_TABLES['comments'],
array('sid', 'type'), array(DB_escapeString($pid), 'polls'));
$poll->set_var('num_comments',
COM_numberFormat($num_comments));
$poll->set_var('lang_comments', $LANG01[3]);
$poll->set_var('poll_comments_url', $_CONF['site_url'] .
'/polls/index.php?pid=' . $pid . '#comments');
$poll->parse('poll_comments', 'comments', true);
} else {
$poll->set_var('poll_comments_url', '');
$poll->set_var('poll_comments', '');
}
$poll->set_var('lang_polltopics', $LANG_POLLS['polltopics']);
$retval .= $poll->finish($poll->parse('output', 'result'));
if ($scale > 99 && $P['commentcode'] >= 0 && $displaytype != 2) {
$delete_option = (SEC_hasRights('polls.edit') &&
SEC_hasAccess($P['owner_id'], $P['group_id'],
$P['perm_owner'], $P['perm_group'], $P['perm_members'],
$P['perm_anon']) == 3 ? true : false);
require_once $_CONF['path_system'] . 'lib-comment.php';
$retval .= CMT_userComments($pid, $P['topic'], 'polls',
$order, $mode, 0, $page, false,
$delete_option, $P['commentcode']
);
}
}
}
return $retval;
}
/**
* Check if we already have a vote from this IP address
*
* @param string $pid Poll ID
* @param string $ip (optional) IP address
* @return boolean true: IP already voted; false: didn't
*/
function POLLS_ipAlreadyVoted($pid, $ip = '')
{
global $_TABLES;
$retval = false;
if (empty($ip)) {
$ip = $_SERVER['REMOTE_ADDR'];
}
if (DB_count($_TABLES['pollvoters'],
array('ipaddress', 'pid'),
array(DB_escapeString($ip), DB_escapeString($pid))) > 0
) {
$retval = true;
}
return $retval;
}
/**
* Display the current poll(s) in a side block
*
* @return string HTML for the poll(s) to be displayed (or an empty string)
*/
function phpblock_polls()
{
return POLLS_showPoll(0, '', false, 1);
}
/**
* Returns the URL of the plugin's icon
*
* @return string URL of the icon
*/
function plugin_geticon_polls()
{
global $_CONF;
return $_CONF['site_url'] . '/polls/images/polls.png';
}
/**
* Plugin should display [a] comment[s]
*
* @param string $id Unique idenifier for item comment belongs to
* @param int $cid Comment id to display (possibly including sub-comments)
* @param string $title Page/comment title
* @param string $order 'ASC' or 'DESC' or blank
* @param string $format 'threaded', 'nested', or 'flat'
* @param int $page Page number of comments to display
* @param boolean $view True to view comment (by cid), false to display (by $pid)
* @return mixed results of calling the plugin_displaycomment_ function
*/
function plugin_displaycomment_polls($id, $cid, $title, $order, $format, $page, $view)
{
global $_TABLES, $LANG_ACCESS, $LANG_POLLS;