OSDN Git Service

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