module.inc 2.6 KB
Newer Older
Dries's avatar
   
Dries committed
1
<?php
2
// $Id$
Dries's avatar
   
Dries committed
3

Dries's avatar
   
Dries committed
4
5
6
7
8
9
// initialize modules:
function module_init() {
  module_list();
}

// apply function $function to every known module:
Dries's avatar
   
Dries committed
10
function module_iterate($function, $argument = "") {
Dries's avatar
   
Dries committed
11
12
13
14
  foreach (module_list() as $name) $function($name, $argument);
}

// invoke hook $hook of module $name with optional arguments:
Dries's avatar
   
Dries committed
15
function module_invoke($name, $hook, $a1 = NULL, $a2 = NULL, $a3 = NULL, $a4 = NULL) {
Dries's avatar
   
Dries committed
16
  $function = $name ."_". $hook;
Dries's avatar
   
Dries committed
17
  if (function_exists($function)) {
Dries's avatar
   
Dries committed
18
    return $function($a1, $a2, $a3, $a4);
Dries's avatar
   
Dries committed
19
  }
Dries's avatar
   
Dries committed
20
21
}

Dries's avatar
   
Dries committed
22
// return array of module names (includes lazy module loading):
Dries's avatar
   
Dries committed
23
24
25
26
function module_list() {
  static $list;

  if (!$list) {
Dries's avatar
   
Dries committed
27
28
29
30
31
32
33
34
    if ($handle = @opendir("modules")) {
      $list = array();
      while ($file = readdir($handle)) {
        if (".module" == substr($file, -7)) {
          $filename = substr($file, 0, -7);
          include "modules/$filename.module";
          $list[$filename] = $filename;
        }
Dries's avatar
   
Dries committed
35
      }
Dries's avatar
   
Dries committed
36
37
      closedir($handle);
      asort($list);
Dries's avatar
   
Dries committed
38
    }
Dries's avatar
   
Dries committed
39
40
41
    else {
      $list = array();
    }
Dries's avatar
   
Dries committed
42
  }
Dries's avatar
   
Dries committed
43
44

  return $list;
Dries's avatar
   
Dries committed
45
46
}

Dries's avatar
   
Dries committed
47
48
49
50
// return 1 if module $name exists, 0 otherwise:
function module_exist($name) {
  $list = module_list();
  return ($list[$name]) ? 1 : 0;
Dries's avatar
   
Dries committed
51
52
}

Dries's avatar
   
Dries committed
53
54
55
// return 1 if module $name implements hook $hook, 0 otherwise:
function module_hook($name, $hook) {
  return function_exists($name ."_". $hook);
Dries's avatar
   
Dries committed
56
57
}

Dries's avatar
   
Dries committed
58
59
// rehash module-exported blocks:
function module_rehash_blocks($name) {
60
  db_query("UPDATE blocks SET remove = '1' WHERE module = '$name'");
Dries's avatar
   
Dries committed
61

Dries's avatar
   
Dries committed
62
  if ($blocks = module_invoke($name, "block")) {
Dries's avatar
   
Dries committed
63
64
    foreach ($blocks as $delta => $block) {
      foreach ($block as $item => $data) {
Kjartan's avatar
Changes    
Kjartan committed
65
66
        $block[$item] = addslashes($data);
      }
Dries's avatar
   
Dries committed
67
      if (!db_fetch_object(db_query("SELECT * FROM blocks WHERE module = '$name' AND name = '$block[info]'"))) {
Dries's avatar
   
Dries committed
68
        db_query("INSERT INTO blocks (name, module, delta) VALUES ('$block[info]', '$name', '$delta')");
69
70
      }
      else {
Dries's avatar
   
Dries committed
71
        db_query("UPDATE blocks SET delta = '$delta', remove = '0' WHERE module = '$name' AND name = '$block[info]'");
72
      }
Dries's avatar
   
Dries committed
73
74
    }
  }
Dries's avatar
   
Dries committed
75

76
  db_query("DELETE FROM blocks WHERE module = '$name' AND remove = '1'");
Dries's avatar
   
Dries committed
77
78
}

Dries's avatar
   
Dries committed
79
// rehash a module:
Dries's avatar
   
Dries committed
80
function module_rehash($name) {
Dries's avatar
   
Dries committed
81
  if (module_exist($name)) {
Dries's avatar
   
Dries committed
82
83
84
    $result = db_query("SELECT * FROM modules WHERE name = '$name'");

    if (!$object = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
85
      db_query("INSERT INTO modules (name) VALUES ('$name')");
Dries's avatar
   
Dries committed
86
87
    }

Dries's avatar
   
Dries committed
88
89
    // rehash module-exported blocks (if necessary):
    module_rehash_blocks($name);
Dries's avatar
   
Dries committed
90
91
92
93
94
95
96
  }
  else {
    // remove all reference to module:
    db_query("DELETE FROM modules WHERE name = '$name'");
    db_query("DELETE FROM blocks WHERE module = '$name'");
  }
}
Dries's avatar
   
Dries committed
97

Dries's avatar
   
Dries committed
98
?>