/voting/inc/voting.class.inc.php

https://github.com/rhempen/cms · PHP · 305 lines · 202 code · 56 blank · 47 comment · 50 complexity · 0ac76c83d279e67f941caf283dddfc12 MD5 · raw file

  1. <?php
  2. /**
  3. * Title........: Voting Class
  4. * Filename.....: voting.class.inc.php
  5. * Author.......: Ralf Stadtaus
  6. * Homepage.....: http://www.stadtaus.com/
  7. * Contact......: http://www.stadtaus.com/forum/
  8. * Version......: 0.3
  9. * Notes........:
  10. * Last changed.: 2004-03-18
  11. * Last change..: IP check
  12. */
  13. /**
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY
  15. * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
  16. * LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  20. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  21. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  22. * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. class Voting
  26. {
  27. /**
  28. * Log all downloads - new line for each download
  29. */
  30. function log($log_path, $vote_name, $intern_value, $extern_value)
  31. {
  32. $email_address = '';
  33. if (isset($_POST['email_address'])) {
  34. $email_address = str_replace(array("\n", "\r"), '', $_POST['email_address']);
  35. }
  36. $log_content = array(
  37. get_ip(), // (Required) IP address of the remote host
  38. @gethostbyaddr(get_ip()), // (Required) Name of the remote host
  39. date("Y-m-d (H:i)", mktime()), // (Required) Date of the download (in international ISO format)
  40. mktime(), // (Required) Date of the download (in Unix timestamp)
  41. $vote_name, // (Required) Internal vote name
  42. $intern_value, // (Required) Internal voting option number
  43. $extern_value, // (Optional) Actual voting option name
  44. $email_address, // (Optional) e-mail address
  45. getenv('HTTP_REFERER'), // (Optional) Referring URL
  46. getenv('HTTP_USER_AGENT'), // (Optional) User agent
  47. );
  48. $log_content = join(' - ', $log_content);
  49. debug_mode($log_content, 'Log Entry');
  50. if ($logfile = fopen($log_path, 'a')) {
  51. flock($logfile, 2);
  52. if (fputs($logfile, $log_content . "\n")) {
  53. fclose($logfile);
  54. return 1;
  55. }
  56. fclose($logfile);
  57. }
  58. }
  59. /**
  60. * Content of the count file
  61. */
  62. function count_content($file_name, $download_number, $first_download, $last_download)
  63. {
  64. $log_content = array($file_name,/* Name of the download file */
  65. $download_number + 1,/* Adds 1 to current number of downloads */
  66. date("Y-m-d (H:i)", $first_download),/* Date of the first download (in international ISO format) */
  67. date("Y-m-d (H:i)", $last_download),/* Date of the last download (in international ISO format) */
  68. $first_download,/* Date of the first download in Unix timestamp */
  69. $last_download/* Date of the last download in Unix timestamp */
  70. );
  71. $log_content = join(' - ', $log_content);
  72. return $log_content;
  73. }
  74. /**
  75. * Reads the numbers of downloads and calculates the
  76. * new download number.
  77. */
  78. function count($log_path, $download_path, $file_name)
  79. {
  80. unset($log_template_content);
  81. $query_file_name = trim($file_name);
  82. $current_time = mktime();
  83. if (is_file($log_path)) {
  84. $count_file_content = file($log_path);
  85. $log_template_content = array();
  86. while (list($key, $line) = each($count_file_content)) {
  87. $line = trim($line);
  88. if (!empty($line)) {
  89. $data = explode(' - ', $line);
  90. $stored_file_name = trim($data[0]);
  91. if (trim($data[0]) == trim($file_name)) {
  92. $download_number = trim($data[1]);
  93. $first_download = trim($data[4]);
  94. $last_download = mktime();
  95. $log_template_content_temp = $this->count_content($stored_file_name, $download_number, $first_download, $last_download);
  96. $log_template_content[] = $log_template_content_temp;
  97. debug_mode($log_template_content_temp, 'Replace Entry');
  98. unset($log_template_content_temp);
  99. $check = 'true';
  100. } else {
  101. $log_template_content[] = $line;
  102. }
  103. }
  104. }
  105. if (!isset($check) or $check != 'true') {
  106. $log_template_content_temp = $this->count_content($query_file_name, 0, $current_time, $current_time);
  107. $log_template_content[] = $log_template_content_temp;
  108. debug_mode($log_template_content_temp, 'New Entry');
  109. unset($log_template_content_temp);
  110. }
  111. $new_file_content = join("\n", $log_template_content);
  112. if ($logfile = fopen($log_path, 'w+')) {
  113. flock($logfile, 2);
  114. fputs($logfile, $new_file_content);
  115. fclose($logfile);
  116. }
  117. } else {
  118. $log_template_content = $this->count_content($query_file_name, 0, mktime(), mktime());
  119. debug_mode($log_template_content, 'First Entry');
  120. if ($logfile = fopen($log_path, 'a')) {
  121. flock($logfile, 2);
  122. fputs ($logfile, $log_template_content . "\n");
  123. fclose ($logfile);
  124. }
  125. }
  126. }
  127. /**
  128. * Get number of votes
  129. */
  130. function get_vote_result($log_path, $vote_name)
  131. {
  132. if (!is_file($log_path)) {
  133. return false;
  134. }
  135. $handle = fopen($log_path, "r");
  136. if (!$handle) {
  137. return false;
  138. }
  139. $log_template_content = array();
  140. $rating = 0;
  141. while (!feof($handle))
  142. {
  143. $line = fgets($handle, 4096);
  144. $line = trim($line);
  145. if (!empty($line)) {
  146. $data = explode(' - ', $line);
  147. $intern_value = trim($data[5]);
  148. if ($vote_name == trim($data[4])) {
  149. if (isset($option[$intern_value])) {
  150. $option[$intern_value]++;
  151. } else {
  152. $option[$intern_value] = 1;
  153. }
  154. $rating_value = trim($data[6]);
  155. if (is_numeric($rating_value)) {
  156. $rating += $rating_value;
  157. }
  158. $check = 'true';
  159. }
  160. }
  161. }
  162. fclose($handle);
  163. if (isset($check) and $check == 'true') {
  164. return array('voting' => $option, 'rating' => $rating);
  165. }
  166. }
  167. /**
  168. * Check last voting time by ip address
  169. */
  170. function check_ip_address($log_path, $name, $time, $ip)
  171. {
  172. if (!is_file($log_path)) {
  173. return false;
  174. }
  175. $handle = fopen($log_path, "r");
  176. if (!$handle) {
  177. return false;
  178. }
  179. $log_template_content = array();
  180. while (!feof($handle))
  181. {
  182. $line = fgets($handle, 4096);
  183. $line = trim($line);
  184. if (!empty($line)) {
  185. $data = explode(' - ', $line);
  186. // echo trim($data[4]);
  187. if ($name == trim($data[4]) and trim($data[3]) >= $time and $ip == trim($data[0])) {
  188. return trim($data[0]);
  189. }
  190. }
  191. }
  192. }
  193. /**
  194. * Check last voting time by e-mail address
  195. */
  196. function check_email_address($log_path, $name, $time, $email_address)
  197. {
  198. if (!is_file($log_path)) {
  199. return false;
  200. }
  201. $handle = fopen($log_path, "r");
  202. if (!$handle) {
  203. return false;
  204. }
  205. $log_template_content = array();
  206. while (!feof($handle))
  207. {
  208. $line = fgets($handle, 4096);
  209. $line = trim($line);
  210. if (!empty($line)) {
  211. $data = explode(' - ', $line);
  212. if ($name == trim($data[4]) and trim($data[3]) >= $time and $email_address == trim($data[7])) {
  213. return trim($data[7]);
  214. }
  215. }
  216. }
  217. }
  218. /**
  219. * Referrer check
  220. */
  221. function check_referrer()
  222. {
  223. global $allow_empty_referrer, $allowed_referrers;
  224. // Skip empty referrers
  225. if ($allow_empty_referrer == 'yes'
  226. and getenv('HTTP_REFERER') == '') {
  227. return true;
  228. }
  229. if ($allow_empty_referrer != 'yes'
  230. and getenv('HTTP_REFERER') == '') {
  231. return false;
  232. }
  233. $http_referrer = parse_url(getenv('HTTP_REFERER'));
  234. $http_referrer = trim(str_replace('www.', '', strtolower($http_referrer['host'])));
  235. if ($allowed_referrers != null) {
  236. $referrers = explode (',', $allowed_referrers);
  237. } else {
  238. $referrers = array();
  239. }
  240. $referrers[] = getenv('HTTP_HOST');
  241. foreach ($referrers AS $key => $val)
  242. {
  243. $allowed = trim(preg_replace('/^www\./', '', strtolower($val)));
  244. if ($allowed == $http_referrer) {
  245. return true;
  246. }
  247. }
  248. }
  249. } // End of class