Sample install.php

From GeeklogWiki
Jump to: navigation, search
<?php 
/* Reminder: always indent with 4 spaces (no tabs). */ 
// +---------------------------------------------------------------------------+ 
// | File Management Plugin v1.0 for Geeklog - The Ultimate Weblog             | 
// +---------------------------------------------------------------------------+ 
// | install.php                                                               | 
// | This is the main install and de-install program for the File Management   | 
// | Plugin. By default this program will automatically create all required    | 
// | Tables, groups, security rights and group assignments.                    | 
// |                                                                           | 
// | I have extended the standard PLUGIN Install script with a admin controls  | 
// | These are set in the install.cfg file. By default - all are set to true   | 
// | for auto install/deinstall operation. But if you are upgrading or had     | 
// | some problems, these switches let you control the install/deinstall       | 
// |                                                                           | 
// | Added display output to users on succes/failure and redirect to main page | 
// |                                                                           | 
// | Additionally, Check your error.log for process execution status           | 
// +---------------------------------------------------------------------------+ 
// | Copyright (C) 2000,2001 by the following authors:                         | 
// |                                                                           | 
// | Authors: Blaine Lang  <aka: efarmboy>     - langmail AT sympatico DOT ca  | 
// | August 2002                                                               | 
// +---------------------------------------------------------------------------+ 
// |                                                                           | 
// | This program is free software; 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 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 more details.                              | 
// |                                                                           | 
// | You should have received a copy of the GNU General Public License         | 
// | along with this program; if not, write to the Free Software Foundation,   | 
// | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           | 
// |                                                                           | 
// +---------------------------------------------------------------------------+ 
// 
// $Id: install.php,v 2.0 2002/08/23 18:22:02 blang Exp $ 

include_once('../../../lib-common.php'); 
include_once($_CONF['path'] . 'plugins/filemgmt/lang.php'); 
include_once($_CONF['path'] . 'plugins/filemgmt/config.php'); 
include_once($_CONF['path'] . 'plugins/filemgmt/install.cfg'); 
include_once($_CONF['path'] . 'plugins/filemgmt/functions.inc'); 

// Note: The de-install function is in the main plugin functions.inc file 
// This allows it to be called from the plugin admin page where you can enable/disable and delete the plugin 


// Only let Root users access this page 
if (!SEC_inGroup('Root')) { 
    // Someone is trying to illegally access this page 
    COM_errorLog($LANG_FILEMGMT['invalid_install'] . $_USER['uid'] . ", Username: {$_USER['username']}, IP: $REMOTE_ADDR",1); 
    $display = COM_siteHeader(); 
    $display .= COM_startBlock($LANG_FILEMGMT['access_denied']); 
    $display .= $LANG_FILEMGMT['access_denied_msg']; 
    $display .= COM_endBlock(); 
    $display .= COM_siteFooter(); 
    COM_output($display); 
    exit; 
} 

