changed git call from https to git readonly
[atutor.git] / mods / mahara / index_admin.php
1 <?php\r
2 /*\r
3     This is the main page for the ATutor+Mahara module accessed by ATutor admins.\r
4     It checks if the current admin is registered with Mahara by checking\r
5     ATutor's 'mahara' table.  If registered, an iframe is created to display\r
6     the Mahara page ('mahara_login.php', a login script to the page also gets called).\r
7     If not registered, 'new_account_admin.php' is called which automatically sets up an\r
8     admin account with Mahara and saves the user information in ATutor's mahara table.\r
9     The page then automatically gets refreshed.\r
10 \r
11     by: Boon-Hau Teh\r
12 */\r
13 \r
14 \r
15 define('AT_INCLUDE_PATH', '../../include/');\r
16 require (AT_INCLUDE_PATH.'vitals.inc.php');\r
17 \r
18 ob_start();  // we need this to be able to set cookies\r
19 \r
20 admin_authenticate(AT_ADMIN_PRIV_MAHARA);\r
21 \r
22 \r
23  /**\r
24  * Adds an ending slash to the given path, and differentiates\r
25  * between windows and unix paths (keeps orginal slashes)\r
26  * @access  public\r
27  * @param   $string The path to some directory.\r
28  * @return  Returns the path in proper format with a trailing slash.\r
29  * @author  Boon-Hau Teh\r
30  */\r
31 function add_ending_slash($path){\r
32     // Check if last character is a slash\r
33     //$last_char = substr($path, strlen($path)-1, 1);\r
34     $last_char = $path[strlen($path)-1];\r
35     if ($last_char != '/' && $last_char != '\\') {\r
36         // determine if windows or unix\r
37         $path .= (substr_count($path, '\\') > 0) ? '\\\\' : '/'; \r
38     }\r
39     return $path;\r
40 }\r
41 \r
42 \r
43 \r
44 \r
45 if (isset($_POST['uri'])) {\r
46         $mahara_path = add_ending_slash(trim($_POST['uri']));\r
47         if (!$mahara_path){\r
48                 $msg->addError('MAHARA_MINURL_ADD_EMPTY');\r
49         }\r
50                         \r
51         if (!$msg->containsErrors()) {\r
52         // If Magic Quotes are not on, then add necessary slashes\r
53         if (!get_magic_quotes_gpc())\r
54             $mahara_path = addslashes($mahara_path);\r
55 \r
56         $sql = "REPLACE INTO ".TABLE_PREFIX."config VALUES ('mahara', '$mahara_path')";\r
57         mysql_query($sql, $db);\r
58                 $msg->addFeedback('MAHARA_MINURL_ADD_SAVED');\r
59 \r
60                 header('Location: '.$_SERVER['PHP_SELF']);\r
61                 exit;\r
62         }\r
63 }\r
64 \r
65 if (isset($_config['mahara'])) {\r
66     if (!file_exists($_config['mahara'].'config.php'))\r
67         $msg->addError ("MAHARA_ERROR_PATH");\r
68 }\r
69 \r
70 require (AT_INCLUDE_PATH.'header.inc.php');\r
71 \r
72 ?>\r
73 \r
74 <form action="<?php  $_SERVER['PHP_SELF']; ?>" method="post">\r
75         <div class="input-form">\r
76                 <div class="row">\r
77                         <p><label for="uri"><?php echo _AT('mahara_location'); ?></label></p>\r
78         \r
79                         <input type="text" name="uri" value="<?php echo $_config['mahara']; ?>" id="uri" size="80" style="min-width: 95%;" />\r
80                 </div>\r
81 \r
82                 <div class="row buttons">\r
83                         <input type="submit" name="submit" value="<?php echo _AT('save'); ?>"  />\r
84                 </div>\r
85         </div>\r
86 </form>\r
87 \r
88 \r
89 <?php\r
90 \r
91 // Show iframe to Mahara if path is correct and user has access to admin site in Mahara\r
92 \r
93 // First check if path is correct\r
94 if (isset($_config['mahara'])) {\r
95     @include ($_config['mahara'].'config.php');\r
96     if (isset($cfg)) {\r
97 \r
98         // Now check for existing account on Mahara and if it's admin\r
99 \r
100         // Read login info for Mahara\r
101         $sql    = "SELECT username, SHA1(password) FROM ".TABLE_PREFIX."mahara WHERE at_login='".$_SESSION['login']."'";\r
102         $result = mysql_query($sql, $db);\r
103 \r
104         if (!($row = @mysql_fetch_array($result))) {\r
105             define('new_admin_account', 1);\r
106 \r
107             // if not configured with ATutor, automatically register for account now\r
108             require('new_account_admin.php');\r
109 \r
110             // refresh the page\r
111             header('Location: index_admin.php');\r
112 \r
113         } else {\r
114             $username = $row[0];\r
115             $password = $row[1];\r
116 \r
117             // Login\r
118             ?>\r
119 \r
120             <?php\r
121             ATutor.setcookie("ATutor_Mahara[at_login]", $_SESSION['login'], time()+1200); \r
122             ATutor.setcookie("ATutor_Mahara[username]", $username, time()+1200); \r
123             ATutor.setcookie("ATutor_Mahara[password]", $password, time()+1200); \r
124 \r
125 \r
126             if (function_exists('url_rewrite')) {   // if "pretty url" feature supported (from ATutor 1.6.1)\r
127                 $url = url_rewrite('mods/mahara/mahara_login.php', AT_PRETTY_URL_IS_HEADER);  // to be directly called in an iframe\r
128                 $url_cookie_forward = url_rewrite('mods/mahara/cookie.php', AT_PRETTY_URL_IS_HEADER);  // to be used in a new window and redirect to mahara_login.php\r
129             } else {\r
130                 $url = AT_BASE_HREF.'mods/mahara/mahara_login.php';\r
131                 $url_cookie_forward = AT_BASE_HREF.'mods/mahara/cookie.php';\r
132             }\r
133 \r
134             ?>\r
135                 <script language='javascript' type='text/javascript'>\r
136                 function iFrameHeight() {\r
137                   var h = 0;\r
138                     if ( !document.all ) {\r
139                         h = document.getElementById('ATutorMahara').contentDocument.height;\r
140                         document.getElementById('ATutorMahara').style.height = h + 60 + 'px';\r
141                     } else if( document.all ) {\r
142                         h = document.frames('ATutorMahara').document.body.scrollHeight;\r
143                         document.all.ATutorMahara.style.height = h + 20 + 'px';\r
144                     }\r
145                 }\r
146                 </script>\r
147                 <iframe onload='iFrameHeight()' id='ATutorMahara' name='ATutorMahara'\r
148                   src='<?=$url?>' width='100%' height='500' scrolling='auto' align='top' frameborder='0'>\r
149                 </iframe>\r
150         <?php\r
151         }\r
152 \r
153 \r
154     }\r
155 }\r
156 \r
157 \r
158 require (AT_INCLUDE_PATH.'footer.inc.php');\r
159 \r
160 ob_end_flush();\r
161 \r
162 ?>\r