* @git Joomla Component Builder * @copyright Copyright (C) 2015 Vast Development Method. All rights reserved. * @license GNU General Public License version 2 or later; see LICENSE.txt */ // No direct access to this file defined('_JEXEC') or die('Restricted access'); use Joomla\CMS\Factory; use Joomla\CMS\Language\Text; use Joomla\CMS\MVC\Controller\AdminController; use Joomla\Utilities\ArrayHelper; use Joomla\CMS\Router\Route; use Joomla\CMS\Session\Session; use VDM\Joomla\Componentbuilder\Compiler\Factory as CFactory; use Joomla\CMS\Version; use VDM\Joomla\Utilities\ArrayHelper as UtilitiesArrayHelper; use VDM\Joomla\Utilities\StringHelper; /** * Compiler Admin Controller */ class ComponentbuilderControllerCompiler extends AdminController { /** * The prefix to use with controller messages. * * @var string * @since 1.6 */ protected $text_prefix = 'COM_COMPONENTBUILDER_COMPILER'; /** * Proxy for getModel. * @since 2.5 */ public function getModel($name = 'Compiler', $prefix = 'ComponentbuilderModel', $config = []) { $model = parent::getModel($name, $prefix, array('ignore_request' => true)); return $model; } public function dashboard() { $this->setRedirect(Route::_('index.php?option=com_componentbuilder', false)); return; } /** * get all the animations used in the compiler * * @return true on success */ public function getDynamicContent() { // Check for request forgeries Session::checkToken() or jexit(Text::_('JINVALID_TOKEN')); // check if user has the right $user = Factory::getUser(); // set page redirect $redirect_url = Route::_('index.php?option=com_componentbuilder&view=compiler', false); $message = Text::_('COM_COMPONENTBUILDER_YOU_DO_NOT_HAVE_PERMISSION_TO_DOWNLOAD_THE_COMPILER_ANIMATIONS'); // currently only those with permissions can get these images if($user->authorise('compiler.compiler_animations', 'com_componentbuilder')) { // get the model $model = $this->getModel('compiler'); if ($model->getDynamicContent($message)) { $message = Text::_('COM_COMPONENTBUILDER_BALL_THE_COMPILER_ANIMATIONS_WERE_SUCCESSFULLY_DOWNLOADED_TO_THIS_JOOMLA_INSTALLB'); $this->setRedirect($redirect_url, $message, 'message'); return true; } } $this->setRedirect($redirect_url, $message, 'error'); return false; } /** * Run the Compiler * * @return true on success */ public function compiler() { // Check for request forgeries Session::checkToken() or jexit(Text::_('JINVALID_TOKEN')); // check if user has the right $user = Factory::getUser(); // currently only those with admin access can compile a component if($user->authorise('core.manage', 'com_componentbuilder')) { $model = $this->getModel('compiler'); if ($model->builder()) { $cache = Factory::getCache('mod_menu'); $cache->clean(); // TODO: Reset the users acl here as well to kill off any missing bits } else { return false; } // switch to set multi install button $add_multi_install = false; $add_plugin_install = false; $add_module_install = false; // get application $app = Factory::getApplication(); // set redirection URL $redirect_url = $app->getUserState('com_componentbuilder.redirect_url'); // get system messages $message = $app->getUserState('com_componentbuilder.message'); if (empty($redirect_url) && CFactory::_('Config')->component_id > 0) { // start new message $message = array(); // update the redirection URL $redirect_url = Route::_('index.php?option=com_componentbuilder&view=compiler', false); if (($pos = strpos($model->compiler->filepath['component'], "/tmp/")) !== FALSE) { $url = \JUri::root() . substr($model->compiler->filepath['component'], $pos + 1); } // check if we have plugins $add_plugin_install = UtilitiesArrayHelper::check($model->compiler->filepath['plugins'], true); // check if we have modules $add_module_install = UtilitiesArrayHelper::check($model->compiler->filepath['modules'], true); // if a multi install we set another kind of header if ($add_plugin_install || $add_module_install) { // Message of successful builds $message[] = '

The Extensions were Successfully Compiled!

'; $message[] = '

You can install any one of the following extensions!

'; // set multi install $add_multi_install = true; } else { // Message of successful build $message[] = '

The (' . $model->compiler->filepath['component-folder'] . ') was Successfully Compiled!

'; } if (CFactory::_('Config')->joomla_version == Version::MAJOR_VERSION) { $message[] = '

'; // check if we have modules if ($add_module_install) { foreach ($model->compiler->filepath['modules-folder'] as $module_id => $module_folder) { $message[] = '