$steps = array(); 
/** 
* Puts the datastructures for this plugin into the Geeklog database 
* 
*/ 
function plugin_install_filemgmt() { 
    global $_TABLES, $_CONF, $LANG_FILEMGMT, $filemgmt_version; 
    global $filemgmt_createtables, $filemgmt_creategroups, $filemgmt_createrights, $filemgmt_assigngroups, $filemgmt_assignrights, $filemgmt_registerPI; 

    COM_errorLog($LANG_FILEMGMT['start_install'],1); 
     
    // Installs the File Management Tables 
    $steps['createtable'] = 1; 
    COM_errorLog("Attempting to create FileMgmt databases", 1); 
    if ($filemgmt_createtables) { 
        if (!plugin_createtables_filemgmt()) { 
            plugin_uninstall_filemgmt($steps); 
            return false; 
            exit; 
        } 
        COM_errorLog('...success',1); 
    } else { 
        COM_errorLog($LANG_FILEMGMT['install_skip'], 1); 
    } 
         
    // Create the FileMgmt security groups 
    COM_errorLog("Attempting to create FileMgmt groups", 1); 
    if ($filemgmt_creategroups) { 
        DB_query("INSERT INTO {$_TABLES['groups']} (grp_name, grp_descr) " 
            . "VALUES ('FileMgmt-Admin', 'Users in this group can administer the Filemgmt plugin')",1); 
        if (DB_error()) { 
            plugin_uninstall_filemgmt($steps); 
            return false; 
            exit; 
        } else { 
            COM_errorLog('About to save Filemgmt-Admin group_id to vars table for use during uninstall',1); 
            // Remove any existing records 
            DB_query("DELETE FROM {$_TABLES['vars']} WHERE name = 'fm_admingrp_id'"); 
            DB_query("INSERT INTO {$_TABLES['vars']} VALUES ('fm_admingrp_id', LAST_INSERT_ID())",1); 
            if (DB_error()) { 
                plugin_uninstall_filemgmt($steps); 
                return false; 
                exit; 
            } 
            COM_errorLog('...success',1); 
            $group1_id = DB_getItem($_TABLES['vars'], 'value', "name = 'fm_admingrp_id'"); 
            $steps['savedgroup1id'] = 1; 
        } 
        DB_query("INSERT INTO {$_TABLES['groups']} (grp_name, grp_descr) " 
            . "VALUES ('FileMgmt-Users', 'Users in this group can use the Filemgmt plugin')",1); 
        if (DB_error()) { 
            plugin_uninstall_filemgmt($steps); 
            return false; 
            exit; 
        }else { 
            COM_errorLog('About to save Filemgmt-Users group_id to vars table for use during uninstall',1); 
            // Remove any existing records 
            DB_query("DELETE FROM {$_TABLES['vars']} WHERE name = 'fm_usersgrp_id'"); 
            DB_query("INSERT INTO {$_TABLES['vars']} VALUES ('fm_usersgrp_id', LAST_INSERT_ID())",1); 
            if (DB_error()) { 
                plugin_uninstall_filemgmt($steps); 
                return false; 
                exit; 
            } 
            $group2_id = DB_getItem($_TABLES['vars'], 'value', "name = 'fm_usersgrp_id'"); 
            COM_errorLog('...success',1); 
            $steps['savedgroup2id'] = 1; 
        } 
        COM_errorLog('...success',1); 
        $steps['creategroups'] = 1; 
        COM_errorLog('Groups added successfully',1); 
    } else { 
        COM_errorLog($LANG_FILEMGMT['install_skip'], 1); 
    }     
             
    // Add Filemgmmt plugin security features 
    COM_errorLog('Attempting to create new Security Rights',1); 
    if ($filemgmt_createrights) { 
        COM_errorLog('Attempting to add filemgmt.edit feature',1); 
        DB_query("INSERT INTO {$_TABLES['features']} (ft_name, ft_descr) " 
            . "VALUES ('filemgmt.edit','Filemgmt plugin Administration')",1); 
        if (DB_error()) { 
            plugin_uninstall_filemgmt($steps); 
            return false; 
            exit; 
        } 
        $edit_id = DB_insertId(); 
         
        COM_errorLog('Attempting to add filemgmt.user feature',1); 
        DB_query("INSERT INTO {$_TABLES['features']} (ft_name, ft_descr) " 
            . "VALUES ('filemgmt.user','Filemgmt User Access')",1); 
        if (DB_error()) { 
            plugin_uninstall_filemgmt($steps); 
            return false; 
            exit; 
        } 
        $user_id = DB_insertId(); 
         
        COM_errorLog('Attempting to add filemgmt.upload feature',1); 
        DB_query("INSERT INTO {$_TABLES['features']} (ft_name, ft_descr) " 
            . "VALUES ('filemgmt.upload','Filemgmt ability to upload a file')",1); 
        if (DB_error()) { 
            plugin_uninstall_filemgmt($steps); 
            return false; 
            exit; 
        } 
        $upload_id = DB_insertId(); 
        COM_errorLog('...success',1); 
        COM_errorLog('Access Rights (features) added successfully',1); 
        $steps['createrights'] = 1; 
    } else { 
        COM_errorLog($LANG_FILEMGMT['install_skip'], 1); 
    }     
     
    // Now assign the features to the FileMgmt groups 
    COM_errorLog('Attempting to give FileMgmt-Admin group access to filemgmt.edit feature',1); 
    if ($filemgmt_assignrights) { 
        DB_query("INSERT INTO {$_TABLES['access']} (acc_ft_id, acc_grp_id) VALUES ($edit_id, $group1_id)"); 
        if (DB_error()) { 
            plugin_uninstall_filemgmt($steps); 
            return false; 
            exit; 
        } 
        COM_errorLog('...success',1); 
     
        COM_errorLog('Attempting to give FileMgmt-Admin group access to filemgmt.user feature',1); 
        DB_query("INSERT INTO {$_TABLES['access']} (acc_ft_id, acc_grp_id) VALUES ($user_id, $group1_id)",1); 
        if (DB_error()) { 
            plugin_uninstall_filemgmt($steps); 
            return false; 
            exit; 
        } 
        COM_errorLog('...success',1); 
         
        COM_errorLog('Attempting to give FileMgmt-Admin group access to filemgmt.upload feature',1); 
        DB_query("INSERT INTO {$_TABLES['access']} (acc_ft_id, acc_grp_id) VALUES ($upload_id, $group1_id)",1); 
        if (DB_error()) { 
            plugin_uninstall_filemgmt($steps); 
            return false; 
            exit; 
        } 
        COM_errorLog('...success',1); 
        $steps['addedusertoadmingroup'] = 1; 
         
        COM_errorLog('Attempting to give FileMgmt-User group access to filemgmt.user feature',1); 
        DB_query("INSERT INTO {$_TABLES['access']} (acc_ft_id, acc_grp_id) VALUES ($user_id, $group2_id)",1); 
        if (DB_error()) { 
            plugin_uninstall_filemgmt($steps); 
            return false; 
            exit; 
        } 
        COM_errorLog('...success',1); 
        COM_errorLog('Access Rights assigned to the Filemgnt groups successfully',1); 
        $steps['assignrights'] = 1; 

    } else { 
        COM_errorLog($LANG_FILEMGMT['install_skip'], 1); 
    }     
     
    // OK, now give Root users access to both new groups for this plugin now! NOTE: Root group should always be 1 
    COM_errorLog('Attempting to give all users in Root group access Filemgmt-Admin group',1); 
    if ($filemgmt_assigngroups) { 
           COM_errorLog('Attempting to assign Filemgmt-Admin to ROOT group',1); 
        DB_query("INSERT INTO {$_TABLES['group_assignments']} VALUES ($group1_id, NULL, 1)"); 
        if (DB_error()) { 
            plugin_uninstall_filemgmt($steps); 
            return false; 
            exit; 
        } 
          COM_errorLog('Attempting to assign Filemgmt-Users to ROOT group',1); 
        DB_query("INSERT INTO {$_TABLES['group_assignments']} VALUES ($group2_id, NULL, 1)"); 
        if (DB_error()) { 
            plugin_uninstall_filemgmt($steps); 
            return false; 
            exit; 
        } 
        COM_errorLog('...success',1); 
        $steps['assigngroup'] = 1; 
    } else { 
        COM_errorLog($LANG_FILEMGMT['install_skip'], 1); 
    }     
    
    COM_errorLog('Registering Filemgmt plugin with Geeklog', 1); 
    if ($filemgmt_registerPI) {   // Register the plugin with Geeklog 
        if (DB_count($_TABLES['plugins'],'pi_name','filemgmt') > 0) { 
            COM_errorLog('Attempting to remove filemgmt plugin entry prior to adding an updated entry',1); 
            DB_query("DELETE FROM {$_TABLES['plugins']} WHERE pi_name = 'filemgmt'"); 
            if (DB_error()) { 
                plugin_uninstall_filemgmt($steps); 
                return false; 
                exit; 
            } 
            COM_errorLog('...success',1); 
        } else { 
            // Only install data on a fresh installation 
            // This plugin has no install data 
        } 
        COM_errorLog('Attempting to create filemgmt plugin entry',1); 
        DB_query("INSERT INTO {$_TABLES['plugins']} (pi_name, pi_version, pi_gl_version, pi_homepage, pi_enabled) " 
            . "VALUES ('filemgmt', {$filemgmt_version}, '1.3.6', 'http://www.langfamily.ca', 1)"); 
        DB_query("INSERT INTO {$_TABLES['vars']} VALUES ('filemgmt','1')"); 
        if (DB_error()) { 
            plugin_uninstall_filemgmt($steps); 
            return false; 
            exit; 
        } 
        COM_errorLog('...success',1); 
        $steps['registerdPI'] = 1; 

    } else { 
        COM_errorLog($LANG_FILEMGMT['install_skip'], 1); 
    } 
         
    COM_errorLog('Succesfully completed FileMgmt Plugin installation!',1); 
    return true; 
} 


