/** * Saves data from the quickStart pane on main dashboard */ public function saveqcontrol() { // Check for request forgeries. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); try { // get current settings for sh404SEF $params = Sh404sefHelperGeneral::getComponentParams($forceRead = true); // set params from the form $params->set('Enabled', JRequest::getInt('Enabled', 0)); $params->set('canReadRemoteConfig', JRequest::getInt('canReadRemoteConfig', 0)); $params->set('shRewriteMode', JRequest::getInt('shRewriteMode', 1)); $params->set('shSecEnableSecurity', JRequest::getInt('shSecEnableSecurity', 1)); // convert to json and store into db $textParams = $params->toString(); ShlDbHelper::update('#__extensions', array('params' => $textParams), array('element' => 'com_sh404sef', 'type' => 'component')); JFactory::getApplication()->enqueueMessage(JText::_('COM_SH404SEF_ELEMENT_SAVED')); } catch (Exception $e) { JFactory::getApplication()->enqueueMessage(JText::_('COM_SH404SEF_ELEMENT_NOT_SAVED'), 'error'); } parent::display(); }
/** * Make the url with id = $cid the main url * in case of duplicates. Also set the previous * main url as secondary, swapping their rank * * @param integer $cid */ public function makeMainUrl($cid) { // get this url record $newMain = $this->getById($cid); $error = $this->getError(); if (!empty($error)) { return; } // is this already the main url ? if ($newMain->rank == 0) { return; } // now get the current main url $options = array('rank' => 0, 'oldurl' => $newMain->oldurl); $previousMains = $this->getByAttr($options); try { // if we got it, do the swapping if (!empty($previousMains)) { foreach ($previousMains as $previousMain) { ShlDbHelper::update($this->_getTableName(), array('rank' => $newMain->rank), array('id' => $previousMain->id)); // another thing we have to do is attach any meta data to the new // main url, so that they keep showing. Meta data are attached to // a NON-sef url, which has the benefit of keeping the attachement // whenever sef url creations are modified and sef urls recreated // but require a bit more work in that case ShlDbHelper::update('#__sh404sef_metas', array('newurl' => $newMain->newurl), array('newurl' => $previousMain->newurl)); } } // finally make it the main url ShlDbHelper::update($this->_getTableName(), array('rank' => 0), array('id' => $newMain->id)); } catch (Exception $e) { $this->setError('Internal database error # ' . $e->getMessage()); } }
function shAddSefUrlToDBAndCache($nonSefUrl, $sefString, $rank, $urlType) { $db = ShlDbHelper::getDb(); $sefString = JString::ltrim($sefString, '/'); // V 1.2.4.t just in case you forgot to remove leading slash switch ($urlType) { case sh404SEF_URLTYPE_AUTO: $dateAdd = '0000-00-00'; break; case sh404SEF_URLTYPE_CUSTOM: $dateAdd = date("Y-m-d"); break; case sh404SEF_URLTYPE_NONE: return null; break; } try { $query = ''; if ($urlType == sh404SEF_URLTYPE_AUTO) { $result = ShlDbHelper::quoteQuery('select ??, ?? from ?? where ?? = ? and (?? = ? or ?? = ?)', array('id', 'newurl', '#__sh404sef_urls', 'oldurl', 'newurl', 'newurl'), array($sefString, '', addslashes(urldecode($nonSefUrl))))->shlLoadObject(); if (!empty($result)) { // sef urls was found either as a 404 or as already existing, with also the same non-sef if ($result->newurl == $nonSefUrl) { // url already in db, nothing to do ShlSystem_Log::debug('sh404sef', 'url already in db, nothing to do'); return true; } ShlDbHelper::update('#__sh404sef_urls', array('newurl' => addslashes(urldecode($nonSefUrl)), 'rank' => $rank, 'dateadd' => $dateAdd), array('oldurl' => $sefString)); } else { // another option: sef exists, but with another non-sef: that's a duplicate // need to check that $result = ShlDbHelper::selectObject('#__sh404sef_urls', array('id', 'newurl', 'rank'), $db->quoteName('oldurl') . ' = ? and ' . $db->quoteName('newurl') . ' <> ?', array($sefString, addslashes(urldecode($nonSefUrl))), array('rank' => 'desc')); if (!empty($result)) { // we found at least one identical SEF url, with another non-sef. Mark the new one as duplicate of the old one $rank = $result->rank + 1; } ShlDbHelper::insert('#__sh404sef_urls', array('oldurl' => $sefString, 'newurl' => $nonSefUrl, 'rank' => $rank, 'dateadd' => $dateAdd)); } } // store new sef/non-sef pair in memory cache Sh404sefHelperCache::addSefUrlToCache($nonSefUrl, $sefString, $urlType); // create shURL : get a shURL model, and ask url creation $model = ShlMvcModel_Base::getInstance('pageids', 'Sh404sefModel'); $model->createPageId($sefString, $nonSefUrl); } catch (Exception $e) { ShlSystem_Log::error('sh404sef', '%s::%s::%d: %s', __CLASS__, __METHOD__, __LINE__, $e->getMessage()); } }
/** * Save an url to the database, updating various elements * at the same time like ranking of duplicates * * @param integer $type force url type, used when saving a custom url */ private function _saveUrl($type = sh404SEF_URLTYPE_AUTO) { // check for homepage handling if (!empty($this->_data['newurl']) && ($this->_data['newurl'] == '/' || $this->_data['newurl'] == sh404SEF_HOMEPAGE_CODE)) { $this->_saveHomeUrl(); return sh404SEF_HOMEPAGE_CODE; } // check for importing urls : if importing, rank will already be set in // incoming data. If saving a url from the UI, rank is never set // as it is caculated upon saving the url $importing = isset($this->_data['rank']); // get required tools jimport('joomla.database.table'); $row = JTable::getInstance($this->_defaultTable, 'Sh404sefTable'); try { // now bind incoming data to table row if (!$row->bind($this->_data)) { $this->setError($row->getError()); return 0; } // pre-save checks if (!$row->check()) { $this->setError($row->getError()); return 0; } // must load cache from disk, so that it can be written back later, with new url require_once JPATH_ROOT . '/components/com_sh404sef/shCache.php'; shLoadURLCache(); // find if we are adding a custom or automatic url $urlType = $row->dateadd == '0000-00-00' ? sh404SEF_URLTYPE_AUTO : sh404SEF_URLTYPE_CUSTOM; // override with user supplied if (!empty($type)) { $urlType = $type; } // adjust date added field if needed if ($urlType == sh404SEF_URLTYPE_CUSTOM) { $row->dateadd = date("Y-m-d"); } // if custom url, and no language string, let's add default one if ($urlType == sh404SEF_URLTYPE_CUSTOM && !preg_match('/(&|\\?)lang=[a-zA-Z]{2,3}/iUu', $row->newurl)) { $shTemp = explode('-', shGetDefaultLang()); $shLangTemp = $shTemp[0] ? $shTemp[0] : 'en'; $row->newurl .= '&lang=' . $shLangTemp; } // normalize the non-sef url representation, sorting query parts alphabetically $row->newurl = shSortUrl($row->newurl); // retrieve previous values of sef and non sef urls $previousSefUrl = JRequest::getVar('previousSefUrl', null, 'POST'); $previousNonSefUrl = JRequest::getVar('previousNonSefUrl', null, 'POST'); // if both were set, and nothing has changed, then nothing to do if (!empty($previousSefUrl) && !empty($previousNonSefUrl) && $previousNonSefUrl == $row->newurl && $previousSefUrl == $row->oldurl) { // nothing changed ! must be changing meta or aliases $this->_url = $row; return $row->id; } // search DB for urls pairs with same SEF url $query = 'SELECT * FROM #__sh404sef_urls WHERE binary oldurl = ' . $this->_db->Quote($row->oldurl) . ' ORDER BY rank ASC'; $this->_db->setQuery($query); $dbUrlList = $this->_db->shlLoadObjectList(); // do we have urls in the db with same SEF ? if (count($dbUrlList) > 0) { // yes we do // get config object $sefConfig = Sh404sefFactory::getConfig(); if (!$sefConfig->shRecordDuplicates) { // we don't allow duplicates : reject this URL $this->setError(JText::_('COM_SH404SEF_DUPLICATE_NOT_ALLOWED')); } else { // same SEF, but we allow duplicates $existingRecord = null; // importing meta data for instance foreach ($dbUrlList as $urlInDB) { // same SEF, but is the incoming non-sef in this list of URl with same SEF ? if ($urlInDB->newurl == $row->newurl) { $existingRecord = $urlInDB; $this->setError(JText::_('COM_SH404SEF_URLEXIST')); } } if (empty($existingRecord)) { // this new non-sef does not already exists $shTemp = array('nonSefURL' => $row->newurl); // which means we must update the record for the old non-sef url Sh404sefHelperCache::removeURLFromCache($shTemp); // remove the old url from cache // then find new rank (as we are adding a duplicate, we add it at the end of the duplicate list) // but only if not importing. When importing, rank is already set if (!$importing) { $row->rank = $dbUrlList[count($dbUrlList) - 1]->rank + 1; } // store will create a new record if id=0, or update existing if id non 0 $row->store(); // put custom URL in DB and cache Sh404sefHelperCache::addSefUrlToCache($row->newurl, $row->oldurl, $urlType); // we must add the previous SEF url to the alias list, only if // - not already there // - this sef url does not already exists in the DB; which will happen if the url // being saved was customized and also had duplicates // TODO this code is duplicated just a few line below, need refactoring if (!empty($previousSefUrl) && strpos($this->_data['shAliasList'], $previousSefUrl) === false) { // check if not already a valid SEF url in the DB $query = 'SELECT count(id) FROM #__sh404sef_urls WHERE binary oldurl = ' . $this->_db->Quote($previousSefUrl); $this->_db->setQuery($query); $isThere = $this->_db->shlLoadResult(); if (empty($isThere)) { $this->_data['shAliasList'] .= $previousSefUrl . "\n"; } } } else { // there is already a record with both this sef and non sef. // just do nothing but return success (ie: record id). // Later, controller may store new aliases or metas // This should never happen when saving regular data as we added // a check for this case earlier // May happen when importing though $this->_url = $row; return $row->id; } // additional step : if we are here, it may be because we have modified // an existing url, and specifically changed it sef value to something else // Now it may be that this record was the one with rank = 0 in a series // of duplicate urls. If so, the urls which was ranked above must now become // the main url, having rank = 0 // note : when importing, we don't enter this test as previousSefUrl is empty // TODO this code is duplicated just a few line below, need refactoring if (!empty($previousSefUrl) && $previousSefUrl != $row->newurl) { // search for the old #2 record in duplicate list $query = 'SELECT id FROM #__sh404sef_urls WHERE binary oldurl = ' . $this->_db->Quote($previousSefUrl) . ' ORDER BY rank ASC'; $this->_db->setQuery($query); $previousRanked2 = $this->_db->shlLoadObject(); // there was more than one duplicate in the same series, promote #2 to top spot if (!empty($previousRanked2)) { $query = 'UPDATE #__sh404sef_urls SET rank="0" WHERE id = ' . $this->_db->Quote($previousRanked2->id); $this->_db->setQuery($query); $this->_db->shlQuery(); } } } } else { // there is no URL with same SEF URL, we are customizing an existing SEF url $shTemp = array('nonSefURL' => $row->newurl); Sh404sefHelperCache::removeURLFromCache($shTemp); // remove it from cache // simply store URL. If there is already one with same non-sef, this will raise an error in store() // as we don't allow creating a custom url for an already existing non-sef. User should // directly edit the existing non-sef/sef pair if (!$row->check()) { $this->setError($row->getError()); return 0; } if (!$row->store()) { $this->setError($row->getError()); return 0; } // add also to cache if saved to db Sh404sefHelperCache::addSefUrlToCache($row->newurl, $row->oldurl, $urlType); // we must add the previous SEF url to the alias list, only if // - not already there // - this sef url does not already exists in the DB; which will happen if the url // begin saved was customized and also had duplicates // note : when importing, we don't enter this test as previousSefUrl is empty // TODO this code is duplicated just a few line above, need refactoring if (!empty($previousSefUrl) && strpos($this->_data['shAliasList'], $previousSefUrl) === false) { // check if not already a valid SEF url in the DB $query = 'SELECT count(id) FROM #__sh404sef_urls WHERE binary oldurl = ' . $this->_db->Quote($previousSefUrl); $this->_db->setQuery($query); $isThere = $this->_db->shlLoadResult(); if (empty($isThere)) { $this->_data['shAliasList'] .= $previousSefUrl . "\n"; } } // finally, also check db for urls with same sef as previous SEF if any. We need // to search for the first duplicate of this old sef, and set it to be // the new main url // note : when importing, we don't enter this test as previousSefUrl is empty // TODO this code is duplicated just a few line above, need refactoring if (!empty($previousSefUrl) && $previousSefUrl != $row->newurl) { // search for the old #2 record in duplicate list $query = 'SELECT id FROM #__sh404sef_urls WHERE binary oldurl = ' . $this->_db->Quote($previousSefUrl) . ' ORDER BY rank ASC'; $this->_db->setQuery($query); $previousRanked2 = $this->_db->shlLoadObject(); // there was more than one duplicate in the same series, promote #2 to top spot if (!empty($previousRanked2)) { ShlDbHelper::update('#__sh404sef_urls', array('rank' => 0), array('id' => $previousRanked2->id)); } } } // store saved url object $this->_url = $row; // return what should be a non-zero id return $this->_url->id; } catch (Exception $e) { ShlSystem_Log::error('sh404sef', '%s::%s::%d: %s', __CLASS__, __METHOD__, __LINE__, $e->getMessage()); $this->setError($e->getMessage()); return 0; } }
/** * Prepare saving of Error documents configuration options set */ private function _saveErrordocs($errorPagecontent) { // update 404 error page $quoteGPC = get_magic_quotes_gpc(); $shIntroText = empty($_POST) ? '' : ($quoteGPC ? stripslashes($errorPagecontent) : $errorPagecontent); try { // is there already a 404 page article? $id = ShlDbHelper::selectResult('#__content', 'id', array('title' => '__404__', 'catid' => Sh404sefHelperCategories::getSh404sefContentCat()->id, 'language' => '*')); if (!empty($id)) { // yes, update it ShlDbHelper::update('#__content', array('introtext' => $shIntroText, 'modified' => date("Y-m-d H:i:s")), array('id' => $id)); } else { $catid = Sh404sefHelperCategories::getSh404sefContentCat()->id; if (empty($catid)) { $this->setError(JText::_('COM_SH404SEF_CANNOT_SAVE_404_NO_UNCAT')); return; } $contentTable = JTable::getInstance('content'); $content = array('title' => '__404__', 'alias' => '__404__', 'title_alias' => '__404__', 'introtext' => $shIntroText, 'state' => 1, 'catid' => $catid, 'attribs' => '{"menu_image":"-1","show_title":"0","show_section":"0","show_category":"0","show_vote":"0","show_author":"0","show_create_date":"0","show_modify_date":"0","show_pdf_icon":"0","show_print_icon":"0","show_email_icon":"0","pageclass_sfx":""', 'language' => '*'); $saved = $contentTable->save($content); if (!$saved) { $this->setError($contentTable->getError()); } } } catch (Exception $e) { $this->setError($e->getMEssage()); } }
private function _shInstallModule($module, $source, $extensionConfig, $moduleConfig) { $app = JFactory::getApplication(); $path = $source . '/admin/modules/' . $module; $installer = new JInstaller(); $result = $installer->install($path); if ($result) { // if files moved to destination, setup module in Joomla database $shouldRestore = $this->_shShouldRestore(); if ($shouldRestore) { // read stored params from disk $this->_shGetExtensionSavedParams($module . '_extension', $extensionConfig); } // update elements in db, only if we need to restore past configuration try { if (!empty($extensionConfig)) { // load module details from extension table $moduleDetails = ShlDbHelper::selectAssoc('#__extensions', array('*'), array('type' => 'module', 'element' => $module)); // merge with saved details and write back to disk $details = array_merge($moduleDetails, $extensionConfig); ShlDbHelper::update('#__extensions', $details, array('extension_id' => (int) $moduleDetails['extension_id'])); } } catch (Exception $e) { $app->enqueueMessage('Error: ' . $e->getMessage()); } if ($shouldRestore) { // read stored params from disk $this->_shGetExtensionSavedParams($module . '_modules', $moduleConfig); } // update elements in db, if we need to restore past configuration try { $instanceDetails = ShlDbHelper::selectAssoc('#__modules', array('*'), array('module' => $module)); // merge with saved details and write back to disk $details = array_merge($instanceDetails, $moduleConfig); ShlDbHelper::update('#__modules', $details, array('id' => (int) $instanceDetails['id'])); } catch (Exception $e) { $app->enqueueMessage('Error: ' . $e->getMessage()); } // and finally we make sure there is a menu item associated with the module $details = array('menuid' => 0); if ($shouldRestore) { // read stored params from disk $this->_shGetExtensionSavedParams($module . '_modules_menu', $details); } $details = array_merge($details, array('moduleid' => (int) $instanceDetails['id'])); // insert or update elements in db, if we need to restore past configuration try { ShlDbHelper::insertUpdate('#__modules_menu', $details, array('moduleid' => (int) $instanceDetails['id'])); } catch (Exception $e) { $app->enqueueMessage('Error: ' . $e->getMessage()); } } else { $app->enqueueMessage('Error installing sh404sef module: ' . $module); } return $result; }