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