rate.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. $writed = false;
  13. function show_rate($for) {
  14. $arr=array();
  15. if (file_exists(RATE_PATH.$for)) {
  16. $arr = unserialize(file_get_contents(RATE_PATH.$for));
  17. }
  18. $cou=count($arr);
  19. $sum=0;
  20. foreach ($arr as $ip => $rate) {
  21. $sum.=$rate;
  22. }
  23. $res=0;
  24. $ret='<table width=100% cellspacing="0" cellpadding="0"><tr width=100%>';
  25. if ($cou != 0) $res = bcdiv($sum,$cou);
  26. $desc = __('Poor');
  27. if ($res == '1') $desc = __('Poor');
  28. if ($res == '2') $desc = __('Fair');
  29. if ($res == '3') $desc = __('Average');
  30. if ($res == '4') $desc = __('Very Good');
  31. if ($res == '5') $desc = __('Excelent');
  32. $txt='&nbsp;'.__('Rate').'('.$cou.'): '.$desc;
  33. for ($i=0;$i<5;$i++) {
  34. if ($i < $res) {
  35. $ret.= '<td class="rate-fill">'.$txt.'</td>';
  36. } else {
  37. $ret.= '<td class="rate-empty">'.$txt.'</td>';
  38. }
  39. $txt='&nbsp;';
  40. }
  41. $ret.='</tr></table>';
  42. return $ret;
  43. }
  44. function show_rate_rbox($for) {
  45. return '<center><form method="POST" name="'.$for.'"><input type="hidden" name="for" value="'.$for.'">'.__('Rate').':&nbsp;&nbsp;&nbsp;'.
  46. '<input type="radio" class = "rate-radio" name="val" value="1">'.__('Poor').'&nbsp;&nbsp;&nbsp;'.
  47. '<input type="radio" class = "rate-radio" name="val" value="2">'.__('Fair').'&nbsp;&nbsp;&nbsp;'.
  48. '<input type="radio" class = "rate-radio" name="val" value="3">'.__('Average').'&nbsp;&nbsp;&nbsp;'.
  49. '<input type="radio" class = "rate-radio" name="val" value="4">'.__('Very Good').'&nbsp;&nbsp;&nbsp;'.
  50. '<input type="radio" class = "rate-radio" name="val" value="5" checked>'.__('Excelent').'&nbsp;&nbsp;&nbsp;'.
  51. '<input type="submit" value="'.__('Submit').'"></form></center>';
  52. }
  53. function write_rate($for,$val,$ip) {
  54. global $write;
  55. if (!$write) {
  56. $arr=array();
  57. if (file_exists(RATE_PATH.$for)) {
  58. $arr = unserialize(file_get_contents(RATE_PATH.$for));
  59. }
  60. $arr[$ip] = $val;
  61. file_write_contents(RATE_PATH.$for, serialize($arr));
  62. }
  63. }
  64. function check_ip ($for,$ip) {
  65. if (file_exists(RATE_PATH.$for)) {
  66. $arr = unserialize(file_get_contents(RATE_PATH.$for));
  67. }
  68. return isset($arr[$ip]);
  69. }
  70. function get_rate($for){
  71. $valid_post=false;
  72. $b_start='<div class="rate"> ';
  73. $b_end=' </div>';
  74. if (trim(rcms_parse_text($for)) != '') {
  75. $valid_post = true;
  76. $for = md5(trim(rcms_parse_text($for)));
  77. }
  78. $post_for = 0;
  79. if (isset($_POST['for'])) {
  80. $post_for = $_POST['for'];
  81. }
  82. $valid_val = 0;
  83. if (isset($_POST['val'])) {
  84. if (($_POST['val'] == '5') or ($_POST['val'] == '4') or ($_POST['val'] == '3') or ($_POST['val'] == '2') or ($_POST['val'] == '1')) {
  85. $valid_val = $_POST['val'];
  86. }
  87. }
  88. if (!$valid_post) {
  89. return $b_start.__('Rate').': '.__('Only for registered users').$b_end;
  90. }
  91. if (!LOGGED_IN) {
  92. return $b_start.show_rate($for).$b_end;;
  93. }
  94. if ($post_for == $for and $valid_val == 0 and $valid_post and !check_ip($for,$_SERVER['REMOTE_ADDR'])) {
  95. return $b_start.show_rate_rbox($for).$b_end;
  96. }
  97. if ($post_for == $for and $valid_val == 0 and check_ip($for,$_SERVER['REMOTE_ADDR'])) {
  98. return $b_start.show_rate($for).$b_end;
  99. }
  100. if ($post_for == $for and $valid_val !== 0 and $valid_post and !check_ip($for,$_SERVER['REMOTE_ADDR'])) {
  101. write_rate($for,$valid_val,$_SERVER['REMOTE_ADDR']);
  102. return $b_start.show_rate($for).$b_end;
  103. }
  104. if ($post_for !== $for and $valid_post and !check_ip($for,$_SERVER['REMOTE_ADDR'])) {
  105. return $b_start.show_rate_rbox($for).$b_end;
  106. }
  107. if ($post_for !== $for and $valid_post and check_ip($for,$_SERVER['REMOTE_ADDR'])) {
  108. return $b_start.show_rate($for).$b_end;
  109. }
  110. return $b_start.__('Rate').': '.__('Data not valid').$b_end;
  111. }
  112. ?>