/** * Not really an AJAX call, we just use the pluginAjax method so we can run this * method for handling scripted downloads. * * @return void */ public function onAjax_download() { $input = $this->app->input; $this->setId($input->getInt('element_id')); $this->loadMeForAjax(); $this->getElement(); $params = $this->getParams(); $url = $input->server->get('HTTP_REFERER', '', 'string'); $this->lang->load('com_fabrik.plg.element.fabrikfileupload', JPATH_ADMINISTRATOR); $rowId = $input->get('rowid', '', 'string'); $repeatCount = $input->getInt('repeatcount', 0); $listModel = $this->getListModel(); $row = $listModel->getRow($rowId, false, true); if (!$this->canView()) { $this->app->enqueueMessage(FText::_('PLG_ELEMENT_FILEUPLOAD_DOWNLOAD_NO_PERMISSION')); $this->app->redirect($url); exit; } if (empty($rowId)) { $errMsg = FText::_('PLG_ELEMENT_FILEUPLOAD_DOWNLOAD_NO_SUCH_FILE'); $errMsg .= FabrikHelperHTML::isDebug() ? ' (empty rowid)' : ''; $this->app->enqueueMessage($errMsg); $this->app->redirect($url); exit; } if (empty($row)) { $errMsg = FText::_('PLG_ELEMENT_FILEUPLOAD_DOWNLOAD_NO_SUCH_FILE'); $errMsg .= FabrikHelperHTML::isDebug() ? " (no such row)" : ''; $this->app->enqueueMessage($errMsg); $this->app->redirect($url); exit; } $aclEl = $this->getFormModel()->getElement($params->get('fu_download_acl', ''), true); if (!empty($aclEl)) { $aclEl = $aclEl->getFullName(); $aclElRaw = $aclEl . '_raw'; $groups = $this->user->getAuthorisedViewLevels(); $canDownload = in_array($row->{$aclElRaw}, $groups); if (!$canDownload) { $this->app->enqueueMessage(FText::_('PLG_ELEMENT_FILEUPLOAD_DOWNLOAD_NO_PERMISSION')); $this->app->redirect($url); } } $storage = $this->getStorage(); $elName = $this->getFullName(true, false); $filePath = $row->{$elName}; $filePath = FabrikWorker::JSONtoData($filePath, false); $filePath = is_object($filePath) ? FArrayHelper::fromObject($filePath) : (array) $filePath; $filePath = FArrayHelper::getValue($filePath, $repeatCount); $filePath = $storage->getFullPath($filePath); $fileContent = $storage->read($filePath); if ($fileContent !== false) { $thisFileInfo = $storage->getFileInfo($filePath); if ($thisFileInfo === false) { $errMsg = FText::_('PLG_ELEMENT_FILEUPLOAD_DOWNLOAD_NO_SUCH_FILE'); $errMsg .= FabrikHelperHTML::isDebug(true) ? ' (path: ' . $filePath . ')' : ''; $this->app->enqueueMessage($errMsg); $this->app->redirect($url); exit; } // Some time in the past header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); header('Accept-Ranges: bytes'); header('Content-Length: ' . $thisFileInfo['filesize']); header('Content-Type: ' . $thisFileInfo['mime_type']); header('Content-Disposition: attachment; filename="' . $thisFileInfo['filename'] . '"'); // Serve up the file echo $fileContent; // $this->downloadEmail($row, $filePath); $this->downloadHit($rowId, $repeatCount); $this->downloadLog($row, $filePath); // And we're done. exit; } else { $this->app->enqueueMessage(FText::_('PLG_ELEMENT_FILEUPLOAD_DOWNLOAD_NO_SUCH_FILE')); $this->app->redirect($url); exit; } }
/** * the function called from the preg_replace_callback - replace the {} with the correct HTML * * @param string $match plug-in match * * @return void */ protected function replace($match) { $app = JFactory::getApplication(); $input = $app->input; $match = $match[0]; $match = trim($match, "{"); $match = trim($match, "}"); $ref = preg_replace('/[^A-Z|a-z|0-9]/', '_', $match); $match = $this->parse(array($match)); $match = explode(" ", $match); array_shift($match); $user = JFactory::getUser(); $usersConfig = JComponentHelper::getParams('com_fabrik'); $unused = array(); // Special case if we are wanting to write in an element's data $element = false; $repeatCounter = 0; // Was defaulting to 1 but that forced filters to show in cal viz even with showfilter=no option turned on $showFilters = $input->get('showfilters', null); $clearFilters = $input->get('clearfilters', 0); $resetFilters = $input->get('resetfilters', 0); $this->origRequestVars = array(); $id = 0; $origLayout = $input->get('layout'); $origFLayout = $input->get('flayout'); $layoutFound = false; $rowId = ''; $useKey = ''; $limit = false; $ajax = true; $session = JFactory::getSession(); $usersConfig->set('rowid', 0); $viewName = ''; foreach ($match as $m) { $m = explode('=', $m); // $$$ hugh - deal with %20 as space in arguments $m[1] = urldecode(FArrayHelper::getValue($m, 1)); switch ($m[0]) { case 'view': $viewName = JString::strtolower($m[1]); break; case 'id': case 'formid': case 'listid': // Cast to int in case there are two spaces after value. $id = (int) $m[1]; break; case 'layout': $layoutFound = true; $layout = $m[1]; $input->set('layout', $layout); break; case 'row': case 'rowid': $row = $m[1]; // When printing the content the rowid can't be passed in the querystring so don't set here if ($row !== '{rowid}') { if ($row == -1) { $row = $user->get('id'); } $usersConfig->set('rowid', $row); // Set the rowid in the session so that print pages can grab it again $session->set('fabrik.plgcontent.rowid', $row); $rowId = $row; } break; case 'element': // {fabrik view=element list=3 rowid=364 element=fielddatatwo} $viewName = 'list'; $element = $m[1]; break; case 'table': case 'list': $listId = $m[1]; break; case 'limit': $limit = $m[1]; case 'usekey': $useKey = $m[1]; break; case 'repeatcounter': $repeatCounter = $m[1]; break; case 'showfilters': $showFilters = $m[1]; break; case 'ajax': $ajax = (bool) $m[1]; break; // $$$ rob for these 2 grab the qs var in priority over the plugin settings // $$$ rob for these 2 grab the qs var in priority over the plugin settings case 'clearfilters': $clearFilters = $input->get('clearfilters', $m[1]); break; case 'resetfilters': $resetFilters = $input->get('resetfilters', $m[1]); break; default: if (array_key_exists(1, $m)) { /** * These are later set as app->input vars if present in list view * html_entity_decode to allow for content plugin values to contain * Urlencode the value for plugin statements such as: asylum_events___start_date[condition]=> */ $unused[] = trim($m[0]) . '=' . urlencode(html_entity_decode($m[1], ENT_NOQUOTES)); } } } // Get the rowid in the session so that print pages can use it // Commented out - was messing with element rendering and seems to general to be correct. IE what if more than one content plugin being used // $rowId = $session->get('fabrik.plgcontent.rowid', $rowId); if ($viewName == 'table') { // Some backwards compat with fabrik 2 $viewName = 'list'; } // Moved out of switch as otherwise first plugin to use this will effect all subsequent plugins $input->set('usekey', $useKey); // If no layout defined - set it if (!$layoutFound) { $thisLayout = $input->get('layout'); // Also test for saving an article if ($input->get('option') === 'com_content' && ($thisLayout === 'blog' || $app->isAdmin() && $thisLayout === 'edit')) { $layout = 'default'; $input->set('layout', $layout); } } /* $$$ hugh - added this so the fabrik2article plugin can arrange to have form CSS * included when the article is rendered by com_content, by inserting ... * {fabrik view=form_css id=X layout=foo} * ... at the top of the article. */ if ($viewName == 'form_css') { // The getFormCss() call blows up if we don't do this jimport('joomla.filesystem.file'); $this->generalIncludes('form'); $controller = $this->getController('form', $id); $view = $this->getView($controller, 'form', $id); $model = $this->getModel($controller, 'form', $id); if (!$model) { return; } $model->setId($id); $model->setEditable(false); $layout = !empty($layout) ? $layout : 'default'; $view->setModel($model, true); $model->getFormCss($layout); return ''; } $this->generalIncludes($viewName); if ($element !== false) { // Special case for rendering element data $controller = $this->getController('list', $listId); $model = $this->getModel($controller, 'list', $listId); if (!$model) { return; } $model->setId($listId); $formModel = $model->getFormModel(); $groups = $formModel->getGroupsHiarachy(); foreach ($groups as $groupModel) { $elements = $groupModel->getMyElements(); foreach ($elements as &$elementModel) { // $$$ rob 26/05/2011 changed it so that you can pick up joined elements without specifying plugin // param 'element' as joinx[x][fullname] but simply 'fullname' if ($element == $elementModel->getFullName(true, false)) { $activeEl = $elementModel; continue 2; } } } // $$$ hugh in case they have a typo in their elementname if (empty($activeEl) || !$activeEl->isPublished()) { throw new RuntimeException('You are trying to embed an element called ' . $element . ' which is not present in the list or has been unpublished'); } if ($rowId === '') { $rows = $model->getData(); $group = array_shift($rows); $row = array_shift($group); $res = $row->{$element}; } else { $this->_setRequest($unused); $row = $model->getRow($rowId, false, true); if (substr($element, JString::strlen($element) - 4, JString::strlen($element)) !== '_raw') { $element = $element . '_raw'; } // $$$ hugh - need to pass all row data, or calc elements that use {placeholders} won't work //$defaultData = is_object($row) ? get_object_vars($row) : $row; $defaultData = is_object($row) ? FArrayHelper::fromObject($row, true) : $row; /* $$$ hugh - if we don't do this, our passed data gets blown away when render() merges the form data * not sure why, but apparently if you do $foo =& $bar and $bar is NULL ... $foo ends up NULL */ $activeEl->getFormModel()->data = $defaultData; $activeEl->editable = false; // Set row id for things like user element $origRowId = $input->get('rowid'); $input->set('rowid', $rowId); // Set detail view for things like youtube element $origView = $input->get('view'); $input->set('view', 'details'); $defaultData = (array) $defaultData; unset($activeEl->defaults); if ($repeatCounter === 'all') { $repeat = $activeEl->getGroupModel()->repeatCount(); $res = array(); for ($j = 0; $j < $repeat; $j++) { $res[] = $activeEl->render($defaultData, $j); } $res = count($res) > 1 ? '<ul><li>' . implode('</li><li>', $res) . '</li></ul>' : $res[0]; } else { $res = $activeEl->render($defaultData, $repeatCounter); $ref = $activeEl->elementJavascript($repeatCounter); } $input->set('rowid', $origRowId); $input->set('view', $origView); $this->resetRequest(); } return $res; } if (!isset($viewName)) { return; } $origId = $input->get('id', '', 'string'); $origView = $input->get('view'); // Allow for random=1 (which has to map to fabrik_random for list views) $origRandom = $input->get('fabrik_random'); // For fabble $input->set('origid', $origId); $input->set('origview', $origView); $input->set('id', $id); $input->set('view', $viewName); /* * $$$ hugh - at least make the $origId available for certain corner cases, like ... * http://fabrikar.com/forums/showthread.php?p=42960#post42960 */ $input->set('origid', $origId, 'GET', false); $cacheKey = $id; if ($rowId !== '') { $cacheKey .= '.' . $rowId; } $controller = $this->getController($viewName, $cacheKey); $view = $this->getView($controller, $viewName, $cacheKey); if ($model = $this->getModel($controller, $viewName, $cacheKey)) { $view->setModel($model, true); } // Display the view $view->error = $controller->getError(); $view->isMambot = true; $displayed = false; // Do some view specific code switch ($viewName) { case 'form_css': $model->getFormCss(); break; case 'form': case 'details': if ($id === 0) { $app->enqueueMessage('No id set in fabrik plugin declaration', 'warning'); return; } $model->ajax = $ajax; $model->setId($id); unset($model->groups); // Set default values set in plugin declaration // - note cant check if the form model has the key' as its not yet loaded $this->_setRequest($unused); // $$$ rob - flayout is used in form/details view when _isMamot = true $input->set('flayout', $input->get('layout')); $input->set('rowid', $rowId); break; case 'csv': case 'table': case 'list': /* $$$ rob 15/02/2011 added this as otherwise when you filtered on a table * with multiple filter set up subsequent tables were showing * the first tables data */ if ($input->get('activelistid') == '') { $input->set('activelistid', $input->getId('listid')); } $input->set('listid', $id); // Allow for simple limit=2 in plugin declaration if ($limit) { $limitKey = 'limit' . $id; $this->origRequestVars[$limitKey] = $input->get($limitKey); $input->set($limitKey, $limit); } $this->_setRequest($unused); $input->set('fabrik_random', $input->get('random', $origRandom)); $input->set('showfilters', $showFilters); $input->set('clearfilters', $clearFilters); $input->set('resetfilters', $resetFilters); if ($id === 0) { $app->enqueueMessage('No id set in fabrik plugin declaration', 'warning'); return; } $model->setId($id); $model->isMambot = true; /** * * Reset this otherwise embedding a list in a list menu page, the embedded list takes the show in list fields from the menu list * * $$$ hugh - nasty little hack to reduce 'emptyish' array, 'cos if no 'elements' in the request, the following ends up setting * returning an array with a single empty string. This ends up meaning that we render a list with no * elements in it. We've run across this before, so we have a FArrayHelper:;emptyish() to detect it. */ $show_in_list = explode('|', $input->getString('elements', '')); if (FArrayHelper::emptyIsh($show_in_list, true)) { $show_in_list = array(); } $input->set('fabrik_show_in_list', $show_in_list); $model->ajax = $ajax; $task = $input->get('task'); if (method_exists($controller, $task) && $input->getInt('activetableid') == $id) { /* * Enable delete() of rows * list controller deals with display after tasks is called * set $displayed to true to stop controller running twice */ $displayed = true; ob_start(); $controller->{$task}(); $result = ob_get_contents(); ob_end_clean(); } $model->setOrderByAndDir(); $formModel = $model->getFormModel(); break; case 'visualization': $input->set('showfilters', $showFilters); $input->set('clearfilters', $clearFilters); $input->set('resetfilters', $resetFilters); $this->_setRequest($unused); break; } // Hack for gallery viz as it may not use the default view $controller->isMambot = true; if (!$displayed) { ob_start(); if (method_exists($model, 'reset')) { $model->reset(); } $controller->display($model); $result = ob_get_contents(); ob_end_clean(); } $input->set('id', $origId); $input->set('view', $origView); if ($origLayout != '') { $input->set('layout', $origLayout); } if ($origFLayout != '') { $input->set('flayout', $origFLayout); } if ($origRandom) { $input->set('fabrik_random', $origRandom); } $this->resetRequest(); return $result; }
/** * From admin the user has authorize the plugin for this app. * Lets store the data for it. * As we are in a pop up window we need to set some js to update the parent window's * parameters * * @return void */ public function onUpdateAdmin() { $input = $this->app->input; $formModel = $this->buildModel($input->getInt('formid')); $params = $formModel->getParams(); $renderOrder = $input->get('repeatCounter'); $consumerKey = FArrayHelper::fromObject($params->get('twitter_consumer_key')); $consumerKey = $consumerKey[$renderOrder]; $consumerSecret = FArrayHelper::fromObject($params->get('twitter_consumer_secret')); $consumerSecret = $consumerSecret[$renderOrder]; $connection = new TwitterOAuth($consumerKey, $consumerSecret, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']); /* Request access tokens from twitter */ $access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $_REQUEST['oauth_verifier'])); // Save the access token to the element params $formModel = JModelLegacy::getInstance('Form', 'FabrikFEModel'); $formModel->setId($input->getInt('formid')); $row = $formModel->getForm(); $params = $formModel->getParams(); $opts = $params->toArray(); $counter = $input->get('repeatCounter'); $pairs = array('twitter_oauth_token' => 'oauth_token', 'twitter_oauth_token_secret' => 'oauth_token_secret', 'twitter_oauth_user' => 'screen_name'); $js = array(); $jsValues = array(); foreach ($pairs as $paramname => $requestname) { $tokens = (array) FArrayHelper::getValue($opts, $paramname); $newtokens = array(); for ($i = 0; $i <= $counter; $i++) { $newtokens[$i] = $i == $counter ? $access_token[$requestname] : ''; $jsid = '#jform_params_' . $paramname . '-' . $i; //$js[] = "window.opener.document.getElement('$jsid').value = '$newtokens[$i]';"; $jsValues[] = array($jsid, $newtokens[$i]); } $opts[$paramname] = $newtokens; } $json = json_encode($jsValues); $row->params = json_encode($opts); $row->store(); $langFile = 'com_fabrik.plg.form.fabriktwitter'; $this->lang->load($langFile, JPATH_ADMINISTRATOR, null, true); // If we had already authorized the app then we will still be in the admin page - so update the fields: echo FText::_('PLG_FORM_TWITTER_CREDENTIALS_SAVED'); $document = JFactory::getDocument(); //$script = implode("\n", $js) . " $script = <<<EOT window.opener.postMessage({$json}, '*'); (function() {window.close()}).delay(4000); EOT; $document->addScriptDeclaration($script); }