OSDN Git Service

CHANGE:クエリ生成に使われているi18n::formatted_datetime()をDB::formatDateTime()に変更
[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                 $aCommentInfo = DB::getRow($query);\r
49 \r
50                 if ( $aCommentInfo )
51                 {
52                         $aCommentInfo['timestamp'] = strtotime($aCommentInfo['ctime']);\r
53                 }\r
54 \r
55                 return $aCommentInfo;\r
56         }\r
57 \r
58         /**\r
59          * Comment::prepare()
60           * Prepares a comment to be saved\r
61           *\r
62           * @static\r
63          * @param       array   $comment        comment data
64          * @return      array   comment date
65          * 
66           */\r
67         function prepare($comment)\r
68         {\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
72 \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
77 \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) )
83                 {\r
84                         $comment['userid'] = 'http://' . $comment['userid'];\r
85                 }
86 \r
87                 $comment['body'] = Comment::prepareBody($comment['body']);\r
88 \r
89                 return $comment;\r
90         }\r
91 \r
92         /**\r
93          * Comment::prepareBody()
94          * Prepares the body of a comment\r
95          *\r
96          * @ static\r
97          * @param       string  $body   string for comment body
98          * @return      string  validate string for comment body
99          * 
100          */\r
101         function prepareBody($body)
102         {
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
105 \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
109 \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
112 \r
113                 // encode special characters as entities\r
114                 $body = Entity::hsc($body);\r
115 \r
116                 // trim away whitespace and newlines at beginning and end\r
117                 $body = trim($body);\r
118 \r
119                 // add <br /> tags\r
120                 $body = addBreaks($body);\r
121 \r
122                 // create hyperlinks for http:// addresses\r
123                 // there's a testcase for this in /build/testcases/urllinking.txt\r
124 \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
130                 );\r
131 \r
132                 $body = preg_replace_callback($replace_from, array('self', 'prepareBody_cb'), $body);\r
133 \r
134                 return $body;\r
135         }\r
136         \r
137         /**\r
138          * Comment::createLinkCode()\r
139          * Creates a link code for unlinked URLs with different protocols\r
140          *\r
141          * @static\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
146          * 
147          */\r
148         function createLinkCode($pre, $url, $protocol = 'http')\r
149         {\r
150                 $post = '';\r
151                 \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('&quot;', '&gt;', '&lt;');\r
156                 foreach ( $aBadEntities as $entity )\r
157                 {\r
158                         $pos = i18n::strpos($url, $entity);\r
159                         \r
160                         if ( $pos )\r
161                         {\r
162                                 $post = i18n::substr($url, $pos) . $post;\r
163                                 $url = i18n::substr($url, 0, $pos);\r
164                         }\r
165                 }\r
166                 \r
167                 // remove entities at end (&&&&)\r
168                 if ( preg_match('/(&\w+;)+$/i', $url, $matches) )\r
169                 {\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
172                 }\r
173                 \r
174                 // move ending comma from url to 'post' part\r
175                 if ( i18n::substr($url, i18n::strlen($url) - 1) == ',' )\r
176                 {\r
177                         $url = i18n::substr($url, 0, i18n::strlen($url) - 1);\r
178                         $post = ',' . $post;\r
179                 }\r
180                 \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
183                 \r
184                 if ( !preg_match('#^' . $protocol . '://#', $url) )\r
185                 {\r
186                         $linkedUrl = $protocol . ( ($protocol == 'mailto') ? ':' : '://') . $url;\r
187                 }\r
188                 else\r
189                 {\r
190                         $linkedUrl = $url;\r
191                 }\r
192                 \r
193                 if ( $protocol != 'mailto' )\r
194                 {\r
195                         $displayedUrl = $linkedUrl;\r
196                 }\r
197                 else\r
198                 {\r
199                         $displayedUrl = $url;\r
200                 }\r
201                 \r
202                 return $pre . '<a href="' . $linkedUrl . '" rel="nofollow">' . Entity::hsc(Entity::shorten($displayedUrl,30,'...')) . '</a>' . $post;\r
203         }\r
204         \r
205         /**\r
206          * Comment::prepareBody_cb()
207          * This method is a callback for creating link codes\r
208          * 
209          * @param       array   $match  elements for achor
210          * @return      string  including anchor element and child text
211          * 
212          */\r
213         function prepareBody_cb($match)\r
214         {\r
215                 if ( !preg_match('/^[a-z]+/i', $match[2], $protocol) )\r
216                 {\r
217                         return $match[0];\r
218                 }\r
219 \r
220                 switch( strtolower($protocol[0]) )\r
221                 {\r
222                         case 'https':\r
223                                 return self::createLinkCode($match[1], $match[2], 'https');\r
224                         break;\r
225 \r
226                         case 'ftp':\r
227                                 return self::createLinkCode($match[1], $match[2], 'ftp');\r
228                         break;\r
229 \r
230                         case 'mailto':\r
231                                 return self::createLinkCode($match[1], $match[3], 'mailto');\r
232                         break;\r
233 \r
234                         default:\r
235                                 return self::createLinkCode($match[1], $match[2], 'http');\r
236                         break;\r
237                 }\r
238                 return;
239         }\r
240 \r
241 }\r
242 \r