4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2007 The Nucleus Group
\r
7 * This program is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation; either version 2
\r
10 * of the License, or (at your option) any later version.
\r
11 * (see nucleus/documentation/index.html#license for more info)
\r
14 * A class representing a single comment
\r
16 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
17 * @copyright Copyright (C) 2002-2007 The Nucleus Group
\r
18 * @version $Id: COMMENT.php 1721 2012-03-31 10:18:25Z sakamocchi $
23 * Comment::getComment()
\r
24 * Returns the requested comment
\r
27 * @param integer $commentid id for comment
28 * @return array comment information
\r
31 function getComment($commentid)
\r
33 $query = 'SELECT cnumber AS commentid,'
\r
36 . ' cmail AS userid,'
\r
37 . ' cemail AS email,'
\r
38 . ' cmember AS memberid,'
\r
41 . ' mname AS member,'
\r
43 . ' cblog AS blogid'
\r
44 . ' FROM %s LEFT OUTER JOIN %s ON cmember = mnumber'
\r
45 . ' WHERE cnumber = %d;';
\r
47 $query = sprintf($query, sql_table('comment'), sql_table('member'), (integer) $commentid);
\r
48 $aCommentInfo = DB::getRow($query);
\r
52 $aCommentInfo['timestamp'] = strtotime($aCommentInfo['ctime']);
\r
55 return $aCommentInfo;
\r
60 * Prepares a comment to be saved
\r
63 * @param array $comment comment data
64 * @return array comment date
67 function prepare($comment)
\r
69 $comment['user'] = strip_tags($comment['user']);
\r
70 $comment['userid'] = strip_tags($comment['userid']);
\r
71 $comment['email'] = strip_tags($comment['email']);
\r
73 // remove newlines from user; remove quotes and newlines from userid and email; trim whitespace from beginning and end
\r
74 $comment['user'] = trim(strtr($comment['user'], "\n", ' ') );
\r
75 $comment['userid'] = trim(strtr($comment['userid'], "\'\"\n", '-- ') );
\r
76 $comment['email'] = trim(strtr($comment['email'], "\'\"\n", '-- ') );
\r
78 // begin if: a comment userid is supplied, but does not have an "http://" or "https://" at the beginning - prepend an "http://"
\r
79 if ( array_key_exists('userid', $comment)
80 && !empty($comment['userid'])
81 && (i18n::strpos($comment['userid'], 'http://') !== 0)
82 && (i18n::strpos($comment['userid'], 'https://') !== 0) )
84 $comment['userid'] = 'http://' . $comment['userid'];
\r
87 $comment['body'] = Comment::prepareBody($comment['body']);
\r
93 * Comment::prepareBody()
94 * Prepares the body of a comment
\r
97 * @param string $body string for comment body
98 * @return string validate string for comment body
101 function prepareBody($body)
103 # replaced ereg_replace() below with preg_replace(). ereg* functions are deprecated in PHP 5.3.0
\r
104 # original ereg_replace: ereg_replace("\n.\n.\n", "\n", $body);
\r
106 // convert Windows and Mac style 'returns' to *nix newlines
\r
107 $body = preg_replace("/\r\n/", "\n", $body);
\r
108 $body = preg_replace("/\r/", "\n", $body);
\r
110 // then remove newlines when too many in a row (3 or more newlines get converted to 1 newline)
\r
111 $body = preg_replace("/\n{3,}/", "\n\n", $body);
\r
113 // encode special characters as entities
\r
114 $body = Entity::hsc($body);
\r
116 // trim away whitespace and newlines at beginning and end
\r
117 $body = trim($body);
\r
120 $body = addBreaks($body);
\r
122 // create hyperlinks for http:// addresses
\r
123 // there's a testcase for this in /build/testcases/urllinking.txt
\r
125 $replace_from = array(
\r
126 '/([^:\/\/\w]|^)((https:\/\/)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',
\r
127 '/([^:\/\/\w]|^)((http:\/\/|www\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',
\r
128 '/([^:\/\/\w]|^)((ftp:\/\/|ftp\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',
\r
129 '/([^:\/\/\w]|^)(mailto:(([a-zA-Z\@\%\.\-\+_])+))/i'
\r
132 $body = preg_replace_callback($replace_from, array('self', 'prepareBody_cb'), $body);
\r
138 * Comment::createLinkCode()
\r
139 * Creates a link code for unlinked URLs with different protocols
\r
142 * @param string $pre Prefix of comment
143 * @param string $url URL
144 * @param string $protocol http, mailto and so on
145 * @return string string including anchor element and child text
148 function createLinkCode($pre, $url, $protocol = 'http')
\r
152 // it's possible that $url ends contains entities we don't want,
\r
153 // since htmlspecialchars is applied _before_ URL linking
\r
154 // move the part of URL, starting from the disallowed entity to the 'post' link part
\r
155 $aBadEntities = array('"', '>', '<');
\r
156 foreach ( $aBadEntities as $entity )
\r
158 $pos = i18n::strpos($url, $entity);
\r
162 $post = i18n::substr($url, $pos) . $post;
\r
163 $url = i18n::substr($url, 0, $pos);
\r
167 // remove entities at end (&&&&)
\r
168 if ( preg_match('/(&\w+;)+$/i', $url, $matches) )
\r
170 $post = $matches[0] . $post; // found entities (1 or more)
\r
171 $url = i18n::substr($url, 0, i18n::strlen($url) - i18n::strlen($post) );
\r
174 // move ending comma from url to 'post' part
\r
175 if ( i18n::substr($url, i18n::strlen($url) - 1) == ',' )
\r
177 $url = i18n::substr($url, 0, i18n::strlen($url) - 1);
\r
178 $post = ',' . $post;
\r
181 # replaced ereg() below with preg_match(). ereg* functions are deprecated in PHP 5.3.0
\r
182 # original ereg: ereg('^' . $protocol . '://', $url)
\r
184 if ( !preg_match('#^' . $protocol . '://#', $url) )
\r
186 $linkedUrl = $protocol . ( ($protocol == 'mailto') ? ':' : '://') . $url;
\r
193 if ( $protocol != 'mailto' )
\r
195 $displayedUrl = $linkedUrl;
\r
199 $displayedUrl = $url;
\r
202 return $pre . '<a href="' . $linkedUrl . '" rel="nofollow">' . Entity::hsc(Entity::shorten($displayedUrl,30,'...')) . '</a>' . $post;
\r
206 * Comment::prepareBody_cb()
207 * This method is a callback for creating link codes
\r
209 * @param array $match elements for achor
210 * @return string including anchor element and child text
213 function prepareBody_cb($match)
\r
215 if ( !preg_match('/^[a-z]+/i', $match[2], $protocol) )
\r
220 switch( strtolower($protocol[0]) )
\r
223 return self::createLinkCode($match[1], $match[2], 'https');
\r
227 return self::createLinkCode($match[1], $match[2], 'ftp');
\r
231 return self::createLinkCode($match[1], $match[3], 'mailto');
\r
235 return self::createLinkCode($match[1], $match[2], 'http');
\r