plugins/PunBB.php
author Dan
Sun, 15 Jun 2008 01:42:31 -0400
changeset 9 a932ce8c4827
parent 8 8baccbad4a55
permissions -rwxr-xr-x
More progress, compatibility updates for 1.1.4, etc.

<?php
/*
Plugin Name: PunBB forum integration
Plugin URI: http://enanocms.org/PunBB_plugin
Description: Provides a complete forum solution in PunBB, a separately maintained and very lightweight GPL forum.
Author: Dan Fuhry
Version: 1.3-beta
Author URI: http://enanocms.org/
*/

/*
 * PunBB Plugin for Enano CMS
 * Version 1.3-beta
 * Copyright (C) 2006-2008 Dan Fuhry
 * Copyright (C) 2002-2008 Rickard Andersson
 *
 * This program is Free Software; you can redistribute 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 program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
 */

$plugins->attachHook('base_classes_initted', '
    global $paths;
    
    $paths->add_page(Array(
      \'name\'=>\'Forum\',
      \'urlname\'=>\'Forum\',
      \'namespace\'=>\'Special\',
      \'special\'=>0,\'visible\'=>1,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\',
    ));
    
  ');

$plugins->attachHook('session_started', '
    $pid = RenderMan::strToPageId($paths->get_pageid_from_url());

    $is_style_or_script = ( ( strpos($_SERVER["REQUEST_URI"], "/style/") || strpos($_SERVER["REQUEST_URI"], "/include/js/") || strpos($_SERVER["REQUEST_URI"], "/img/") ) && !strpos($_SERVER["REQUEST_URI"], "/help/") );
    if ( getConfig("punbb_installed") == "yes" && getConfig("punbb_version") == PUNANO_VERSION && $pid[0] == "Forum" && $pid[1] == "Special" && !$is_style_or_script )
    {
      require( "punbb/include/common.php" );
    }
    ');

$plugins->attachHook('user_registered', 'pun_handle_reg_event($user_id, $username);');

define('PUNANO_VERSION', '0.1.3-beta');
define('PUNBB_VERSION',  '1.3-beta');
define('PUN_ROOT', ENANO_ROOT . '/punbb/');
define('PUN', '');
define('PUN_DISABLE_BUFFERING', '');
// define('PUN_SHOW_QUERIES', '');

function page_Special_Forum()
{
  global $db, $session, $paths, $template, $plugins; // Common objects
  if ( getConfig('punbb_installed') != 'yes' )
  {
    punano_installer();
  }
  else if ( getConfig('punbb_version') != PUNANO_VERSION )
  {
    punano_upgrade();
  }
  else if ( $paths->getParam(0) == 'admin' )
  {
    punano_admin_console();
  }
  else if ( ( $paths->getParam(0) == 'style' ) || ( $paths->getParam(0) == 'include' && $paths->getParam(1) == 'js' ) || ( $paths->getParam(0) == 'img' ) )
  {
    $path = $paths->getAllParams();
    header('Location: ' . scriptPath . '/punbb/' . $path);
    exit();
  }
  else
  {
    punano_main();
  }
}

function punano_installer()
{
  global $db, $session, $paths, $template, $plugins; // Common objects
  // First check our permissions
  if ( $session->user_level < USER_LEVEL_ADMIN )
  {
    die_friendly('Punano initialization error', '<p>The Punano plugin doesn\'t have its database schema installed yet, and your user account doesn\'t have permission to install it. Please ask the administrator of this site to set up Punano.</p>');
  }
  else if ( $session->auth_level < USER_LEVEL_ADMIN )
  {
    $url = makeUrlNS('Special', 'Login/' . $paths->page, 'level=' . USER_LEVEL_ADMIN, true);
    redirect($url, 'Permission denied', 'You need to have an active high-privilege session to set up Punano.', 4);
  }
  
  $template->header();
  
  // Permissions are good
  if ( isset($_POST['do_install']) )
  {
    $result = _punano_perform_install();
    if ( $result )
    {
      echo '<p>PunBB installation has succeeded.</p>';
      echo '<p><b><a href="' . makeUrlNS('Special', 'Forum') . '">Take me to my forum!</a></b></p>';
    }
  }
  else
  {
    $url = makeUrlNS('Special', 'Forum');
    ?>
    <form action="<?php echo $url; ?>" method="post">
      <p><b>Before Punano can be used, you need to install the database.</b></p>
      <p>This process will create several new tables in your database, and then fill them in with a default configuration for PunBB.
         You should only continue if you have CREATE TABLE and CREATE INDEX privileges on your database.</p>
      <p><input type="submit" style="font-weight: bold;" name="do_install" value="Install PunBB" /></p>
    </form>
    <?php
  }
  
  $template->footer();
  
}

function _punano_perform_install()
{
  global $db, $session, $paths, $template, $plugins; // Common objects
  global $dbdriver;
  
  $db_prefix = table_prefix . 'pun_';
  $admin_email = getConfig('contact_email');
  $pun_version = PUNBB_VERSION;
  
  $schema = file_get_contents( ENANO_ROOT . "/punbb/install-$dbdriver.sql" );
  if ( empty($schema) )
  {
    echo 'ERROR: cannot load schema file!';
    return false;
  }
  
  $replace = array(
      '{{TABLE_PREFIX}}' => $db_prefix,
      '{{ENANO_ADMIN_EMAIL}}' => $admin_email,
      '{{PUN_VERSION}}' => $pun_version,
      '{{NOW}}' => strval(time())
    );
  
  $schema = strtr($schema, $replace);
  
  // Build an array of queries (from Enano's install.php)
  $schema = explode("\n", $schema);
  
  foreach ( $schema as $i => $sql )
  {
    $query =& $schema[$i];
    $t = trim($query);
    if ( empty($t) || preg_match('/^(\#|--)/i', $t) )
    {
      unset($schema[$i]);
      unset($query);
    }
  }
  
  $schema = array_values($schema);
  $schema = implode("\n", $schema);
  $schema = explode(";\n", $schema);
  
  foreach ( $schema as $i => $sql )
  {
    $query =& $schema[$i];
    if ( substr($query, ( strlen($query) - 1 ), 1 ) != ';' )
    {
      $query .= ';';
    }
    if ( !$db->check_query($query) )
    {
      echo 'ERROR: Query safety check failed.<pre>' . $query . '</pre>';
    }
  }
  
  foreach ( $schema as $query )
  {
    if ( !$db->sql_query($query) )
    {
      echo $db->get_error();
      return false;
    }
  }
  
  // Insert users
  $q = $db->sql_query('SELECT user_id, user_level FROM '.table_prefix.'users WHERE user_id > 1;');
  if ( !$q )
  {
    echo $db->get_error();
    return false;
  }
  $uid_list = array();
  while ( $row = $db->fetchrow_num() )
  {
    $g_id = 4;
    switch ( $row[1] )
    {
      case USER_LEVEL_ADMIN: $g_id = 1; break;
      case USER_LEVEL_MOD:   $g_id = 3; break;
    }
    $uid_list[] = "{$row[0]}, {$g_id}";
  }
  $query = 'INSERT INTO '.table_prefix.'pun_users(id, group_id) VALUES(' . implode('),(', $uid_list) . ');';
  
  if ( !$db->sql_query($query) )
  {
    echo $db->get_error();
    return false;
  }
  
  setConfig('punbb_installed', 'yes');
  setConfig('punbb_version', PUNANO_VERSION);
  
  return true;
  
}

function punano_upgrade()
{
  global $db, $session, $paths, $template, $plugins; // Common objects
  
}

function punano_main()
{
  global $db, $session, $paths, $template, $plugins; // Common objects
  
  // At this point, the PunBB API is already loaded
  // So we'll include one of the Pun frontend files
  
  /*
  $valid = array('delete', 'edit', 'extern', 'help', 'index', 'misc', 'moderate', 'post', 'profile', 'search', 'userlist', 'viewforum', 'viewtopic');
  
  $file = 'index';
  if ( $x = $paths->getParam(0) )
  {
    $x = preg_replace('/\.php$/', '', $x);
    if ( in_array(strtolower($x), $valid) )
    {
      $file = strtolower($x);
    }
  }
  */
  $file = 'rewrite';
  
  // Don't worry. This is sanitized.
  $file = PUN_ROOT . $file . '.php';
  require $file;
}

function punano_admin_console()
{
  global $db, $session, $paths, $template, $plugins; // Common objects
  
  $valid = array('bans', 'categories', 'censoring', 'extensions', 'forums', 'groups', 'index', 'options', 'prune', 'ranks', 'reindex', 'reports', 'users');
  $mod   = array();

  $file = 'index';
  
  if ( $x = $paths->getParam(1) )
  {
    $x = preg_replace('/\.php$/', '', $x);
    $x = preg_replace('/^admin_/i', '', $x);
    if ( in_array(strtolower($x), $valid) || in_array(strtolower($x), $mod) )
    {
      $file = strtolower($x);
    }
  }
  
  if ( in_array($file, $mod) )
  {
    $need_level = USER_LEVEL_MOD;
  }
  else
  {
    $need_level = USER_LEVEL_ADMIN;
  }

  if ( $session->auth_level < $need_level )
  {
    redirect( makeUrlNS('Special', 'Login/' . $paths->fullpage, 'level=' . $session->user_level, false), 'Permission denied', 'You need to have permission level ' . $session->userlevel_to_string($need_level) . ' to use the PunBB administration console.', 2);
  }
  
  // Don't worry. This is sanitized.
  require PUN_ROOT . 'admin/' . $file . '.php';
  
}

function pun_handle_reg_event($user_id, $username)
{
  global $db, $session, $paths, $template, $plugins; // Common objects
  if ( getConfig('punbb_installed') != 'yes' )
  {
    return false;
  }
  
  // register the new user in PunBB
  // at this point $username has been SQL-injection-proofed already by the session manager
  $q = $db->sql_query('INSERT INTO ' . table_prefix . "pun_users ( id, username ) VALUES ( $user_id, '$username' );");
  if ( !$q )
    $db->_die();
}

?>