root/twitter/files/lib/system/event/listener/UserLoginTwitterListener.class.php @ 1330

Revision 1330, 4.3 kB (checked in by Torben Brodt, 2 years ago)

should fix twitter + facebook login when login captcha is enabled

Line 
1<?php
2require_once(WCF_DIR.'lib/util/TwitterUtil.class.php');
3
4/**
5 * display twitter link button
6 *
7 * @author      Torben Brodt
8 * @url         http://trac.easy-coding.de/trac/wcf/wiki/twitter
9 * @license     GNU General Public License <http://opensource.org/licenses/gpl-3.0.html>
10 */
11class UserLoginTwitterListener implements EventListener {
12        protected static $ignoreForms = array(
13                'rulesagree',
14                'userprofileedit',
15                'accountmanagement',
16        );
17        protected static $ignorePages = array(
18                'legalnotice'
19        );
20
21        /**
22         * @see EventListener::execute()
23         */
24        public function execute($eventObj, $className, $eventName) {
25                if (!MODULE_TWITTER || !TWITTER_CONSUMER_KEY || !TWITTER_CONSUMER_SECRET) {
26                        return;
27                }
28               
29                switch($className) {
30                       
31                        // did agree with rules?
32                        default:
33                                // didInit
34                                if($eventObj instanceof SessionFactory) $this->validateRuleAgree($eventObj->session);
35                        break;
36               
37                        // login or register with twitter
38                        case 'UserLoginForm':
39                                // readData
40                                $user = TwitterUtil::loginOrRegister($eventObj);
41                               
42                                if($user) {
43                                        // UserLoginForm should not write cookie, since interfaces only support unhashed password
44                                        $eventObj->useCookies = 0;
45                                        $eventObj->useCaptcha = 0;
46
47                                        // set cookies
48                                        UserAuth::getInstance()->storeAccessData($user, $user->username, $user->password);
49                                        HeaderUtil::setCookie('password', $user->password, TIME_NOW + 365 * 24 * 3600);
50
51                                        // save cookie and redirect
52                                        $eventObj->user = $user;
53                                        $eventObj->save();
54
55                                        exit;
56                                }
57                        break;
58                       
59                        // registered user links with twitter
60                        case 'UserProfileEditForm':
61                                // assignVariables
62                                if($eventObj->activeCategory == 'settings.general') {
63                                        TwitterUtil::updateCurrentUser();
64                                }
65                        break;
66                       
67                        // password validations can be skipped if twitter auth is successfully
68                        case 'AccountManagementForm':
69                                // validate
70                                if($eventName == 'validate' && TwitterUtil::isValidTwitterUser()) {
71
72                                        // bypass password query with a little hack
73                                        $password = UserRegistrationUtil::getNewPassword((REGISTER_PASSWORD_MIN_LENGTH > 9 ? REGISTER_PASSWORD_MIN_LENGTH : 9));
74                                        WCF::getUser()->password = StringUtil::getDoubleSaltedHash($password, WCF::getUser()->salt);
75                                        $eventObj->password = $password;
76                                }
77                               
78                                // hide password input
79                                else if($eventName == 'assignVariables' && TwitterUtil::hasTwitterUser(WCF::getUser()->userID)) {
80                                        WCF::getTPL()->append('additionalFields', '<script type="text/javascript">
81                                                $($("password").parentNode.parentNode).hide();
82                                        </script>');
83                                }
84                        break;
85                }
86        }
87
88        /**
89         *
90         */
91        protected function validateRuleAgree($session) {
92               
93                // if the modul deactivated, or the user must no agree the rules, we can leave the event.
94                // if we log out or on the rulesagree page, we also leave the event.
95                if (MODULE_RULE == 0 || $session->getUser()->getPermission('admin.general.canIgnoreRules')) return;
96                if ((isset($_REQUEST['action']) && strtolower($_REQUEST['action']) == 'userlogout') || (isset($_REQUEST['form']) && in_array(strtolower($_REQUEST['form']), self::$ignoreForms)) || (isset($_REQUEST['page']) && in_array(strtolower($_REQUEST['page']), self::$ignorePages))) return;
97               
98                // if the modul activate and the user is twitter user he must agree the rules after a change, and the user is not a guest.
99                if ($session->getUser()->userID && $session->getUser()->didSkipRulesTwitter) {
100                        // select all packageids of the packages where the user is agree with the rules.
101                        $packageIDs = $session->getVar('package_agrees');
102                       
103                        // if the packageid array null or the current package is not in the id, must check the agreement.
104                        if (is_null($packageIDs) || !in_array(PACKAGE_ID, $packageIDs)) {
105                                // we check the agreement, is the user agree with the rules, we put the package id in to the array and leave the event.
106                                if (Ruleset::isUserAgree($session->getUser()->userID, PACKAGE_ID)) {
107                                        if (is_null($packageIDs) || !is_array($packageIDs)) {
108                                                $packageIDs = array(PACKAGE_ID);
109                                        } else {
110                                                $packageIDs[] = PACKAGE_ID;
111                                        }
112                                        $session->register('package_agrees', $packageIDs);
113                                       
114                                        $session->getUser()->getEditor()->update(null, null, null, null, array(
115                                                array(
116                                                        'optionID' => User::getUserOptionID('didSkipRulesTwitter'),
117                                                        'optionValue' => 0
118                                                )
119                                        ));
120                                        return;
121                                }
122                                HeaderUtil::redirect('index.php?form=RulesAgree'.SID_ARG_2ND_NOT_ENCODED, false);
123                                exit;
124                        }
125                }
126        }
127}
128?>
Note: See TracBrowser for help on using the browser.