OSDN Git Service

Revert "4.0のtrunkを展開するためにmasterディレクトリを作成しファイル群を移動した。"
[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 /**
14  * A class representing a single comment
15  *
16  * @license http://nucleuscms.org/license.txt GNU General Public License
17  * @copyright Copyright (C) 2002-2007 The Nucleus Group
18  * @version $Id$
19  * $NucleusJP: COMMENT.php,v 1.4 2006/07/17 20:03:44 kimitake Exp $
20  */
21 class COMMENT {
22
23         /**
24           * Returns the requested comment
25           *
26           * @static
27           */
28         function getComment($commentid) {
29                 $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'
30                                         . ' FROM ' . sql_table('comment') . ' LEFT OUTER JOIN ' . sql_table('member') . ' ON `cmember` = `mnumber`'
31                                         . ' WHERE `cnumber` = ' . intval($commentid);
32                 $comments = sql_query($query);
33
34                 $aCommentInfo = sql_fetch_assoc($comments);
35
36                 if ($aCommentInfo) {
37                         $aCommentInfo['timestamp'] = strtotime($aCommentInfo['ctime']);
38                 }
39
40                 return $aCommentInfo;
41         }
42
43         /**
44           * Prepares a comment to be saved
45           *
46           * @static
47           */
48         function prepare($comment)\r
49         {
50                 $comment['user'] = strip_tags($comment['user']);
51                 $comment['userid'] = strip_tags($comment['userid']);
52                 $comment['email'] = strip_tags($comment['email']);
53
54                 // remove newlines from user; remove quotes and newlines from userid and email; trim whitespace from beginning and end
55                 $comment['user'] = trim(strtr($comment['user'], "\n", ' ') );
56                 $comment['userid'] = trim(strtr($comment['userid'], "\'\"\n", '-- ') );
57                 $comment['email'] = trim(strtr($comment['email'], "\'\"\n", '-- ') );
58
59                 // begin if: a comment userid is supplied, but does not have an "http://" or "https://" at the beginning - prepend an "http://"
60                 if ( !empty($comment['userid']) && (strpos($comment['userid'], 'http://') !== 0) && (strpos($comment['userid'], 'https://') !== 0) )\r
61                 {
62                         $comment['userid'] = 'http://' . $comment['userid'];
63                 } // end if
64
65                 $comment['body'] = COMMENT::prepareBody($comment['body']);
66
67                 return $comment;
68         }
69
70         /**
71          * Prepares the body of a comment
72          *
73          * @ static
74          */
75         function prepareBody($body) {
76
77                 # replaced ereg_replace() below with preg_replace(). ereg* functions are deprecated in PHP 5.3.0
78                 # original ereg_replace: ereg_replace("\n.\n.\n", "\n", $body);
79
80                 // convert Windows and Mac style 'returns' to *nix newlines
81                 $body = preg_replace("/\r\n/", "\n", $body);
82                 $body = preg_replace("/\r/", "\n", $body);
83
84                 // then remove newlines when too many in a row (3 or more newlines get converted to 1 newline)
85                 $body = preg_replace("/\n{3,}/", "\n\n", $body);
86
87                 // encode special characters as entities
88                 $body = htmlspecialchars($body);
89
90                 // trim away whitespace and newlines at beginning and end
91                 $body = trim($body);
92
93                 // add <br /> tags
94                 $body = addBreaks($body);
95
96                 // create hyperlinks for http:// addresses
97                 // there's a testcase for this in /build/testcases/urllinking.txt
98 \r
99                 $replace_from = array(\r
100                         '/([^:\/\/\w]|^)((https:\/\/)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',\r
101                         '/([^:\/\/\w]|^)((http:\/\/|www\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',\r
102                         '/([^:\/\/\w]|^)((ftp:\/\/|ftp\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',\r
103                         '/([^:\/\/\w]|^)(mailto:(([a-zA-Z\@\%\.\-\+_])+))/i'\r
104                 );
105
106                 $body = preg_replace_callback($replace_from, array('self', 'prepareBody_cb'), $body);
107
108                 return $body;
109         }
110
111
112
113         /**
114          * Creates a link code for unlinked URLs with different protocols
115          *
116          * @ static
117          */
118         function createLinkCode($pre, $url, $protocol = 'http') {
119                 $post = '';
120
121                 // it's possible that $url ends contains entities we don't want,
122                 // since htmlspecialchars is applied _before_ URL linking
123                 // move the part of URL, starting from the disallowed entity to the 'post' link part
124                 $aBadEntities = array('&quot;', '&gt;', '&lt;');
125                 foreach ($aBadEntities as $entity) {
126
127                         $pos = strpos($url, $entity);
128
129                         if ($pos) {
130                                 $post = substr($url, $pos) . $post;
131                                 $url = substr($url, 0, $pos);
132                         }
133
134                 }
135
136                 // remove entities at end (&&&&)
137                 if (preg_match('/(&\w+;)+$/i', $url, $matches) ) {
138                         $post = $matches[0] . $post;    // found entities (1 or more)
139                         $url = substr($url, 0, strlen($url) - strlen($post) );
140                 }
141
142                 // move ending comma from url to 'post' part
143                 if (substr($url, strlen($url) - 1) == ',') {
144                         $url = substr($url, 0, strlen($url) - 1);
145                         $post = ',' . $post;
146                 }
147
148                 # replaced ereg() below with preg_match(). ereg* functions are deprecated in PHP 5.3.0
149                 # original ereg: ereg('^' . $protocol . '://', $url)
150
151                 if (!preg_match('#^' . $protocol . '://#', $url) )
152                 {
153                         $linkedUrl = $protocol . ( ($protocol == 'mailto') ? ':' : '://') . $url;
154                 }
155                 else
156                 {
157                         $linkedUrl = $url;
158                 }
159
160                 if ($protocol != 'mailto') {
161                         $displayedUrl = $linkedUrl;
162                 } else {
163                         $displayedUrl = $url;
164                 }
165
166                 return $pre . '<a href="' . $linkedUrl . '" rel="nofollow">' . shorten($displayedUrl,30,'...') . '</a>' . $post;
167         }
168
169 \r
170         /**\r
171          * This method is a callback for creating link codes\r
172          * @param array $match\r
173          * @return string\r
174          */\r
175         function prepareBody_cb($match)\r
176         {\r
177                 if ( !preg_match('/^[a-z]+/i', $match[2], $protocol) )\r
178                 {\r
179                         return $match[0];\r
180                 }\r
181 \r
182                 switch( strtolower($protocol[0]) )\r
183                 {\r
184                         case 'https':\r
185                                 return self::createLinkCode($match[1], $match[2], 'https');\r
186                         break;\r
187 \r
188                         case 'ftp':\r
189                                 return self::createLinkCode($match[1], $match[2], 'ftp');\r
190                         break;\r
191 \r
192                         case 'mailto':\r
193                                 return self::createLinkCode($match[1], $match[3], 'mailto');\r
194                         break;\r
195 \r
196                         default:\r
197                                 return self::createLinkCode($match[1], $match[2], 'http');\r
198                         break;\r
199                 }\r
200         }
201
202 }
203 ?>