<?php if (!defined('PmWiki')) exit();
/*  Copyright 2005 Patrick R. Michaud (pmichaud@pobox.com)
    This file is searchterms.php; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published
    by the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This script enables the access to a mysqldatabase.
    To use this script, simply place it in the cookbook/ directory and add the following lines
    $MyPmWiki_Hostname = '<hostname>';
    $MyPmWiki_Username = '<mysqluser>';
    $MyPmWiki_Password = '<mysqluserpassword>';
    $MyPmWiki_DBName = '<mysqldatabase>';
    $MyPmWiki_TableFmt = '';
    include_once('cookbook/mypmwiki.php');

    to a local customization file.

    This script is based on ideas by JamesDavis, but has 
    been rewritten to work wiht PmWiki v2.

    Version 0.2
      change Markup vom _begin to inline
    Version 2019-01-26 (Siegfried Seibert)
      update for PHP 7.2 compatibility

*/
$RecipeInfo['MyPmWiki']['Version'] = '2019-01-26';

if($GLOBALS['VersionNum'] < 2002056) {
    # we want the cookbook to also work with older PmWiki versions
    Markup('MyPmWiki','inline','/\(:MyPmWiki *(.+):\)/e',"MyPmWiki('\$1')");
} else {
    Markup('MyPmWiki','inline','/\(:MyPmWiki *(.+):\)/',"MarkupMyPmWiki");
}
function MarkupMyPmWiki($m) {
    return MyPmWiki($m[1]);
}

function MyPmWiki($SQL) {
  global $MyPmWiki_Hostname, $MyPmWiki_Username, $MyPmWiki_Password, $MyPmWiki_DBName, $MyPmWiki_TableFmt;
  global $action;
  global $FarmD;
  $ExtraErrMsg = "Please contact the administrator of this wiki.";
  if (!isset($MyPmWiki_Hostname))
    $MyPmWiki_Hostname = "localhost";
  if (!isset($MyPmWiki_TableFmt))
    $MyPmWiki_TableFmt = "border='1'";
  if (!isset($MyPmWiki_Username))
    return "<font color='red'><strong>Error:</strong> \$MyPmWiki_Username not defined. $ExtraErrMsg</font>";
  if (!isset($MyPmWiki_Password))
    return "<font color='red'><strong>Error:</strong> \$MyPmWiki_Password not defined. $ExtraErrMsg</font>";
  if (!isset($MyPmWiki_DBName))
    return "<font color='red'><strong>Error:</strong> \$MyPmWiki_DBName not defined. $ExtraErrMsg</font>";
  if (!$dbh = mysqli_connect ($MyPmWiki_Hostname, $MyPmWiki_Username, $MyPmWiki_Password))
    return "<font color='red'><strong>Connection Error:</strong> ".mysqli_error($dbh)." $ExtraErrMsg</font>";;
  if(!mysqli_select_db ($dbh,$MyPmWiki_DBName))
    $resultStr = "<font color='red'><strong>Error:</strong> ".mysqli_error($dbh)." $ExtraErrMsg</font>";
  elseif($result = mysqli_query($dbh,$SQL)) {
    $resultStr = "<table $MyPmWiki_TableFmt><tr>";
    for ($i = 0; $i < mysqli_num_fields($result); $i++) {
      $resultStr .= "<th>".mysqli_fetch_field_direct($result, $i)->name."</th>";
    }
    $resultStr .= "</tr>";
    while($row = mysqli_fetch_row($result)) {
      $resultStr .= "<tr>";
      foreach ($row as $field)
        $resultStr .= "<td>$field</td>";
      $resultStr .= "</tr>";
    }
    $resultStr .= "</table>";
  }
  else
    $resultStr = "<font color='red'><strong>SQL Error:</strong> ".mysqli_error($dbh)."</font>";
  mysqli_close($dbh);
  return $resultStr;
}

?>