00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00031 class Comments_XML extends Comments
00032 {
00033 var $comments;
00034
00035 function loadComments($file, $for)
00036 {
00037 global $settings;
00038
00039 if (is_null($this->minixml))
00040 {
00041 $this->minixml = $settings->attribute("minixml_path");
00042 }
00043 require_once ($this->minixml."/minixml.inc.php");
00044
00045 $doc = new MiniXMLDoc();
00046 $doc->fromFile($file);
00047 $root =& $doc->getRoot();
00048
00049 $comments =& $root->getElement("comments");
00050 if (!method_exists($comments, "numChildren"))
00051 {
00052 return;
00053 }
00054 for ($c = 0; $c < $comments->numChildren(); $c++)
00055 {
00056 $comment =& $comments->xchildren[$c];
00057 $foru = trim($comment->attribute("for"));
00058 if ($foru != $for && $for != "@all")
00059 {
00060 continue;
00061 }
00062 for ($o = 0; $o < count($comment->xchildren); $o++)
00063 {
00064 $child =& $comment->xchildren[$o];
00065 $data[trim($child->name())] = trim($child->text());
00066 }
00067 $this->comments[] = $data;
00068 }
00069 }
00070
00071 function canAdd($file, $for)
00072 {
00073 if (!is_writable($file))
00074 {
00075 return false;
00076 }
00077 return true;
00078 }
00079
00080 function addComment($file, $data)
00081 {
00082 global $settings;
00083 if (is_null($this->minixml))
00084 {
00085 $this->minixml = $settings->attribute("minixml_path");
00086 }
00087 require_once ($this->minixml."/minixml.inc.php");
00088 $doc = new MiniXMLDoc();
00089 $doc->fromFile($file);
00090 $root =& $doc->getRoot();
00091 $comments =& $root->getElement("comments");
00092
00093
00094
00095 $ids = array();
00096 for ($c = 0; $c < count($comments->xchildren); $c++)
00097 {
00098 $comment =& $comments->xchildren[$c];
00099 $id = $comment->attribute("id");
00100 $ids[] = trim($id);
00101 $msg =& $comment->getElement("message");
00102 $msgs[] = md5(trim($msg->text()));
00103 }
00104 if (is_array($msgs))
00105 {
00106 $msgmd5 = md5($data['message']);
00107 if (in_array($msgmd5, $msgs))
00108 {
00109 return MESSAGE_ALREADY_EXISTS;
00110 }
00111 }
00112
00113 $try = 0;
00114 while ($try < 5)
00115 {
00116 $id = md5(time());
00117 if (!in_array($id, $ids))
00118 {
00119 $try = 10;
00120 }
00121 $try++;
00122 }
00123 if ($try == 10)
00124 {
00125 return ID_GENERATE_ERROR;
00126 }
00127
00128 $data['id'] = $id;
00129
00130 $comment =& $comments->createChild("comment");
00131 $comment->attribute("for", $data['for']);
00132 $comment->attribute("id", $data['id']);
00133
00134 $keys = array_keys($data);
00135 for ($o = 0; $o < count($data); $o++)
00136 {
00137 $var = $keys[$o];
00138 if ($var == "id" || $var == "for")
00139 {
00140 continue;
00141 }
00142 $child =& $comment->createChild($var);
00143 $child->text($data[$var]);
00144 }
00145
00146 $fp = fopen($file, "w");
00147 if ($fp)
00148 {
00149 fputs($fp, $doc->toString());
00150 fclose($fp);
00151 }
00152 return COMMENT_ADD_SUCCESS;
00153 }
00154 }
00155 ?>