/** 
* Create the requried database tables for this plugin into the Geeklog database 
* 
*/ 
function plugin_createtables_filemgmt() { 
global $LANG_FILEMGMT, $_FM_TABLES; 

# 
# Table structure for table to hold reported broken links 
# 
$createsql = "CREATE TABLE {$_FM_TABLES['filemgmt_brokenlinks']} ( 
  reportid int(5) NOT NULL auto_increment, 
  lid int(11) NOT NULL default '0', 
  sender int(11) NOT NULL default '0', 
  ip varchar(20) NOT NULL default '', 
  PRIMARY KEY  (reportid), 
  KEY lid (lid), 
  KEY sender (sender), 
  KEY ip (ip) 
) TYPE=MyISAM"; 

//COM_errorLOG("SQL is:". $createsql); 

DB_query($createsql,1); 
if (DB_error()) { 
    return false; 
    exit; 
} 

# 
# Table structure for filemgmt categories - Top Level and subcategories 
# 

$createsql = "CREATE TABLE {$_FM_TABLES['filemgmt_cat']} ( 
  cid int(5) unsigned NOT NULL auto_increment, 
  pid int(5) unsigned NOT NULL default '0', 
  title varchar(50) NOT NULL default '', 
  imgurl varchar(150) NOT NULL default '', 
  PRIMARY KEY  (cid), 
  KEY pid (pid) 
) TYPE=MyISAM"; 

