4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2005 The Nucleus Group
\r
7 * This program is free software; you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation; either version 2
\r
10 * of the License, or (at your option) any later version.
\r
11 * (see nucleus/documentation/index.html#license for more info)
\r
13 * A class representing site members
\r
15 * $Id: MEMBER.php,v 1.5 2005-03-16 08:10:35 kimitake Exp $
\r
16 * $NucleusJP: MEMBER.php,v 1.4 2005/03/12 06:19:05 kimitake Exp $
\r
20 // 1 when authenticated, 0 when not
\r
22 var $password; // not the actual password, but rather a MD5 hash
\r
24 var $cookiekey; // value that should also be in the client cookie to allow authentication
\r
32 var $language; // name of the language file to use (e.g. 'english' -> english.php)
\r
33 var $admin; // (either 0 or 1)
\r
34 var $canlogin; // (either 0 or 1)
\r
43 function createFromName($displayname) {
\r
44 $mem =& new MEMBER();
\r
45 $mem->readFromName($displayname);
\r
50 function createFromID($id) {
\r
51 $mem =& new MEMBER();
\r
52 $mem->readFromID($id);
\r
56 function readFromName($displayname) {
\r
57 return $this->read("mname='".addslashes($displayname)."'");
\r
60 function readFromID($id) {
\r
61 return $this->read("mnumber=" . intval($id));
\r
65 * Tries to login as a given user. Returns true when succeeded,
\r
66 * returns false when failed
\r
68 function login($login, $password) {
\r
69 $this->loggedin = 0;
\r
70 if (!$this->readFromName($login))
\r
72 if (!$this->checkPassword($password))
\r
74 $this->loggedin = 1;
\r
75 return $this->isLoggedIn();
\r
78 // login using cookie key
\r
79 function cookielogin($login, $cookiekey) {
\r
80 $this->loggedin = 0;
\r
81 if (!$this->readFromName($login))
\r
83 if (!$this->checkCookieKey($cookiekey))
\r
85 $this->loggedin = 1;
\r
86 return $this->isLoggedIn();
\r
93 function isLoggedIn() {
\r
94 return $this->loggedin;
\r
97 function read($where) {
\r
99 $query = 'SELECT * FROM '.sql_table('member') . ' WHERE ' . $where;
\r
101 $res = sql_query($query);
\r
102 $obj = mysql_fetch_object($res);
\r
104 $this->setRealName($obj->mrealname);
\r
105 $this->setEmail($obj->memail);
\r
106 $this->password = $obj->mpassword;
\r
107 $this->setCookieKey($obj->mcookiekey);
\r
108 $this->setURL($obj->murl);
\r
109 $this->setDisplayName($obj->mname);
\r
110 $this->setAdmin($obj->madmin);
\r
111 $this->id = $obj->mnumber;
\r
112 $this->setCanLogin($obj->mcanlogin);
\r
113 $this->setNotes($obj->mnotes);
\r
114 $this->setLanguage($obj->deflang);
\r
116 return mysql_num_rows($res);
\r
121 * Returns true if member is an admin for the given blog
\r
122 * (returns false if not a team member)
\r
124 function isBlogAdmin($blogid) {
\r
125 $query = 'SELECT tadmin FROM '.sql_table('team').' WHERE'
\r
126 . ' tblog=' . intval($blogid)
\r
127 . ' and tmember='. $this->getID();
\r
128 $res = sql_query($query);
\r
129 if (mysql_num_rows($res) == 0)
\r
132 return (mysql_result($res,0,0) == 1) ;
\r
135 function blogAdminRights($blogid) {
\r
136 return ($this->isAdmin() || $this->isBlogAdmin($blogid));
\r
140 function teamRights($blogid) {
\r
141 return ($this->isAdmin() || $this->isTeamMember($blogid));
\r
145 * Returns true if this member is a team member of the given blog
\r
147 function isTeamMember($blogid) {
\r
148 $query = 'SELECT * FROM '.sql_table('team').' WHERE'
\r
149 . ' tblog=' . intval($blogid)
\r
150 . ' and tmember='. $this->getID();
\r
151 return (mysql_num_rows(sql_query($query)) != 0);
\r
155 * Returns true if this member can edit/delete a commentitem. This can be in the
\r
157 * - member is a super-admin
\r
158 * - member is the author of the comment
\r
159 * - member is admin of the blog associated with the comment
\r
160 * - member is author of the item associated with the comment
\r
162 function canAlterComment($commentid) {
\r
163 if ($this->isAdmin()) return 1;
\r
165 $query = 'SELECT citem as itemid, iblog as blogid, cmember as cauthor, iauthor'
\r
166 . ' FROM '.sql_table('comment') .', '.sql_table('item').', '.sql_table('blog')
\r
167 . ' WHERE citem=inumber and iblog=bnumber and cnumber=' . intval($commentid);
\r
168 $obj = mysql_fetch_object(sql_query($query));
\r
170 return ($obj->cauthor == $this->getID()) or $this->isBlogAdmin($obj->blogid) or ($obj->iauthor == $this->getID());
\r
174 * Returns true if this member can edit/delete an item. This is true in the following
\r
175 * cases: - member is a super-admin
\r
176 * - member is the author of the item
\r
177 * - member is admin of the the associated blog
\r
179 function canAlterItem($itemid) {
\r
180 if ($this->isAdmin()) return 1;
\r
182 $query = 'SELECT iblog, iauthor FROM '.sql_table('item').' WHERE inumber=' . intval($itemid);
\r
183 $obj = mysql_fetch_object(sql_query($query));
\r
184 return ($obj->iauthor == $this->getID()) or $this->isBlogAdmin($obj->iblog);
\r
188 * returns true if this member can move/update an item to a given category,
\r
189 * false if not (see comments fot the tests that are executed)
\r
192 * @param newcat (can also be of form 'newcat-x' with x=blogid)
\r
194 function canUpdateItem($itemid, $newcat) {
\r
197 // item does not exists -> NOK
\r
198 if (!$manager->existsItem($itemid,1,1)) return 0;
\r
200 // cannot alter item -> NOK
\r
201 if (!$this->canAlterItem($itemid)) return 0;
\r
203 // if this is a 'newcat' style newcat
\r
204 // no blog admin of destination blog -> NOK
\r
205 // blog admin of destination blog -> OK
\r
206 if (strstr($newcat,'newcat')) {
\r
208 list($blogid) = sscanf($newcat,'newcat-%d');
\r
209 return $this->blogAdminRights($blogid);
\r
212 // category does not exist -> NOK
\r
213 if (!$manager->existsCategory($newcat)) return 0;
\r
217 $item =& $manager->getItem($itemid,1,1);
\r
219 // old catid = new catid -> OK
\r
220 if ($item['catid'] == $newcat) return 1;
\r
222 // not a valid category -> NOK
\r
223 $validCat = quickQuery('SELECT COUNT(*) AS result FROM '.sql_table('category').' WHERE catid='.intval($newcat));
\r
224 if (!$validCat) return 0;
\r
226 // get destination blog
\r
227 $source_blogid = getBlogIDFromItemID($itemid);
\r
228 $dest_blogid = getBlogIDFromCatID($newcat);
\r
230 // not a team member of destination blog -> NOK
\r
231 if (!$this->teamRights($dest_blogid)) return 0;
\r
233 // if member is author of item -> OK
\r
234 if ($item['authorid'] == $this->getID()) return 1;
\r
236 // if member has admin rights on both blogs: OK
\r
237 if (($this->blogAdminRights($dest_blogid)) && ($this->blogAdminRights($source_blogid))) return 1;
\r
239 // all other cases: NOK
\r
244 function canAddItem($catid) {
\r
247 // if this is a 'newcat' style newcat
\r
248 // no blog admin of destination blog -> NOK
\r
249 // blog admin of destination blog -> OK
\r
250 if (strstr($catid,'newcat')) {
\r
252 list($blogid) = sscanf($catid,"newcat-%d");
\r
253 return $this->blogAdminRights($blogid);
\r
256 // category does not exist -> NOK
\r
257 if (!$manager->existsCategory($catid)) return 0;
\r
259 $blogid = getBlogIDFromCatID($catid);
\r
261 // no team rights for blog -> NOK
\r
262 if (!$this->teamRights($blogid)) return 0;
\r
264 // all other cases: OK
\r
269 * Return true if member can be deleted. This means that there are no items or comments
\r
270 * posted by the member
\r
272 function canBeDeleted() {
\r
273 $res = sql_query('SELECT * FROM '.sql_table('item').' WHERE iauthor=' . $this->getID());
\r
274 $res2 = sql_query('SELECT * FROM '.sql_table('comment').' WHERE cmember=' . $this->getID());
\r
275 return ((mysql_num_rows($res) == 0) and (mysql_num_rows($res2)==0));
\r
279 * Sets the cookies for the member
\r
282 * set this to 1 when using a shared computer. Cookies will expire
\r
283 * at the end of the session in this case.
\r
285 function setCookies($shared = 0) {
\r
288 if ($CONF['SessionCookie'] || $shared)
\r
291 $lifetime = (time()+2592000);
\r
293 setcookie($CONF['CookiePrefix'] .'user',$this->getDisplayName(),$lifetime,$CONF['CookiePath'],$CONF['CookieDomain'],$CONF['CookieSecure']);
\r
294 setcookie($CONF['CookiePrefix'] .'loginkey', $this->getCookieKey(),$lifetime,$CONF['CookiePath'],$CONF['CookieDomain'],$CONF['CookieSecure']);
\r
296 // make sure cookies on shared pcs don't get renewed
\r
298 setcookie($CONF['CookiePrefix'] .'sharedpc', '1',$lifetime,$CONF['CookiePath'],$CONF['CookieDomain'],$CONF['CookieSecure']);
\r
301 function sendActivationLink($type, $extra='')
\r
305 // generate key and URL
\r
306 $key = $this->generateActivationEntry($type, $extra);
\r
307 $url = $CONF['AdminURL'] . 'index.php?action=activate&key=' . $key;
\r
309 // choose text to use in mail
\r
313 $message = _ACTIVATE_REGISTER_MAIL;
\r
314 $title = _ACTIVATE_REGISTER_MAILTITLE;
\r
317 $message = _ACTIVATE_FORGOT_MAIL;
\r
318 $title = _ACTIVATE_FORGOT_MAILTITLE;
\r
320 case 'addresschange':
\r
321 $message = _ACTIVATE_CHANGE_MAIL;
\r
322 $title = _ACTIVATE_CHANGE_MAILTITLE;
\r
327 // fill out variables in text
\r
330 'siteName' => $CONF['SiteName'],
\r
331 'siteUrl' => $CONF['IndexURL'],
\r
332 'memberName' => $this->getDisplayName(),
\r
333 'activationUrl' => $url
\r
336 $message = TEMPLATE::fill($message, $aVars);
\r
337 $title = TEMPLATE::fill($title, $aVars);
\r
342 mb_internal_encoding(_CHARSET);
\r
343 @mb_send_mail($this->getEmail(), $title ,$message,'From: ' . $CONF['AdminEmail']);
\r
345 ACTIONLOG::add(INFO, _ACTIONLOG_ACTIVATIONLINK . ' (' . $this->getDisplayName() . ' / type: ' . $type . ')');
\r
351 * Returns an array of all blogids for which member has admin rights
\r
353 function getAdminBlogs() {
\r
356 if ($this->isAdmin())
\r
357 $query = 'SELECT bnumber as blogid from '.sql_table('blog');
\r
359 $query = 'SELECT tblog as blogid from '.sql_table('team').' where tadmin=1 and tmember=' . $this->getID();
\r
361 $res = sql_query($query);
\r
362 if (mysql_num_rows($res) > 0) {
\r
363 while ($obj = mysql_fetch_object($res)) {
\r
364 array_push($blogs, $obj->blogid);
\r
372 * Returns an email address from which notification of commenting/karma voting can
\r
373 * be sent. A suggestion can be given for when the member is not logged in
\r
375 function getNotifyFromMailAddress($suggest = "") {
\r
377 if ($this->isLoggedIn()) {
\r
378 return $this->getDisplayName() . " <" . $this->getEmail() . ">";
\r
379 } else if (isValidMailAddress($suggest)) {
\r
382 return $CONF['AdminEmail'];
\r
387 * Write data to database
\r
391 $query = 'UPDATE '.sql_table('member')
\r
392 . " SET mname='" . addslashes($this->getDisplayName()) . "',"
\r
393 . " mrealname='". addslashes($this->getRealName()) . "',"
\r
394 . " mpassword='". addslashes($this->getPassword()) . "',"
\r
395 . " mcookiekey='". addslashes($this->getCookieKey()) . "',"
\r
396 . " murl='" . addslashes($this->getURL()) . "',"
\r
397 . " memail='" . addslashes($this->getEmail()) . "',"
\r
398 . " madmin=" . $this->isAdmin() . ","
\r
399 . " mnotes='" . addslashes($this->getNotes()) . "',"
\r
400 . " mcanlogin=" . $this->canLogin() . ","
\r
401 . " deflang='" . addslashes($this->getLanguage()) . "'"
\r
402 . " WHERE mnumber=" . $this->getID();
\r
406 function checkPassword($pw) {
\r
407 return (md5($pw) == $this->getPassword());
\r
410 function checkCookieKey($key) {
\r
411 return (($key != '') && ($key == $this->getCookieKey()));
\r
414 function getRealName() {
\r
415 return $this->realname;
\r
418 function setRealName($name) {
\r
419 $this->realname = $name;
\r
422 function getEmail() {
\r
423 return $this->email;
\r
426 function setEmail($email) {
\r
427 $this->email = $email;
\r
430 function getPassword() {
\r
431 return $this->password;
\r
434 function setPassword($pwd) {
\r
435 $this->password = md5($pwd);
\r
438 function getCookieKey() {
\r
439 return $this->cookiekey;
\r
443 * Generate new cookiekey, save it, and return it
\r
445 function newCookieKey() {
\r
446 mt_srand( (double) microtime() * 1000000);
\r
447 $this->cookiekey = md5(uniqid(mt_rand()));
\r
449 return $this->cookiekey;
\r
452 function setCookieKey($val) {
\r
453 $this->cookiekey = $val;
\r
456 function getURL() {
\r
460 function setURL($site) {
\r
461 $this->url = $site;
\r
464 function getLanguage() {
\r
465 return $this->language;
\r
468 function setLanguage($lang) {
\r
469 $this->language = $lang;
\r
472 function setDisplayName($nick) {
\r
473 $this->displayname = $nick;
\r
476 function getDisplayName() {
\r
477 return $this->displayname;
\r
480 function isAdmin() {
\r
481 return $this->admin;
\r
484 function setAdmin($val) {
\r
485 $this->admin = $val;
\r
488 function canLogin() {
\r
489 return $this->canlogin;
\r
492 function setCanLogin($val) {
\r
493 $this->canlogin = $val;
\r
496 function getNotes() {
\r
497 return $this->notes;
\r
500 function setNotes($val) {
\r
501 $this->notes = $val;
\r
508 // returns true if there is a member with the given login name (static)
\r
509 function exists($name) {
\r
510 $r = sql_query('select * FROM '.sql_table('member')." WHERE mname='".addslashes($name)."'");
\r
511 return (mysql_num_rows($r) != 0);
\r
514 // returns true if there is a member with the given ID (static)
\r
515 function existsID($id) {
\r
516 $r = sql_query('select * FROM '.sql_table('member')." WHERE mnumber='".intval($id)."'");
\r
517 return (mysql_num_rows($r) != 0);
\r
520 // checks if a username is protected. If so, it can not be used on anonymous comments
\r
521 function isNameProtected($name) {
\r
524 $name = strip_tags($name);
\r
525 $name = trim($name);
\r
527 return MEMBER::exists($name);
\r
530 // adds a new member (static)
\r
531 function create($name, $realname, $password, $email, $url, $admin, $canlogin, $notes) {
\r
532 if (!isValidMailAddress($email))
\r
533 return _ERROR_BADMAILADDRESS;
\r
535 if (!isValidDisplayName($name))
\r
536 return _ERROR_BADNAME;
\r
538 if (MEMBER::exists($name))
\r
539 return _ERROR_NICKNAMEINUSE;
\r
542 return _ERROR_REALNAMEMISSING;
\r
545 return _ERROR_PASSWORDMISSING;
\r
547 // Sometimes user didn't prefix the URL with http://, this cause a malformed URL. Let's fix it.
\r
548 if (!eregi("^https?://", $url))
\r
549 $url = "http://".$url;
\r
551 $name = addslashes($name);
\r
552 $realname = addslashes($realname);
\r
553 $password = addslashes(md5($password));
\r
554 $email = addslashes($email);
\r
555 $url = addslashes($url);
\r
556 $admin = intval($admin);
\r
557 $canlogin = intval($canlogin);
\r
558 $notes = addslashes($notes);
\r
560 $query = 'INSERT INTO '.sql_table('member')." (MNAME,MREALNAME,MPASSWORD,MEMAIL,MURL, MADMIN, MCANLOGIN, MNOTES) "
\r
561 . "VALUES ('$name','$realname','$password','$email','$url',$admin, $canlogin, '$notes')";
\r
564 ACTIONLOG::add(INFO, _ACTIONLOG_NEWMEMBER . ' ' . $name);
\r
570 * Returns activation info for a certain key (an object with properties vkey, vmember, ...)
\r
575 function getActivationInfo($key)
\r
577 $query = 'SELECT * FROM ' . sql_table('activation') . ' WHERE vkey=\'' . addslashes($key). '\'';
\r
578 $res = sql_query($query);
\r
580 if (!$res || (mysql_num_rows($res) == 0))
\r
583 return mysql_fetch_object($res);
\r
587 * Creates an account activation key
\r
589 * @param $type one of the following values (determines what to do when activation expires)
\r
590 * 'register' (new member registration)
\r
591 * 'forgot' (forgotton password)
\r
592 * 'addresschange' (member address has changed)
\r
593 * @param $extra extra info (needed when validation link expires)
\r
594 * addresschange -> old email address
\r
597 function generateActivationEntry($type, $extra = '')
\r
599 // clean up old entries
\r
600 $this->cleanupActivationTable();
\r
602 // kill any existing entries for the current member (delete is ok)
\r
603 // (only one outstanding activation key can be present for a member)
\r
604 sql_query('DELETE FROM ' . sql_table('activation') . ' WHERE vmember=' . intval($this->getID()));
\r
606 $canLoginWhileActive = false; // indicates if the member can log in while the link is active
\r
610 $canLoginWhileActive = true;
\r
614 case 'addresschange':
\r
615 $extra = $extra . '/' . ($this->canLogin() ? '1' : '0');
\r
622 // generate a random key
\r
623 srand((double)microtime()*1000000);
\r
624 $key = md5(uniqid(rand(), true));
\r
626 // attempt to add entry in database
\r
627 // add in database as non-active
\r
628 $query = 'INSERT INTO ' . sql_table('activation'). ' (vkey, vtime, vmember, vtype, vextra) ';
\r
629 $query .= 'VALUES (\'' . addslashes($key). '\', \'' . date('Y-m-d H:i:s',time()) . '\', \'' . intval($this->getID()). '\', \'' . addslashes($type). '\', \'' . addslashes($extra). '\')';
\r
630 if (sql_query($query))
\r
634 // mark member as not allowed to log in
\r
635 if (!$canLoginWhileActive)
\r
637 $this->setCanLogin(0);
\r
646 * Inidicates that an activation link has been clicked and any forms displayed
\r
647 * there have been successfully filled out.
\r
650 function activate($key)
\r
652 // get activate info
\r
653 $info = MEMBER::getActivationInfo($key);
\r
659 switch ($info->vtype)
\r
665 // set canlogin value
\r
667 sql_query('UPDATE ' . sql_table('member') . ' SET mcanlogin=' . intval($CONF['NewMemberCanLogon']). ' WHERE mnumber=' . intval($info->vmember));
\r
669 case 'addresschange':
\r
670 // reset old 'canlogin' value
\r
671 list($oldEmail, $oldCanLogin) = explode('/', $info->vextra);
\r
672 sql_query('UPDATE ' . sql_table('member') . ' SET mcanlogin=' . intval($oldCanLogin). ' WHERE mnumber=' . intval($info->vmember));
\r
676 // delete from activation table
\r
677 sql_query('DELETE FROM ' . sql_table('activation') . ' WHERE vkey=\'' . addslashes($key) . '\'');
\r
684 * Cleans up entries in the activation table. All entries older than 2 days are removed.
\r
689 function cleanupActivationTable()
\r
691 $boundary = time() - (60 * 60 * 24 * 2);
\r
693 // 1. walk over all entries, and see if special actions need to be performed
\r
694 $res = sql_query('SELECT * FROM ' . sql_table('activation') . ' WHERE vtime < \'' . date('Y-m-d H:i:s',$boundary) . '\'');
\r
696 while ($o = mysql_fetch_object($res))
\r
701 // delete all information about this site member. registration is undone because there was
\r
702 // no timely activation
\r
703 include_once($DIR_LIBS . 'ADMIN.php');
\r
704 ADMIN::deleteOneMember(intval($o->vmember));
\r
706 case 'addresschange':
\r
707 // revert the e-mail address of the member back to old address
\r
708 list($oldEmail, $oldCanLogin) = explode('/', $o->vextra);
\r
709 sql_query('UPDATE ' . sql_table('member') . ' SET mcanlogin=' . intval($oldCanLogin). ', memail=\'' . addslashes($oldEmail). '\' WHERE mnumber=' . intval($o->vmember));
\r
712 // delete the activation link and ignore. member can request a new password using the
\r
713 // forgot password link
\r
718 // 2. delete activation entries for real
\r
719 sql_query('DELETE FROM ' . sql_table('activation') . ' WHERE vtime < \'' . date('Y-m-d H:i:s',$boundary) . '\'');
\r