4 * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/)
\r
5 * Copyright (C) 2002-2009 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
14 * A class representing site members
\r
16 * @license http://nucleuscms.org/license.txt GNU General Public License
\r
17 * @copyright Copyright (C) 2002-2009 The Nucleus Group
\r
18 * @version $Id: MEMBER.php 1616 2012-01-08 09:48:15Z sakamocchi $
\r
22 // 1 when authenticated, 0 when not
\r
23 public $loggedin = 0;
\r
24 public $password; // not the actual password, but rather a MD5 hash
\r
25 private $algorism = 'md5';
\r
27 public $cookiekey; // value that should also be in the client cookie to allow authentication
\r
28 private $cookie_salt = FALSE;
\r
33 public $displayname;
\r
36 public $admin = 0; // (either 0 or 1)
\r
37 public $canlogin = 0; // (either 0 or 1)
\r
39 public $autosave = 1; // if the member use the autosave draft function
\r
40 public $adminskin = 0; // if the member use the autosave draft function
\r
41 public $bookmarklet = 0; // if the member use the autosave draft function
\r
42 private $locale = '';
\r
45 * Member::__construct()
\r
46 * Constructor for a member object
\r
52 public function __construct()
\r
58 * Member::createFromName()
\r
59 * Create a member object for a given displayname
\r
62 * @param String $displayname login name
\r
63 * @return Object member object
\r
66 public static function &createFromName($displayname)
\r
68 $mem = new Member();
\r
69 $mem->readFromName($displayname);
\r
74 * Member::createFromID()
\r
75 * Create a member object for a given ID
\r
78 * @param Integer $id id for member
\r
80 public static function &createFromID($id)
\r
82 $mem = new Member();
\r
83 $mem->readFromID($id);
\r
88 * Member::readFromName()
\r
89 * Read member table in database
\r
91 * @param String $displayname login name
\r
92 * @return Object SQL resource
\r
95 public function readFromName($displayname)
\r
97 return $this->read('mname='.DB::quoteValue($displayname));
\r
101 * Member::readFromID()
\r
102 * Read member table in database
\r
104 * @param Integer $id id for member
\r
105 * @return Object SQL resource
\r
108 public function readFromID($id)
\r
110 return $this->read("mnumber=" . intval($id));
\r
115 * hash the target string
\r
117 * @param String $string target string
\r
118 * @return Void hashed string
\r
120 public function hash($string)
\r
122 switch ( $this->algorism )
\r
126 $string = md5($string);
\r
132 * Member::set_cookie_salt()
\r
134 * @param integer $key secureCookieKey value
\r
138 private function set_cookie_salt($key = 0)
\r
148 $this->cookie_salt = preg_replace('/\.[0-9]+\.[0-9]+\.[0-9]+$/', '', serverVar('REMOTE_ADDR'));
\r
151 $this->cookie_salt = preg_replace('/\.[0-9]+\.[0-9]+$/', '', serverVar('REMOTE_ADDR'));
\r
154 $this->cookie_salt = preg_replace('/\.[0-9]+$/', '', serverVar('REMOTE_ADDR'));
\r
157 $this->cookie_salt = serverVar('REMOTE_ADDR');
\r
160 $this->cookie_salt = 'none';
\r
167 * Tries to login as a given user.
\r
168 * Returns true when succeeded, returns false when failed
\r
169 * 3.40 adds CustomLogin event
\r
171 * @param String $login login name for member
\r
172 * @param String $password password for member
\r
173 * @param Integer $shared whether the user agent is shared or not
\r
176 public function login($login, $password, $shared=1)
\r
178 global $CONF, $errormessage, $manager;
\r
180 /* TODO: validation for $login, $password, $shared */
\r
181 if ( $login == '' || $password == '' )
\r
185 /* limiting the length of password to avoid hash collision */
\r
186 $password=i18n::substr($password, 0, 40);
\r
189 * generate cookie salt from secure cookie key settings
\r
190 * (either 'none', 0, 8, 16, 24, or 32)
\r
192 if ( !$this->cookie_salt )
\r
195 if ( array_key_exists('secureCookieKey', $CONF) )
\r
197 $salt = $CONF['secureCookieKey'];
\r
199 $this->set_cookie_salt($salt);
\r
204 $manager->notify('CustomLogin', array('login' => &$login, 'password'=>&$password, 'success'=>&$success, 'allowlocal'=>&$allowlocal));
\r
206 $this->loggedin = 0;
\r
209 $this->loggedin = ( $this->readFromName($login) );
\r
211 elseif ( $allowlocal )
\r
213 $this->loggedin = ( $this->readFromName($login) && $this->checkPassword($password) );
\r
217 if ( !$this->loggedin )
\r
219 $trimlogin = trim($login);
\r
220 if ( empty($trimlogin) )
\r
222 $errormessage = "Please enter a username.";
\r
226 $errormessage = 'Login failed for ' . $login;
\r
228 $manager->notify('LoginFailed', array('username' => $login) );
\r
229 ActionLog::add(INFO, $errormessage);
\r
231 /* login success */
\r
234 /* For lower compatibility */
\r
235 if ( strlen($this->password) === 32 )
\r
237 $this->password = $this->hash($password);
\r
240 $this->newCookieKey();
\r
241 $this->setCookies($shared);
\r
243 if ( $this->cookie_salt !== 'none' )
\r
245 /* secure cookie key */
\r
246 $this->setCookieKey($this->hash($this->getCookieKey() . $this->cookie_salt));
\r
250 $errormessage = '';
\r
251 $manager->notify('LoginSuccess', array('member' => &$member, 'username' => $login) );
\r
252 ActionLog::add(INFO, "Login successful for $login (sharedpc=$shared)");
\r
255 return $this->loggedin;
\r
259 * Member::cookielogin()
\r
260 * Login using cookie key
\r
262 * @param String $login not used
\r
263 * @param String $cookiekey not used
\r
264 * @return Boolean login or not
\r
266 public function cookielogin($login='', $cookiekey='')
\r
268 global $CONF, $manager;
\r
270 if ( !headers_sent() && cookieVar("{$CONF['CookiePrefix']}user") )
\r
272 /* Cookie Authentication */
\r
273 $ck = cookieVar("{$CONF['CookiePrefix']}loginkey");
\r
275 /* TODO: validation for each cookie values */
\r
277 /* limiting the length of password to avoid hash collision */
\r
278 $ck = i18n::substr($ck,0,32);
\r
281 * generate cookie salt from secure cookie key settings
\r
282 * (either 'none', 0, 8, 16, 24, or 32)
\r
284 if ( !$this->cookie_salt )
\r
287 if ( array_key_exists('secureCookieKey', $CONF) )
\r
289 $salt = $CONF['secureCookieKey'];
\r
291 $this->set_cookie_salt($salt);
\r
294 if ( $this->cookie_salt !== 'none' )
\r
296 $ck = $this->hash($ck . $this->cookie_salt);
\r
298 $this->loggedin = ( $this->readFromName(cookieVar("{$CONF['CookiePrefix']}user")) && $this->checkCookieKey($ck) );
\r
301 /* renew cookies when not on a shared computer */
\r
302 if ( $this->loggedin && (cookieVar($CONF['CookiePrefix'] . 'sharedpc') != 1) )
\r
304 $this->setCookieKey(cookieVar("{$CONF['CookiePrefix']}loginkey"));
\r
305 $this->setCookies();
\r
308 return $this->loggedin;
\r
313 * logout and expire cookie
\r
318 public function logout()
\r
320 global $CONF, $manager;
\r
322 if ( !headers_sent() && cookieVar("{$CONF['CookiePrefix']}user") )
\r
324 /* remove cookies on logout */
\r
325 setcookie("{$CONF['CookiePrefix']}user", '', (time() - 2592000), $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
\r
326 setcookie("{$CONF['CookiePrefix']}loginkey", '', (time() - 2592000), $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
\r
327 $manager->notify('Logout', array('username' => cookieVar("{$CONF['CookiePrefix']}user") ) );
\r
330 $this->loggedin = 0;
\r
335 * Member::isLoggedIn()
\r
336 * return member is loggedin or not
\r
341 public function isLoggedIn()
\r
343 return $this->loggedin;
\r
348 * Read member information from the database
\r
350 * @param String $where where statement
\r
351 * @return Resource SQL resource
\r
354 public function read($where)
\r
357 $query = 'SELECT * FROM '.sql_table('member') . ' WHERE ' . $where;
\r
359 $row = DB::getRow($query);
\r
361 $this->setRealName($row['mrealname']);
\r
362 $this->setEmail($row['memail']);
\r
363 $this->password = $row['mpassword'];
\r
364 $this->setCookieKey($row['mcookiekey']);
\r
365 $this->setURL($row['murl']);
\r
366 $this->setDisplayName($row['mname']);
\r
367 $this->setAdmin($row['madmin']);
\r
368 $this->id = $row['mnumber'];
\r
369 $this->setCanLogin($row['mcanlogin']);
\r
370 $this->setNotes($row['mnotes']);
\r
371 $this->setLocale($row['mlocale']);
\r
372 $this->setAutosave($row['mautosave']);
\r
373 $this->setAdminSkin($row['madminskin']);
\r
374 $this->setBookmarklet($row['mbkmklt']);
\r
376 return $row ? TRUE : FALSE;
\r
380 * Member::isBlogAdmin()
\r
381 * Returns true if member is an admin for the given blog
\r
382 * (returns false if not a team member)
\r
384 * @param Integer $blogid weblog id
\r
385 * @return Integer weblog admin or not
\r
388 public function isBlogAdmin($blogid)
\r
390 $query = 'SELECT tadmin FROM '.sql_table('team').' WHERE'
\r
391 . ' tblog=' . intval($blogid)
\r
392 . ' and tmember='. $this->getID();
\r
393 $res = DB::getValue($query);
\r
395 return ($res == 1);
\r
401 * Member::blogAdminRights()
\r
403 * @param integer $blogid ID of target weblog
\r
404 * @return boolean whether to have admin rights to the weblog or not
\r
407 public function blogAdminRights($blogid)
\r
409 return ($this->isAdmin() || $this->isBlogAdmin($blogid));
\r
413 * Member::teamRights()
\r
415 * @param integer $blogid ID of target weblog
\r
416 * @return boolean whether to have admin right to the weblog or not
\r
419 public function teamRights($blogid)
\r
421 return ($this->isAdmin() || $this->isTeamMember($blogid));
\r
425 * Member::isTeamMember()
\r
426 * Returns true if this member is a team member of the given blog
\r
428 * @param integer $blogid ID of target weblog
\r
429 * @return boolean whether to join the weblog or not
\r
432 public function isTeamMember($blogid)
\r
434 $query = 'SELECT * FROM '.sql_table('team').' WHERE'
\r
435 . ' tblog=' . intval($blogid)
\r
436 . ' and tmember='. $this->getID();
\r
437 $res = DB::getResult($query);
\r
438 return ($res->rowCount() != 0);
\r
442 * Member::canAddItem()
\r
444 * @param integer $catid ID of target category
\r
445 * @return boolean whether to be able to add items to the category or not
\r
448 public function canAddItem($catid)
\r
452 // if this is a 'newcat' style newcat
\r
453 // no blog admin of destination blog -> NOK
\r
454 // blog admin of destination blog -> OK
\r
455 if ( i18n::strpos($catid,'newcat') === 0 )
\r
458 list($blogid) = sscanf($catid,"newcat-%d");
\r
459 return $this->blogAdminRights($blogid);
\r
462 // category does not exist -> NOK
\r
463 if ( !$manager->existsCategory($catid) )
\r
468 $blogid = getBlogIDFromCatID($catid);
\r
470 // no team rights for blog -> NOK
\r
471 if (!$this->teamRights($blogid))
\r
476 // all other cases: OK
\r
481 * Member::canAlterComment()
\r
482 * Returns true if this member can edit/delete a commentitem. This can be in the
\r
484 * - member is a super-admin
\r
485 * - member is the author of the comment
\r
486 * - member is admin of the blog associated with the comment
\r
487 * - member is author of the item associated with the comment
\r
489 * @param integer $commentid ID of target comment
\r
490 * @return boolean delete/edit the comment or not
\r
493 public function canAlterComment($commentid)
\r
495 if ( $this->isAdmin() )
\r
500 $query = 'SELECT citem as itemid, iblog as blogid, cmember as cauthor, iauthor'
\r
501 . ' FROM '.sql_table('comment') .', '.sql_table('item').', '.sql_table('blog')
\r
502 . ' WHERE citem=inumber and iblog=bnumber and cnumber=' . intval($commentid);
\r
503 $res = DB::getRow($query);
\r
505 return ($res['cauthor'] == $this->getID()) or $this->isBlogAdmin($res['blogid']) or ($res['iauthor'] == $this->getID());
\r
509 * Member::canAlterItem()
\r
510 * Returns true if this member can edit/delete an item. This is true in the following
\r
511 * cases: - member is a super-admin
\r
512 * - member is the author of the item
\r
513 * - member is admin of the the associated blog
\r
515 * @param integer $itemid ID of target item
\r
516 * @return boolean delete/edit the item or not
\r
519 public function canAlterItem($itemid)
\r
521 if ($this->isAdmin()) return 1;
\r
523 $query = 'SELECT iblog, iauthor FROM '.sql_table('item').' WHERE inumber=' . intval($itemid);
\r
524 $res = DB::getRow($query);
\r
525 return ($res['iauthor'] == $this->getID()) or $this->isBlogAdmin($res['iblog']);
\r
529 * Member::canBeDeleted()
\r
530 * Return true if member can be deleted. This means that there are no items posted by the member left
\r
533 * @return boolean whether there is no items or exists
\r
536 public function canBeDeleted()
\r
538 $res = DB::getResult('SELECT * FROM '.sql_table('item').' WHERE iauthor=' . $this->getID());
\r
539 return ( $res->rowCount() == 0 );
\r
543 * Member::canUpdateItem()
\r
544 * returns true if this member can move/update an item to a given category,
\r
545 * false if not (see comments fot the tests that are executed)
\r
547 * @param integer $itemid
\r
548 * @param string $newcat (can also be of form 'newcat-x' with x=blogid)
\r
549 * @return boolean whether being able to update the item or not
\r
552 public function canUpdateItem($itemid, $newcat)
\r
556 // item does not exists -> NOK
\r
557 if ( !$manager->existsItem($itemid, 1, 1) )
\r
562 // cannot alter item -> NOK
\r
563 if (!$this->canAlterItem($itemid))
\r
568 // if this is a 'newcat' style newcat
\r
569 // no blog admin of destination blog -> NOK
\r
570 // blog admin of destination blog -> OK
\r
571 if ( i18n::strpos($newcat, 'newcat') === 0 )
\r
574 list($blogid) = sscanf($newcat, 'newcat-%d');
\r
575 return $this->blogAdminRights($blogid);
\r
578 // category does not exist -> NOK
\r
579 if (!$manager->existsCategory($newcat))
\r
585 $item =& $manager->getItem($itemid, 1, 1);
\r
587 // old catid = new catid -> OK
\r
588 if ($item['catid'] == $newcat)
\r
593 // not a valid category -> NOK
\r
594 $validCat = DB::getValue('SELECT COUNT(*) AS result FROM '.sql_table('category').' WHERE catid='.intval($newcat));
\r
600 // get destination blog
\r
601 $item =& $manager->getItem($itemid, 1, 1);
\r
602 $source_blogid = $item['blogid'];
\r
603 $dest_blogid = getBlogIDFromCatID($newcat);
\r
605 // not a team member of destination blog -> NOK
\r
606 if ( !$this->teamRights($dest_blogid) )
\r
611 // if member is author of item -> OK
\r
612 if ( $item['authorid'] == $this->getID() )
\r
617 // if member has admin rights on both blogs: OK
\r
618 if ( ($this->blogAdminRights($dest_blogid)) && ($this->blogAdminRights($source_blogid)) )
\r
623 // all other cases: NOK
\r
628 * Member::setCookies()
\r
629 * Sets the cookies for the member
\r
631 * @param boolean $shared set this to 1 when using a shared computer. Cookies will expire
\r
632 * at the end of the session in this case.
\r
636 public function setCookies($shared = 0)
\r
640 if ( $CONF['SessionCookie'] || $shared )
\r
646 $lifetime = time()+2592000;
\r
649 setcookie($CONF['CookiePrefix'] . 'user', $this->getDisplayName(), $lifetime, $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
\r
650 setcookie($CONF['CookiePrefix'] . 'loginkey', $this->getCookieKey(), $lifetime, $CONF['CookiePath'], $CONF['CookieDomain'], $CONF['CookieSecure']);
\r
652 // make sure cookies on shared pcs don't get renewed
\r
655 setcookie($CONF['CookiePrefix'] .'sharedpc', '1',$lifetime,$CONF['CookiePath'],$CONF['CookieDomain'],$CONF['CookieSecure']);
\r
661 * Member::sendActivationLink()
\r
662 * Send activation mail
\r
664 * @param string $type activation type
\r
665 * @param string $extra extra info
\r
668 public function sendActivationLink($type, $extra='')
\r
672 if ( !isset($CONF['ActivationDays']) )
\r
674 $CONF['ActivationDays'] = 2;
\r
677 // generate key and URL
\r
678 $key = $this->generateActivationEntry($type, $extra);
\r
679 $url = $CONF['AdminURL'] . 'index.php?action=activate&key=' . $key;
\r
681 // choose text to use in mail
\r
685 $message = _ACTIVATE_REGISTER_MAIL;
\r
686 $subject = _ACTIVATE_REGISTER_MAILTITLE;
\r
689 $message = _ACTIVATE_FORGOT_MAIL;
\r
690 $subject = _ACTIVATE_FORGOT_MAILTITLE;
\r
692 case 'addresschange':
\r
693 $message = _ACTIVATE_CHANGE_MAIL;
\r
694 $subject = _ACTIVATE_CHANGE_MAILTITLE;
\r
699 // fill out variables in text
\r
701 'siteName' => $CONF['SiteName'],
\r
702 'siteUrl' => $CONF['IndexURL'],
\r
703 'memberName' => $this->getDisplayName(),
\r
704 'activationUrl' => $url,
\r
705 'activationDays' => $CONF['ActivationDays']
\r
708 $message = Template::fill($message, $aVars);
\r
709 $subject = Template::fill($subject, $aVars);
\r
712 NOTIFICATION::mail($this->getEmail(), $subject ,$message, $CONF['AdminEmail'], i18n::get_current_charset());
\r
714 ActionLog::add(INFO, _ACTIONLOG_ACTIVATIONLINK . ' (' . $this->getDisplayName() . ' / type: ' . $type . ')');
\r
719 * Member::getAdminBlogs()
\r
720 * Returns an array of all blogids for which member has admin rights
\r
723 * @return array weblog IDs in which this member has admin rights
\r
726 public function getAdminBlogs()
\r
730 if ($this->isAdmin())
\r
732 $query = 'SELECT bnumber as blogid from '.sql_table('blog');
\r
736 $query = 'SELECT tblog as blogid from '.sql_table('team').' where tadmin=1 and tmember=' . $this->getID();
\r
739 $res = DB::getResult($query);
\r
740 if ( $res->rowCount() > 0 )
\r
742 foreach ( $res as $row )
\r
744 array_push($blogs, $row['blogid']);
\r
751 * Member::getTeamBlogs()
\r
752 * Returns an array of all blogids for which member has team rights
\r
754 * @param boolean $incAdmin whether checking weblog admin rights or not
\r
755 * @return array weblog IDs in which this member join
\r
758 public function getTeamBlogs($incAdmin = 1)
\r
760 $incAdmin = intval($incAdmin);
\r
763 if ( $this->isAdmin() && $incAdmin )
\r
765 $query = 'SELECT bnumber as blogid from '.sql_table('blog');
\r
769 $query = 'SELECT tblog as blogid from '.sql_table('team').' where tmember=' . $this->getID();
\r
772 $res = DB::getResult($query);
\r
773 if ( $res->rowCount() > 0 )
\r
775 foreach ( $res as $row )
\r
777 array_push($blogs, $row['blogid']);
\r
784 * Member::getNotifyFromMailAddress()
\r
786 * Returns an email address from which notification of commenting/karma voting can
\r
787 * be sent. A suggestion can be given for when the member is not logged in
\r
789 * @param String $suggest
\r
790 * @return String mail address or suggestion
\r
792 public function getNotifyFromMailAddress($suggest = "")
\r
795 if ( $this->isLoggedIn() )
\r
797 return $this->getDisplayName() . " <" . $this->getEmail() . ">";
\r
799 else if ( NOTIFICATION::address_validation($suggest) )
\r
803 return $CONF['AdminEmail'];
\r
808 * Write data to database
\r
814 public function write()
\r
816 $query = 'UPDATE '.sql_table('member')
\r
817 . ' SET mname=' . DB::quoteValue($this->displayname) . ', '
\r
818 . 'mrealname='. DB::quoteValue($this->realname) . ', '
\r
819 . 'mpassword='. DB::quoteValue($this->password) . ', '
\r
820 . 'mcookiekey='. DB::quoteValue($this->cookiekey) . ', '
\r
821 . 'murl=' . DB::quoteValue($this->url) . ', '
\r
822 . 'memail=' . DB::quoteValue($this->email) . ', '
\r
823 . 'madmin=' . intval($this->admin) . ', '
\r
824 . 'mnotes=' . DB::quoteValue($this->notes) . ', '
\r
825 . 'mcanlogin=' . intval($this->canlogin) . ', '
\r
826 . 'mlocale=' . DB::quoteValue($this->locale) . ', '
\r
827 . 'madminskin=' . DB::quoteValue($this->adminskin) . ', '
\r
828 . 'mbkmklt=' . DB::quoteValue($this->bookmarklet) . ', '
\r
829 . 'mautosave=' . intval($this->autosave) . ' '
\r
830 . 'WHERE mnumber=' . intval($this->id);
\r
831 DB::execute($query);
\r
835 public function checkCookieKey($key)
\r
837 return ( ($key != '') && ( $key == $this->getCookieKey() ) );
\r
840 public function checkPassword($pw)
\r
842 /* for lower compatibility (md5) */
\r
843 if ( strlen($this->password) === 32 )
\r
845 return (md5($pw) == $this->password);
\r
847 return ($this->hash($pw) == $this->password);
\r
850 public function getRealName()
\r
852 return $this->realname;
\r
855 public function setRealName($name)
\r
857 $this->realname = $name;
\r
860 public function getEmail()
\r
862 return $this->email;
\r
865 public function setEmail($email)
\r
867 $this->email = $email;
\r
870 public function getPassword()
\r
872 return $this->password;
\r
875 public function setPassword($pwd)
\r
877 $this->password = $this->hash($pwd);
\r
880 public function getCookieKey()
\r
882 return $this->cookiekey;
\r
886 * Member::newCookieKey()
\r
887 * Generate new cookiekey, save it, and return it
\r
893 public function newCookieKey()
\r
895 mt_srand( (double) microtime() * 1000000);
\r
896 $this->cookiekey = $this->hash(uniqid(mt_rand()));
\r
898 return $this->cookiekey;
\r
901 public function setCookieKey($val)
\r
903 $this->cookiekey = $val;
\r
906 public function getURL()
\r
911 public function setURL($site)
\r
913 $this->url = $site;
\r
916 public function setAdminSkin($skin)
\r
918 $this->adminskin = $skin;
\r
921 public function setBookmarklet($skin)
\r
923 $this->bookmarklet = $skin;
\r
926 public function getAdminSkin()
\r
928 return $this->adminskin;
\r
931 public function getBookmarklet()
\r
933 return $this->bookmarklet;
\r
936 public function getLocale()
\r
938 return $this->locale;
\r
941 public function setLocale($locale)
\r
943 if ( !preg_match('#^(.+)_(.+)_(.+)$#', $locale)
\r
944 && ($locale = i18n::convert_old_language_file_name_to_locale($locale)) === FALSE )
\r
948 $this->locale = $locale;
\r
952 public function setDisplayName($nick)
\r
954 $this->displayname = $nick;
\r
957 public function getDisplayName()
\r
959 return $this->displayname;
\r
962 public function isAdmin()
\r
964 return $this->admin;
\r
967 public function setAdmin($val)
\r
969 $this->admin = $val;
\r
972 public function canLogin()
\r
974 return $this->canlogin;
\r
977 public function setCanLogin($val)
\r
979 $this->canlogin = $val;
\r
982 public function getNotes()
\r
984 return $this->notes;
\r
987 public function setNotes($val)
\r
989 $this->notes = $val;
\r
992 public function getAutosave()
\r
994 return $this->autosave;
\r
997 public function setAutosave($val)
\r
999 $this->autosave = $val;
\r
1007 * @return integer id of this member object
\r
1010 public function getID()
\r
1016 * Member::exists()
\r
1017 * Returns true if there is a member with the given login name
\r
1020 * @param string $name target name
\r
1021 * @return boolean whether target name exists or not
\r
1023 public static function exists($name)
\r
1025 $r = DB::getResult('SELECT * FROM ' . sql_table('member') . ' WHERE mname=' . DB::quoteValue($name));
\r
1026 return ( $r->rowCount() != 0 );
\r
1030 * Member::existsID()
\r
1031 * Returns true if there is a member with the given ID
\r
1034 * @param integer $id target id
\r
1035 * @return boolean whether target id exists or not
\r
1038 public static function existsID($id)
\r
1040 $r = DB::getResult('SELECT * FROM ' . sql_table('member') . ' WHERE mnumber=' . intval($id));
\r
1041 return ( $r->rowCount() != 0 );
\r
1045 * Member::isNameProtected()
\r
1046 * Checks if a username is protected.
\r
1047 * If so, it can not be used on anonymous comments
\r
1049 * @param string $name target name
\r
1050 * @return boolean whether the name exists or not
\r
1053 public function isNameProtected($name)
\r
1056 $name = strip_tags($name);
\r
1057 $name = trim($name);
\r
1058 return self::exists($name);
\r
1062 * Member::create()
\r
1063 * Adds a new member
\r
1066 * @param String $name
\r
1067 * @param String $realname
\r
1068 * @param String $password
\r
1069 * @param String $email
\r
1070 * @param String $url
\r
1071 * @param String $admin
\r
1072 * @param String $canlogin
\r
1073 * @param String $notes
\r
1074 * @return String 1 if success, others if fail
\r
1076 static public function create($name, $realname, $password, $email, $url, $admin, $canlogin, $notes)
\r
1078 if ( !NOTIFICATION::address_validation($email) )
\r
1080 return _ERROR_BADMAILADDRESS;
\r
1083 /* TODO: this method should be in MEMBER class, not globalfunctions */
\r
1084 if ( !isValidDisplayName($name) )
\r
1086 return _ERROR_BADNAME;
\r
1089 if ( self::exists($name) )
\r
1091 return _ERROR_NICKNAMEINUSE;
\r
1096 return _ERROR_REALNAMEMISSING;
\r
1099 /* TODO: check the number of characters */
\r
1102 return _ERROR_PASSWORDMISSING;
\r
1106 * begin if: sometimes user didn't prefix the URL with http:// or https://,
\r
1107 * this cause a malformed URL. Let's fix it.
\r
1110 if ( !preg_match('#^https?://#', $url) )
\r
1112 $url = 'http://' . $url;
\r
1115 $name = DB::quoteValue($name);
\r
1116 $realname = DB::quoteValue($realname);
\r
1117 /* NOTE: hashed password is automatically updated if the length is 32 bytes when logging in */
\r
1118 $password = DB::quoteValue(md5($password));
\r
1119 $email = DB::quoteValue($email);
\r
1120 $url = DB::quoteValue($url);
\r
1121 $admin = (integer) $admin;
\r
1122 $canlogin = (integer) $canlogin;
\r
1123 $notes = DB::quoteValue($notes);
\r
1125 $query = "INSERT INTO %s"
\r
1126 . " (MNAME,MREALNAME,MPASSWORD,MEMAIL,MURL, MADMIN, MCANLOGIN, MNOTES)"
\r
1127 . " VALUES (%s, %s, %s, %s, %s, %d, %d, %s)";
\r
1128 $query = sprintf($query, sql_table('member'), $name, $realname, $password, $email, $url, $admin, $canlogin, $notes);
\r
1129 DB::execute($query);
\r
1131 ActionLog::add(INFO, _ACTIONLOG_NEWMEMBER . ' ' . $name);
\r
1137 * Member::getActivationInfo()
\r
1138 * Returns activation info for a certain key (an object with properties vkey, vmember, ...)
\r
1141 * @param string $key activation key
\r
1142 * @return mixed return 0 if failed, else return activation table object
\r
1145 public static function getActivationInfo($key)
\r
1147 $query = 'SELECT * FROM ' . sql_table('activation') . ' WHERE vkey=' . DB::quoteValue($key);
\r
1148 $res = DB::getResult($query);
\r
1150 if ( !$res || ($res->rowCount() == 0) )
\r
1154 return $res->fetch();
\r
1158 * Member::generateActivationEntry()
\r
1159 * Creates an account activation key
\r
1160 * addresschange -> old email address
\r
1162 * @param string $type one of the following values (determines what to do when activation expires)
\r
1163 * 'register' (new member registration)
\r
1164 * 'forgot' (forgotton password)
\r
1165 * 'addresschange' (member address has changed)
\r
1166 * @param string $extra extra info (needed when validation link expires)
\r
1167 * @return string activation key
\r
1169 public function generateActivationEntry($type, $extra = '')
\r
1171 // clean up old entries
\r
1172 $this->cleanupActivationTable();
\r
1174 // kill any existing entries for the current member (delete is ok)
\r
1175 // (only one outstanding activation key can be present for a member)
\r
1176 DB::execute('DELETE FROM ' . sql_table('activation') . ' WHERE vmember=' . intval($this->getID()));
\r
1178 // indicates if the member can log in while the link is active
\r
1179 $canLoginWhileActive = false;
\r
1183 $canLoginWhileActive = true;
\r
1187 case 'addresschange':
\r
1188 $extra = $extra . '/' . ( $this->canLogin() ? '1' : '0' );
\r
1195 // generate a random key
\r
1196 srand((double)microtime()*1000000);
\r
1197 $key = $this->hash(uniqid(rand(), true));
\r
1199 // attempt to add entry in database
\r
1200 // add in database as non-active
\r
1201 $query = 'INSERT INTO %s (vkey, vtime, vmember, vtype, vextra) VALUES (%s, %s, %d, %s, %s)';
\r
1202 $query = sprintf($query
\r
1203 , sql_table('activation')
\r
1204 , DB::quoteValue($key)
\r
1205 , DB::formatDateTime()
\r
1206 , intval($this->getID())
\r
1207 , DB::quoteValue($type)
\r
1208 , DB::quoteValue($extra)
\r
1210 if ( DB::execute($query) !== FALSE )
\r
1214 // mark member as not allowed to log in
\r
1215 if ( !$canLoginWhileActive )
\r
1217 $this->setCanLogin(0);
\r
1226 * Member::activate()
\r
1227 * Inidicates that an activation link has been clicked and any forms displayed
\r
1228 * there have been successfully filled out.
\r
1230 * @param string $key activation key
\r
1234 public function activate($key)
\r
1236 // get activate info
\r
1237 $info = self::getActivationInfo($key);
\r
1245 switch ( $info['vtype'] )
\r
1251 // set canlogin value
\r
1253 DB::execute('UPDATE ' . sql_table('member') . ' SET mcanlogin=' . intval($CONF['NewMemberCanLogon']). ' WHERE mnumber=' . intval($info['vmember']));
\r
1255 case 'addresschange':
\r
1256 // reset old 'canlogin' value
\r
1257 list($oldEmail, $oldCanLogin) = preg_split('#/#', $info['vextra']);
\r
1258 DB::execute('UPDATE ' . sql_table('member') . ' SET mcanlogin=' . intval($oldCanLogin). ' WHERE mnumber=' . intval($info['vmember']));
\r
1262 // delete from activation table
\r
1263 DB::execute('DELETE FROM ' . sql_table('activation') . ' WHERE vkey=' . DB::quoteValue($key));
\r
1270 * Member::cleanupActivationTable()
\r
1271 * Cleans up entries in the activation table. All entries older than 2 days are removed.
\r
1277 public function cleanupActivationTable()
\r
1280 if ( isset($CONF['ActivationDays']) && intval($CONF['ActivationDays']) > 0 )
\r
1282 $actdays = intval($CONF['ActivationDays']);
\r
1286 $CONF['ActivationDays'] = 2;
\r
1288 $boundary = time() - (60 * 60 * 24 * $actdays);
\r
1290 // 1. walk over all entries, and see if special actions need to be performed
\r
1291 $query = sprintf('SELECT * FROM %s WHERE vtime < %s', sql_table('activation'), DB::formatDateTime($boundary));
\r
1292 $res = DB::getResult($query);
\r
1294 foreach ( $res as $row )
\r
1296 switch ( $row['vtype'] )
\r
1299 // delete all information about this site member. registration is undone because there was
\r
1300 // no timely activation
\r
1301 include_once($DIR_LIBS . 'ADMIN.php');
\r
1302 Admin::deleteOneMember(intval($row['vmember']));
\r
1304 case 'addresschange':
\r
1305 // revert the e-mail address of the member back to old address
\r
1306 list($oldEmail, $oldCanLogin) = preg_split('#/#', $row['vextra']);
\r
1307 DB::execute('UPDATE ' . sql_table('member') . ' SET mcanlogin=' . intval($oldCanLogin). ', memail=' . DB::quoteValue($oldEmail). ' WHERE mnumber=' . intval($row['vmember']));
\r
1310 // delete the activation link and ignore. member can request a new password using the
\r
1311 // forgot password link
\r
1316 // 2. delete activation entries for real
\r
1317 $query = sprintf('DELETE FROM %s WHERE vtime < %s', sql_table('activation'), DB::formatDateTime($boundary));
\r
1318 DB::execute($query);
\r
1323 * Member::$language
\r
1330 public $language = '';
\r
1332 * Member::getLanguage()
\r
1339 public function getLanguage()
\r
1341 if ( ($language = i18n::convert_locale_to_old_language_file_name($this->locale)) === FALSE )
\r