Extension notes and verbose SQL table rewritten to templates. Resolved #13
[mailer.git] / inc / extensions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 03/25/2004 *
4  * ===============                              Last change: 09/29/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : extensions.php                                   *
8  * -------------------------------------------------------------------- *
9  * Short description : Extension management                             *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Erweiterungen-Management                         *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Load the extension and maybe found language and function files.
41 function LOAD_EXTENSION ($ext_name, $EXT_LOAD_MODE = "", $EXT_VER = "", $dry_run = false, &$SQLs = array()) {
42         global $EXT_LOADED, $_CONFIG, $CSS, $cacheMode, $EXT_VER_HISTORY;
43         global $INC_POOL, $EXT_UPDATE_DEPENDS, $EXT_DEPRECATED, $UPDATE_NOTES;
44         global $EXT_VERSION, $EXT_ALWAYS_ACTIVE;
45
46         // Init array
47         $INC_POOL = array();
48
49         // Is the extension already loaded?
50         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "Loading extension {$ext_name}, mode={$EXT_LOAD_MODE}, ver={$EXT_VER}.");
51         if ((isset($EXT_LOADED['ext'][$ext_name])) && (empty($EXT_LOAD_MODE))) {
52                 // Debug message
53                 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Extension %s already loaded.", $ext_name));
54                 return false;
55         } // END - if
56
57         // Construct FQFN for extension file
58         $extInclude = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
59
60         // Is the extension file NOT there?
61         if (!FILE_READABLE($extInclude)) {
62                 // Debug message
63                 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Extension %s not found.", $ext_name));
64
65                 // Abort here
66                 return false;
67         } // END - if
68
69         // Construct FQFN for language file
70         $langInclude = sprintf("%sinc/language/%s_%s.php", PATH, $ext_name, GET_LANGUAGE());
71
72         // Is this include there?
73         if ((FILE_READABLE($langInclude)) && (!isset($EXT_LOADED['lang'][$ext_name]))) {
74                 // Then load it
75                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "--- Language loaded.");
76                 $EXT_LOADED['lang'][$ext_name] = true;
77                 require($langInclude);
78         } // END - if
79
80         // Construct FQFN for functions file
81         $funcsInclude = sprintf("%sinc/libs/%s_functions.php", PATH, $ext_name);
82
83         // Is this include there?
84         if ((FILE_READABLE($funcsInclude)) && (!isset($EXT_LOADED['funcs'][$ext_name]))) {
85                 // Then load it
86                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "--- Functions loaded.");
87                 $EXT_LOADED['funcs'][$ext_name] = true;
88                 require($funcsInclude);
89         } // END - if
90
91         // Extensions are not deprecated by default
92         $EXT_DEPRECATED = "N";
93
94         // Extensions are not always active by default
95         $EXT_ALWAYS_ACTIVE = "N";
96
97         // By default an extension update does not depend on other extensions
98         $EXT_UPDATE_DEPENDS = "";
99
100         // Extension update notes
101         $UPDATE_NOTES = "";
102
103         // Include the extension file
104         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "Extension loaded.");
105         require($extInclude);
106
107         // Is this extension deprecated?
108         if ($EXT_DEPRECATED == "Y") {
109                 // Deactivate the extension
110                 DEACTIVATE_EXTENSION($ext_name);
111
112                 // Abort here
113                 return false;
114         } // END - if
115
116         // Mark it as loaded in normal mode
117         if (empty($EXT_LOAD_MODE)) {
118                 // Mark it now...
119                 $EXT_LOADED['ext'][$ext_name] = true;
120         } // END - if
121
122         // All fine!
123         return true;
124 }
125
126 // Registeres an extension and possible update depencies
127 function EXTENSION_REGISTER ($ext_name, $task_id, $dry_run = false, $logout = true) {
128         global $UPDATE_NOTES, $INC_POOL, $cacheInstance;
129         global $EXT_VER_HISTORY, $NOTES, $EXT_ALWAYS_ACTIVE, $EXT_VERSION;
130         global $EXT_UPDATE_DEPENDS;
131
132         // This shall never do a non-admin user!
133         if (!IS_ADMIN()) return false;
134
135         // Is this extension already installed?
136         if (EXT_IS_ACTIVE($ext_name)) return false;
137
138         // Init variables
139         $ret = false; $SQLs = array();
140         $NOTES = "";
141         $INC_POOL = array();
142
143         // By default we have no failtures
144         $EXT_REPORTS_FAILURE = false;
145
146         // Does this extension exists?
147         if (LOAD_EXTENSION($ext_name, "register", "", $dry_run, $SQLs)) {
148                 // And run possible updates
149                 $history = $EXT_VER_HISTORY;
150                 foreach ($history as $ver) {
151                         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "ext_name={$ext_name}, ext_ver={$ver}");
152                         // Load extension in update mode
153                         LOAD_EXTENSION($ext_name, "update", $ver, $dry_run, $SQLs);
154
155                         // Add update notes to our output
156                         $NOTES .= ADD_EXTENSION_NOTES($ver, $UPDATE_NOTES);
157                 } // END - foreach
158
159                 // Does this extension depends on an outstanding update of another update?
160                 if (!empty($EXT_UPDATE_DEPENDS)) {
161                         // Backup SQL commands and clear current
162                         $SQLs2 = $SQLs;
163                         $SQLs  = array();
164                         $test  = false;
165                         $ext_update = $EXT_UPDATE_DEPENDS;
166
167                         // Check for required file
168                         if (LOAD_EXTENSION($ext_update, "register", "", $dry_run, $SQLs)) {
169                                 // If versions mismatch update extension first
170                                 $ext_ver = GET_EXT_VERSION($ext_update);
171
172                                 // Extension version set? If empty the extension is not registered
173                                 if (empty($ext_ver)) {
174                                         // Extension not registered so far so first load task's ID...
175                                         $task = DETERMINE_EXTENSION_TASK_ID($ext_update);
176
177                                         // Entry found?
178                                         if ($task > 0) {
179                                                 // Try to register the extension
180                                                 $test = EXTENSION_REGISTER($ext_update, $task, $dry_run, false);
181                                         } // END - if
182                                 } elseif ($ext_ver != $EXT_VERSION) {
183                                         // Ok, update this extension now
184                                         EXTENSION_UPDATE($ext_update, $ext_ver, $dry_run);
185
186                                         // All okay!
187                                         $test = true;
188                                 } else {
189                                         // Nothing to register / update before...
190                                         $test = true;
191                                 }
192                         } else {
193                                 // Required file for update does not exists!
194                                 $test = true;
195                                 // But this is fine for the first time...
196                         }
197
198                         // Finally restore previous SQLs
199                         $SQLs = $SQLs2; unset($SQLs2);
200                 } else {
201                         // Does not depend on an other extension
202                         $test = true;
203                 }
204
205                 // Switch back to register mode
206                 $EXT_LOAD_MODE = "register";
207
208                 // Remains true if extension registration reports no failtures
209                 $test = ($test && !$EXT_REPORTS_FAILURE);
210
211                 // Does everthing before wents ok?
212                 if ($test) {
213                         // "Dry-run-mode" activated?
214                         if (!$dry_run) {
215                                 // Run installation pre-installation filters
216                                 RUN_FILTER('pre_extension_installed', array('dry_run' => $dry_run, 'sqls' => $SQLs));
217
218                                 // Register extension
219                                 SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_extensions (ext_name, ext_active, ext_version) VALUES ('%s','%s','%s')",
220                                         array($ext_name, $EXT_ALWAYS_ACTIVE, $EXT_VERSION), __FILE__, __LINE__);
221
222                                 // Remove cache file(s) if extension is active
223                                 RUN_FILTER('post_extension_installed', array('ext_name' => $ext_name, 'task_id' => $task_id, 'inc_pool' => $INC_POOL));
224
225                                 // In normal mode return a true on success
226                                 $ret = true;
227                         } else {
228                                 // Rewrite SQL command to keep { and } inside
229                                 foreach ($SQLs as $key => $sql) {
230                                         $sql = str_replace('{', "&#123;", str_replace('}', "&#125;", $sql));
231                                         $SQLs[$key] = $sql;
232                                 } // END - foreach
233
234                                 // In  "dry-run" mode return array with all SQL commands
235                                 $ret = $SQLs;
236
237                                 // Remove all SQL commands
238                                 unset($SQLs);
239                         }
240                 } else {
241                         // No, an error occurs while registering extension :-(
242                         $ret = false;
243                 }
244         } elseif (($task_id > 0) && (!empty($ext_name))) {
245                 // Remove task from system when id and extension's name is valid
246                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE id=%s AND status='NEW' LIMIT 1",
247                         array(bigintval($task_id)), __FILE__, __LINE__);
248         }
249
250         // Is this the sql_patches?
251         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":{$ext_name}/{$EXT_LOAD_MODE}");
252         if (($ext_name == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove")) && (!$dry_run) && ($test)) {
253                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
254                 if ($logout) {
255                         // Then redirect to logout
256                         LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
257                 } else {
258                         // Add temporary filter
259                         REGISTER_FILTER('shutdown', 'REDIRECT_TO_LOGOUT_SQL_PATCHES', true, true);
260                         $GLOBALS['ext_load_mode'] = $EXT_LOAD_MODE;
261                 }
262         } // END - if
263
264         // Return status code
265         return $ret;
266 }
267
268 // Run SQL queries for given extension id
269 // @TODO Change from ext_id to ext_name (not just even the variable! ;-) )
270 function EXTENSION_RUN_SQLS ($ext_id, $load_mode) {
271         global $cacheInstance;
272
273         // This shall never do a non-admin user!
274         if (!IS_ADMIN()) return false;
275
276         // Init array
277         $SQLs = array();
278
279         // By default no SQL has been executed
280         $sqlRan = false;
281
282         // Get extension's name
283         $ext_name = GET_EXT_NAME($ext_id);
284         if (empty($ext_name)) return false;
285
286         // Load extension in detected mode
287         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":ext_name[{$ext_id}]={$ext_name}");
288         LOAD_EXTENSION($ext_name, $load_mode, "", false, $SQLs);
289
290         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":SQLs::count=".count($SQLs)."");
291         if ((is_array($SQLs) && (sizeof($SQLs) > 0))) {
292                 // Run SQL commands...
293                 RUN_FILTER('run_sqls', array('dry_run' => false, 'sqls' => $SQLs));
294
295                 // Removal mode?
296                 if ($load_mode == "remove") {
297                         // Delete this extension (remember to remove it from your server *before* you click on welcome!
298                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
299                                 array($ext_name), __FILE__, __LINE__);
300                 } // END - if
301         } // END - if
302
303         // Remove cache file(s) if extension is active
304         if (((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) && (((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == "activate") || ($load_mode == "deactivate"))) {
305                 // Run filters
306                 RUN_FILTER('post_extension_run_sql', $ext_name);
307         } // END - if
308
309         // Is this the sql_patches?
310         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": {$ext_id}/{$ext_name}/{$load_mode}");
311         if (($ext_name == "sql_patches") && (($load_mode == "register") || ($load_mode == "remove"))) {
312                 // Then redirect to logout
313                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
314                 LOAD_URL("modules.php?module=admin&logout=1&".$load_mode."=sql_patches");
315         } // END - if
316 }
317
318 // Check if given extension is active
319 function EXT_IS_ACTIVE ($ext_name) {
320         global $cacheArray;
321
322         // Extensions are all inactive during installation
323         if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing')) || (empty($ext_name))) return false;
324
325         // Not active is the default
326         $active = "N";
327
328         // Check cache
329         if (isset($cacheArray['extensions']['ext_active'][$ext_name])) {
330                 // Load from cache
331                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
332                 $active = $cacheArray['extensions']['ext_active'][$ext_name];
333
334                 // Count cache hits
335                 incrementConfigEntry('cache_hits');
336         } elseif (($ext_name == "cache") || (GET_EXT_VERSION("cache") == "")) {
337                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
338                 // Load from database
339                 $result = SQL_QUERY_ESC("SELECT ext_active FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
340                         array($ext_name), __FILE__, __LINE__);
341
342                 // Entry found?
343                 if (SQL_NUMROWS($result) == 1) {
344                         // Load entry
345                         list($active) = SQL_FETCHROW($result);
346                 } // END - if
347
348                 // Free result
349                 SQL_FREERESULT($result);
350
351                 // Write cache array
352                 //* DEBUG: */ echo $ext_name."[DB]: {$active}");
353                 $cacheArray['extensions']['ext_active'][$ext_name] = $active;
354         } else {
355                 // Extension not active!
356                 //* DEBUG: */ echo $ext_name.": Not active!");
357                 $cacheArray['extensions']['ext_active'][$ext_name] = "N";
358         }
359
360         // Debug message
361         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, " ext_name={$ext_name},active={$active}");
362
363         // Is this extension activated? (For admins we always have active extensions...)
364         return ($active == "Y");
365 }
366 // Get version from extensions
367 function GET_EXT_VERSION ($ext_name) {
368         global $cacheArray, $cacheInstance;
369         $ext_ver = false;
370
371         // Extensions are all inactive during installation
372         if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return "";
373         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ext_name={$ext_name}");
374
375         // Is the cache written?
376         if (isset($cacheArray['extensions']['ext_version'][$ext_name])) {
377                 // Load data from cache
378                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": CACHE!");
379                 $ext_ver = $cacheArray['extensions']['ext_version'][$ext_name];
380
381                 // Count cache hits
382                 incrementConfigEntry('cache_hits');
383         } elseif (!is_object($cacheInstance)) {
384                 // Load from database
385                 $result = SQL_QUERY_ESC("SELECT ext_version FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
386                         array($ext_name), __FILE__, __LINE__);
387
388                 // Is the extension there?
389                 if (SQL_NUMROWS($result) == 1) {
390                         // Load entry
391                         list($ext_ver) = SQL_FETCHROW($result);
392                 } // END - if
393
394                 // Free result
395                 SQL_FREERESULT($result);
396
397                 // Set cache
398                 $cacheArray['extensions']['ext_version'][$ext_name] = $ext_ver;
399         }
400
401         // Return result
402         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ret={$ext_ver}");
403         return $ext_ver;
404 }
405
406 // Updates a given extension with current extension version to latest version
407 function EXTENSION_UPDATE ($ext_name, $ext_ver, $dry_run = false) {
408         // This shall never do a non-admin user!
409         global $cacheInstance, $UPDATE_NOTES, $NOTES, $EXT_VER_HISTORY;
410         global $EXT_UPDATE_DEPENDS, $EXT_VERSION, $INC_POOL, $cacheArray, $SQLs;
411
412         // Init arrays
413         $SQLs = array(); $INC_POOL = array();
414
415         // Init notes
416         $NOTES = "";
417
418         // Only admins are allowed to update extensions
419         if ((!IS_ADMIN()) || (empty($ext_name))) return false;
420
421         // Load extension in test mode
422         LOAD_EXTENSION($ext_name, "test", $ext_ver, $dry_run, $SQLs);
423
424         // Save version history
425         $history = $EXT_VER_HISTORY;
426
427         // Remove old SQLs array to prevent possible bugs
428         $SQLs = array();
429
430         // Check if version is updated
431         if ((($EXT_VERSION != $ext_ver) || ($dry_run)) && (is_array($history))) {
432                 // Search for starting point
433                 $start = array_search($ext_ver, $history);
434
435                 // And load SQL queries in order of version history
436                 for ($idx = ($start + 1); $idx < sizeof($history); $idx++) {
437                         // Set extension version
438                         $cacheArray['update_ver'][$ext_name] = $history[$idx];
439
440                         // Load again...
441                         LOAD_EXTENSION($ext_name, "update", $cacheArray['update_ver'][$ext_name], $dry_run, $SQLs);
442
443                         if (!empty($EXT_UPDATE_DEPENDS)) {
444                                 // Backup current SQL queries
445                                 $cacheArray['update_sqls'][$ext_name] = $SQLs;
446
447                                 // Is the extension there?
448                                 if (GET_EXT_VERSION($EXT_UPDATE_DEPENDS) != "") {
449                                         // Update another extension first!
450                                         $test = EXTENSION_UPDATE($EXT_UPDATE_DEPENDS, GET_EXT_VERSION($EXT_UPDATE_DEPENDS), $dry_run);
451                                 } else {
452                                         // Register new extension
453                                         $test = EXTENSION_REGISTER($EXT_UPDATE_DEPENDS, 0, $dry_run, false);
454                                 }
455
456                                 // Restore previous SQL queries
457                                 $SQLs = $cacheArray['update_sqls'][$ext_name];
458                                 unset($cacheArray['update_sqls'][$ext_name]);
459                         } // END - if
460
461                         // Add notes
462                         $NOTES .= ADD_EXTENSION_NOTES($cacheArray['update_ver'][$ext_name], $UPDATE_NOTES);
463                 } // END - for
464
465                 // In real-mode execute any existing includes
466                 if (!$dry_run) {
467                         $cacheArray['inc_pool'][$ext_name] = $INC_POOL;
468                         RUN_FILTER('load_includes', $INC_POOL);
469                         $INC_POOL = $cacheArray['inc_pool'][$ext_name];
470                         unset($cacheArray['inc_pool'][$ext_name]);
471                 } // END - if
472
473                 // Run SQLs
474                 RUN_FILTER('run_sqls', array('dry_run' => $dry_run, 'sqls' => $SQLs));
475
476                 if (!$dry_run) {
477                         // Create task
478                         CREATE_EXTENSION_UPDATE_TASK(GET_CURRENT_ADMIN_ID(), $ext_name, $cacheArray['update_ver'][$ext_name], addslashes($NOTES));
479
480                         // Update extension's version
481                         SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
482                                 array($cacheArray['update_ver'][$ext_name], $ext_name), __FILE__, __LINE__);
483
484                         // Remove arrays
485                         unset($SQLs);
486                         unset($cacheArray['update_ver'][$ext_name]);
487
488                         // Run filters on success extension update
489                         RUN_FILTER('extension_update', $ext_name);
490                 } else {
491                         // In "dry-run" mode return array with SQL commands
492                         return $SQLs;
493                 }
494         } // END - if
495 }
496
497 // Output verbose SQL table for extension
498 function EXTENSION_VERBOSE_TABLE ($queries = array(), $title = ADMIN_SQLS_EXECUTED_ON_REMOVAL, $dashed = "", $switch = false, $width = "100%") {
499         global $SQLs;
500
501         // Are there some queries in $queries?
502         if (count($queries) > 0) {
503                 // Then use them instead!
504                 $SQLs = $queries;
505         } // END - if
506
507         // Init variables
508         $SW = 2; $i = 1;
509         $OUT = "";
510
511         // Do we have queries?
512         if ((is_array($SQLs)) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
513                 foreach ($SQLs as $idx => $sql) {
514                         // Trim out spaces
515                         $sql = trim($sql);
516
517                         // Output command if set
518                         if (!empty($sql)) {
519                                 // Prepare output for template
520                                 $content = array(
521                                         'sw'  => $SW,
522                                         'i'   => $i,
523                                         'sql' => $sql
524                                 );
525
526                                 // Load row template
527                                 $OUT .= LOAD_TEMPLATE("admin_ext_sql_row", true, $content);
528
529                                 // Switch color and count up
530                                 $SW = 3 - $SW;
531                                 $i++;
532                         } // END - if
533                 } // END - foreach
534
535                 // Prepare content for template
536                 $content = array(
537                         'width'  => $width,
538                         'dashed' => $dashed,
539                         'title'  => $title,
540                         'out'    => $OUT
541                 );
542
543                 // Load main template
544                 $OUT = LOAD_TEMPLATE("admin_ext_sql_table", true, $content);
545         } elseif ((GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
546                 // No addional SQL commands to run
547                 $OUT = LOAD_TEMPLATE("admin_settings_saved", true, ADMIN_NO_ADDITIONAL_SQLS);
548         } // END - if
549
550         // Return output
551         return $OUT;
552 }
553
554 // Get extension name from id
555 function GET_EXT_NAME ($ext_id) {
556         global $cacheArray;
557
558         // Init extension name
559         $ret = "";
560
561         // Is cache there?
562         if (isset($cacheArray['extensions']['ext_name'][$ext_id])) {
563                 // Load from cache
564                 $ret = $cacheArray['extensions']['ext_name'][$ext_id];
565
566                 // Count cache hits
567                 incrementConfigEntry('cache_hits');
568         } elseif (!EXT_IS_ACTIVE("cache")) {
569                 // Load from database
570                 $result = SQL_QUERY_ESC("SELECT ext_name FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
571                         array(bigintval($ext_id)), __FILE__, __LINE__);
572                 list($ret) = SQL_FETCHROW($result);
573                 SQL_FREERESULT($result);
574         }
575         return $ret;
576 }
577
578 // Get extension id from name
579 function GET_EXT_ID ($ext_name) {
580         global $cacheArray;
581
582         // Init ID number
583         $ret = 0;
584         if (isset($cacheArray['extensions']['ext_id'][$ext_name])) {
585                 // Load from cache
586                 $ret = $cacheArray['extensions']['ext_id'][$ext_name];
587
588                 // Count cache hits
589                 incrementConfigEntry('cache_hits');
590         } elseif (!EXT_IS_ACTIVE("cache")) {
591                 // Load from database
592                 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
593                         array($ext_name), __FILE__, __LINE__);
594                 list($ret) = SQL_FETCHROW($result);
595                 SQL_FREERESULT($result);
596         }
597
598         // Return value
599         return $ret;
600 }
601
602 // Activate given extension
603 function ACTIVATE_EXTENSION ($ext_name) {
604         // Activate the extension
605         SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='Y' WHERE ext_name='%s' LIMIT 1",
606                 array($ext_name), __FILE__, __LINE__);
607
608         // Extension has been activated?
609         if (SQL_AFFECTEDROWS() == 1) {
610                 // Then run all queries
611                 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "activate");
612         } // END - if
613 }
614
615 // Deactivate given extension
616 function DEACTIVATE_EXTENSION($ext_name) {
617         // Activate the extension
618         SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='N' WHERE ext_name='%s' LIMIT 1",
619                 array($ext_name), __FILE__, __LINE__);
620
621         // Extension has been activated?
622         if (SQL_AFFECTEDROWS() == 1) {
623                 // Then run all queries
624                 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "deactivate");
625
626                 // Create new task
627                 CREATE_EXTENSION_DEACTIVATION_TASK($ext_name);
628
629                 // Notify the admin
630                 SEND_ADMIN_NOTIFICATION(ADMIN_SUBJECT_EXTENSION_DEACTIVATED, "admin_ext_deactivated", array('ext_name' => $ext_name));
631         } // END - if
632 }
633
634 // Checks wether the extension is older than given
635 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
636         // Get current extension version
637         $currVersion = GET_EXT_VERSION($ext_name);
638
639         // Remove all dots from both versions
640         $currVersion = str_replace(".", "", $currVersion);
641         $ext_ver = str_replace(".", "", $ext_ver);
642
643         // Now compare both and return the result
644         return ($currVersion < $ext_ver);
645 }
646
647 // Creates a new task for updated extension
648 function CREATE_EXTENSION_UPDATE_TASK ($admin_id, $ext_name, $ext_ver, $notes) {
649         // Create subject line
650         $subject = "[UPDATE-".$ext_name."-".$ext_ver.":] ".ADMIN_UPDATE_EXT_SUBJ;
651
652         // Is the extension there?
653         if (GET_EXT_VERSION($ext_name) != "") {
654                 // Check if task is not there
655                 if (DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) {
656                         // Task not created so it's a brand-new extension which we need to register and create a task for!
657                         SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, userid, status, task_type, subject, text, task_created) VALUES ('%s','0','NEW','EXTENSION_UPDATE','%s','%s', UNIX_TIMESTAMP())",
658                                 array($admin_id, $subject, $notes), __FILE__, __LINE__);
659                 } // END - if
660         } // END - if
661 }
662
663 // Creates a new task for newly installed extension
664 function CREATE_NEW_EXTENSION_TASK ($admin_id, $subject, $ext) {
665         // Not installed and do we have created a task for the admin?
666         if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) == "")) {
667                 // Template file
668                 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
669                         PATH,
670                         GET_LANGUAGE(),
671                         $ext
672                 );
673
674                 // Load text for task
675                 if (FILE_READABLE($tpl)) {
676                         // Load extension's own text template (HTML!)
677                         $msg = LOAD_TEMPLATE("ext_".$ext, true);
678                 } else {
679                         // Load default message
680                         $msg = LOAD_TEMPLATE("admin_new_ext", "", 0);
681                 }
682
683                 // Task not created so it's a brand-new extension which we need to register and create a task for!
684                 SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, userid, status, task_type, subject, text, task_created)
685 VALUES (%s,0,'NEW','EXTENSION','%s','%s',UNIX_TIMESTAMP())",
686                         array(
687                                 $admin_id,
688                                 $subject,
689                                 addslashes($msg),
690                         ),  __FILE__, __LINE__, true, false
691                 );
692         } // END - if
693 }
694
695 // Creates a task for automatically deactivated (deprecated) extension
696 function CREATE_EXTENSION_DEACTIVATION_TASK ($ext) {
697         // Create subject line
698         $subject = sprintf("[%s:] %s", $ext, TASK_SUBJ_EXTENSION_DEACTIVATED);
699
700         // Not installed and do we have created a task for the admin?
701         if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) != "")) {
702                 // Task not created so add it
703                 SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, userid, status, task_type, subject, text, task_created)
704 VALUES (0,0,'NEW','EXTENSION_DEACTIVATION','%s','%s',UNIX_TIMESTAMP())",
705                         array(
706                                 $subject,
707                                 addslashes(LOAD_TEMPLATE("task_ext_deactivated", true, $ext)),
708                         ),  __FILE__, __LINE__, true, false
709                 );
710         } // END - if
711 }
712
713 // Checks if the module has a menu
714 function MODULE_HAS_MENU ($mod, $forceDb = false) {
715         global $cacheArray;
716
717         // All is false by default
718         $ret = false;
719         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):mod={$mod},cache=".GET_EXT_VERSION("cache")."<br />\n";
720         if (GET_EXT_VERSION("cache") >= "0.1.2") {
721                 // Cache version is okay, so let's check the cache!
722                 if (isset($cacheArray['modules']['has_menu'][$mod])) {
723                         // Check module cache and count hit
724                         $ret = ($cacheArray['modules']['has_menu'][$mod] == "Y");
725                         incrementConfigEntry('cache_hits');
726                 } elseif (isset($cacheArray['extensions']['ext_menu'][$mod])) {
727                         // Check cache and count hit
728                         $ret = ($cacheArray['extensions']['ext_menu'][$mod] == "Y");
729                         incrementConfigEntry('cache_hits');
730                 } elseif ((IS_ADMIN()) && ($mod == "admin")) {
731                         // Admin module has always a menu!
732                         $ret = true;
733                 }
734         } elseif ((GET_EXT_VERSION("sql_patches") >= "0.3.6") && ((!EXT_IS_ACTIVE("cache")) || ($forceDb === true))) {
735                 // Check database for entry
736                 $result = SQL_QUERY_ESC("SELECT has_menu FROM "._MYSQL_PREFIX."_mod_reg WHERE module='%s' LIMIT 1",
737                         array($mod), __FILE__, __LINE__);
738
739                 // Entry found?
740                 if (SQL_NUMROWS($result) == 1) {
741                         // Load "has_menu" column
742                         list($has_menu) = SQL_FETCHROW($result);
743
744                         // Fake cache... ;-)
745                         $cacheArray['extensions']['ext_menu'][$mod] = $has_menu;
746
747                         // Does it have a menu?
748                         $ret = ($has_menu == "Y");
749                 } // END  - if
750
751                 // Free memory
752                 SQL_FREERESULT($result);
753         } elseif (GET_EXT_VERSION("sql_patches") == "") {
754                 // No sql_patches installed, so maybe in admin area?
755                 $ret = ((IS_ADMIN()) && ($mod == "admin")); // Then there is a menu!
756         }
757
758         // Return status
759         //* DEBUG: */ var_dump($ret);
760         return $ret;
761 }
762
763 // Determines the task id for given extension
764 function DETERMINE_EXTENSION_TASK_ID ($ext_name) {
765         // Default is not found
766         $task_id = 0;
767
768         // Search for extension task's id
769         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE task_type='EXTENSION' AND subject='[%s:]' LIMIT 1",
770                 array($ext_name), __FILE__, __LINE__);
771
772         // Entry found?
773         if (SQL_NUMROWS($result) == 1) {
774                 // Task found so load task's ID and register extension...
775                 list($task_id) = SQL_FETCHROW($result);
776         } // END - if
777
778         // Free result
779         SQL_FREERESULT($result);
780
781         // Return it
782         return $task_id;
783 }
784
785 // Determines the task id for given subject
786 function DETERMINE_TASK_ID_BY_SUBJECT ($subject) {
787         // Default is not found
788         $task_id = 0;
789
790         // Search for task id
791         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE subject LIKE '%s%%' LIMIT 1",
792                 array($subject), __FILE__, __LINE__);
793
794         // Entry found?
795         if (SQL_NUMROWS($result) == 1) {
796                 // Task found so load task's ID and register extension...
797                 list($task_id) = SQL_FETCHROW($result);
798         } // END - if
799
800         // Free result
801         SQL_FREERESULT($result);
802
803         // Return it
804         return $task_id;
805 }
806
807 // Add updates notes for given version
808 function ADD_EXTENSION_NOTES ($ver, &$UPDATE_NOTES) {
809         // Init notes/content
810         $out = ""; $content = array();
811
812         // Is do we have verbose output enabled?
813         if ((getConfig('verbose_sql') == "Y") || (!EXT_IS_ACTIVE("sql_patches"))) {
814
815                 // Update notes found?
816                 if (!empty($UPDATE_NOTES)) {
817                         // Update notes found
818                         $content = array(
819                                 'ver'   => $ver,
820                                 'notes' => $UPDATE_NOTES
821                         );
822                         $UPDATE_NOTES = "";
823                 } elseif (($ver == "0.0") || ($ver == "0.0.0")) {
824                         // Initial release
825                         $content = array(
826                                 'ver'   => $ver,
827                                 'notes' => INITIAL_RELEASE
828                         );
829                 } else {
830                         // No update notes found!
831                         $content = array(
832                                 'ver'   => $ver,
833                                 'notes' => NO_UPDATE_NOTES
834                         );
835                 }
836
837                 // Load template
838                 $out = LOAD_TEMPLATE("admin_ext_notes", true, $content);
839         } // END - if
840
841         // Return the output
842         return $out;
843 }
844
845 //
846 ?>