Source for file install.php

Documentation is available at install.php

  1. <?php
  2. /**
  3.  * PLUGIN INSTALLER
  4.  *
  5.  * Version 0.4
  6.  * Für Contenido 4.4.4 and up 4.5.x
  7.  *
  8.  * @author      Martin Horwath (horwath@dayside.net)
  9.  * @date        26.09.2004
  10.  * @date        10.10.2005
  11.  * @date        15.02.2006 - Paul Sauer <contenido@saueronline.de>
  12.  * @date        12.09.2006 - Murat Purc <murat@purc.de>
  13.  * @package     Contenido
  14.  * @subpackage  Plugin installer
  15.  */
  16.  
  17. /* PLUGIN INSTALLER
  18.  * Version 0.4
  19.  * Für Contenido 4.4.4 and up 4.5.x
  20.  * Autor: Martin Horwath (horwath@dayside.net)
  21.  * Datum: 26.09.2004
  22.  * Modifiziert: 10.10.2005
  23.  *              15.02.2006 - Paul Sauer <contenido@saueronline.de>
  24.  *              12.09.2006 - Murat Purc <murat@purc.de>
  25.  */
  26.  
  27.  
  28. if (!defined("CON_FRAMEWORK")) {
  29.     define("CON_FRAMEWORK", true);
  30. }
  31.  
  32. $contenido_path = '../../';
  33.  
  34. // include security class and check request variables
  35. include_once ($contenido_path . 'classes/class.security.php');
  36. Contenido_Security::checkRequests();
  37.  
  38. $cfg['debug']['installer'] = false;
  39. $checktablestatus = true;
  40.  
  41. if (file_exists($contenido_path . 'includes/startup.php')) {
  42.     include_once($contenido_path . 'includes/startup.php');
  43. } else {
  44.     include_once($contenido_path . 'includes/config.php');
  45. }
  46.  
  47. cInclude('includes', 'functions.general.php');
  48.  
  49. $cfg['debug']['backend_exectime']['fullstart'] = getmicrotime();
  50.  
  51. cInclude('includes', 'functions.i18n.php');
  52. cInclude('includes', 'functions.api.php');
  53. cInclude('includes', 'functions.general.php');
  54. cInclude('includes', 'functions.database.php');
  55. cInclude('includes', 'functions.str.php');
  56.  
  57. cInclude('classes', 'class.xml.php');
  58. cInclude('classes', 'class.navigation.php');
  59. cInclude('classes', 'class.template.php');
  60. cInclude('classes', 'class.backend.php');
  61. cInclude('classes', 'class.notification.php');
  62. cInclude('classes', 'class.area.php');
  63. cInclude('classes', 'class.action.php');
  64. cInclude('classes', 'class.layout.php');
  65. cInclude('classes', 'class.treeitem.php');
  66. cInclude('classes', 'class.user.php');
  67. cInclude('classes', 'class.group.php');
  68. cInclude('classes', 'class.cat.php');
  69. cInclude('classes', 'class.client.php');
  70. cInclude('classes', 'class.inuse.php');
  71. cInclude('classes', 'class.table.php');
  72.  
  73. plugin_include('mod_rewrite', 'includes/functions.mod_rewrite_setup.php');
  74.  
  75. page_open(array('sess' => 'Contenido_Session', 'auth' => 'Contenido_Challenge_Crypt_Auth', 'perm' => 'Contenido_Perm'));
  76.  
  77. i18nInit($cfg['path']['contenido'] . $cfg['path']['locale'], $belang);
  78. cInclude('includes', 'cfg_language_de.inc.php');
  79.  
  80. class DB_Upgrade extends DB_Contenido {/*donut*/}
  81.  
  82. // instanzen der db_contenido
  83. $db  = new DB_Contenido;
  84. $db2 = new DB_Contenido;
  85.  
  86. $data['top']         = '';
  87. $data['content']     = '';
  88. $data['bottom']      = '';
  89. $data['body_bottom'] = '';
  90.  
  91. #$currentuser = new User();
  92. #$currentuser->loadUserByUserID($auth->auth["uid"]);
  93.  
  94. $cfg["debug"]["backend_exectime"]["start"] = getmicrotime();
  95.  
  96. if ($checktablestatus) {
  97.     $required_fields = array(
  98.         "idplugin", "name", "version", "author", "idinternal", "url",
  99.         "status", "description", "install", "uninstall", "date"
  100.     );
  101.  
  102. //    $required_table = "DROP TABLE " . $cfg['tab']['plugins'] . ";
  103.     $required_table = "RENAME TABLE " . $cfg['tab']['plugins'] . " TO " . $cfg['tab']['plugins'] . "_" . date('Ymd') . ";
  104.                       CREATE TABLE " . $cfg['tab']['plugins'] . " (
  105.                           idplugin INT(10) NOT NULL default '0',
  106.                           name VARCHAR(60) default NULL,
  107.                           version VARCHAR(10) NOT NULL default '0',
  108.                           author VARCHAR(60) default NULL,
  109.                           idinternal VARCHAR(32) NOT NULL default '0',
  110.                           url TEXT,
  111.                           status INT(10) NOT NULL default '0',
  112.                           description TEXT,
  113.                           install TEXT,
  114.                           uninstall TEXT,
  115.                           date DATETIME NOT NULL default '0000-00-00 00:00:00',
  116.                           PRIMARY KEY (idplugin)
  117.                       ) TYPE=MyISAM;";
  118.     // now we check if the plugin table has the right format...
  119.     msg("Checking status " . $cfg['tab']['plugins']);
  120.     $ptable = $db->metadata($cfg['tab']['plugins']);
  121.  
  122.     foreach ($ptable as $key) {
  123.         if (!in_array($key['name'], $required_fields)) {
  124.             msg($key['name'] . " (this key can be deleted)", "unused key");
  125.         } else {
  126.             $availableKeys[] = $key['name'];
  127.         }
  128.         $foundkeys[] = $key['name'];
  129.     }
  130.     foreach ($required_fields as $key) {
  131.         if (!in_array($key, $foundkeys)) {
  132.             msg($key . " (this key must be added)", "missing key");
  133.             $missingKeys[] = $key;
  134.         }
  135.     }
  136.     unset ($foundkeys, $key);
  137.     // available elements in table are stored in array -> $availableKeys;
  138.     // missing elements in table are stored in array -> $missingKeys;
  139.     // this is a possible way to handle new versions of plugin installer
  140.     // since this is initial release the table will be dropped and recreated
  141.     // when a missing element is found.
  142.     if (count($missingKeys) > 0) {
  143.         $sql_data = remove_remarks($required_table);
  144.         $sql_pieces = split_sql_file($sql_data, ';');
  145.         msg(count($sql_pieces) . " queries", "Executing:");
  146.         foreach ($sql_pieces as $sqlinit) {
  147.             $db->query($sqlinit);
  148.             msg($sqlinit);
  149.         }
  150.     } else {
  151.         msg("ok");
  152.     }
  153. }
  154.  
  155. // con_sequence update
  156.  
  157. #print '<html><body style="font-family: Verdana; font-size:11px;">';
  158. // installer starts here
  159. #print "<b>PLUGIN INSTALLER</b><br><br>\n";
  160. $data['top'] .= "<b>PLUGIN INSTALLER</b><br><br>\n";
  161.  
  162. $clink = "<br><a href=\"../../index.php?contenido=$contenido\">Switch to backend</a>\r\n";
  163.  
  164. if ($installsql = file_get_contents('install.sql')) {
  165.     // get info from sql file
  166.     if (preg_match ("/####(.*)####/i", $installsql, $pinfo)) {
  167.         $pinfo = explode (";", $pinfo[1]);
  168.         // take some nice names easier to work with...
  169.         $pname       = $pinfo[0];
  170.         $pversion    = $pinfo[1];
  171.         $pauthor     = $pinfo[2];
  172.         $pinternalid = $pinfo[3];
  173.  
  174.         unset($pinfo);
  175.         // first show info
  176.         $data['content'] .= "Plugin Name: " . $pname . "<br>\n";
  177.         $data['content'] .= "Plugin Version: " . $pversion . "<br>\n";
  178.         $data['content'] .= "Author: " . $pauthor . "<br>\n";
  179.         $data['content'] .= "Internal ID: " . $pinternalid . "<br>\n";
  180.         // the user don't need this info...
  181.         $installsql = preg_replace ("/####(.*)####/i", "", $installsql);
  182.  
  183.         $pstatus = true;
  184.     } else {
  185.         $data['content'] .= "Info missing. First line of install.sql should include following line:<br>";
  186.         $data['content'] .= "<b>####NAME;VERSION;AUTHOR;INTERNAL_ID####</b><br>";
  187.         $data['content'] .= "No further action takes place<br>";
  188.  
  189.         $pstatus = false;
  190.     }
  191.  
  192.     // check if idinternal is allready available in table
  193.     $sql = "SELECT * FROM " . $cfg["tab"]["plugins"] . " WHERE idinternal='" . $pinternalid . "';";
  194.     $db->query($sql);
  195.     if ($db->next_record()) {
  196.         $mode     = "update";
  197.         $message .= "Plugin with this internal id allready exists in table.<br>\n";
  198.         if ($pversion == $db->f('version')) {
  199.             $message .= "This version is allready installed.<br>\n";
  200.             $mode     = "uninstall";
  201.         } else {
  202.             $message .= "Switching to upgrade mode.<br>\n";
  203.         }
  204.         $pluginid = $db->f('idplugin');
  205.     } else {
  206.         $mode     = "install";
  207.         $message .= "No plugin with this internal id exists in table.<br>\n";
  208.         $pluginid = false;
  209.     }
  210.  
  211.     if (!$install && !$uninstall) {
  212.         $data['content'] .= "<br>" . $message;
  213.     }
  214.  
  215.     if (!$install && $mode == "update") {
  216.         $data['content'] .= "<br><a href=$PHP_SELF?install=1&contenido=$contenido>Update $pname $pversion</a></br>";
  217.     }
  218.  
  219.     if (!$install && $mode == "install") {
  220.         $data['content'] .= "<br><a href=$PHP_SELF?install=1&contenido=$contenido>Install $pname $pversion</a></br>";
  221.     }
  222.  
  223.     if (!$uninstall && $mode == "uninstall") {
  224.         $data['content'] .= "<br><a href=$PHP_SELF?uninstall=1&contenido=$contenido>UnInstall $pname $pversion</a></br>";
  225.     }
  226.  
  227.     if ($uninstall && $pluginid) {
  228.         $sql = "SELECT uninstall FROM " . $cfg["tab"]["plugins"] . " WHERE idplugin='" . $pluginid . "'";
  229.         msg($sql);
  230.         $db->query($sql);
  231.         $db->next_record();
  232.  
  233.         $uninstallsql = $db->f('uninstall');
  234.         $sql_data     = remove_remarks($uninstallsql);
  235.         $sql_pieces   = split_sql_file($sql_data, ';');
  236.  
  237.         msg(count($sql_pieces)." queries", "Executing:");
  238.         foreach ($sql_pieces as $sqlinit) {
  239.             $db->query($sqlinit);
  240.             msg($sqlinit);
  241.         }
  242.  
  243.         uninstallModRewrite();
  244.  
  245.         $data['content'] .= "<br><b>Uninstall complete.</b><br>\r\n";
  246.     }
  247.  
  248.     if ($pstatus && $install) {
  249.         if ($mode == "install") { // insert all data from install.sql
  250.             $pluginid = $db->nextid($cfg["tab"]["plugins"]); // get next free id using phplib method
  251.  
  252.             $PID = 100 + $pluginid; // generate !PID! replacement
  253.             $replace = array('!PREFIX!' => $cfg['sql']['sqlprefix'], '!PID!' => $PID);
  254.  
  255.             $installsql = strtr($installsql, $replace);
  256.  
  257.             $sql = "INSERT INTO " . $cfg["tab"]["plugins"] . " (idplugin,name,`version`,author,idinternal,`status`,`date`) VALUES ('" . $pluginid . "','" . $pname . "','" . $pversion . "','" . $pauthor . "','" . $pinternalid . "','0','" . date("Y-m-d H:i:s") . "');";
  258.             $uninstallsql = "DELETE FROM " . $cfg["tab"]["plugins"] . " WHERE idplugin='" . $pluginid . "';\r\n";
  259.             msg($sql, "Insert statement for plugin: ");
  260.             $db->query($sql);
  261.  
  262.             msg ($installsql, "Install query:");
  263.  
  264.             $sql_data = remove_remarks($installsql);
  265.             $sql_pieces = split_sql_file($sql_data, ';');
  266.             msg(count($sql_pieces) . " queries", "Executing:");
  267.             foreach ($sql_pieces as $sqlinit) {
  268.                 // $sqlinit = strtr($sqlinit, $replace);
  269.                 // create uninstall.sql for each insert entry
  270.                 if (preg_match("/INSERT\s+INTO\s+(.*)\s+VALUES\s*\([´\"'\s]*(\d+)/i", $sqlinit, $tmpsql)) {
  271.                     $tmpidname = $db->metadata(trim(str_replace("`", "", $tmpsql[1])));
  272.                     $tmpidname = $tmpidname[0]['name'];
  273.                     $uninstallsql = "DELETE FROM " . trim($tmpsql[1]) . " WHERE " . $tmpidname . "='" . trim($tmpsql[2]) . "';\r\n" . $uninstallsql;
  274.                 }
  275.  
  276.                 $db->query($sqlinit);
  277.                 msg($sqlinit);
  278.             }
  279.  
  280.             if ($uninstallsqlfile = file_get_contents('uninstall.sql')) {
  281.                 $uninstallsqlfile = remove_remarks($uninstallsqlfile); // remove all comments
  282.  
  283.                 $uninstallsql .= strtr($uninstallsqlfile, $replace); // add to generated sql
  284.                 $data['content'] .= "I found uninstall.sql in " . dirname(__FILE__) . "<br>Statements added to uninstall query.<br>\n";
  285.             }
  286.  
  287.             msg ($uninstallsql, "Uninstall query:");
  288.  
  289.             $sql = "UPDATE " . $cfg["tab"]["plugins"] . " SET install=0x" . bin2hex($installsql) . ", uninstall=0x" . bin2hex($uninstallsql) . " WHERE (idplugin='" . $pluginid . "');";
  290.             msg($sql, "un/install statements stored");
  291.             $db->query($sql);
  292.  
  293.             upgradeModRewrite();
  294.  
  295.             $data['content'] .= "<br><b>Install complete.</b><br>\r\n";
  296.         }
  297.  
  298.         if ($mode == "update") {
  299.             $sql  = "UPDATE " . $cfg["tab"]["plugins"] . " SET\n";
  300.             $sql .= " version = '" . $pversion . "'\n";
  301.             $sql .= "WHERE (idplugin='" . $pluginid . "');";
  302.             msg($sql, "Store new plugin version: ");
  303.             $db->query($sql);
  304.             if ($updatesqlfile = @file_get_contents('update.sql')) {
  305.                 $sql = "SELECT uninstall FROM " . $cfg["tab"]["plugins"] . " WHERE idplugin='" . $pluginid . "'";
  306.                 msg($sql, "Getting stored uninstall statements: ");
  307.                 $db->query($sql);
  308.                 $db->next_record();
  309.  
  310.                 $uninstallsql = $db->f('uninstall');
  311.                 $updatesqlfile = remove_remarks($updatesqlfile); // remove all comments
  312.  
  313.                 $data['content'] .= "I found update.sql in " . dirname(__FILE__) . "<br>\n";
  314.  
  315.                 $PID = 100 + $pluginid; // generate !PID! replacement
  316.                 $replace = array('!PREFIX!' => $cfg['sql']['sqlprefix'], '!PID!' => $PID);
  317.                 $updatesql .= strtr($updatesqlfile, $replace); // add to generated sql
  318.  
  319.                 $sql_pieces = split_sql_file($updatesql, ';');
  320.                 msg(count($sql_pieces) . " queries", "Executing:");
  321.                 foreach ($sql_pieces as $sqlinit) {
  322.                     // $sqlinit = strtr($sqlinit, $replace);
  323.                     // create uninstall.sql for each insert entry
  324.                     if (preg_match("/INSERT\s+INTO\s+(.*)\s+VALUES\s*\([´\"'\s]*(\d+)/i", $sqlinit, $tmpsql)) {
  325.                         $tmpidname    = $db->metadata(trim(str_replace("`", "", $tmpsql[1])));
  326.                         $tmpidname    = $tmpidname[0]['name'];
  327.                         $uninstallsql = "DELETE FROM " . trim($tmpsql[1]) . " WHERE " . $tmpidname . "='" . trim($tmpsql[2]) . "';\r\n" . $uninstallsql;
  328.                     } else if (preg_match("/REPLACE \s+INTO\s+(.*)\s+VALUES\s*\([´\"'\s]*(\d+)/i", $sqlinit, $tmpsql)) {
  329.                         $tmpidname    = $db->metadata(trim(str_replace("`", "", $tmpsql[1])));
  330.                         $tmpidname    = $tmpidname[0]['name'];
  331.                         $uninstallsql = "DELETE FROM " . trim($tmpsql[1]) . " WHERE " . $tmpidname . "='" . trim($tmpsql[2]) . "';\r\n" . $uninstallsql;
  332.                     }
  333.  
  334.                     $db->query($sqlinit);
  335.                     msg($sqlinit);
  336.                 }
  337.                 $sql = "UPDATE " . $cfg["tab"]["plugins"] . " SET uninstall = 0x" . bin2hex($uninstallsql) . " WHERE (idplugin='" . $pluginid . "');";
  338.                 msg($sql, "New uninstall statements stored: ");
  339.                 $db->query($sql);
  340.             }
  341.  
  342.             upgradeModRewrite();
  343.  
  344.             $data['content'] .= "<br><b>Update complete.</b><br>\r\n";
  345.         }
  346.  
  347.         // con_sequence update
  348.         updateSequence();
  349.     }
  350. } else {
  351.     $data['content'] .= "Sorry i found no install.sql in " . dirname(__FILE__) . "<br>\n";
  352. }
  353.  
  354.  
  355. // print $clink;
  356.  
  357. #print '</body></html>';
  358.  
  359. $cfg["debug"]["backend_exectime"]["end"] = getmicrotime();
  360.  
  361. if ($cfg["debug"]["rendering"] == true) {
  362.     $data['body_bottom'] .= "Rendering this page took: " . ($cfg["debug"]["backend_exectime"]["end"] - $cfg["debug"]["backend_exectime"]["start"]) . " seconds<br>";
  363.     $data['body_bottom'] .= "Building the complete page took: " . ($cfg["debug"]["backend_exectime"]["end"] - $cfg["debug"]["backend_exectime"]["fullstart"]) . " seconds<br>";
  364.  
  365.     if (function_exists("memory_get_usage")) {
  366.         $data['body_bottom'] .= "Include memory usage: " . human_readable_size(memory_get_usage() - $cfg["debug"]["oldmemusage"]) . "<br>";
  367.         $data['body_bottom'] .= "Complete memory usage: " . human_readable_size(memory_get_usage()) . "<br>";
  368.     }
  369. }
  370.  
  371.  
  372. page_close();
  373.  
  374.  
  375. ####################################################################################################
  376. ##### Output
  377.  
  378. echo <<<HTML
  379. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  380. <head>
  381.     <title>Plugin installer</title>
  382. </head>
  383. <body style="font-family:verdana;font-size:11px;">
  384. <form name="client_properties" method="post" action="$PHP_SELF">
  385.     <input type="hidden" name="contenido" value="$contenido">
  386. {$data['top']}<br>
  387. {$data['content']}
  388. {$data['bottom']}
  389. </form>
  390. {$data['body_bottom']}
  391. </body>
  392. </html>
  393. HTML;
  394.  
  395.  
  396. ####################################################################################################
  397. ##### Functions
  398.  
  399. // some functions to work with...
  400. /**
  401.  * removes '# blabla...' from the mysql_dump.
  402.  * This function was originally developed for phpbb 2.01
  403.  * (C) 2001 The phpBB Group http://www.phpbb.com
  404.  *
  405.  * @return string input_without_#
  406.  */
  407. function remove_remarks($sql) {
  408.     $lines = explode("\n", $sql);
  409.     // try to keep mem. use down
  410.     $sql = "";
  411.  
  412.     $linecount = count($lines);
  413.     $output = "";
  414.  
  415.     for ($i = 0; $i < $linecount; $i++) {
  416.         if (($i != ($linecount - 1)) || (strlen($lines[$i]) > 0)) {
  417.             $output .= ($lines[$i][0] != "#")  ? $lines[$i]."\n" : "\n";
  418.  
  419.             // Trading a bit of speed for lower mem. use here.
  420.             $lines[$i] = "";
  421.         }
  422.     }
  423.     return $output;
  424. }
  425.  
  426. /**
  427.  * Splits sql- statements into handy pieces.
  428.  * This function was original developed for the phpbb 2.01
  429.  * (C) 2001 The phpBB Group http://www.phpbb.com
  430.  *
  431.  * @return array sql_pieces
  432.  */
  433. function split_sql_file($sql, $delimiter) {
  434.   // Split up our string into "possible" SQL statements.
  435.   $tokens = explode($delimiter, $sql);
  436.   // try to save mem.
  437.   $sql = "";
  438.   $output = array();
  439.   // we don't actually care about the matches preg gives us.
  440.   $matches = array();
  441.   // this is faster than calling count($oktens) every time thru the loop.
  442.   $token_count = count($tokens);
  443.   for ($i = 0; $i < $token_count; $i++) {
  444.     // Dont wanna add an empty string as the last thing in the array.
  445.     if (($i != ($token_count - 1)) || (strlen($tokens[$i] > 0))) {
  446.       // This is the total number of single quotes in the token.
  447.       $total_quotes = preg_match_all("/'/", $tokens[$i], $matches);
  448.       // Counts single quotes that are preceded by an odd number of backslashes,
  449.       // which means they're escaped quotes.
  450.       $escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$i], $matches);
  451.  
  452.       $unescaped_quotes = $total_quotes - $escaped_quotes;
  453.       // If the number of unescaped quotes is even, then the delimiter did NOT occur inside a string literal.
  454.       if (($unescaped_quotes % 2) == 0) {
  455.         // It's a complete sql statement.
  456.         $output[] = $tokens[$i];
  457.         // save memory.
  458.         $tokens[$i] = "";
  459.       } else {
  460.         // incomplete sql statement. keep adding tokens until we have a complete one.
  461.         // $temp will hold what we have so far.
  462.         $temp = $tokens[$i] . $delimiter;
  463.         // save memory..
  464.         $tokens[$i] = "";
  465.         // Do we have a complete statement yet?
  466.         $complete_stmt = false;
  467.  
  468.         for ($j = $i + 1; (!$complete_stmt && ($j < $token_count)); $j++) {
  469.           // This is the total number of single quotes in the token.
  470.           $total_quotes = preg_match_all("/'/", $tokens[$j], $matches);
  471.           // Counts single quotes that are preceded by an odd number of backslashes,
  472.           // which means theyre escaped quotes.
  473.           $escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$j], $matches);
  474.  
  475.           $unescaped_quotes = $total_quotes - $escaped_quotes;
  476.  
  477.           if (($unescaped_quotes % 2) == 1) {
  478.             // odd number of unescaped quotes. In combination with the previous incomplete
  479.             // statement(s), we now have a complete statement. (2 odds always make an even)
  480.             $output[] = $temp . $tokens[$j];
  481.             // save memory.
  482.             $tokens[$j] = "";
  483.             $temp = "";
  484.             // exit the loop.
  485.             $complete_stmt = true;
  486.             // make sure the outer loop continues at the right point.
  487.             $i = $j;
  488.           } else {
  489.             // even number of unescaped quotes. We still dont have a complete statement.
  490.             // (1 odd and 1 even always make an odd)
  491.             $temp .= $tokens[$j] . $delimiter;
  492.             // save memory.
  493.             $tokens[$j] = "";
  494.           }
  495.         } // for..
  496.       } // else
  497.     }
  498.   }
  499.   return $output;
  500. }
  501. // simple function to update con_sequence
  502. function updateSequence($table = false) {
  503.     global $db, $cfg;
  504.     if (!$table) {
  505.         $sql = "SHOW TABLES";
  506.         $db->query($sql);
  507.         while ($db->next_record()) {
  508.             dbUpdateSequence($cfg['sql']['sqlprefix'] . "_sequence", $db->f(0));
  509.         }
  510.     } else {
  511.         dbUpdateSequence($cfg['sql']['sqlprefix'] . "_sequence", $table);
  512.     }
  513. }
  514. // read out next free id * deprecated
  515. function getSequenceId($table) {
  516.     global $db2, $cfg;
  517.     $sql = "SELECT nextid FROM " . $cfg['sql']['sqlprefix'] . "_sequence" . " where seq_name = '$table'";
  518.     $db2->query($sql);
  519.     if ($db2->next_record()) {
  520.         return ($db2->f("nextid") + 1);
  521.     } else {
  522.         msg($table, "missing in " . $cfg['sql']['sqlprefix'] . "_sequence");
  523.         return 0;
  524.     }
  525. }
  526. // debug functions
  527. function msg($value, $info = false) {
  528.     global $cfg;
  529.     if (trim($cfg["debug"]["messages"]) == "") $cfg["debug"]["messages"] = "<br><b>DEBUG:</b>";
  530.     if (!$cfg["debug"]["installer"]) {
  531.         return;
  532.     }
  533.     if ($info) {
  534.         $cfg["debug"]["messages"] .= "<b>$info</b> -> ";
  535.     }
  536.     if (is_array($value)) {
  537.         ob_start();
  538.         print_r($value);
  539.         $output = ob_get_contents();
  540.         ob_end_clean();
  541.         $cfg["debug"]["messages"] .= "<pre>" . htmlspecialchars($output) . "</pre>";
  542.     } else {
  543.         $cfg["debug"]["messages"] .= htmlspecialchars($value) . "<br>";
  544.     }
  545. }
  546.  
  547. function showmsg() {
  548.     global $cfg;
  549.     if ($cfg["debug"]["installer"]) {
  550.         print "<div style=\"font-family: Verdana, Arial, Helvetica, Sans-Serif; font-size: 11px; color: #000000\">";
  551.         print $cfg["debug"]["messages"];
  552.         print "</div>";
  553.     }
  554. }
  555.  
  556.  
  557. /**
  558.  * isWriteable:
  559.  * Checks if a specific file is writeable. Includes a PHP 4.0.4
  560.  * workaround where is_writable doesn't return a value of type
  561.  * boolean. Also clears the stat cache and checks if the file
  562.  * exists.
  563.  *
  564.  * Copied from /setup/lib/functions.filesystem.php
  565.  *
  566.  * @param $file string    Path to the file, accepts absolute and relative files
  567.  * @return boolean true if the file exists and is writeable, false otherwise
  568.  */
  569. function isWriteable($file) {
  570.     clearstatcache();
  571.     if (!file_exists($file)) {
  572.         return false;
  573.     }
  574.  
  575.     $bStatus = is_writable($file);
  576.     /* PHP 4.0.4 workaround */
  577.     settype($bStatus, "boolean");
  578.  
  579.     return $bStatus;
  580. }
  581.  
  582.  
  583. function copyFile($source, $destination, $backupName=null) {
  584.     global $cfg;
  585.  
  586.     // check source and destination, allow filesystem processes only inside htdocs
  587.     if (strpos($source, $cfg['path']['frontend']) === false) {
  588.         return false;
  589.     } elseif (strpos($destination, $cfg['path']['frontend']) === false) {
  590.         return false;
  591.     } elseif (isset($backupName) && strpos($backupName, $cfg['path']['frontend']) === false) {
  592.         return false;
  593.     }
  594.  
  595.     if ($backupName !== null) {
  596.         if (!rename($destination, $backupName)) {
  597.             return false;
  598.         }
  599.     }
  600.  
  601.     if (!copy($source, $destination . '.bak')) {
  602.         return false;
  603.     }
  604.  
  605.     return true;
  606. }

Documentation generated on Tue, 12 Aug 2008 00:06:47 +0200 by phpDocumentor 1.4.0