/shoutbox/class/file.php

https://github.com/cesagonchu/shoutbox · PHP · 150 lines · 112 code · 16 blank · 22 comment · 11 complexity · 63d0638aa7a2a19e9a4e100c924ed2e5 MD5 · raw file

  1. <?php
  2. /*
  3. You may not change or alter any portion of this comment or credits
  4. of supporting developers from this source code or any supporting source code
  5. which is considered copyrighted (c) material of the original comment or credit authors.
  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. /**
  11. * Shoutbox class
  12. *
  13. * @copyright The XUUPS Project http://sourceforge.net/projects/xuups/
  14. * @license http://www.fsf.org/copyleft/gpl.html GNU public license
  15. * @package Shoutbox
  16. * @since 5.0
  17. * @author trabis <lusopoemas@gmail.com>
  18. * @version $Id: file.php 0 2010-01-06 18:47:04Z trabis $
  19. */
  20. defined("XOOPS_ROOT_PATH") or die("XOOPS root path not defined");
  21. class ShoutboxFile extends XoopsObject
  22. {
  23. /**
  24. * constructor
  25. */
  26. function ShoutboxFile()
  27. {
  28. $this->initVar("id", XOBJ_DTYPE_INT);
  29. $this->initVar("uid", XOBJ_DTYPE_INT);
  30. $this->initVar("uname", XOBJ_DTYPE_TXTBOX);
  31. $this->initVar("time", XOBJ_DTYPE_STIME);
  32. $this->initVar("ip", XOBJ_DTYPE_TXTBOX);
  33. $this->initVar("message", XOBJ_DTYPE_TXTAREA);
  34. $this->initVar("dohtml", XOBJ_DTYPE_INT, 0);
  35. $this->initVar("doxcode", XOBJ_DTYPE_INT, 0);
  36. $this->initVar("dosmiley", XOBJ_DTYPE_INT, 1);
  37. $this->initVar("doimage", XOBJ_DTYPE_INT, 1);
  38. $this->initVar("dobr", XOBJ_DTYPE_INT, 0);
  39. }
  40. function time($dateFormat = 's', $format = 'S')
  41. {
  42. return formatTimestamp($this->getVar('time', $format), $dateFormat);
  43. }
  44. }
  45. class ShoutboxFileHandler extends XoopsPersistableObjectHandler
  46. {
  47. var $csvfile;
  48. function ShoutboxFileHandler(&$db)
  49. {
  50. $this->__construct($db);
  51. }
  52. function __construct($db)
  53. {
  54. $this->csvfile = XOOPS_ROOT_PATH . '/uploads/shoutbox/shout.csv';
  55. parent::__construct($db, '', 'ShoutboxFile', 'id', 'uid');
  56. }
  57. function createShout()
  58. {
  59. return $this->create();
  60. }
  61. function saveShout($obj)
  62. {
  63. $f = fopen($this->csvfile, 'a');
  64. fwrite($f, $obj->getVar('uname', 'n') . '|' .
  65. $obj->getVar('message', 'n') . '|' .
  66. $obj->getVar('time', 'n') . '|' .
  67. $obj->getVar('ip', 'n') . '|' .
  68. $obj->getVar('uid', 'n') . "\n");
  69. fclose($f);
  70. return true;
  71. }
  72. function getShouts($limit)
  73. {
  74. $objs = array();
  75. $shouts = file($this->csvfile);
  76. $count = count($shouts) - 1;
  77. $i = 0;
  78. for ($count; $count >= 0; $count--) {
  79. if ($limit <= $i) {
  80. break;
  81. }
  82. $oneline = array();
  83. $oneline = explode('|', $shouts[$count]);
  84. $obj = $this->create();
  85. $obj->setVar('uname', $oneline[0]);
  86. $obj->setVar('message', $oneline[1]);
  87. $obj->setVar('time', $oneline[2]);
  88. $obj->setVar('ip' , $oneline[3]);
  89. $obj->setVar('uid', $oneline[4]);
  90. $objs[] =& $obj;
  91. unset($obj);
  92. $i++;
  93. }
  94. return $objs;
  95. }
  96. function pruneShouts($limit)
  97. {
  98. $shouts = file($this->csvfile);
  99. $totrim = count($shouts) - $limit;
  100. if ($totrim > 0) {
  101. for ($i = 0; $i < $totrim; $i++) {
  102. array_shift($shouts);
  103. }
  104. $f = fopen($this->csvfile, 'w');
  105. foreach ($shouts as $i => $line) {
  106. fputs($f, $line);
  107. }
  108. fclose($f);
  109. }
  110. return true;
  111. }
  112. function deleteShouts()
  113. {
  114. $f = fopen($this->csvfile, 'w');
  115. fclose($f);
  116. return true;
  117. }
  118. function shoutExists($message, $ip)
  119. {
  120. $shouts = file($this->csvfile);
  121. if (!empty($shouts)) {
  122. $count = count($shouts) - 1;
  123. $oneline = explode('|', $shouts[$count]);
  124. if (count($oneline) != 0) {
  125. if ($oneline[3] == $ip && $oneline[1] == $message) {
  126. return true;
  127. }
  128. }
  129. }
  130. return false;
  131. }
  132. }
  133. ?>