OSDN Git Service

MERGE: リビジョン1778/1779のマージ。Skin::getFriendlyNames()のアクセス方法の変更
[nucleus-jp/nucleus-next.git] / nucleus / libs / COMMENT.php
1 <?php\r
2 \r
3 /*\r
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)\r
5  * Copyright (C) 2002-2007 The Nucleus Group\r
6  *\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
12  */\r
13 /**\r
14  * A class representing a single comment\r
15  *\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 $
19  */\r
20 class Comment\r
21 {\r
22         /**\r
23          * Comment::getComment()\r
24          * Returns the requested comment\r
25          * \r
26          * @static\r
27          * @param       integer $commentid      id for comment
28          * @return      array   comment information\r
29          * \r
30          */\r
31         function getComment($commentid)\r
32         {\r
33                 $query = 'SELECT cnumber AS commentid,'\r
34                               . ' cbody AS body,'\r
35                               . ' cuser AS user,'\r
36                               . ' cmail AS userid,'\r
37                               . ' cemail AS email,'\r
38                               . ' cmember AS memberid,'\r
39                               . ' ctime,'\r
40                               . ' chost AS host,'\r
41                               . ' mname AS member,'\r
42                               . ' cip AS ip,'\r
43                               . ' cblog AS blogid'\r
44                        . ' FROM %s LEFT OUTER JOIN %s ON cmember = mnumber'\r
45                        . ' WHERE cnumber = %d;';\r
46                 \r
47                 $query = sprintf($query, sql_table('comment'), sql_table('member'), (integer) $commentid);\r
48                 $comments = sql_query($query);
49                 \r
50                 $aCommentInfo = sql_fetch_assoc($comments);\r
51 \r
52                 if ( $aCommentInfo )
53                 {
54                         $aCommentInfo['timestamp'] = strtotime($aCommentInfo['ctime']);\r
55                 }\r
56 \r
57                 return $aCommentInfo;\r
58         }\r
59 \r
60         /**\r
61          * Comment::prepare()
62           * Prepares a comment to be saved\r
63           *\r
64           * @static\r
65          * @param       array   $comment        comment data
66          * @return      array   comment date
67          * 
68           */\r
69         function prepare($comment)\r
70         {\r
71                 $comment['user'] = strip_tags($comment['user']);\r
72                 $comment['userid'] = strip_tags($comment['userid']);\r
73                 $comment['email'] = strip_tags($comment['email']);\r
74 \r
75                 // remove newlines from user; remove quotes and newlines from userid and email; trim whitespace from beginning and end\r
76                 $comment['user'] = trim(strtr($comment['user'], "\n", ' ') );\r
77                 $comment['userid'] = trim(strtr($comment['userid'], "\'\"\n", '-- ') );\r
78                 $comment['email'] = trim(strtr($comment['email'], "\'\"\n", '-- ') );\r
79 \r
80                 // begin if: a comment userid is supplied, but does not have an "http://" or "https://" at the beginning - prepend an "http://"\r
81                 if ( array_key_exists('userid', $comment)
82                   && !empty($comment['userid'])
83                   && (i18n::strpos($comment['userid'], 'http://') !== 0)
84                   && (i18n::strpos($comment['userid'], 'https://') !== 0) )
85                 {\r
86                         $comment['userid'] = 'http://' . $comment['userid'];\r
87                 }
88 \r
89                 $comment['body'] = Comment::prepareBody($comment['body']);\r
90 \r
91                 return $comment;\r
92         }\r
93 \r
94         /**\r
95          * Comment::prepareBody()
96          * Prepares the body of a comment\r
97          *\r
98          * @ static\r
99          * @param       string  $body   string for comment body
100          * @return      string  validate string for comment body
101          * 
102          */\r
103         function prepareBody($body)
104         {
105                 # replaced ereg_replace() below with preg_replace(). ereg* functions are deprecated in PHP 5.3.0\r
106                 # original ereg_replace: ereg_replace("\n.\n.\n", "\n", $body);\r
107 \r
108                 // convert Windows and Mac style 'returns' to *nix newlines\r
109                 $body = preg_replace("/\r\n/", "\n", $body);\r
110                 $body = preg_replace("/\r/", "\n", $body);\r
111 \r
112                 // then remove newlines when too many in a row (3 or more newlines get converted to 1 newline)\r
113                 $body = preg_replace("/\n{3,}/", "\n\n", $body);\r
114 \r
115                 // encode special characters as entities\r
116                 $body = Entity::hsc($body);\r
117 \r
118                 // trim away whitespace and newlines at beginning and end\r
119                 $body = trim($body);\r
120 \r
121                 // add <br /> tags\r
122                 $body = addBreaks($body);\r
123 \r
124                 // create hyperlinks for http:// addresses\r
125                 // there's a testcase for this in /build/testcases/urllinking.txt\r
126 \r
127                 $replace_from = array(\r
128                         '/([^:\/\/\w]|^)((https:\/\/)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',\r
129                         '/([^:\/\/\w]|^)((http:\/\/|www\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',\r
130                         '/([^:\/\/\w]|^)((ftp:\/\/|ftp\.)([\w\.-]+)([\/\w+\.~%&?@=_:;#,-]+))/i',\r
131                         '/([^:\/\/\w]|^)(mailto:(([a-zA-Z\@\%\.\-\+_])+))/i'\r
132                 );\r
133 \r
134                 $body = preg_replace_callback($replace_from, array('self', 'prepareBody_cb'), $body);\r
135 \r
136                 return $body;\r
137         }\r
138         \r
139         /**\r
140          * Comment::createLinkCode()\r
141          * Creates a link code for unlinked URLs with different protocols\r
142          *\r
143          * @static\r
144          * @param       string  $pre    Prefix of comment
145          * @param       string  $url    URL
146          * @param       string  $protocol       http, mailto and so on
147          * @return      string  string  including anchor element and child text
148          * 
149          */\r
150         function createLinkCode($pre, $url, $protocol = 'http')\r
151         {\r
152                 $post = '';\r
153                 \r
154                 // it's possible that $url ends contains entities we don't want,\r
155                 // since htmlspecialchars is applied _before_ URL linking\r
156                 // move the part of URL, starting from the disallowed entity to the 'post' link part\r
157                 $aBadEntities = array('&quot;', '&gt;', '&lt;');\r
158                 foreach ( $aBadEntities as $entity )\r
159                 {\r
160                         $pos = i18n::strpos($url, $entity);\r
161                         \r
162                         if ( $pos )\r
163                         {\r
164                                 $post = i18n::substr($url, $pos) . $post;\r
165                                 $url = i18n::substr($url, 0, $pos);\r
166                         }\r
167                 }\r
168                 \r
169                 // remove entities at end (&&&&)\r
170                 if ( preg_match('/(&\w+;)+$/i', $url, $matches) )\r
171                 {\r
172                         $post = $matches[0] . $post;    // found entities (1 or more)\r
173                         $url = i18n::substr($url, 0, i18n::strlen($url) - i18n::strlen($post) );\r
174                 }\r
175                 \r
176                 // move ending comma from url to 'post' part\r
177                 if ( i18n::substr($url, i18n::strlen($url) - 1) == ',' )\r
178                 {\r
179                         $url = i18n::substr($url, 0, i18n::strlen($url) - 1);\r
180                         $post = ',' . $post;\r
181                 }\r
182                 \r
183                 # replaced ereg() below with preg_match(). ereg* functions are deprecated in PHP 5.3.0\r
184                 # original ereg: ereg('^' . $protocol . '://', $url)\r
185                 \r
186                 if ( !preg_match('#^' . $protocol . '://#', $url) )\r
187                 {\r
188                         $linkedUrl = $protocol . ( ($protocol == 'mailto') ? ':' : '://') . $url;\r
189                 }\r
190                 else\r
191                 {\r
192                         $linkedUrl = $url;\r
193                 }\r
194                 \r
195                 if ( $protocol != 'mailto' )\r
196                 {\r
197                         $displayedUrl = $linkedUrl;\r
198                 }\r
199                 else\r
200                 {\r
201                         $displayedUrl = $url;\r
202                 }\r
203                 \r
204                 return $pre . '<a href="' . $linkedUrl . '" rel="nofollow">' . Entity::hsc(Entity::shorten($displayedUrl,30,'...')) . '</a>' . $post;\r
205         }\r
206         \r
207         /**\r
208          * Comment::prepareBody_cb()
209          * This method is a callback for creating link codes\r
210          * 
211          * @param       array   $match  elements for achor
212          * @return      string  including anchor element and child text
213          * 
214          */\r
215         function prepareBody_cb($match)\r
216         {\r
217                 if ( !preg_match('/^[a-z]+/i', $match[2], $protocol) )\r
218                 {\r
219                         return $match[0];\r
220                 }\r
221 \r
222                 switch( strtolower($protocol[0]) )\r
223                 {\r
224                         case 'https':\r
225                                 return self::createLinkCode($match[1], $match[2], 'https');\r
226                         break;\r
227 \r
228                         case 'ftp':\r
229                                 return self::createLinkCode($match[1], $match[2], 'ftp');\r
230                         break;\r
231 \r
232                         case 'mailto':\r
233                                 return self::createLinkCode($match[1], $match[3], 'mailto');\r
234                         break;\r
235 \r
236                         default:\r
237                                 return self::createLinkCode($match[1], $match[2], 'http');\r
238                         break;\r
239                 }\r
240                 return;
241         }\r
242 \r
243 }\r
244 \r