api.guestbook.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. define('RCMS_GB_DEFAULT_FILE', DF_PATH . 'guestbook.dat');
  13. define('RCMS_MC_DEFAULT_FILE', DF_PATH . 'minichat.dat');
  14. $_CACHE['gbook'] = array();
  15. function guestbook_get_msgs($page = 0, $parse = true, $limited = true, $file = RCMS_GB_DEFAULT_FILE, $config = 'guestbook.ini') {
  16. global $_CACHE, $system;
  17. $config = parse_ini_file(CONFIG_PATH . $config);
  18. $data = &$_CACHE['gbook'][$file];
  19. if(!isset($data)) {
  20. if (!is_readable($file) || !($data = unserialize(file_get_contents($file)))) $data = array();
  21. }
  22. if(!empty($data)){
  23. $c = sizeof($data);
  24. $ndata = $rdata = array();
  25. foreach ($data as $key => $value) $ndata[$key . ''] = $value;
  26. $ndata = array_reverse($ndata, true);
  27. if($page !== null){
  28. $i = 0;
  29. while($i < (($page+1) * $system->config['perpage']) && $el = each($ndata)){
  30. if($i >= $page * $system->config['perpage']) $rdata[$el['key']] = $el['value'];
  31. $i++;
  32. }
  33. } else {
  34. $rdata = $ndata;
  35. }
  36. if($parse){
  37. foreach($rdata as $id => $msg){
  38. if(!empty($msg)) $rdata[$id]['text'] = rcms_parse_text($msg['text'], !$limited, false, !$limited);
  39. }
  40. }
  41. return $rdata;
  42. } else return array();
  43. }
  44. function guestbook_get_last_msgs($num = 10, $parse = true, $limited, $file = RCMS_GB_DEFAULT_FILE, $config = 'guestbook.ini') {
  45. global $_CACHE, $system;
  46. $config = parse_ini_file(CONFIG_PATH . $config);
  47. $data = &$_CACHE['gbook'][$file];
  48. if(!isset($data)) {
  49. if (!is_readable($file) || !($data = unserialize(file_get_contents($file)))) $data = array();
  50. }
  51. if(!empty($data)){
  52. $ndata = $rdata = array();
  53. foreach ($data as $key => $value) $ndata[$key . ''] = $value;
  54. $ndata = array_reverse($ndata, true);
  55. if($num !== null){
  56. $i = 0;
  57. while($i < $num && $el = each($ndata)){
  58. $rdata[$el['key']] = $el['value'];
  59. $i++;
  60. }
  61. } else {
  62. $rdata = $ndata;
  63. }
  64. if($parse){
  65. foreach($rdata as $id => $msg){
  66. if(!empty($msg)) {
  67. $rdata[$id]['text'] = rcms_parse_text($msg['text'], !$limited, false, !$limited);
  68. }
  69. }
  70. }
  71. return $rdata;
  72. } else return array();
  73. }
  74. function guestbook_get_pages_num($file = RCMS_GB_DEFAULT_FILE, $config = 'guestbook.ini') {
  75. global $_CACHE, $system;
  76. $config = parse_ini_file(CONFIG_PATH . $config);
  77. $data = &$_CACHE['gbook'][$file];
  78. if(!isset($data)) {
  79. if (!is_readable($file) || !($data = unserialize(file_get_contents($file)))) $data = array();
  80. }
  81. if(!empty($system->config['perpage'])) {
  82. return ceil(sizeof($data)/$system->config['perpage']);
  83. } else return 1;
  84. }
  85. function guestbook_post_msg($username, $nickname, $text, $file = RCMS_GB_DEFAULT_FILE, $config = 'guestbook.ini') {
  86. global $_CACHE;
  87. $text = trim($text);
  88. if(empty($text)) return false;
  89. $config = parse_ini_file(CONFIG_PATH . $config);
  90. $data = &$_CACHE['gbook'][$file];
  91. if(!isset($data)) {
  92. if (!is_readable($file) || !($data = unserialize(file_get_contents($file)))) $data = array();
  93. }
  94. if(!empty($config['max_db_size'])) $data = array_slice($data, -$config['max_db_size']+1);
  95. $newmesg['username'] = $username;
  96. $newmesg['nickname'] = (!empty($config['max_word_len']) && strlen($nickname) > $config['max_word_len']) ? '<abbr title="' . htmlspecialchars($nickname) . '">' . substr($nickname, 0, $config['max_word_len']) . '</abbr>' : htmlspecialchars($nickname);
  97. $newmesg['time'] = rcms_get_time();
  98. $newmesg['text'] = (strlen($text) > $config['max_message_len']) ? substr($text, 0, $config['max_message_len']) : $text;
  99. $data[] = $newmesg;
  100. return file_write_contents($file, serialize($data));
  101. }
  102. function guestbook_post_remove($id, $file = RCMS_GB_DEFAULT_FILE, $config = 'guestbook.ini') {
  103. global $_CACHE;
  104. $config = parse_ini_file(CONFIG_PATH . $config);
  105. $data = &$_CACHE['gbook'][$file];
  106. if(!isset($data)) {
  107. if (!is_readable($file) || !($data = unserialize(file_get_contents($file)))) $data = array();
  108. }
  109. rcms_remove_index($id, $data, true);
  110. return file_write_contents($file, serialize($data));
  111. }
  112. ?>