plugins/PunBB.php
changeset 2 a8a21e1c7afa
parent 1 8f6143115bf5
child 3 c0c445d4a13e
equal deleted inserted replaced
1:8f6143115bf5 2:a8a21e1c7afa
    28       \'name\'=>\'Forum\',
    28       \'name\'=>\'Forum\',
    29       \'urlname\'=>\'Forum\',
    29       \'urlname\'=>\'Forum\',
    30       \'namespace\'=>\'Special\',
    30       \'namespace\'=>\'Special\',
    31       \'special\'=>0,\'visible\'=>1,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\',
    31       \'special\'=>0,\'visible\'=>1,\'comments_on\'=>0,\'protected\'=>1,\'delvotes\'=>0,\'delvote_ips\'=>\'\',
    32     ));
    32     ));
       
    33     
    33   ');
    34   ');
    34 
    35 
       
    36 $plugins->attachHook('session_started', '
       
    37     $pid = RenderMan::strToPageId($paths->get_pageid_from_url());
       
    38 
       
    39     if ( getConfig("punbb_installed") == "yes" && getConfig("punbb_version") == PUNANO_VERSION && $pid[0] == "Forum" && $pid[1] == "Special" )
       
    40     {
       
    41       require( "punbb/include/common.php" );
       
    42     }
       
    43     ');
       
    44 
    35 define('PUNANO_VERSION', '0.1.12.15');
    45 define('PUNANO_VERSION', '0.1.12.15');
       
    46 define('PUNBB_VERSION',  '1.2.15');
       
    47 define('PUN_ROOT', ENANO_ROOT . '/punbb/');
       
    48 define('PUN', '');
       
    49 define('PUN_DISABLE_BUFFERING', '');
    36 
    50 
    37 function page_Special_Forum()
    51 function page_Special_Forum()
    38 {
    52 {
    39   if ( getConfig('punbb_installed') != 'yes' )
    53   if ( getConfig('punbb_installed') != 'yes' )
    40   {
    54   {
    61   else if ( $session->auth_level < USER_LEVEL_ADMIN )
    75   else if ( $session->auth_level < USER_LEVEL_ADMIN )
    62   {
    76   {
    63     $url = makeUrlNS('Special', 'Login/' . $paths->page, 'level=' . USER_LEVEL_ADMIN, true);
    77     $url = makeUrlNS('Special', 'Login/' . $paths->page, 'level=' . USER_LEVEL_ADMIN, true);
    64     redirect($url, 'Permission denied', 'You need to have an active high-privilege session to set up Punano.', 4);
    78     redirect($url, 'Permission denied', 'You need to have an active high-privilege session to set up Punano.', 4);
    65   }
    79   }
       
    80   
       
    81   $template->header();
       
    82   
    66   // Permissions are good
    83   // Permissions are good
       
    84   if ( isset($_POST['do_install']) )
       
    85   {
       
    86     $result = _punano_perform_install();
       
    87     if ( $result )
       
    88     {
       
    89       echo '<p>PunBB installation has succeeded.</p>';
       
    90       echo '<p><b><a href="' . makeUrlNS('Special', 'Forum') . '">Take me to my forum!</a></b></p>';
       
    91     }
       
    92   }
       
    93   else
       
    94   {
       
    95     $url = makeUrlNS('Special', 'Forum');
       
    96     ?>
       
    97     <form action="<?php echo $url; ?>" method="post">
       
    98       <p><b>Before Punano can be used, you need to install the database.</b></p>
       
    99       <p>This process will create several new tables in your database, and then fill them in with a default configuration for PunBB.
       
   100          You should only continue if you have CREATE TABLE and CREATE INDEX privileges on your database.</p>
       
   101       <p><input type="submit" style="font-weight: bold;" name="do_install" value="Install PunBB" /></p>
       
   102     </form>
       
   103     <?php
       
   104   }
       
   105   
       
   106   $template->footer();
       
   107   
       
   108 }
       
   109 
       
   110 function _punano_perform_install()
       
   111 {
       
   112   global $db, $session, $paths, $template, $plugins; // Common objects
       
   113   $db_prefix = table_prefix . 'pun_';
       
   114   $admin_email = getConfig('contact_email');
       
   115   $pun_version = PUNBB_VERSION;
       
   116   
       
   117   $schema = file_get_contents( ENANO_ROOT . '/punbb/schema.sql' );
       
   118   if ( empty($schema) )
       
   119   {
       
   120     echo 'ERROR: cannot load schema file!';
       
   121     return false;
       
   122   }
       
   123   
       
   124   $replace = array(
       
   125       '{{TABLE_PREFIX}}' => $db_prefix,
       
   126       '{{ENANO_ADMIN_EMAIL}}' => $admin_email,
       
   127       '{{PUN_VERSION}}' => $pun_version
       
   128     );
       
   129   
       
   130   $schema = strtr($schema, $replace);
       
   131   
       
   132   // Build an array of queries (from Enano's install.php)
       
   133   $schema = explode("\n", $schema);
       
   134   
       
   135   foreach ( $schema as $i => $sql )
       
   136   {
       
   137     $query =& $schema[$i];
       
   138     $t = trim($query);
       
   139     if ( empty($t) || preg_match('/^(\#|--)/i', $t) )
       
   140     {
       
   141       unset($schema[$i]);
       
   142       unset($query);
       
   143     }
       
   144   }
       
   145   
       
   146   $schema = array_values($schema);
       
   147   $schema = implode("\n", $schema);
       
   148   $schema = explode(";\n", $schema);
       
   149   
       
   150   foreach ( $schema as $i => $sql )
       
   151   {
       
   152     $query =& $schema[$i];
       
   153     if ( substr($query, ( strlen($query) - 1 ), 1 ) != ';' )
       
   154     {
       
   155       $query .= ';';
       
   156     }
       
   157     if ( !$db->check_query($query) )
       
   158     {
       
   159       echo 'ERROR: Query safety check failed.<pre>' . $query . '</pre>';
       
   160     }
       
   161   }
       
   162   
       
   163   foreach ( $schema as $query )
       
   164   {
       
   165     if ( !$db->sql_query($query) )
       
   166     {
       
   167       echo $db->get_error();
       
   168       return false;
       
   169     }
       
   170   }
       
   171   
       
   172   // Insert users
       
   173   $q = $db->sql_query('SELECT user_id FROM '.table_prefix.'users WHERE user_id > 1;');
       
   174   if ( !$q )
       
   175   {
       
   176     echo $db->get_error();
       
   177     return false;
       
   178   }
       
   179   $uid_list = array();
       
   180   while ( $row = $db->fetchrow_num() )
       
   181   {
       
   182     $uid_list[] = $row[0];
       
   183   }
       
   184   $query = 'INSERT INTO '.table_prefix.'pun_users(id) VALUES(' . implode('),(', $uid_list) . ');';
       
   185   
       
   186   if ( !$db->sql_query($query) )
       
   187   {
       
   188     echo $db->get_error();
       
   189     return false;
       
   190   }
       
   191   
       
   192   setConfig('punbb_installed', 'yes');
       
   193   setConfig('punbb_version', PUNANO_VERSION);
       
   194   
       
   195   return true;
       
   196   
    67 }
   197 }
    68 
   198 
    69 function punano_upgrade()
   199 function punano_upgrade()
    70 {
   200 {
    71   global $db, $session, $paths, $template, $plugins; // Common objects
   201   global $db, $session, $paths, $template, $plugins; // Common objects
    74 
   204 
    75 function punano_main()
   205 function punano_main()
    76 {
   206 {
    77   global $db, $session, $paths, $template, $plugins; // Common objects
   207   global $db, $session, $paths, $template, $plugins; // Common objects
    78   
   208   
       
   209   // At this point, the PunBB API is already loaded
       
   210   // So we'll include one of the Pun frontend files
       
   211   
       
   212   $valid = array('delete', 'edit', 'extern', 'help', 'index', 'misc', 'moderate', 'post', 'profile', 'search', 'userlist', 'viewforum', 'viewtopic');
       
   213   
       
   214   $file = 'index';
       
   215   if ( $x = $paths->getParam(0) )
       
   216   {
       
   217     $x = preg_replace('/\.php$/', '', $x);
       
   218     if ( in_array(strtolower($x), $valid) )
       
   219     {
       
   220       $file = strtolower($x);
       
   221     }
       
   222   }
       
   223   
       
   224   // Don't worry. This is sanitized.
       
   225   require PUN_ROOT . $file . '.php';
       
   226   
    79 }
   227 }
    80 
   228 
    81 ?>
   229 ?>