DB_query($createsql,1); 
if (DB_error()) { 
    return false; 
    exit; 
} 

# 
# Table structure for filemgmt description details 
# 

$createsql = "CREATE TABLE {$_FM_TABLES['filemgmt_filedesc']} ( 
  lid int(11) unsigned NOT NULL default '0', 
  description text NOT NULL, 
  KEY lid (lid) 
) TYPE=MyISAM"; 

DB_query($createsql,1); 
if (DB_error()) { 
    return false; 
    exit; 
} 

# 
# Table structure for filemgmt file details - main table 
# 

$createsql = "CREATE TABLE {$_FM_TABLES['filemgmt_filedetail']} ( 
  lid int(11) unsigned NOT NULL auto_increment, 
  cid int(5) unsigned NOT NULL default '0', 
  title varchar(100) NOT NULL default '', 
  url varchar(250) NOT NULL default '', 
  homepage varchar(100) NOT NULL default '', 
  version varchar(10) NOT NULL default '', 
  size int(8) NOT NULL default '0', 
  platform varchar(50) NOT NULL default '', 
  logourl varchar(250) NOT NULL default '', 
  submitter int(11) NOT NULL default '0', 
  status tinyint(2) NOT NULL default '0', 
  date int(10) NOT NULL default '0', 
  hits int(11) unsigned NOT NULL default '0', 
  rating double(6,4) NOT NULL default '0.0000', 
  votes int(11) unsigned NOT NULL default '0', 
  comments tinyint(2) NOT NULL default '1', 
  PRIMARY KEY  (lid), 
  KEY cid (cid), 
  KEY status (status), 
  KEY title (title(40)) 
) TYPE=MyISAM"; 

