LiveUser
[ class tree: LiveUser ] [ index: LiveUser ] [ all elements ]

Source for file install.php

Documentation is available at install.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * A framework for authentication and authorization in PHP applications
  6.  *
  7.  * LiveUser is an authentication/permission framework designed
  8.  * to be flexible and easily extendable.
  9.  *
  10.  * Since it is impossible to have a
  11.  * "one size fits all" it takes a container
  12.  * approach which should enable it to
  13.  * be versatile enough to meet most needs.
  14.  *
  15.  * PHP version 4 and 5
  16.  *
  17.  * LICENSE: This library is free software; you can redistribute it and/or
  18.  * modify it under the terms of the GNU Lesser General Public
  19.  * License as published by the Free Software Foundation; either
  20.  * version 2.1 of the License, or (at your option) any later version.
  21.  *
  22.  * This library is distributed in the hope that it will be useful,
  23.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  25.  * Lesser General Public License for more details.
  26.  *
  27.  * You should have received a copy of the GNU Lesser General Public
  28.  * License along with this library; if not, write to the Free Software
  29.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30.  * MA  02111-1307  USA
  31.  *
  32.  *
  33.  * @category authentication
  34.  * @package LiveUser
  35.  * @author  Lukas Smith <smith@pooteeweet.org>
  36.  * @copyright 2002-2006 Markus Wolff
  37.  * @license http://www.gnu.org/licenses/lgpl.txt
  38.  * @version CVS: $Id: install.php,v 1.51 2006/05/01 10:46:30 lsmith Exp $
  39.  * @link http://pear.php.net/LiveUser
  40.  */
  41.  
  42. require_once 'LiveUser.php';
  43. require_once 'MDB2/Schema.php';
  44.  
  45. /* ATTENTION: uncomment the following lines as needed
  46.  
  47. // error handler
  48. function handleError($err)
  49. {
  50.    var_dump($err);
  51.    return PEAR_ERRORSTACK_PUSH;
  52. }
  53.  
  54. PEAR_ErrorStack::setDefaultCallback('handleError');
  55.  
  56. echo '<pre>';
  57.  
  58. // customize DSN as needed
  59. $dsn = 'mysql://root:@localhost/liveuser_test_installer';
  60.  
  61. // customize config array as needed
  62. $conf = array(
  63.     'authContainers' => array(
  64.         array(
  65.             'type'         => 'MDB2',
  66.             'expireTime'   => 3600,
  67.             'idleTime'     => 1800,
  68.             'storage' => array(
  69.                 'dsn' => $dsn,
  70. #                'force_seq' => false,
  71.                 'alias' => array(
  72.                     'auth_user_id' => 'authUserId',
  73.                     'lastlogin' => 'lastLogin',
  74.                     'is_active' => 'isActive',
  75.                     'owner_user_id' => 'owner_user_id',
  76.                     'owner_group_id' => 'owner_group_id',
  77.                 ),
  78.                 'fields' => array(
  79. #                    'auth_user_id' => 'integer',
  80.                     'lastlogin' => 'timestamp',
  81.                     'is_active' => 'boolean',
  82.                     'owner_user_id' => 'integer',
  83.                     'owner_group_id' => 'integer',
  84.                 ),
  85.                 'tables' => array(
  86.                     'users' => array(
  87.                         'fields' => array(
  88.                             'lastlogin' => null,
  89.                             'is_active' => null,
  90.                             'owner_user_id' => null,
  91.                             'owner_group_id' => null,
  92.                         ),
  93.                     ),
  94.                 ),
  95.             ),
  96.         ),
  97.     ),
  98.     'permContainer'  => array(
  99.         'type'  => 'Complex',
  100.         'storage' => array(
  101.             'MDB2' => array(
  102.                 'dsn' => $dsn,
  103.                 'prefix' => 'liveuser_',
  104. #                'force_seq' => false,
  105.                 'fields' => array(
  106. #                    'auth_user_id' => 'integer',
  107.                 ),
  108.             )
  109.         )
  110.     )
  111. );
  112.  
  113. @unlink('dump.sql');
  114. function dump_to_file(&$db, $scope, $message, $is_manip)
  115. {
  116.     if ($is_manip) {
  117.         $fp = fopen('dump.sql', 'a');
  118.         fwrite($fp, $message."\n");
  119.         fclose($fp);
  120.     }
  121. }
  122.  
  123. // customize MDB2_SCHEMA configuration options as needed
  124. $options = array(
  125.     'debug' => true,
  126.     'log_line_break' => '<br>',
  127. // to dump the SQL to a file uncommented the following line
  128. // and set the disable_query parameter in the installSchema calls
  129. #    'debug_handler' => 'dump_to_file',
  130. );
  131.  
  132. // field name - value pairs of lengths to use in the schema
  133. $lengths = array('description' => 255);
  134.  
  135. // field name - value pairs of defaults to use in the schema
  136. $defaults = array('right_level' => LIVEUSER_MAX_LEVEL);
  137.  
  138. // create instance of the auth container
  139. $auth =& LiveUser::authFactory($conf['authContainers'][0], 'foo');
  140. // generate xml schema file for auth container
  141. $result = LiveUser_Misc_Schema_Install::generateSchema(
  142.     $auth,
  143.     'auth_schema.xml',
  144.     $lengths,
  145.     $defaults
  146. );
  147. var_dump($result);
  148.  
  149. // install the auth xml schema .. notice the 4th parameter controls if the
  150. // database needs to be created or not
  151. $variables = array();
  152. $result = LiveUser_Misc_Schema_Install::installSchema(
  153.     $auth,
  154.     'auth_schema.xml',
  155.     $variables,
  156.     true,
  157.     $options,
  158.     false,
  159.     false
  160. );
  161. var_dump($result);
  162.  
  163. // create instance of the perm container
  164. $perm =& LiveUser::storageFactory($conf['permContainer']['storage']);
  165. // generate xml schema file for perm container
  166. $result = LiveUser_Misc_Schema_Install::generateSchema(
  167.     $perm,
  168.     'perm_schema.xml',
  169.     $lengths,
  170.     $defaults
  171. );
  172. var_dump($result);
  173.  
  174. // install the perm xml schema .. notice the 4th parameter controls if the
  175. // database needs to be created or not
  176. $variables = array();
  177. $result = LiveUser_Misc_Schema_Install::installSchema(
  178.     $perm,
  179.     'perm_schema.xml',
  180.     $variables,
  181.     false,
  182.     $options,
  183.     false,
  184.     false
  185. );
  186. var_dump($result);
  187.  
  188. /* */
  189.  
  190. /**
  191.  * database schema installer class
  192.  *
  193.  * This class generates XML based schema files and uses PEAR:MDB2_Schema to
  194.  * install them inside the users database
  195.  *
  196.  * Requirements:
  197.  * - PEAR::LiveUser
  198.  * - PEAR::MDB2_Schema
  199.  * - PEAR::MDB2
  200.  * - PEAR::MDB2_Driver_* (where * is the name of the backend database)
  201.  * - a valid LiveUser configuration
  202.  *
  203.  * @category authentication
  204.  * @package LiveUser
  205.  * @author  Lukas Smith <smith@pooteeweet.org>
  206.  * @version $Id: install.php,v 1.51 2006/05/01 10:46:30 lsmith Exp $
  207.  * @copyright 2002-2006 Markus Wolff
  208.  * @license http://www.gnu.org/licenses/lgpl.txt
  209.  * @version Release: @package_version@
  210.  * @link http://pear.php.net/LiveUser
  211.  */
  212. {
  213.  
  214.     /**
  215.      * Accepts a PDO DSN and returns a PEAR DSN
  216.      *
  217.      * The PEAR DSN format is specified here:
  218.      * http://pear.php.net/manual/en/package.database.db.intro-dsn.php
  219.      *
  220.      * @param string PDO DSN
  221.      * @return array PEAR DSN
  222.      *
  223.      * @access public
  224.      */
  225.     function parsePDODSN($pdo_dsn)
  226.     {
  227.         die('Hardcode the parsed DSN to the PEAR array dsn format.');
  228.         return array(
  229.             'phptype'  => false,
  230.             'dbsyntax' => false,
  231.     // not needed as its fetched from the options array
  232.     #        'username' => false,
  233.     #        'password' => false,
  234.             'protocol' => false,
  235.             'hostspec' => false,
  236.             'port'     => false,
  237.             'socket'   => false,
  238.             'database' => false,
  239.             'mode'     => false,
  240.         );
  241.     }
  242.  
  243.     /**
  244.      * Generates a schema file from the instance
  245.      *
  246.      * @param object LiveUser storage instance
  247.      * @param string name of the file into which the xml schema should be written
  248.      * @param array key-value pairs with keys being field names and values being the default length
  249.      * @param array key-value pairs with keys being field names and values being the default values
  250.      * @return bool|PEAR_Errortrue on success or a PEAR_Error on error
  251.      *
  252.      * @access public
  253.      */
  254.     function generateSchema($obj$file$lengths = array()$defaults = array())
  255.     {
  256.         if (!is_object($obj)) {
  257.             return false;
  258.         }
  259.  
  260.         $use_auto_increment = false;
  261.         if (isset($obj->force_seq&& !$obj->force_seq{
  262.             if (MDB2::isConnection($obj->dbc)) {
  263.                 $use_auto_increment ($obj->dbc->supports('auto_increment'=== true);
  264.             elseif (is_a($obj->dbc'PDO')) {
  265.                 // todo: need to figure out what to do here
  266.                 $use_auto_increment = true;
  267.             }
  268.         }
  269.  
  270.         // generate xml schema
  271.         $tables = array();
  272.         $sequences = array();
  273.         foreach ($obj->tables as $table_name => $table{
  274.             $fields = array();
  275.             $table_indexes = array();
  276.             foreach($table['fields'as $field_name => $required{
  277.                 $type $obj->fields[$field_name];
  278.                 $field_name $obj->alias[$field_name];
  279.                 $fields[$field_name]['name'$field_name;
  280.                 $fields[$field_name]['type'$type;
  281.                 if ($fields[$field_name]['type'== 'text'{
  282.                     $length array_key_exists($field_name$lengths$lengths[$field_name: 32;
  283.                     $fields[$field_name]['length'$length;
  284.                 }
  285.  
  286.                 $default array_key_exists($field_name$defaults$defaults[$field_name'';
  287.                 if ($required || array_key_exists($field_name$defaults)) {
  288.                     $fields[$field_name]['default'$default;
  289.                 }
  290.  
  291.                 // check if not null
  292.                 if ($required{
  293.                     $fields[$field_name]['notnull'= true;
  294.                     // Sequences
  295.                     if ($required === 'seq'{
  296.                         if ($fields[$field_name]['type'== 'integer' && $use_auto_increment{
  297.                             $fields[$field_name]['autoincrement'= true;
  298.                             $fields[$field_name]['default'= 0;
  299.                         else {
  300.                             $sequences[$obj->prefix . $obj->alias[$table_name]] = array(
  301.                                 'on' => array(
  302.                                     'table' => $obj->prefix . $obj->alias[$table_name],
  303.                                     'field' => $field_name,
  304.                                 )
  305.                             );
  306.  
  307.                             $table_indexes[$table_name.'_'.$field_name= array(
  308.                                 'fields' => array(
  309.                                     $field_name => true,
  310.                                 ),
  311.                                 'primary' => true
  312.                             );
  313.                         }
  314.                     // Generate indexes
  315.                     elseif (is_string($required)) {
  316.                         $index_name $table_name.'_'.$required '_i';
  317.                         $table_indexes[$index_name]['fields'][$field_name= true;
  318.                         $table_indexes[$index_name]['unique'= true;
  319.                     }
  320.                 else {
  321.                     $fields[$field_name]['notnull'($required === false);
  322.                 }
  323.             }
  324.             $tables[$obj->prefix . $obj->alias[$table_name]]['fields'$fields;
  325.             $tables[$obj->prefix . $obj->alias[$table_name]]['indexes'$table_indexes;
  326.         }
  327.  
  328.         $definition = array(
  329.             'name' => '<variable>database</variable>',
  330.             'create' => '<variable>create</variable>',
  331.             'overwrite' => '<variable>overwrite</variable>',
  332.             'tables' => $tables,
  333.             'sequences' => $sequences,
  334.         );
  335.  
  336.         return LiveUser_Misc_Schema_Install::writeSchema($definition$file);
  337.     }
  338.  
  339.     /**
  340.      * Takes a given definition array and writes it as xml to a file
  341.      *
  342.      * @param array schema definition
  343.      * @return bool|PEAR_Errortrue on success or a PEAR_Error on error
  344.      *
  345.      * @access public
  346.      */
  347.     function writeSchema($definition$file)
  348.     {
  349.         require_once 'MDB2/Schema/Writer.php';
  350.         $writer =new MDB2_Schema_Writer();
  351.         $arguments = array(
  352.             'output_mode' => 'file',
  353.             'output' => $file,
  354.         );
  355.         return $writer->dumpDatabase($definition$arguments);
  356.     }
  357.  
  358.     /**
  359.      * Install a schema file into the database
  360.      *
  361.      * @param object LiveUser storage instance
  362.      * @param string name of the file into which the xml schema should be written
  363.      * @param array key-value pairs with keys being variable names and values being the variable values
  364.      * @param bool determines if the database should be created or not
  365.      * @param array MDB2_Schema::connect() options
  366.      * @param bool determines if the database should be created or not
  367.      * @param bool determines if the old schema file should be unlinked first
  368.      * @param bool determines if the disable_query option should be set in MDB2
  369.      * @return bool|PEAR_Errortrue on success or a PEAR_Error on error
  370.      *
  371.      * @access public
  372.      */
  373.     function installSchema($obj$file$variables = array()$create = true,
  374.         $options = array()$overwrite = false$disable_query = false)
  375.     {
  376.         $dsn = array();
  377.         if (is_a($obj->dbc'DB_Common')) {
  378.             $dsn $obj->dbc->dsn;
  379.             $options['seqcol_name''id';
  380.         elseif (is_a($obj->dbc'PDO')) {
  381.             $dsn LiveUser_Misc_Schema_Install::parsePDODSN($obj->dbc->dsn);
  382.             $dsn['username'array_key_exists('username'$obj->dbc->options)
  383.                 ? $obj->dbc->options['username''';
  384.             $dsn['password'array_key_exists('password'$obj->dbc->options)
  385.                 ? $obj->dbc->options['password''';
  386.             $options['seqname_format''%s';
  387.         elseif (MDB2::isConnection($obj->dbc)) {
  388.             $dsn $obj->dbc->getDSN('array');
  389.         }
  390.  
  391.         $file_old $file.'.'.$dsn['hostspec'].'.'.$dsn['database'].'.old';
  392.         $variables['create'= (int)$create;
  393.         $variables['overwrite'= (int)$overwrite;
  394.         $variables['database'$dsn['database'];
  395.         unset($dsn['database']);
  396.  
  397.         $manager =MDB2_Schema::factory($dsn$options);
  398.         if (PEAR::isError($manager)) {
  399.         return $manager;
  400.         }
  401.  
  402.         if ($overwrite && file_exists($file_old)) {
  403.             unlink($file_old);
  404.         }
  405.         $result $manager->updateDatabase($file$file_old$variables$disable_query);
  406.  
  407.         $debug $manager->db->getOption('debug');
  408.         if ($debug && !PEAR::isError($debug)) {
  409.             echo('Debug messages<br>');
  410.             echo($manager->db->getDebugOutput().'<br>');
  411.         }
  412.         $manager->disconnect();
  413.         return $result;
  414.     }
  415. }
  416.  
  417. ?>

Documentation generated on Mon, 28 Jan 2008 03:30:16 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.