pspell.c 26 KB

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