OSDN Git Service

9d33b80de77f48218e6c8974c2d945519136e4d9
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / 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-2005 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   * A class representing a single comment\r
14   *\r
15   * $Id: COMMENT.php,v 1.3 2005-03-12 06:19:05 kimitake Exp $\r
16   * $NucleusJP$\r
17   */\r
18 class COMMENT {\r
19 \r
20         /**\r
21           * Returns the requested comment (static)\r
22           */\r
23         function getComment($commentid) {\r
24                 $query =  'SELECT cnumber as commentid, cbody as body, cuser as user, cmail as userid, cmember as memberid, ctime, chost as host, mname as member, cip as ip, cblog as blogid'\r
25                            . ' FROM '.sql_table('comment').' left outer join '.sql_table('member').' on cmember=mnumber'\r
26                            . ' WHERE cnumber=' . intval($commentid);\r
27                 $comments = sql_query($query);\r
28 \r
29                 $aCommentInfo = mysql_fetch_assoc($comments);\r
30                 if ($aCommentInfo)\r
31                 {\r
32                         $aCommentInfo['timestamp'] = strtotime($aCommentInfo['ctime']);\r
33                 }\r
34                 return $aCommentInfo;\r
35         }\r
36 \r
37         /**\r
38           * prepares a comment to be saved\r
39           * (static)\r
40           */\r
41         function prepare($comment) {\r
42                 $comment['user'] = strip_tags($comment['user']);\r
43                 $comment['userid'] = strip_tags($comment['userid']);\r
44 \r
45                 // remove quotes and newlines from user and userid\r
46                 $comment['user'] = strtr($comment['user'], "\'\"\n",'-- ');\r
47                 $comment['userid'] = strtr($comment['userid'], "\'\"\n",'-- ');\r
48 \r
49                 $comment['body'] = COMMENT::prepareBody($comment['body']);\r
50 \r
51                 return $comment;\r
52         }\r
53 \r
54         // prepares the body of a comment (static)\r
55         function prepareBody($body) {\r
56 \r
57                 // remove newlines when too many in a row\r
58                 $body = ereg_replace("\n.\n.\n","\n",$body);\r
59 \r
60                 // encode special characters as entities\r
61                 $body = htmlspecialchars($body);\r
62 \r
63                 // trim away whitespace and newlines at beginning and end\r
64                 $body = trim($body);\r
65 \r
66                 // add <br /> tags\r
67                 $body = addBreaks($body);\r
68 \r
69                 // create hyperlinks for http:// addresses\r
70                 // there's a testcase for this in /build/testcases/urllinking.txt\r
71                 $replaceFrom = array(\r
72                         '/([^:\/\/\w]|^)((https:\/\/)([a-z0-9_\.-]+)([\/a-z0-9_+\.~%&?@=_:;#,-]+))/ie',\r
73                         '/([^:\/\/\w]|^)((http:\/\/|www\.)([a-z0-9_\.-]+)([\/a-z0-9_+\.~%&?@=_:;#,-]+))/ie',\r
74                         '/([^:\/\/\w]|^)((ftp:\/\/|ftp\.)([a-z0-9_\.-]+)([\/a-z0-9_+\.~%&?@=_:;#,-]+))/ie',\r
75                         '/([^:\/\/\w]|^)(mailto:(([a-zA-Z\@\%\.\-\+_])+))/ie'\r
76                 );\r
77                 $replaceTo = array(\r
78                         'COMMENT::createLinkCode("\\1", "\\2","https")',\r
79                         'COMMENT::createLinkCode("\\1", "\\2","http")',\r
80                         'COMMENT::createLinkCode("\\1", "\\2","ftp")',\r
81                         'COMMENT::createLinkCode("\\1", "\\3","mailto")'\r
82                 );\r
83                 $body = preg_replace($replaceFrom, $replaceTo, $body);\r
84 \r
85                 return $body;\r
86         }\r
87 \r
88         function createLinkCode($pre, $url, $protocol = 'http') {\r
89                 $post = '';\r
90 \r
91                 // it's possible that $url ends contains entities we don't want,\r
92                 // since htmlspecialchars is applied _before_ URL linking\r
93                 // move the part of URL, starting from the disallowed entity to the 'post' link part\r
94                 $aBadEntities = array('&quot;', '&gt;', '&lt;');\r
95                 foreach ($aBadEntities as $entity)\r
96                 {\r
97                         $pos = strpos($url, $entity);\r
98                         if ($pos)\r
99                         {\r
100                                 $post = substr($url, $pos) . $post;\r
101                                 $url = substr($url, 0, $pos);\r
102 \r
103                         }\r
104                 }\r
105 \r
106                 // remove entities at end (&&&&)\r
107                 if (preg_match('/(&\w+;)+$/i', $url, $matches)) {\r
108                         $post = $matches[0] . $post;    // found entities (1 or more)\r
109                         $url = substr($url, 0, strlen($url) - strlen($post));\r
110                 }\r
111 \r
112                 // move ending comma from url to 'post' part\r
113                 if (substr($url, strlen($url) - 1) == ',')\r
114                 {\r
115                         $url = substr($url, 0, strlen($url) - 1);\r
116                         $post = ',' . $post;\r
117                 }\r
118 \r
119                 if (!ereg('^'.$protocol.'://',$url))\r
120                         $linkedUrl = $protocol . (($protocol == 'mailto') ? ':' : '://') . $url;\r
121                 else\r
122                         $linkedUrl = $url;\r
123 \r
124 \r
125                 if ($protocol != 'mailto')\r
126                         $displayedUrl = $linkedUrl;\r
127                 else\r
128                         $displayedUrl = $url;\r
129                 return $pre . '<a href="'.$linkedUrl.'" rel="nofollow">'.shorten($displayedUrl,30,'...').'</a>' . $post;\r
130         }\r
131 \r
132 }\r
133 \r
134 ?>\r