OSDN Git Service

merged 3.3 beta1
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / libs / ACTION.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2005 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  * Actions that can be called via action.php
15  *
16  * @license http://nucleuscms.org/license.txt GNU General Public License
17  * @copyright Copyright (C) 2002-2005 The Nucleus Group
18  * @version $Id: ACTION.php,v 1.4 2006-07-12 07:11:47 kimitake Exp $
19  * $NucleusJP: ACTION.php,v 1.3 2005/08/13 07:30:01 kimitake Exp $
20  */
21 class ACTION
22 {
23         function ACTION()
24         {
25         
26         }
27         
28         function doAction($action) 
29         {
30                 switch($action) {
31                         case 'addcomment':
32                                 return $this->addComment();
33                                 break;
34                         case 'sendmessage':
35                                 return $this->sendMessage();
36                                 break;
37                         case 'createaccount':
38                                 return $this->createAccount();
39                                 break;          
40                         case 'forgotpassword':
41                                 return $this->forgotPassword();
42                                 break;
43                         case 'votepositive':
44                                 return $this->doKarma('pos');
45                                 break;
46                         case 'votenegative':
47                                 return $this->doKarma('neg');
48                                 break;
49                         case 'plugin':
50                                 return $this->callPlugin();
51                                 break;
52                         default:
53                                 doError(_ERROR_BADACTION);
54                 }
55         }
56         
57         function addComment() {
58                 global $CONF, $errormessage, $manager;
59
60                 $post['itemid'] =       intPostVar('itemid');
61                 $post['user'] =         postVar('user');
62                 $post['userid'] =       postVar('userid');
63                 $post['body'] =         postVar('body');
64
65                 // set cookies when required
66                 $remember = intPostVar('remember');
67                 if ($remember == 1) {
68                         $lifetime = time()+2592000;
69                         setcookie($CONF['CookiePrefix'] . 'comment_user',$post['user'],$lifetime,'/','',0);
70                         setcookie($CONF['CookiePrefix'] . 'comment_userid', $post['userid'],$lifetime,'/','',0);
71                 }
72
73                 $comments = new COMMENTS($post['itemid']);
74
75                 $blogid = getBlogIDFromItemID($post['itemid']);
76                 $this->checkban($blogid);
77                 $blog =& $manager->getBlog($blogid);
78
79                 // note: PreAddComment and PostAddComment gets called somewhere inside addComment
80                 $errormessage = $comments->addComment($blog->getCorrectTime(),$post);
81
82                 if ($errormessage == '1') {             
83                         // redirect when adding comments succeeded
84                         if (postVar('url')) {
85                                 redirect(postVar('url'));
86                         } else {
87                                 $url = createItemLink($post['itemid']);
88                                 redirect($url);
89                         }
90                 } else {
91                         // else, show error message using default skin for blog
92                         return array(
93                                 'message' => $errormessage,
94                                 'skinid' => $blog->getDefaultSkin()
95                         );
96                 }
97                 
98                 exit;
99         }
100
101         // Sends a message from the current member to the member given as argument
102         function sendMessage() {
103                 global $CONF, $member;
104
105                 $error = $this->validateMessage();
106                 if ($error != '')
107                         return array('message' => $error);
108
109                 if (!$member->isLoggedIn()) {
110                         $fromMail = postVar('frommail');
111                         $fromName = _MMAIL_FROMANON;
112                 } else {
113                         $fromMail = $member->getEmail();
114                         $fromName = $member->getDisplayName();
115                 }
116
117                 $tomem = new MEMBER();
118                 $tomem->readFromId(postVar('memberid'));
119
120                 $message  = _MMAIL_MSG . ' ' . $fromName . "\n"
121                           . '(' . _MMAIL_FROMNUC. ' ' . $CONF['IndexURL'] .") \n\n"
122                           . _MMAIL_MAIL . " \n\n"
123                           . postVar('message');
124                 $message .= getMailFooter();
125
126                 $title = _MMAIL_TITLE . ' ' . $fromName;
127                 mb_language('ja');
128                 mb_internal_encoding(_CHARSET);
129                 @mb_send_mail($tomem->getEmail(), $title, $message, "From: ". $fromMail);
130
131                 if (postVar('url')) {
132                         redirect(postVar('url'));
133                 } else {
134                         $CONF['MemberURL'] = $CONF['IndexURL'];
135                         if ($CONF['URLMode'] == 'pathinfo')
136                         {
137                                 $url = createLink('member', array('memberid' => $tomem->getID(), 'name' => $tomem->getDisplayName()));
138                         }
139                         else
140                         {
141                                 $url = $CONF['IndexURL'] . createMemberLink($tomem->getID());
142                         }
143                         redirect($url);
144                 }
145                 
146                 exit;
147
148         }
149         
150         function validateMessage() {
151                 global $CONF, $member, $manager;
152
153                 if (!$CONF['AllowMemberMail']) 
154                         return _ERROR_MEMBERMAILDISABLED;
155
156                 if (!$member->isLoggedIn() && !$CONF['NonmemberMail'])
157                         return _ERROR_DISALLOWED;
158
159                 if (!$member->isLoggedIn() && (!isValidMailAddress(postVar('frommail'))))
160                         return _ERROR_BADMAILADDRESS;
161                         
162                 // let plugins do verification (any plugin which thinks the comment is invalid
163                 // can change 'error' to something other than '')
164                 $result = '';
165                 $manager->notify('ValidateForm', array('type' => 'membermail', 'error' => &$result));
166                 
167                 return $result;
168                 
169         }
170
171         // creates a new user account
172         function createAccount() {
173                 global $CONF, $manager;
174
175                 if (!$CONF['AllowMemberCreate']) 
176                         doError(_ERROR_MEMBERCREATEDISABLED);
177
178                 // even though the member can not log in, set some random initial password. One never knows.
179                 srand((double)microtime()*1000000);
180                 $initialPwd = md5(uniqid(rand(), true));
181
182                 // create member (non admin/can not login/no notes/random string as password)
183                 $r = MEMBER::create(postVar('name'), postVar('realname'), $initialPwd, postVar('email'), postVar('url'), 0, 0, '');
184                 
185                 if ($r != 1)
186                         doError($r);
187                         
188                 // send message containing password.
189                 $newmem = new MEMBER();
190                 $newmem->readFromName(postVar('name'));
191                 $newmem->sendActivationLink('register');
192
193                 $manager->notify('PostRegister',array('member' => &$newmem));           
194
195                 if (postVar('desturl')) {
196                         redirect(postVar('desturl'));
197                 } else {
198                         header ("Content-Type: text/html; charset="._CHARSET);
199                         echo _MSG_ACTIVATION_SENT;
200                 }
201                 
202                 exit;
203         }
204
205         // sends a new password 
206         function forgotPassword() {
207                 $membername = trim(postVar('name'));
208
209                 if (!MEMBER::exists($membername))
210                         doError(_ERROR_NOSUCHMEMBER);
211                 $mem = MEMBER::createFromName($membername);
212
213                 if (!$mem->canLogin())
214                         doError(_ERROR_NOLOGON_NOACTIVATE);
215
216                 // check if e-mail address is correct
217                 if (!($mem->getEmail() == postVar('email')))
218                         doError(_ERROR_INCORRECTEMAIL);
219
220                 // send activation link
221                 $mem->sendActivationLink('forgot');
222
223                 if (postVar('url')) {
224                         redirect(postVar('url'));
225                 } else {
226                         header ("Content-Type: text/html; charset="._CHARSET);
227                         echo _MSG_ACTIVATION_SENT;
228                 }
229                 
230                 exit;
231         }
232
233         // handle karma votes
234         function doKarma($type) {
235                 global $itemid, $member, $CONF, $manager;
236
237                 // check if itemid exists
238                 if (!$manager->existsItem($itemid,0,0)) 
239                         doError(_ERROR_NOSUCHITEM);
240
241                 $blogid = getBlogIDFromItemID($itemid);
242                 $this->checkban($blogid);       
243
244                 $karma =& $manager->getKarma($itemid);
245
246                 // check if not already voted
247                 if (!$karma->isVoteAllowed(serverVar('REMOTE_ADDR'))) 
248                         doError(_ERROR_VOTEDBEFORE);            
249
250                 // check if item does allow voting
251                 $item =& $manager->getItem($itemid,0,0);
252                 if ($item['closed'])
253                         doError(_ERROR_ITEMCLOSED);
254
255                 switch($type) {
256                         case 'pos': 
257                                 $karma->votePositive();
258                                 break;
259                         case 'neg':
260                                 $karma->voteNegative();
261                                 break;
262                 }
263
264                 $blogid = getBlogIDFromItemID($itemid);
265                 $blog =& $manager->getBlog($blogid);
266
267                 // send email to notification address, if any
268                 if ($blog->getNotifyAddress() && $blog->notifyOnVote()) {
269
270                         $mailto_msg = _NOTIFY_KV_MSG . ' ' . $itemid . "\n";
271                         $mailto_msg .= $CONF['IndexURL'] . 'index.php?itemid=' . $itemid . "\n\n";
272                         if ($member->isLoggedIn()) {
273                                 $mailto_msg .= _NOTIFY_MEMBER . ' ' . $member->getDisplayName() . ' (ID=' . $member->getID() . ")\n";
274                         }
275                         $mailto_msg .= _NOTIFY_IP . ' ' . serverVar('REMOTE_ADDR') . "\n";
276                         $mailto_msg .= _NOTIFY_HOST . ' ' .  gethostbyaddr(serverVar('REMOTE_ADDR'))  . "\n";
277                         $mailto_msg .= _NOTIFY_VOTE . "\n " . $type . "\n";
278                         $mailto_msg .= getMailFooter();
279
280                         $mailto_title = _NOTIFY_KV_TITLE . ' ' . strip_tags($item['title']) . ' (' . $itemid . ')';
281
282                         $frommail = $member->getNotifyFromMailAddress();
283
284                         $notify = new NOTIFICATION($blog->getNotifyAddress());
285                         $notify->notify($mailto_title, $mailto_msg , $frommail);
286                 }
287
288
289                 $refererUrl = serverVar('HTTP_REFERER');
290                 if ($refererUrl)
291                         $url = $refererUrl;
292                 else
293                         $url = $CONF['IndexURL'] . 'index.php?itemid=' . $itemid;
294
295                 redirect($url); 
296                 exit;
297         }
298
299         /**
300           * Calls a plugin action
301           */
302         function callPlugin() {
303                 global $manager;
304
305                 $pluginName = 'NP_' . requestVar('name');
306                 $actionType = requestVar('type');
307
308                 // 1: check if plugin is installed
309                 if (!$manager->pluginInstalled($pluginName))
310                         doError(_ERROR_NOSUCHPLUGIN);
311
312                 // 2: call plugin
313                 $pluginObject =& $manager->getPlugin($pluginName);
314                 if ($pluginObject)
315                         $error = $pluginObject->doAction($actionType);
316                 else
317                         $error = 'Could not load plugin (see actionlog)';
318
319                 // doAction returns error when:
320                 // - an error occurred (duh)
321                 // - no actions are allowed (doAction is not implemented)
322                 if ($error)
323                         doError($error);
324                         
325                 exit;
326
327         }
328
329         function checkban($blogid) {
330                 // check if banned
331                 $ban = BAN::isBanned($blogid, serverVar('REMOTE_ADDR'));
332                 if ($ban != 0) {
333                         doError(_ERROR_BANNED1 . $ban->iprange . _ERROR_BANNED2 . $ban->message . _ERROR_BANNED3);
334                 }
335
336         }
337
338
339 }
340
341 ?>