/** * Insert in the db the previously retrieved parameters for a plugin * including publication information. Also move files as required * * @param string $basePath , the base path to get original files from * @param array $shConfig an array holding the database parameters of the plugin * @param array $files, an array holding list of files from the plugin */ function shInsertPlugin($basePath, $shConfig, $files, $folders, $foldersToCopy) { // check data if (empty($files)) { return; } // move the files to target location $result = array(); $resultFolders = array(); $success = true; // create folders as needed if (!empty($folders)) { foreach ($folders as $folder) { $success = $success && JFolder::create(JPATH_ROOT . DS . 'plugins' . DS . $folder); } } // copy raw folders as needed if (!empty($foldersToCopy)) { foreach ($foldersToCopy as $folder) { $success = $success && JFolder::copy(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_sh404sef' . DS . 'plugins' . DS . $shConfig['folder'] . DS . $folder, JPATH_ROOT . DS . 'plugins' . DS . $shConfig['folder'] . DS . $folder, $path = '', $forced = true); $result[$folder] = $success; } } // now move files across if ($success) { foreach ($files as $pluginFile) { $target = JPath::clean(JPATH_ROOT . DS . 'plugins' . DS . $shConfig['folder'] . DS . $pluginFile); $source = JPath::clean($basePath . DS . $pluginFile); $success = $success && true === JFile::copy($source, $target); $resultFolders[$pluginFile] = $success; } // check and add an index.html file if it does not exists $target = JPath::clean(JPATH_ROOT . DS . 'plugins' . DS . $shConfig['folder'] . DS . 'index.html'); if (!JFile::exists($target)) { $source = JPath::clean(JPATH_ROOT . DS . 'plugins' . DS . 'index.html'); $success = $success && true === JFile::copy($source, $target); } } // if files moved to destination, setup plugin in Joomla database if ($success) { $shouldRestore = shShouldRestore(); if ($shouldRestore) { // read stored params from disk shGetExtensionSavedParams($shConfig['folder'] . '.' . $shConfig['element'], $shConfig); } // insert elements in db, but only if it does not exist // we can't use insert ignore, as the record may have been modified by user (unpublished, order change) $db =& JFactory::getDBO(); $sql = 'select count(id) from `#__plugins` where ' . ' `element` = ' . $db->Quote($shConfig['element']) . ' and `folder` = ' . $db->Quote($shConfig['folder']); $db->setQuery($sql); $alreadyInstalled = $db->loadResult(); if (empty($alreadyInstalled)) { $sql = "INSERT INTO `#__plugins` ( `name`, `element`, `folder`, `access`, `ordering`, `published`," . " `iscore`, `client_id`, `checked_out`, `checked_out_time`, `params`)" . " VALUES ('{$shConfig['name']}', '{$shConfig['element']}', '{$shConfig['folder']}', '{$shConfig['access']}', '{$shConfig['ordering']}'," . " '{$shConfig['published']}', '{$shConfig['iscore']}', '{$shConfig['client_id']}', '{$shConfig['checked_out']}'," . " '{$shConfig['checked_out_time']}', '{$shConfig['params']}');"; $db->setQuery($sql); $db->query(); if (!empty($db->getErrorNum)) { echo $db->getErrorMsg() . '<br />'; } } } else { // failure while copying files // don't leave anything behind foreach ($files as $pluginFile) { if (!empty($result[$pluginFile])) { // if file was copied, try to delete it JFile::delete(JPATH_ROOT . DS . 'plugins' . DS . $shConfig['folder'] . DS . $pluginFile); } } // also delete folders if (!empty($resultFolders)) { foreach ($resultFolders as $folder) { if ($resultFolders[$folder]) { // if file was copied, try to delete it JFolder::delete(JPATH_ROOT . DS . 'plugins' . DS . $shConfig['folder'] . DS . $folder); } } } JError::RaiseWarning(500, JText::_('Could not install plugin')); } return $success; }
/** * Insert in the db the previously retrieved parameters for a plugin * including publication information. Also move files as required * * @param string $basePath , the base path to get original files from * @param array $shConfig an array holding the database parameters of the plugin * @param array $files, an array holding list of files from the plugin */ function shInsertPlugin($basePath, $shConfig, $files, $folders, $foldersToCopy) { // check data if (empty($files)) { return; } // move the files to target location $result = array(); $resultFolders = array(); $success = true; // create folders as needed if (!empty($folders)) { foreach ($folders as $folder) { $success = $success && JFolder::create(JPATH_ROOT . DS . 'plugins' . DS . $folder); } } // copy raw folders as needed if (!empty($foldersToCopy)) { foreach ($foldersToCopy as $folder) { $success = $success && JFolder::copy(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_sh404sef' . DS . 'plugins' . DS . $shConfig['folder'] . DS . $folder, JPATH_ROOT . DS . 'plugins' . DS . $shConfig['folder'] . DS . $folder); $result[$folder] = $success; } } // now move files across if ($success) { foreach ($files as $pluginFile) { $target = JPath::clean(JPATH_ROOT . DS . 'plugins' . DS . $shConfig['folder'] . DS . $pluginFile); $source = JPath::clean($basePath . DS . $pluginFile); $success = $success && true === JFile::copy($source, $target); $resultFolders[$pluginFile] = $success; } } // if files moved to destination, setup plugin in Joomla database if ($success) { // read stored params from disk shGetExtensionSavedParams($shConfig['folder'] . '.' . $shConfig['element'], $shConfig); // insert elements in db $db =& JFactory::getDBO(); $sql = "INSERT INTO `#__plugins` ( `name`, `element`, `folder`, `access`, `ordering`, `published`," . " `iscore`, `client_id`, `checked_out`, `checked_out_time`, `params`)" . " VALUES ('{$shConfig['name']}', '{$shConfig['element']}', '{$shConfig['folder']}', '{$shConfig['access']}', '{$shConfig['ordering']}'," . " '{$shConfig['published']}', '{$shConfig['iscore']}', '{$shConfig['client_id']}', '{$shConfig['checked_out']}'," . " '{$shConfig['checked_out_time']}', '{$shConfig['params']}');"; $db->setQuery($sql); $db->query(); } else { // don't leave anything behind foreach ($files as $pluginFile) { if ($result[$pluginFile]) { // if file was copied, try to delete it JFile::delete(JPATH_ROOT . DS . 'plugins' . DS . $shConfig['folder'] . DS . $pluginFile); } } // also delete folders if (!empty($resultFolders)) { foreach ($resultFolders as $folder) { if ($resultFolders[$folder]) { // if file was copied, try to delete it JFolder::delete(JPATH_ROOT . DS . 'plugins' . DS . $shConfig['folder'] . DS . $folder); } } } JError::RaiseWarning(500, JText::_('Could not install plugin')); } return $success; }