OSDN Git Service

code cleanup for my next commit
[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         /**
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          * ACTION::validateMessage()
207          *  Checks if a mail to a member is allowed
208          *  Returns a string with the error message if the mail is disallowed
209          *  
210          *  @param      void
211          *  @return     String  Null character string
212          */
213         function validateMessage()
214         {
215                 global $CONF, $member, $manager;
216                 
217                 if ( !$CONF['AllowMemberMail'] )
218                 {
219                         return _ERROR_MEMBERMAILDISABLED;
220                 }
221                 
222                 if ( !$member->isLoggedIn() && !$CONF['NonmemberMail'] )
223                 {
224                         return _ERROR_DISALLOWED;
225                 }
226                 
227                 if ( !$member->isLoggedIn() && (!isValidMailAddress(postVar('frommail') ) ) )
228                 {
229                         return _ERROR_BADMAILADDRESS;
230                 }
231                 
232                 // let plugins do verification (any plugin which thinks the comment is invalid
233                 // can change 'error' to something other than '')
234                 $result = '';
235                 $manager->notify('ValidateForm', array('type' => 'membermail', 'error' => &$result) );
236                 
237                 return $result;
238         }
239         
240         /**
241          *  Creates a new user account
242          */
243         function createAccount()
244         {
245                 global $CONF, $manager;
246
247                 if ( !$CONF['AllowMemberCreate'] )
248                 {
249                         doError(_ERROR_MEMBERCREATEDISABLED);
250                 }
251                 
252                 // evaluate content from FormExtra
253                 $result = 1;
254                 $data = array('type' => 'membermail', 'error' => &$result);
255                 $manager->notify('ValidateForm', $data);
256                 
257                 if ( $result != 1 )
258                 {
259                         return $result;
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          *  Sends a new password
299          */
300         function forgotPassword()
301         {
302                 $membername = trim(postVar('name') );
303
304                 if ( !MEMBER::exists($membername) )
305                 {
306                         doError(_ERROR_NOSUCHMEMBER);
307                 }
308
309                 $mem = MEMBER::createFromName($membername);
310                 
311                 /* below keeps regular users from resetting passwords using forgot password feature
312                      Removing for now until clear why it is required.*/
313                 /*if (!$mem->canLogin())
314                         doError(_ERROR_NOLOGON_NOACTIVATE);*/
315
316                 // check if e-mail address is correct
317                 if ( !($mem->getEmail() == postVar('email') ) )
318                 {
319                         doError(_ERROR_INCORRECTEMAIL);
320                 }
321
322                 // send activation link
323                 $mem->sendActivationLink('forgot');
324
325                 if ( postVar('url') )
326                 {
327                         redirect(postVar('url') );
328                 }
329                 else
330                 {
331                         echo _MSG_ACTIVATION_SENT;
332                         echo '<br /><br />Return to <a href="'.$CONF['IndexURL'].'" title="'.$CONF['SiteName'].'">'.$CONF['SiteName'].'</a>';
333                 }
334
335                 exit;
336         }
337
338
339         /**
340          * ACTION::doKarma()
341          * 
342          * Handle karma votes
343          * 
344          * @param       String  $type   pos or neg
345          * @return      Void
346          */
347         function doKarma($type)
348         {
349                 global $itemid, $member, $CONF, $manager;
350                 
351                 // check if itemid exists
352                 if ( !$manager->existsItem($itemid, 0, 0) )
353                 {
354                         doError(_ERROR_NOSUCHITEM);
355                 }
356                 
357                 $blogid = getBlogIDFromItemID($itemid);
358                 $this->checkban($blogid);
359                 
360                 $karma =& $manager->getKarma($itemid);
361                 
362                 // check if not already voted
363                 if ( !$karma->isVoteAllowed(serverVar('REMOTE_ADDR') ) )
364                 {
365                         doError(_ERROR_VOTEDBEFORE);
366                 }
367                 
368                 // check if item does allow voting
369                 $item =& $manager->getItem($itemid, 0, 0);
370                 
371                 if ( $item['closed'] )
372                 {
373                         doError(_ERROR_ITEMCLOSED);
374                 }
375                 
376                 switch ( $type )
377                 {
378                         case 'pos':
379                                 $karma->votePositive();
380                         break;
381                         
382                         case 'neg':
383                                 $karma->voteNegative();
384                         break;
385                 }
386                 
387 //              $blogid = getBlogIDFromItemID($itemid);
388                 $blog =& $manager->getBlog($blogid);
389                 
390                 // send email to notification address, if any
391                 if ( $blog->getNotifyAddress() && $blog->notifyOnVote() )
392                 {
393                         
394                         $mailto_msg = _NOTIFY_KV_MSG . ' ' . $itemid . "\n";
395 //                      if ($CONF['URLMode'] == 'pathinfo') {
396 //                              $itemLink = createItemLink(intval($itemid));
397 //                      } else {
398 //                              $itemLink = $CONF['IndexURL'] . createItemLink(intval($itemid));
399 //                      }
400 //                      $mailto_msg .= $CONF['IndexURL'] . 'index.php?itemid=' . $itemid . "\n\n";
401                         $itemLink = createItemLink(intval($itemid) );
402                         $temp = parse_url($itemLink);
403                         
404                         if ( !$temp['scheme'] )
405                         {
406                                 $itemLink = $CONF['IndexURL'] . $itemLink;
407                         }
408                         
409                         $mailto_msg .= $itemLink . "\n\n";
410                         
411                         if ( $member->isLoggedIn() )
412                         {
413                                 $mailto_msg .= _NOTIFY_MEMBER . ' ' . $member->getDisplayName() . ' (ID=' . $member->getID() . ")\n";
414                         }
415                         
416                         $mailto_msg .= _NOTIFY_IP . ' ' . serverVar('REMOTE_ADDR') . "\n";
417                         $mailto_msg .= _NOTIFY_HOST . ' ' .  gethostbyaddr(serverVar('REMOTE_ADDR'))  . "\n";
418                         $mailto_msg .= _NOTIFY_VOTE . "\n " . $type . "\n";
419                         $mailto_msg .= getMailFooter();
420                         
421                         $mailto_title = _NOTIFY_KV_TITLE . ' ' . strip_tags($item['title']) . ' (' . $itemid . ')';
422                         
423                         $frommail = $member->getNotifyFromMailAddress();
424                         
425                         $notify = new NOTIFICATION($blog->getNotifyAddress() );
426                         $notify->notify($mailto_title, $mailto_msg, $frommail);
427                 }
428                 
429                 $refererUrl = serverVar('HTTP_REFERER');
430                 
431                 if ( $refererUrl )
432                 {
433                         $url = $refererUrl;
434                 }
435                 else
436                 {
437 //                      $url = $CONF['IndexURL'] . 'index.php?itemid=' . $itemid;
438                         $url = $itemLink;
439                 }
440                 
441                 redirect($url);
442                 exit;
443         }
444
445
446         /**
447           * Calls a plugin action
448           */
449         function callPlugin()
450         {
451                 global $manager;
452
453                 $pluginName = 'NP_' . requestVar('name');
454                 $actionType = requestVar('type');
455
456                 // 1: check if plugin is installed
457                 if ( !$manager->pluginInstalled($pluginName) )
458                 {
459                         doError(_ERROR_NOSUCHPLUGIN);
460                 }
461
462                 // 2: call plugin
463                 $pluginObject =& $manager->getPlugin($pluginName);
464
465                 if ( $pluginObject )
466                 {
467                         $error = $pluginObject->doAction($actionType);
468                 }
469                 else
470                 {
471                         $error = 'Could not load plugin (see actionlog)';
472                 }
473
474                 // doAction returns error when:
475                 // - an error occurred (duh)
476                 // - no actions are allowed (doAction is not implemented)
477                 if ( $error )
478                 {
479                         doError($error);
480                 }
481
482                 exit;
483
484         }
485
486
487         /**
488          *  Checks if an IP or IP range is banned
489          */
490         function checkban($blogid)
491         {
492                 // check if banned
493                 $ban = BAN::isBanned($blogid, serverVar('REMOTE_ADDR') );
494
495                 if ( $ban != 0 )
496                 {
497                         doError(_ERROR_BANNED1 . $ban->iprange . _ERROR_BANNED2 . $ban->message . _ERROR_BANNED3);
498                 }
499
500         }
501
502
503         /**
504          * Gets a new ticket
505          */
506         function updateTicket()
507         {
508                 global $manager;
509
510                 if ( $manager->checkTicket() )
511                 {
512                         echo $manager->getNewTicket();
513                 }
514                 else
515                 {
516                         echo _ERROR . ':' . _ERROR_BADTICKET;
517                 }
518
519                 return FALSE;
520         }
521
522
523         /**
524          * Handles AutoSaveDraft
525          */
526         function autoDraft()
527         {
528                 global $manager;
529
530                 if ( $manager->checkTicket() )
531                 {
532                         $manager->loadClass('ITEM');
533                         $info = ITEM::createDraftFromRequest();
534
535                         if ( $info['status'] == 'error' )
536                         {
537                                 echo $info['message'];
538                         }
539                         else
540                         {
541                                 echo $info['draftid'];
542                         }
543                 }
544                 else
545                 {
546             echo _ERROR . ':' . _ERROR_BADTICKET;
547                 }
548
549                 return FALSE;
550         }
551
552 }
553