OSDN Git Service

CHANGE: フィードとゲストアカウント作成フォームのためのスクリプトを修正
[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 1646 2012-01-29 10:47:32Z 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          * ACTION::addComment()
83          * Adds a new comment to an item (if IP isn't banned)
84          * 
85          * @param       Void
86          * @return      Void
87          */
88         function addComment()
89         {
90                 global $CONF, $errormessage, $manager;
91                 
92                 $post['itemid']         = intPostVar('itemid');
93                 $post['user']           = postVar('user');
94                 $post['userid']         = postVar('userid');
95                 $post['email']          = postVar('email');
96                 $post['body']           = postVar('body');
97                 $post['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                 }
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                 if ( $errormessage == '1' )
118                 {
119                         // redirect when adding comments succeeded
120                         if ( postVar('url') )
121                         {
122                                 redirect(postVar('url') );
123                         }
124                         else
125                         {
126                                 $url = LINK::create_item_link($post['itemid']);
127                                 redirect($url);
128                         }
129                 }
130                 // else, show error message using default skin for blo
131                 else
132                 {
133                         return array(
134                                 'message'       => $errormessage,
135                                 'skinid'        => $blog->getDefaultSkin()
136                         );
137                 }
138                 exit;
139         }
140         
141         /**
142          * ACTION::sendMessage()
143          * Sends a message from the current member to the member given as argument
144          * 
145          * @param       Void
146          * @return      Void
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 .= NOTIFICATION::get_mail_footer();
178                 
179                 $title = _MMAIL_TITLE . ' ' . $fromName;
180                 NOTIFICATION::mail($tomem->getEmail(), $title, $message, $fromMail, i18n::get_current_charset());
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 = LINK::create_link('member', array('memberid' => $tomem->getID(), 'name' => $tomem->getDisplayName() ) );
193                         }
194                         else
195                         {
196                                 $url = $CONF['IndexURL'] . LINK::create_member_link($tomem->getID());
197                         }
198                         
199                         redirect($url);
200                 }
201                 exit;
202         }
203         
204         /**
205          * ACTION::validateMessage()
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          *  @param      void
210          *  @return     String  Null character string
211          */
212         function validateMessage()
213         {
214                 global $CONF, $member, $manager;
215                 
216                 if ( !$CONF['AllowMemberMail'] )
217                 {
218                         return _ERROR_MEMBERMAILDISABLED;
219                 }
220                 
221                 if ( !$member->isLoggedIn() && !$CONF['NonmemberMail'] )
222                 {
223                         return _ERROR_DISALLOWED;
224                 }
225                 
226                 if ( !$member->isLoggedIn() && !NOTIFICATION::address_validation(postVar('frommail')) )
227                 {
228                         return _ERROR_BADMAILADDRESS;
229                 }
230                 
231                 // let plugins do verification (any plugin which thinks the comment is invalid
232                 // can change 'error' to something other than '')
233                 $result = '';
234                 $manager->notify('ValidateForm', array('type' => 'membermail', 'error' => &$result) );
235                 
236                 return $result;
237         }
238         
239         /**
240          * ACTION::createAccount()
241          * Creates a new user account
242          *  
243          * @param       Void
244          * @return      Mixed
245          */
246         function createAccount()
247         {
248                 global $CONF, $manager;
249                 
250                 if ( !$CONF['AllowMemberCreate'] )
251                 {
252                         doError(_ERROR_MEMBERCREATEDISABLED);
253                 }
254                 
255                 // evaluate content from FormExtra
256                 $result = 1;
257                 $data = array('type' => 'membermail', 'error' => &$result);
258                 $manager->notify('ValidateForm', $data);
259                 
260                 if ( $result != 1 )
261                 {
262                         return $result;
263                 }
264                 
265                 // even though the member can not log in, set some random initial password. One never knows.
266                 srand( (double) microtime() * 1000000);
267                 $initialPwd = md5(uniqid(rand(), TRUE) );
268                 
269                 // create member (non admin/can not login/no notes/random string as password)
270                 $name = ENTITY::shorten(postVar('name'), 32, '');
271                 $r = MEMBER::create($name, postVar('realname'), $initialPwd, postVar('email'), postVar('url'), 0, 0, '');
272                 
273                 if ( $r != 1 )
274                 {
275                         return $r;
276                 }
277                 
278                 // send message containing password.
279                 $newmem = new MEMBER();
280                 $newmem->readFromName($name);
281                 $newmem->sendActivationLink('register');
282                 
283                 $manager->notify('PostRegister', array('member' => &$newmem) );
284                 
285                 if ( postVar('desturl') )
286                 {
287                         redirect(postVar('desturl') );
288                 }
289                 return 1;
290         }
291         
292         /**
293          *  Sends a new password
294          */
295         function forgotPassword()
296         {
297                 $membername = trim(postVar('name') );
298
299                 if ( !MEMBER::exists($membername) )
300                 {
301                         doError(_ERROR_NOSUCHMEMBER);
302                 }
303
304                 $mem = MEMBER::createFromName($membername);
305                 
306                 /* below keeps regular users from resetting passwords using forgot password feature
307                      Removing for now until clear why it is required.*/
308                 /*if (!$mem->canLogin())
309                         doError(_ERROR_NOLOGON_NOACTIVATE);*/
310
311                 // check if e-mail address is correct
312                 if ( !($mem->getEmail() == postVar('email') ) )
313                 {
314                         doError(_ERROR_INCORRECTEMAIL);
315                 }
316
317                 // send activation link
318                 $mem->sendActivationLink('forgot');
319
320                 if ( postVar('url') )
321                 {
322                         redirect(postVar('url') );
323                 }
324                 else
325                 {
326                         echo _MSG_ACTIVATION_SENT;
327                         echo '<br /><br />Return to <a href="'.$CONF['IndexURL'].'" title="'.$CONF['SiteName'].'">'.$CONF['SiteName'].'</a>';
328                 }
329
330                 exit;
331         }
332
333
334         /**
335          * ACTION::doKarma()
336          * Handle karma votes
337          * 
338          * @param       String  $type   pos or neg
339          * @return      Void
340          */
341         function doKarma($type)
342         {
343                 global $itemid, $member, $CONF, $manager;
344                 
345                 // check if itemid exists
346                 if ( !$manager->existsItem($itemid, 0, 0) )
347                 {
348                         doError(_ERROR_NOSUCHITEM);
349                 }
350                 
351                 $blogid = getBlogIDFromItemID($itemid);
352                 $this->checkban($blogid);
353                 
354                 $karma =& $manager->getKarma($itemid);
355                 
356                 // check if not already voted
357                 if ( !$karma->isVoteAllowed(serverVar('REMOTE_ADDR') ) )
358                 {
359                         doError(_ERROR_VOTEDBEFORE);
360                 }
361                 
362                 // check if item does allow voting
363                 $item =& $manager->getItem($itemid, 0, 0);
364                 
365                 if ( $item['closed'] )
366                 {
367                         doError(_ERROR_ITEMCLOSED);
368                 }
369                 
370                 switch ( $type )
371                 {
372                         case 'pos':
373                                 $karma->votePositive();
374                         break;
375                         
376                         case 'neg':
377                                 $karma->voteNegative();
378                         break;
379                 }
380                 
381                 $blog =& $manager->getBlog($blogid);
382                 
383                 // send email to notification address, if any
384                 if ( $blog->getNotifyAddress() && $blog->notifyOnVote() )
385                 {
386                         $message = _NOTIFY_KV_MSG . ' ' . $itemid . "\n";
387                         $itemLink = LINK::create_item_link(intval($itemid) );
388                         $temp = parse_url($itemLink);
389                         
390                         if ( !$temp['scheme'] )
391                         {
392                                 $itemLink = $CONF['IndexURL'] . $itemLink;
393                         }
394                         
395                         $message .= $itemLink . "\n\n";
396                         
397                         if ( $member->isLoggedIn() )
398                         {
399                                 $message .= _NOTIFY_MEMBER . ' ' . $member->getDisplayName() . ' (ID=' . $member->getID() . ")\n";
400                         }
401                         
402                         $message .= _NOTIFY_IP . ' ' . serverVar('REMOTE_ADDR') . "\n";
403                         $message .= _NOTIFY_HOST . ' ' .  gethostbyaddr(serverVar('REMOTE_ADDR'))  . "\n";
404                         $message .= _NOTIFY_VOTE . "\n " . $type . "\n";
405                         $message .= NOTIFICATION::get_mail_footer();
406                         
407                         $subject = _NOTIFY_KV_TITLE . ' ' . strip_tags($item['title']) . ' (' . $itemid . ')';
408                         
409                         $from = $member->getNotifyFromMailAddress();
410                         
411                         NOTIFICATION::mail($blog->getNotifyAddress(), $subject, $message, $from, i18n::get_current_charset());
412                 }
413                 
414                 $refererUrl = serverVar('HTTP_REFERER');
415                 
416                 if ( $refererUrl )
417                 {
418                         $url = $refererUrl;
419                 }
420                 else
421                 {
422                         $url = $itemLink;
423                 }
424                 
425                 redirect($url);
426                 exit;
427         }
428
429
430         /**
431           * Calls a plugin action
432           */
433         function callPlugin()
434         {
435                 global $manager;
436
437                 $pluginName = 'NP_' . requestVar('name');
438                 $actionType = requestVar('type');
439
440                 // 1: check if plugin is installed
441                 if ( !$manager->pluginInstalled($pluginName) )
442                 {
443                         doError(_ERROR_NOSUCHPLUGIN);
444                 }
445
446                 // 2: call plugin
447                 $pluginObject =& $manager->getPlugin($pluginName);
448
449                 if ( $pluginObject )
450                 {
451                         $error = $pluginObject->doAction($actionType);
452                 }
453                 else
454                 {
455                         $error = 'Could not load plugin (see actionlog)';
456                 }
457
458                 // doAction returns error when:
459                 // - an error occurred (duh)
460                 // - no actions are allowed (doAction is not implemented)
461                 if ( $error )
462                 {
463                         doError($error);
464                 }
465
466                 exit;
467
468         }
469
470
471         /**
472          *  Checks if an IP or IP range is banned
473          */
474         function checkban($blogid)
475         {
476                 // check if banned
477                 $ban = BAN::isBanned($blogid, serverVar('REMOTE_ADDR') );
478
479                 if ( $ban != 0 )
480                 {
481                         doError(_ERROR_BANNED1 . $ban->iprange . _ERROR_BANNED2 . $ban->message . _ERROR_BANNED3);
482                 }
483
484         }
485
486
487         /**
488          * Gets a new ticket
489          */
490         function updateTicket()
491         {
492                 global $manager;
493
494                 if ( $manager->checkTicket() )
495                 {
496                         echo $manager->getNewTicket();
497                 }
498                 else
499                 {
500                         echo _ERROR . ':' . _ERROR_BADTICKET;
501                 }
502
503                 return FALSE;
504         }
505
506
507         /**
508          * Handles AutoSaveDraft
509          */
510         function autoDraft()
511         {
512                 global $manager;
513
514                 if ( $manager->checkTicket() )
515                 {
516                         $manager->loadClass('ITEM');
517                         $info = ITEM::createDraftFromRequest();
518
519                         if ( $info['status'] == 'error' )
520                         {
521                                 echo $info['message'];
522                         }
523                         else
524                         {
525                                 echo $info['draftid'];
526                         }
527                 }
528                 else
529                 {
530             echo _ERROR . ':' . _ERROR_BADTICKET;
531                 }
532
533                 return FALSE;
534         }
535
536 }
537