tagging as ATutor 1.5.4-release
[atutor.git] / tools / access.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2007 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto                  */
7 /* http://atutor.ca                                                                                                             */
8 /*                                                                                                                                              */
9 /* This program is free software. You can redistribute it and/or                */
10 /* modify it under the terms of the GNU General Public License                  */
11 /* as published by the Free Software Foundation.                                                */
12 /************************************************************************/
13 // $Id$
14
15 define('AT_INCLUDE_PATH', '../include/');
16 require(AT_INCLUDE_PATH.'vitals.inc.php');
17 authenticate(AT_PRIV_ADMIN);
18
19 if (isset($_POST['regenerate'])) {
20         $password = strtoupper(substr(md5(rand()), 3, 8));
21
22         $sql = "UPDATE ".TABLE_PREFIX."course_access SET `password`='$password' WHERE course_id=".$_SESSION['course_id'];
23         $result = mysql_query($sql, $db);
24         if (!mysql_affected_rows($db)) {
25                 // conflict. try again
26                 $password = strtoupper(substr(md5(rand()), 2, 7));
27                 $sql = "UPDATE ".TABLE_PREFIX."course_access SET `password`='$password' WHERE course_id=".$_SESSION['course_id'];
28                 $result = mysql_query($sql, $db);
29         }
30
31         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
32         header('Location: '.$_SERVER['PHP_SELF']);
33         exit;
34 } else if (isset($_POST['cancel'])) {
35         $msg->addFeedback('CANCELLED');
36         header('Location: '.$_SERVER['PHP_SELF']);
37         exit;
38 } else if (isset($_POST['submit'])) {
39         $auth = intval($_POST['auth']);
40
41         //expiry date
42         if (intval($_POST['expiry_date'])) {
43                 $day_expire             = intval($_POST['day_expire']);
44                 $month_expire   = intval($_POST['month_expire']);
45                 $year_expire    = intval($_POST['year_expire']);
46                 $hour_expire    = intval($_POST['hour_expire']);
47                 $min_expire             = intval($_POST['min_expire']);
48
49                 if (strlen($month_expire) == 1){
50                         $month_expire = "0$month_expire";
51                 }
52                 if (strlen($day_expire) == 1){
53                         $day_expire = "0$day_expire";
54                 }
55                 if (strlen($hour_expire) == 1){
56                         $hour_expire = "0$hour_expire";
57                 }
58                 if (strlen($min_expire) == 1){
59                         $min_expire = "0$min_expire";
60                 }
61                 $expiry_date = "$year_expire-$month_expire-$day_expire $hour_expire:$min_expire:00";
62         } else {
63                 $expiry_date = 0;
64         }
65
66         $sql = "UPDATE ".TABLE_PREFIX."course_access SET `expiry_date`='$expiry_date', enabled=$auth WHERE course_id=".$_SESSION['course_id'];
67         $result = mysql_query($sql, $db);
68         
69         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
70         header('Location: '.$_SERVER['PHP_SELF']);
71         exit;
72 }
73
74 require(AT_INCLUDE_PATH.'header.inc.php'); 
75
76 if ($system_courses[$_SESSION['course_id']]['access'] == 'public') { 
77         // if this course is public, then we can't use this feature
78         $msg->printInfos('ACCESS_PUBLIC');
79         require(AT_INCLUDE_PATH.'footer.inc.php'); 
80         exit;
81 }
82
83 $sql = "SELECT password, expiry_date+0 AS expiry_date, enabled FROM ".TABLE_PREFIX."course_access WHERE course_id=".$_SESSION['course_id'];
84 $result = mysql_query($sql, $db);
85
86 if ($row = mysql_fetch_assoc($result)) {                
87         $enabled = $row['enabled'];
88         $password = $row['password'];
89         $expiry = $row['expiry_date'];
90 } else {
91         $enabled = 0;
92         $password = strtoupper(substr(md5(rand()), 3, 8));
93         $expiry = 0;
94         $sql = "INSERT INTO ".TABLE_PREFIX."course_access VALUES ('$password', {$_SESSION['course_id']},'0000-00-00 00:00:00', 0)";
95         $result = mysql_query($sql, $db);
96 }
97 $url = AT_BASE_HREF.'acl.php?'.$password;
98
99 ?>
100         <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
101                 <div class="input-form">
102                         <div class="row">                               
103                                 <?php echo _AT('auth_access_text'); ?>
104                         </div>
105                         <div class="row">
106                                 <?php echo _AT('url'); ?><br />
107                                 <kbd><?php echo $url; ?></kbd>
108                         </div>
109                         <div class="row buttons">
110                                 <input type="submit" name="regenerate" value="<?php echo _AT('regenerate'); ?>"  />
111                         </div>
112                 </div>
113
114                 <div class="input-form">
115                         <div class="row">
116                                 <?php echo _AT('authenticated_access'); ?><br />
117                                 <input type="radio" name="auth" id="enable" value="1" <?php if($enabled) { echo 'checked="checked"'; } ?> /> <label for="enable"><?php echo _AT('enable'); ?></label> <input type="radio" name="auth" id="disable" value="0" <?php if(!$enabled) { echo 'checked="checked"'; } ?> /> <label for="disable"><?php echo _AT('disable'); ?></label> 
118                         </div>
119
120                         <div class="row">
121                                 <?php echo _AT('expiry_date'); ?><br />
122                                 <?php
123                                         $exp_no = $exp_yes = '';
124
125                                         if (intval($expiry)) {
126                                                 $exp_yes = ' checked="checked"';
127
128                                                 $today_day   = substr($expiry, 6, 2);
129                                                 $today_mon   = substr($expiry, 4, 2);
130                                                 $today_year  = substr($expiry, 0, 4);
131                                                 $today_hour  = substr($expiry, 8, 2);
132                                                 $today_min   = substr($expiry, 10, 2);
133
134                                         } else {
135                                                 $exp_no = ' checked="checked"'; 
136                                                 $today_day       = date('d');
137                                                 $today_mon       = date('m');
138                                                 $today_year  = date('Y');
139                                         }
140                                 ?>
141
142                                 <input type="radio" name="expiry_date" value="0" id="expire_never" <?php echo $exp_no; ?> /> <label for="expire_never"><?php echo _AT('expire_never'); ?></label><br />
143
144                                 <input type="radio" name="expiry_date" value="1" id="expire_on" <?php echo $exp_yes; ?> /> <label for="expire_on"><?php echo _AT('expire_on'); ?></label> 
145                                 <?php
146                                         $name = '_expire';
147                                         require(AT_INCLUDE_PATH.'html/release_date.inc.php');
148                                 ?>
149                         </div>
150
151                         <div class="row buttons">
152                                 <input type="submit" name="submit" value="<?php echo _AT('save'); ?>" /> 
153                                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
154                         </div>
155                 </div>
156         </form>
157 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>