/[drupal]/contributions/modules/bugbits/bugbits.module
ViewVC logotype

Contents of /contributions/modules/bugbits/bugbits.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Fri Aug 21 23:19:20 2009 UTC (3 months ago) by daften
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +248 -166 lines
File MIME type: text/x-php
Copied the DRUPAL-6--1 branch to head (merge didn't seem to work)
1 <?php
2
3 // $Id: bugbits.module,v 1.4.2.14 2009/08/21 23:04:34 daften Exp $
4
5
6 /**
7 * @file
8 * Allows for an integration between the Mantis bug tracking system and drupal.
9 */
10
11 /**
12 * Display help and module information
13 *
14 * @param path which path of the site we're displaying help
15 * @param arg array that holds the current path as would be returned from arg() function
16 * @return help text for the path
17 */
18 function bugbits_help($path, $arg) {
19 $output = '';
20 switch ($path) {
21 case "admin/help#onthisdate":
22 $output = '<p>' . t("Integrate the Mantis bug tracking system") . '</p>';
23 break;
24 case "admin/modules#description":
25 $output = t("Integrate the Mantis bug tracking system");
26 break;
27 }
28 return $output;
29 }
30
31 /**
32 * Valid permissions for this module
33 * @return array An array of valid permissions for the bugbits module
34 */
35 function bugbits_perm() {
36 return array('access mantis', 'administer bugbits');
37 }
38
39 function bugbits_menu() {
40 $items['mantis'] = array(
41 'title' => 'Issue Tracking',
42 'page callback' => 'bugbits_all',
43 'access arguments' => array('access mantis'),
44 'type' => MENU_NORMAL_ITEM
45 );
46 $items['admin/settings/bugbits'] = array(
47 'title' => 'Bugbits settings',
48 'description' => 'Change and test bugbits settings',
49 'page callback' => 'drupal_get_form',
50 'page arguments' => array('bugbits_admin_settings'),
51 'access arguments' => array('administer bugbits'),
52 'type' => MENU_NORMAL_ITEM,
53 'file' => 'bugbits.admin.inc'
54 );
55 $items['admin/settings/bugbits/config'] = array(
56 'title' => t('Configure Bugbits'),
57 'description' => t('Change the bugbits settings'),
58 'type' => MENU_DEFAULT_LOCAL_TASK,
59 'access callback' => 'user_access',
60 'access arguments' => array('administer bugbits'),
61 );
62 $items['admin/settings/bugbits/test'] = array(
63 'title' => t('Test bugbits settings'),
64 'description' => t('Test your bugbits settings'),
65 'type' => MENU_LOCAL_TASK,
66 'page callback' => 'bugbits_admin_test',
67 'access callback' => 'user_access',
68 'access arguments' => array('administer bugbits'),
69 'file' => 'bugbits.admin.inc'
70 );
71
72 return $items;
73 }
74
75 function bugbits_all() {
76 drupal_set_header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
77 drupal_add_js(drupal_get_path('module', 'bugbits') . '/bugbits_iframe.js');
78 $url = variable_get('bugbits_mantis_url','');
79 $output .= '<iframe id="mantis_frame" src="' . $url . '" style="overflow:visible;width:100%; display:none" marginwidth="0" marginheight="0" frameborder="0" vspace="0" hspace="0" scrolling="no" width="100%"></iframe>';
80
81 return $output;
82 }
83
84 function bugbits_user($op, &$edit, &$account, $category = NULL) {
85 // Connect to the mantis database. This sets the drupal database as inactive and changes
86 // the connection. Always call _bugbits_db_set_inactive() again when finished!!!
87 require_once 'database-common.inc';
88 _bugbits_db_set_active();
89 if(!function_exists('_bugbits_db_connect')) {
90 _bugbits_db_set_inactive();
91 return;
92 }
93 _bugbits_db_connect(variable_get('bugbits_db_username', ''), variable_get('bugbits_db_password', ''), variable_get('bugbits_db_name', ''), variable_get('bugbits_db_host', ''), variable_get('bugbits_db_port', ''));
94
95 switch ($op) {
96 /** Edit has the following keys name, mail, status, roles, signature, timezone, mailalias,pass **/
97 case "insert":
98 _bugbits_create_user($edit["name"], $edit["pass"], $edit["mail"]);
99 break;
100 case "after_update":
101 _bugbits_uppass($account->name, $account->pass);
102 break;
103 case "validate":
104 if (!$account->uid) {
105 if (!_bugbits_check_name($edit["name"])) {
106 form_set_error('name', 'Sorry, that username is not available');
107 }
108 if (!_bugbits_check_email($edit["mail"])) {
109 form_set_error('mail', 'Sorry, that email address has already been taken');
110 }
111 }
112 break;
113 case "delete":
114 _bugbits_userdel($account->name);
115 break;
116 case "login":
117 /** $edit["pass"] is plain text while $user->pass is md5 hash of password **/
118 if (!bugbits_login($edit["name"], $edit["pass"])) {
119 watchdog("Bugbits", "Error user " . $edit["name"] . " was not able to log into Mantis", array(), WATCHDOG_ERROR);
120 }
121 break;
122 case "logout":
123 bugbits_logout();
124 break;
125 }
126
127 _bugbits_db_set_inactive();
128 }
129
130 function _bugbits_check_name($name) {
131 $result = _bugbits_db_query("SELECT COUNT(username) from {user_table} where username = '$name'");
132 $mant_unique = (_bugbits_db_result($result) > 0) ? false : true;
133
134 if ($mant_unique) {
135 return true;
136 }
137 else {
138 return false;
139 }
140 }
141
142 function _bugbits_check_email($email){
143 $result = _bugbits_db_query("SELECT COUNT(email) from {user_table} where email = '$email'");
144 $mant_unique = (_bugbits_db_result($result) > 0) ? false : true;
145
146 if($mant_unique){
147 return true;
148 }else{
149 return false;
150 }
151 }
152
153 function _bugbits_create_user($username, $password, $email) {
154 $password_hash = md5($password);
155
156 $cookie_hash = "";
157 $lcv = 0;
158 $c_flag = false;
159
160 do {
161 if ($c_flag) {
162 $cookie_hash = md5($cookie_hash);
163 }
164 else {
165 $cookie_hash = md5($username . "openmfg" . $password . $email);
166 }
167
168 $c_flag = bad_cookie($cookie_hash);
169 $lcv++;
170
171 } while ($c_flag && $lcv < 5);
172
173 $insert_user = "INSERT INTO {user_table}
174 ( username, email, password, date_created, last_visit,
175 enabled, access_level, login_count, cookie_string, realname )
176 VALUES ( '$username', '$email', '$password_hash', now(), now(),
177 1, 10, 0, '$cookie_hash', '');";
178
179 $result = _bugbits_db_query($insert_user);
180
181 if (!$result) {
182 watchdog("Bugbits", "User $username could not be inserted into the mantis database", array(), WATCHDOG_ERROR);
183 }
184
185 $count_query = "SELECT COUNT(id) FROM {user_table} WHERE username = '$username';";
186
187 if (_bugbits_db_result(_bugbits_db_query($count_query)) == 1) {
188 $query = "SELECT id FROM {user_table} WHERE username = '$username';";
189 $id = _bugbits_db_result(_bugbits_db_query($query));
190 }
191 else {
192 watchdog("Bugbits", "There was an error in finding the user $username while registering", array(), WATCHDOG_ERROR);
193 }
194
195 $insert_user_pref = "INSERT INTO mantis_user_pref_table
196 (user_id, project_id, default_profile, default_project, advanced_report, advanced_view,
197 advanced_update, refresh_delay, redirect_delay, bugnote_order, email_on_new,
198 email_on_assigned, email_on_feedback, email_on_resolved, email_on_closed,
199 email_on_reopened, email_on_bugnote, email_on_status, email_on_priority,
200 email_on_new_minimum_severity, email_on_assigned_minimum_severity,
201 email_on_feedback_minimum_severity, email_on_resolved_minimum_severity,
202 email_on_closed_minimum_severity, email_on_reopened_minimum_severity,
203 email_on_bugnote_minimum_severity, email_on_status_minimum_severity,
204 email_on_priority_minimum_severity, email_bugnote_limit, language)
205 VALUES ($id, '0', '0', '0', '0', '0',
206 '0', '30', '2', 'ASC', '1',
207 '1', '1', '1', '1',
208 '1', '1', '0', '0',
209 '0', '0',
210 '0', '0',
211 '0', '0',
212 '0', '0',
213 '0', '0', 'english');";
214 $result = _bugbits_db_query($insert_user_pref);
215
216 if (!$result) {
217 watchdog("Bugbits", "There was an error inserting preferences for user $username", array(), WATCHDOG_ERROR);
218 }
219
220 watchdog("Bugbits", "User $username was successfully created", array(), WATCHDOG_INFO);
221 }
222
223 function bad_cookie($cookie) {
224 $query = "SELECT COUNT(cookie_string) FROM {user_table} WHERE cookie_string = '$cookie';";
225 $result = _bugbits_db_query($query);
226 if (_bugbits_db_result($result) >= 1) {
227 return true;
228 }
229 else {
230 return false;
231 }
232 }
233
234 function _bugbits_uppass($username, $password) {
235 $query = "UPDATE {user_table} SET password = '$password' WHERE username = '$username';";
236 $result = _bugbits_db_query($query);
237 if (!$result) {
238 watchdog("Bugbits", "There was an error updating $username's password", array(), WATCHDOG_ERROR);
239 }
240 }
241
242 function _bugbits_userdel($username) {
243 watchdog('Bugbits', "Deleting user $username", array(), WATCHDOG_INFO);
244 $query = "DELETE FROM {user_table} WHERE username = '$username';";
245 $result = _bugbits_db_query($query);
246 if (!$result) {
247 watchdog("Bugbits", "There was an error deleting user $username.", array(), WATCHDOG_ERROR);
248 }
249 }
250
251 function bugbits_login($p_username, $p_password) {
252 $mantis_id = bugbits_mantis_id($p_username);
253 $processed_pass = md5($p_password);
254
255 if (false === $mantis_id) {
256 watchdog("Bugbits", "User $p_username doesn't exist in mantis, failed mantis_id check.", array(), WATCHDOG_NOTICE);
257 return false;
258 }
259 $db_pass = bugbits_mantis_pass($mantis_id);
260
261 if (!(bugbits_mantis_pass($mantis_id) == $processed_pass)) {
262 watchdog("Bugbits", "Failed password check for $p_username, db_pass = $db_pass proc pass = $processed_pass", array(), WATCHDOG_NOTICE);
263 return false;
264 }
265
266 if (!bugbits_setcookie($mantis_id)) {
267 watchdog("Bugbits", "Failed to set mantis cookie for $p_username", array(), WATCHDOG_ERROR);
268 return false;
269 }
270 else {
271 return true;
272 }
273 }
274
275 function bugbits_logout() {
276 if (!bugbits_clear_cookie()) {
277 watchdog("Bugbits", "Mantis cookie was NOT cleared on user logging out", array(), WATCHDOG_NOTICE);
278 }
279 }
280
281 function bugbits_setcookie($mantis_id) {
282 $name = 'MANTIS_STRING_COOKIE';
283 $length = 300000000 + time();
284 $path = '/';
285 $domain = '';
286
287 if ($user_string = bugbits_cookie_string($mantis_id)) {
288 return setcookie($name, $user_string, $length, $path, $domain);
289 }
290 else {
291 return false;
292 }
293 }
294
295 function bugbits_clear_cookie() {
296 $name = 'MANTIS_STRING_COOKIE';
297 $length = - 1;
298 $path = '/';
299 $domain = '';
300
301 return setcookie($name, '', $length, $path, $domain);
302 }
303
304 function bugbits_cookie_string($mantis_id) {
305 $result = _bugbits_db_query("SELECT COUNT(cookie_string) FROM {user_table} where id = $mantis_id");
306
307 if (_bugbits_db_result($result) == 1) {
308 $result = _bugbits_db_query("SELECT cookie_string FROM {user_table} where id = $mantis_id");
309 $row = _bugbits_db_fetch_object($result);
310 return $row->cookie_string;
311 }
312 else {
313 return false;
314 }
315 }
316
317 function bugbits_mantis_pass($p_user_id) {
318 $result = _bugbits_db_query("SELECT COUNT(password) FROM {user_table} WHERE id = $p_user_id");
319
320 if (_bugbits_db_result($result) == 1) {
321 $result = _bugbits_db_query("SELECT password FROM {user_table} WHERE id = $p_user_id");
322 $row = _bugbits_db_fetch_object($result);
323 return $row->password;
324 }
325 else {
326 watchdog("Bugbits", "Returning false", array(), WATCHDOG_ERROR);
327 return false;
328 }
329 }
330
331 function bugbits_mantis_id($username) {
332 $result = _bugbits_db_query("SELECT COUNT(id) FROM {user_table} WHERE username = '$username'");
333
334 if (_bugbits_db_result($result) == 0) {
335 return false;
336 }
337 else {
338 $result = _bugbits_db_query("SELECT id FROM {user_table} WHERE username = '$username'");
339 $row = _bugbits_db_fetch_object($result);
340 return $row->id;
341 }
342 }
343 ?>

  ViewVC Help
Powered by ViewVC 1.1.2