DB_query($createsql,1); 
if (DB_error()) { 
    return false; 
    exit; 
} 

# 
# Table structure for filemgmt voting detail records 
# 

$createsql = "CREATE TABLE {$_FM_TABLES['filemgmt_votedata']} ( 
  ratingid int(11) unsigned NOT NULL auto_increment, 
  lid int(11) unsigned NOT NULL default '0', 
  ratinguser int(11) NOT NULL default '0', 
  rating tinyint(3) unsigned NOT NULL default '0', 
  ratinghostname varchar(60) NOT NULL default '', 
  ratingtimestamp int(10) NOT NULL default '0', 
  PRIMARY KEY  (ratingid), 
  KEY ratinguser (ratinguser), 
  KEY ratinghostname (ratinghostname), 
  KEY lid (lid) 
) TYPE=MyISAM"; 


DB_query($createsql,1); 
if (DB_error()) { 
    return false; 
} 

# 
# Table structure for filemgmt download history 
# 

$createsql = "CREATE TABLE {$_FM_TABLES['filemgmt_history']} ( 
  uid mediumint(8) NOT NULL default '0', 
  lid int(11) NOT NULL default '0', 
  remote_ip varchar(15) NOT NULL default '', 
  date datetime NOT NULL default '0000-00-00 00:00:00', 
  KEY lid (lid), 
  KEY uid (uid) 
) TYPE=MyISAM"; 


DB_query($createsql,1); 
if (DB_error()) { 
    return false; 
} 


return true; 

} 

function redirect_header($url, $time=4, $message=""){ 
    echo "<html><head>\n"; 
    echo "<meta http-equiv='Content-Type' content='text/html; charset="._CHARSET."' />\n"; 
    echo "<meta http-equiv='Refresh' content='$time; url=$url' />\n"; 
    echo "<style> 
    body {     
        margin:50px 0px; padding:0px; 
        text-align:center; 
    } 
    #Content { 
        width:100%; 
        margin:0px auto; 
        text-align:center; 
        padding:15px; 
        border:1px dashed #333; 
        background-color:#eee; 
    } 
    </style>"; 
    echo "</head><body><div id='content'>\n"; 
    if ( $message!="" ) { 
        echo "<h4>".$message."</h4>\n"; 
    } else { 
        echo "<h4>"._TAKINGBACK."</h4>\n"; 
    } 
    echo "<br /><b>\n"; 
    printf(_IFNOTRELOAD,$url); 
    echo "</b>\n"; 
    echo "</div>\n"; 
    echo "</body></html>"; 
} 


/* MAIN 
*/ 

$display = COM_siteHeader(); 
// User can set the filemgmt_autoinstall variable to override automatic install 
// By Default the varaibles in filemgmt.cfg are all set to true 
COM_errorLOG("Filemgmt Plugin Install Script executing"); 

