xref: /PHP-8.2/ext/pspell/pspell.c (revision f72d2645)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Author: Vlad Krupin <phpdevel@echospace.com>                         |
14    +----------------------------------------------------------------------+
15 */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include "php.h"
22 
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 
27 #ifdef HAVE_PSPELL
28 
29 /* this will enforce compatibility in .12 version (broken after .11.2) */
30 #define USE_ORIGINAL_MANAGER_FUNCS
31 
32 #include "php_pspell.h"
33 #include <pspell.h>
34 #include "ext/standard/info.h"
35 
36 #define PSPELL_FAST 1L
37 #define PSPELL_NORMAL 2L
38 #define PSPELL_BAD_SPELLERS 3L
39 #define PSPELL_SPEED_MASK_INTERNAL 3L
40 #define PSPELL_RUN_TOGETHER 8L
41 
42 #include "pspell_arginfo.h"
43 
44 /* Largest ignored word can be 999 characters (this seems sane enough),
45  * and it takes 3 bytes to represent that (see pspell_config_ignore)
46  */
47 #define PSPELL_LARGEST_WORD 3
48 
49 static PHP_MINIT_FUNCTION(pspell);
50 static PHP_MINFO_FUNCTION(pspell);
51 
52 static zend_class_entry *php_pspell_ce = NULL;
53 static zend_object_handlers php_pspell_handlers;
54 static zend_class_entry *php_pspell_config_ce = NULL;
55 static zend_object_handlers php_pspell_config_handlers;
56 
57 zend_module_entry pspell_module_entry = {
58 	STANDARD_MODULE_HEADER,
59 	"pspell",
60 	ext_functions,
61 	PHP_MINIT(pspell),
62 	NULL,
63 	NULL,
64 	NULL,
65 	PHP_MINFO(pspell),
66 	PHP_PSPELL_VERSION,
67 	STANDARD_MODULE_PROPERTIES,
68 };
69 
70 #ifdef COMPILE_DL_PSPELL
71 ZEND_GET_MODULE(pspell)
72 #endif
73 
74 /* class PSpell */
75 
76 typedef struct _php_pspell_object {
77 	PspellManager *mgr;
78 	zend_object std;
79 } php_pspell_object;
80 
php_pspell_object_from_zend_object(zend_object * zobj)81 static php_pspell_object *php_pspell_object_from_zend_object(zend_object *zobj) {
82 	return ((php_pspell_object*)(zobj + 1)) - 1;
83 }
84 
php_pspell_object_to_zend_object(php_pspell_object * obj)85 static zend_object *php_pspell_object_to_zend_object(php_pspell_object *obj) {
86 	return ((zend_object*)(obj + 1)) - 1;
87 }
88 
php_pspell_object_get_constructor(zend_object * object)89 static zend_function *php_pspell_object_get_constructor(zend_object *object)
90 {
91 	zend_throw_error(NULL, "You cannot initialize a PSpell\\Dictionary object except through helper functions");
92 	return NULL;
93 }
94 
php_pspell_object_create(zend_class_entry * ce)95 static zend_object *php_pspell_object_create(zend_class_entry *ce)
96 {
97 	php_pspell_object *obj = zend_object_alloc(sizeof(php_pspell_object), ce);
98 	zend_object *zobj = php_pspell_object_to_zend_object(obj);
99 
100 	obj->mgr = NULL;
101 	zend_object_std_init(zobj, ce);
102 	object_properties_init(zobj, ce);
103 	zobj->handlers = &php_pspell_handlers;
104 
105 	return zobj;
106 }
107 
php_pspell_object_free(zend_object * zobj)108 static void php_pspell_object_free(zend_object *zobj) {
109 	delete_pspell_manager(php_pspell_object_from_zend_object(zobj)->mgr);
110 }
111 
112 /* class PSpellConfig */
113 
114 typedef struct _php_pspell_config_object {
115 	PspellConfig *cfg;
116 	zend_object std;
117 } php_pspell_config_object;
118 
php_pspell_config_object_from_zend_object(zend_object * zobj)119 static php_pspell_config_object *php_pspell_config_object_from_zend_object(zend_object *zobj) {
120 	return ((php_pspell_config_object*)(zobj + 1)) - 1;
121 }
122 
php_pspell_config_object_to_zend_object(php_pspell_config_object * obj)123 static zend_object *php_pspell_config_object_to_zend_object(php_pspell_config_object *obj) {
124 	return ((zend_object*)(obj + 1)) - 1;
125 }
126 
php_pspell_config_object_get_constructor(zend_object * object)127 static zend_function *php_pspell_config_object_get_constructor(zend_object *object)
128 {
129 	zend_throw_error(NULL, "You cannot initialize a PSpell\\Config object except through helper functions");
130 	return NULL;
131 }
132 
php_pspell_config_object_create(zend_class_entry * ce)133 static zend_object *php_pspell_config_object_create(zend_class_entry *ce)
134 {
135 	php_pspell_config_object *obj = zend_object_alloc(sizeof(php_pspell_config_object), ce);
136 	zend_object *zobj = php_pspell_config_object_to_zend_object(obj);
137 
138 	obj->cfg = NULL;
139 	zend_object_std_init(zobj, ce);
140 	object_properties_init(zobj, ce);
141 	zobj->handlers = &php_pspell_config_handlers;
142 
143 	return zobj;
144 }
145 
php_pspell_config_object_free(zend_object * zobj)146 static void php_pspell_config_object_free(zend_object *zobj) {
147 	delete_pspell_config(php_pspell_config_object_from_zend_object(zobj)->cfg);
148 }
149 
150 /* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(pspell)151 static PHP_MINIT_FUNCTION(pspell)
152 {
153 	php_pspell_ce = register_class_PSpell_Dictionary();
154 	php_pspell_ce->create_object = php_pspell_object_create;
155 
156 	memcpy(&php_pspell_handlers, &std_object_handlers, sizeof(zend_object_handlers));
157 	php_pspell_handlers.clone_obj = NULL;
158 	php_pspell_handlers.free_obj = php_pspell_object_free;
159 	php_pspell_handlers.get_constructor = php_pspell_object_get_constructor;
160 	php_pspell_handlers.offset = XtOffsetOf(php_pspell_object, std);
161 
162 	php_pspell_config_ce = register_class_PSpell_Config();
163 	php_pspell_config_ce->create_object = php_pspell_config_object_create;
164 
165 	memcpy(&php_pspell_config_handlers, &std_object_handlers, sizeof(zend_object_handlers));
166 	php_pspell_config_handlers.clone_obj = NULL;
167 	php_pspell_config_handlers.free_obj = php_pspell_config_object_free;
168 	php_pspell_config_handlers.get_constructor = php_pspell_config_object_get_constructor;
169 	php_pspell_config_handlers.offset = XtOffsetOf(php_pspell_config_object, std);
170 
171 	register_pspell_symbols(module_number);
172 
173 	return SUCCESS;
174 }
175 /* }}} */
176 
177 /* {{{ Load a dictionary */
PHP_FUNCTION(pspell_new)178 PHP_FUNCTION(pspell_new)
179 {
180 	char *language, *spelling = NULL, *jargon = NULL, *encoding = NULL;
181 	size_t language_len, spelling_len = 0, jargon_len = 0, encoding_len = 0;
182 	zend_long mode = Z_L(0), speed = Z_L(0);
183 	int argc = ZEND_NUM_ARGS();
184 
185 #ifdef PHP_WIN32
186 	TCHAR aspell_dir[200];
187 	TCHAR data_dir[220];
188 	TCHAR dict_dir[220];
189 	HKEY hkey;
190 	DWORD dwType,dwLen;
191 #endif
192 
193 	PspellCanHaveError *ret;
194 	PspellConfig *config;
195 
196 	if (zend_parse_parameters(argc, "s|sssl", &language, &language_len, &spelling, &spelling_len,
197 		&jargon, &jargon_len, &encoding, &encoding_len, &mode) == FAILURE) {
198 		RETURN_THROWS();
199 	}
200 
201 	config = new_pspell_config();
202 
203 #ifdef PHP_WIN32
204 	/* If aspell was installed using installer, we should have a key
205 	 * pointing to the location of the dictionaries
206 	 */
207 	if (0 == RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Aspell", &hkey)) {
208 		LONG result;
209 		dwLen = sizeof(aspell_dir) - 1;
210 		result = RegQueryValueEx(hkey, "", NULL, &dwType, (LPBYTE)&aspell_dir, &dwLen);
211 		RegCloseKey(hkey);
212 		if (result == ERROR_SUCCESS) {
213 			strlcpy(data_dir, aspell_dir, sizeof(data_dir));
214 			strlcat(data_dir, "\\data", sizeof(data_dir));
215 			strlcpy(dict_dir, aspell_dir, sizeof(dict_dir));
216 			strlcat(dict_dir, "\\dict", sizeof(dict_dir));
217 
218 			pspell_config_replace(config, "data-dir", data_dir);
219 			pspell_config_replace(config, "dict-dir", dict_dir);
220 		}
221 	}
222 #endif
223 
224 	pspell_config_replace(config, "language-tag", language);
225 
226 	if (spelling_len) {
227 		pspell_config_replace(config, "spelling", spelling);
228 	}
229 
230 	if (jargon_len) {
231 		pspell_config_replace(config, "jargon", jargon);
232 	}
233 
234 	if (encoding_len) {
235 		pspell_config_replace(config, "encoding", encoding);
236 	}
237 
238 	if (mode) {
239 		speed = mode & PSPELL_SPEED_MASK_INTERNAL;
240 
241 		/* First check what mode we want (how many suggestions) */
242 		if (speed == PSPELL_FAST) {
243 			pspell_config_replace(config, "sug-mode", "fast");
244 		} else if (speed == PSPELL_NORMAL) {
245 			pspell_config_replace(config, "sug-mode", "normal");
246 		} else if (speed == PSPELL_BAD_SPELLERS) {
247 			pspell_config_replace(config, "sug-mode", "bad-spellers");
248 		}
249 
250 		/* Then we see if run-together words should be treated as valid components */
251 		if (mode & PSPELL_RUN_TOGETHER) {
252 			pspell_config_replace(config, "run-together", "true");
253 		}
254 	}
255 
256 	ret = new_pspell_manager(config);
257 	delete_pspell_config(config);
258 
259 	if (pspell_error_number(ret) != 0) {
260 		php_error_docref(NULL, E_WARNING, "PSPELL couldn't open the dictionary. reason: %s", pspell_error_message(ret));
261 		delete_pspell_can_have_error(ret);
262 		RETURN_FALSE;
263 	}
264 
265 	object_init_ex(return_value, php_pspell_ce);
266 	php_pspell_object_from_zend_object(Z_OBJ_P(return_value))->mgr = to_pspell_manager(ret);
267 }
268 /* }}} */
269 
270 /* {{{ Load a dictionary with a personal wordlist*/
PHP_FUNCTION(pspell_new_personal)271 PHP_FUNCTION(pspell_new_personal)
272 {
273 	char *personal, *language, *spelling = NULL, *jargon = NULL, *encoding = NULL;
274 	size_t personal_len, language_len, spelling_len = 0, jargon_len = 0, encoding_len = 0;
275 	zend_long mode = Z_L(0), speed = Z_L(0);
276 	int argc = ZEND_NUM_ARGS();
277 
278 #ifdef PHP_WIN32
279 	TCHAR aspell_dir[200];
280 	TCHAR data_dir[220];
281 	TCHAR dict_dir[220];
282 	HKEY hkey;
283 	DWORD dwType,dwLen;
284 #endif
285 
286 	PspellCanHaveError *ret;
287 	PspellConfig *config;
288 
289 	if (zend_parse_parameters(argc, "ps|sssl", &personal, &personal_len, &language, &language_len,
290 		&spelling, &spelling_len, &jargon, &jargon_len, &encoding, &encoding_len, &mode) == FAILURE) {
291 		RETURN_THROWS();
292 	}
293 
294 	config = new_pspell_config();
295 
296 #ifdef PHP_WIN32
297 	/* If aspell was installed using installer, we should have a key
298 	 * pointing to the location of the dictionaries
299 	 */
300 	if (0 == RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Aspell", &hkey)) {
301 		LONG result;
302 		dwLen = sizeof(aspell_dir) - 1;
303 		result = RegQueryValueEx(hkey, "", NULL, &dwType, (LPBYTE)&aspell_dir, &dwLen);
304 		RegCloseKey(hkey);
305 		if (result == ERROR_SUCCESS) {
306 			strlcpy(data_dir, aspell_dir, sizeof(data_dir));
307 			strlcat(data_dir, "\\data", sizeof(data_dir));
308 			strlcpy(dict_dir, aspell_dir, sizeof(dict_dir));
309 			strlcat(dict_dir, "\\dict", sizeof(dict_dir));
310 
311 			pspell_config_replace(config, "data-dir", data_dir);
312 			pspell_config_replace(config, "dict-dir", dict_dir);
313 		}
314 	}
315 #endif
316 
317 	if (php_check_open_basedir(personal)) {
318 		delete_pspell_config(config);
319 		RETURN_FALSE;
320 	}
321 
322 	pspell_config_replace(config, "personal", personal);
323 	pspell_config_replace(config, "save-repl", "false");
324 
325 	pspell_config_replace(config, "language-tag", language);
326 
327 	if (spelling_len) {
328 		pspell_config_replace(config, "spelling", spelling);
329 	}
330 
331 	if (jargon_len) {
332 		pspell_config_replace(config, "jargon", jargon);
333 	}
334 
335 	if (encoding_len) {
336 		pspell_config_replace(config, "encoding", encoding);
337 	}
338 
339 	if (mode) {
340 		speed = mode & PSPELL_SPEED_MASK_INTERNAL;
341 
342 		/* First check what mode we want (how many suggestions) */
343 		if (speed == PSPELL_FAST) {
344 			pspell_config_replace(config, "sug-mode", "fast");
345 		} else if (speed == PSPELL_NORMAL) {
346 			pspell_config_replace(config, "sug-mode", "normal");
347 		} else if (speed == PSPELL_BAD_SPELLERS) {
348 			pspell_config_replace(config, "sug-mode", "bad-spellers");
349 		}
350 
351 		/* Then we see if run-together words should be treated as valid components */
352 		if (mode & PSPELL_RUN_TOGETHER) {
353 			pspell_config_replace(config, "run-together", "true");
354 		}
355 	}
356 
357 	ret = new_pspell_manager(config);
358 	delete_pspell_config(config);
359 
360 	if (pspell_error_number(ret) != 0) {
361 		php_error_docref(NULL, E_WARNING, "PSPELL couldn't open the dictionary. reason: %s", pspell_error_message(ret));
362 		delete_pspell_can_have_error(ret);
363 		RETURN_FALSE;
364 	}
365 
366 	object_init_ex(return_value, php_pspell_ce);
367 	php_pspell_object_from_zend_object(Z_OBJ_P(return_value))->mgr = to_pspell_manager(ret);
368 }
369 /* }}} */
370 
371 /* {{{ Load a dictionary based on the given config */
PHP_FUNCTION(pspell_new_config)372 PHP_FUNCTION(pspell_new_config)
373 {
374 	zval *zcfg;
375 	PspellCanHaveError *ret;
376 	PspellConfig *config;
377 
378 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zcfg, php_pspell_config_ce) == FAILURE) {
379 		RETURN_THROWS();
380 	}
381 	config = php_pspell_config_object_from_zend_object(Z_OBJ_P(zcfg))->cfg;
382 
383 	ret = new_pspell_manager(config);
384 
385 	if (pspell_error_number(ret) != 0) {
386 		php_error_docref(NULL, E_WARNING, "PSPELL couldn't open the dictionary. reason: %s", pspell_error_message(ret));
387 		delete_pspell_can_have_error(ret);
388 		RETURN_FALSE;
389 	}
390 
391 	object_init_ex(return_value, php_pspell_ce);
392 	php_pspell_object_from_zend_object(Z_OBJ_P(return_value))->mgr = to_pspell_manager(ret);
393 }
394 /* }}} */
395 
396 /* {{{ Returns true if word is valid */
PHP_FUNCTION(pspell_check)397 PHP_FUNCTION(pspell_check)
398 {
399 	zval *zmgr;
400 	zend_string *word;
401 	PspellManager *manager;
402 
403 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &zmgr, php_pspell_ce, &word) == FAILURE) {
404 		RETURN_THROWS();
405 	}
406 	manager = php_pspell_object_from_zend_object(Z_OBJ_P(zmgr))->mgr;
407 
408 	if (pspell_manager_check(manager, ZSTR_VAL(word))) {
409 		RETURN_TRUE;
410 	} else {
411 		RETURN_FALSE;
412 	}
413 }
414 /* }}} */
415 
416 /* {{{ Returns array of suggestions */
PHP_FUNCTION(pspell_suggest)417 PHP_FUNCTION(pspell_suggest)
418 {
419 	zval *zmgr;
420 	zend_string *word;
421 	PspellManager *manager;
422 	const PspellWordList *wl;
423 	const char *sug;
424 
425 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &zmgr, php_pspell_ce, &word) == FAILURE) {
426 		RETURN_THROWS();
427 	}
428 	manager = php_pspell_object_from_zend_object(Z_OBJ_P(zmgr))->mgr;
429 
430 	array_init(return_value);
431 
432 	wl = pspell_manager_suggest(manager, ZSTR_VAL(word));
433 	if (wl) {
434 		PspellStringEmulation *els = pspell_word_list_elements(wl);
435 		while ((sug = pspell_string_emulation_next(els)) != 0) {
436 			add_next_index_string(return_value,(char *)sug);
437 		}
438 		delete_pspell_string_emulation(els);
439 	} else {
440 		php_error_docref(NULL, E_WARNING, "PSPELL had a problem. details: %s", pspell_manager_error_message(manager));
441 		RETURN_FALSE;
442 	}
443 }
444 /* }}} */
445 
446 /* {{{ Notify the dictionary of a user-selected replacement */
PHP_FUNCTION(pspell_store_replacement)447 PHP_FUNCTION(pspell_store_replacement)
448 {
449 	zval *zmgr;
450 	zend_string *miss, *corr;
451 	PspellManager *manager;
452 
453 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OSS", &zmgr, php_pspell_ce, &miss, &corr) == FAILURE) {
454 		RETURN_THROWS();
455 	}
456 	manager = php_pspell_object_from_zend_object(Z_OBJ_P(zmgr))->mgr;
457 
458 	pspell_manager_store_replacement(manager, ZSTR_VAL(miss), ZSTR_VAL(corr));
459 	if (pspell_manager_error_number(manager) == 0) {
460 		RETURN_TRUE;
461 	} else {
462 		php_error_docref(NULL, E_WARNING, "pspell_store_replacement() gave error: %s", pspell_manager_error_message(manager));
463 		RETURN_FALSE;
464 	}
465 }
466 /* }}} */
467 
468 /* {{{ Adds a word to a personal list */
PHP_FUNCTION(pspell_add_to_personal)469 PHP_FUNCTION(pspell_add_to_personal)
470 {
471 	zval *zmgr;
472 	zend_string *word;
473 	PspellManager *manager;
474 
475 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &zmgr, php_pspell_ce, &word) == FAILURE) {
476 		RETURN_THROWS();
477 	}
478 	manager = php_pspell_object_from_zend_object(Z_OBJ_P(zmgr))->mgr;
479 
480 	/*If the word is empty, we have to return; otherwise we'll segfault! ouch!*/
481 	if (ZSTR_LEN(word) == 0) {
482 		RETURN_FALSE;
483 	}
484 
485 	pspell_manager_add_to_personal(manager, ZSTR_VAL(word));
486 	if (pspell_manager_error_number(manager) == 0) {
487 		RETURN_TRUE;
488 	} else {
489 		php_error_docref(NULL, E_WARNING, "pspell_add_to_personal() gave error: %s", pspell_manager_error_message(manager));
490 		RETURN_FALSE;
491 	}
492 }
493 /* }}} */
494 
495 /* {{{ Adds a word to the current session */
PHP_FUNCTION(pspell_add_to_session)496 PHP_FUNCTION(pspell_add_to_session)
497 {
498 	zval *zmgr;
499 	zend_string *word;
500 	PspellManager *manager;
501 
502 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &zmgr, php_pspell_ce, &word) == FAILURE) {
503 		RETURN_THROWS();
504 	}
505 	manager = php_pspell_object_from_zend_object(Z_OBJ_P(zmgr))->mgr;
506 
507 	/*If the word is empty, we have to return; otherwise we'll segfault! ouch!*/
508 	if (ZSTR_LEN(word) == 0) {
509 		RETURN_FALSE;
510 	}
511 
512 	pspell_manager_add_to_session(manager, ZSTR_VAL(word));
513 	if (pspell_manager_error_number(manager) == 0) {
514 		RETURN_TRUE;
515 	} else {
516 		php_error_docref(NULL, E_WARNING, "pspell_add_to_session() gave error: %s", pspell_manager_error_message(manager));
517 		RETURN_FALSE;
518 	}
519 }
520 /* }}} */
521 
522 /* {{{ Clears the current session */
PHP_FUNCTION(pspell_clear_session)523 PHP_FUNCTION(pspell_clear_session)
524 {
525 	zval *zmgr;
526 	PspellManager *manager;
527 
528 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zmgr, php_pspell_ce) == FAILURE) {
529 		RETURN_THROWS();
530 	}
531 	manager = php_pspell_object_from_zend_object(Z_OBJ_P(zmgr))->mgr;
532 
533 	pspell_manager_clear_session(manager);
534 	if (pspell_manager_error_number(manager) == 0) {
535 		RETURN_TRUE;
536 	} else {
537 		php_error_docref(NULL, E_WARNING, "pspell_clear_session() gave error: %s", pspell_manager_error_message(manager));
538 		RETURN_FALSE;
539 	}
540 }
541 /* }}} */
542 
543 /* {{{ Saves the current (personal) wordlist */
PHP_FUNCTION(pspell_save_wordlist)544 PHP_FUNCTION(pspell_save_wordlist)
545 {
546 	zval *zmgr;
547 	PspellManager *manager;
548 
549 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zmgr, php_pspell_ce) == FAILURE) {
550 		RETURN_THROWS();
551 	}
552 	manager = php_pspell_object_from_zend_object(Z_OBJ_P(zmgr))->mgr;
553 
554 	pspell_manager_save_all_word_lists(manager);
555 
556 	if (pspell_manager_error_number(manager) == 0) {
557 		RETURN_TRUE;
558 	} else {
559 		php_error_docref(NULL, E_WARNING, "pspell_save_wordlist() gave error: %s", pspell_manager_error_message(manager));
560 		RETURN_FALSE;
561 	}
562 
563 }
564 /* }}} */
565 
566 /* {{{ Create a new config to be used later to create a manager */
PHP_FUNCTION(pspell_config_create)567 PHP_FUNCTION(pspell_config_create)
568 {
569 	char *language, *spelling = NULL, *jargon = NULL, *encoding = NULL;
570 	size_t language_len, spelling_len = 0, jargon_len = 0, encoding_len = 0;
571 	PspellConfig *config;
572 
573 #ifdef PHP_WIN32
574 	TCHAR aspell_dir[200];
575 	TCHAR data_dir[220];
576 	TCHAR dict_dir[220];
577 	HKEY hkey;
578 	DWORD dwType,dwLen;
579 #endif
580 
581 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|sss", &language, &language_len, &spelling, &spelling_len,
582 		&jargon, &jargon_len, &encoding, &encoding_len) == FAILURE) {
583 		RETURN_THROWS();
584 	}
585 
586 	config = new_pspell_config();
587 
588 #ifdef PHP_WIN32
589     /* If aspell was installed using installer, we should have a key
590      * pointing to the location of the dictionaries
591      */
592 	if (0 == RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Aspell", &hkey)) {
593 		LONG result;
594 		dwLen = sizeof(aspell_dir) - 1;
595 		result = RegQueryValueEx(hkey, "", NULL, &dwType, (LPBYTE)&aspell_dir, &dwLen);
596 		RegCloseKey(hkey);
597 		if (result == ERROR_SUCCESS) {
598 			strlcpy(data_dir, aspell_dir, sizeof(data_dir));
599 			strlcat(data_dir, "\\data", sizeof(data_dir));
600 			strlcpy(dict_dir, aspell_dir, sizeof(dict_dir));
601 			strlcat(dict_dir, "\\dict", sizeof(dict_dir));
602 
603 			pspell_config_replace(config, "data-dir", data_dir);
604 			pspell_config_replace(config, "dict-dir", dict_dir);
605 		}
606 	}
607 #endif
608 
609 	pspell_config_replace(config, "language-tag", language);
610 
611 	if (spelling_len) {
612 		pspell_config_replace(config, "spelling", spelling);
613 	}
614 
615 	if (jargon_len) {
616 		pspell_config_replace(config, "jargon", jargon);
617 	}
618 
619 	if (encoding_len) {
620 		pspell_config_replace(config, "encoding", encoding);
621 	}
622 
623 	/* By default I do not want to write anything anywhere because it'll try to write to $HOME
624 	which is not what we want */
625 	pspell_config_replace(config, "save-repl", "false");
626 
627 	object_init_ex(return_value, php_pspell_config_ce);
628 	php_pspell_config_object_from_zend_object(Z_OBJ_P(return_value))->cfg = config;
629 }
630 /* }}} */
631 
632 /* {{{ Consider run-together words as valid components */
PHP_FUNCTION(pspell_config_runtogether)633 PHP_FUNCTION(pspell_config_runtogether)
634 {
635 	zval *zcfg;
636 	bool runtogether;
637 	PspellConfig *config;
638 
639 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &zcfg, php_pspell_config_ce, &runtogether) == FAILURE) {
640 		RETURN_THROWS();
641 	}
642 	config = php_pspell_config_object_from_zend_object(Z_OBJ_P(zcfg))->cfg;
643 
644 	pspell_config_replace(config, "run-together", runtogether ? "true" : "false");
645 
646 	RETURN_TRUE;
647 }
648 /* }}} */
649 
650 /* {{{ Select mode for config (PSPELL_FAST, PSPELL_NORMAL or PSPELL_BAD_SPELLERS) */
PHP_FUNCTION(pspell_config_mode)651 PHP_FUNCTION(pspell_config_mode)
652 {
653 	zval *zcfg;
654 	zend_long mode;
655 	PspellConfig *config;
656 
657 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &zcfg, php_pspell_config_ce, &mode) == FAILURE) {
658 		RETURN_THROWS();
659 	}
660 	config = php_pspell_config_object_from_zend_object(Z_OBJ_P(zcfg))->cfg;
661 
662 	/* First check what mode we want (how many suggestions) */
663 	if (mode == PSPELL_FAST) {
664 		pspell_config_replace(config, "sug-mode", "fast");
665 	} else if (mode == PSPELL_NORMAL) {
666 		pspell_config_replace(config, "sug-mode", "normal");
667 	} else if (mode == PSPELL_BAD_SPELLERS) {
668 		pspell_config_replace(config, "sug-mode", "bad-spellers");
669 	}
670 
671 	RETURN_TRUE;
672 }
673 /* }}} */
674 
675 /* {{{ Ignore words <= n chars */
PHP_FUNCTION(pspell_config_ignore)676 PHP_FUNCTION(pspell_config_ignore)
677 {
678 	char ignore_str[MAX_LENGTH_OF_LONG + 1];
679 	zval *zcfg;
680 	zend_long ignore = 0L;
681 	PspellConfig *config;
682 
683 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ol", &zcfg, php_pspell_config_ce, &ignore) == FAILURE) {
684 		RETURN_THROWS();
685 	}
686 	config = php_pspell_config_object_from_zend_object(Z_OBJ_P(zcfg))->cfg;
687 
688 	snprintf(ignore_str, sizeof(ignore_str), ZEND_LONG_FMT, ignore);
689 
690 	pspell_config_replace(config, "ignore", ignore_str);
691 	RETURN_TRUE;
692 }
693 /* }}} */
694 
pspell_config_path(INTERNAL_FUNCTION_PARAMETERS,char * option)695 static void pspell_config_path(INTERNAL_FUNCTION_PARAMETERS, char *option)
696 {
697 	zval *zcfg;
698 	zend_string *value;
699 	PspellConfig *config;
700 
701 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP", &zcfg, php_pspell_config_ce, &value) == FAILURE) {
702 		RETURN_THROWS();
703 	}
704 	config = php_pspell_config_object_from_zend_object(Z_OBJ_P(zcfg))->cfg;
705 
706 	if (php_check_open_basedir(ZSTR_VAL(value))) {
707 		RETURN_FALSE;
708 	}
709 
710 	pspell_config_replace(config, option, ZSTR_VAL(value));
711 
712 	RETURN_TRUE;
713 }
714 
715 /* {{{ Use a personal dictionary for this config */
PHP_FUNCTION(pspell_config_personal)716 PHP_FUNCTION(pspell_config_personal)
717 {
718 	pspell_config_path(INTERNAL_FUNCTION_PARAM_PASSTHRU, "personal");
719 }
720 /* }}} */
721 
722 /* {{{ location of the main word list */
PHP_FUNCTION(pspell_config_dict_dir)723 PHP_FUNCTION(pspell_config_dict_dir)
724 {
725 	pspell_config_path(INTERNAL_FUNCTION_PARAM_PASSTHRU, "dict-dir");
726 }
727 /* }}} */
728 
729 /* {{{ location of language data files */
PHP_FUNCTION(pspell_config_data_dir)730 PHP_FUNCTION(pspell_config_data_dir)
731 {
732 	pspell_config_path(INTERNAL_FUNCTION_PARAM_PASSTHRU, "data-dir");
733 }
734 /* }}} */
735 
736 /* {{{ Use a personal dictionary with replacement pairs for this config */
PHP_FUNCTION(pspell_config_repl)737 PHP_FUNCTION(pspell_config_repl)
738 {
739 	zval *zcfg;
740 	zend_string *repl;
741 	PspellConfig *config;
742 
743 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP", &zcfg, php_pspell_config_ce, &repl) == FAILURE) {
744 		RETURN_THROWS();
745 	}
746 	config = php_pspell_config_object_from_zend_object(Z_OBJ_P(zcfg))->cfg;
747 
748 	pspell_config_replace(config, "save-repl", "true");
749 
750 	if (php_check_open_basedir(ZSTR_VAL(repl))) {
751 		RETURN_FALSE;
752 	}
753 
754 	pspell_config_replace(config, "repl", ZSTR_VAL(repl));
755 
756 	RETURN_TRUE;
757 }
758 /* }}} */
759 
760 /* {{{ Save replacement pairs when personal list is saved for this config */
PHP_FUNCTION(pspell_config_save_repl)761 PHP_FUNCTION(pspell_config_save_repl)
762 {
763 	zval *zcfg;
764 	bool save;
765 	PspellConfig *config;
766 
767 	if (zend_parse_parameters(ZEND_NUM_ARGS(), "Ob", &zcfg, php_pspell_config_ce, &save) == FAILURE) {
768 		RETURN_THROWS();
769 	}
770 	config = php_pspell_config_object_from_zend_object(Z_OBJ_P(zcfg))->cfg;
771 
772 	pspell_config_replace(config, "save-repl", save ? "true" : "false");
773 
774 	RETURN_TRUE;
775 }
776 /* }}} */
777 
778 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(pspell)779 static PHP_MINFO_FUNCTION(pspell)
780 {
781 	php_info_print_table_start();
782 	php_info_print_table_row(2, "PSpell Support", "enabled");
783 	php_info_print_table_end();
784 }
785 /* }}} */
786 
787 #endif
788