OSDN Git Service

Merged codes from sourceforge.net.
[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-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  * 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-2007 The Nucleus Group
18  * @version $Id: ACTION.php,v 1.11 2008-02-08 09:31:22 kimitake Exp $
19  * $NucleusJP: ACTION.php,v 1.10 2007/05/31 07:23:39 kimitake Exp $
20  */
21 class ACTION
22 {
23         /**
24          *  Constructor for an new ACTION object
25          */
26         function ACTION()
27         {
28                 // do nothing
29         }
30
31         /**
32          *  Calls functions that handle an action called from action.php
33          */
34         function doAction($action)
35         {
36                 switch($action) {
37                         case 'autodraft':
38                                 return $this->autoDraft();
39                                 break;
40                         case 'updateticket':
41                                 return $this->updateTicket();
42                                 break;
43                         case 'addcomment':
44                                 return $this->addComment();
45                                 break;
46                         case 'sendmessage':
47                                 return $this->sendMessage();
48                                 break;
49                         case 'createaccount':
50                                 return $this->createAccount();
51                                 break;
52                         case 'forgotpassword':
53                                 return $this->forgotPassword();
54                                 break;
55                         case 'votepositive':
56                                 return $this->doKarma('pos');
57                                 break;
58                         case 'votenegative':
59                                 return $this->doKarma('neg');
60                                 break;
61                         case 'plugin':
62                                 return $this->callPlugin();
63                                 break;
64                         default:
65                                 doError(_ERROR_BADACTION);
66                 }
67         }
68
69         /**
70          *  Adds a new comment to an item (if IP isn't banned)
71          */
72         function addComment() {
73                 global $CONF, $errormessage, $manager;
74
75                 $post['itemid'] =       intPostVar('itemid');
76                 $post['user'] =         postVar('user');
77                 $post['userid'] =       postVar('userid');
78                 $post['email'] =   postVar('email');
79                 $post['body'] =         postVar('body');
80
81                 // set cookies when required
82                 $remember = intPostVar('remember');
83                 if ($remember == 1) {
84                         $lifetime = time()+2592000;
85                         setcookie($CONF['CookiePrefix'] . 'comment_user',$post['user'],$lifetime,'/','',0);
86                         setcookie($CONF['CookiePrefix'] . 'comment_userid', $post['userid'],$lifetime,'/','',0);
87                         setcookie($CONF['CookiePrefix'] . 'comment_email',  $post['email'], $lifetime,'/','',0);
88                 }
89
90                 $comments = new COMMENTS($post['itemid']);
91
92                 $blogid = getBlogIDFromItemID($post['itemid']);
93                 $this->checkban($blogid);
94                 $blog =& $manager->getBlog($blogid);
95
96                 // note: PreAddComment and PostAddComment gets called somewhere inside addComment
97                 $errormessage = $comments->addComment($blog->getCorrectTime(),$post);
98
99                 if ($errormessage == '1') {
100                         // redirect when adding comments succeeded
101                         if (postVar('url')) {
102                                 redirect(postVar('url'));
103                         } else {
104                                 $url = createItemLink($post['itemid']);
105                                 redirect($url);
106                         }
107                 } else {
108                         // else, show error message using default skin for blog
109                         return array(
110                                 'message' => $errormessage,
111                                 'skinid' => $blog->getDefaultSkin()
112                         );
113                 }
114
115                 exit;
116         }
117
118         /**
119          *  Sends a message from the current member to the member given as argument
120          */
121         function sendMessage() {
122                 global $CONF, $member;
123
124                 $error = $this->validateMessage();
125                 if ($error != '')
126                         return array('message' => $error);
127
128                 if (!$member->isLoggedIn()) {
129                         $fromMail = postVar('frommail');
130                         $fromName = _MMAIL_FROMANON;
131                 } else {
132                         $fromMail = $member->getEmail();
133                         $fromName = $member->getDisplayName();
134                 }
135
136                 $tomem = new MEMBER();
137                 $tomem->readFromId(postVar('memberid'));
138
139                 $message  = _MMAIL_MSG . ' ' . $fromName . "\n"
140                           . '(' . _MMAIL_FROMNUC. ' ' . $CONF['IndexURL'] .") \n\n"
141                           . _MMAIL_MAIL . " \n\n"
142                           . postVar('message');
143                 $message .= getMailFooter();
144
145                 $title = _MMAIL_TITLE . ' ' . $fromName;
146                 mb_language('ja');
147                 mb_internal_encoding(_CHARSET);
148                 @mb_send_mail($tomem->getEmail(), $title, $message, "From: ". $fromMail);
149
150                 if (postVar('url')) {
151                         redirect(postVar('url'));
152                 } else {
153                         $CONF['MemberURL'] = $CONF['IndexURL'];
154                         if ($CONF['URLMode'] == 'pathinfo')
155                         {
156                                 $url = createLink('member', array('memberid' => $tomem->getID(), 'name' => $tomem->getDisplayName()));
157                         }
158                         else
159                         {
160                                 $url = $CONF['IndexURL'] . createMemberLink($tomem->getID());
161                         }
162                         redirect($url);
163                 }
164                 exit;
165         }
166
167         /**
168          *  Checks if a mail to a member is allowed
169          *  Returns a string with the error message if the mail is disallowed
170          */
171         function validateMessage() {
172                 global $CONF, $member, $manager;
173
174                 if (!$CONF['AllowMemberMail'])
175                         return _ERROR_MEMBERMAILDISABLED;
176
177                 if (!$member->isLoggedIn() && !$CONF['NonmemberMail'])
178                         return _ERROR_DISALLOWED;
179
180                 if (!$member->isLoggedIn() && (!isValidMailAddress(postVar('frommail'))))
181                         return _ERROR_BADMAILADDRESS;
182
183                 // let plugins do verification (any plugin which thinks the comment is invalid
184                 // can change 'error' to something other than '')
185                 $result = '';
186                 $manager->notify('ValidateForm', array('type' => 'membermail', 'error' => &$result));
187
188                 return $result;
189
190         }
191
192         /**
193          *  Creates a new user account
194          */
195         function createAccount() {
196                 global $CONF, $manager;
197
198                 if (!$CONF['AllowMemberCreate'])
199                         doError(_ERROR_MEMBERCREATEDISABLED);
200
201                 // evaluate content from FormExtra
202                 $result = 1;
203                 $manager->notify('ValidateForm', array('type' => 'membermail', 'error' => &$result));
204
205                 if ($result!=1) {
206                         return $result;
207                 }
208                 else {
209
210                         // even though the member can not log in, set some random initial password. One never knows.
211                         srand((double)microtime()*1000000);
212                         $initialPwd = md5(uniqid(rand(), true));
213
214                         // create member (non admin/can not login/no notes/random string as password)
215                         $r = MEMBER::create(postVar('name'), postVar('realname'), $initialPwd, postVar('email'), postVar('url'), 0, 0, '');
216
217                         if ($r != 1) {
218                                 return $r;
219                         }
220
221                         // send message containing password.
222                         $newmem = new MEMBER();
223                         $newmem->readFromName(postVar('name'));
224                         $newmem->sendActivationLink('register');
225
226                         $manager->notify('PostRegister',array('member' => &$newmem));
227
228                         if (postVar('desturl')) {
229                                 redirect(postVar('desturl'));
230                         } else {
231                                 // header has been already sent, so deleted the line below
232                                 //header ("Content-Type: text/html; charset="._CHARSET);
233                                 echo _MSG_ACTIVATION_SENT;
234                         }
235                         exit;
236                 }
237         }
238
239         /**
240          *  Sends a new password
241          */
242         function forgotPassword() {
243                 $membername = trim(postVar('name'));
244
245                 if (!MEMBER::exists($membername))
246                         doError(_ERROR_NOSUCHMEMBER);
247                 $mem = MEMBER::createFromName($membername);
248
249                 if (!$mem->canLogin())
250                         doError(_ERROR_NOLOGON_NOACTIVATE);
251
252                 // check if e-mail address is correct
253                 if (!($mem->getEmail() == postVar('email')))
254                         doError(_ERROR_INCORRECTEMAIL);
255
256                 // send activation link
257                 $mem->sendActivationLink('forgot');
258
259                 if (postVar('url')) {
260                         redirect(postVar('url'));
261                 } else {
262                         header ("Content-Type: text/html; charset="._CHARSET);
263                         echo _MSG_ACTIVATION_SENT;
264                 }
265                 exit;
266         }
267
268         /**
269          *  Handle karma votes
270          */
271         function doKarma($type) {
272                 global $itemid, $member, $CONF, $manager;
273
274                 // check if itemid exists
275                 if (!$manager->existsItem($itemid,0,0))
276                         doError(_ERROR_NOSUCHITEM);
277
278                 $blogid = getBlogIDFromItemID($itemid);
279                 $this->checkban($blogid);
280
281                 $karma =& $manager->getKarma($itemid);
282
283                 // check if not already voted
284                 if (!$karma->isVoteAllowed(serverVar('REMOTE_ADDR')))
285                         doError(_ERROR_VOTEDBEFORE);
286
287                 // check if item does allow voting
288                 $item =& $manager->getItem($itemid,0,0);
289                 if ($item['closed'])
290                         doError(_ERROR_ITEMCLOSED);
291
292                 switch($type) {
293                         case 'pos':
294                                 $karma->votePositive();
295                                 break;
296                         case 'neg':
297                                 $karma->voteNegative();
298                                 break;
299                 }
300
301                 $blogid = getBlogIDFromItemID($itemid);
302                 $blog =& $manager->getBlog($blogid);
303
304                 // send email to notification address, if any
305                 if ($blog->getNotifyAddress() && $blog->notifyOnVote()) {
306
307                         $mailto_msg = _NOTIFY_KV_MSG . ' ' . $itemid . "\n";
308                         $mailto_msg .= $CONF['IndexURL'] . 'index.php?itemid=' . $itemid . "\n\n";
309                         if ($member->isLoggedIn()) {
310                                 $mailto_msg .= _NOTIFY_MEMBER . ' ' . $member->getDisplayName() . ' (ID=' . $member->getID() . ")\n";
311                         }
312                         $mailto_msg .= _NOTIFY_IP . ' ' . serverVar('REMOTE_ADDR') . "\n";
313                         $mailto_msg .= _NOTIFY_HOST . ' ' .  gethostbyaddr(serverVar('REMOTE_ADDR'))  . "\n";
314                         $mailto_msg .= _NOTIFY_VOTE . "\n " . $type . "\n";
315                         $mailto_msg .= getMailFooter();
316
317                         $mailto_title = _NOTIFY_KV_TITLE . ' ' . strip_tags($item['title']) . ' (' . $itemid . ')';
318
319                         $frommail = $member->getNotifyFromMailAddress();
320
321                         $notify = new NOTIFICATION($blog->getNotifyAddress());
322                         $notify->notify($mailto_title, $mailto_msg , $frommail);
323                 }
324
325
326                 $refererUrl = serverVar('HTTP_REFERER');
327                 if ($refererUrl)
328                         $url = $refererUrl;
329                 else
330                         $url = $CONF['IndexURL'] . 'index.php?itemid=' . $itemid;
331
332                 redirect($url);
333                 exit;
334         }
335
336         /**
337           * Calls a plugin action
338           */
339         function callPlugin() {
340                 global $manager;
341
342                 $pluginName = 'NP_' . requestVar('name');
343                 $actionType = requestVar('type');
344
345                 // 1: check if plugin is installed
346                 if (!$manager->pluginInstalled($pluginName))
347                         doError(_ERROR_NOSUCHPLUGIN);
348
349                 // 2: call plugin
350                 $pluginObject =& $manager->getPlugin($pluginName);
351                 if ($pluginObject)
352                         $error = $pluginObject->doAction($actionType);
353                 else
354                         $error = 'Could not load plugin (see actionlog)';
355
356                 // doAction returns error when:
357                 // - an error occurred (duh)
358                 // - no actions are allowed (doAction is not implemented)
359                 if ($error)
360                         doError($error);
361
362                 exit;
363
364         }
365
366         /**
367          *  Checks if an IP or IP range is banned
368          */
369         function checkban($blogid) {
370                 // check if banned
371                 $ban = BAN::isBanned($blogid, serverVar('REMOTE_ADDR'));
372                 if ($ban != 0) {
373                         doError(_ERROR_BANNED1 . $ban->iprange . _ERROR_BANNED2 . $ban->message . _ERROR_BANNED3);
374                 }
375
376         }
377
378         /**
379          * Gets a new ticket
380          */
381         function updateTicket() {
382                 global $manager;
383                 if ($manager->checkTicket()) {
384                         echo $manager->getNewTicket();
385                 }
386                 else {
387                         echo 'err:' . _ERROR_BADTICKET;
388                 }
389                 return false;
390         }
391
392         /**
393          * Handles AutoSaveDraft
394          */
395         function autoDraft() {
396                 global $manager;
397                 if ($manager->checkTicket()) {
398                         $manager->loadClass('ITEM');
399                         $info = ITEM::createDraftFromRequest();
400                         if ($info['status'] == 'error') {
401                                 echo $info['message'];
402                         }
403                         else {
404                                 echo $info['draftid'];
405                         }
406                 }
407                 else {
408                         echo 'err:' . _ERROR_BADTICKET;
409                 }
410                 return false;
411         }
412
413
414 }
415
416 ?>