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

Dries's avatar
   
Dries committed
3
// applies function $function to every known module:
Dries's avatar
   
Dries committed
4
function module_iterate($function, $argument = "") {
Dries's avatar
   
Dries committed
5
6
  global $repository;
  foreach ($repository as $name=>$module) {
Dries's avatar
   
Dries committed
7
    $function($name, $module, $argument);
Dries's avatar
   
Dries committed
8
9
10
  }
}

Dries's avatar
   
Dries committed
11
// executes hook $hook of module $module with optional arguments:
Dries's avatar
   
Dries committed
12
13
14
15
16
function module_execute($module, $hook, $argument = "") {
  global $repository;
  return ($repository[$module][$hook]) ? $repository[$module][$hook]($argument) : "";
}

Dries's avatar
   
Dries committed
17
18
19
20
21
22
23
// returns true if module $module supports hook $hook, and false otherwise:
function module_hook($module, $hook) {
  global $repository;
  return $repository[$module][$hook];
}

// rehashes the crons:
Dries's avatar
   
Dries committed
24
25
26
function module_rehash_crons($name, $module) {
  if ($module["cron"]) {
    if (!db_fetch_object(db_query("SELECT * FROM crons WHERE module = '$name'"))) {
Dries's avatar
   
Dries committed
27
      db_query("INSERT INTO crons (module, scheduled, timestamp) VALUES ('$name', '172800', '0')");
Dries's avatar
   
Dries committed
28
    }
Dries's avatar
   
Dries committed
29
30
31
32
33
34
  }
  else {
    db_query("DELETE FROM crons WHERE module = '$name'");
  }
}

Dries's avatar
   
Dries committed
35
// rehashes the blocks:
Dries's avatar
   
Dries committed
36
function module_rehash_blocks($name, $module) {
37
  db_query("UPDATE blocks SET remove = '1' WHERE module = '$name'");
Dries's avatar
   
Dries committed
38
39
  if ($module["block"] && $blocks = $module["block"]()) {
    foreach ($blocks as $offset=>$block) {
Kjartan's avatar
Changes    
Kjartan committed
40
41
42
      foreach ($block as $item=>$data) {
        $block[$item] = addslashes($data);
      }
Dries's avatar
   
Dries committed
43
44
      if (!db_fetch_object(db_query("SELECT * FROM blocks WHERE module = '$name' AND name = '$block[info]'"))) {
        db_query("INSERT INTO blocks (name, module, offset) VALUES ('$block[info]', '$name', '$offset')");
45
46
      }
      else {
Dries's avatar
   
Dries committed
47
        db_query("UPDATE blocks SET offset = '$offset', remove = '0' WHERE module = '$name' AND name = '$block[info]'");
48
      }
Dries's avatar
   
Dries committed
49
50
    }
  }
51
  db_query("DELETE FROM blocks WHERE module = '$name' AND remove = '1'");
Dries's avatar
   
Dries committed
52
53
}

Dries's avatar
   
Dries committed
54
// rehashes a module:
Dries's avatar
   
Dries committed
55
56
function module_rehash($name) {
  global $repository;
Dries's avatar
   
Dries committed
57

Dries's avatar
   
Dries committed
58
59
60
61
  if ($module = $repository[$name]) {
    $result = db_query("SELECT * FROM modules WHERE name = '$name'");

    if (!$object = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
62
      db_query("INSERT INTO modules (name) VALUES ('$name')");
Dries's avatar
   
Dries committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    }

    // 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");
Kjartan's avatar
Changes    
Kjartan committed
81
$repository = array();
Dries's avatar
   
Dries committed
82
while ($file = readdir($handle)) {
Kjartan's avatar
Changes    
Kjartan committed
83
84
  if (".module" == substr($file, -7)) {
    $filename = substr($file, 0, -7);
Dries's avatar
   
Dries committed
85
86
87
88
89
90
91
    include "modules/$filename.module";
    $repository[$filename] = $module;
  }
}
closedir($handle);

?>