]> git.mxchange.org Git - mailer.git/blob - inc/extensions.php
Fixes for sql_patches removal vs. password reset of admin
[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 (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
36 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40 //
41 function EXTENSION_REGISTER ($ext_name, $id, $dry_run=false)
42 {
43         global $NOTES, $_CONFIG, $INC_POOL;
44         // We want to register an extension and registration status is by default "failed" (= false)
45         $EXT_LOAD_MODE = "register"; $ret = false; $SQLs = array();
46         $INC_POOL = array();
47
48         // This shall never do a non-admin user!
49         if (!IS_ADMIN()) return false;
50
51         // Is this extension already installed?
52         if (EXT_IS_ACTIVE($ext_name)) return false;
53
54         // Generate file name
55         $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
56
57         // Does this extension exists?
58         if (file_exists($file) && is_readable($file))
59         {
60                 // Extension was found so we can load it in registration mode
61                 require($file);
62
63                 // And run possible updates
64                 $EXT_LOAD_MODE = "update"; $EXT_UPDATE_DEPENDS = "";
65                 foreach ($EXT_VER_HISTORY as $EXT_VER)
66                 {
67                         require($file);
68                         if (((GET_EXT_VERSION("sql_patches") != '') && ($_CONFIG['verbose_sql'] == "Y")) || (!EXT_IS_ACTIVE("sql_patches")))
69                         {
70                                 if (!empty($UPDATE_NOTES))
71                                 {
72                                         // Update notes found
73                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
74                                         $UPDATE_NOTES = "";
75                                 }
76                                  elseif (($EXT_VER == "0.0") || ($EXT_VER == "0.0.0"))
77                                 {
78                                         // Initial release
79                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
80                                 }
81                                  else
82                                 {
83                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
84                                 }
85                         }
86                 }
87
88                 // Does this extension depends on an outstanding update of another update?
89                 if (!empty($EXT_UPDATE_DEPENDS))
90                 {
91                         // Backup SQL commands and clear current
92                         $SQLs2 = $SQLs;  $SQLs = array(); $test = false;
93
94                         // Backup language as well
95                         $LANG_BCK = $EXT_LANG_PREFIX; $EXT_ALWAYS_ACTIVE = "N";
96
97                         // Load required extension also in update mode
98                         $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $EXT_UPDATE_DEPENDS);
99
100                         // Check for required file
101                         if (file_exists($file) && is_readable($file))
102                         {
103                                 // File exists so let's load it
104                                 $VER_BACKUP = $EXT_VERSION;
105                                 require($file);
106                                 $EXT_VERSION = $VER_BACKUP;
107
108                                 // If versions mismatch update extension first
109                                 $ext_ver = GET_EXT_VERSION($EXT_UPDATE_DEPENDS);
110                                 if (empty($ext_ver))
111                                 {
112                                         // Extension not registered so far so first load task's ID...
113                                         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE task_type='EXTENSION' AND subject LIKE '[%s:]%%' LIMIT 1",
114                                          array($EXT_UPDATE_DEPENDS), __FILE__, __LINE__);
115                                         if (SQL_NUMROWS($result) == 1)
116                                         {
117                                                 // Task found so load task's ID and register extension...
118                                                 list($task) = SQL_FETCHROW($result);
119                                                 SQL_FREERESULT($result);
120                                                 $test = EXTENSION_REGISTER($EXT_UPDATE_DEPENDS, $task, $dry_run);
121                                         }
122                                 }
123                                  elseif ($ext_ver != $EXT_VERSION)
124                                 {
125                                         // Ok, update this extension now
126                                         EXTENSION_UPDATE(basename($file), $EXT_UPDATE_DEPENDS, $ext_ver, $dry_run);
127                                         $test = true;
128                                 }
129                                  else
130                                 {
131                                         // Nothing to register / update before...
132                                         $test = true;
133                                 }
134                         }
135                          else
136                         {
137                                 // Required file for update does not exists!
138                                 $test = true;
139                                 // But this is fine for the first time...
140                         }
141
142                         // Finally restore previous SQLs
143                         $SQLs = $SQLs2; unset($SQLs2);
144                         $EXT_LANG_PREFIX = $LANG_BCK;
145                 }
146                  else
147                 {
148                         // Does not depend on an other extension
149                         $test = true;
150                 }
151
152                 // Does everthing before wents ok?
153                 if ($test)
154                 {
155                         // "Dry-run-mode" activated?
156                         if (!$dry_run)
157                         {
158                                 // Are there any SQL commands to run?
159                                 if (count($SQLs) > 0)
160                                 {
161                                         foreach ($SQLs as $sql)
162                                         {
163                                                 $sql = trim($sql);
164                                                 // Run SQL command
165                                                 if (!empty($sql))
166                                                 {
167                                                         // Run SQLs without compiling them...
168                                                         $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
169                                                 }
170                                         }
171                                 }
172
173                                 // Check for added include files
174                                 if (count($INC_POOL > 0))
175                                 {
176                                         // Loads every include file
177                                         foreach ($INC_POOL as $inc)
178                                         {
179                                                 require_once($inc);
180                                         }
181                                 }
182
183                                 // Register extension
184                                 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_extensions (ext_name, ext_lang_file, ext_active, ext_version) VALUES ('%s', '%s', '%s', '%s')",
185                                  array($ext_name, $EXT_LANG_PREFIX, $EXT_ALWAYS_ACTIVE, $EXT_VERSION), __FILE__, __LINE__);
186
187                                 // Update task management
188                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_task_system SET status='SOLVED' WHERE id=%d LIMIT 1",
189                                  array(bigintval($id)), __FILE__, __LINE__);
190
191                                 // In normal mode return a true on success
192                                 $ret = true; unset($SQLs);
193                         }
194                          else
195                         {
196                                 // Rewrite SQL command to keep { and } inside
197                                 foreach ($SQLs as $key=>$sql)
198                                 {
199                                         $sql = str_replace('{', "&#123;", str_replace('}', "&#125;", $sql));
200                                         $SQLs[$key] = $sql;
201                                 }
202
203                                 // In  "dry-run" mode return array with all SQL commands
204                                 $ret = $SQLs;
205
206                                 // Remove all SQL commands
207                                 unset($SQLs);
208                         }
209                 }
210                  else
211                 {
212                         // No, an error occurs while registering extension :-(
213                         $ret = false;
214                 }
215         }
216          elseif (($id > 0) && (!empty($ext_name)))
217         {
218                 // Remove task from system when id and extension's name is valid
219                 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE id=%d AND status='NEW' LIMIT 1",
220                  array(bigintval($id)), __FILE__, __LINE__);
221         }
222         // Return status code
223         return $ret;
224 }
225 //
226 function EXTENSION_RUN_SQLS($id, $EXT_LOAD_MODE) {
227         global $cacheInstance;
228         $SQLs = array();
229
230         // This shall never do a non-admin user!
231         if (!IS_ADMIN()) return false;
232
233         // Get extension's name
234         $ext_name = GET_EXT_NAME($id);
235         if (empty($ext_name)) return false;
236
237         // Load extension in detected mode
238         $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
239         if (file_exists($file) && is_readable($file)) require($file);
240
241         if ((is_array($SQLs) && (sizeof($SQLs) > 0))) {
242                 // Run SQL commands...
243                 foreach ($SQLs as $sql) {
244                         // Trim spaces away which we don't need
245                         $sql = trim($sql);
246
247                         // Is there still an SQL query?
248                         if (!empty($sql)) {
249                                 // Run SQL command
250                                 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
251                         } // END - if
252                 } // END - foreach
253
254                 // Remove cache file(s) if extension is active
255                 if ((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) {
256                         // Remove cache files
257                         if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
258                         if ($cacheInstance->cache_file("mod_reg", true))    $cacheInstance->cache_destroy();
259                         if ($cacheInstance->cache_file("config", true))     $cacheInstance->cache_destroy();
260                 } // END - if
261
262                 // Is this the sql_patches?
263                 if ($ext_name == "sql_patches") {
264                         // Then redirect to logout
265                         LOAD_URL("modules.php?module=admin&logout=1&remove=sql_patches");
266                 } // END - if
267         } // END - if
268 }
269 //
270 function EXT_IS_ACTIVE ($ext_name, $ignore_admin = false, $ignore_cache = false) {
271         global $cacheArray, $_CONFIG;
272
273         // Extensions are all inactive during installation
274         if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing')) || (empty($ext_name))) return false;
275
276         // Extension's file name will also be checked
277         $file = PATH."inc/extensions/ext-".$ext_name.".php";
278         if ((!file_exists($file)) && (!is_readable($file))) return false;
279         //* DEBUG: */ echo "*".$ext_name."*<br />";
280
281         // Failed is the default
282         $ret = false;
283         if ((!empty($cacheArray['extensions']['ext_active'][$ext_name])) && (!$ignore_cache))
284         {
285                 // Load from cache
286                 $active = $cacheArray['extensions']['ext_active'][$ext_name];
287
288                 // Count cache hits
289                 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++;
290         }
291          else
292         {
293                 // Load from database
294                 $result = SQL_QUERY_ESC("SELECT ext_active FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
295                  array($ext_name), __FILE__, __LINE__);
296                 if (SQL_NUMROWS($result) == 0)
297                 {
298                         // Extension was not found!
299                         return false;
300                 }
301                 list($active) = SQL_FETCHROW($result);
302                 SQL_FREERESULT($result);
303
304                 // Write cache array
305                 $cacheArray['extensions']['ext_active'][$ext_name] = $active;
306         }
307
308         // Is this extension activated? (For admins we always have active extensions...)
309         $inc = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
310         // Shorter way
311         return (
312                 (
313                         ($active == "Y") || (
314                                 (IS_ADMIN()) &&
315                                 (!$ignore_admin) &&
316                                 (!empty($active))
317                         )
318                 ) && (
319                         file_exists($inc)
320                 ) && (
321                         is_readable($inc)
322                 )
323         );
324 }
325 // Get version from extensions
326 function GET_EXT_VERSION ($ext_name) {
327         global $cacheArray, $_CONFIG, $cacheInstance;
328         $ret = false;
329
330         // Extensions are all inactive during installation
331         if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return "";
332
333         // Is the cache written?
334         if (!empty($cacheArray['extensions']['ext_version'][$ext_name])) {
335                 // Load data from cache
336                 $ret = $cacheArray['extensions']['ext_version'][$ext_name];
337
338                 // Count cache hits
339                 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++; else $_CONFIG['cache_hits'] = 1;
340         } elseif (!is_object($cacheInstance)) {
341                 // Load from database
342                 $result = SQL_QUERY_ESC("SELECT ext_version FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
343                  array($ext_name), __FILE__, __LINE__);
344                 list($ret) = SQL_FETCHROW($result);
345                 SQL_FREERESULT($result);
346
347                 // Set cache
348                 $cacheArray['extensions']['ext_version'][$ext_name] = $ret;
349         }
350         return $ret;
351 }
352 //
353 function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false)
354 {
355         // This shall never do a non-admin user!
356         global $cacheInstance, $_CONFIG, $NOTES; $SQLs = array();
357         if ((!IS_ADMIN()) || (empty($ext))) return false;
358
359         // Load extension in update mode
360         $EXT_LOAD_MODE = "update"; $EXT_UPDATE_DEPENDS = ""; $NOTES = "";
361         include(PATH."inc/extensions/".$file);
362         if (!empty($EXT_UPDATE_DEPENDS))
363         {
364                 // Update another extension first!
365                 $test = EXTENSION_UPDATE(("ext-".$EXT_UPDATE_DEPENDS.".php"), $EXT_UPDATE_DEPENDS, GET_EXT_VERSION($EXT_UPDATE_DEPENDS), $dry_run);
366         }
367
368         // Check if version is updated
369         if ((($EXT_VERSION != $EXT_VER) || ($dry_run)) && (is_array($EXT_VER_HISTORY)))
370         {
371                 // Search for starting point
372                 $start = array_search($EXT_VER, $EXT_VER_HISTORY);
373                 $NOTES = "";
374
375                 // And load SQL queries in order of version history
376                 for ($idx = ($start + 1); $idx < sizeof($EXT_VER_HISTORY); $idx++)
377                 {
378                         // Remove old SQLs array to prevent possible bugs
379                         if (!$dry_run) { unset($SQLs); $SQLs = array(); }
380
381                         // Set version
382                         $EXT_VER = $EXT_VER_HISTORY[$idx];
383
384                         // Include again...
385                         include(PATH."inc/extensions/".$file);
386
387                         // Add notes
388                         if ($_CONFIG['verbose_sql'] == "Y")
389                         {
390                                 $EXT_VER = $EXT_VER_HISTORY[$idx];
391                                 if (!empty($UPDATE_NOTES))
392                                 {
393                                         // Update notes found
394                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
395                                         $UPDATE_NOTES = "";
396                                 }
397                                  elseif ($EXT_VER == "0.0")
398                                 {
399                                         // Initial release
400                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
401                                 }
402                                  else
403                                 {
404                                         $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
405                                 }
406                         }
407
408                         // Run SQLs
409                         if ((is_array($SQLs)) && (!$dry_run))
410                         {
411                                 // Run SQL commands
412                                 foreach ($SQLs as $sql)
413                                 {
414                                         $sql = trim($sql);
415                                         if (!empty($sql))
416                                         {
417                                                 // Run SQL command...
418                                                 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
419                                         }
420                                 }
421                         } elseif (GET_EXT_VERSION("sql_patches") == "") {
422                                 // Remove SQLs if extension is not installed
423                                 $SQLs = array();
424                         }
425                 }
426
427                 if (!$dry_run)
428                 {
429                         // In normal mode insert task and update extension's version...
430                         $ext_subj = "[UPDATE-".$ext."-".$EXT_VERSION.":] ".ADMIN_UPDATE_EXT_SUBJ;
431
432                         // Create task
433                         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE subject='%s' LIMIT 1",
434                          array($ext_subj), __FILE__, __LINE__);
435                         if (SQL_NUMROWS($result) == 0)
436                         {
437                                 // Task not created so it's a brand-new extension which we need to register and create a task for!
438                                 $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())",
439                                  array(GET_ADMIN_ID(SQL_ESCAPE(get_session('admin_login'))), $ext_subj, addslashes($NOTES)), __FILE__, __LINE__);
440                         }
441
442                         // Free memory
443                         SQL_FREERESULT($result);
444
445                         // Update extension's version
446                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
447                          array($EXT_VERSION, $ext), __FILE__, __LINE__);
448
449                         // Update cache
450                         if (EXT_IS_ACTIVE("cache"))
451                         {
452                                 if ($cacheInstance->cache_file("extensions", true) == true) $cacheInstance->cache_destroy();
453                                 if ($cacheInstance->cache_file("config", true) == true)     $cacheInstance->cache_destroy();
454                                 if ($cacheInstance->cache_file("mod_reg", true) == true)    $cacheInstance->cache_destroy();
455                         }
456
457                         // Remove array
458                         unset($SQLs);
459                 }
460                  else
461                 {
462                         // In "dry-run" mode return array with SQL commands
463                         return $SQLs;
464                 }
465         }
466 }
467 //
468 function EXTENSION_VERBOSE_TABLE($SQLs, $title=ADMIN_SQLS_EXECUTED_ON_REMOVAL, $dashed="", $switch=false, $WIDTH="480")
469 {
470         global $_CONFIG;
471
472         $S = false; $SW = 2; $i = 1;
473         $OUT = "";
474         if ((is_array($SQLs)) && (GET_EXT_VERSION("sql_patches")) && ($_CONFIG['verbose_sql'] == "Y"))
475         {
476                 $OUT  = "<DIV align=\"center\">
477 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"".$WIDTH."\" align=\"center\"".$dashed.">
478 <TR>
479   <TD colspan=\"2\" align=\"center\" class=\"admin_title bottom2\" height=\"24\">
480     <STRONG>".$title.":</STRONG>
481   </TD>
482 </TR>\n";
483                 foreach ($SQLs as $idx=>$sql)
484                 {
485                         $sql = trim($sql);
486                         if (!empty($sql))
487                         {
488                                 $S = true;
489                                 $OUT .= "<TR>
490   <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 2px\" width=\"30\">".$i.".</TD>
491   <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 5px; padding-right: 5px\">
492     ".$sql."
493   </TD>
494 </TR>\n";
495                                 if ($switch) $SW = 3 - $SW;
496                                 $i++;
497                         }
498                 }
499         }
500
501         if ((!$S) && (GET_EXT_VERSION("sql_patches")) && ($_CONFIG['verbose_sql'] == "Y"))
502         {
503                 // No addional SQL commands to run
504                 $OUT .= "<TR>
505   <TD colspan=\"2\" align=\"center\" class=\"switch_sw2 bottom2\" height=\"24\">
506     <FONT class=\"admin_note\">".ADMIN_NO_ADDIONAL_SQLS."</FONT>
507   </TD>
508 </TR>\n";
509         }
510
511         if (!empty($OUT)) {
512                 // Add missing close-table tag
513                 $OUT .= "</TABLE>
514 </DIV>\n";
515         }
516         return $OUT;
517 }
518 //
519 function GET_EXT_NAME($id)
520 {
521         $ret = "";
522         global $cacheArray, $_CONFIG;
523         if (!empty($cacheArray['extensions']['ext_id'][$id]))
524         {
525                 // Load from cache
526                 $ret = $cacheArray['extensions']['ext_id'][$id];
527
528                 // Count cache hits
529                 $_CONFIG['cache_hits']++;
530         }
531          else
532         {
533                 // Load from database
534                 $result = SQL_QUERY_ESC("SELECT ext_name FROM "._MYSQL_PREFIX."_extensions WHERE id=%d LIMIT 1",
535                  array(bigintval($id)), __FILE__, __LINE__);
536                 list($ret) = SQL_FETCHROW($result);
537                 SQL_FREERESULT($result);
538         }
539         return $ret;
540 }
541 //
542 function GET_EXT_ID($name)
543 {
544         $ret = "0";
545         global $cacheArray, $_CONFIG;
546         if ((isset($cacheArray['extensions']['ext_id'])) && (is_array($cacheArray['extensions']['ext_id'])))
547         {
548                 // Load from cache
549                 $ret = array_search($name, $cacheArray['extensions']['ext_id']);
550
551                 // Count cache hits
552                 $_CONFIG['cache_hits']++;
553         }
554          else
555         {
556                 // Load from database
557                 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
558                  array($name), __FILE__, __LINE__);
559                 list($ret) = SQL_FETCHROW($result);
560                 SQL_FREERESULT($result);
561         }
562         return $ret;
563 }
564 //
565 ?>