/[scire]/trunk/scire/.lib/DB_functions.php
Gentoo

Contents of /trunk/scire/.lib/DB_functions.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 338 - (show annotations) (download)
Sun Jan 6 21:59:21 2008 UTC (6 years, 7 months ago) by codeman
File size: 15316 byte(s)
adding recurring_jobs_clients to hold the information for recurring jobs.

1 <?php
2
3 function get_scire_modules() {
4 global $db;
5 $result = $db->select('*', 'modules');
6 if ($result && count($result) > 0) {
7 return $result;
8 } else {
9 return array();
10 }
11 }
12
13 function get_scire_module_list() {
14 global $db;
15 $result = $db->select(array('main_content', 'name'), 'modules');
16 if ($result && count($result) > 0) {
17 return $result;
18 } else {
19 return array();
20 }
21 }
22
23 function get_profile_list() {
24 global $db;
25 $result = $db->select('profile_name', 'GLI_profiles');
26 if ($result && count($result) > 0) {
27 return $result;
28 } else {
29 return array();
30 }
31 }
32
33 function get_group_clients($group) {
34 global $db;
35 $result = $db->select('*', 'clients', '`gli_profile` = \'' . $group . '\'');
36 if ($result && count($result) > 0) {
37 return $result;
38 } else {
39 return array();
40 }
41 }
42
43 function get_scire_clients($orderby, $direction, $status='Active') {
44 global $db;
45 if ($status == "All") {
46 $where = ""; #Don't filter on status.
47 } elseif ($status) {
48 $where = " WHERE cs.statusname='$status' ";
49 } else {
50 $where = " WHERE cs.statusname='Active' ";
51 }
52 if ($orderby) {
53 $where .= "ORDER BY `$orderby` $direction";
54 } else {
55 $where .= '';
56 }
57 $result = $db->select('SELECT c.clientid, c.assetid, c.digest, c.hostname, c.mac, c.ip, cs.statusname as status, c.installtime, p.profile_name, o.osname, u.username FROM clients AS c LEFT JOIN GLI_profiles AS p ON (c.gli_profile=p.profileid) LEFT JOIN os AS o ON (c.osid=o.osid) LEFT JOIN users AS u ON (c.contact=u.userid) LEFT JOIN client_status AS cs ON (c.status=cs.statusid) '.$where);
58 if ($result && count($result) > 0) {
59 return $result;
60 } else {
61 return array();
62 }
63 }
64
65 function get_scire_client($clientid) {
66 global $db;
67 $result = $db->select('*', 'clients', '`clientid` = \'' . $clientid . '\'');
68 if ($result && count($result) > 0) {
69 return $result[0];
70 } else {
71 return false;
72 }
73 }
74
75 function get_client_statuses() {
76 global $db;
77 $result = $db->select('*', 'client_status');
78 if ($result && count($result) > 0) {
79 return $result;
80 } else {
81 return array();
82 }
83 }
84
85 function scire_add_client($clientid, $digest, $hostname, $mac, $ip, $profileid, $osid, $contact, $status, $installtime, $assetid) {
86 #clientid, assetid, digest, cert, hostname, mac, ip, gli_profile, osid, status, contact, installtime
87 if (!$installtime) {$installtime = $db->now(); }
88 global $db;
89 #we need to add the client to the axo table first and then to the clients
90 $result = $db->insert('clients', array('clientid' => $clientid, 'digest' => $digest, 'hostname' => $hostname, 'mac' => $mac, 'ip' => $ip, 'gli_profile' => $profileid, 'osid' => $osid, 'contact' => $contact, 'status' => $status, 'installtime' => $installtime, 'assetid' => $assetid));
91 if ($result) {
92 return true;
93 } else {
94 return $db->error;
95 }
96 }
97
98 function scire_edit_client($clientid, $fields) {
99 global $db;
100 if (isset($fields['status'])) {
101 $res = $db->select(array('statusid'), 'client_status', '`statusname` = \'' . $fields['status'] . '\'');
102 var_dump($res);
103 if ($res && count($res)>0)
104 $fields['status'] = $res[0]['statusid'];
105 else
106 unset($fields['status']);
107 }
108 $result = $db->update('clients', $fields, "`clientid` = $clientid");
109 if ($result) {
110 return true;
111 } else {
112 return $db->error;
113 }
114 }
115
116 function scire_del_client($client_id) {
117 global $db;
118 $result = $db->delete('clients', "`clientid` = $client_id");
119 if ($result) {
120 return true;
121 } else {
122 return $db->error;
123 }
124 }
125
126 function get_scire_users($orderby, $direction) {
127 #we don't want to store the password for security reasons. <- ???
128 global $db;
129 if ($orderby) {
130 $where = "1 ORDER BY `$orderby` $direction";
131 } else {
132 $where = '1';
133 }
134 $result = $db->select(array('userid', 'username', 'comment', 'real_name'), 'users', $where);
135 if ($result && count($result) > 0) {
136 return $result;
137 } else {
138 return array();
139 }
140 }
141
142 function get_scire_user($userid) {
143 global $db;
144 $result = $db->select('*', 'users', '`userid` = ' . $userid . '');
145 if ($result && count($result) > 0) {
146 return $result[0];
147 } else {
148 return false;
149 }
150 }
151
152 function scire_add_user($userid, $username, $password, $real_name, $comment, $email, $phone, $pager) {
153 global $db;
154 $result = $db->insert('users', array('userid' => $userid, 'username' => $username, 'password' => crypt($password, 447470567), 'comment' => $comment, 'real_name' => $real_name, 'email' => $email, 'phone' => $phone, 'pager' => $pager));
155 if ($result) {
156 return true;
157 } else {
158 return $db->error;
159 }
160 }
161
162 function scire_edit_user($userid, $fields) {
163 global $db;
164 $result = $db->update('users', $fields, '`userid` = \'' . $userid . '\'');
165 if ($result) {
166 return true;
167 } else {
168 return $db->error;
169 }
170 }
171
172 function scire_del_user($userid) {
173 global $db;
174 $result = $db->delete('users', '`userid` = \'' . $userid . '\'');
175 if ($result) {
176 return true;
177 } else {
178 return $db->error;
179 }
180 }
181
182 function scire_add_permission($permid, $perm_name, $desc, $created_by) {
183 global $db;
184 $result = $db->insert('permissions', array('permid' => $permid, 'name' => $perm_name, 'description' => $desc, 'creator' => $created_by));
185 if ($result) {
186 return true;
187 } else {
188 return $db->error;
189 }
190 }
191
192 function scire_edit_permission($perm_name, $desc) {
193 global $db;
194 $result = $db->update('permissions', array('description' => $desc), '`perm_name` = \'' . $perm_name . '\'');
195 if ($result) {
196 return true;
197 } else {
198 return $db->error;
199 }
200 }
201
202 function scire_del_permission($perm_name) {
203 global $db;
204 $result = $db->delete('permissions', '`perm_name` = \'' . $perm_name . '\'');
205 if ($result) {
206 return true;
207 } else {
208 return $db->error;
209 }
210 }
211
212 function get_scire_permissions() {
213 global $db;
214 $result = $db->select('*', 'permissions');
215 if ($result && count($result) > 0) {
216 return $result;
217 } else {
218 return array();
219 }
220 }
221
222 function get_scire_permission_by_name($permname) {
223 global $db;
224 $result = $db->select('*', 'permissions', "`name` = '$permname'");
225 if ($result && count($result) > 0) {
226 return $result[0];
227 } else {
228 return false;
229 }
230
231 }
232 function get_scire_ui_permissions() {
233 global $db;
234 $result = $db->select('*', 'permissions', "`permcategory` = 'scire_ui'");
235 if ($result && count($result) > 0) {
236 return $result;
237 } else {
238 return array();
239 }
240 }
241
242 function get_scire_gli_profiles() {
243 global $db;
244 $result = $db->select(array('profile_name','profileid'), 'GLI_profiles');
245 if ($result && count($result) > 0) {
246 return $result;
247 } else {
248 return array();
249 }
250 }
251
252 function get_scire_oss() {
253 global $db;
254 $result = $db->select(array('osname','osid'), 'os');
255 if ($result && count($result) > 0) {
256 return $result;
257 } else {
258 return array();
259 }
260 }
261
262 function get_scire_script($scriptid) {
263 global $db;
264 $scriptid = (int) $scriptid;
265 $result = $db->select('*', 'scripts', "`scriptid` = $scriptid");
266 if ($result && count($result) > 0) {
267 return $result[0];
268 } else {
269 return false;
270 }
271 }
272
273 function get_scire_scripts() {
274 global $db;
275 $result = $db->select('*', 'scripts');
276 if ($result && count($result) > 0) {
277 return $result;
278 } else {
279 return array();
280 }
281 }
282
283 function get_scire_settings($userid) {
284 global $db;
285 $result = $db->select('*', 'settings', "`userid` = $userid");
286 if ($result && count($result) > 0) {
287 $result2 = array();
288 foreach($result as $row) {
289 $result2[$row['setting_name']] = $row['setting_value'];
290 }
291 return $result2;
292 } else {
293 return array();
294 }
295 }
296
297 function scire_set_setting($userid, $name, $value) {
298 global $db;
299 $result = $db->update('settings',array('setting_value' => $value), "`userid` = '$userid' AND `setting_name`='$name'");
300 if ($result) {
301 return true;
302 } else {
303 return $db->error;
304 }
305 }
306
307 function scire_add_setting($userid, $name, $value) {
308 global $db;
309 $result = $db->insert('settings',array('userid' => $userid, 'setting_name' => $name, 'setting_value' => $value ));
310 if ($result) {
311 return true;
312 } else {
313 return $db->error;
314 }
315 }
316
317 function scire_approve_client($clientid) {
318 global $db;
319 $clientid = (int) $clientid;
320 $res = $db->select(array('statusid'), 'client_status', '`statusname` = \'Active\'');
321
322 if ($clientid) {
323 $result = $db->update('clients',array('status' => $res[0]['statusid']), "`clientid` = '$clientid'");
324 if ($result) {
325 return true;
326 } else {
327 return $db->error;
328 }
329 } else {
330 return false;
331 }
332 }
333
334 function scire_reject_client($clientid) {
335 global $db;
336 if (int($clientid) != $clientid) {return false;}
337 $res = $db->select(array('statusid'), 'client_status', '`statusname` = \'Rejected\'');
338
339 $result = $db->update('clients',array('status' => $res[0]['statusid']), "`clientid` = '$clientid'");
340 if ($result) {
341 return true;
342 } else {
343 return $db->error;
344 }
345 }
346
347 function scire_add_job($script, $priority, $creator, $permission, $description, $pending, $clients, $clientgroups, $job_dependency, $run_schedule, $validity_period) {
348 global $db;
349 #First make the job id
350 $jobid = $db->get_next_id('jobs_seq');
351 print "Got a Job ID, it is $jobid <br>";
352 $result = $db->insert('jobs', array('jobid' => $jobid, 'script' => $script, 'priority' => $priority, 'created' => $db->now(), 'creator' => $creator, 'permission' => $permission, 'description' => $description, 'pending' => $pending ));
353 if (!$result) {
354 return $db->error;
355 }
356
357 if ($run_schedule != "") {
358 print "Run schedule is $run_schedule<br>";
359 $cron = new CronParser($run_schedule);
360 $nextRun = $cron->calculateNextRun();
361 var_dump($nextRun);
362 $nextRun = mktime( $nextRun[1], $nextRun[0], 0, $nextRun[3], $nextRun[2], $nextRun[4] );
363 $expTime = $nextRun + ( $validity_period * 60);
364 $nextRun = strftime( '%Y-%m-%d %T', $nextRun );
365 $expTime = strftime( '%Y-%m-%d %T', $expTime );
366 } else {
367 $nextRun = strftime( '%Y-%m-%d %T', time());
368 $expTime = "";
369 }
370 # Add conditions
371 $result = $db->insert('job_conditions', array('jobid' => $jobid, 'job_dependency' => $job_dependency, 'run_schedule' => $run_schedule, 'deploy_time' => $nextRun, 'expiration_time' => $expTime, 'validity_period' => $validity_period));
372 if (!$result) {
373 return $db->error;
374 }
375
376 # Add history entry only if the job is assigned to a specific
377 # client. Managing groups require a different approach
378 #Now add the clients.
379 $status = get_statusid('Pending');
380 if ($clients) {
381 foreach ($clients as $client) {
382 $result = $db->insert('job_history', array('jobid' => $jobid, 'clientid' => $client,
383 'statusid' => $status,
384 'eventmsg' => 'Job created'));
385 if (!$result) {
386 return $db->error;
387 }
388 $result = $db->insert('jobs_clients', array('jobid' => $jobid, 'clientid' => $client));
389 if (!$result) {
390 return $db->error;
391 }
392 if ($run_schedule != "") {
393 $result = $db->insert('recurring_jobs_clients', array('jobid' => $jobid, 'clientid' => $client));
394 if (!$result) {
395 return $db->error;
396 }
397 }
398 }
399 }
400
401 if ($clientgroups) {
402 foreach ($clientgroups as $cgroup) {
403 $result = $db->insert('jobs_clients', array('jobid' => $jobid, 'groupid' => $cgroup));
404 if (!$result) {
405 return $db->error;
406 }
407 if ($run_schedule != "") {
408 $result = $db->insert('recurring_jobs_clients', array('jobid' => $jobid, 'groupid' => $cgroup));
409 if (!$result) {
410 return $db->error;
411 }
412 }
413 }
414 }
415 return 0; #Success
416 }
417
418 function get_scire_jobs($orderby, $direction, $status='Pending') {
419 global $db;
420
421 if ($status == "All") {
422 $where = ""; #Don't filter on pending status.
423 } elseif ($status == "Pending") {
424 $where = " AND j.pending > 0 ";
425 } else {
426 $where = " ";
427 }
428 if ($orderby) {
429 $where .= " ORDER BY `$orderby` $direction";
430 } else {
431 $where .= '';
432 }
433
434 $result = $db->select('SELECT j.*,jc.groupid,c.hostname,s.name as scriptname, u.username, p.name as permname '.
435 'FROM jobs AS j '.
436 'JOIN jobs_clients AS jc ON (j.jobid=jc.jobid) '.
437 'JOIN clients AS c ON (jc.clientid=c.clientid) '.
438 'LEFT JOIN scripts AS s ON (j.script=s.scriptid) '.
439 'JOIN users AS u ON (j.creator=u.userid) '.
440 'LEFT JOIN permissions AS p ON (j.permission=p.permid) '.
441 ' '.$where);
442 if ($result && count($result) > 0) {
443 return $result;
444 } else {
445 return array();
446 }
447 }
448
449 function get_scire_job($jobid) {
450 global $db;
451 # Falta el job history y el job clients
452 $result = $db->select('SELECT j.jobid, j.priority, j.script, j.description, u.username as creator, p.name as permission, jc.validity_period, jc.run_schedule ' .
453 'FROM jobs AS j '.
454 'JOIN users AS u ON (j.creator=u.userid) '.
455 'LEFT JOIN job_conditions AS jc ON (j.jobid=jc.jobid) '.
456 'LEFT JOIN permissions AS p ON (j.permission=p.permid) '.
457 ' WHERE j.jobid=\'' . (int) $jobid . '\'');
458 if ($result && count($result) > 0) {
459 return $result[0];
460 } else {
461 return false;
462 }
463 }
464
465 function scire_edit_job($jobid, $fields) {
466 global $db;
467 $jobConditions = array();
468 if (isset($fields["run_schedule"])) {
469 $jobConditions["run_schedule"] = $fields["run_schedule"];
470 unset($fields["run_schedule"]);
471 }
472 if (isset($fields["validity_period"])) {
473 $jobConditions["validity_period"] = $fields["validity_period"];
474 unset($fields["validity_period"]);
475 }
476 $result = $db->update('jobs', $fields, '`jobid` = \'' . $jobid . '\'');
477
478 if (count($jobConditions) > 0) {
479 $result = $result && $db->update('job_conditions', $jobConditions, '`jobid` = \'' . $jobid . '\'');
480 }
481
482 if ($result) {
483 return true;
484 } else {
485 return $db->error;
486 }
487 }
488
489 function get_statusid($statusname) {
490 global $db;
491 $name = htmlentities($statusname);
492 $result = $db->select('SELECT statusid FROM jobs_status WHERE statusname = \'' . $name . '\'');
493
494 if ($result) {
495 var_dump( $result[0]['statusid'] );
496
497 return $result[0]['statusid'];
498 }
499 else {
500 return $db->error;
501 }
502 }
503
504 function scire_add_script($name, $desc, $location, $script_data, $log_location, $success_code, $run_as, $priority, $permission, $pp_location, $pp_script_data, $script_tags) {
505 global $db;
506 # First make the script ID
507 $scriptid = $db->get_next_id('scripts_seq');
508 print "Got a Script ID, it is $scriptid<br>";
509
510 $result = $db->insert('scripts', array('scriptid' => $scriptid, 'name' => $name, 'description' => $desc, 'location' => $location, 'script_data' => $script_data, 'log_location' => $log_location, 'success_code' => $success_code, 'run_as' => $run_as, 'priority' => $priority, 'permission' => $permission, 'pp_location' => $pp_location, 'pp_script_data' => $pp_script_data ));
511 if (!$result) {
512 return $db->error;
513 }
514
515 #Now add the tags.
516 foreach ($script_tags as $name => $value) {
517 $result = $db->insert('dyn_tags', array('scriptid' => $scriptid, 'tag' => $name, 'tag_value' => $value) );
518 if (!$result) {
519 return $db->error;
520 }
521 }
522
523 return 0; #Success
524 }
525
526 function get_dyn_tag_value($scriptid,$tag) {
527 global $db;
528 $scriptid = (int) $scriptid;
529 $result = $db->select('tag_value', 'dyn_tags', "`scriptid` = $scriptid AND `tag` = '$tag'");
530 if ($result && count($result) > 0) {
531 return $result[0]['tag_value'];
532 } else {
533 return false;
534 }
535 }
536
537
538
539 ?>

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20