Alpha Framework alpha--controller
[ class tree: alpha--controller ] [ index: alpha--controller ] [ all elements ]

Source for file Install.php

Documentation is available at Install.php

  1. <?php
  2.  
  3. // include the config file
  4. if(!isset($config)) {
  5.     require_once '../util/AlphaConfig.inc';
  6.     $config AlphaConfig::getInstance();
  7. }
  8.  
  9. require_once $config->get('sysRoot').'alpha/controller/AlphaController.inc';
  10. require_once $config->get('sysRoot').'alpha/view/AlphaView.inc';
  11. require_once $config->get('sysRoot').'alpha/controller/AlphaControllerInterface.inc';
  12.  
  13. /**
  14.  * 
  15.  * Controller used install the database
  16.  * 
  17.  * @package alpha::controller
  18.  * @since 1.0
  19.  * @author John Collins <dev@alphaframework.org>
  20.  * @version $Id: Install.php 1453 2011-12-04 15:12:54Z johnc $
  21.  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
  22.  * @copyright Copyright (c) 2011, John Collins (founder of Alpha Framework).
  23.  *  All rights reserved.
  24.  * 
  25.  *  <pre>
  26.  *  Redistribution and use in source and binary forms, with or
  27.  *  without modification, are permitted provided that the
  28.  *  following conditions are met:
  29.  * 
  30.  *  * Redistributions of source code must retain the above
  31.  *    copyright notice, this list of conditions and the
  32.  *    following disclaimer.
  33.  *  * Redistributions in binary form must reproduce the above
  34.  *    copyright notice, this list of conditions and the
  35.  *    following disclaimer in the documentation and/or other
  36.  *    materials provided with the distribution.
  37.  *  * Neither the name of the Alpha Framework nor the names
  38.  *    of its contributors may be used to endorse or promote
  39.  *    products derived from this software without specific
  40.  *    prior written permission.
  41.  *   
  42.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  43.  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  44.  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  45.  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  46.  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  47.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  48.  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  49.  *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  50.  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  51.  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  52.  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  53.  *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  54.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  55.  *  </pre>
  56.  *  
  57.  */
  58. class Install extends AlphaController implements AlphaControllerInterface {
  59.     /**
  60.      * Trace logger
  61.      * 
  62.      * @var Logger 
  63.      * @since 1.0
  64.      */
  65.     private static $logger null;
  66.     
  67.     /**
  68.      * the constructor
  69.      * 
  70.      * @since 1.0
  71.      */
  72.     public function __construct({
  73.         self::$logger new Logger('Install');
  74.         self::$logger->debug('>>__construct()');
  75.         
  76.         global $config;
  77.         
  78.         parent::__construct('Public');
  79.         
  80.         // if there is nobody logged in, we will send them off to the Login controller to do so before coming back here
  81.         if(!isset($_SESSION['currentUser'])) {
  82.             self::$logger->info('Nobody logged in, invoking Login controller...');
  83.             
  84.             require_once $config->get('sysRoot').'alpha/controller/Login.php';
  85.  
  86.             $controller new Login();
  87.             $controller->setName('Login');
  88.             $controller->setUnitOfWork(array('Login''Install'));
  89.             $controller->doGET(array());
  90.             
  91.             self::$logger->debug('<<__construct');
  92.             exit;
  93.         }else{
  94.             
  95.             // ensure that the super class constructor is called, indicating the rights group
  96.             parent::__construct('Admin');
  97.             
  98.             // set up the title and meta details
  99.             $this->setTitle('Installing '.$config->get('sysTitle'));
  100.             
  101.             self::$logger->debug('<<__construct');
  102.         }
  103.     }
  104.     
  105.     /**
  106.      * Handle GET requests
  107.      * 
  108.      * @param array $params 
  109.      * @since 1.0
  110.      */
  111.     public function doGET($params{
  112.         self::$logger->debug('>>doGET($params=['.var_export($paramstrue).'])');
  113.         
  114.         global $config;
  115.         
  116.         echo AlphaView::displayPageHead($this);
  117.         
  118.         $this->createAppDirectories();
  119.         
  120.         // start a new database transaction
  121.         AlphaDAO::begin();
  122.         
  123.         /*
  124.          * Create DEnum tables
  125.          */
  126.         $DEnum new DEnum();
  127.         $DEnumItem new DEnumItem();
  128.         try{
  129.             echo '<p>Attempting to create the DEnum tables...';
  130.             if(!$DEnum->checkTableExists())
  131.                 $DEnum->makeTable();
  132.             self::$logger->info('Created the ['.$DEnum->getTableName().'] table successfully');
  133.             
  134.             if(!$DEnumItem->checkTableExists())
  135.                 $DEnumItem->makeTable();
  136.             self::$logger->info('Created the ['.$DEnumItem->getTableName().'] table successfully');
  137.             
  138.             
  139.             // create a default article DEnum category
  140.             $DEnum new DEnum('ArticleObject::section');
  141.             $DEnumItem new DEnumItem();
  142.             $DEnumItem->set('value''Main');
  143.             $DEnumItem->set('DEnumID'$DEnum->getID());
  144.             $DEnumItem->save();
  145.             
  146.             echo AlphaView::displayUpdateMessage('DEnums set up successfully.');
  147.         }catch (Exception $e{
  148.             echo AlphaView::displayErrorMessage($e->getMessage());
  149.             echo AlphaView::displayErrorMessage('Aborting.');
  150.             self::$logger->error($e->getMessage());
  151.             AlphaDAO::rollback();
  152.             exit;
  153.         }
  154.         
  155.         /*
  156.          * Loop over each business object in the system, and create a table for it
  157.          */
  158.         $classNames AlphaDAO::getBOClassNames();
  159.         $loadedClasses array();
  160.         
  161.         foreach($classNames as $classname{
  162.             AlphaDAO::loadClassDef($classname);
  163.             array_push($loadedClasses$classname);
  164.         }
  165.         
  166.         foreach($loadedClasses as $classname{
  167.             try {
  168.                 echo '<p>Attempting to create the table for the class ['.$classname.']...';
  169.                             
  170.                 try {echo '1<br>';
  171.                     $BO new $classname();
  172.                     
  173.                     if(!$BO->checkTableExists()) {
  174.                         $BO->makeTable();
  175.                     }else{
  176.                         if($BO->checkTableNeedsUpdate()) {        
  177.                             $missingFields $BO->findMissingFields();
  178.                             
  179.                             $count count($missingFields);
  180.                 
  181.                             for($i 0$i $count$i++)
  182.                                 $BO->addProperty($missingFields[$i]);
  183.                         }
  184.                     }
  185.                 }catch (FailedIndexCreateException $eice{
  186.                     // this are safe to ignore for now as they will be auto-created later once all of the tables are in place
  187.                     self::$logger->warn($eice->getMessage());
  188.                 }catch (FailedLookupCreateException $elce{
  189.                     // this are safe to ignore for now as they will be auto-created later once all of the tables are in place
  190.                     self::$logger->warn($elce->getMessage());
  191.                 }
  192.                 
  193.                 self::$logger->info('Created the ['.$BO->getTableName().'] table successfully');
  194.                 echo AlphaView::displayUpdateMessage('Created the ['.$BO->getTableName().'] table successfully');
  195.             }catch (Exception $e{
  196.                 echo AlphaView::displayErrorMessage($e->getMessage());
  197.                 echo AlphaView::displayErrorMessage('Aborting.');
  198.                 self::$logger->error($e->getMessage());
  199.                 AlphaDAO::rollback();
  200.                 exit;
  201.             }
  202.         }
  203.         
  204.         echo AlphaView::displayUpdateMessage('All business object tables created successfully!');
  205.         
  206.         /*
  207.          * Create the Admin and Standard groups
  208.          */
  209.         $adminGroup new RightsObject();
  210.         $adminGroup->set('name''Admin');
  211.         $standardGroup new RightsObject();
  212.         $standardGroup->set('name''Standard');
  213.         try{
  214.             try{
  215.                 echo '<p>Attempting to create the Admin and Standard groups...';
  216.                 $adminGroup->save();
  217.                 $standardGroup->save();
  218.                 
  219.                 self::$logger->info('Created the Admin and Standard rights groups successfully');
  220.                 echo AlphaView::displayUpdateMessage('Created the Admin and Standard rights groups successfully');
  221.             }catch (FailedIndexCreateException $eice{
  222.                 // this are safe to ignore for now as they will be auto-created later once all of the tables are in place
  223.                 self::$logger->warn($eice->getMessage());
  224.             }catch (FailedLookupCreateException $elce{
  225.                 // this are safe to ignore for now as they will be auto-created later once all of the tables are in place
  226.                 self::$logger->warn($elce->getMessage());
  227.             }
  228.         }catch (Exception $e{
  229.             echo AlphaView::displayErrorMessage($e->getMessage());
  230.             echo AlphaView::displayErrorMessage('Aborting.');
  231.             self::$logger->error($e->getMessage());
  232.             AlphaDAO::rollback();
  233.             exit;
  234.         }
  235.         
  236.         /*
  237.          * Save the admin user to the database in the right group
  238.          */
  239.         try{
  240.             try {
  241.                 echo '<p>Attempting to save the Admin account...';
  242.                 $admin new PersonObject();
  243.                 $admin->set('displayName''Admin');
  244.                 $admin->set('email'$_SESSION['currentUser']->get('email'));
  245.                 $admin->set('password'$_SESSION['currentUser']->get('password'));
  246.                 $admin->save();
  247.                 self::$logger->info('Created the admin user account ['.$_SESSION['currentUser']->get('email').'] successfully');
  248.                 
  249.                 $adminGroup->loadByAttribute('name''Admin');
  250.                         
  251.                 $lookup $adminGroup->getMembers()->getLookup();
  252.                 $lookup->setValue(array($admin->getID()$adminGroup->getID()));
  253.                 $lookup->save();
  254.                 
  255.                 self::$logger->info('Added the admin account to the Admin group successfully');
  256.                 echo AlphaView::displayUpdateMessage('Added the admin account to the Admin group successfully');
  257.             }catch (FailedIndexCreateException $eice{
  258.                 // this are safe to ignore for now as they will be auto-created later once all of the tables are in place
  259.                 self::$logger->warn($eice->getMessage());
  260.             }catch (FailedLookupCreateException $elce{
  261.                 // this are safe to ignore for now as they will be auto-created later once all of the tables are in place
  262.                 self::$logger->warn($elce->getMessage());
  263.             }
  264.         }catch (Exception $e{
  265.             echo AlphaView::displayErrorMessage($e->getMessage());
  266.             echo AlphaView::displayErrorMessage('Aborting.');
  267.             self::$logger->error($e->getMessage());
  268.             AlphaDAO::rollback();
  269.             exit;
  270.         }        
  271.         
  272.         echo '<br><p align="center"><a href="'.FrontController::generateSecureURL('act=ListBusinessObjects').'">Administration Home Page</a></p><br>';
  273.         echo AlphaView::displayPageFoot($this);
  274.         
  275.         // commit
  276.         AlphaDAO::commit();
  277.         
  278.         self::$logger->info('Finished installation!');
  279.         self::$logger->debug('<<doGET');
  280.     }
  281.     
  282.     /**
  283.      * Copies a .htaccess file that restricts public access to the target directory
  284.      * 
  285.      * @param string $dir 
  286.      * @since 1.0
  287.      */
  288.     private function copyRestrictedAccessFileToDirectory($dir{
  289.         global $config;
  290.         
  291.         copy($config->get('sysRoot').'alpha/.htaccess'$dir.'/.htaccess');
  292.     }
  293.     
  294.     /**
  295.      * Creates the standard application directories
  296.      * 
  297.      * @since 1.0
  298.      */
  299.     private function createAppDirectories({
  300.         global $config;
  301.         
  302.         // set the umask first before attempt mkdir
  303.         umask(0);
  304.         
  305.         /*
  306.          * Create the logs directory, then instantiate a new logger
  307.          */
  308.         try {
  309.             $logsDir $config->get('sysRoot').'logs';
  310.             
  311.             echo '<p>Attempting to create the logs directory <em>'.$logsDir.'</em>...';
  312.             
  313.             if(!file_exists($logsDir))
  314.                 mkdir($logsDir0766);
  315.                 
  316.             $this->copyRestrictedAccessFileToDirectory($logsDir);
  317.             
  318.             self::$logger new Logger('Install');
  319.             self::$logger->info('Started installation process!');
  320.             self::$logger->info('Logs directory ['.$logsDir.'] successfully created');
  321.             echo AlphaView::displayUpdateMessage('Logs directory ['.$logsDir.'] successfully created');
  322.         }catch (Exception $e{
  323.             echo AlphaView::displayErrorMessage($e->getMessage());
  324.             echo AlphaView::displayErrorMessage('Aborting.');
  325.             exit;
  326.         }
  327.         
  328.         /*
  329.          * Create the cron tasks directory
  330.          */
  331.         try {
  332.             $tasksDir $config->get('sysRoot').'tasks';
  333.             
  334.             echo '<p>Attempting to create the tasks directory <em>'.$tasksDir.'</em>...';
  335.             
  336.             if(!file_exists($tasksDir))
  337.                 mkdir($tasksDir0766);
  338.                 
  339.             $this->copyRestrictedAccessFileToDirectory($logsDir);
  340.             
  341.             self::$logger->info('Tasks directory ['.$tasksDir.'] successfully created');
  342.             echo AlphaView::displayUpdateMessage('Tasks directory ['.$tasksDir.'] successfully created');
  343.         }catch (Exception $e{
  344.             echo AlphaView::displayErrorMessage($e->getMessage());
  345.             echo AlphaView::displayErrorMessage('Aborting.');
  346.             exit;
  347.         }
  348.         
  349.         /*
  350.          * Create the controller directory
  351.          */
  352.         try {
  353.             $controllerDir $config->get('sysRoot').'controller';
  354.             
  355.             echo '<p>Attempting to create the controller directory <em>'.$controllerDir.'</em>...';
  356.             
  357.             if(!file_exists($controllerDir))
  358.                 mkdir($controllerDir0766);
  359.             
  360.             self::$logger->info('Controller directory ['.$controllerDir.'] successfully created');
  361.             echo AlphaView::displayUpdateMessage('Controllers directory ['.$controllerDir.'] successfully created');
  362.         }catch (Exception $e{
  363.             echo AlphaView::displayErrorMessage($e->getMessage());
  364.             echo AlphaView::displayErrorMessage('Aborting.');
  365.             exit;
  366.         }
  367.         
  368.         /*
  369.          * Create the model directory
  370.          */
  371.         try {
  372.             $modelDir $config->get('sysRoot').'model';
  373.             
  374.             echo '<p>Attempting to create the model directory <em>'.$modelDir.'</em>...';
  375.             
  376.             if(!file_exists($modelDir))
  377.                 mkdir($modelDir0766);
  378.                 
  379.             $this->copyRestrictedAccessFileToDirectory($modelDir);
  380.             
  381.             self::$logger->info('Model directory ['.$modelDir.'] successfully created');
  382.             echo AlphaView::displayUpdateMessage('Model directory ['.$modelDir.'] successfully created');
  383.         }catch (Exception $e{
  384.             echo AlphaView::displayErrorMessage($e->getMessage());
  385.             echo AlphaView::displayErrorMessage('Aborting.');
  386.             exit;
  387.         }
  388.         
  389.         /*
  390.          * Create the view directory
  391.          */
  392.         try {
  393.             $viewDir $config->get('sysRoot').'view';
  394.             
  395.             echo '<p>Attempting to create the view directory <em>'.$viewDir.'</em>...';
  396.             
  397.             if(!file_exists($viewDir))
  398.                 mkdir($viewDir0766);
  399.                 
  400.             $this->copyRestrictedAccessFileToDirectory($viewDir);
  401.             
  402.             self::$logger->info('View directory ['.$viewDir.'] successfully created');
  403.             echo AlphaView::displayUpdateMessage('View directory ['.$viewDir.'] successfully created');
  404.         }catch (Exception $e{
  405.             echo AlphaView::displayErrorMessage($e->getMessage());
  406.             echo AlphaView::displayErrorMessage('Aborting.');
  407.             exit;
  408.         }
  409.         
  410.         /*
  411.          * Create the attachments directory
  412.          */
  413.         try {
  414.             $attachmentsDir $config->get('sysRoot').'attachments';
  415.             
  416.             echo '<p>Attempting to create the attachments directory <em>'.$attachmentsDir.'</em>...';
  417.             
  418.             if(!file_exists($attachmentsDir))
  419.                 mkdir($attachmentsDir0766);
  420.                 
  421.             $this->copyRestrictedAccessFileToDirectory($attachmentsDir);
  422.             
  423.             self::$logger->info('Attachments directory ['.$attachmentsDir.'] successfully created');
  424.             echo AlphaView::displayUpdateMessage('Attachments directory ['.$attachmentsDir.'] successfully created');
  425.         }catch (Exception $e{
  426.             echo AlphaView::displayErrorMessage($e->getMessage());
  427.             echo AlphaView::displayErrorMessage('Aborting.');
  428.             exit;
  429.         }
  430.         
  431.         /*
  432.          * Create the cache directory and sub-directories
  433.          */
  434.         try {
  435.             $cacheDir $config->get('sysRoot').'cache';
  436.             $htmlDir $config->get('sysRoot').'cache/html';
  437.             $imagesDir $config->get('sysRoot').'cache/images';
  438.             $pdfDir $config->get('sysRoot').'cache/pdf';
  439.             $xlsDir $config->get('sysRoot').'cache/xls';
  440.             
  441.             // cache
  442.             echo '<p>Attempting to create the cache directory <em>'.$cacheDir.'</em>...';
  443.             if(!file_exists($cacheDir))            
  444.                 mkdir($cacheDir0766);
  445.                 
  446.             $this->copyRestrictedAccessFileToDirectory($cacheDir);
  447.             
  448.             self::$logger->info('Cache directory ['.$cacheDir.'] successfully created');
  449.             echo AlphaView::displayUpdateMessage('Cache directory ['.$cacheDir.'] successfully created');
  450.             
  451.             // cache/html
  452.             echo '<p>Attempting to create the HTML cache directory <em>'.$htmlDir.'</em>...';
  453.             if(!file_exists($htmlDir))            
  454.                 mkdir($htmlDir0766);
  455.                 
  456.             $this->copyRestrictedAccessFileToDirectory($htmlDir);
  457.             
  458.             self::$logger->info('Cache directory ['.$htmlDir.'] successfully created');
  459.             echo AlphaView::displayUpdateMessage('Cache directory ['.$htmlDir.'] successfully created');
  460.             
  461.             // cache/images
  462.             echo '<p>Attempting to create the cache directory <em>'.$imagesDir.'</em>...';
  463.             if(!file_exists($imagesDir))            
  464.                 mkdir($imagesDir0766);
  465.             
  466.             $this->copyRestrictedAccessFileToDirectory($imagesDir);
  467.             
  468.             self::$logger->info('Cache directory ['.$imagesDir.'] successfully created');
  469.             echo AlphaView::displayUpdateMessage('Cache directory ['.$imagesDir.'] successfully created');
  470.             
  471.             // cache/pdf
  472.             echo '<p>Attempting to create the cache directory <em>'.$pdfDir.'</em>...';
  473.             if(!file_exists($pdfDir))            
  474.                 mkdir($pdfDir0766);
  475.                 
  476.             $this->copyRestrictedAccessFileToDirectory($pdfDir);
  477.             
  478.             self::$logger->info('Cache directory ['.$pdfDir.'] successfully created');
  479.             echo AlphaView::displayUpdateMessage('Cache directory ['.$pdfDir.'] successfully created');
  480.             
  481.             // cache/xls
  482.             echo '<p>Attempting to create the cache directory <em>'.$xlsDir.'</em>...';
  483.             if(!file_exists($xlsDir))            
  484.                 mkdir($xlsDir0766);
  485.                 
  486.             $this->copyRestrictedAccessFileToDirectory($xlsDir);
  487.             
  488.             self::$logger->info('Cache directory ['.$xlsDir.'] successfully created');
  489.             echo AlphaView::displayUpdateMessage('Cache directory ['.$xlsDir.'] successfully created');
  490.         }catch (Exception $e{
  491.             echo AlphaView::displayErrorMessage($e->getMessage());
  492.             echo AlphaView::displayErrorMessage('Aborting.');
  493.             exit;
  494.         }
  495.     }
  496.     
  497.     /**
  498.      * Handle POST requests
  499.      * 
  500.      * @param array $params 
  501.      * @since 1.0
  502.      */
  503.     public function doPOST($params{
  504.         self::$logger->debug('>>doPOST($params=['.var_export($paramstrue).'])');
  505.         
  506.         self::$logger->debug('<<doPOST');
  507.     }
  508.     
  509.     /**
  510.      * Custom version of the check rights method that only checks for a session for the config admin username/password,
  511.      * when the system database is not set-up
  512.      * 
  513.      * @return boolean 
  514.      * @since 1.0
  515.      */
  516.     public function checkRights({
  517.         self::$logger->debug('>>checkRights()');
  518.         
  519.         global $config;
  520.         
  521.         if ($this->getVisibility(== 'Public'{
  522.             self::$logger->debug('<<checkRights [true]');
  523.             return true;
  524.         }
  525.         
  526.         if(AlphaDAO::isInstalled()) {
  527.             self::$logger->debug('<<checkRights [false]');
  528.             return false;
  529.         }
  530.         
  531.         // the person is logged in?
  532.         if (isset($_SESSION['currentUser'])) {
  533.             if ($_SESSION['currentUser']->get('email'== $config->get('sysInstallUsername')) {
  534.                 self::$logger->debug('<<checkRights [true]');
  535.                 return true;
  536.             }
  537.         }
  538.     }
  539. }
  540.  
  541. // now build the new controller
  542. if(basename($_SERVER['PHP_SELF']== 'Install.php'{
  543.     $controller new Install();
  544.     
  545.     if(!empty($_POST)) {            
  546.         $controller->doPOST($_REQUEST);
  547.     }else{
  548.         $controller->doGET($_GET);
  549.     }
  550. }
  551.  
  552. ?>

Documentation generated on Tue, 13 Dec 2011 20:27:04 +0000 by phpDocumentor 1.4.3