4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2007 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
18 * Returns the requested comment
22 function getComment($commentid) {
23 $query = 'SELECT `cnumber` AS commentid, `cbody` AS body, `cuser` AS user, `cmail` AS userid, `cemail` AS email, `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 = sql_fetch_assoc($comments);
31 $aCommentInfo['timestamp'] = strtotime($aCommentInfo['ctime']);
38 * Prepares a comment to be saved
42 function prepare($comment)
\r
44 $comment['user'] = strip_tags($comment['user']);
45 $comment['userid'] = strip_tags($comment['userid']);
46 $comment['email'] = strip_tags($comment['email']);
48 // remove newlines from user; remove quotes and newlines from userid and email; trim whitespace from beginning and end
49 $comment['user'] = trim(strtr($comment['user'], "\n", ' ') );
50 $comment['userid'] = trim(strtr($comment['userid'], "\'\"\n", '-- ') );
51 $comment['email'] = trim(strtr($comment['email'], "\'\"\n", '-- ') );
53 // begin if: a comment userid is supplied, but does not have an "http://" or "https://" at the beginning - prepend an "http://"
54 if ( !empty($comment['userid']) && (strpos($comment['userid'], 'http://') !== 0) && (strpos($comment['userid'], 'https://') !== 0) )
\r
56 $comment['userid'] = 'http://' . $comment['userid'];
59 $comment['body'] = COMMENT::prepareBody($comment['body']);
65 * Prepares the body of a comment
69 function prepareBody($body) {
71 # replaced ereg_replace() below with preg_replace(). ereg* functions are deprecated in PHP 5.3.0
72 # original ereg_replace: ereg_replace("\n.\n.\n", "\n", $body);
74 // convert Windows and Mac style 'returns' to *nix newlines
75 $body = preg_replace("/\r\n/", "\n", $body);
76 $body = preg_replace("/\r/", "\n", $body);
78 // then remove newlines when too many in a row (3 or more newlines get converted to 1 newline)
79 $body = preg_replace("/\n{3,}/", "\n\n", $body);
81 // encode special characters as entities
82 $body = htmlspecialchars($body);
84 // trim away whitespace and newlines at beginning and end
88 $body = addBreaks($body);
90 // create hyperlinks for http:// addresses
91 // there's a testcase for this in /build/testcases/urllinking.txt
93 $replace_from = array(
\r
94 '/([^:\/\/\w]|^)((https:\/\/)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',
\r
95 '/([^:\/\/\w]|^)((http:\/\/|www\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',
\r
96 '/([^:\/\/\w]|^)((ftp:\/\/|ftp\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',
\r
97 '/([^:\/\/\w]|^)(mailto:(([a-zA-Z\@\%\.\-\+_])+))/i'
\r
100 $body = preg_replace_callback($replace_from, array('self', 'prepareBody_cb'), $body);
108 * Creates a link code for unlinked URLs with different protocols
112 function createLinkCode($pre, $url, $protocol = 'http') {
115 // it's possible that $url ends contains entities we don't want,
116 // since htmlspecialchars is applied _before_ URL linking
117 // move the part of URL, starting from the disallowed entity to the 'post' link part
118 $aBadEntities = array('"', '>', '<');
119 foreach ($aBadEntities as $entity) {
121 $pos = strpos($url, $entity);
124 $post = substr($url, $pos) . $post;
125 $url = substr($url, 0, $pos);
130 // remove entities at end (&&&&)
131 if (preg_match('/(&\w+;)+$/i', $url, $matches) ) {
132 $post = $matches[0] . $post; // found entities (1 or more)
133 $url = substr($url, 0, strlen($url) - strlen($post) );
136 // move ending comma from url to 'post' part
137 if (substr($url, strlen($url) - 1) == ',') {
138 $url = substr($url, 0, strlen($url) - 1);
142 # replaced ereg() below with preg_match(). ereg* functions are deprecated in PHP 5.3.0
143 # original ereg: ereg('^' . $protocol . '://', $url)
145 if (!preg_match('#^' . $protocol . '://#', $url) )
147 $linkedUrl = $protocol . ( ($protocol == 'mailto') ? ':' : '://') . $url;
154 if ($protocol != 'mailto') {
155 $displayedUrl = $linkedUrl;
157 $displayedUrl = $url;
160 return $pre . '<a href="' . $linkedUrl . '" rel="nofollow">' . shorten($displayedUrl,30,'...') . '</a>' . $post;
165 * This method is a callback for creating link codes
\r
166 * @param array $match
\r
169 function prepareBody_cb($match)
\r
171 if ( !preg_match('/^[a-z]+/i', $match[2], $protocol) )
\r
176 switch( strtolower($protocol[0]) )
\r
179 return self::createLinkCode($match[1], $match[2], 'https');
\r
183 return self::createLinkCode($match[1], $match[2], 'ftp');
\r
187 return self::createLinkCode($match[1], $match[3], 'mailto');
\r
191 return self::createLinkCode($match[1], $match[2], 'http');
\r