pspell.c 26 KB

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