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 1844 2012-05-13 11:14:38Z sakamocchi $
\r
23 * Comment::getComment()
\r
24 * Returns the requested comment
\r
27 * @param integer $commentid id for comment
\r
28 * @return array comment information
\r
31 static 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
50 if ( $aCommentInfo )
\r
52 $aCommentInfo['timestamp'] = strtotime($aCommentInfo['ctime']);
\r
55 return $aCommentInfo;
\r
59 * Comment::prepare()
\r
60 * Prepares a comment to be saved
\r
63 * @param array $comment comment data
\r
64 * @return array comment date
\r
67 static 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)
\r
80 && !empty($comment['userid'])
\r
81 && (i18n::strpos($comment['userid'], 'http://') !== 0)
\r
82 && (i18n::strpos($comment['userid'], 'https://') !== 0) )
\r
84 $comment['userid'] = 'http://' . $comment['userid'];
\r
87 $comment['body'] = Comment::prepareBody($comment['body']);
\r
93 * Comment::prepareBody()
\r
94 * Prepares the body of a comment
\r
97 * @param string $body string for comment body
\r
98 * @return string validate string for comment body
\r
100 static public function prepareBody($body)
\r
102 // convert Windows and Mac style 'returns' to *nix newlines
\r
103 $body = preg_replace("/\r\n/", "\n", $body);
\r
104 $body = preg_replace("/\r/", "\n", $body);
\r
106 // then remove newlines when too many in a row (3 or more newlines get converted to 1 newline)
\r
107 $body = preg_replace("/\n{3,}/", "\n\n", $body);
\r
109 // encode special characters as entities
\r
110 $body = Entity::hsc($body);
\r
112 // trim away whitespace and newlines at beginning and end
\r
113 $body = trim($body);
\r
116 $body = addBreaks($body);
\r
118 // create hyperlinks for http:// addresses
\r
119 // there's a testcase for this in /build/testcases/urllinking.txt
\r
120 $replace_from = array(
\r
121 '/([^:\/\/\w]|^)((https:\/\/)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',
\r
122 '/([^:\/\/\w]|^)((http:\/\/|www\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',
\r
123 '/([^:\/\/\w]|^)((ftp:\/\/|ftp\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',
\r
124 '/([^:\/\/\w]|^)(mailto:(([a-zA-Z\@\%\.\-\+_])+))/i'
\r
127 return preg_replace_callback($replace_from, array(__CLASS__, 'prepareBody_cb'), $body);
\r
131 * Comment::createLinkCode()
\r
132 * Creates a link code for unlinked URLs with different protocols
\r
135 * @param string $pre Prefix of comment
\r
136 * @param string $url URL
\r
137 * @param string $protocol http, mailto and so on
\r
138 * @return string string including anchor element and child text
\r
140 static private function createLinkCode($pre, $url, $protocol = 'http')
\r
144 // it's possible that $url ends contains entities we don't want,
\r
145 // since htmlspecialchars is applied _before_ URL linking
\r
146 // move the part of URL, starting from the disallowed entity to the 'post' link part
\r
147 $aBadEntities = array('"', '>', '<');
\r
148 foreach ( $aBadEntities as $entity )
\r
150 $pos = i18n::strpos($url, $entity);
\r
154 $post = i18n::substr($url, $pos) . $post;
\r
155 $url = i18n::substr($url, 0, $pos);
\r
159 // remove entities at end (&&&&)
\r
160 if ( preg_match('/(&\w+;)+$/i', $url, $matches) )
\r
162 $post = $matches[0] . $post; // found entities (1 or more)
\r
163 $url = i18n::substr($url, 0, i18n::strlen($url) - i18n::strlen($post) );
\r
166 // move ending comma from url to 'post' part
\r
167 if ( i18n::substr($url, i18n::strlen($url) - 1) == ',' )
\r
169 $url = i18n::substr($url, 0, i18n::strlen($url) - 1);
\r
170 $post = ',' . $post;
\r
173 if ( !preg_match('#^' . $protocol . '://#', $url) )
\r
175 $linkedUrl = $protocol . ( ($protocol == 'mailto') ? ':' : '://') . $url;
\r
182 if ( $protocol != 'mailto' )
\r
184 $displayedUrl = $linkedUrl;
\r
188 $displayedUrl = $url;
\r
191 return $pre . '<a href="' . $linkedUrl . '" rel="nofollow">' . Entity::hsc(Entity::shorten($displayedUrl,30,'...')) . '</a>' . $post;
\r
195 * Comment::prepareBody_cb()
\r
196 * This method is a callback for creating link codes
\r
198 * @param array $match elements for achor
\r
199 * @return string including anchor element and child text
\r
202 static public function prepareBody_cb($match)
\r
204 if ( !preg_match('/^[a-z]+/i', $match[2], $protocol) )
\r
209 switch( strtolower($protocol[0]) )
\r
212 return self::createLinkCode($match[1], $match[2], 'https');
\r
216 return self::createLinkCode($match[1], $match[2], 'ftp');
\r
220 return self::createLinkCode($match[1], $match[3], 'mailto');
\r
224 return self::createLinkCode($match[1], $match[2], 'http');
\r