module.inc 2 KB
Newer Older
Dries's avatar
   
Dries committed
1
2
<?

Dries's avatar
   
Dries committed
3
function module_iterate($function, $argument = "") {
Dries's avatar
   
Dries committed
4
5
  global $repository;
  foreach ($repository as $name=>$module) {
Dries's avatar
   
Dries committed
6
    $function($name, $module, $argument);
Dries's avatar
   
Dries committed
7
8
9
10
11
12
13
14
15
16
17
18
  }
}

function module_execute($module, $hook, $argument = "") {
  global $repository;
  return ($repository[$module][$hook]) ? $repository[$module][$hook]($argument) : "";
}

function module_rehash_crons($name, $module) {
  if ($module["cron"]) {
    if (!db_fetch_object(db_query("SELECT * FROM crons WHERE module = '$name'"))) {
      db_query("INSERT INTO crons (module, scheduled, timestamp) VALUES ('". check_input($name) ."', '172800', '0')");
Dries's avatar
   
Dries committed
19
    }
Dries's avatar
   
Dries committed
20
21
22
23
24
25
26
  }
  else {
    db_query("DELETE FROM crons WHERE module = '$name'");
  }
}

function module_rehash_blocks($name, $module) {
Dries's avatar
   
Dries committed
27
  db_query("DELETE FROM blocks WHERE module = '$name'");
Dries's avatar
   
Dries committed
28
29
  if ($module["block"] && $blocks = $module["block"]()) {
    foreach ($blocks as $offset=>$block) {
Dries's avatar
   
Dries committed
30
31
32
33
34
35
36
      db_query("INSERT INTO blocks (name, module, offset) VALUES ('". check_input($block["info"]) ."', '". check_input($name) ."', '". check_input($offset) ."')");
    }
  }
}

function module_rehash($name) {
  global $repository;
Dries's avatar
   
Dries committed
37

Dries's avatar
   
Dries committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  if ($module = $repository[$name]) {
    $result = db_query("SELECT * FROM modules WHERE name = '$name'");

    if (!$object = db_fetch_object($result)) {
      db_query("INSERT INTO modules (name) VALUES ('". check_input($name) ."')");
    }

    // rehash crons (if necessary):
    module_rehash_crons($name, $module);

    // rehash blocks (if necessary):
    module_rehash_blocks($name, $module);
  }
  else {
    // remove all reference to module:
    db_query("DELETE FROM modules WHERE name = '$name'");
    db_query("DELETE FROM blocks WHERE module = '$name'");
    db_query("DELETE FROM crons WHERE module = '$name'");
  }
}

// load modules into repository:
$handle = opendir("modules");
while ($file = readdir($handle)) {
  if ($filename = substr($file, 0, strpos($file, ".module"))) {
    include "modules/$filename.module";
    $repository[$filename] = $module;
  }
}
closedir($handle);

?>