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