4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2004 The Nucleus Group
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * (see nucleus/documentation/index.html#license for more info)
13 * A class representing a single comment
15 * $Id: COMMENT.php,v 1.1.1.1 2005-02-28 07:14:10 kimitake Exp $
20 * Returns the requested comment (static)
22 function getComment($commentid) {
23 $query = 'SELECT cnumber as commentid, cbody as body, cuser as user, cmail as userid, cmember as memberid, ctime, chost as host, mname as member, cip as ip, cblog as blogid'
24 . ' FROM '.sql_table('comment').' left outer join '.sql_table('member').' on cmember=mnumber'
25 . ' WHERE cnumber=' . intval($commentid);
26 $comments = sql_query($query);
28 $aCommentInfo = mysql_fetch_assoc($comments);
31 $aCommentInfo['timestamp'] = strtotime($aCommentInfo['ctime']);
37 * prepares a comment to be saved
40 function prepare($comment) {
41 $comment['user'] = strip_tags($comment['user']);
42 $comment['userid'] = strip_tags($comment['userid']);
44 // remove quotes and newlines from user and userid
45 $comment['user'] = strtr($comment['user'], "\'\"\n",'-- ');
46 $comment['userid'] = strtr($comment['userid'], "\'\"\n",'-- ');
48 $comment['body'] = COMMENT::prepareBody($comment['body']);
53 // prepares the body of a comment (static)
54 function prepareBody($body) {
56 // remove newlines when too many in a row
57 $body = ereg_replace("\n.\n.\n","\n",$body);
59 // encode special characters as entities
60 $body = htmlspecialchars($body);
62 // trim away whitespace and newlines at beginning and end
66 $body = addBreaks($body);
68 // create hyperlinks for http:// addresses
69 // there's a testcase for this in /build/testcases/urllinking.txt
71 '/([^:\/\/\w]|^)((https:\/\/)([a-z0-9_\.-]+)([\/a-z0-9_+\.~%&?@=_:;#,-]+))/ie',
72 '/([^:\/\/\w]|^)((http:\/\/|www\.)([a-z0-9_\.-]+)([\/a-z0-9_+\.~%&?@=_:;#,-]+))/ie',
73 '/([^:\/\/\w]|^)((ftp:\/\/|ftp\.)([a-z0-9_\.-]+)([\/a-z0-9_+\.~%&?@=_:;#,-]+))/ie',
74 '/([^:\/\/\w]|^)(mailto:(([a-zA-Z\@\%\.\-\+_])+))/ie'
77 'COMMENT::createLinkCode("\\1", "\\2","https")',
78 'COMMENT::createLinkCode("\\1", "\\2","http")',
79 'COMMENT::createLinkCode("\\1", "\\2","ftp")',
80 'COMMENT::createLinkCode("\\1", "\\3","mailto")'
82 $body = preg_replace($replaceFrom, $replaceTo, $body);
87 function createLinkCode($pre, $url, $protocol = 'http') {
90 // it's possible that $url ends contains entities we don't want,
91 // since htmlspecialchars is applied _before_ URL linking
92 // move the part of URL, starting from the disallowed entity to the 'post' link part
93 $aBadEntities = array('"', '>', '<');
94 foreach ($aBadEntities as $entity)
96 $pos = strpos($url, $entity);
99 $post = substr($url, $pos) . $post;
100 $url = substr($url, 0, $pos);
105 // remove entities at end (&&&&)
106 if (preg_match('/(&\w+;)+$/i', $url, $matches)) {
107 $post = $matches[0] . $post; // found entities (1 or more)
108 $url = substr($url, 0, strlen($url) - strlen($post));
111 // move ending comma from url to 'post' part
112 if (substr($url, strlen($url) - 1) == ',')
114 $url = substr($url, 0, strlen($url) - 1);
118 if (!ereg('^'.$protocol.'://',$url))
119 $linkedUrl = $protocol . (($protocol == 'mailto') ? ':' : '://') . $url;
124 if ($protocol != 'mailto')
125 $displayedUrl = $linkedUrl;
127 $displayedUrl = $url;
128 return $pre . '<a href="'.$linkedUrl.'">'.shorten($displayedUrl,30,'...').'</a>' . $post;