OSDN Git Service

FIX:変数名の誤記を修正
[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 1844 2012-05-13 11:14:38Z sakamocchi $\r
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\r
28          * @return      array   comment information\r
29          * \r
30          */\r
31         static 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 )\r
51                 {\r
52                         $aCommentInfo['timestamp'] = strtotime($aCommentInfo['ctime']);\r
53                 }\r
54                 \r
55                 return $aCommentInfo;\r
56         }\r
57         \r
58         /**\r
59          * Comment::prepare()\r
60          * Prepares a comment to be saved\r
61          *\r
62          * @static\r
63          * @param       array   $comment        comment data\r
64          * @return      array   comment date\r
65          * \r
66          */\r
67         static 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)\r
80                   && !empty($comment['userid'])\r
81                   && (i18n::strpos($comment['userid'], 'http://') !== 0)\r
82                   && (i18n::strpos($comment['userid'], 'https://') !== 0) )\r
83                 {\r
84                         $comment['userid'] = 'http://' . $comment['userid'];\r
85                 }\r
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()\r
94          * Prepares the body of a comment\r
95          *\r
96          * @static\r
97          * @param       string  $body   string for comment body\r
98          * @return      string  validate string for comment body\r
99          */\r
100         static public function prepareBody($body)\r
101         {\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
105                 \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
108                 \r
109                 // encode special characters as entities\r
110                 $body = Entity::hsc($body);\r
111                 \r
112                 // trim away whitespace and newlines at beginning and end\r
113                 $body = trim($body);\r
114                 \r
115                 // add <br /> tags\r
116                 $body = addBreaks($body);\r
117                 \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
125                 );\r
126                 \r
127                 return preg_replace_callback($replace_from, array(__CLASS__, 'prepareBody_cb'), $body);\r
128         }\r
129         \r
130         /**\r
131          * Comment::createLinkCode()\r
132          * Creates a link code for unlinked URLs with different protocols\r
133          *\r
134          * @static\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
139          */\r
140         static private function createLinkCode($pre, $url, $protocol = 'http')\r
141         {\r
142                 $post = '';\r
143                 \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('&quot;', '&gt;', '&lt;');\r
148                 foreach ( $aBadEntities as $entity )\r
149                 {\r
150                         $pos = i18n::strpos($url, $entity);\r
151                         \r
152                         if ( $pos )\r
153                         {\r
154                                 $post = i18n::substr($url, $pos) . $post;\r
155                                 $url = i18n::substr($url, 0, $pos);\r
156                         }\r
157                 }\r
158                 \r
159                 // remove entities at end (&&&&)\r
160                 if ( preg_match('/(&\w+;)+$/i', $url, $matches) )\r
161                 {\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
164                 }\r
165                 \r
166                 // move ending comma from url to 'post' part\r
167                 if ( i18n::substr($url, i18n::strlen($url) - 1) == ',' )\r
168                 {\r
169                         $url = i18n::substr($url, 0, i18n::strlen($url) - 1);\r
170                         $post = ',' . $post;\r
171                 }\r
172                 \r
173                 if ( !preg_match('#^' . $protocol . '://#', $url) )\r
174                 {\r
175                         $linkedUrl = $protocol . ( ($protocol == 'mailto') ? ':' : '://') . $url;\r
176                 }\r
177                 else\r
178                 {\r
179                         $linkedUrl = $url;\r
180                 }\r
181                 \r
182                 if ( $protocol != 'mailto' )\r
183                 {\r
184                         $displayedUrl = $linkedUrl;\r
185                 }\r
186                 else\r
187                 {\r
188                         $displayedUrl = $url;\r
189                 }\r
190                 \r
191                 return $pre . '<a href="' . $linkedUrl . '" rel="nofollow">' . Entity::hsc(Entity::shorten($displayedUrl,30,'...')) . '</a>' . $post;\r
192         }\r
193         \r
194         /**\r
195          * Comment::prepareBody_cb()\r
196          * This method is a callback for creating link codes\r
197          * \r
198          * @param       array   $match  elements for achor\r
199          * @return      string  including anchor element and child text\r
200          * \r
201          */\r
202         static public function prepareBody_cb($match)\r
203         {\r
204                 if ( !preg_match('/^[a-z]+/i', $match[2], $protocol) )\r
205                 {\r
206                         return $match[0];\r
207                 }\r
208                 \r
209                 switch( strtolower($protocol[0]) )\r
210                 {\r
211                         case 'https':\r
212                                 return self::createLinkCode($match[1], $match[2], 'https');\r
213                         break;\r
214                         \r
215                         case 'ftp':\r
216                                 return self::createLinkCode($match[1], $match[2], 'ftp');\r
217                         break;\r
218                         \r
219                         case 'mailto':\r
220                                 return self::createLinkCode($match[1], $match[3], 'mailto');\r
221                         break;\r
222                         \r
223                         default:\r
224                                 return self::createLinkCode($match[1], $match[2], 'http');\r
225                         break;\r
226                 }\r
227                 return;\r
228         }\r
229 }\r