if($_POST['submit']) {   // True if ForceDeinstall set and user confirms they want to proceed 
    COM_errorLOG("Filemgmt Plugin De-Install initiating - Removing filemgmt record in vars table"); 
    $res = @mysql_query("SELECT 1 FROM {$_FM_TABLES['filemgmt_cat']} LIMIT 1"); 
    if ($res) { 
        DB_query("DELETE FROM {$_TABLES['vars']} WHERE name = 'filemgmt'"); 
    } 
    if (plugin_uninstall_filemgmt()) { 
        // Uninstall worked 
        COM_errorLOG("Plugin de-install completed - exiting"); 
        redirect_header($_CONF['site_url'] ."/index.php",2,$LANG_FILEMGMT['uninstall_complete_msg']); 
        exit(); 
    } else { 
        // Uninstall failed 
        COM_errorLOG("Plugin de-install failed - exiting"); 
        redirect_header($_CONF['site_url'] ."/index.php",2,$LANG_FILEMGMT['uninstall_failed_msg']); 
        exit();             
    } 

} else { 

if ((DB_count($_TABLES['vars'], 'name', 'filemgmt') == 0) AND ($filemgmt_autoinstall)) { 
    // Record in vars table doesn' exit, install this plugin 
    COM_errorLOG("Autoinstall initiated"); 
    if (plugin_install_filemgmt()) { 
        COM_errorLOG("Autoinstall completed"); 
        redirect_header($_CONF['site_url'] ."/index.php",2,$LANG_FILEMGMT['installation_complete_msg']); 
        exit(); 
    } else { 
        // Error occured 
        COM_errorLOG("Autoinstall Failed"); 
        redirect_header($_CONF['site_url'] ."/index.php",2,$LANG_FILEMGMT['installation_failed_msg']); 
        exit(); 
    } 
} else { 
    // Check if this plugin is installed and enabled 
    if (DB_getItem($_TABLES['plugins'],'pi_enabled',"pi_name = 'filemgmt'") == 1) { 
        COM_errorLOG("Plugin allready installed"); 
        // Check to see if user has set override to force de-install of plugin and then prompt user to confirm de-install 
        if($filemgmt_forceDeinstall){ 
            // Uninstall plugin 
            $display .= COM_startBlock("<b>Filemgmt Plugin De-Install</b>"); 
            $display .= "<table border=0 cellpadding=1 cellspacing=0 width='80%'>"; 
            $display .= "<tr><td align='center'>"; 
            $display .= "<br><p><H2>Do you really want to forceably remove all tables and data associated with the File Mgmt Plugin?</H2><p>"; 
            $display .= "</td></tr><tr><td align = 'center'>"; 
            $display .= "<form action='install.php' method='post'>"; 
            $display .= "<br><br><input type='submit' name='submit' value='Yes-Deinstall Now!'\n>"; 
            $display .= " <input type='button' value='Cancel' onclick='javascript:history.go(-1)'>\n"; 
               $display .= "</form></td></tr></table>"; 
            $display .= COM_endBlock(); 
            COM_output($display); 
             
        } else { 
            // This is locked, do nothing 
            COM_errorLOG("Plugin is enabled and Force De-install was false. Exiting"); 
            redirect_header($_CONF['site_url'] ."/index.php",2,$LANG_FILEMGMT['system_locked_msg']); 
            exit(); 
        } 
    } else { 
        // Uninstall plugin 
        COM_errorLOG("Plugin not enabled, De-Install initiating"); 
        if ($filemgmt_autodeinstall) {        // Check to see if option has been disabled 
            $display .= COM_startBlock("<b>Filemgmt Plugin De-Install</b>"); 
            $display .= "<table border=0 cellpadding=1 cellspacing=0 width='80%'>"; 
            $display .= "<tr><td align='center'>"; 
            $display .= "<br><p><H2>Do you really want to forceably remove all tables and data associated with the File Mgmt Plugin?</H2><p>"; 
            $display .= "</td></tr><tr><td align = 'center'>"; 
            $display .= "<form action='install.php' method='post'>"; 
            $display .= "<br><br><input type='submit' name='submit' value='Yes-Deinstall Now!'\n>"; 
            $display .= " <input type='button' value='Cancel' onclick='javascript:history.go(-1)'>\n"; 
               $display .= "</form></td></tr></table>"; 
            $display .= COM_endBlock(); 
            COM_output($display)     
         } else { 
            COM_errorLOG("Plugin de-install, Nothing to do. Check your plugin install.cfg settings"); 
            redirect_header($_CONF['site_url'] ."/index.php",2,$LANG_FILEMGMT['install_noop_msg']); 
            exit();     
        } 
         
    } 
} 

} 

?>