Source for file poll.php

Documentation is available at poll.php

  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////////
  3. // Copyright (C) ReloadCMS Development Team //
  4. // http://reloadcms.sf.net //
  5. // //
  6. // This program is distributed in the hope that it will be useful, //
  7. // but WITHOUT ANY WARRANTY, without even the implied warranty of //
  8. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. //
  9. // //
  10. // This product released under GNU General Public License v2 //
  11. ////////////////////////////////////////////////////////////////////////////////
  12.  
  13. define('RCMS_POLL_DATAFILE', DF_PATH . 'poll.dat');
  14. define('RCMS_POLL_ARCHIVE_DATAFILE', DF_PATH . 'poll-archive.dat');
  15.  
  16. class polls{
  17. var $polls_file = RCMS_POLL_DATAFILE;
  18. var $old_polls_file = RCMS_POLL_ARCHIVE_DATAFILE;
  19. var $current = array();
  20. var $old = array();
  21. var $lasterror = '';
  22. var $copened = false;
  23. var $oopened = false;
  24. function openCurrentPolls(){
  25. if(file_exists($this->polls_file) && !is_readable($this->polls_file)) {
  26. $this->lasterror = $this->polls_file . ' ' . __('is not readable');
  27. return false;
  28. }
  29. if(file_exists($this->polls_file)){
  30. if(($file = unserialize(file_get_contents($this->polls_file))) === false) {
  31. $this->lasterror = __('Cannot parse') . ' ' . $this->polls_file;
  32. return false;
  33. }
  34. } else $file = array();
  35. $this->current = $file;
  36. $this->copened = true;
  37. }
  38. function openArchivedPolls(){
  39. if(file_exists($this->old_polls_file) && !is_readable($this->old_polls_file)) {
  40. $this->lasterror = $this->old_polls_file . ' ' . __('is not readable');
  41. return false;
  42. }
  43. if(file_exists($this->old_polls_file)){
  44. if(!($file = unserialize(file_get_contents($this->old_polls_file)))) {
  45. $this->lasterror = __('Cannot parse') . ' ' . $this->old_polls_file;
  46. return false;
  47. }
  48. } else $file = array();
  49. $this->old = $file;
  50. $this->oopened = true;
  51. }
  52. function close($uc = true, $uo = true){
  53. if($uc) {
  54. if(!$this->copened) $this->openCurrentPolls();
  55. $a = file_write_contents($this->polls_file, serialize($this->current));
  56. }
  57. if($uo) {
  58. if(!$this->oopened) $this->openArchivedPolls();
  59. $b = file_write_contents($this->old_polls_file, serialize($this->old));
  60. }
  61. if($uc && $uo) {
  62. return $a && $b;
  63. } elseif ($uo) {
  64. return $b;
  65. } elseif ($uc) {
  66. return $a;
  67. } else {
  68. return true;
  69. }
  70. }
  71. function getCurrentPolls(){
  72. if(!$this->copened) $this->openCurrentPolls();
  73. $return = array();
  74. foreach ($this->current as $id => $data){
  75. $data['t'] = 0;
  76. foreach ($data['c'] as $v_id => $v_total){
  77. $data['t'] += $v_total;
  78. }
  79. foreach ($data['c'] as $v_id => $v_total){
  80. if($data['t'] != 0) $data['p'][$v_id] = round(($v_total/$data['t'])*100); else $data['p'][$v_id] = 0;
  81. }
  82. $data['X'] = $data['c'];
  83. natsort($data['X']);
  84. $data['X'] = array_reverse($data['X'], true);
  85. $return[$id] = $data;
  86. }
  87. return $return;
  88. }
  89. function getArchivedPolls(){
  90. if(!$this->oopened) $this->openArchivedPolls();
  91. $return = array();
  92. foreach ($this->old as $id => $data){
  93. $data['t'] = 0;
  94. foreach ($data['c'] as $v_id => $v_total){
  95. $data['t'] += $v_total;
  96. }
  97. foreach ($data['c'] as $v_id => $v_total){
  98. if($data['t'] != 0) $data['p'][$v_id] = round(($v_total/$data['t'])*100); else $data['p'][$v_id] = 0;
  99. }
  100. $data['X'] = $data['c'];
  101. natsort($data['X']);
  102. $data['X'] = array_reverse($data['X'], true);
  103. $return[$id] = $data;
  104. }
  105. return $return;
  106. }
  107. function startPoll($question, $answers){
  108. if(empty($question) || empty($answers)) {
  109. $this->lasterror = __('Empty question or no variants');
  110. return false;
  111. }
  112. if(!$this->copened) $this->openCurrentPolls();
  113. $data['q'] = $question;
  114. foreach (explode("\n", preg_replace("/[\n\r]+/", "\n", $answers)) as $variant) {
  115. if(!empty($variant)) {
  116. $data['v'][] = $variant;
  117. $data['c'][] = 0;
  118. }
  119. }
  120. $this->current[rcms_random_string(8)] = $data;
  121. return true;
  122. }
  123.  
  124. function removePoll($id, $archive = true){
  125. if(!$this->copened) $this->openCurrentPolls();
  126. if($archive) $this->archivePoll($id, $this->current[$id]);
  127. $new = array();
  128. foreach ($this->current as $key => $value) {
  129. if($key != $id) $new[$key] = $value;
  130. }
  131. $this->current = $new;
  132. return true;
  133. }
  134.  
  135. function removePollFromArchive($id){
  136. if(!$this->oopened) $this->openArchivedPolls();
  137. $new = array();
  138. foreach ($this->old as $key => $value) {
  139. if($key != $id) $new[$key] = $value;
  140. }
  141. $this->old = $new;
  142. return true;
  143. }
  144. function archivePoll($id, $data){
  145. if(!$this->oopened) $this->openArchivedPolls();
  146. $this->old[$id] = $data;
  147. return true;
  148. }
  149. function voteInPoll($poll, $answer, $ip){
  150. if(!$this->copened) $this->openCurrentPolls();
  151. if(empty($this->current[$poll])) {
  152. $this->lasterror = __('No poll with this id');
  153. return false;
  154. }
  155. if(!isset($this->current[$poll]['c'][$answer])) {
  156. $this->lasterror = __('This answer does not exists in this poll');
  157. return false;
  158. }
  159. if($this->isVotedInPoll($poll, $ip)) {
  160. $this->lasterror = __('You already voted in this poll');
  161. return false;
  162. }
  163. $this->current[$poll]['c'][$answer]++;
  164. $this->current[$poll]['ips'][] = $ip;
  165. setcookie('reloadcms_poll[' . $poll . ']', $poll, FOREVER_COOKIE);
  166. return true;
  167. }
  168. function isVotedInPoll($poll, $ip){
  169. if(!$this->copened) $this->openCurrentPolls();
  170. if(empty($this->current[$poll])) {
  171. return true;
  172. }
  173. if(@is_array($this->current[$poll]['ips']) && in_array($ip, $this->current[$poll]['ips'])) {
  174. return true;
  175. }
  176. if(!empty($_COOKIE['reloadcms_poll']) && is_array($_COOKIE['reloadcms_poll']) && in_array($poll, $_COOKIE['reloadcms_poll'])) {
  177. return true;
  178. }
  179. return false;
  180. }
  181. }
  182. ?>

Documentation generated on Fri, 08 Jun 2007 12:21:26 +0300 by phpDocumentor 1.3.0RC3