OSDN Git Service

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