1<?php
2if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
3if (!defined('DOKU_REG')) define ('DOKU_REG', DOKU_PLUGIN.'autolink3/register/');
4
5
6/**
7 * pars $ text to find potential links
8 * @param $text content of $page
9 * @param $word processing word(link)
10 * @param $link associate page
11 * @param $filename name of the treaty file
12 * @return $ret (modified $text)
13 */
14
15function link_replace($text = NULL, $word, $link, $filename)
16{
17	if ($text == NULL)
18	{
19		$rd_page = fopen($filename, 'r');
20		while ($ligne = fgets($rd_page)){
21			$text .= $ligne;
22		}
23	}
24	$tit = preg_split("/(==*)/",$text, -1 ,PREG_SPLIT_DELIM_CAPTURE);
25	$ign = false;
26	$old = NULL;
27	foreach ($tit as $em):
28	{
29		if (preg_match("/==*/", $em)==1)$ign == false ? $ign = true : $ign = false;
30
31			$ignore = 0;
32			$tab = preg_split("/(\[\[|\]\]|\}\}|\{\{)/",$em, -1 ,PREG_SPLIT_DELIM_CAPTURE);
33			foreach($tab as $element):
34			{
35				if ($ign == false){
36					if ($element == "[[" || $element == "{{") $ignore = 1;
37					elseif ($element == "]]" || $element == "}}") $ignore = 0;
38					elseif ($ignore == 0)
39					{
40						if (preg_match('/(\W|^)('.$word.')($|[^a-z0-9_\-])/i', $element) == 1)
41						{
42							$element = preg_replace('/(\W|^)('.$word.')($|[^a-z0-9_\-])/i','\1'."[[".substr($link,0, strlen($link)-4)."|".'\2'."]]".'\3', $element);
43							$element = link_replace($element, $word, $link, $filename);
44						}
45					}
46				}
47				$ret .= $element;
48			}
49			endforeach;
50		}
51	endforeach;
52	return ($ret);
53}
54
55/**
56 * addon of sort_tab : size comparison
57 * @param $s1 : first string
58 * @param $s2 :second string
59 * @return returns the result of the size comparison of two strings
60 */
61
62function compare_len($s1, $s2)
63{
64	return (strlen ($s1) < strlen($s2));
65}
66
67/**
68 * addon of sort_tab : alphabetic comparison
69 * @param $s1 : first string
70 * @param $s2 :second string
71 * @return returns the result of the comparison of the two strings Alphabetically
72 */
73
74function compare_alpha($s1, $s2)
75{
76	return (strcmp($s1, $s2) < 0);
77}
78
79function compare_unalpha($s1, $s2)
80{
81	return (strcmp($s1, $s2) > 0);
82}
83
84/**
85 *
86 * sort tab function (size and alphabetic sort)
87 * @param $tab : link tab
88 * @param $fcmp : function pointer for the comparison type
89 * @param $col : for alphabetical comparison, determines the column to sort
90 * @return $tab
91 */
92
93function sort_tab($tab ,$fcmp, $col = 0)
94{
95	if (isset ($tab)){
96	foreach ($tab as $ligne):{
97		$i = 0;
98		while($tab[$i + 1])
99		{
100			if ($fcmp($tab[$i+1][$col], $tab[$i][$col]))
101				list($tab[$i], $tab[$i + 1]) = array($tab[$i + 1], $tab[$i]);
102			$i += 1;
103		}
104	}
105	endforeach;
106	return($tab);
107	}
108}
109
110function sort_tab_space($tab)
111{
112	if (isset ($tab)){
113	foreach ($tab as $ligne):{
114		$i = 0;
115		while($tab[$i + 1])
116		{
117			if (($tab[$i][0] == $tab[$� + 1][0]) && (strlen($tab[$i][2]) < strlen($tab[$i + 1][2])))
118				list($tab[$i], $tab[$i + 1]) = array($tab[$i + 1], $tab[$i]);
119			$i++;
120		}
121	}
122	endforeach;
123	return($tab);
124	}
125}
126
127/**
128 * check if the link already exist
129 * @param $page : link associate page
130 * @param $local : location of the link application
131 * @param $word : link
132 * @return true or false if the link is already registered or not
133 */
134
135function is_link_exist($page, $local, $word)
136{
137	$global = sprintf("%s	%s	%s\r\n", $word, $page, $local);
138	$rd = fopen(DOKU_INC.'lib/plugins/autolink3/register/register.txt', r);
139	while ($check = fgets($rd))
140	{
141		$nword = explode('	', $check);
142		if (!strcmp($check, $global) || (!strcmp($word, $nword[0]) && !strcmp($local, $nword[2])))
143			return(1);
144	}
145	if (preg_match('/:/', $word))
146		return(1);
147	return (0);
148}
149
150/**
151 * addon of get_dokuwiki_tree, check if the processing element is a page
152 * @param $page : name of the page checked
153 * @return true or false if the page exist or not
154 */
155
156function is_txt($page)
157{
158	return substr_count($page, '.txt');
159}
160
161/**
162 * read the data/pages folder and get the pages tree
163 * @param $adress : path of the pages repository
164 * @param $tab : final table with all the page
165 * @param $old : used for the recursivity in the folders
166 * @param $flag : option witch determines the try content (folders or not)
167 * @return $tab: the pages tree
168 */
169
170function get_dokupage_tree($adress, $tab, $old, $flag){
171	{
172		$rd = opendir($adress.str_replace(':','/',$old));
173		while ($element = readdir($rd))
174		{
175			if ($element != '.' && $element != '..')
176			{
177
178				if ($old != '')
179				$element = $old.':'.$element;
180				if (is_txt($element) > 0) $tab[] = ':'. $element;
181				elseif (is_dir($adress.str_replace(':','/',$element)))
182				{
183					if ($flag == 1)
184					{$tab[] = ':'.$element;
185					}
186					$tab = get_dokupage_tree($adress, $tab, $element, $flag);
187				}
188
189			}
190		}
191		closedir($rd);
192		return ($tab);
193	}
194}
195
196/**
197 * read all the registered links
198 * @return : $tab witch content the links
199 */
200
201function read_file()
202{
203	$rd = fopen (DOKU_REG."register.txt", "r");
204	while ($str = fgets($rd))
205	{
206		$ret[] = explode("	",$str);
207	}
208	fclose($rd);
209	return ($ret);
210}
211
212/**
213 * modification function to register.txt (delete and modification)
214 * @param $ligne : ligne to modifie
215 * @param  $new : the modification
216 */
217
218function mod_link($ligne, $new = '')
219{
220	$rd = fopen (DOKU_REG.'register.txt', 'r + b');
221	while ($str = fgets($rd))
222	{
223		$tab[] = $str;
224	}
225	if (isset($tab))
226	foreach ($tab as $elm):
227	{
228		if(strcmp($elm, str_replace(";", "	",$ligne."\r\n")))
229			$tab2[] = $elm;
230		else
231			$tab2[] = $new;
232	}
233	endforeach;
234	fclose($rd);
235	$rd = fopen(DOKU_REG.'register.txt', 'w');
236	if (isset($tab2))
237	foreach ($tab2 as $eee):
238	{
239		fwrite($rd, $eee);
240	}
241	endforeach;
242	fclose($rd);
243}
244?>