PageRenderTime 32ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 0ms

/cms/modules/search/include/commonfuncs.php

https://github.com/swat/pragyan
PHP | 683 lines | 480 code | 95 blank | 108 comment | 57 complexity | e08bc54e975055549543990980f8193f MD5 | raw file
  1. <?php
  2. /*******************************************
  3. * Sphider Version 1.3.*
  4. * This program is licensed under the GNU GPL.
  5. * By Ando Saabas ando(a t)cs.ioc.ee
  6. ********************************************/
  7. /**
  8. * Returns the result of a query as an array
  9. *
  10. * @param string $query SQL päring stringina
  11. * @return array|null massiiv
  12. */
  13. function sql_fetch_all($query) {
  14. $result = mysql_query($query);
  15. if($mysql_err = mysql_errno()) {
  16. print $query.'<br>'.mysql_error();
  17. } else {
  18. while($row=mysql_fetch_array($result)) {
  19. $data[]=$row;
  20. }
  21. }
  22. return $data;
  23. }
  24. /*
  25. Removes duplicate elements from an array
  26. */
  27. function distinct_array($arr) {
  28. rsort($arr);
  29. reset($arr);
  30. $newarr = array();
  31. $i = 0;
  32. $element = current($arr);
  33. for ($n = 0; $n < sizeof($arr); $n++) {
  34. if (next($arr) != $element) {
  35. $newarr[$i] = $element;
  36. $element = current($arr);
  37. $i++;
  38. }
  39. }
  40. return $newarr;
  41. }
  42. function get_cats($parent) {
  43. global $mysql_table_prefix;
  44. $query = "SELECT * FROM ".$mysql_table_prefix."categories WHERE parent_num=$parent";
  45. echo mysql_error();
  46. $result = mysql_query($query);
  47. $arr[] = $parent;
  48. if (mysql_num_rows($result) <> '') {
  49. while ($row = mysql_fetch_array($result)) {
  50. $id = $row[category_id];
  51. $arr = add_arrays($arr, get_cats($id));
  52. }
  53. }
  54. return $arr;
  55. }
  56. function add_arrays($arr1, $arr2) {
  57. foreach ($arr2 as $elem) {
  58. $arr1[] = $elem;
  59. }
  60. return $arr1;
  61. }
  62. global $entities;
  63. $entities = array
  64. (
  65. "&amp" => "&",
  66. "&apos" => "'",
  67. "&THORN;" => "Þ",
  68. "&szlig;" => "ß",
  69. "&agrave;" => "à",
  70. "&aacute;" => "á",
  71. "&acirc;" => "â",
  72. "&atilde;" => "ã",
  73. "&auml;" => "ä",
  74. "&aring;" => "å",
  75. "&aelig;" => "æ",
  76. "&ccedil;" => "ç",
  77. "&egrave;" => "è",
  78. "&eacute;" => "é",
  79. "&ecirc;" => "ê",
  80. "&euml;" => "ë",
  81. "&igrave;" => "ì",
  82. "&iacute;" => "í",
  83. "&icirc;" => "î",
  84. "&iuml;" => "ï",
  85. "&eth;" => "ð",
  86. "&ntilde;" => "ñ",
  87. "&ograve;" => "ò",
  88. "&oacute;" => "ó",
  89. "&ocirc;" => "ô",
  90. "&otilde;" => "õ",
  91. "&ouml;" => "ö",
  92. "&oslash;" => "ø",
  93. "&ugrave;" => "ù",
  94. "&uacute;" => "ú",
  95. "&ucirc;" => "û",
  96. "&uuml;" => "ü",
  97. "&yacute;" => "ý",
  98. "&thorn;" => "þ",
  99. "&yuml;" => "ÿ",
  100. "&THORN;" => "Þ",
  101. "&szlig;" => "ß",
  102. "&Agrave;" => "à",
  103. "&Aacute;" => "á",
  104. "&Acirc;" => "â",
  105. "&Atilde;" => "ã",
  106. "&Auml;" => "ä",
  107. "&Aring;" => "å",
  108. "&Aelig;" => "æ",
  109. "&Ccedil;" => "ç",
  110. "&Egrave;" => "è",
  111. "&Eacute;" => "é",
  112. "&Ecirc;" => "ê",
  113. "&Euml;" => "ë",
  114. "&Igrave;" => "ì",
  115. "&Iacute;" => "í",
  116. "&Icirc;" => "î",
  117. "&Iuml;" => "ï",
  118. "&ETH;" => "ð",
  119. "&Ntilde;" => "ñ",
  120. "&Ograve;" => "ò",
  121. "&Oacute;" => "ó",
  122. "&Ocirc;" => "ô",
  123. "&Otilde;" => "õ",
  124. "&Ouml;" => "ö",
  125. "&Oslash;" => "ø",
  126. "&Ugrave;" => "ù",
  127. "&Uacute;" => "ú",
  128. "&Ucirc;" => "û",
  129. "&Uuml;" => "ü",
  130. "&Yacute;" => "ý",
  131. "&Yhorn;" => "þ",
  132. "&Yuml;" => "ÿ"
  133. );
  134. global $apache_indexes;
  135. //Apache multi indexes parameters
  136. $apache_indexes = array (
  137. "N=A" => 1,
  138. "N=D" => 1,
  139. "M=A" => 1,
  140. "M=D" => 1,
  141. "S=A" => 1,
  142. "S=D" => 1,
  143. "D=A" => 1,
  144. "D=D" => 1,
  145. "C=N;O=A" => 1,
  146. "C=M;O=A" => 1,
  147. "C=S;O=A" => 1,
  148. "C=D;O=A" => 1,
  149. "C=N;O=D" => 1,
  150. "C=M;O=D" => 1,
  151. "C=S;O=D" => 1,
  152. "C=D;O=D" => 1);
  153. function remove_accents($string) {
  154. return (strtr($string, "ÀÁÂÃÄÅÆàáâãäåæÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñÞßÿý",
  155. "aaaaaaaaaaaaaaoooooooooooooeeeeeeeeecceiiiiiiiiuuuuuuuunntsyy"));
  156. }
  157. global $common;
  158. $common = array
  159. (
  160. );
  161. global $lines;
  162. $lines = @file($include_dir.'/common.txt');
  163. if (is_array($lines)) {
  164. while (list($id, $word) = each($lines))
  165. $common[trim($word)] = 1;
  166. }
  167. $ext = array
  168. (
  169. );
  170. $lines = @file('ext.txt');
  171. if (is_array($lines)) {
  172. while (list($id, $word) = each($lines))
  173. $ext[] = trim($word);
  174. }
  175. function is_num($var) {
  176. for ($i=0;$i<strlen($var);$i++) {
  177. $ascii_code=ord($var[$i]);
  178. if ($ascii_code >=49 && $ascii_code <=57){
  179. continue;
  180. } else {
  181. return false;
  182. }
  183. }
  184. return true;
  185. }
  186. function getHttpVars() {
  187. $superglobs = array(
  188. '_POST',
  189. '_GET',
  190. 'HTTP_POST_VARS',
  191. 'HTTP_GET_VARS');
  192. $httpvars = array();
  193. // extract the right array
  194. foreach ($superglobs as $glob) {
  195. global $$glob;
  196. if (isset($$glob) && is_array($$glob)) {
  197. $httpvars = $$glob;
  198. }
  199. if (count($httpvars) > 0)
  200. break;
  201. }
  202. return $httpvars;
  203. }
  204. function countSubstrs($haystack, $needle) {
  205. $count = 0;
  206. while(strpos($haystack,$needle) !== false) {
  207. $haystack = substr($haystack, (strpos($haystack,$needle) + 1));
  208. $count++;
  209. }
  210. return $count;
  211. }
  212. function quote_replace($str) {
  213. $str = str_replace("\"",
  214. "&quot;", $str);
  215. return str_replace("'","&apos;", $str);
  216. }
  217. function fst_lt_snd($version1, $version2) {
  218. $list1 = explode(".", $version1);
  219. $list2 = explode(".", $version2);
  220. $length = count($list1);
  221. $i = 0;
  222. while ($i < $length) {
  223. if ($list1[$i] < $list2[$i])
  224. return true;
  225. if ($list1[$i] > $list2[$i])
  226. return false;
  227. $i++;
  228. }
  229. if ($length < count($list2)) {
  230. return true;
  231. }
  232. return false;
  233. }
  234. function get_dir_contents($dir) {
  235. $contents = Array();
  236. if ($handle = opendir($dir)) {
  237. while (false !== ($file = readdir($handle))) {
  238. if ($file != "." && $file != "..") {
  239. $contents[] = $file;
  240. }
  241. }
  242. closedir($handle);
  243. }
  244. return $contents;
  245. }
  246. function replace_ampersand($str) {
  247. return str_replace("&", "%26", $str);
  248. }
  249. /**
  250. * Stemming algorithm
  251. * Copyright (c) 2005 Richard Heyes (http://www.phpguru.org/)
  252. * All rights reserved.
  253. * This script is free software.
  254. * Modified to work with php versions prior 5 by Ando Saabas
  255. */
  256. /**
  257. * Regex for matching a consonant
  258. */
  259. global $regex_consonant;
  260. $regex_consonant = '(?:[bcdfghjklmnpqrstvwxz]|(?<=[aeiou])y|^y)';
  261. /**
  262. * Regex for matching a vowel
  263. */
  264. global $regex_vowel;
  265. $regex_vowel = '(?:[aeiou]|(?<![aeiou])y)';
  266. /**
  267. * Stems a word. Simple huh?
  268. *
  269. * @param string $word Word to stem
  270. * @return string Stemmed word
  271. */
  272. function stem($word)
  273. {
  274. if (strlen($word) <= 2) {
  275. return $word;
  276. }
  277. $word = step1ab($word);
  278. $word = step1c($word);
  279. $word = step2($word);
  280. $word = step3($word);
  281. $word = step4($word);
  282. $word = step5($word);
  283. return $word;
  284. }
  285. /**
  286. * Step 1
  287. */
  288. function step1ab($word)
  289. {
  290. global $regex_vowel, $regex_consonant;
  291. // Part a
  292. if (substr($word, -1) == 's') {
  293. replace($word, 'sses', 'ss')
  294. OR replace($word, 'ies', 'i')
  295. OR replace($word, 'ss', 'ss')
  296. OR replace($word, 's', '');
  297. }
  298. // Part b
  299. if (substr($word, -2, 1) != 'e' OR !replace($word, 'eed', 'ee', 0)) { // First rule
  300. $v = $regex_vowel;
  301. // ing and ed
  302. if ( preg_match("#$v+#", substr($word, 0, -3)) && replace($word, 'ing', '')
  303. OR preg_match("#$v+#", substr($word, 0, -2)) && replace($word, 'ed', '')) { // Note use of && and OR, for precedence reasons
  304. // If one of above two test successful
  305. if ( !replace($word, 'at', 'ate')
  306. AND !replace($word, 'bl', 'ble')
  307. AND !replace($word, 'iz', 'ize')) {
  308. // Double consonant ending
  309. if ( doubleConsonant($word)
  310. AND substr($word, -2) != 'll'
  311. AND substr($word, -2) != 'ss'
  312. AND substr($word, -2) != 'zz') {
  313. $word = substr($word, 0, -1);
  314. } else if (m($word) == 1 AND cvc($word)) {
  315. $word .= 'e';
  316. }
  317. }
  318. }
  319. }
  320. return $word;
  321. }
  322. /**
  323. * Step 1c
  324. *
  325. * @param string $word Word to stem
  326. */
  327. function step1c($word)
  328. {
  329. global $regex_vowel, $regex_consonant;
  330. $v = $regex_vowel;
  331. if (substr($word, -1) == 'y' && preg_match("#$v+#", substr($word, 0, -1))) {
  332. replace($word, 'y', 'i');
  333. }
  334. return $word;
  335. }
  336. /**
  337. * Step 2
  338. *
  339. * @param string $word Word to stem
  340. */
  341. function step2($word)
  342. {
  343. switch (substr($word, -2, 1)) {
  344. case 'a':
  345. replace($word, 'ational', 'ate', 0)
  346. OR replace($word, 'tional', 'tion', 0);
  347. break;
  348. case 'c':
  349. replace($word, 'enci', 'ence', 0)
  350. OR replace($word, 'anci', 'ance', 0);
  351. break;
  352. case 'e':
  353. replace($word, 'izer', 'ize', 0);
  354. break;
  355. case 'g':
  356. replace($word, 'logi', 'log', 0);
  357. break;
  358. case 'l':
  359. replace($word, 'entli', 'ent', 0)
  360. OR replace($word, 'ousli', 'ous', 0)
  361. OR replace($word, 'alli', 'al', 0)
  362. OR replace($word, 'bli', 'ble', 0)
  363. OR replace($word, 'eli', 'e', 0);
  364. break;
  365. case 'o':
  366. replace($word, 'ization', 'ize', 0)
  367. OR replace($word, 'ation', 'ate', 0)
  368. OR replace($word, 'ator', 'ate', 0);
  369. break;
  370. case 's':
  371. replace($word, 'iveness', 'ive', 0)
  372. OR replace($word, 'fulness', 'ful', 0)
  373. OR replace($word, 'ousness', 'ous', 0)
  374. OR replace($word, 'alism', 'al', 0);
  375. break;
  376. case 't':
  377. replace($word, 'biliti', 'ble', 0)
  378. OR replace($word, 'aliti', 'al', 0)
  379. OR replace($word, 'iviti', 'ive', 0);
  380. break;
  381. }
  382. return $word;
  383. }
  384. /**
  385. * Step 3
  386. *
  387. * @param string $word String to stem
  388. */
  389. function step3($word)
  390. {
  391. switch (substr($word, -2, 1)) {
  392. case 'a':
  393. replace($word, 'ical', 'ic', 0);
  394. break;
  395. case 's':
  396. replace($word, 'ness', '', 0);
  397. break;
  398. case 't':
  399. replace($word, 'icate', 'ic', 0)
  400. OR replace($word, 'iciti', 'ic', 0);
  401. break;
  402. case 'u':
  403. replace($word, 'ful', '', 0);
  404. break;
  405. case 'v':
  406. replace($word, 'ative', '', 0);
  407. break;
  408. case 'z':
  409. replace($word, 'alize', 'al', 0);
  410. break;
  411. }
  412. return $word;
  413. }
  414. /**
  415. * Step 4
  416. *
  417. * @param string $word Word to stem
  418. */
  419. function step4($word)
  420. {
  421. switch (substr($word, -2, 1)) {
  422. case 'a':
  423. replace($word, 'al', '', 1);
  424. break;
  425. case 'c':
  426. replace($word, 'ance', '', 1)
  427. OR replace($word, 'ence', '', 1);
  428. break;
  429. case 'e':
  430. replace($word, 'er', '', 1);
  431. break;
  432. case 'i':
  433. replace($word, 'ic', '', 1);
  434. break;
  435. case 'l':
  436. replace($word, 'able', '', 1)
  437. OR replace($word, 'ible', '', 1);
  438. break;
  439. case 'n':
  440. replace($word, 'ant', '', 1)
  441. OR replace($word, 'ement', '', 1)
  442. OR replace($word, 'ment', '', 1)
  443. OR replace($word, 'ent', '', 1);
  444. break;
  445. case 'o':
  446. if (substr($word, -4) == 'tion' OR substr($word, -4) == 'sion') {
  447. replace($word, 'ion', '', 1);
  448. } else {
  449. replace($word, 'ou', '', 1);
  450. }
  451. break;
  452. case 's':
  453. replace($word, 'ism', '', 1);
  454. break;
  455. case 't':
  456. replace($word, 'ate', '', 1)
  457. OR replace($word, 'iti', '', 1);
  458. break;
  459. case 'u':
  460. replace($word, 'ous', '', 1);
  461. break;
  462. case 'v':
  463. replace($word, 'ive', '', 1);
  464. break;
  465. case 'z':
  466. replace($word, 'ize', '', 1);
  467. break;
  468. }
  469. return $word;
  470. }
  471. /**
  472. * Step 5
  473. *
  474. * @param string $word Word to stem
  475. */
  476. function step5($word)
  477. {
  478. // Part a
  479. if (substr($word, -1) == 'e') {
  480. if (m(substr($word, 0, -1)) > 1) {
  481. replace($word, 'e', '');
  482. } else if (m(substr($word, 0, -1)) == 1) {
  483. if (!cvc(substr($word, 0, -1))) {
  484. replace($word, 'e', '');
  485. }
  486. }
  487. }
  488. // Part b
  489. if (m($word) > 1 AND doubleConsonant($word) AND substr($word, -1) == 'l') {
  490. $word = substr($word, 0, -1);
  491. }
  492. return $word;
  493. }
  494. /**
  495. * Replaces the first string with the second, at the end of the string. If third
  496. * arg is given, then the preceding string must match that m count at least.
  497. *
  498. * @param string $str String to check
  499. * @param string $check Ending to check for
  500. * @param string $repl Replacement string
  501. * @param int $m Optional minimum number of m() to meet
  502. * @return bool Whether the $check string was at the end
  503. * of the $str string. True does not necessarily mean
  504. * that it was replaced.
  505. */
  506. function replace(&$str, $check, $repl, $m = null)
  507. {
  508. $len = 0 - strlen($check);
  509. if (substr($str, $len) == $check) {
  510. $substr = substr($str, 0, $len);
  511. if (is_null($m) OR m($substr) > $m) {
  512. $str = $substr . $repl;
  513. }
  514. return true;
  515. }
  516. return false;
  517. }
  518. /**
  519. * What, you mean it's not obvious from the name?
  520. *
  521. * m() measures the number of consonant sequences in $str. if c is
  522. * a consonant sequence and v a vowel sequence, and <..> indicates arbitrary
  523. * presence,
  524. *
  525. * <c><v> gives 0
  526. * <c>vc<v> gives 1
  527. * <c>vcvc<v> gives 2
  528. * <c>vcvcvc<v> gives 3
  529. *
  530. * @param string $str The string to return the m count for
  531. * @return int The m count
  532. */
  533. function m($str)
  534. {
  535. global $regex_vowel, $regex_consonant;
  536. $c = $regex_consonant;
  537. $v = $regex_vowel;
  538. $str = preg_replace("#^$c+#", '', $str);
  539. $str = preg_replace("#$v+$#", '', $str);
  540. preg_match_all("#($v+$c+)#", $str, $matches);
  541. return count($matches[1]);
  542. }
  543. /**
  544. * Returns true/false as to whether the given string contains two
  545. * of the same consonant next to each other at the end of the string.
  546. *
  547. * @param string $str String to check
  548. * @return bool Result
  549. */
  550. function doubleConsonant($str)
  551. {
  552. global $regex_consonant;
  553. $c = $regex_consonant;
  554. return preg_match("#$c{2}$#", $str, $matches) AND $matches[0]{0} == $matches[0]{1};
  555. }
  556. /**
  557. * Checks for ending CVC sequence where second C is not W, X or Y
  558. *
  559. * @param string $str String to check
  560. * @return bool Result
  561. */
  562. function cvc($str)
  563. {
  564. $c = $regex_consonant;
  565. $v = $regex_vowel;
  566. return preg_match("#($c$v$c)$#", $str, $matches)
  567. AND strlen($matches[1]) == 3
  568. AND $matches[1]{2} != 'w'
  569. AND $matches[1]{2} != 'x'
  570. AND $matches[1]{2} != 'y';
  571. }
  572. ?>