1<?php
2/**
3 * Load all internal libraries and setup class autoloader
4 *
5 * @author Andreas Gohr <andi@splitbrain.org>
6 */
7
8use dokuwiki\Extension\PluginController;
9
10// setup class autoloader
11spl_autoload_register('load_autoload');
12
13// require all the common libraries
14// for a few of these order does matter
15require_once(DOKU_INC.'inc/defines.php');
16require_once(DOKU_INC.'inc/actions.php');
17require_once(DOKU_INC.'inc/changelog.php');
18require_once(DOKU_INC.'inc/common.php');
19require_once(DOKU_INC.'inc/confutils.php');
20require_once(DOKU_INC.'inc/pluginutils.php');
21require_once(DOKU_INC.'inc/form.php');
22require_once(DOKU_INC.'inc/fulltext.php');
23require_once(DOKU_INC.'inc/html.php');
24require_once(DOKU_INC.'inc/httputils.php');
25require_once(DOKU_INC.'inc/indexer.php');
26require_once(DOKU_INC.'inc/infoutils.php');
27require_once(DOKU_INC.'inc/io.php');
28require_once(DOKU_INC.'inc/mail.php');
29require_once(DOKU_INC.'inc/media.php');
30require_once(DOKU_INC.'inc/pageutils.php');
31require_once(DOKU_INC.'inc/parserutils.php');
32require_once(DOKU_INC.'inc/search.php');
33require_once(DOKU_INC.'inc/template.php');
34require_once(DOKU_INC.'inc/toolbar.php');
35require_once(DOKU_INC.'inc/utf8.php');
36require_once(DOKU_INC.'inc/auth.php');
37require_once(DOKU_INC.'inc/compatibility.php');
38require_once(DOKU_INC.'inc/deprecated.php');
39require_once(DOKU_INC.'inc/legacy.php');
40
41/**
42 * spl_autoload_register callback
43 *
44 * Contains a static list of DokuWiki's core classes and automatically
45 * require()s their associated php files when an object is instantiated.
46 *
47 * @author Andreas Gohr <andi@splitbrain.org>
48 * @todo   add generic loading of renderers and auth backends
49 *
50 * @param string $name
51 *
52 * @return bool
53 */
54function load_autoload($name){
55    static $classes = null;
56    if($classes === null) $classes = array(
57        'Diff'                  => DOKU_INC.'inc/DifferenceEngine.php',
58        'UnifiedDiffFormatter'  => DOKU_INC.'inc/DifferenceEngine.php',
59        'TableDiffFormatter'    => DOKU_INC.'inc/DifferenceEngine.php',
60        'cache'                 => DOKU_INC.'inc/cache.php',
61        'cache_parser'          => DOKU_INC.'inc/cache.php',
62        'cache_instructions'    => DOKU_INC.'inc/cache.php',
63        'cache_renderer'        => DOKU_INC.'inc/cache.php',
64        'Input'                 => DOKU_INC.'inc/Input.class.php',
65        'JpegMeta'              => DOKU_INC.'inc/JpegMeta.php',
66        'SimplePie'             => DOKU_INC.'inc/SimplePie.php',
67        'FeedParser'            => DOKU_INC.'inc/FeedParser.php',
68        'SafeFN'                => DOKU_INC.'inc/SafeFN.class.php',
69        'Sitemapper'            => DOKU_INC.'inc/Sitemapper.php',
70        'Mailer'                => DOKU_INC.'inc/Mailer.class.php',
71
72        'Doku_Handler'          => DOKU_INC.'inc/parser/handler.php',
73        'Doku_Renderer'          => DOKU_INC.'inc/parser/renderer.php',
74        'Doku_Renderer_xhtml'    => DOKU_INC.'inc/parser/xhtml.php',
75        'Doku_Renderer_code'     => DOKU_INC.'inc/parser/code.php',
76        'Doku_Renderer_xhtmlsummary' => DOKU_INC.'inc/parser/xhtmlsummary.php',
77        'Doku_Renderer_metadata' => DOKU_INC.'inc/parser/metadata.php',
78    );
79
80    if(isset($classes[$name])){
81        require ($classes[$name]);
82        return true;
83    }
84
85    // namespace to directory conversion
86    $name = str_replace('\\', '/', $name);
87
88    // test mock namespace
89    if (substr($name, 0, 19) === 'dokuwiki/test/mock/') {
90        $file = DOKU_INC . '_test/mock/' . substr($name, 19) . '.php';
91        if (file_exists($file)) {
92            require $file;
93            return true;
94        }
95    }
96
97    // tests namespace
98    if (substr($name, 0, 14) === 'dokuwiki/test/') {
99        $file = DOKU_INC . '_test/tests/' . substr($name, 14) . '.php';
100        if (file_exists($file)) {
101            require $file;
102            return true;
103        }
104    }
105
106    // plugin namespace
107    if(substr($name, 0, 16) === 'dokuwiki/plugin/') {
108        $name = str_replace('/test/', '/_test/', $name); // no underscore in test namespace
109        $file = DOKU_PLUGIN . substr($name, 16) . '.php';
110        if(file_exists($file)) {
111            try {
112                require $file;
113            } catch (\Throwable $e) {
114                \dokuwiki\ErrorHandler::showExceptionMsg($e, "Error loading plugin $name");
115            }
116            return true;
117        }
118    }
119
120    // template namespace
121    if(substr($name, 0, 18) === 'dokuwiki/template/') {
122        $name = str_replace('/test/', '/_test/', $name); // no underscore in test namespace
123        $file = DOKU_INC.'lib/tpl/' . substr($name, 18) . '.php';
124        if(file_exists($file)) {
125            try {
126                require $file;
127            } catch (\Throwable $e) {
128                \dokuwiki\ErrorHandler::showExceptionMsg($e, "Error loading template $name");
129            }
130            return true;
131        }
132    }
133
134    // our own namespace
135    if(substr($name, 0, 9) === 'dokuwiki/') {
136        $file = DOKU_INC . 'inc/' . substr($name, 9) . '.php';
137        if(file_exists($file)) {
138            require $file;
139            return true;
140        }
141    }
142
143    // Plugin loading
144    if(preg_match(
145        '/^(' . implode('|', PluginController::PLUGIN_TYPES) . ')_plugin_(' .
146        DOKU_PLUGIN_NAME_REGEX .
147        ')(?:_([^_]+))?$/',
148        $name,
149        $m
150    )) {
151        // try to load the wanted plugin file
152        $c = ((count($m) === 4) ? "/{$m[3]}" : '');
153        $plg = DOKU_PLUGIN . "{$m[2]}/{$m[1]}$c.php";
154        if(file_exists($plg)){
155            try {
156                require $plg;
157            } catch (\Throwable $e) {
158                \dokuwiki\ErrorHandler::showExceptionMsg($e, "Error loading plugin {$m[2]}");
159            }
160        }
161        return true;
162    }
163    return false;
164}
165
166