4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
5 * Copyright (C) 2002-2009 The Nucleus Group
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)
14 * Actions that can be called via action.php
16 * @license http://nucleuscms.org/license.txt GNU General Public License
17 * @copyright Copyright (C) 2002-2009 The Nucleus Group
19 * $NucleusJP: ACTION.php,v 1.10 2007/05/31 07:23:39 kimitake Exp $
24 * Constructor for an new ACTION object
32 * Calls functions that handle an action called from action.php
34 function doAction($action)
38 return $this->autoDraft();
41 return $this->updateTicket();
44 return $this->addComment();
47 return $this->sendMessage();
50 return $this->createAccount();
52 case 'forgotpassword':
53 return $this->forgotPassword();
56 return $this->doKarma('pos');
59 return $this->doKarma('neg');
62 return $this->callPlugin();
65 doError(_ERROR_BADACTION);
70 * Adds a new comment to an item (if IP isn't banned)
72 function addComment() {
73 global $CONF, $errormessage, $manager;
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');
81 // set cookies when required
82 $remember = intPostVar('remember');
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);
90 $comments = new COMMENTS($post['itemid']);
92 $blogid = getBlogIDFromItemID($post['itemid']);
93 $this->checkban($blogid);
94 $blog =& $manager->getBlog($blogid);
96 // note: PreAddComment and PostAddComment gets called somewhere inside addComment
97 $errormessage = $comments->addComment($blog->getCorrectTime(),$post);
99 if ($errormessage == '1') {
100 // redirect when adding comments succeeded
101 if (postVar('url')) {
102 redirect(postVar('url'));
104 $url = createItemLink($post['itemid']);
108 // else, show error message using default skin for blog
110 'message' => $errormessage,
111 'skinid' => $blog->getDefaultSkin()
119 * Sends a message from the current member to the member given as argument
121 function sendMessage() {
122 global $CONF, $member;
124 $error = $this->validateMessage();
126 return array('message' => $error);
128 if (!$member->isLoggedIn()) {
129 $fromMail = postVar('frommail');
130 $fromName = _MMAIL_FROMANON;
132 $fromMail = $member->getEmail();
133 $fromName = $member->getDisplayName();
136 $tomem = new MEMBER();
137 $tomem->readFromId(postVar('memberid'));
139 $message = _MMAIL_MSG . ' ' . $fromName . "\n"
140 . '(' . _MMAIL_FROMNUC. ' ' . $CONF['IndexURL'] .") \n\n"
141 . _MMAIL_MAIL . " \n\n"
142 . postVar('message');
143 $message .= getMailFooter();
145 $title = _MMAIL_TITLE . ' ' . $fromName;
147 mb_internal_encoding(_CHARSET);
148 @mb_send_mail($tomem->getEmail(), $title, $message, "From: ". $fromMail);
150 if (postVar('url')) {
151 redirect(postVar('url'));
153 $CONF['MemberURL'] = $CONF['IndexURL'];
154 if ($CONF['URLMode'] == 'pathinfo')
156 $url = createLink('member', array('memberid' => $tomem->getID(), 'name' => $tomem->getDisplayName()));
160 $url = $CONF['IndexURL'] . createMemberLink($tomem->getID());
168 * Checks if a mail to a member is allowed
169 * Returns a string with the error message if the mail is disallowed
171 function validateMessage() {
172 global $CONF, $member, $manager;
174 if (!$CONF['AllowMemberMail'])
175 return _ERROR_MEMBERMAILDISABLED;
177 if (!$member->isLoggedIn() && !$CONF['NonmemberMail'])
178 return _ERROR_DISALLOWED;
180 if (!$member->isLoggedIn() && (!isValidMailAddress(postVar('frommail'))))
181 return _ERROR_BADMAILADDRESS;
183 // let plugins do verification (any plugin which thinks the comment is invalid
184 // can change 'error' to something other than '')
186 $manager->notify('ValidateForm', array('type' => 'membermail', 'error' => &$result));
193 * Creates a new user account
195 function createAccount() {
196 global $CONF, $manager;
198 if (!$CONF['AllowMemberCreate'])
199 doError(_ERROR_MEMBERCREATEDISABLED);
201 // evaluate content from FormExtra
203 $data = array('type' => 'membermail', 'error' => &$result);
204 $manager->notify('ValidateForm', &$data);
211 // even though the member can not log in, set some random initial password. One never knows.
212 srand((double)microtime()*1000000);
213 $initialPwd = md5(uniqid(rand(), true));
215 // create member (non admin/can not login/no notes/random string as password)
216 $name = shorten(postVar('name'),16,'');
217 $r = MEMBER::create($name, postVar('realname'), $initialPwd, postVar('email'), postVar('url'), 0, 0, '');
223 // send message containing password.
224 $newmem = new MEMBER();
225 $newmem->readFromName($name);
226 $newmem->sendActivationLink('register');
228 $manager->notify('PostRegister',array('member' => &$newmem));
230 if (postVar('desturl')) {
231 redirect(postVar('desturl'));
233 // header has been already sent, so deleted the line below
234 //header ("Content-Type: text/html; charset="._CHARSET);
235 echo _MSG_ACTIVATION_SENT;
236 echo "\n</body>\n</html>";
243 * Sends a new password
245 function forgotPassword() {
246 $membername = trim(postVar('name'));
248 if (!MEMBER::exists($membername))
249 doError(_ERROR_NOSUCHMEMBER);
250 $mem = MEMBER::createFromName($membername);
252 /* below keeps regular users from resetting passwords using forgot password feature
253 Removing for now until clear why it is required.*/
254 /*if (!$mem->canLogin())
255 doError(_ERROR_NOLOGON_NOACTIVATE);*/
257 // check if e-mail address is correct
258 if (!($mem->getEmail() == postVar('email')))
259 doError(_ERROR_INCORRECTEMAIL);
261 // send activation link
262 $mem->sendActivationLink('forgot');
264 if (postVar('url')) {
265 redirect(postVar('url'));
267 header ("Content-Type: text/html; charset="._CHARSET);
268 echo _MSG_ACTIVATION_SENT;
276 function doKarma($type) {
277 global $itemid, $member, $CONF, $manager;
279 // check if itemid exists
280 if (!$manager->existsItem($itemid,0,0))
281 doError(_ERROR_NOSUCHITEM);
283 $blogid = getBlogIDFromItemID($itemid);
284 $this->checkban($blogid);
286 $karma =& $manager->getKarma($itemid);
288 // check if not already voted
289 if (!$karma->isVoteAllowed(serverVar('REMOTE_ADDR')))
290 doError(_ERROR_VOTEDBEFORE);
292 // check if item does allow voting
293 $item =& $manager->getItem($itemid,0,0);
295 doError(_ERROR_ITEMCLOSED);
299 $karma->votePositive();
302 $karma->voteNegative();
306 // $blogid = getBlogIDFromItemID($itemid);
307 $blog =& $manager->getBlog($blogid);
309 // send email to notification address, if any
310 if ($blog->getNotifyAddress() && $blog->notifyOnVote()) {
312 $mailto_msg = _NOTIFY_KV_MSG . ' ' . $itemid . "\n";
313 if ($CONF['URLMode'] == 'pathinfo') {
314 $itemLink = createItemLink(intval($itemid));
316 $itemLink = createItemLink(intval($itemid));
317 if (strpos($itemLink, 'http') === false) {
318 $itemLink = $CONF['IndexURL'] . $itemLink;
321 $mailto_msg .= $itemLink . "\n\n";
322 if ($member->isLoggedIn()) {
323 $mailto_msg .= _NOTIFY_MEMBER . ' ' . $member->getDisplayName() . ' (ID=' . $member->getID() . ")\n";
325 $mailto_msg .= _NOTIFY_IP . ' ' . serverVar('REMOTE_ADDR') . "\n";
326 $mailto_msg .= _NOTIFY_HOST . ' ' . gethostbyaddr(serverVar('REMOTE_ADDR')) . "\n";
327 $mailto_msg .= _NOTIFY_VOTE . "\n " . $type . "\n";
328 $mailto_msg .= getMailFooter();
330 $mailto_title = _NOTIFY_KV_TITLE . ' ' . strip_tags($item['title']) . ' (' . $itemid . ')';
332 $frommail = $member->getNotifyFromMailAddress();
334 $notify = new NOTIFICATION($blog->getNotifyAddress());
335 $notify->notify($mailto_title, $mailto_msg , $frommail);
339 $refererUrl = serverVar('HTTP_REFERER');
343 // $url = $CONF['IndexURL'] . 'index.php?itemid=' . $itemid;
352 * Calls a plugin action
354 function callPlugin() {
357 $pluginName = 'NP_' . requestVar('name');
358 $actionType = requestVar('type');
360 // 1: check if plugin is installed
361 if (!$manager->pluginInstalled($pluginName))
362 doError(_ERROR_NOSUCHPLUGIN);
365 $pluginObject =& $manager->getPlugin($pluginName);
367 $error = $pluginObject->doAction($actionType);
369 $error = 'Could not load plugin (see actionlog)';
371 // doAction returns error when:
372 // - an error occurred (duh)
373 // - no actions are allowed (doAction is not implemented)
382 * Checks if an IP or IP range is banned
384 function checkban($blogid) {
386 $ban = BAN::isBanned($blogid, serverVar('REMOTE_ADDR'));
388 doError(_ERROR_BANNED1 . $ban->iprange . _ERROR_BANNED2 . $ban->message . _ERROR_BANNED3);
396 function updateTicket() {
398 if ($manager->checkTicket()) {
399 echo $manager->getNewTicket();
402 echo _ERROR . ':' . _ERROR_BADTICKET;
408 * Handles AutoSaveDraft
410 function autoDraft() {
412 if ($manager->checkTicket()) {
413 $manager->loadClass('ITEM');
414 $info = ITEM::createDraftFromRequest();
415 if ($info['status'] == 'error') {
416 echo $info['message'];
419 echo $info['draftid'];
423 echo _ERROR . ':' . _ERROR_BADTICKET;