07fd2858b0eba2875b4bc0b4b7df4efbdd0d3dfe
[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("inc/extensions/ext-%s.php", $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("inc/language/%s_%s.php", $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                 LOAD_INC_ONCE($langInclude);
78         } // END - if
79
80         // Construct FQFN for functions file
81         $funcsInclude = sprintf("inc/libs/%s_functions.php", $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                 LOAD_INC_ONCE($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         LOAD_INC($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 failures
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 failures
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 it is not set then maybe there is no extension for that ID number
285         if (empty($ext_name)) return false;
286
287         // Load extension in detected mode
288         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":ext_name[{$ext_id}]={$ext_name}");
289         LOAD_EXTENSION($ext_name, $load_mode, "", false, $SQLs);
290
291         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ":SQLs::count=".count($SQLs)."");
292         if ((is_array($SQLs) && (sizeof($SQLs) > 0))) {
293                 // Run SQL commands...
294                 RUN_FILTER('run_sqls', array('dry_run' => false, 'sqls' => $SQLs));
295
296                 // Removal mode?
297                 if ($load_mode == "remove") {
298                         // Delete this extension (remember to remove it from your server *before* you click on welcome!
299                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
300                                 array($ext_name), __FILE__, __LINE__);
301                 } // END - if
302         } // END - if
303
304         // Remove cache file(s) if extension is active
305         if (((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) && (((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($load_mode == "activate") || ($load_mode == "deactivate"))) {
306                 // Run filters
307                 RUN_FILTER('post_extension_run_sql', $ext_name);
308         } // END - if
309
310         // Is this the sql_patches?
311         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": {$ext_id}/{$ext_name}/{$load_mode}");
312         if (($ext_name == "sql_patches") && (($load_mode == "register") || ($load_mode == "remove"))) {
313                 // Then redirect to logout
314                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": LOAD!");
315                 LOAD_URL("modules.php?module=admin&logout=1&".$load_mode."=sql_patches");
316         } // END - if
317 }
318
319 // Check if given extension is active
320 function EXT_IS_ACTIVE ($ext_name) {
321         global $cacheArray;
322
323         // Extensions are all inactive during installation
324         if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing')) || (empty($ext_name))) return false;
325
326         // Not active is the default
327         $active = "N";
328
329         // Check cache
330         if (isset($cacheArray['extensions']['ext_active'][$ext_name])) {
331                 // Load from cache
332                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "CACHE! ext_name={$ext_name}");
333                 $active = $cacheArray['extensions']['ext_active'][$ext_name];
334
335                 // Count cache hits
336                 incrementConfigEntry('cache_hits');
337         } elseif (($ext_name == "cache") || (GET_EXT_VERSION("cache") == "")) {
338                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, "DB! ext_name={$ext_name}");
339                 // Load from database
340                 $result = SQL_QUERY_ESC("SELECT ext_active FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
341                         array($ext_name), __FILE__, __LINE__);
342
343                 // Entry found?
344                 if (SQL_NUMROWS($result) == 1) {
345                         // Load entry
346                         list($active) = SQL_FETCHROW($result);
347                 } // END - if
348
349                 // Free result
350                 SQL_FREERESULT($result);
351
352                 // Write cache array
353                 //* DEBUG: */ echo $ext_name."[DB]: {$active}");
354                 $cacheArray['extensions']['ext_active'][$ext_name] = $active;
355         } else {
356                 // Extension not active!
357                 //* DEBUG: */ echo $ext_name.": Not active!");
358                 $cacheArray['extensions']['ext_active'][$ext_name] = "N";
359         }
360
361         // Debug message
362         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, " ext_name={$ext_name},active={$active}");
363
364         // Is this extension activated? (For admins we always have active extensions...)
365         return ($active == "Y");
366 }
367 // Get version from extensions
368 function GET_EXT_VERSION ($ext_name) {
369         global $cacheArray, $cacheInstance;
370         $ext_ver = false;
371
372         // Extensions are all inactive during installation
373         if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return "";
374         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ext_name={$ext_name}");
375
376         // Is the cache written?
377         if (isset($cacheArray['extensions']['ext_version'][$ext_name])) {
378                 // Load data from cache
379                 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": CACHE!");
380                 $ext_ver = $cacheArray['extensions']['ext_version'][$ext_name];
381
382                 // Count cache hits
383                 incrementConfigEntry('cache_hits');
384         } elseif (!is_object($cacheInstance)) {
385                 // Load from database
386                 $result = SQL_QUERY_ESC("SELECT ext_version FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
387                         array($ext_name), __FILE__, __LINE__);
388
389                 // Is the extension there?
390                 if (SQL_NUMROWS($result) == 1) {
391                         // Load entry
392                         list($ext_ver) = SQL_FETCHROW($result);
393                 } // END - if
394
395                 // Free result
396                 SQL_FREERESULT($result);
397
398                 // Set cache
399                 $cacheArray['extensions']['ext_version'][$ext_name] = $ext_ver;
400         }
401
402         // Return result
403         //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, ": ret={$ext_ver}");
404         return $ext_ver;
405 }
406
407 // Updates a given extension with current extension version to latest version
408 function EXTENSION_UPDATE ($ext_name, $ext_ver, $dry_run = false) {
409         // This shall never do a non-admin user!
410         global $cacheInstance, $UPDATE_NOTES, $NOTES, $EXT_VER_HISTORY;
411         global $EXT_UPDATE_DEPENDS, $EXT_VERSION, $INC_POOL, $cacheArray, $SQLs;
412
413         // Init arrays
414         $SQLs = array(); $INC_POOL = array();
415
416         // Init notes
417         $NOTES = "";
418
419         // Only admins are allowed to update extensions
420         if ((!IS_ADMIN()) || (empty($ext_name))) return false;
421
422         // Load extension in test mode
423         LOAD_EXTENSION($ext_name, "test", $ext_ver, $dry_run, $SQLs);
424
425         // Save version history
426         $history = $EXT_VER_HISTORY;
427
428         // Remove old SQLs array to prevent possible bugs
429         $SQLs = array();
430
431         // Check if version is updated
432         if ((($EXT_VERSION != $ext_ver) || ($dry_run)) && (is_array($history))) {
433                 // Search for starting point
434                 $start = array_search($ext_ver, $history);
435
436                 // And load SQL queries in order of version history
437                 for ($idx = ($start + 1); $idx < sizeof($history); $idx++) {
438                         // Set extension version
439                         $cacheArray['update_ver'][$ext_name] = $history[$idx];
440
441                         // Load again...
442                         LOAD_EXTENSION($ext_name, "update", $cacheArray['update_ver'][$ext_name], $dry_run, $SQLs);
443
444                         if (!empty($EXT_UPDATE_DEPENDS)) {
445                                 // Backup current SQL queries
446                                 $cacheArray['update_sqls'][$ext_name] = $SQLs;
447
448                                 // Is the extension there?
449                                 if (GET_EXT_VERSION($EXT_UPDATE_DEPENDS) != "") {
450                                         // Update another extension first!
451                                         $test = EXTENSION_UPDATE($EXT_UPDATE_DEPENDS, GET_EXT_VERSION($EXT_UPDATE_DEPENDS), $dry_run);
452                                 } else {
453                                         // Register new extension
454                                         $test = EXTENSION_REGISTER($EXT_UPDATE_DEPENDS, 0, $dry_run, false);
455                                 }
456
457                                 // Restore previous SQL queries
458                                 $SQLs = $cacheArray['update_sqls'][$ext_name];
459                                 unset($cacheArray['update_sqls'][$ext_name]);
460                         } // END - if
461
462                         // Add notes
463                         $NOTES .= ADD_EXTENSION_NOTES($cacheArray['update_ver'][$ext_name], $UPDATE_NOTES);
464                 } // END - for
465
466                 // In real-mode execute any existing includes
467                 if (!$dry_run) {
468                         $cacheArray['inc_pool'][$ext_name] = $INC_POOL;
469                         RUN_FILTER('load_includes', $INC_POOL);
470                         $INC_POOL = $cacheArray['inc_pool'][$ext_name];
471                         unset($cacheArray['inc_pool'][$ext_name]);
472                 } // END - if
473
474                 // Run SQLs
475                 RUN_FILTER('run_sqls', array('dry_run' => $dry_run, 'sqls' => $SQLs));
476
477                 if (!$dry_run) {
478                         // Create task
479                         CREATE_EXTENSION_UPDATE_TASK(GET_CURRENT_ADMIN_ID(), $ext_name, $cacheArray['update_ver'][$ext_name], addslashes($NOTES));
480
481                         // Update extension's version
482                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
483                                 array($cacheArray['update_ver'][$ext_name], $ext_name), __FILE__, __LINE__);
484
485                         // Remove arrays
486                         unset($SQLs);
487                         unset($cacheArray['update_ver'][$ext_name]);
488
489                         // Run filters on success extension update
490                         RUN_FILTER('extension_update', $ext_name);
491                 } else {
492                         // In "dry-run" mode return array with SQL commands
493                         return $SQLs;
494                 }
495         } // END - if
496 }
497
498 // Output verbose SQL table for extension
499 function EXTENSION_VERBOSE_TABLE ($queries = array(), $title = ADMIN_SQLS_EXECUTED_ON_REMOVAL, $dashed = "", $switch = false, $width = "100%") {
500         global $SQLs;
501
502         // Are there some queries in $queries?
503         if (count($queries) > 0) {
504                 // Then use them instead!
505                 $SQLs = $queries;
506         } // END - if
507
508         // Init variables
509         $SW = 2; $i = 1;
510         $OUT = "";
511
512         // Do we have queries?
513         if ((is_array($SQLs)) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
514                 foreach ($SQLs as $idx => $sql) {
515                         // Trim out spaces
516                         $sql = trim($sql);
517
518                         // Output command if set
519                         if (!empty($sql)) {
520                                 // Prepare output for template
521                                 $content = array(
522                                         'sw'  => $SW,
523                                         'i'   => $i,
524                                         'sql' => $sql
525                                 );
526
527                                 // Load row template
528                                 $OUT .= LOAD_TEMPLATE("admin_ext_sql_row", true, $content);
529
530                                 // Switch color and count up
531                                 $SW = 3 - $SW;
532                                 $i++;
533                         } // END - if
534                 } // END - foreach
535
536                 // Prepare content for template
537                 $content = array(
538                         'width'  => $width,
539                         'dashed' => $dashed,
540                         'title'  => $title,
541                         'out'    => $OUT
542                 );
543
544                 // Load main template
545                 $OUT = LOAD_TEMPLATE("admin_ext_sql_table", true, $content);
546         } elseif ((GET_EXT_VERSION("sql_patches") >= "0.0.7") && (getConfig('verbose_sql') == "Y")) {
547                 // No addional SQL commands to run
548                 $OUT = LOAD_TEMPLATE("admin_settings_saved", true, getMessage('ADMIN_NO_ADDITIONAL_SQLS'));
549         } // END - if
550
551         // Return output
552         return $OUT;
553 }
554
555 // Get extension name from id
556 function GET_EXT_NAME ($ext_id) {
557         global $cacheArray;
558
559         // Init extension name
560         $ret = "";
561
562         // Is cache there?
563         if (isset($cacheArray['extensions']['ext_name'][$ext_id])) {
564                 // Load from cache
565                 $ret = $cacheArray['extensions']['ext_name'][$ext_id];
566
567                 // Count cache hits
568                 incrementConfigEntry('cache_hits');
569         } elseif (!EXT_IS_ACTIVE("cache")) {
570                 // Load from database
571                 $result = SQL_QUERY_ESC("SELECT ext_name FROM `{!_MYSQL_PREFIX!}_extensions` WHERE id=%s LIMIT 1",
572                         array(bigintval($ext_id)), __FILE__, __LINE__);
573                 list($ret) = SQL_FETCHROW($result);
574                 SQL_FREERESULT($result);
575         }
576         return $ret;
577 }
578
579 // Get extension id from name
580 function GET_EXT_ID ($ext_name) {
581         global $cacheArray;
582
583         // Init ID number
584         $ret = 0;
585         if (isset($cacheArray['extensions']['ext_id'][$ext_name])) {
586                 // Load from cache
587                 $ret = $cacheArray['extensions']['ext_id'][$ext_name];
588
589                 // Count cache hits
590                 incrementConfigEntry('cache_hits');
591         } elseif (!EXT_IS_ACTIVE("cache")) {
592                 // Load from database
593                 $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
594                         array($ext_name), __FILE__, __LINE__);
595                 list($ret) = SQL_FETCHROW($result);
596                 SQL_FREERESULT($result);
597         }
598
599         // Return value
600         return $ret;
601 }
602
603 // Activate given extension
604 function ACTIVATE_EXTENSION ($ext_name) {
605         // Activate the extension
606         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_active='Y' WHERE ext_name='%s' LIMIT 1",
607                 array($ext_name), __FILE__, __LINE__);
608
609         // Extension has been activated?
610         if (SQL_AFFECTEDROWS() == 1) {
611                 // Then run all queries
612                 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "activate");
613         } // END - if
614 }
615
616 // Deactivate given extension
617 function DEACTIVATE_EXTENSION($ext_name) {
618         // Activate the extension
619         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_extensions` SET ext_active='N' WHERE ext_name='%s' LIMIT 1",
620                 array($ext_name), __FILE__, __LINE__);
621
622         // Extension has been activated?
623         if (SQL_AFFECTEDROWS() == 1) {
624                 // Then run all queries
625                 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "deactivate");
626
627                 // Create new task
628                 CREATE_EXTENSION_DEACTIVATION_TASK($ext_name);
629
630                 // Notify the admin
631                 SEND_ADMIN_NOTIFICATION(ADMIN_SUBJECT_EXTENSION_DEACTIVATED, "admin_ext_deactivated", array('ext_name' => $ext_name));
632         } // END - if
633 }
634
635 // Checks wether the extension is older than given
636 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
637         // Get current extension version
638         $currVersion = GET_EXT_VERSION($ext_name);
639
640         // Remove all dots from both versions
641         $currVersion = str_replace(".", "", $currVersion);
642         $ext_ver = str_replace(".", "", $ext_ver);
643
644         // Now compare both and return the result
645         return ($currVersion < $ext_ver);
646 }
647
648 // Creates a new task for updated extension
649 function CREATE_EXTENSION_UPDATE_TASK ($admin_id, $ext_name, $ext_ver, $notes) {
650         // Create subject line
651         $subject = "[UPDATE-".$ext_name."-".$ext_ver.":] {!ADMIN_UPDATE_EXT_SUBJ!}";
652
653         // Is the extension there?
654         if (GET_EXT_VERSION($ext_name) != "") {
655                 // Check if task is not there
656                 if (DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) {
657                         // Task not created so it's a brand-new extension which we need to register and create a task for!
658                         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())",
659                                 array($admin_id, $subject, $notes), __FILE__, __LINE__);
660                 } // END - if
661         } // END - if
662 }
663
664 // Creates a new task for newly installed extension
665 function CREATE_NEW_EXTENSION_TASK ($admin_id, $subject, $ext) {
666         // Not installed and do we have created a task for the admin?
667         if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) == "")) {
668                 // Template file
669                 $tpl = sprintf("%stemplates/%s/html/ext/ext_%s.tpl",
670                         constant('PATH'),
671                         GET_LANGUAGE(),
672                         $ext
673                 );
674
675                 // Load text for task
676                 if (FILE_READABLE($tpl)) {
677                         // Load extension's own text template (HTML!)
678                         $msg = LOAD_TEMPLATE("ext_".$ext, true);
679                 } else {
680                         // Load default message
681                         $msg = LOAD_TEMPLATE("admin_new_ext", "", 0);
682                 }
683
684                 // Task not created so it's a brand-new extension which we need to register and create a task for!
685                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created)
686 VALUES (%s,0,'NEW','EXTENSION','%s','%s',UNIX_TIMESTAMP())",
687                         array(
688                                 $admin_id,
689                                 $subject,
690                                 addslashes($msg),
691                         ),  __FILE__, __LINE__, true, false
692                 );
693         } // END - if
694 }
695
696 // Creates a task for automatically deactivated (deprecated) extension
697 function CREATE_EXTENSION_DEACTIVATION_TASK ($ext) {
698         // Create subject line
699         $subject = sprintf("[%s:] %s", $ext, TASK_SUBJ_EXTENSION_DEACTIVATED);
700
701         // Not installed and do we have created a task for the admin?
702         if ((DETERMINE_TASK_ID_BY_SUBJECT($subject) == 0) && (GET_EXT_VERSION($ext) != "")) {
703                 // Task not created so add it
704                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, userid, status, task_type, subject, text, task_created)
705 VALUES (0,0,'NEW','EXTENSION_DEACTIVATION','%s','%s',UNIX_TIMESTAMP())",
706                         array(
707                                 $subject,
708                                 addslashes(LOAD_TEMPLATE("task_ext_deactivated", true, $ext)),
709                         ),  __FILE__, __LINE__, true, false
710                 );
711         } // END - if
712 }
713
714 // Checks if the module has a menu
715 function MODULE_HAS_MENU ($mod, $forceDb = false) {
716         global $cacheArray;
717
718         // All is false by default
719         $ret = false;
720         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):mod={$mod},cache=".GET_EXT_VERSION("cache")."<br />\n";
721         if (GET_EXT_VERSION("cache") >= "0.1.2") {
722                 // Cache version is okay, so let's check the cache!
723                 if (isset($cacheArray['modules']['has_menu'][$mod])) {
724                         // Check module cache and count hit
725                         $ret = ($cacheArray['modules']['has_menu'][$mod] == "Y");
726                         incrementConfigEntry('cache_hits');
727                 } elseif (isset($cacheArray['extensions']['ext_menu'][$mod])) {
728                         // Check cache and count hit
729                         $ret = ($cacheArray['extensions']['ext_menu'][$mod] == "Y");
730                         incrementConfigEntry('cache_hits');
731                 } elseif ((IS_ADMIN()) && ($mod == "admin")) {
732                         // Admin module has always a menu!
733                         $ret = true;
734                 }
735         } elseif ((GET_EXT_VERSION("sql_patches") >= "0.3.6") && ((!EXT_IS_ACTIVE("cache")) || ($forceDb === true))) {
736                 // Check database for entry
737                 $result = SQL_QUERY_ESC("SELECT has_menu FROM `{!_MYSQL_PREFIX!}_mod_reg` WHERE module='%s' LIMIT 1",
738                         array($mod), __FILE__, __LINE__);
739
740                 // Entry found?
741                 if (SQL_NUMROWS($result) == 1) {
742                         // Load "has_menu" column
743                         list($has_menu) = SQL_FETCHROW($result);
744
745                         // Fake cache... ;-)
746                         $cacheArray['extensions']['ext_menu'][$mod] = $has_menu;
747
748                         // Does it have a menu?
749                         $ret = ($has_menu == "Y");
750                 } // END  - if
751
752                 // Free memory
753                 SQL_FREERESULT($result);
754         } elseif (GET_EXT_VERSION("sql_patches") == "") {
755                 // No sql_patches installed, so maybe in admin area?
756                 $ret = ((IS_ADMIN()) && ($mod == "admin")); // Then there is a menu!
757         }
758
759         // Return status
760         //* DEBUG: */ var_dump($ret);
761         return $ret;
762 }
763
764 // Determines the task id for given extension
765 function DETERMINE_EXTENSION_TASK_ID ($ext_name) {
766         // Default is not found
767         $task_id = 0;
768
769         // Search for extension task's id
770         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_task_system` WHERE task_type='EXTENSION' AND subject='[%s:]' LIMIT 1",
771                 array($ext_name), __FILE__, __LINE__);
772
773         // Entry found?
774         if (SQL_NUMROWS($result) == 1) {
775                 // Task found so load task's ID and register extension...
776                 list($task_id) = SQL_FETCHROW($result);
777         } // END - if
778
779         // Free result
780         SQL_FREERESULT($result);
781
782         // Return it
783         return $task_id;
784 }
785
786 // Determines the task id for given subject
787 function DETERMINE_TASK_ID_BY_SUBJECT ($subject) {
788         // Default is not found
789         $task_id = 0;
790
791         // Search for task id
792         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_task_system` WHERE subject LIKE '%s%%' LIMIT 1",
793                 array($subject), __FILE__, __LINE__);
794
795         // Entry found?
796         if (SQL_NUMROWS($result) == 1) {
797                 // Task found so load task's ID and register extension...
798                 list($task_id) = SQL_FETCHROW($result);
799         } // END - if
800
801         // Free result
802         SQL_FREERESULT($result);
803
804         // Return it
805         return $task_id;
806 }
807
808 // Add updates notes for given version
809 function ADD_EXTENSION_NOTES ($ver, &$UPDATE_NOTES) {
810         // Init notes/content
811         $out = ""; $content = array();
812
813         // Is do we have verbose output enabled?
814         if ((getConfig('verbose_sql') == "Y") || (!EXT_IS_ACTIVE("sql_patches"))) {
815
816                 // Update notes found?
817                 if (!empty($UPDATE_NOTES)) {
818                         // Update notes found
819                         $content = array(
820                                 'ver'   => $ver,
821                                 'notes' => $UPDATE_NOTES
822                         );
823                         $UPDATE_NOTES = "";
824                 } elseif (($ver == "0.0") || ($ver == "0.0.0")) {
825                         // Initial release
826                         $content = array(
827                                 'ver'   => $ver,
828                                 'notes' => INITIAL_RELEASE
829                         );
830                 } else {
831                         // No update notes found!
832                         $content = array(
833                                 'ver'   => $ver,
834                                 'notes' => NO_UPDATE_NOTES
835                         );
836                 }
837
838                 // Load template
839                 $out = LOAD_TEMPLATE("admin_ext_notes", true, $content);
840         } // END - if
841
842         // Return the output
843         return $out;
844 }
845
846 //
847 ?>