OSDN Git Service

本家Nucleus CMSの開発を補助するためにコミット
[nucleus-jp/nucleus-next.git] / nucleus / libs / ACTION.php
1 <?php
2
3 /*
4  * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5  * Copyright (C) 2002-2009 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-2009 The Nucleus Group
18  * @version $Id: ACTION.php 1522 2011-06-21 09:46:14Z sakamocchi $
19  */
20 class ACTION
21 {
22
23         /**
24          *  Constructor for an new ACTION object
25          */
26         function ACTION()
27         {
28                 // do nothing
29         }
30
31
32         /**
33          *  Calls functions that handle an action called from action.php
34          */
35         function doAction($action)
36         {
37                 switch($action)
38                 {
39                         case 'autodraft':
40                                 return $this->autoDraft();
41                         break;
42                 
43                         case 'updateticket':
44                                 return $this->updateTicket();
45                         break;
46
47                         case 'addcomment':
48                                 return $this->addComment();
49                         break;
50
51                         case 'sendmessage':
52                                 return $this->sendMessage();
53                         break;
54
55                         case 'createaccount':
56                                 return $this->createAccount();
57                         break;
58
59                         case 'forgotpassword':
60                                 return $this->forgotPassword();
61                         break;
62
63                         case 'votepositive':
64                                 return $this->doKarma('pos');
65                         break;
66
67                         case 'votenegative':
68                                 return $this->doKarma('neg');
69                         break;
70
71                         case 'plugin':
72                                 return $this->callPlugin();
73                         break;
74
75                         default:
76                                 doError(_ERROR_BADACTION);
77                         break;
78                 }
79         }
80
81
82         /**
83          *  Adds a new comment to an item (if IP isn't banned)
84          */
85         function addComment()
86         {
87                 global $CONF, $errormessage, $manager;
88
89                 $post['itemid']         = intPostVar('itemid');
90                 $post['user']           = postVar('user');
91                 $post['userid']         = postVar('userid');
92                 $post['email']          = postVar('email');
93                 $post['body']           = postVar('body');
94                 $post['remember']       = intPostVar('remember');
95
96                 // set cookies when required
97                 #$remember = intPostVar('remember');
98
99                 // begin if: "Remember Me" box checked
100                 if ( $post['remember'] == 1 )
101                 {
102                         $lifetime = time() + 2592000;
103                         setcookie($CONF['CookiePrefix'] . 'comment_user', $post['user'], $lifetime, '/', '', 0);
104                         setcookie($CONF['CookiePrefix'] . 'comment_userid', $post['userid'], $lifetime, '/', '', 0);
105                         setcookie($CONF['CookiePrefix'] . 'comment_email', $post['email'], $lifetime, '/', '', 0);
106                 } // end if
107
108                 $comments = new COMMENTS($post['itemid']);
109
110                 $blog_id = getBlogIDFromItemID($post['itemid']);
111                 $this->checkban($blog_id);
112                 $blog =& $manager->getBlog($blog_id);
113
114                 // note: PreAddComment and PostAddComment gets called somewhere inside addComment
115                 $errormessage = $comments->addComment($blog->getCorrectTime(), $post);
116
117                 // begin if:
118                 if ( $errormessage == '1' )
119                 {
120                         // redirect when adding comments succeeded
121                         if ( postVar('url') )
122                         {
123                                 redirect(postVar('url') );
124                         }
125                         else
126                         {
127                                 $url = createItemLink($post['itemid']);
128                                 redirect($url);
129                         } // end if
130
131                 }
132                 // else, show error message using default skin for blo
133                 else
134                 {
135                         return array(
136                                 'message'       => $errormessage,
137                                 'skinid'        => $blog->getDefaultSkin()
138                         );
139                 } // end if
140
141                 exit;
142         }
143
144
145         /**
146          *  Sends a message from the current member to the member given as argument
147          */
148         function sendMessage()
149         {
150                 global $CONF, $member;
151                 
152                 $error = $this->validateMessage();
153                 
154                 if ( $error != '' )
155                 {
156                         return array('message' => $error);
157                 }
158                 
159                 if ( !$member->isLoggedIn() )
160                 {
161                         $fromMail = postVar('frommail');
162                         $fromName = _MMAIL_FROMANON;
163                 }
164                 else
165                 {
166                         $fromMail = $member->getEmail();
167                         $fromName = $member->getDisplayName();
168                 }
169                 
170                 $tomem = new MEMBER();
171                 $tomem->readFromId(postVar('memberid') );
172
173                 $message  = _MMAIL_MSG . ' ' . $fromName . "\n"
174                           . '(' . _MMAIL_FROMNUC. ' ' . $CONF['IndexURL'] .") \n\n"
175                           . _MMAIL_MAIL . " \n\n"
176                           . postVar('message');
177                 $message .= getMailFooter();
178                 
179                 $title = _MMAIL_TITLE . ' ' . $fromName;
180                 i18n::mail($tomem->getEmail(), $title, $message, $fromMail);
181                 
182                 if ( postVar('url') )
183                 {
184                         redirect(postVar('url') );
185                 }
186                 else
187                 {
188                         $CONF['MemberURL'] = $CONF['IndexURL'];
189                         
190                         if ( $CONF['URLMode'] == 'pathinfo' )
191                         {
192                                 $url = createLink('member', array('memberid' => $tomem->getID(), 'name' => $tomem->getDisplayName() ) );
193                         }
194                         else
195                         {
196                                 $url = $CONF['IndexURL'] . createMemberLink($tomem->getID());
197                         }
198                         
199                         redirect($url);
200                 }
201                 exit;
202         }
203         
204         
205         /**
206          *  Checks if a mail to a member is allowed
207          *  Returns a string with the error message if the mail is disallowed
208          */
209         function validateMessage()
210         {
211                 global $CONF, $member, $manager;
212
213                 if ( !$CONF['AllowMemberMail'] )
214                 {
215                         return _ERROR_MEMBERMAILDISABLED;
216                 }
217
218                 if ( !$member->isLoggedIn() && !$CONF['NonmemberMail'] )
219                 {
220                         return _ERROR_DISALLOWED;
221                 }
222
223                 if ( !$member->isLoggedIn() && (!isValidMailAddress(postVar('frommail') ) ) )
224                 {
225                         return _ERROR_BADMAILADDRESS;
226                 }
227
228                 // let plugins do verification (any plugin which thinks the comment is invalid
229                 // can change 'error' to something other than '')
230                 $result = '';
231                 $manager->notify('ValidateForm', array('type' => 'membermail', 'error' => &$result) );
232
233                 return $result;
234
235         }
236
237
238         /**
239          *  Creates a new user account
240          */
241         function createAccount()
242         {
243                 global $CONF, $manager;
244
245                 if ( !$CONF['AllowMemberCreate'] )
246                 {
247                         doError(_ERROR_MEMBERCREATEDISABLED);
248                 }
249
250                 // evaluate content from FormExtra
251                 $result = 1;
252                 $data = array('type' => 'membermail', 'error' => &$result);
253                 $manager->notify('ValidateForm', &$data);
254
255                 if ( $result != 1 )
256                 {
257                         return $result;
258                 }
259                 else
260                 {
261
262                         // even though the member can not log in, set some random initial password. One never knows.
263                         srand( (double) microtime() * 1000000);
264                         $initialPwd = md5(uniqid(rand(), TRUE) );
265
266                         // create member (non admin/can not login/no notes/random string as password)
267                         $name = shorten(postVar('name'), 32, '');
268                         $r = MEMBER::create($name, postVar('realname'), $initialPwd, postVar('email'), postVar('url'), 0, 0, '');
269
270                         if ( $r != 1 )
271                         {
272                                 return $r;
273                         }
274
275                         // send message containing password.
276                         $newmem = new MEMBER();
277                         $newmem->readFromName($name);
278                         $newmem->sendActivationLink('register');
279
280                         $manager->notify('PostRegister', array('member' => &$newmem) );
281
282                         if ( postVar('desturl') )
283                         {
284                                 redirect(postVar('desturl') );
285                         }
286                         else
287                         {
288                                 echo _MSG_ACTIVATION_SENT;
289                                 echo '<br /><br />Return to <a href="'.$CONF['IndexURL'].'" title="'.$CONF['SiteName'].'">'.$CONF['SiteName'].'</a>';
290                                 echo "\n</body>\n</html>";
291                         }
292
293                         exit;
294                 }
295
296         }
297
298
299         /**
300          *  Sends a new password
301          */
302         function forgotPassword()
303         {
304                 $membername = trim(postVar('name') );
305
306                 if ( !MEMBER::exists($membername) )
307                 {
308                         doError(_ERROR_NOSUCHMEMBER);
309                 }
310
311                 $mem = MEMBER::createFromName($membername);
312                 
313                 /* below keeps regular users from resetting passwords using forgot password feature
314                      Removing for now until clear why it is required.*/
315                 /*if (!$mem->canLogin())
316                         doError(_ERROR_NOLOGON_NOACTIVATE);*/
317
318                 // check if e-mail address is correct
319                 if ( !($mem->getEmail() == postVar('email') ) )
320                 {
321                         doError(_ERROR_INCORRECTEMAIL);
322                 }
323
324                 // send activation link
325                 $mem->sendActivationLink('forgot');
326
327                 if ( postVar('url') )
328                 {
329                         redirect(postVar('url') );
330                 }
331                 else
332                 {
333                         echo _MSG_ACTIVATION_SENT;
334                         echo '<br /><br />Return to <a href="'.$CONF['IndexURL'].'" title="'.$CONF['SiteName'].'">'.$CONF['SiteName'].'</a>';
335                 }
336
337                 exit;
338         }
339
340
341         /**
342          *  Handle karma votes
343          */
344         function doKarma($type)
345         {
346                 global $itemid, $member, $CONF, $manager;
347
348                 // check if itemid exists
349                 if ( !$manager->existsItem($itemid, 0, 0) )
350                 {
351                         doError(_ERROR_NOSUCHITEM);
352                 }
353
354                 $blogid = getBlogIDFromItemID($itemid);
355                 $this->checkban($blogid);
356
357                 $karma =& $manager->getKarma($itemid);
358
359                 // check if not already voted
360                 if ( !$karma->isVoteAllowed(serverVar('REMOTE_ADDR') ) )
361                 {
362                         doError(_ERROR_VOTEDBEFORE);
363                 }
364
365                 // check if item does allow voting
366                 $item =& $manager->getItem($itemid, 0, 0);
367
368                 if ( $item['closed'] )
369                 {
370                         doError(_ERROR_ITEMCLOSED);
371                 }
372
373                 switch ( $type )
374                 {
375                         case 'pos':
376                                 $karma->votePositive();
377                         break;
378
379                         case 'neg':
380                                 $karma->voteNegative();
381                         break;
382                 }
383
384 //              $blogid = getBlogIDFromItemID($itemid);
385                 $blog =& $manager->getBlog($blogid);
386
387                 // send email to notification address, if any
388                 if ( $blog->getNotifyAddress() && $blog->notifyOnVote() )
389                 {
390
391                         $mailto_msg = _NOTIFY_KV_MSG . ' ' . $itemid . "\n";
392 //                      if ($CONF['URLMode'] == 'pathinfo') {
393 //                              $itemLink = createItemLink(intval($itemid));
394 //                      } else {
395 //                              $itemLink = $CONF['IndexURL'] . createItemLink(intval($itemid));
396 //                      }
397 //                      $mailto_msg .= $CONF['IndexURL'] . 'index.php?itemid=' . $itemid . "\n\n";
398                         $itemLink = createItemLink(intval($itemid) );
399                         $temp = parse_url($itemLink);
400
401                         if ( !$temp['scheme'] )
402                         {
403                                 $itemLink = $CONF['IndexURL'] . $itemLink;
404                         }
405
406                         $mailto_msg .= $itemLink . "\n\n";
407
408                         if ( $member->isLoggedIn() )
409                         {
410                                 $mailto_msg .= _NOTIFY_MEMBER . ' ' . $member->getDisplayName() . ' (ID=' . $member->getID() . ")\n";
411                         }
412
413                         $mailto_msg .= _NOTIFY_IP . ' ' . serverVar('REMOTE_ADDR') . "\n";
414                         $mailto_msg .= _NOTIFY_HOST . ' ' .  gethostbyaddr(serverVar('REMOTE_ADDR'))  . "\n";
415                         $mailto_msg .= _NOTIFY_VOTE . "\n " . $type . "\n";
416                         $mailto_msg .= getMailFooter();
417
418                         $mailto_title = _NOTIFY_KV_TITLE . ' ' . strip_tags($item['title']) . ' (' . $itemid . ')';
419
420                         $frommail = $member->getNotifyFromMailAddress();
421
422                         $notify = new NOTIFICATION($blog->getNotifyAddress() );
423                         $notify->notify($mailto_title, $mailto_msg, $frommail);
424                 }
425
426                 $refererUrl = serverVar('HTTP_REFERER');
427
428                 if ( $refererUrl )
429                 {
430                         $url = $refererUrl;
431                 }
432                 else
433                 {
434 //                      $url = $CONF['IndexURL'] . 'index.php?itemid=' . $itemid;
435                         $url = $itemLink;
436                 }
437
438                 redirect($url);
439                 exit;
440         }
441
442
443         /**
444           * Calls a plugin action
445           */
446         function callPlugin()
447         {
448                 global $manager;
449
450                 $pluginName = 'NP_' . requestVar('name');
451                 $actionType = requestVar('type');
452
453                 // 1: check if plugin is installed
454                 if ( !$manager->pluginInstalled($pluginName) )
455                 {
456                         doError(_ERROR_NOSUCHPLUGIN);
457                 }
458
459                 // 2: call plugin
460                 $pluginObject =& $manager->getPlugin($pluginName);
461
462                 if ( $pluginObject )
463                 {
464                         $error = $pluginObject->doAction($actionType);
465                 }
466                 else
467                 {
468                         $error = 'Could not load plugin (see actionlog)';
469                 }
470
471                 // doAction returns error when:
472                 // - an error occurred (duh)
473                 // - no actions are allowed (doAction is not implemented)
474                 if ( $error )
475                 {
476                         doError($error);
477                 }
478
479                 exit;
480
481         }
482
483
484         /**
485          *  Checks if an IP or IP range is banned
486          */
487         function checkban($blogid)
488         {
489                 // check if banned
490                 $ban = BAN::isBanned($blogid, serverVar('REMOTE_ADDR') );
491
492                 if ( $ban != 0 )
493                 {
494                         doError(_ERROR_BANNED1 . $ban->iprange . _ERROR_BANNED2 . $ban->message . _ERROR_BANNED3);
495                 }
496
497         }
498
499
500         /**
501          * Gets a new ticket
502          */
503         function updateTicket()
504         {
505                 global $manager;
506
507                 if ( $manager->checkTicket() )
508                 {
509                         echo $manager->getNewTicket();
510                 }
511                 else
512                 {
513                         echo _ERROR . ':' . _ERROR_BADTICKET;
514                 }
515
516                 return FALSE;
517         }
518
519
520         /**
521          * Handles AutoSaveDraft
522          */
523         function autoDraft()
524         {
525                 global $manager;
526
527                 if ( $manager->checkTicket() )
528                 {
529                         $manager->loadClass('ITEM');
530                         $info = ITEM::createDraftFromRequest();
531
532                         if ( $info['status'] == 'error' )
533                         {
534                                 echo $info['message'];
535                         }
536                         else
537                         {
538                                 echo $info['draftid'];
539                         }
540                 }
541                 else
542                 {
543             echo _ERROR . ':' . _ERROR_BADTICKET;
544                 }
545
546                 return FALSE;
547         }
548
549 }
550