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

Revision 1305, 4.1 kB (checked in by Torben Brodt, 2 years ago)
  • show rules to users registering with twitter
  • twitter update job to fetch feeds
RevLine 
[1286]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 {
[1305]12        protected static $ignoreForms = array(
13                'rulesagree',
14                'userprofileedit',
15                'accountmanagement',
16        );
17        protected static $ignorePages = array(
18                'legalnotice'
19        );
[1286]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) {
[1305]30                       
31                        // did agree with rules?
32                        case 'SessionFactory':
33                                // didInit
34                                $this->validateRuleAgree($eventObj->session);
35                        break;
36               
[1291]37                        // login or register with twitter
38                        case 'UserLoginForm':
39                                // readData
[1298]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
46                                        // set cookies
47                                        UserAuth::getInstance()->storeAccessData($user, $user->username, $user->password);
48                                        HeaderUtil::setCookie('password', $user->password, TIME_NOW + 365 * 24 * 3600);
49
50                                        // save cookie and redirect
51                                        $eventObj->user = $user;
52                                        $eventObj->save();
53
54                                        exit;
55                                }
[1291]56                        break;
57                       
[1286]58                        // registered user links with twitter
59                        case 'UserProfileEditForm':
60                                // assignVariables
61                                if($eventObj->activeCategory == 'settings.general') {
62                                        TwitterUtil::updateCurrentUser();
63                                }
64                        break;
[1291]65                       
66                        // password validations can be skipped if twitter auth is successfully
67                        case 'AccountManagementForm':
68                                // validate
69                                if($eventName == 'validate' && TwitterUtil::isValidTwitterUser()) {
70
71                                        // bypass password query with a little hack
72                                        $password = UserRegistrationUtil::getNewPassword((REGISTER_PASSWORD_MIN_LENGTH > 9 ? REGISTER_PASSWORD_MIN_LENGTH : 9));
73                                        WCF::getUser()->password = StringUtil::getDoubleSaltedHash($password, WCF::getUser()->salt);
74                                        $eventObj->password = $password;
75                                }
76                               
77                                // hide password input
[1298]78                                else if($eventName == 'assignVariables' && TwitterUtil::hasTwitterUser(WCF::getUser()->userID)) {
[1291]79                                        WCF::getTPL()->append('additionalFields', '<script type="text/javascript">
80                                                $($("password").parentNode.parentNode).hide();
81                                        </script>');
82                                }
83                        break;
[1286]84                }
85        }
[1305]86
87        /**
88         *
89         */
90        protected function validateRuleAgree($session) {
91               
92                // if the modul deactivated, or the user must no agree the rules, we can leave the event.
93                // if we log out or on the rulesagree page, we also leave the event.
94                if (MODULE_RULE == 0 || $session->getUser()->getPermission('admin.general.canIgnoreRules')) return;
95                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;
96               
97                // 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.
98                if ($session->getUser()->userID && TwitterUtil::hasTwitterUser($session->getUser()->userID)) {
99                        // select all packageids of the packages where the user is agree with the rules.
100                        $packageIDs = $session->getVar('package_agrees');
101                       
102                        // if the packageid array null or the current package is not in the id, must check the agreement.
103                        if (is_null($packageIDs) || !in_array(PACKAGE_ID, $packageIDs)) {
104                                // we check the agreement, is the user agree with the rules, we put the package id in to the array and leave the event.
105                                if (Ruleset::isUserAgree($session->getUser()->userID, PACKAGE_ID)) {
106                                        if (is_null($packageIDs) || !is_array($packageIDs)) 
107                                                $packageIDs = array(PACKAGE_ID);
108                                        else $packageIDs[] = PACKAGE_ID;
109                                                $session->register('package_agrees', $packageIDs);
110                                        return;
111                                }
112                                HeaderUtil::redirect('index.php?form=RulesAgree'.SID_ARG_2ND_NOT_ENCODED, false);
113                                exit;
114                        }
115                }
116        }
[1286]117}
118?>
Note: See TracBrowser for help on using the browser.