OSDN Git Service

Subversion由来のタグを削除
[nucleus-jp/nucleus-jp-ancient.git] / nucleus / libs / COMMENT.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2007 The Nucleus Group
6  *
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)
12  *
13  * A class representing a single comment
14  */
15 class COMMENT {
16
17         /**
18           * Returns the requested comment
19           *
20           * @static
21           */
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);
27
28                 $aCommentInfo = sql_fetch_assoc($comments);
29
30                 if ($aCommentInfo) {
31                         $aCommentInfo['timestamp'] = strtotime($aCommentInfo['ctime']);
32                 }
33
34                 return $aCommentInfo;
35         }
36
37         /**
38           * Prepares a comment to be saved
39           *
40           * @static
41           */
42         function prepare($comment)\r
43         {
44                 $comment['user'] = strip_tags($comment['user']);
45                 $comment['userid'] = strip_tags($comment['userid']);
46                 $comment['email'] = strip_tags($comment['email']);
47
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", '-- ') );
52
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
55                 {
56                         $comment['userid'] = 'http://' . $comment['userid'];
57                 } // end if
58
59                 $comment['body'] = COMMENT::prepareBody($comment['body']);
60
61                 return $comment;
62         }
63
64         /**
65          * Prepares the body of a comment
66          *
67          * @ static
68          */
69         function prepareBody($body) {
70
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);
73
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);
77
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);
80
81                 // encode special characters as entities
82                 $body = htmlspecialchars($body);
83
84                 // trim away whitespace and newlines at beginning and end
85                 $body = trim($body);
86
87                 // add <br /> tags
88                 $body = addBreaks($body);
89
90                 // create hyperlinks for http:// addresses
91                 // there's a testcase for this in /build/testcases/urllinking.txt
92 \r
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
98                 );
99
100                 $body = preg_replace_callback($replace_from, array('self', 'prepareBody_cb'), $body);
101
102                 return $body;
103         }
104
105
106
107         /**
108          * Creates a link code for unlinked URLs with different protocols
109          *
110          * @ static
111          */
112         function createLinkCode($pre, $url, $protocol = 'http') {
113                 $post = '';
114
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('&quot;', '&gt;', '&lt;');
119                 foreach ($aBadEntities as $entity) {
120
121                         $pos = strpos($url, $entity);
122
123                         if ($pos) {
124                                 $post = substr($url, $pos) . $post;
125                                 $url = substr($url, 0, $pos);
126                         }
127
128                 }
129
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) );
134                 }
135
136                 // move ending comma from url to 'post' part
137                 if (substr($url, strlen($url) - 1) == ',') {
138                         $url = substr($url, 0, strlen($url) - 1);
139                         $post = ',' . $post;
140                 }
141
142                 # replaced ereg() below with preg_match(). ereg* functions are deprecated in PHP 5.3.0
143                 # original ereg: ereg('^' . $protocol . '://', $url)
144
145                 if (!preg_match('#^' . $protocol . '://#', $url) )
146                 {
147                         $linkedUrl = $protocol . ( ($protocol == 'mailto') ? ':' : '://') . $url;
148                 }
149                 else
150                 {
151                         $linkedUrl = $url;
152                 }
153
154                 if ($protocol != 'mailto') {
155                         $displayedUrl = $linkedUrl;
156                 } else {
157                         $displayedUrl = $url;
158                 }
159
160                 return $pre . '<a href="' . $linkedUrl . '" rel="nofollow">' . shorten($displayedUrl,30,'...') . '</a>' . $post;
161         }
162
163 \r
164         /**\r
165          * This method is a callback for creating link codes\r
166          * @param array $match\r
167          * @return string\r
168          */\r
169         function prepareBody_cb($match)\r
170         {\r
171                 if ( !preg_match('/^[a-z]+/i', $match[2], $protocol) )\r
172                 {\r
173                         return $match[0];\r
174                 }\r
175 \r
176                 switch( strtolower($protocol[0]) )\r
177                 {\r
178                         case 'https':\r
179                                 return self::createLinkCode($match[1], $match[2], 'https');\r
180                         break;\r
181 \r
182                         case 'ftp':\r
183                                 return self::createLinkCode($match[1], $match[2], 'ftp');\r
184                         break;\r
185 \r
186                         case 'mailto':\r
187                                 return self::createLinkCode($match[1], $match[3], 'mailto');\r
188                         break;\r
189 \r
190                         default:\r
191                                 return self::createLinkCode($match[1], $match[2], 'http');\r
192                         break;\r
193                 }\r
194         }
195
196 }
197 ?>