PageRenderTime 65ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_sef/classes/seftools.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 1287 lines | 888 code | 210 blank | 189 comment | 198 complexity | 3aee2ab152da73d243f839b70dffc677 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * SEF component for Joomla!
  4. *
  5. * @package JoomSEF
  6. * @version 4.2.8
  7. * @author ARTIO s.r.o., http://www.artio.net
  8. * @copyright Copyright (C) 2012 ARTIO s.r.o.
  9. * @license GNU/GPLv3 http://www.artio.net/license/gnu-general-public-license
  10. */
  11. // Check to ensure this file is included in Joomla!
  12. defined('_JEXEC') or die();
  13. require_once (JPATH_ROOT . DS . 'administrator' . DS . 'components' . DS . 'com_sef' . DS . 'tables' . DS . 'extension.php');
  14. jimport('joomla.filesystem.file');
  15. jimport('joomla.form.form');
  16. define('_COM_SEF_PRIORITY_DEFAULT_ITEMID', 90);
  17. define('_COM_SEF_PRIORITY_DEFAULT', 95);
  18. JLoader::register('SefExt', JPATH_ROOT.DS.'components'.DS.'com_sef'.DS.'sef.ext.php');
  19. JLoader::register('JoomSEF', JPATH_ROOT.DS.'components'.DS.'com_sef'.DS.'joomsef.php');
  20. class SEFTools
  21. {
  22. function getSEFVersion()
  23. {
  24. static $version;
  25. if (! isset($version)) {
  26. $xml = JFactory::getXMLParser('Simple');
  27. $xmlFile = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_sef' . DS . 'sef.xml';
  28. if (JFile::exists($xmlFile)) {
  29. if ($xml->loadFile($xmlFile)) {
  30. $root = & $xml->document;
  31. $element = & $root->getElementByPath('version');
  32. $version = $element ? $element->data() : '';
  33. }
  34. }
  35. }
  36. return $version;
  37. }
  38. function getSEFInfo()
  39. {
  40. static $info;
  41. if( !isset($info) ) {
  42. $info = array();
  43. $xml = JFactory::getXMLParser('Simple');
  44. $xmlFile = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_sef' . DS . 'sef.xml';
  45. if (JFile::exists($xmlFile)) {
  46. if ($xml->loadFile($xmlFile)) {
  47. $root = & $xml->document;
  48. $element = & $root->getElementByPath('version');
  49. $info['version'] = $element ? $element->data() : '';
  50. $element = & $root->getElementByPath('creationdate');
  51. $info['creationDate'] = $element ? $element->data() : '';
  52. $element = & $root->getElementByPath('author');
  53. $info['author'] = $element ? $element->data() : '';
  54. $element = & $root->getElementByPath('authoremail');
  55. $info['authorEmail'] = $element ? $element->data() : '';
  56. $element = & $root->getElementByPath('authorurl');
  57. $info['authorUrl'] = $element ? $element->data() : '';
  58. $element = & $root->getElementByPath('copyright');
  59. $info['copyright'] = $element ? $element->data() : '';
  60. $element = & $root->getElementByPath('license');
  61. $info['license'] = $element ? $element->data() : '';
  62. $element = & $root->getElementByPath('description');
  63. $info['description'] = $element ? $element->data() : '';
  64. }
  65. }
  66. }
  67. return $info;
  68. }
  69. function getExtVersion($extension)
  70. {
  71. $xml = & SEFTools::getExtXML($extension);
  72. $version = null;
  73. if ($xml) {
  74. $root =$xml;
  75. $ver = $root->getAttribute('version');
  76. if (($root->name() == 'extension') && version_compare($ver, '1.5', '>=') && ($root->getAttribute('type') == 'sef_ext')) {
  77. $element = & $root->version;
  78. $version = $element ? $element->data() : '';
  79. }
  80. }
  81. return $version;
  82. }
  83. /**
  84. * Returns extension name from its XML file.
  85. *
  86. * @return string
  87. */
  88. function getExtName($extension)
  89. {
  90. $xml = & SEFTools::getExtXML($extension);
  91. $name = null;
  92. if ($xml) {
  93. $root = & $xml;
  94. $ver = $root->getAttribute('version');
  95. if (($root->name() == 'extension') && version_compare($ver, '1.5', '>=') && ($root->getAttribute('type') == 'sef_ext')) {
  96. $element = & $root->name;
  97. $name = $element ? $element->data() : '';
  98. }
  99. }
  100. return $name;
  101. }
  102. /**
  103. * Returns the extension XML object
  104. *
  105. * @param string $extension Extension option
  106. * @return JSimpleXML Extension XML
  107. */
  108. function getExtXML($extension)
  109. {
  110. static $xmls;
  111. if (! isset($xmls)) {
  112. $xmls = array();
  113. }
  114. if (! isset($xmls[$extension])) {
  115. $xmls[$extension] = null;
  116. $xmlFile = JPATH_ROOT . DS . 'components' . DS . 'com_sef' . DS . 'sef_ext' . DS . $extension . '.xml';
  117. if (JFile::exists($xmlFile)) {
  118. $xmls[$extension] = JFactory::getXML($xmlFile);
  119. }
  120. }
  121. return $xmls[$extension];
  122. }
  123. function &getExtAcceptVars($option, $includeGlobal = true)
  124. {
  125. static $acceptVars;
  126. if( !isset($acceptVars) ) {
  127. $acceptVars = array();
  128. }
  129. if( !isset($acceptVars[$option]) ) {
  130. $sefConfig =& SEFConfig::getConfig();
  131. $params =& SEFTools::getExtParams($option);
  132. $aVars = trim($params->get('acceptVars', ''));
  133. if( $aVars == '' ) {
  134. $acceptVars[$option] = array();
  135. }
  136. else {
  137. $aVars = explode(';', $aVars);
  138. $aVars = array_map('trim', $aVars);
  139. $acceptVars[$option] = $aVars;
  140. }
  141. }
  142. return $acceptVars[$option];
  143. }
  144. function &getExtFilters($option, $includeGlobal = true)
  145. {
  146. static $filters;
  147. if( !isset($filters) ) {
  148. $filters = array();
  149. }
  150. if( !isset($filters[$option]) ) {
  151. $filters[$option] = array();
  152. $filters[$option]['pos'] = array();
  153. $filters[$option]['neg'] = array();
  154. $db =& JFactory::getDBO();
  155. $element = str_replace('com_', 'ext_joomsef4_', $option);
  156. $query=$db->getQuery(true);
  157. $query->select('custom_data')->from('#__extensions')->where('type='.$db->quote('sef_ext'))->where('state>=0')->where('enabled=1')->where('element='.$db->quote($element));
  158. $db->setQuery($query);
  159. $row = $db->loadResult();
  160. if( $row ) {
  161. // Parse the filters
  162. $rules = explode("\n", $row);
  163. $rules = array_map('trim', $rules);
  164. if( count($rules) > 0 ) {
  165. foreach($rules as $rule) {
  166. // Is the rule positive or negative?
  167. if( $rule[0] == '+' ) {
  168. $type = 'pos';
  169. }
  170. else if( $rule[0] == '-' ) {
  171. $type = 'neg';
  172. }
  173. else {
  174. continue;
  175. }
  176. $rule = substr($rule, 1);
  177. // Split the rule to regexp and variables parts
  178. $pos = strrpos($rule, '=');
  179. if( $pos === false ) {
  180. continue;
  181. }
  182. $re = substr($rule, 0, $pos);
  183. $vars = substr($rule, $pos + 1);
  184. if ($re == '') {
  185. continue;
  186. }
  187. // Create the filter object
  188. $filter = new stdClass();
  189. $filter->rule = $re;
  190. if ($vars != '') {
  191. $filter->vars = array_map('trim', explode(',', $vars));
  192. }
  193. else {
  194. $filter->vars = array();
  195. }
  196. // Add the filter to filters
  197. $filters[$option][$type][] = $filter;
  198. }
  199. }
  200. }
  201. }
  202. return $filters[$option];
  203. }
  204. function &getExtFiltersByVars($option, $includeGlobal = true)
  205. {
  206. static $byVars;
  207. if( !isset($byVars) ) {
  208. $byVars = array();
  209. }
  210. if (empty($option)) {
  211. $option = '_default';
  212. }
  213. if( !isset($byVars[$option]) ) {
  214. $byVars[$option] = array();
  215. // Get filters
  216. $filters =& SEFTools::getExtFilters($option, $includeGlobal);
  217. if( count($filters) > 0 ) {
  218. // Loop through filter types (pos, neg)
  219. foreach($filters as $type => $typeFilters) {
  220. if( count($typeFilters) > 0 ) {
  221. // Loop through filters
  222. foreach($typeFilters as $filter) {
  223. if( count($filter->vars) > 0 ) {
  224. // Loop through variables
  225. foreach($filter->vars as $var) {
  226. // Add filter to var and type
  227. if( !isset($byVars[$option][$var]) ) {
  228. $byVars[$option][$var] = array();
  229. }
  230. if( !isset($byVars[$option][$var][$type]) ) {
  231. $byVars[$option][$var][$type] = array();
  232. }
  233. $byVars[$option][$var][$type][] = $filter->rule;
  234. }
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241. return $byVars[$option];
  242. }
  243. /**
  244. * Returns JParameter object representing extension's parameters
  245. *
  246. * @param string Extension name
  247. * @return JParameter Extension's parameters
  248. */
  249. function &getExtParamsForm($option)
  250. {
  251. static $forms;
  252. if (!isset($forms)) {
  253. $forms = array();
  254. }
  255. if (!isset($forms[$option])) {
  256. $forms[$option] = new JForm($option, array('control' => 'params'));
  257. // Set the extension's parameters renderer
  258. $pxml = SEFTools::getExtParamsXML($option);
  259. if (is_a($pxml, 'JXMLElement')) {
  260. $forms[$option]->load($pxml);
  261. }
  262. else if( is_array($pxml) && count($pxml) > 0 ) {
  263. for( $i = 0, $n = count($pxml); $i < $n; $i++ ) {
  264. if( is_a($pxml[$i], 'JXMLElement') ) {
  265. $forms[$option]->load($pxml[$i]);
  266. }
  267. }
  268. }
  269. // Set the default parameters renderer
  270. $xml = SEFTools::getExtsDefaultParamsXML();
  271. if (is_a($xml, 'JXMLElement')) {
  272. $forms[$option]->load($xml);
  273. }
  274. else if( is_array($xml) && count($xml) > 0 ) {
  275. for( $i = 0, $n = count($xml); $i < $n; $i++ ) {
  276. if( is_a($xml[$i], 'JXMLElement') ) {
  277. $forms[$option]->load($xml[$i]);
  278. }
  279. }
  280. }
  281. // Bind data
  282. $forms[$option]->bind(self::getExtParams($option));
  283. }
  284. return $forms[$option];
  285. }
  286. function &getExtParams($option)
  287. {
  288. $db = JFactory::getDBO();
  289. $element = str_replace('com_', 'ext_joomsef4_', $option);
  290. // Cache all data and parameters!!!
  291. static $exts, $params;
  292. // Load all params data only once
  293. if (!isset($exts)) {
  294. $query = $db->getQuery(true);
  295. $query->select(array('element', 'params'))->from('#__extensions')->where('type='.$db->quote('sef_ext'));
  296. $db->setQuery($query);
  297. $exts = $db->loadObjectList('element');
  298. }
  299. // Cache params objects
  300. if (!isset($params)) {
  301. $params = array();
  302. }
  303. if (!isset($params[$element])) {
  304. // Create new params object if used for the first time
  305. $data = '';
  306. if (isset($exts[$element])) {
  307. $data = $exts[$element]->params;
  308. }
  309. $params[$element] = new JRegistry($data);
  310. }
  311. return $params[$element];
  312. }
  313. /**
  314. * Returns the JSimpleXMLElement object representing
  315. * the default parameters for every extension
  316. *
  317. * @return JSimpleXMLElement Extensions' default parameters
  318. */
  319. function getExtsDefaultParamsXML()
  320. {
  321. static $xml;
  322. if (isset($xml)) {
  323. return $xml;
  324. }
  325. $xml = null;
  326. $xmlpath = JPATH_ROOT . DS . 'administrator' . DS . 'components' . DS . 'com_sef' . DS . 'extensions_params.xml';
  327. if (JFile::exists($xmlpath)) {
  328. $xml = JFactory::getXML($xmlpath);
  329. }
  330. return $xml;
  331. }
  332. /**
  333. * Returns the JSimpleXMLElement object representing
  334. * the extension's parameters
  335. *
  336. * @param string $option Extension name
  337. * @return JSimpleXMLElement Extension's parameters
  338. */
  339. function getExtParamsXML($option)
  340. {
  341. static $xmls;
  342. if (! isset($xmls)) {
  343. $xmls = array();
  344. }
  345. if (! isset($xmls[$option])) {
  346. $xmls[$option] = null;
  347. $xml = SEFTools::getExtXML($option);
  348. if ($xml) {
  349. $form = $xml->form;
  350. if (!empty($form)) {
  351. $xmls[$option] = $form;
  352. }
  353. }
  354. }
  355. return $xmls[$option];
  356. }
  357. /** Returns the array of texts used by the extension for creating URLs
  358. * in currently selected language (for JoomFish support)
  359. *
  360. * @param string Extension name
  361. * @return array Extension's texts
  362. */
  363. function getExtTexts($option, $lang = '')
  364. {
  365. $database = & JFactory::getDBO();
  366. static $extTexts;
  367. if ($option == '') {
  368. return false;
  369. }
  370. // Set the language
  371. if ($lang == '') {
  372. $lang = JFactory::getLanguage()->getTag();
  373. }
  374. if (! isset($extTexts)) {
  375. $extTexts = array();
  376. }
  377. if (! isset($extTexts[$option])) {
  378. $extTexts[$option] = array();
  379. }
  380. if (! isset($extTexts[$option][$lang])) {
  381. $extTexts[$option][$lang] = array();
  382. // If lang is different than current language, change it
  383. if ($lang !=JFactory::getLanguage()->getTag()) {
  384. $language = & JFactory::getLanguage();
  385. $oldLang = $language->setLanguage($lang);
  386. $language->load();
  387. }
  388. $query="SELECT lang_id AS id \n";
  389. $query.="FROM #__languages \n";
  390. $query.="WHERE lang_code=".$database->quote($lang);
  391. $database->setQuery($query);
  392. $lang_id=$database->loadResult();
  393. //$query = "SELECT `id`, `name`, `value` FROM `#__sefexttexts` WHERE `extension` = '$option'";
  394. $query="SELECT lang_id, `name`, `value` \n";
  395. $query.="FROM #__sefexttexts \n";
  396. $query.="WHERE extension=".$database->quote($option);
  397. $query.="AND (lang_id=0 OR lang_id=".$lang_id.") \n";
  398. $query.="ORDER BY lang_id DESC \n";
  399. $database->setQuery($query);
  400. $texts = $database->loadObjectList();
  401. /*$ntexts=array();
  402. for($i=0;$i<count($texts);$i++) {
  403. $ntexts[$texts[$i]->lang_id][$texts[$i]->name]=$texts[$i];
  404. }*/
  405. if (is_array($texts) && (count($texts) > 0)) {
  406. foreach (array_keys($texts) as $i) {
  407. $name = $texts[$i]->name;
  408. //$value = $texts[$i]->value;
  409. if(!isset($extTexts[$option][$lang][$name])) {
  410. $extTexts[$option][$lang][$name] = $texts[$i]->value;
  411. }
  412. //$extTexts[$option][$lang][$name] = $value;
  413. }
  414. }
  415. // Set the language back to previously selected one
  416. if (isset($oldLang) && ($oldLang != SEFTools::getLangLongCode())) {
  417. $language = & JFactory::getLanguage();
  418. $language->setLanguage($oldLang);
  419. $language->load();
  420. }
  421. }
  422. return $extTexts[$option][$lang];
  423. }
  424. function removeVariable($url, $var, $value = '')
  425. {
  426. if ($value == '') {
  427. //$newurl = eregi_replace("(&|\?)$var=[^&]*", '\\1', $url);
  428. $regex = "(&|\?)$var=[^&]*";
  429. $regex = addcslashes($regex, '/');
  430. $newurl = preg_replace('/' . $regex . '/i', '$1', $url);
  431. } else {
  432. $trans = array('?' => '\\?' , '.' => '\\.' , '+' => '\\+' , '*' => '\\*' , '^' => '\\^' , '$' => '\\$');
  433. $value = strtr($value, $trans);
  434. //$newurl = eregi_replace("(&|\?)$var=$value(&|\$)", '\\1\\2', $url);
  435. $regex = "(&|\?)$var=$value(&|\$)";
  436. $regex = addcslashes($regex, '/');
  437. $newurl = preg_replace('/' . $regex . '/i', '$1$2', $url);
  438. }
  439. $newurl = trim($newurl, '&?');
  440. $trans = array('&&' => '&' , '?&' => '?');
  441. $newurl = strtr($newurl, $trans);
  442. return $newurl;
  443. }
  444. function getVariable($url, $var)
  445. {
  446. $value = null;
  447. $matches = array();
  448. if( preg_match("/[&\?]$var=([^&]*)/", $url, $matches) > 0 ) {
  449. $value = $matches[1];
  450. }
  451. return $value;
  452. }
  453. function extractVariable(&$url, $var)
  454. {
  455. $value = SEFTools::getVariable($url, $var);
  456. $url = SEFTools::removeVariable($url, $var);
  457. return $value;
  458. }
  459. function fixVariable(&$uri, $varName)
  460. {
  461. $value = $uri->getVar($varName);
  462. if (! is_null($value)) {
  463. $pos = strpos($value, ':');
  464. if ($pos !== false) {
  465. $value = substr($value, 0, $pos);
  466. $uri->setVar($varName, $value);
  467. }
  468. }
  469. }
  470. /**
  471. * Removes given variables from URI and returns a query string
  472. * built of them
  473. *
  474. * @param JURI $uri
  475. * @param array $vars Variables to remove
  476. */
  477. function RemoveVariables(&$uri, &$vars)
  478. {
  479. $query = array();
  480. if (is_array($vars) && count($vars) > 0) {
  481. foreach($vars as $var) {
  482. // Get the variable value
  483. $value = $uri->getVar($var);
  484. // Skip variables not present in URL
  485. if( is_null($value) ) {
  486. continue;
  487. }
  488. // Add variable to query
  489. if( is_array($value) ) {
  490. // Variable is an array, let's remove all its occurences
  491. foreach($value as $key => $val) {
  492. $query[] = $var.'['.$key.']='.urlencode($val);
  493. }
  494. }
  495. else {
  496. // Variable is not an array
  497. $query[] = $var.'='.urlencode($value);
  498. }
  499. // Remove variable from URI
  500. $uri->delVar($var);
  501. }
  502. }
  503. $query = implode('&amp;', $query);
  504. return $query;
  505. }
  506. function ReplaceAll($search, $replace, $subject)
  507. {
  508. while (strpos($subject, $search) !== false) {
  509. $subject = str_replace($search, $replace, $subject);
  510. }
  511. return $subject;
  512. }
  513. /**
  514. * Checks whether to use alias from extension parameter value
  515. *
  516. * @param string $params
  517. * @param string $paramName
  518. * @return boolean
  519. */
  520. function UseAlias(&$params, $paramName)
  521. {
  522. $sefConfig =& SEFConfig::getConfig();
  523. $param = $params->get($paramName, 'global');
  524. if( ($param == 'alias') ||
  525. ($param == 'global' && $sefConfig->useAlias) )
  526. {
  527. return true;
  528. }
  529. return false;
  530. }
  531. /**
  532. * Convert description of extensions from html to plain for metatags
  533. *
  534. * @param string $text
  535. * @return string
  536. */
  537. function cleanDesc($text) {
  538. // Remove javascript
  539. $regex = "'<script[^>]*?>.*?</script>'si";
  540. $text = preg_replace($regex, " ", $text);
  541. $regex = "'<noscript[^>]*?>.*?</noscript>'si";
  542. $text = preg_replace($regex, " ", $text);
  543. // Strip any remaining html tags
  544. $text = strip_tags($text);
  545. // Remove any mambot codes
  546. $regex = '(\{.*?\})';
  547. $text = preg_replace($regex, " ", $text);
  548. // Some replacements
  549. $text = str_replace(array('\n', '\r', '"'), array(' ', '', '&quot;'), $text);
  550. $text = trim($text);
  551. return $text;
  552. }
  553. /**
  554. * Clip text to use as meta description
  555. *
  556. * @param string $text
  557. * @param int $limit
  558. * @return string
  559. */
  560. function clipDesc($text, $limit) {
  561. if (JString::strlen($text) > $limit) {
  562. $text = JString::substr($text, 0, $limit);
  563. $pos = JString::strrpos($text, ' ');
  564. if ($pos !== false) {
  565. $text = JString::substr($text, 0, $pos);
  566. }
  567. $text = JString::trim($text);
  568. }
  569. return $text;
  570. }
  571. /**
  572. * Generate for metatags
  573. *
  574. * @param string $desc
  575. * @param string $blacklist
  576. * @param int $count
  577. * @param int $minLength
  578. * @return string
  579. */
  580. function generateKeywords($desc, $blacklist, $count, $minLength) {
  581. // Remove any email addresses
  582. $regex = '/(([_A-Za-z0-9-]+)(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-]+)(\\.[A-Za-z0-9-]+)*)/iex';
  583. $desc = preg_replace($regex, '', $desc);
  584. // Some unwanted replaces
  585. $desc = preg_replace('/<[^>]*>/', ' ', $desc);
  586. $desc = preg_replace('/[\.;:|\'|\"|\`|\,|\(|\)|\-]/', ' ', $desc);
  587. $keysArray = explode(" ", $desc);
  588. // Sort words from up to down
  589. $keysArray = array_count_values(array_map(array('JoomSEF', '_utf8LowerCase'), $keysArray));
  590. if( is_null($blacklist) ) {
  591. $blacklist = "a, able, about, above, abroad, according, accordingly, across, actually, adj, after, afterwards, again, against, ago, ahead, ain't, all, allow, allows, almost, alone, along, alongside, already, also, although, always, am, amid, amidst, among, amongst, an, and, another, any, anybody, anyhow, anyone, anything, anyway, anyways, anywhere, apart, appear, appreciate, appropriate, are, aren't, around, as, a's, aside, ask, asking, associated, at, available, away, awfully, b, back, backward, backwards, be, became, because, become, becomes, becoming, been, before, beforehand, begin, behind, being, believe, below, beside, besides, best, better, between, beyond, both, brief, but, by, c, came, can, cannot, cant, can't, caption, cause, causes, certain, certainly, changes, clearly, c'mon, co, co., com, come, comes, concerning, consequently, consider, considering, contain, containing, contains, corresponding, could, couldn't, course, c's, currently, d, dare, daren't, definitely, described, despite, did, didn't, different, directly, do, does, doesn't, doing, done, don't, down, downwards, during, e, each, edu, eg, eight, eighty, either, else, elsewhere, end, ending, enough, entirely, especially, et, etc, even, ever, evermore, every, everybody, everyone, everything, everywhere, ex, exactly, example, except, f, fairly, far, farther, few, fewer, fifth, first, five, followed, following, follows, for, forever, former, formerly, forth, forward, found, four, from, further, furthermore, g, get, gets, getting, given, gives, go, goes, going, gone, got, gotten, greetings, h, had, hadn't, half, happens, hardly, has, hasn't, have, haven't, having, he, he'd, he'll, hello, help, , hence, her, here, hereafter, hereby, herein, here's, hereupon, hers, herself, he's, hi, him, himself, his, hither, hopefully, how, howbeit, however, hundred, i, i'd, ie, if, ignored, i'll, i'm, immediate, in, inasmuch, inc, inc., indeed, indicate, indicated, indicates, inner, inside, insofar, instead, into, inward, is, isn't, it, it'd, it'll, its, it's, itself, i've, j, just, k, keep, keeps, kept, know, known, knows, l, last, lately, later, latter, latterly, least, less, lest, let, let's, like, liked, likely, likewise, little, look, looking, looks, low, lower, ltd, m, made, mainly, make, makes, many, may, maybe, mayn't, me, mean, meantime, meanwhile, merely, might, mightn't, mine, minus, miss, more, moreover, most, mostly, mr, mrs, much, must, mustn't, my, myself, n, name, namely, nd, near, nearly, necessary, need, needn't, needs, neither, never, neverf, neverless, nevertheless, new, next, nine, ninety, no, nobody, non, none, nonetheless, noone, no-one, nor, normally, not, nothing, notwithstanding, novel, now, nowhere, o, obviously, of, off, often, oh, ok, okay, old, on, once, one, ones, one's, only, onto, opposite, or, other, others, otherwise, ought, oughtn't, our, ours, ourselves, out, outside, over, overall, own, p, particular, particularly, past, per, perhaps, placed, please, plus, possible, presumably, probably, provided, provides, q, que, quite, qv, r, rather, rd, re, really, reasonably, recent, recently, regarding, regardless, regards, relatively, respectively, right, round, s, said, same, saw, say, saying, says, second, secondly, , see, seeing, seem, seemed, seeming, seems, seen, self, selves, sensible, sent, serious, seriously, seven, several, shall, shan't, she, she'd, she'll, she's, should, shouldn't, since, six, so, some, somebody, someday, somehow, someone, something, sometime, sometimes, somewhat, somewhere, soon, sorry, specified, specify, specifying, still, sub, such, sup, sure, t, take, taken, taking, tell, tends, th, than, thank, thanks, thanx, that, that'll, thats, that's, that've, the, their, theirs, them, themselves, then, thence, there, thereafter, thereby, there'd, therefore, therein, there'll, there're, theres, there's, thereupon, there've, these, they, they'd, they'll, they're, they've, thing, things, think, third, thirty, this, thorough, thoroughly, those, though, three, through, throughout, thru, thus, till, to, together, too, took, toward, towards, tried, tries, truly, try, trying, t's, twice, two, u, un, under, underneath, undoing, unfortunately, unless, unlike, unlikely, until, unto, up, upon, upwards, us, use, used, useful, uses, using, usually, v, value, various, versus, very, via, viz, vs, w, want, wants, was, wasn't, way, we, we'd, welcome, well, we'll, went, were, we're, weren't, we've, what, whatever, what'll, what's, what've, when, whence, whenever, where, whereafter, whereas, whereby, wherein, where's, whereupon, wherever, whether, which, whichever, while, whilst, whither, who, who'd, whoever, whole, who'll, whom, whomever, who's, whose, why, will, willing, wish, with, within, without, wonder, won't, would, wouldn't, x, y, yes, yet, you, you'd, you'll, your, you're, yours, yourself, yourselves, you've, z, zero";
  592. }
  593. $blackArray = explode(",", $blacklist);
  594. foreach($blackArray as $blackWord){
  595. if(isset($keysArray[trim($blackWord)]))
  596. unset($keysArray[trim($blackWord)]);
  597. }
  598. arsort($keysArray);
  599. $i = 1;
  600. $keywords = '';
  601. foreach($keysArray as $word=>$instances){
  602. if($i > $count)
  603. break;
  604. if(JString::strlen(trim($word)) >= $minLength ) {
  605. $keywords .= $word . ", ";
  606. $i++;
  607. }
  608. }
  609. $keywords = rtrim($keywords, ", ");
  610. return $keywords;
  611. }
  612. function GetSEFGlobalMeta() {
  613. return '3cb3b22c24e22b3dcc13f79970b94b03'; // sef.global.meta
  614. }
  615. /**
  616. * Sends the POST request
  617. *
  618. * @param string $url
  619. * @param string $referer
  620. * @param array $_data
  621. * @return object
  622. */
  623. function PostRequest($url, $referer = null, $_data = null, $method = 'post', $userAgent = null, $headers = null) {
  624. // convert variables array to string:
  625. $data = '';
  626. if( is_array($_data) && count($_data) > 0 ) {
  627. // format --> test1=a&test2=b etc.
  628. $data = array();
  629. while( list($n, $v) = each($_data) ) {
  630. $data[] = "$n=$v";
  631. }
  632. $data = implode('&', $data);
  633. $contentType = "Content-type: application/x-www-form-urlencoded\r\n";
  634. }
  635. else {
  636. $data = $_data;
  637. $contentType = "Content-type: text/xml\r\n";
  638. }
  639. if( is_null($referer) ) {
  640. $referer = JURI::root();
  641. }
  642. // parse the given URL
  643. $url = parse_url($url);
  644. if( !isset($url['scheme']) ) {
  645. return false;
  646. }
  647. // extract host and path:
  648. $host = $url['host'];
  649. $path = isset($url['path']) ? $url['path'] : '/';
  650. // Prepare host and port to connect to
  651. $connhost = $host;
  652. $port = 80;
  653. // Workaround for some PHP versions, where fsockopen can't connect to
  654. // 'localhost' string on Windows servers
  655. if ($connhost == 'localhost') {
  656. $connhost = gethostbyname('localhost');
  657. }
  658. // Handle scheme
  659. if ($url['scheme'] == 'https') {
  660. $connhost = 'ssl://'.$connhost;
  661. $port = 443;
  662. }
  663. else if ($url['scheme'] != 'http') {
  664. return false;
  665. }
  666. // open a socket connection
  667. $errno = null;
  668. $errstr = null;
  669. $fp = @fsockopen($connhost, $port, $errno, $errstr, 5);
  670. if( $fp === false ) {
  671. return false;
  672. }
  673. if (!is_null($userAgent)) {
  674. $userAgent = "User-Agent: ".$userAgent."\r\n";
  675. }
  676. // send the request
  677. if ($method == 'post') {
  678. fputs($fp, "POST $path HTTP/1.1\r\n");
  679. if (!is_null($userAgent)) {
  680. fputs($fp, $userAgent);
  681. }
  682. fputs($fp, "Host: $host\r\n");
  683. fputs($fp, "Referer: $referer\r\n");
  684. fputs($fp, $contentType);
  685. fputs($fp, "Content-length: ". strlen($data) ."\r\n");
  686. // Send additional headers if set
  687. if (is_array($headers)) {
  688. foreach ($headers as $h) {
  689. $h = rtrim($h);
  690. $h .= "\r\n";
  691. fputs($fp, $h);
  692. }
  693. }
  694. fputs($fp, "Connection: close\r\n\r\n");
  695. fputs($fp, $data);
  696. }
  697. elseif ($method == 'get') {
  698. $query = '';
  699. if (isset($url['query'])) {
  700. $query = '?'.$url['query'];
  701. }
  702. fputs($fp, "GET {$path}{$query} HTTP/1.1\r\n");
  703. if (!is_null($userAgent)) {
  704. fputs($fp, $userAgent);
  705. }
  706. fputs($fp, "Host: $host\r\n");
  707. // Send additional headers if set
  708. if (is_array($headers)) {
  709. foreach ($headers as $h) {
  710. $h = rtrim($h);
  711. $h .= "\r\n";
  712. fputs($fp, $h);
  713. }
  714. }
  715. fputs($fp, "Connection: close\r\n\r\n");
  716. }
  717. $result = '';
  718. while(!feof($fp)) {
  719. // receive the results of the request
  720. $result .= fgets($fp, 128);
  721. }
  722. // close the socket connection:
  723. fclose($fp);
  724. // split the result header from the content
  725. $result = explode("\r\n\r\n", $result, 2);
  726. $header = isset($result[0]) ? $result[0] : '';
  727. $content = isset($result[1]) ? $result[1] : '';
  728. $response = new stdClass();
  729. $response->header = $header;
  730. $response->content = $content;
  731. // Handle chunked transfer if needed
  732. if( strpos(strtolower($response->header), 'transfer-encoding: chunked') !== false ) {
  733. $parsed = '';
  734. $left = $response->content;
  735. while( true ) {
  736. $pos = strpos($left, "\r\n");
  737. if( $pos === false ) {
  738. return $response;
  739. }
  740. $chunksize = substr($left, 0, $pos);
  741. $pos += strlen("\r\n");
  742. $left = substr($left, $pos);
  743. $pos = strpos($chunksize, ';');
  744. if( $pos !== false ) {
  745. $chunksize = substr($chunksize, 0, $pos);
  746. }
  747. $chunksize = hexdec($chunksize);
  748. if( $chunksize == 0 ) {
  749. break;
  750. }
  751. $parsed .= substr($left, 0, $chunksize);
  752. $left = substr($left, $chunksize + strlen("\r\n"));
  753. }
  754. $response->content = $parsed;
  755. }
  756. // Get the response code from header
  757. $headerLines = explode("\n", $response->header);
  758. $header1 = explode(' ', trim($headerLines[0]));
  759. $code = intval($header1[1]);
  760. $response->code = $code;
  761. return $response;
  762. }
  763. function getSEOStatus()
  764. {
  765. static $status;
  766. if( !isset($status) ) {
  767. $sefConfig =& SEFConfig::getConfig();
  768. $status = array();
  769. $config =& JFactory::getConfig();
  770. $status['sef'] = (bool)$config->getValue('config.sef');
  771. $status['mod_rewrite'] = (bool)$config->getValue('config.sef_rewrite');
  772. $status['sef_suffix'] = (bool)$config->getValue('config.sef_suffix');
  773. $status['joomsef'] = (bool)$sefConfig->enabled;
  774. $status['plugin'] = JPluginHelper::isEnabled('system', 'joomsef');
  775. $status['newurls'] = !$sefConfig->disableNewSEF;
  776. }
  777. return $status;
  778. }
  779. function getNonSefVars(&$uri, $nonSefVars, $ignoreVars)
  780. {
  781. $mainframe =& JFactory::getApplication();
  782. $sefConfig =& SEFConfig::getConfig();
  783. // Get the parameters for this component
  784. if( !is_null($uri->getVar('option')) ) {
  785. $params =& SEFTools::getExtParams($uri->getVar('option'));
  786. }
  787. // Build array of nonSef vars if set to
  788. $nonSef = array();
  789. if( $sefConfig->appendNonSef ) {
  790. // Save the given nonsef vars
  791. $nonSef = $nonSefVars;
  792. // load the nonSEF vars from option parameters
  793. $paramNonSef = array();
  794. if( isset($params) ) {
  795. $nsef = $params->get('customNonSef', '');
  796. if( !empty($nsef) ) {
  797. // Some variables are set, let's explode them
  798. $paramNonSef = explode(';', $nsef);
  799. }
  800. }
  801. // get globally configured nonSEF vars
  802. $configNonSef = array();
  803. if( !empty($sefConfig->customNonSef) ) {
  804. $configNonSef = explode(';', $sefConfig->customNonSef);
  805. }
  806. // Get nonSEF vars from variable filter test if set to
  807. $failedVars = array();
  808. // combine all the nonSEF vars arrays
  809. $nsefvars = array_merge($paramNonSef, $configNonSef, $failedVars);
  810. if (!empty($nsefvars)) {
  811. foreach($nsefvars as $nsefvar) {
  812. // add each variable, that isn't already set, and that is present in our URL
  813. if( !isset($nonSef[$nsefvar]) && !is_null($uri->getVar($nsefvar)) ) {
  814. $nonSef[$nsefvar] = $uri->getVar($nsefvar);
  815. }
  816. }
  817. }
  818. // if $nonSefVars mixes with $GLOBALS['JOOMSEF_NONSEFVARS'], exclude the mixed vars
  819. // this is important to prevent duplicating params by adding JOOMSEF_NONSEFVARS to
  820. // $ignoreSefVars
  821. $gNonSef = $mainframe->get('sef.global.nonsefvars');
  822. if (!empty($gNonSef)) {
  823. foreach (array_keys($gNonSef) as $key) {
  824. if (isset($nonSef[$key])) unset($gNonSef[$key]);
  825. }
  826. $mainframe->set('sef.global.nonsefvars', $gNonSef);
  827. }
  828. }
  829. // Combine nonSef and ignore vars
  830. if (!empty($ignoreVars)) {
  831. $nonSef = array_merge($nonSef, $ignoreVars);
  832. }
  833. // If the component requests strict accept variables filtering, add the ones that don't match
  834. if( isset($params) && ($params->get('acceptStrict', '0') == '1') ) {
  835. $acceptVars =& SEFTools::getExtAcceptVars($uri->getVar('option'));
  836. $uriVars = $uri->getQuery(true);
  837. if( (count($acceptVars) > 0) && (count($uriVars) > 0) ) {
  838. foreach($uriVars as $name => $value) {
  839. // Standard Joomla variables
  840. if( in_array($name, array('option', 'Itemid', 'limit', 'limitstart', 'format', 'tmpl', 'lang')) ) {
  841. continue;
  842. }
  843. // Accepted variables
  844. if( in_array($name, $acceptVars) ) {
  845. continue;
  846. }
  847. // Variable not accepted, add it to non-SEF
  848. $nonSef[$name] = $value;
  849. }
  850. }
  851. }
  852. return $nonSef;
  853. }
  854. function getHomeQueries($includeLang = true)
  855. {
  856. // Cache result to save DB queries!!!
  857. static $items;
  858. if (!isset($items)) {
  859. $db = JFactory::getDBO();
  860. $query = $db->getQuery(true);
  861. $query->select('id, link, language')->from('#__menu')->where('home=1');
  862. $db->setQuery($query);
  863. $items = $db->loadObjectList('id');
  864. }
  865. return $items;
  866. }
  867. function getMenuItemSubDomains($id)
  868. {
  869. // Cache results to save DB queries!!!
  870. static $items;
  871. if (!isset($items)) {
  872. $items = array();
  873. }
  874. if (!isset($items[$id])) {
  875. $db = JFactory::getDBO();
  876. $query = $db->getQuery(true);
  877. $query->select('link')->from('#__menu')->where('id='.$id);
  878. $db->setQuery($query);
  879. $items[$id] = $db->loadResult();
  880. }
  881. return $items[$id];
  882. }
  883. function getAllSubdomains()
  884. {
  885. // Cache subdomains data to save DB queries!
  886. static $subdomains;
  887. if (!isset($subdomains)) {
  888. $subdomains = array();
  889. // Load all subdomains
  890. $db = JFactory::getDBO();
  891. $query = $db->getQuery(true);
  892. $query->select('*')->from('#__sef_subdomains');
  893. $db->setQuery($query);
  894. $rows = $db->loadObjectList();
  895. // Create structure of subdomains according to languages
  896. foreach ($rows as $row) {
  897. // Add subdomain to correct language
  898. if (!empty($row->lang)) {
  899. $subdomains[$row->lang][] = $row;
  900. }
  901. if ($row->lang != '*') {
  902. $subdomains['*'][] = $row;
  903. }
  904. }
  905. }
  906. return $subdomains;
  907. }
  908. function getSubDomain($Itemid,$uri,&$titlepage)
  909. {
  910. $sefConfig=SEFConfig::getConfig();
  911. $titlepage=false;
  912. $option=$uri->getVar('option');
  913. $db=JFactory::getDBO();
  914. $lang = '*';
  915. if ($sefConfig->langEnable) {
  916. $lang = $uri->getVar('lang', '*');
  917. }
  918. // Get subdomains
  919. $subdomains = self::getAllSubdomains();
  920. if (!array_key_exists($lang, $subdomains)) {
  921. // No subdomain for given language
  922. return null;
  923. }
  924. for($i=0;$i<count($subdomains[$lang]);$i++) {
  925. $Itemids=explode(",",$subdomains[$lang][$i]->Itemid);
  926. if($Itemid==$subdomains[$lang][$i]->Itemid_titlepage) {
  927. $link=new JURI(self::getMenuItemSubDomains($Itemid));
  928. $uri_query=$uri->getQuery(true);
  929. $titlepage=true;
  930. foreach($link->getQuery(true) as $opt=>$val) {
  931. if($val!=@$uri_query[$opt]) {
  932. $titlepage=false;
  933. }
  934. }
  935. return $subdomains[$lang][$i]->subdomain;
  936. }
  937. if($option==$subdomains[$lang][$i]->option) {
  938. return $subdomains[$lang][$i]->subdomain;
  939. }
  940. if(strlen($subdomains[$lang][$i]->Itemid) && in_array($Itemid,$Itemids)) {
  941. return $subdomains[$lang][$i]->subdomain;
  942. }
  943. }
  944. return null;
  945. }
  946. function normalizeURI(&$uri)
  947. {
  948. $option = $uri->getVar('option');
  949. if (!is_null($option)) {
  950. $extfile = JPATH_ROOT.DS.'components'.DS.'com_sef'.DS.'sef_ext'.DS.$option.'.php';
  951. if (file_exists($extfile)) {
  952. require_once($extfile);
  953. $class = 'SefExt_'.$option;
  954. $ext = new $class();
  955. $ext->beforeCreate($uri);
  956. list($nonsef, $ignore) = $ext->getNonSefVars($uri);
  957. if (!empty($ignore)) {
  958. $nonsef = array_merge($nonsef, $ignore);
  959. }
  960. $keys = array_keys($nonsef);
  961. SEFTools::RemoveVariables($uri, $keys);
  962. }
  963. }
  964. }
  965. /**
  966. * Checks, whether the given Itemid is ignored for the given component
  967. *
  968. * @param string $option
  969. * @param string $Itemid
  970. * @return bool
  971. */
  972. function isItemidIgnored($option, $Itemid)
  973. {
  974. $params =& SEFTools::getExtParams($option);
  975. $ignoredIds = trim($params->get('ignoreItemids', ''));
  976. if (empty($ignoredIds)) {
  977. return false;
  978. }
  979. $ids = array_map('trim', explode(',', $ignoredIds));
  980. if (in_array($Itemid, $ids)) {
  981. return true;
  982. }
  983. return false;
  984. }
  985. public static function getInstalledComponents()
  986. {
  987. static $components;
  988. if (!isset($components)) {
  989. $db =& JFactory::getDbo();
  990. // Get components
  991. $db->setQuery("SELECT `name`, `element` AS `option` FROM `#__extensions` WHERE `type` = 'component'");
  992. $components = $db->loadObjectList('option');
  993. if (is_null($components)) {
  994. $components = array();
  995. return $components;
  996. }
  997. // Remove system components
  998. $remove = array('com_sef', 'com_admin', 'com_cache', 'com_categories', 'com_checkin', 'com_config', 'com_cpanel', 'com_installer', 'com_joomfish', 'com_languages', 'com_login', 'com_media', 'com_menus', 'com_messages', 'com_modules', 'com_plugins', 'com_redirect', 'com_templates');
  999. foreach($remove as $r) {
  1000. if (isset($components[$r])) {
  1001. unset($components[$r]);
  1002. }
  1003. }
  1004. // Translate names
  1005. $lang = JFactory::getLanguage();
  1006. foreach($components as &$item) {
  1007. $extension = $item->option;
  1008. $source = JPATH_ADMINISTRATOR . '/components/' . $extension;
  1009. $lang->load("$extension.sys", JPATH_ADMINISTRATOR, null, false, false)
  1010. || $lang->load("$extension.sys", $source, null, false, false)
  1011. || $lang->load("$extension.sys", JPATH_ADMINISTRATOR, $lang->getDefault(), false, false)
  1012. || $lang->load("$extension.sys", $source, $lang->getDefault(), false, false);
  1013. $item->name = JText::_($item->name);
  1014. }
  1015. // Sort by name
  1016. uasort($components, array('SEFTools', 'cmpComponents'));
  1017. }
  1018. return $components;
  1019. }
  1020. protected static function cmpComponents(&$a, &$b)
  1021. {
  1022. return strnatcasecmp($a->name, $b->name);
  1023. }
  1024. function getDefaultParams($element)
  1025. {
  1026. //$element = $this->manifest->getElementByPath('install/defaultparams');
  1027. //$element=$this->manifest->install->defaultparams;
  1028. if( !is_a($element, 'JXMLElement') || !count($element->children()) ) {
  1029. return '';
  1030. }
  1031. $defaultParams = $element->children();
  1032. if( count($defaultParams) == 0 ) {
  1033. return '';
  1034. }
  1035. $params = array();
  1036. foreach($defaultParams as $param) {
  1037. if( $param->name() != 'defaultParam' ) {
  1038. continue;
  1039. }
  1040. $name = $param->attributes()->name;
  1041. $value = $param->attributes()->value;
  1042. $params[] = $name . '=' . $value;
  1043. }
  1044. if( count($params) > 0 ) {
  1045. return implode("\n", $params);
  1046. }
  1047. else {
  1048. return '';
  1049. }
  1050. }
  1051. function getDefaultFilters($element)
  1052. {
  1053. //$element = $this->manifest->getElementByPath('install/defaultfilters');
  1054. //$element=$this->manigest->install->defaultfilters;
  1055. if( !is_a($element, 'JXMLElement') || !count($element->children()) ) {
  1056. return '';
  1057. }
  1058. $defaultFilters = $element->children();
  1059. if( count($defaultFilters) == 0 ) {
  1060. return '';
  1061. }
  1062. $filters = array();
  1063. foreach($defaultFilters as $filter) {
  1064. if( $filter->name() != 'defaultFilter' ) {
  1065. continue;
  1066. }
  1067. $filters[] = $filter->data();
  1068. }
  1069. if( count($filters) > 0 ) {
  1070. return implode("\n", $filters);
  1071. }
  1072. else {
  1073. return '';
  1074. }
  1075. }
  1076. }
  1077. ?>