/** * Save fields for many-to-many relations in their pivot tables. * * @param F0FTable $table Current item table. * * @return bool True if the object can be saved successfully, false elsewhere. * @throws Exception The error message get trying to save fields into the pivot tables. */ public function onAfterStore(&$table) { // Retrieve the relations configured for this table $input = new F0FInput(); $key = $table->getConfigProviderKey() . '.relations'; $relations = $table->getConfigProvider()->get($key, array()); // Abandon the process if not a save task if (!in_array($input->getWord('task'), array('apply', 'save', 'savenew'))) { return true; } // For each relation check relative field foreach ($relations as $relation) { // Only if it is a multiple relation, sure! if ($relation['type'] == 'multiple') { // Retrive the fully qualified relation data from F0FTableRelations object $relation = array_merge(array('itemName' => $relation['itemName']), $table->getRelations()->getRelation($relation['itemName'], $relation['type'])); // Deduce the name of the field used in the form $field_name = F0FInflector::pluralize($relation['itemName']); // If field exists we catch its values! $field_values = $input->get($field_name, array(), 'array'); // If the field exists, build the correct pivot couple objects $new_couples = array(); foreach ($field_values as $value) { $new_couples[] = array($relation['ourPivotKey'] => $table->getId(), $relation['theirPivotKey'] => $value); } // Find existent relations in the pivot table $query = $table->getDbo()->getQuery(true)->select($relation['ourPivotKey'] . ', ' . $relation['theirPivotKey'])->from($relation['pivotTable'])->where($relation['ourPivotKey'] . ' = ' . $table->getId()); $existent_couples = $table->getDbo()->setQuery($query)->loadAssocList(); // Find new couples and create its foreach ($new_couples as $couple) { if (!in_array($couple, $existent_couples)) { $query = $table->getDbo()->getQuery(true)->insert($relation['pivotTable'])->columns($relation['ourPivotKey'] . ', ' . $relation['theirPivotKey'])->values($couple[$relation['ourPivotKey']] . ', ' . $couple[$relation['theirPivotKey']]); // Use database to create the new record if (!$table->getDbo()->setQuery($query)->execute()) { throw new Exception('Can\'t create the relation for the ' . $relation['pivotTable'] . ' table'); } } } // Now find the couples no more present, that will be deleted foreach ($existent_couples as $couple) { if (!in_array($couple, $new_couples)) { $query = $table->getDbo()->getQuery(true)->delete($relation['pivotTable'])->where($relation['ourPivotKey'] . ' = ' . $couple[$relation['ourPivotKey']])->where($relation['theirPivotKey'] . ' = ' . $couple[$relation['theirPivotKey']]); // Use database to create the new record if (!$table->getDbo()->setQuery($query)->execute()) { throw new Exception('Can\'t delete the relation for the ' . $relation['pivotTable'] . ' table'); } } } } } return true; }
/** * Sets the list of IDs from the request data * * @return F0FModel */ public function setIDsFromRequest() { // Get the ID or list of IDs from the request or the configuration $cid = $this->input->get('cid', array(), 'array'); $id = $this->input->getInt('id', 0); $kid = $this->input->getInt($this->getTable($this->table)->getKeyName(), 0); if (is_array($cid) && !empty($cid)) { $this->setIds($cid); } else { if (empty($id)) { $this->setId($kid); } else { $this->setId($id); } } return $this; }
public function importData() { $db = $this->getDbo(); $input = new F0FInput('files'); $file = $input->get('importfile', null, 'file', 2); // Sanity checks if (!$file) { $this->setError(JText::_('COM_ADMINTOOLS_IMPORTEXPORT_NOFILE')); return false; } $data = file_get_contents($file['tmp_name']); if ($data === false) { $this->setError(JText::_('COM_ADMINTOOLS_IMPORTEXPORT_ERR_READING_FILE')); return false; } $data = json_decode($data, true); if (!$data) { $this->setError(JText::_('COM_ADMINTOOLS_IMPORTEXPORT_ERR_READING_FILE')); return false; } // Everything seems ok, let's start importing data $result = true; if (isset($data['wafconfig'])) { /** @var AdmintoolsModelWafconfig $config */ $config = F0FModel::getTmpInstance('Wafconfig', 'AdmintoolsModel'); $config->saveConfig($data['wafconfig']); } if (isset($data['ipblacklist'])) { try { $db->truncateTable('#__admintools_ipblock'); $insert = $db->getQuery(true)->insert($db->qn('#__admintools_ipblock'))->columns(array($db->qn('ip'), $db->qn('description'))); // I could have several records, let's create a single big query foreach ($data['ipblacklist'] as $row) { $insert->values($db->q($row['ip']) . ', ' . $db->q($row['description'])); } $db->setQuery($insert)->execute(); } catch (Exception $e) { $this->setError(JText::_('COM_ADMINTOOLS_IMPORTEXPORT_ERR_BLACKLIST')); $result = false; } } if (isset($data['ipwhitelist'])) { try { $db->truncateTable('#__admintools_adminiplist'); // I could have several records, let's create a single big query $insert = $db->getQuery(true)->insert($db->qn('#__admintools_adminiplist'))->columns(array($db->qn('ip'), $db->qn('description'))); foreach ($data['ipwhitelist'] as $row) { $insert->values($db->q($row['ip']) . ', ' . $db->q($row['description'])); } $db->setQuery($insert)->execute(); } catch (Exception $e) { $this->setError(JText::_('COM_ADMINTOOLS_IMPORTEXPORT_ERR_WHITELIST')); $result = false; } } if (isset($data['badwords'])) { try { $db->truncateTable('#__admintools_badwords'); // I could have several records, let's create a single big query $insert = $db->getQuery(true)->insert($db->qn('#__admintools_badwords'))->columns(array($db->qn('word'))); foreach ($data['badwords'] as $row) { $insert->values($db->q($row['word'])); } $db->setQuery($insert)->execute(); } catch (Exception $e) { $this->setError(JText::_('COM_ADMINTOOLS_IMPORTEXPORT_ERR_BADWORDS')); $result = false; } } if (isset($data['emailtemplates'])) { try { $db->truncateTable('#__admintools_waftemplates'); } catch (Exception $e) { $this->setError(JText::_('COM_ADMINTOOLS_IMPORTEXPORT_ERR_EMAILTEMPLATES')); $result = false; } $table = F0FModel::getTmpInstance('Waftemplate', 'AdmintoolsModel')->getTable(); // Most likely I will only have 10-12 templates max, so I can use the table instead of directly writing inside the db foreach ($data['emailtemplates'] as $row) { $table->reset(); $table->admintools_waftemplate_id = null; // Let's leave primary key handling to the database unset($row['admintools_waftemplate_id']); unset($row['created_by']); unset($row['created_on']); unset($row['modified_by']); unset($row['modified_on']); // Calling the save method will trigger all the checks if (!$table->save($row)) { // There was an error, better stop here $this->setError(JText::_('COM_ADMINTOOLS_IMPORTEXPORT_ERR_EMAILTEMPLATES')); $result = false; break; } } } return $result; }
/** * This method will try retrieving a variable from the request (input) data. * * @param string $key The user state key for the variable * @param string $request The request variable name for the variable * @param F0FInput $input The F0FInput object with the request (input) data * @param mixed $default The default value. Default: null * @param string $type The filter type for the variable data. Default: none (no filtering) * @param boolean $setUserState Should I set the user state with the fetched value? * * @see F0FPlatformInterface::getUserStateFromRequest() * * @return mixed The value of the variable */ public function getUserStateFromRequest($key, $request, $input, $default = null, $type = 'none', $setUserState = true) { return $input->get($request, $default, $type); }
/** * This method will try retrieving a variable from the request (input) data. * * @param string $key The user state key for the variable * @param string $request The request variable name for the variable * @param F0FInput $input The F0FInput object with the request (input) data * @param mixed $default The default value. Default: null * @param string $type The filter type for the variable data. Default: none (no filtering) * @param boolean $setUserState Should I set the user state with the fetched value? * * @see F0FPlatformInterface::getUserStateFromRequest() * * @return mixed The value of the variable */ public function getUserStateFromRequest($key, $request, $input, $default = null, $type = 'none', $setUserState = true) { list($isCLI, $isAdmin) = $this->isCliAdmin(); if ($isCLI) { return $input->get($request, $default, $type); } $app = JFactory::getApplication(); if (method_exists($app, 'getUserState')) { $old_state = $app->getUserState($key, $default); } else { $old_state = null; } $cur_state = !is_null($old_state) ? $old_state : $default; $new_state = $input->get($request, null, $type); // Save the new value only if it was set in this request if ($setUserState) { if ($new_state !== null) { $app->setUserState($key, $new_state); } else { $new_state = $cur_state; } } elseif (is_null($new_state)) { $new_state = $cur_state; } return $new_state; }
/** * Get the content type for ucm * * @return string The content type alias */ public function getContentType() { if ($this->contentType) { return $this->contentType; } /** * When tags was first introduced contentType variable didn't exist - so we guess one * This will fail if content history behvaiour is enabled. This code is deprecated * and will be removed in F0F 3.0 in favour of the content type class variable */ $component = $this->input->get('option'); $view = F0FInflector::singularize($this->input->get('view')); $alias = $component . '.' . $view; return $alias; }
/** * sampleJoocialIntegration * * @param int $productId Param * * Example of how to save Joocial Advanced attributes * Copy-paste into your extension, and customize freely * * @return void */ private static function sampleJoocialIntegration($productId) { if (!defined('AUTOTWEET_API')) { include_once JPATH_ADMINISTRATOR . '/components/com_autotweet/api/autotweetapi.php'; } // Joocial - Saving Advanced Attrs $input = new F0FInput(); $autotweet_advanced = $input->get('autotweet_advanced_attrs', null, 'string'); if ($autotweet_advanced) { $advanced_attrs = AdvancedattrsHelper::retrieveAdvancedAttrs($autotweet_advanced); if ($advanced_attrs) { AdvancedattrsHelper::saveAdvancedAttrs($advanced_attrs, $productId); } } }
/** * _getAppKey * * @return string */ protected function getAppKey() { static $appKey = null; if ($appKey) { return $appKey; } $getInput = new F0FInput('GET'); $uri = (string) JFactory::getUri(); // Routed by GET if ($getInput->get('option')) { $appKey = md5($uri); return $appKey; } // Routed by POST // option - view - task - Itemid - lang $postInput = new F0FInput('POST'); if ($postInput->get('option')) { $buffer = array(); $buffer[] = $postInput->get('option'); $buffer[] = $postInput->get('view'); $buffer[] = $postInput->get('task'); $buffer[] = $postInput->get('Itemid'); $buffer[] = $postInput->get('lang'); $appKey = md5(implode('', $buffer)); return $appKey; } $appKey = md5($uri); return $appKey; }
/** * Obtain a request token from Twitter * * @return string */ public function getAccessToken() { $session = JFactory::getSession(); // Set the request token and secret we have stored $user_token = $session->get('authtoken'); $user_secret = $session->get('authsecret'); $this->access_token = $user_token; $this->token_secret = $user_secret; $this->login(); $input = new F0FInput(); $oauth_verifier = $input->get('oauth_verifier'); // Send request for an access token $this->_twitter->request('POST', $this->_twitter->url('oauth/access_token', ''), array('oauth_verifier' => $oauth_verifier)); if ($this->_twitter->response['code'] == 200) { // Get the access token and store it in a cookie $response = $this->_twitter->extract_params($this->_twitter->response['response']); $access_token = $response['oauth_token']; $access_token_secret = $response['oauth_token_secret']; return array('access_token' => $access_token, 'access_token_secret' => $access_token_secret); } return false; }
/** * buildQuery * * @param bool $overrideLimits Param * * @return F0FQuery */ public function buildQuery($overrideLimits = false) { $db = $this->getDbo(); $query = F0FQueryAbstract::getNew($db)->select('*')->from($db->quoteName('#__autotweet_requests')); $fltPublishup = $this->getState('publish_up', null, 'date'); if ($fltPublishup) { $fltPublishup = $fltPublishup . '%'; $query->where($db->qn('publish_up') . ' LIKE ' . $db->q($fltPublishup)); } $fltUntilDate = $this->getState('until_date', null, 'date'); if ($fltUntilDate) { $query->where($db->qn('publish_up') . ' <= ' . $db->q($fltUntilDate)); } $input = new F0FInput(); $start = $input->get('xtstart'); if ($start) { $date = new JDate($start); $query->where($db->qn('publish_up') . ' >= ' . $db->q($date->toSql())); } $end = $input->get('xtend'); if ($end) { $date = new JDate($end); $query->where($db->qn('publish_up') . ' <= ' . $db->q($date->toSql())); } $fltPlugin = $this->getState('plugin', null, 'string'); if ($fltPlugin) { $query->where($db->qn('plugin') . ' = ' . $db->q($fltPlugin)); } $fltRefId = $this->getState('ref_id', null, 'string'); if ($fltRefId) { $query->where($db->qn('ref_id') . ' = ' . $db->q($fltRefId)); } $fltRids = $this->getState('rids', null); if ($fltRids != '') { if (is_string($fltRids)) { $fltRids = TextUtil::listToArray($fltRids); } $list = array(); foreach ($fltRids as $p) { $list[] = $db->q($p); } $fltRids = implode(',', $list); $query->where($db->qn('id') . ' IN (' . $fltRids . ')'); } $fltTypeinfo = $this->getState('typeinfo', null, 'string'); if ($fltTypeinfo) { $query->where($db->qn('typeinfo') . ' = ' . $db->q($fltTypeinfo)); } $fltPublished = $this->getState('published', 0, 'int'); $query->where($db->qn('published') . ' = ' . $db->q($fltPublished)); $search = $this->getState('search', null); if ($search) { $search = '%' . $search . '%'; $query->where('(' . $db->qn('id') . ' LIKE ' . $db->quote($search) . ' OR ' . $db->qn('ref_id') . ' LIKE ' . $db->quote($search) . ' OR ' . $db->qn('description') . ' LIKE ' . $db->quote($search) . ' OR ' . $db->qn('url') . ' LIKE ' . $db->quote($search) . ')'); } AclPermsHelper::whereOwnership($query); $order = $this->getState('filter_order', 'publish_up', 'cmd'); if (!in_array($order, array_keys($this->getTable()->getData()))) { $order = 'publish_up'; } $dir = $this->getState('filter_order_Dir', 'ASC', 'cmd'); $query->order($order . ' ' . $dir); return $query; }
/** * isEnabledAttrComps * * @return bool */ public static function isEnabledAttrComps() { $input = new F0FInput(); $option = $input->get('option'); $controller = $input->get('controller', '-'); $view = $input->get('view', '-'); $layout = $input->get('layout', '-'); $task = $input->get('task', '-'); if (array_key_exists($option, self::$enabledAttrComps) && array_key_exists($controller, self::$enabledAttrComps[$option]) && array_key_exists($view, self::$enabledAttrComps[$option][$controller]) && array_key_exists($layout, self::$enabledAttrComps[$option][$controller][$view]) && array_key_exists($task, self::$enabledAttrComps[$option][$controller][$view][$layout])) { return self::$enabledAttrComps[$option][$controller][$view][$layout][$task]; } return false; }
/** * retrieveAdvancedAttrs * * @return void */ public function retrieveAdvancedAttrs() { if (!AUTOTWEETNG_JOOCIAL) { return; } $input = new F0FInput(); $autotweet_advanced = $input->get('autotweet_advanced_attrs', null, 'string'); if ($autotweet_advanced) { $this->advanced_attrs = AdvancedattrsHelper::retrieveAdvancedAttrs($autotweet_advanced); if (isset($this->advanced_attrs->ref_id)) { // Safe to save $this->saveAdvancedAttrs($this->advanced_attrs->ref_id); } } }
/** * Obtain a request token from Twitter * * @return string */ public function getAccessToken() { $session = JFactory::getSession(); // Set the request token and secret we have stored $oauth_token = $session->get('oauth_token'); $oauth_token_secret = $session->get('oauth_token_secret'); $this->oauth_user_token = $oauth_token; $this->oauth_user_secret = $oauth_token_secret; $this->login(); $input = new F0FInput(); $oauth_verifier = $input->get('oauth_verifier'); // Send request for an access token $response = $this->_linkedin->retrieveTokenAccess($oauth_token, $oauth_token_secret, $oauth_verifier); if ($response['info']['http_code'] == 200) { $oauth_user_token = $response['linkedin']['oauth_token']; $oauth_user_secret = $response['linkedin']['oauth_token_secret']; return array('oauth_user_token' => $oauth_user_token, 'oauth_user_secret' => $oauth_user_secret); } return false; }
/** * Renders a raw fieldset of a F0FForm and returns the corresponding HTML * * @param stdClass &$fieldset The fieldset to render * @param F0FForm &$form The form to render * @param F0FModel $model The model providing our data * @param F0FInput $input The input object * @param string $formType The form type e.g. 'edit' or 'read' * @param boolean $showHeader Should I render the fieldset's header? * * @return string The HTML rendering of the fieldset */ protected function renderFieldset(stdClass &$fieldset, F0FForm &$form, F0FModel $model, F0FInput $input, $formType, $showHeader = true) { $html = ''; $fields = $form->getFieldset($fieldset->name); if (isset($fieldset->class)) { $class = 'class="' . $fieldset->class . '"'; } else { $class = ''; } $element = empty($fields) ? 'div' : 'fieldset'; $html .= "\t" . '<' . $element . ' id="' . $fieldset->name . '" ' . $class . '>' . PHP_EOL; $isTabbedFieldset = $this->isTabFieldset($fieldset); if (isset($fieldset->label) && !empty($fieldset->label) && !$isTabbedFieldset) { $html .= "\t\t" . '<h3>' . JText::_($fieldset->label) . '</h3>' . PHP_EOL; } foreach ($fields as $field) { $groupClass = $form->getFieldAttribute($field->fieldname, 'groupclass', '', $field->group); // Auto-generate label and description if needed // Field label $title = $form->getFieldAttribute($field->fieldname, 'label', '', $field->group); $emptylabel = $form->getFieldAttribute($field->fieldname, 'emptylabel', false, $field->group); if (empty($title) && !$emptylabel) { $model->getName(); $title = strtoupper($input->get('option') . '_' . $model->getName() . '_' . $field->id . '_LABEL'); } // Field description $description = $form->getFieldAttribute($field->fieldname, 'description', '', $field->group); /** * The following code is backwards incompatible. Most forms don't require a description in their form * fields. Having to use emptydescription="1" on each one of them is an overkill. Removed. */ /* $emptydescription = $form->getFieldAttribute($field->fieldname, 'emptydescription', false, $field->group); if (empty($description) && !$emptydescription) { $description = strtoupper($input->get('option') . '_' . $model->getName() . '_' . $field->id . '_DESC'); } */ if ($formType == 'read') { $inputField = $field->static; } elseif ($formType == 'edit') { $inputField = $field->input; } if (empty($title)) { $html .= "\t\t\t" . $inputField . PHP_EOL; if (!empty($description) && $formType == 'edit') { $html .= "\t\t\t\t" . '<span class="help-block">'; $html .= JText::_($description) . '</span>' . PHP_EOL; } } else { $html .= "\t\t\t" . '<div class="fof-row ' . $groupClass . '">' . PHP_EOL; $html .= $this->renderFieldsetLabel($field, $form, $title); $html .= "\t\t\t\t" . $inputField . PHP_EOL; if (!empty($description)) { $html .= "\t\t\t\t" . '<span class="help-block">'; $html .= JText::_($description) . '</span>' . PHP_EOL; } $html .= "\t\t\t" . '</div>' . PHP_EOL; } } $element = empty($fields) ? 'div' : 'fieldset'; $html .= "\t" . '</' . $element . '>' . PHP_EOL; return $html; }
/** * Old static methods are now deprecated. This magic method makes sure there * is a continuity in our approach. The downside is that it's only compatible * with PHP 5.3.0. Sorry! * * @param string $name Name of the method we're calling * @param array $arguments The arguments passed to the method * * @return mixed */ public static function __callStatic($name, $arguments) { F0FPlatform::getInstance()->logDeprecated('F0FInput: static getXXX() methods are deprecated. Use the input object\'s methods instead.'); if (substr($name, 0, 3) == 'get') { // Initialise arguments $key = array_shift($arguments); $default = array_shift($arguments); $input = array_shift($arguments); $type = 'none'; $mask = 0; $type = strtolower(substr($name, 3)); if ($type == 'var') { $type = array_shift($arguments); $mask = array_shift($arguments); } if (is_null($type)) { $type = 'none'; } if (is_null($mask)) { $mask = 0; } if (!$input instanceof F0FInput && !$input instanceof JInput) { $input = new F0FInput($input); } return $input->get($key, $default, $type, $mask); } return false; }
/** * Applies CSRF protection by means of a standard Joomla! token (nonce) check. * Raises a 403 Access Forbidden error through the platform if the check fails. * * TODO Move this check inside the platform * * @return boolean True if the CSRF check is successful * * @throws Exception */ protected function _csrfProtection() { static $isCli = null, $isAdmin = null; if (is_null($isCli)) { $isCli = F0FPlatform::getInstance()->isCli(); $isAdmin = F0FPlatform::getInstance()->isBackend(); } switch ($this->csrfProtection) { // Never case 0: return true; break; // Always // Always case 1: break; // Only back-end and HTML format // Only back-end and HTML format case 2: if ($isCli) { return true; } elseif (!$isAdmin && $this->input->get('format', 'html', 'cmd') != 'html') { return true; } break; // Only back-end // Only back-end case 3: if (!$isAdmin) { return true; } break; } $hasToken = false; $session = JFactory::getSession(); // Joomla! 1.5/1.6/1.7/2.5 (classic Joomla! API) method if (method_exists('JUtility', 'getToken')) { $token = JUtility::getToken(); $hasToken = $this->input->get($token, false, 'none') == 1; if (!$hasToken) { $hasToken = $this->input->get('_token', null, 'none') == $token; } } // Joomla! 2.5+ (Platform 12.1+) method if (!$hasToken) { if (method_exists($session, 'getToken')) { $token = $session->getToken(); $hasToken = $this->input->get($token, false, 'none') == 1; if (!$hasToken) { $hasToken = $this->input->get('_token', null, 'none') == $token; } } } // Joomla! 2.5+ formToken method if (!$hasToken) { if (method_exists($session, 'getFormToken')) { $token = $session->getFormToken(); $hasToken = $this->input->get($token, false, 'none') == 1; if (!$hasToken) { $hasToken = $this->input->get('_token', null, 'none') == $token; } } } if (!$hasToken) { F0FPlatform::getInstance()->raiseError(403, JText::_('JLIB_APPLICATION_ERROR_ACCESS_FORBIDDEN')); return false; } }
/** * getControllerParams * * @return array */ public static function getControllerParams() { list($isCli, $isAdmin) = F0FDispatcher::isCliAdmin(); $input = new F0FInput(); $option = $input->get('option'); $controller = $input->get('controller'); $task = $input->get('task'); $view = $input->get('view'); $layout = $input->get('layout'); $id = $input->get('id', null, 'int'); if (!$id) { $cid = $input->get('cid', array(), 'ARRAY'); if (is_array($cid) && count($cid) == 1) { $id = $cid[0]; } elseif (is_numeric($cid) && $cid > 0) { $id = $cid; } } // EasyBlog if (!$id) { $id = $input->get('blogid', null, 'int'); } // JoomShopping if (!$id) { $id = $input->get('product_id', null, 'int'); } // Content - Front if (!$id) { $id = $input->get('a_id', null, 'int'); } // SobiPro if (!$id) { $id = $input->get('sid', null, 'int'); } // Zoo - Front if (!$id) { $id = $input->get('item_id', null, 'int'); } // Joocial - Composer if (!$id) { $id = $input->get('ref_id', null, 'cmd'); } return array($isAdmin, $option, $controller, $task, $view, $layout, $id); }