OSDN Git Service

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