changed git call from https to git readonly
[atutor.git] / mods / mahara / mahara_login.php
1 <?php\r
2 \r
3 /*\r
4     This belongs to the ATutor Mahara module page. It is called within an iframe or \r
5     a new window from index.php and allows a user to access\r
6     his/her ePortfolio account on Mahara through their account on ATutor.\r
7 \r
8     Login information for Mahara is passed using cookies (password encrypted in SHA1).\r
9     This is to avoid conflicting sessions between ATutor and Mahara from within\r
10     the same script.\r
11 \r
12     by: Boon-Hau Teh\r
13 */\r
14 \r
15 $_user_location = 'public';\r
16 \r
17 define('AT_INCLUDE_PATH', '../../include/');\r
18 \r
19 \r
20 /*~~~~~~~~~~~~~few essentials copied from ATutor's vitals.inc.php~~~~~~~~~~~~*/\r
21 \r
22     /**** 0. start system configuration options block ****/\r
23     error_reporting(0);\r
24     if (!defined(AT_REDIRECT_LOADED)){\r
25         include_once(AT_INCLUDE_PATH.'config.inc.php');\r
26     }\r
27     error_reporting(AT_ERROR_REPORTING);\r
28 \r
29     if (!defined('AT_INSTALL') || !AT_INSTALL) {\r
30         header('Cache-Control: no-store, no-cache, must-revalidate');\r
31         header('Pragma: no-cache');\r
32 \r
33         $relative_path = substr(AT_INCLUDE_PATH, 0, -strlen('include/'));\r
34         header('Location: ' . $relative_path . 'install/not_installed.php');\r
35         exit;\r
36     }\r
37 \r
38     /*** 1. constants ***/\r
39     if (!defined(AT_REDIRECT_LOADED)){\r
40         require_once(AT_INCLUDE_PATH.'lib/constants.inc.php');\r
41     }\r
42 \r
43     $db = @mysql_connect(DB_HOST . ':' . DB_PORT, DB_USER, DB_PASSWORD);\r
44     if (!$db) {\r
45         /* AT_ERROR_NO_DB_CONNECT */\r
46         require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');\r
47         $err =& new ErrorHandler();\r
48         trigger_error('VITAL#Unable to connect to db.', E_USER_ERROR);\r
49         exit;\r
50     }\r
51     if (!@mysql_select_db(DB_NAME, $db)) {\r
52         require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');\r
53         $err =& new ErrorHandler();\r
54         trigger_error('VITAL#DB connection established, but database "'.DB_NAME.'" cannot be selected.',\r
55                         E_USER_ERROR);\r
56         exit;\r
57     }\r
58 \r
59     /* get config variables. if they're not in the db then it uses the installation default value in constants.inc.php */\r
60     $sql    = "SELECT * FROM ".TABLE_PREFIX."config";\r
61     $result = mysql_query($sql, $db);\r
62     while ($row = mysql_fetch_assoc($result)) { \r
63         $_config[$row['name']] = $row['value'];\r
64     }\r
65 \r
66     /***** 7. start language block *****/\r
67         // set current language\r
68         require(AT_INCLUDE_PATH . 'classes/Language/LanguageManager.class.php');\r
69         $languageManager =& new LanguageManager();\r
70 \r
71         $myLang =& $languageManager->getMyLanguage();\r
72 \r
73         if ($myLang === FALSE) {\r
74             echo 'There are no languages installed!';\r
75             exit;\r
76         }\r
77         $myLang->saveToSession();\r
78         if (isset($_GET['lang']) && $_SESSION['valid_user']) {\r
79             if ($_SESSION['course_id'] == -1) {\r
80                 $myLang->saveToPreferences($_SESSION['login'], 1);      //1 for admin                   \r
81             } else {\r
82                 $myLang->saveToPreferences($_SESSION['member_id'], 0);  //0 for non-admin\r
83             }\r
84         }\r
85         $myLang->sendContentTypeHeader();\r
86 \r
87         /* set right-to-left language */\r
88         $rtl = '';\r
89         if ($myLang->isRTL()) {\r
90             $rtl = 'rtl_'; /* basically the prefix to a rtl variant directory/filename. eg. rtl_tree */\r
91         }\r
92     /***** end language block ****/\r
93 \r
94 /*~~~~~~~~~~~~~~~~~~~~~~~end of vitals.inc.php~~~~~~~~~~~~~~~~~~~~~~*/\r
95 \r
96 \r
97 \r
98 \r
99 \r
100 // Read Mahara login information from cookies passed by ATutor\r
101 $usr = array();\r
102 if (isset($_COOKIE['ATutor_Mahara'])) {\r
103     foreach ($_COOKIE['ATutor_Mahara'] as $name => $value) {\r
104         $usr[$name] = $value;\r
105 \r
106         // expire the cookie\r
107         ATutor.setcookie ("ATutor_Mahara[".$name."]", "", time() - 3600);\r
108     }\r
109     //expire the cookie array\r
110     ATutor.setcookie ("ATutor_Mahara", "", time() - 3600);\r
111 } else {\r
112     echo 'Unable to detect cookies or the session has timed out.  Please check that cookies are enabled on your browser and try again.';\r
113     exit;\r
114 }\r
115 \r
116 // Get password from ATutor's database\r
117 $sql    = "SELECT password FROM ".TABLE_PREFIX."mahara WHERE at_login='".$usr["at_login"]."' AND username='".$usr["username"]."' AND SHA1(password)='".$usr["password"]."'";\r
118 $result = mysql_query($sql, $db);\r
119 if (!($row = @mysql_fetch_array($result))) {\r
120     echo 'Incorrect login information. Please check with course instructor or administrator.';\r
121     exit;\r
122 } else {\r
123     $pwd = $row[0];\r
124 \r
125     if (isset($_config['mahara'])) {\r
126 \r
127         /****** Taken from index.php of /mahara  *****/\r
128         define('INTERNAL', 1);\r
129         define('PUBLIC', 1);\r
130         define('MENUITEM', '');\r
131         define (MAHARA_PATH, $_config['mahara']);\r
132         require (MAHARA_PATH.'init.php');\r
133         define('TITLE', get_string('home'));\r
134 \r
135         // Check if user exists in Mahara\r
136         if (!(record_exists('usr', 'username', $usr["username"]))) {\r
137             // Reconnect to ATutor Database and remove the record from the mahara table\r
138             $db_atutor = @mysql_connect(DB_HOST . ':' . DB_PORT, DB_USER, DB_PASSWORD);\r
139             if (!$db_atutor) {\r
140                 /* AT_ERROR_NO_DB_CONNECT */\r
141                 require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');\r
142                 $err =& new ErrorHandler();\r
143                 trigger_error('VITAL#Unable to connect to db.', E_USER_ERROR);\r
144                 exit;\r
145             }\r
146             if (!@mysql_select_db(DB_NAME, $db_atutor)) {\r
147                 require_once(AT_INCLUDE_PATH . 'classes/ErrorHandler/ErrorHandler.class.php');\r
148                 $err =& new ErrorHandler();\r
149                 trigger_error('VITAL#DB connection established, but database "'.DB_NAME.'" cannot be selected.',\r
150                                 E_USER_ERROR);\r
151                 exit;\r
152             }\r
153 \r
154             // Delete record from ATutor database since it should not be there\r
155             $sql = "DELETE FROM ".TABLE_PREFIX."mahara WHERE at_login='".$usr["at_login"]."'";\r
156 \r
157             $result = mysql_query($sql, $db_atutor);\r
158 \r
159             echo "Successfully synchronized user login with Mahara database. Please refresh the page from ATutor.";\r
160             exit;\r
161         }\r
162 \r
163         session_start();\r
164 \r
165         /*~~~~~~~~~~~copied from index.php of Mahara~~~~~~~~~~~~~~~*/\r
166         // Check for whether the user is logged in, before processing the page. After\r
167         // this, we can guarantee whether the user is logged in or not for this page.\r
168         if (!$USER->is_logged_in()) {\r
169             $lang = param_alphanumext('lang', null);\r
170             if (!empty($lang)) {\r
171                 $SESSION->set('lang', $lang);\r
172             }\r
173 \r
174             // Read login information\r
175             $values['login_username'] = $usr["username"];\r
176             $values['login_password'] = $pwd;\r
177             $values['submit'] = "Login";\r
178             $values['sesskey'] = "";\r
179             $values['pieform_login'] = "";\r
180 \r
181             // login\r
182             login_submit(null, $values);\r
183 \r
184             $adminpage = ($USER->get('admin')) ? 'admin/' : '';\r
185         }\r
186 \r
187         /* Logged in session should be created.  Now redirect to the Mahara page\r
188            and it should read from this session\r
189          */\r
190         header('Location: '.get_config('wwwroot').$adminpage);\r
191         /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/\r
192     } else {\r
193         echo 'You have incorrect config settings for the Mahara module.';\r
194         exit;\r
195     }\r
196 }\r
197 \r
198 \r
199 ?>