Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

comments_xml.php

Go to the documentation of this file.
00001 <?php
00002 /* $Id: comments_xml.php,v 1.1 2003/10/03 15:28:41 mikko Exp $ */
00003 /*
00004  * ImaComm - a web based photo album software
00005  * Copyright (C) 2003, Mikko Kokkonen
00006  *
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or (at
00010  * your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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                 // Load all IDs...
00094                 // And all messages, calculate MD5s, that same msg wont be twice!
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                 // Calculate a new ID...
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                 // We have ID...
00128                 $data['id'] = $id;
00129                 // Create a new children..
00130                 $comment =& $comments->createChild("comment");
00131                 $comment->attribute("for", $data['for']);
00132                 $comment->attribute("id", $data['id']);
00133                 // And the other data...
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                 // And save file...
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 ?>

Generated on Sun Oct 19 11:08:00 2003 for ImaComm by doxygen1.3