'; } } // check if we have plugins if ($add_plugin_install) { foreach ($model->compiler->filepath['plugins-folder'] as $plugin_id => $plugin_folder) { $message[] = '

'; } } // set multi install button if ($add_multi_install) { $message[] = '

You can install all compiled extensions!

'; $message[] = '

'; } } $message[] = '

Total time saved

'; $message[] = ''; $message[] = '

#'.'##totalHours##'.'# Hours or #'.'##totalDays##'.'# Eight Hour Days (actual time you saved)
'; $message[] = '(if creating a folder and file took 5 seconds and writing one line of code took 10 seconds, never making one mistake or taking any coffee break.)
'; $message[] = '#'.'##actualHoursSpent##'.'# Hours or #'.'##actualDaysSpent##'.'# Eight Hour Days (the actual time you spent)
'; $message[] = '(with the following break down: debugging @#'.'##debuggingHours##'.'#hours = codingtime / 4; planning @#'.'##planningHours##'.'#hours = codingtime / 7; mapping @#'.'##mappingHours##'.'#hours = codingtime / 10; office @#'.'##officeHours##'.'#hours = codingtime / 6;)

'; $message[] = '

#'.'##actualTotalHours##'.'# Hours or #'.'##actualTotalDays##'.'# Eight Hour Days (a total of the realistic time frame for this project)
'; $message[] = '(if creating a folder and file took 5 seconds and writing one line of code took 10 seconds, with the normal everyday realities at the office, that includes the component planning, mapping & debugging.)

'; $message[] = '

Project duration: '.$model->compiler->projectWeekTime. ' weeks or #'.'##projectMonthTime##'.'# months

'; // check if we have modules or plugins if ($add_multi_install) { $message[] = '

Path to Zip Files

'; $message[] = '

Component Path: ' . $model->compiler->filepath['component'] . '
'; $message[] = 'Component URL: ' . $url . '

'; // load modules if found if ($add_module_install) { $module_urls = array(); // load the modules path/url foreach ($model->compiler->filepath['modules'] as $module_id => $module_path) { // set module path $message[] = 'Module Path: ' . $module_path . '
'; if (($pos = strpos($module_path, "/tmp/")) !== FALSE) { $module_urls[$module_id] = \JUri::root() . substr($module_path, $pos + 1); $message[] = 'Module URL: ' . $module_urls[$module_id] . '
'; } } } // load plugins if found if ($add_plugin_install) { $plugin_urls = array(); // load the plugins path/url foreach ($model->compiler->filepath['plugins'] as $plugin_id => $plugin_path) { // set plugin path $message[] = 'Plugin Path: ' . $plugin_path . '
'; if (($pos = strpos($plugin_path, "/tmp/")) !== FALSE) { $plugin_urls[$plugin_id] = \JUri::root() . substr($plugin_path, $pos + 1); $message[] = 'Plugin URL: ' . $plugin_urls[$plugin_id] . '
'; } } } $message[] = '
Hey! you can also download these zip files right now!
'; $message[] = 'Download Component  '; // load the module download URL's if (isset($module_urls) && UtilitiesArrayHelper::check($module_urls)) { foreach ($module_urls as $module_id => $module_url) { $message[] = ' '; $message[] = 'Download ' . $model->compiler->filepath['modules-folder'][$module_id] . '  '; } } // load the plugin download URL's if (isset($plugin_urls) && UtilitiesArrayHelper::check($plugin_urls)) { foreach ($plugin_urls as $plugin_id => $plugin_url) { $message[] = ' '; $message[] = 'Download ' . $model->compiler->filepath['plugins-folder'][$plugin_id] . '  '; } } $message[] = '

'; $message[] = '

Remember! These zip files are in your tmp folder and therefore publicly accessible until you click [Clear tmp]!

'; } else { $message[] = '

Path to Zip File

'; $message[] = '

Path: ' . $model->compiler->filepath['component'] . '
'; $message[] = 'URL: ' . $url . '

'; $message[] = 'Hey! you can also download the zip file right now!
'; $message[] = 'Download

'; $message[] = '

Remember! This zip file is in your tmp folder and therefore publicly accessible until you click [Clear tmp]!

'; } $message[] = '

Compilation took #'.'##COMPILER_TIMER##'.'# seconds to complete.

'; // pass the message via the user state... wow this is painful $app->setUserState('com_componentbuilder.success_message', CFactory::_('Placeholder')->update( implode(PHP_EOL, $message), CFactory::_('Compiler.Builder.Content.One')->allActive() ) ); // set redirect $this->setRedirect($redirect_url, '

Successful Build!

', 'message'); $app->setUserState('com_componentbuilder.component_folder_name', $model->compiler->filepath['component-folder']); // check if we have modules if ($add_module_install) { $app->setUserState('com_componentbuilder.modules_folder_name', $model->compiler->filepath['modules-folder']); } // check if we have plugins if ($add_plugin_install) { $app->setUserState('com_componentbuilder.plugins_folder_name', $model->compiler->filepath['plugins-folder']); } } else { // wipe out the user state when we're going to redirect $app->setUserState('com_componentbuilder.redirect_url', ''); $app->setUserState('com_componentbuilder.message', ''); $app->setUserState('com_componentbuilder.extension_message', ''); $app->setUserState('com_componentbuilder.component_folder_name', ''); $app->setUserState('com_componentbuilder.modules_folder_name', ''); $app->setUserState('com_componentbuilder.plugins_folder_name', ''); $app->setUserState('com_componentbuilder.success_message', ''); // set redirect $this->setRedirect($redirect_url, $message); } return true; } return false; } /** * Install All Compiled Extensions * * @return true on success */ public function installCompiledExtensions() { // Check for request forgeries Session::checkToken() or jexit(Text::_('JINVALID_TOKEN')); // check if user has the right $user = Factory::getUser(); // set page redirect $redirect_url = Route::_('index.php?option=com_componentbuilder&view=compiler', false); $message = Text::_('COM_COMPONENTBUILDER_YOU_DO_NOT_HAVE_PERMISSION_TO_INSTALL_THESE_EXTENSIONS'); // currently only those with admin access can install a component via JCB if($user->authorise('core.manage')) { $message = Text::_('COM_COMPONENTBUILDER_COULD_NOT_INSTALL_EXTENSIONS'); $_message = array('success' => array(), 'error' => array()); $app = Factory::getApplication(); // start file name array $fileNames = array(); $fileNames[] = $app->getUserState('com_componentbuilder.component_folder_name', null); // check if we have modules $fileNames = UtilitiesArrayHelper::merge(array($fileNames, $app->getUserState('com_componentbuilder.modules_folder_name', array()) )); // check if we have plugins $fileNames = UtilitiesArrayHelper::merge(array($fileNames, $app->getUserState('com_componentbuilder.plugins_folder_name', array()) )); // wipe out the user c-m-p since we are done with them all $app->setUserState('com_componentbuilder.component_folder_name', ''); $app->setUserState('com_componentbuilder.modules_folder_name', ''); $app->setUserState('com_componentbuilder.plugins_folder_name', ''); $app->setUserState('com_componentbuilder.success_message', ''); // loop and install all extensions found foreach ($fileNames as $fileName) { if ($this->installExtension($fileName)) { $_message['success'][] = Text::sprintf('COM_COMPONENTBUILDER_SZIP_WAS_REMOVED_THE_FROM_TMP_FOLDER_DURING_INSTALLATION', $fileName); } else { $_message['error'][] = Text::sprintf('COM_COMPONENTBUILDER_SZIP_COULD_NOT_BE_INSTALLED', $fileName); } } // catch errors if (UtilitiesArrayHelper::check($_message['error'])) { $app->enqueueMessage(implode('
', $_message['error']), 'Error'); } // build success message if (UtilitiesArrayHelper::check($_message['success'])) { $this->setRedirect($redirect_url, implode('
', $_message['success']), 'message'); return true; } } $this->setRedirect($redirect_url, $message, 'error'); return false; } /** * Install Compiled Component * * @return true on success */ public function installCompiledComponent() { // Check for request forgeries Session::checkToken() or jexit(Text::_('JINVALID_TOKEN')); // check if user has the right $user = Factory::getUser(); // set page redirect $redirect_url = Route::_('index.php?option=com_componentbuilder&view=compiler', false); $message = Text::_('COM_COMPONENTBUILDER_YOU_DO_NOT_HAVE_PERMISSION_TO_INSTALL_THE_COMPONENT'); // currently only those with admin access can install a component via JCB if($user->authorise('core.manage')) { $message = Text::_('COM_COMPONENTBUILDER_COULD_NOT_INSTALL_COMPONENT'); $app = Factory::getApplication(); $fileName = $app->getUserState('com_componentbuilder.component_folder_name'); // wipe out the user c-m-p since we are done with them all $app->setUserState('com_componentbuilder.component_folder_name', ''); $app->setUserState('com_componentbuilder.modules_folder_name', ''); $app->setUserState('com_componentbuilder.plugins_folder_name', ''); $app->setUserState('com_componentbuilder.success_message', ''); if ($this->installExtension($fileName)) { $message = Text::sprintf('COM_COMPONENTBUILDER_ONLY_SZIP_FILE_WAS_REMOVED_THE_FROM_TMP_FOLDER_DURING_INSTALLATION', $fileName); $this->setRedirect($redirect_url, $message, 'message'); return true; } } $this->setRedirect($redirect_url, $message, 'error'); return false; } /** * Install Compiled Module * * @return true on success */ public function installCompiledModule() { // Check for request forgeries Session::checkToken() or jexit(Text::_('JINVALID_TOKEN')); // check if user has the right $user = Factory::getUser(); // set page redirect $redirect_url = Route::_('index.php?option=com_componentbuilder&view=compiler', false); $message = Text::_('COM_COMPONENTBUILDER_YOU_DO_NOT_HAVE_PERMISSION_TO_INSTALL_THE_MODULE'); // currently only those with admin access can install a molule via JCB if($user->authorise('core.manage')) { $message = Text::_('COM_COMPONENTBUILDER_COULD_NOT_INSTALL_MODULE'); $app = Factory::getApplication(); $fileNames = $app->getUserState('com_componentbuilder.modules_folder_name'); // wipe out the user c-m-p since we are done with them all $app->setUserState('com_componentbuilder.component_folder_name', ''); $app->setUserState('com_componentbuilder.modules_folder_name', ''); $app->setUserState('com_componentbuilder.plugins_folder_name', ''); $app->setUserState('com_componentbuilder.success_message', ''); if (UtilitiesArrayHelper::check($fileNames)) { $jinput = Factory::getApplication()->input; $moduleId = $jinput->post->get('install_item_id', 0, 'INT'); if ($moduleId > 0 && isset($fileNames[$moduleId]) && $this->installExtension($fileNames[$moduleId])) { $message = Text::sprintf('COM_COMPONENTBUILDER_ONLY_SZIP_FILE_WAS_REMOVED_THE_FROM_TMP_FOLDER_DURING_INSTALLATION', $fileNames[$moduleId]); $this->setRedirect($redirect_url, $message, 'message'); return true; } } } $this->setRedirect($redirect_url, $message, 'error'); return false; } /** * Install Compiled Plugin * * @return true on success */ public function installCompiledPlugin() { // Check for request forgeries Session::checkToken() or jexit(Text::_('JINVALID_TOKEN')); // check if user has the right $user = Factory::getUser(); // set page redirect $redirect_url = Route::_('index.php?option=com_componentbuilder&view=compiler', false); $message = Text::_('COM_COMPONENTBUILDER_YOU_DO_NOT_HAVE_PERMISSION_TO_INSTALL_THE_PLUGIN'); // currently only those with admin access can install a plugin via JCB if($user->authorise('core.manage')) { $message = Text::_('COM_COMPONENTBUILDER_COULD_NOT_INSTALL_PLUGIN'); $app = Factory::getApplication(); $fileNames = $app->getUserState('com_componentbuilder.plugins_folder_name'); // wipe out the user c-m-p since we are done with them all $app->setUserState('com_componentbuilder.component_folder_name', ''); $app->setUserState('com_componentbuilder.modules_folder_name', ''); $app->setUserState('com_componentbuilder.plugins_folder_name', ''); $app->setUserState('com_componentbuilder.success_message', ''); if (UtilitiesArrayHelper::check($fileNames)) { $jinput = Factory::getApplication()->input; $pluginId = $jinput->post->get('install_item_id', 0, 'INT'); if ($pluginId > 0 && isset($fileNames[$pluginId]) && $this->installExtension($fileNames[$pluginId])) { $message = Text::sprintf('COM_COMPONENTBUILDER_ONLY_SZIP_FILE_WAS_REMOVED_THE_FROM_TMP_FOLDER_DURING_INSTALLATION', $fileNames[$pluginId]); $this->setRedirect($redirect_url, $message, 'message'); return true; } } } $this->setRedirect($redirect_url, $message, 'error'); return false; } /** * Install Extension * * @return true on success */ protected function installExtension($fileName) { // check that the model is set if (!isset($this->_compiler_model)) { // get the compiler model $this->_compiler_model = $this->getModel('compiler'); } // set the language if not set if (!isset($this->_installer_lang)) { $this->_installer_lang = Factory::getLanguage(); $extension = 'com_installer'; $base_dir = JPATH_ADMINISTRATOR; $language_tag = 'en-GB'; $reload = true; $this->_installer_lang->load($extension, $base_dir, $language_tag, $reload); } // make sure we have a string if (StringHelper::check($fileName)) { return $this->_compiler_model->install($fileName.'.zip'); } return false; } /** * Run the Expansion * * @return void */ public function runExpansion() { // Check for request forgeries Session::checkToken() or \jexit(Text::_('JINVALID_TOKEN')); // check if user has the right $user = Factory::getUser(); // set page redirect $redirect_url = \JRoute::_('index.php?option=com_componentbuilder&view=compiler', false); // set massage $message = Text::_('COM_COMPONENTBUILDER_YOU_DO_NOT_HAVE_PERMISSION_TO_RUN_THE_EXPANSION_MODULE'); // check if this user has the right to run expansion if($user->authorise('compiler.run_expansion', 'com_componentbuilder')) { // set massage $message = Text::_('COM_COMPONENTBUILDER_EXPANSION_FAILED_PLEASE_CHECK_YOUR_SETTINGS_IN_THE_GLOBAL_OPTIONS_OF_JCB_UNDER_THE_DEVELOPMENT_METHOD_TAB'); // run expansion via API $result = ComponentbuilderHelper::getFileContents(\JUri::root() . 'index.php?option=com_componentbuilder&task=api.expand'); // is there a message returned if (!is_numeric($result) && StringHelper::check($result)) { $this->setRedirect($redirect_url, $result); return true; } elseif (is_numeric($result) && 1 == $result) { $message = Text::_('COM_COMPONENTBUILDER_BTHE_EXPANSION_WAS_SUCCESSFULLYB_TO_SEE_MORE_INFORMATION_CHANGE_THE_BRETURN_OPTIONS_FOR_BUILDB_TO_BDISPLAY_MESSAGEB_IN_THE_GLOBAL_OPTIONS_OF_JCB_UNDER_THE_DEVELOPMENT_METHOD_TABB'); $this->setRedirect($redirect_url, $message, 'message'); return true; } } $this->setRedirect($redirect_url, $message, 'error'); return false; } /** * Clear tmp folder * * @return true on success */ public function clearTmp() { // Check for request forgeries Session::checkToken() or \jexit(Text::_('JINVALID_TOKEN')); // check if user has the right $user = Factory::getUser(); // set page redirect $redirect_url = Route::_('index.php?option=com_componentbuilder&view=compiler', false); $message = Text::_('COM_COMPONENTBUILDER_COULD_NOT_CLEAR_THE_TMP_FOLDER'); if($user->authorise('compiler.clear_tmp', 'com_componentbuilder') && $user->authorise('core.manage', 'com_componentbuilder')) { // get the model $model = $this->getModel('compiler'); // get tmp folder $comConfig = Factory::getConfig(); $tmp = $comConfig->get('tmp_path'); if ($model->emptyFolder($tmp)) { $message = Text::_('COM_COMPONENTBUILDER_BTHE_TMP_FOLDER_HAS_BEEN_CLEAR_SUCCESSFULLYB'); $this->setRedirect($redirect_url, $message, 'message'); // get application $app = Factory::getApplication(); // wipe out the user c-m-p since we are done with them all $app->setUserState('com_componentbuilder.component_folder_name', ''); $app->setUserState('com_componentbuilder.modules_folder_name', ''); $app->setUserState('com_componentbuilder.plugins_folder_name', ''); $app->setUserState('com_componentbuilder.success_message', ''); return true; } } $this->setRedirect($redirect_url, $message, 'error'); return false; } /** * Run the Translator * * @return void */ public function runTranslator() { // Check for request forgeries Session::checkToken() or \jexit(Text::_('JINVALID_TOKEN')); // check if user has the right $user = Factory::getUser(); // set page redirect $redirect_url = Route::_('index.php?option=com_componentbuilder&view=compiler', false); // set massage $message = Text::_('COM_COMPONENTBUILDER_YOU_DO_NOT_HAVE_PERMISSION_TO_RUN_THE_TRANSLATOR_MODULE'); // check if this user has the right to run expansion if($user->authorise('compiler.run_translator', 'com_componentbuilder')) { // set massage $message = Text::_('COM_COMPONENTBUILDER_TRANSLATION_FAILED_SINCE_THERE_ARE_NO_COMPONENTS_LINKED_WITH_TRANSLATION_TOOLS'); // run translator via API $result = ComponentbuilderHelper::getFileContents(Uri::root() . 'index.php?option=com_componentbuilder&task=api.translator'); // is there a message returned if (!is_numeric($result) && StringHelper::check($result)) { $this->setRedirect($redirect_url, $result); return true; } } $this->setRedirect($redirect_url, $message, 'error'); return false; } }