Source for file admin.installer.php

Documentation is available at admin.installer.php

  1. <?php
  2. /**
  3. @package Mambo
  4. @subpackage Installer
  5. @copyright  Refer to copyright.php
  6. @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  7. @author Mambo Foundation Inc see README.php
  8. */
  9.  
  10. /** ensure this file is being included by a parent file */
  11. defined'_VALID_MOS' or dieT_('Direct Access to this location is not allowed.') );
  12.  
  13. require_once$mainframe->getPath'admin_html' ) );
  14. require_once$mainframe->getPath'class' ) );
  15.  
  16. $element     mosGetParam$_REQUEST'element''' );
  17. $client     mosGetParam$_REQUEST'client''' );
  18. // ensure user has access to this function
  19. if (!$acl->acl_check'administration''install''users'$my->usertype$element 's''all' ) ) {
  20.     mosRedirect'index2.php'T_('You are not authorized to view this resource.') );
  21. }
  22.  
  23. // map the element to the required derived class
  24. $classMap array(
  25.     'universal' => 'mosInstaller',
  26.     'component' => 'mosInstaller',
  27.     'language' => 'mosInstaller',
  28.     'mambot' => 'mosInstaller',
  29.     'module' => 'mosInstaller',
  30.     'template' => 'mosInstaller',
  31.     'include' => 'mosInstaller',
  32.     'parameters' => 'mosInstaller'
  33. );
  34.  
  35. if (array_key_exists $element$classMap )) {
  36. //    require_once( $mainframe->getPath( 'installer_class', $element ) );
  37.  
  38.     switch ($task{
  39.  
  40.         case 'uploadfile':
  41.             uploadPackage$classMap[$element]$option$element$client );
  42.             break;
  43.  
  44.         case 'installfromdir':
  45.             installFromDirectory$classMap[$element]$option$element$client );
  46.             break;
  47.         
  48.         case 'installfromurl':
  49.             installFromUrl ($classMap[$element]$option$element$client);
  50.             break;
  51.             
  52.         case 'thesource':
  53.             HTML_installer::theSourceForm($option$element$client);
  54.             break;
  55.             
  56.         case 'addon':
  57.             HTML_installer::AddonForm($classMap[$element]$option$element$client);
  58.             break;
  59.  
  60.         case 'remove':
  61.             $uninstaller $element.'_uninstall';
  62.             if (is_callable($uninstaller)) {
  63.                 $cid mosGetParam($_REQUEST'cid'array(0));
  64.                 if (is_array($cidAND isset($cid[0])) {
  65.                     $uninstaller ($cid[0]$option$client);
  66.                     exit ();
  67.                 }
  68.                 mosRedirect(returnTo($option$element$client)T_('There was nothing selected to be uninstalled') );
  69.             }
  70.             else mosRedirect(returnTo($option$element$client)T_('Uninstaller not found for element [%s]') );
  71.             break;
  72.  
  73.         default:
  74.             $path $mosConfig_absolute_path "/administrator/components/com_installer/$element/$element.php";
  75.  
  76.             if (file_exists$path )) {
  77.                 require $path;
  78.             else {
  79.                 echo sprintf(T_('Installer not found for element [%s]')$element);
  80.             }
  81.             break;
  82.     }
  83. }
  84. else {
  85.     echo sprintf(T_('Installer not available for element [%s]')$element);
  86. }
  87.  
  88.  
  89. function returnTo ($option$element$client{
  90.     switch ($element{
  91.         case 'template':
  92.             return "index2.php?option=com_templates&client=$client";
  93.         case 'language':
  94.             return "index2.php?option=com_languages";
  95.         default:
  96.             return "index2.php?option=$option&element=$element";
  97.     }
  98. }
  99.  
  100. /**
  101. @param string Path to the addons xml file
  102. */
  103. function getCurrentAddonVersion ($xmlfile{
  104.     global $mosConfig_absolute_path;
  105.     $xmlfilepath $mosConfig_absolute_path $xmlfile;
  106.     //Check the file exists and is thus installed
  107.     if (file_exists($xmlfilepath)) {
  108.         //Load the XML files
  109.         if (!$mXML simplexml_load_file($xmlfilepath)) {
  110.             return "Can't parse XML";
  111.         else {
  112.         //Look for current version
  113.         /* Note: Ampersands in  the XML data create an issue for SimpleXML.  The solution
  114.         is to either wrap the text in CDATA or enter in the XML file as &amp;.  More info
  115.         on the issue here - http://changelog.ca/log/2005/06/14/php-simplexml-cdata-problem--and-my-solution.
  116.         Need to revisit later... for now the current XML files have been corrected.*/
  117.         $version $mXML->version;
  118.         return $version;
  119.         }
  120.     else {
  121.         return "Not Installed";
  122.     }    
  123. }
  124.  
  125. /**
  126. @param string The class name for the installer
  127. @param string The URL option
  128. @param string The element name
  129. */
  130. function uploadPackage$installerClass$option$element$client {
  131.     global $mainframe;
  132.  
  133.     // Check if file uploads are enabled
  134.     if (!(bool)ini_get('file_uploads')) {
  135.         $message new mosError (T_('The installer can\'t continue before file uploads are enabled. Please use the install from directory method.')_MOS_ERROR_FATAL);
  136.         HTML_installer::showInstallMessage($messageT_('Installer - Error')returnTo$option$element$client ));
  137.         exit();
  138.     }
  139.  
  140.     // Check that the zlib is available
  141.     if(!extension_loaded('zlib')) {
  142.         $message new mosError (T_('The installer can\'t continue before zlib is installed')_MOS_ERROR_FATAL);
  143.         HTML_installer::showInstallMessage($messageT_('Installer - Error')returnTo$option$element$client ));
  144.         exit();
  145.     }
  146.  
  147.     $userfile mosGetParam$_FILES'userfile'null );
  148.  
  149.     if (!$userfile{
  150.         $message new mosError (T_('No file selected')_MOS_ERROR_FATAL);
  151.         HTML_installer::showInstallMessage($messageT_('Upload new module - error')returnTo($option$element$client));
  152.         exit();
  153.     }
  154.  
  155.     $userfile_name $userfile['name'];
  156.  
  157.     
  158.   if (get_magic_quotes_gpc()==0$userfile['tmp_name'stripslashes($userfile['tmp_name']);
  159.   if (uploadFile$userfile['tmp_name']$userfile['name']$message )) {
  160. //    if (uploadFile( stripslashes($userfile['tmp_name']), $userfile['name'], $message )) {
  161.         $installer =new $installerClass();
  162.         if (!$installer->extractArchive$userfile['name')) {
  163.             $installer->cleanUpInstall();
  164.             HTML_installer::showInstallMessage$installer->getErrors()sprintf(T_('Upload %s - Upload Failed')$element),
  165.                 returnTo$option$element$client ) );
  166.         }
  167.         $ret $installer->install();
  168.         $installer->cleanUpInstall();
  169.         HTML_installer::showInstallMessage$installer->getErrors()T_('Upload ').$element.' - '.($ret T_('Success'T_('Failed')),
  170.             returnTo$option$element$client ) );
  171.     }
  172.     else HTML_installer::showInstallMessage$messagesprintf(T_('Upload %s -  Upload Error')$element),
  173.             returnTo$option$element$client ) );
  174. }
  175.  
  176. /**
  177. * Install a template from a directory
  178. @param string The URL option
  179. */
  180. function installFromDirectory$installerClass$option$element$client {
  181.     $userfile mosGetParam$_REQUEST'userfile''' );
  182.     if (!$userfile{
  183.         mosRedirect"index2.php?option=$option&element=module"T_('Please select a directory') );
  184.     }
  185.     $path mosPathName$userfile );
  186.     if (!is_dir$path )) {
  187.         $path dirname$path );
  188.     }
  189.     $installer =new $installerClass();
  190.     $ret $installer->install$path );
  191.     $installer->cleanUpInstall();
  192.     HTML_installer::showInstallMessage$installer->getErrors()T_('Install new ').$element.' - '.($ret T_('Success'T_('Error')),
  193.         returnTo$option$element$client ) );
  194. }
  195. /**
  196. * Install a template from an HTTP URL
  197. @param string The URL option
  198. */
  199. function installFromUrl$installerClass$option$element$client {
  200.     // Check that the zlib is available
  201.     if(!extension_loaded('zlib')) {
  202.         $message new mosError (T_('The installer can\'t continue before zlib is installed')_MOS_ERROR_FATAL);
  203.         HTML_installer::showInstallMessage($message'Installer - Error'returnTo$option$element$client ));
  204.         exit();
  205.     }
  206.     $userurl mosGetParam$_REQUEST'userurl''' );
  207.     if (!$userurl || $userurl[0]=='http://'{
  208.         $message new mosError (T_('Please select an HTTP URL')_MOS_ERROR_FATAL);
  209.         HTML_installer::showInstallMessage($messageT_('Installer - Error')returnTo($option$element$client));
  210.         exit();
  211.     }
  212.     foreach ($userurl as $value{
  213.     $url_data parse_url($value);
  214.     if (isset($url_data['path'])) $userfilename basename($url_data['path']);
  215.     else $userfilename '';
  216.     if (!$userfilename{
  217.         $message new mosError (T_('The URL did not define a file name')_MOS_ERROR_FATAL);
  218.         HTML_installer::showInstallMessage($messageT_('Installer - Error')returnTo($option$element$client));
  219.     }
  220.     if (uploadUrl($value$userfilename$message )) {
  221.         $installer new $installerClass();
  222.         if (!$installer->extractArchive($userfilename)) {
  223.             $installer->cleanUpInstall();
  224.             HTML_installer::showInstallMessage$installer->getErrors()T_('Upload ').$element.' - '.T_('Failed'),
  225.                 returnTo$option$element$client ) );
  226.         }
  227.         $ret $installer->install();
  228.         $installer->cleanUpInstall();
  229.         HTML_installer::showInstallMessage$installer->getErrors()T_('Upload ').$element.' - '.($ret T_('Success'T_('Failed')),
  230.             returnTo$option$element$client ) );
  231.     else {
  232.         HTML_installer::showInstallMessage$messageT_('Upload ').$element.' - '.T_('Error'),
  233.             returnTo$option$element$client ) );
  234.     }
  235. }
  236. }
  237. /**
  238. @param string The name of the php (temporary) uploaded file
  239. @param string The name of the file to put in the temp directory
  240. @param string The message to return
  241. */
  242. function uploadFile$filename$userfile_name&$error {
  243.     global $mosConfig_absolute_path;
  244.     $baseDir mosPathName$mosConfig_absolute_path '/media' );
  245.  
  246.     if (file_exists$baseDir )) {
  247.         if (is_writable$baseDir )) {
  248.             if (move_uploaded_file$filename$baseDir $userfile_name )) {
  249.                 if (mosChmod$baseDir $userfile_name )) {
  250.                     return true;
  251.                 else {
  252.                     $msg T_('Failed to change the permissions of the uploaded file.');
  253.                 }
  254.             else {
  255.                 $msg T_('Failed to move uploaded file to <code>/media</code> directory.');
  256.             }
  257.         else {
  258.             $msg T_('Upload failed as <code>/media</code> directory is not writable.');
  259.         }
  260.     else {
  261.         $msg T_('Upload failed as <code>/media</code> directory does not exist.');
  262.     }
  263.     $error new mosError ($msg_MOS_ERROR_FATAL);
  264.     return false;
  265. }
  266. /**
  267. @param string The name of the php (temporary) uploaded file
  268. @param string The name of the file to put in the temp directory
  269. @param string The message to return
  270. */
  271. function uploadUrl$userurl$userfilename&$error {
  272.     global $mosConfig_absolute_path;
  273.     $baseDir mosPathName$mosConfig_absolute_path '/media' );
  274.     if (file_exists$baseDir )) {
  275.         if (is_writable$baseDir )) {
  276.             if ($fpin @fopen($userurl'rb'AND is_resource($fpin)) {
  277.                 if ($fpout @fopen($baseDir.$userfilename'wb'AND is_resource($fpout)) {
  278.                     while (!feof($fpin)) {
  279.                         $data fgets($fpin1024);
  280.                         fwrite($fpout$data);
  281.                     }
  282.                     fclose($fpout);
  283.                     fclose($fpin);
  284.                     if (mosChmod$baseDir.$userfilename )) return true;
  285.                     else $msg T_('Failed to change the permissions of the uploaded file.');
  286.                 }
  287.                 else $msg T_('Failed to open the local file from the URL.');
  288.             }
  289.             else $msg T_('Failed to open the specified URL.');
  290.         }
  291.         else $msg T_('Upload failed as <code>/media</code> directory is not writable.');
  292.     }
  293.     else $msg T_('Upload failed as <code>/media</code> directory does not exist.');
  294.     $error new mosError ($msg_MOS_ERROR_FATAL);
  295.     return false;
  296. }
  297.  
  298.     /**
  299.     * Component uninstall method
  300.     * @param int The id of the module
  301.     * @param string The URL option
  302.     * @param int The client id
  303.     */
  304.     function component_uninstall$cid$option$client={
  305.         $database =mamboDatabase::getInstance();
  306.         $sql "SELECT * FROM #__components WHERE id=$cid";
  307.         $database->setQuery($sql);
  308.         if (!$database->loadObject$row )) {
  309.             $message new mosError ($database->stderr(true)_MOS_ERROR_FATAL);
  310.             HTML_installer::showInstallMessage($messageT_('Uninstall -  error')"index2.php?option=$option&element=component");
  311.             exit();
  312.         }
  313.         if ($row->iscore{
  314.             $message new mosError (sprintf(T_('Component %s is a core component, and can not be uninstalled.<br />You need to unpublish it if you don\'t want to use it')$row->name)_MOS_ERROR_FATAL);
  315.             HTML_installer::showInstallMessage($message'Uninstall -  error'"index2.php?option=$option&element=component");
  316.             exit();
  317.         }
  318.         // Try to find the XML file
  319.         $here mosPathNamemamboCore::get('mosConfig_absolute_path').'/administrator/components/'.$row->option );
  320.         $filesindir mosReadDirectory($here'.xml$');
  321.         if (count($filesindir0{
  322.             $allerrors new mosErrorSet();
  323.             foreach ($filesindir as $file{
  324.                 $parser =new mosUninstallXML ($here.$file);
  325.                 $parser->uninstall();
  326.                 $allerrors->mergeAnother($parser->errors);
  327.             }
  328.               $ret ($allerrors->getMaxLevel(_MOS_ERROR_FATAL);
  329.             HTML_installer::showInstallMessage$allerrors->getErrors()T_('Uninstall component - ').($ret T_('Success'T_('Error')),
  330.                 returnTo$option'component'$client ) );
  331.         }
  332.         else {
  333.             $com_name $row->option;
  334.             $dir new mosDirectory(mosPathName(mamboCore::get('mosConfig_absolute_path').'/components/'.$com_name));
  335.             $dir->deleteAll();
  336.             $dir new mosDirectory(mosPathName(mamboCore::get('mosConfig_absolute_path').'/administrator/components/'.$com_name));
  337.             $dir->deleteAll();
  338.             $sql "DELETE FROM #__components WHERE `option`='$com_name'";
  339.             $database->setQuery($sql);
  340.             $database->query();
  341.             $message new mosError (T_('Uninstaller could not find XML file, but cleaned database')_MOS_ERROR_WARN);
  342.             HTML_installer::showInstallMessage($messageT_('Uninstall ').T_('component - ').T_('Success')returnTo($option'component'$client));
  343.         }
  344.         exit();
  345.     }
  346.  
  347.     /**
  348.     * Module uninstall method
  349.     * @param int The id of the module
  350.     * @param string The URL option
  351.     * @param int The client id
  352.     */
  353.     function module_uninstall$id$option$client={
  354.         $database =mamboDatabase::getInstance();
  355.         $mosConfig_absolute_path mamboCore::get('mosConfig_absolute_path');
  356.         $query "SELECT module, iscore, client_id FROM #__modules WHERE id = '$id'";
  357.         $database->setQuery$query );
  358.         $database->loadObject$row );
  359.         if ($row->iscore{
  360.             $message new mosError (sprintf(T_('%s is a core module, and can not be uninstalled.<br />You need to unpublish it if you don\'t want to use it')$row->title)_MOS_ERROR_FATAL);
  361.             HTML_installer::showInstallMessage($message'Uninstall -  error'returnTo$option'module'$row->client_id '' 'admin' ) );
  362.             exit();
  363.         }
  364.         $query "DELETE FROM #__modules_menu WHERE moduleid=$id";
  365.         $database->setQuery$query );
  366.         if (!$database->query()) {
  367.             $msg $database->stderr;
  368.             die$msg );
  369.         }
  370.         if $row->client_id $basepath $mosConfig_absolute_path '/administrator/modules/';
  371.         else $basepath $mosConfig_absolute_path '/modules/';
  372.           $xmlfile $basepath $row->module '.xml';
  373.           $parser =new mosUninstallXML ($xmlfile);
  374.           $parser->uninstall();
  375.           $ret ($parser->errors->getMaxLevel(_MOS_ERROR_FATAL);
  376.         HTML_installer::showInstallMessage$parser->errors->getErrors()T_('Uninstall module - ').($ret T_('Success'T_('Error')),
  377.             returnTo$option'module'$client ) );
  378.           exit ();
  379.     }
  380.  
  381.     /**
  382.     * Mambot install method
  383.     * @param int The id of the module
  384.     * @param string The URL option
  385.     * @param int The client id
  386.     */
  387.     function mambot_uninstall$id$option$client={
  388.         $database =mamboDatabase::getInstance();
  389.         $mosConfig_absolute_path mamboCore::get('mosConfig_absolute_path');
  390.         $database->setQuery"SELECT name, folder, element, iscore FROM #__mambots WHERE id = $id);
  391.         $database->loadObject$row );
  392.         if ($database->getErrorNum()) {
  393.             $message new mosError ($database->stderr()_MOS_ERROR_FATAL);
  394.             HTML_installer::showInstallMessage($messageT_('Uninstall -  error'),
  395.             returnTo$option'mambot'$client ) );
  396.             exit();
  397.         }
  398.         if ($row == null{
  399.             $message new mosError (T_('Invalid object id')_MOS_ERROR_FATAL);
  400.             HTML_installer::showInstallMessage($messageT_('Uninstall -  error')returnTo($option'mambot'$client));
  401.             exit();
  402.         }
  403.         if (trim$row->folder == ''{
  404.             $message new mosError (T_('Folder field empty, cannot remove files')_MOS_ERROR_FATAL);
  405.             HTML_installer::showInstallMessage($messageT_('Uninstall -  error')returnTo($option'mambot'$client));
  406.             exit();
  407.         }
  408.         $xmlfile $mosConfig_absolute_path.'/mambots/'.$row->folder.'/'.$row->element.'.xml';
  409.         if (file_exists($xmlfile)) {
  410.             $parser =new mosUninstallXML ($xmlfile);
  411.             $ret $parser->uninstall();
  412.             $showerrors $parser->getErrors();
  413.         }
  414.         else {
  415.             $database->setQuery("DELETE FROM #__mambots WHERE id = $id");
  416.             $ret $database->query();
  417.             $showerrors new mosError (T_('Uninstaller did its best with no XML file present')_MOS_ERROR_WARN);
  418.         }
  419.         HTML_installer::showInstallMessage$showerrorsT_('Uninstall mambot - ').($ret T_('Success'T_('Error')),
  420.             returnTo$option'mambot'$client ) );
  421.         exit ();
  422.     }
  423.  
  424.     /**
  425.     * Template uninstall method
  426.     * @param int The id of the module
  427.     * @param string The URL option
  428.     * @param int The client id
  429.     */
  430.     function template_uninstall$id$option$client={
  431.         $id str_replacearray'\\''/' )''$id );
  432.         $mosConfig_absolute_path mamboCore::get('mosConfig_absolute_path');
  433.         // Find if normal or admin template and delete corresponding files & directories
  434.         if ($client=='admin'{
  435.             $basepath mamboCore::get('mosConfig_absolute_path').'/administrator/templates/' $id
  436.         }
  437.         else {
  438.             $basepath mamboCore::get('mosConfig_absolute_path').'/templates/' $id
  439.         }
  440.         //Use $basepath to remove the template files and directory    
  441.         $tdir new mosDirectory($basepath);
  442.         $tdir->deleteAll();
  443.  
  444.         $message new mosError (T_('Uninstall template - ')_MOS_ERROR_INFORM);
  445.         HTML_installer::showInstallMessage($messageT_('Success')returnTo($option'template'$client));
  446.         exit ();
  447.     }
  448.  
  449.     /**
  450.     * Language uninstall method
  451.     * @param int The id of the module
  452.     * @param string The URL option
  453.     * @param int The client id
  454.     */
  455.     function language_uninstall$id$option$client={
  456.         $id str_replacearray'\\''/' )''$id );
  457.         $basepath mamboCore::get('mosConfig_absolute_path').'/language/';
  458.         $xmlfile $basepath $id '.xml';
  459.         // see if there is an xml install file, must be same name as element
  460.         if (file_exists$xmlfile )) {
  461.             $parser =new mosUninstallXML ($xmlfile);
  462.             $parser->uninstall();
  463.               $ret ($parser->errors->getMaxLevel(_MOS_ERROR_FATAL);
  464.             HTML_installer::showInstallMessage$parser->errors->getErrors()T_('Uninstall language - ').($ret T_('Success'T_('Error')),
  465.                 returnTo$option'language'$client ) );
  466.         }
  467.         else {
  468.             $message new mosError (T_('Language id empty, cannot remove files')_MOS_ERROR_FATAL);
  469.             HTML_installer::showInstallMessage($messageT_('Uninstall -  error')"index2.php?option=com_languages");
  470.         }
  471.         exit();
  472.     }
  473.  
  474.  
  475. ?>

Documentation generated on Mon, 05 May 2008 16:15:19 +0400 by phpDocumentor 1.4.0