spoofchecker_main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  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. | http://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. | Authors: Scott MacVicar <scottmac@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include "php_intl.h"
  20. #include "spoofchecker_class.h"
  21. /* {{{ proto bool Spoofchecker::isSuspicious( string text[, int &error_code ] )
  22. * Checks if a given text contains any suspicious characters
  23. */
  24. PHP_METHOD(Spoofchecker, isSuspicious)
  25. {
  26. int ret;
  27. char *text;
  28. size_t text_len;
  29. zval *error_code = NULL;
  30. SPOOFCHECKER_METHOD_INIT_VARS;
  31. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|z", &text, &text_len, &error_code)) {
  32. return;
  33. }
  34. SPOOFCHECKER_METHOD_FETCH_OBJECT;
  35. ret = uspoof_checkUTF8(co->uspoof, text, text_len, NULL, SPOOFCHECKER_ERROR_CODE_P(co));
  36. if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
  37. php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
  38. RETURN_TRUE;
  39. }
  40. if (error_code) {
  41. zval_ptr_dtor(error_code);
  42. ZVAL_LONG(error_code, ret);
  43. }
  44. RETVAL_BOOL(ret != 0);
  45. }
  46. /* }}} */
  47. /* {{{ proto bool Spoofchecker::areConfusable( string str1, string str2[, int &error_code ] )
  48. * Checks if a given text contains any confusable characters
  49. */
  50. PHP_METHOD(Spoofchecker, areConfusable)
  51. {
  52. int ret;
  53. char *s1, *s2;
  54. size_t s1_len, s2_len;
  55. zval *error_code = NULL;
  56. SPOOFCHECKER_METHOD_INIT_VARS;
  57. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "ss|z", &s1, &s1_len,
  58. &s2, &s2_len, &error_code)) {
  59. return;
  60. }
  61. SPOOFCHECKER_METHOD_FETCH_OBJECT;
  62. if(s1_len > INT32_MAX || s2_len > INT32_MAX) {
  63. SPOOFCHECKER_ERROR_CODE(co) = U_BUFFER_OVERFLOW_ERROR;
  64. } else {
  65. ret = uspoof_areConfusableUTF8(co->uspoof, s1, (int32_t)s1_len, s2, (int32_t)s2_len, SPOOFCHECKER_ERROR_CODE_P(co));
  66. }
  67. if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
  68. php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
  69. RETURN_TRUE;
  70. }
  71. if (error_code) {
  72. zval_ptr_dtor(error_code);
  73. ZVAL_LONG(error_code, ret);
  74. }
  75. RETVAL_BOOL(ret != 0);
  76. }
  77. /* }}} */
  78. /* {{{ proto void Spoofchecker::setAllowedLocales( string locales )
  79. * Locales to use when running checks
  80. */
  81. PHP_METHOD(Spoofchecker, setAllowedLocales)
  82. {
  83. char *locales;
  84. size_t locales_len;
  85. SPOOFCHECKER_METHOD_INIT_VARS;
  86. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &locales, &locales_len)) {
  87. return;
  88. }
  89. SPOOFCHECKER_METHOD_FETCH_OBJECT;
  90. uspoof_setAllowedLocales(co->uspoof, locales, SPOOFCHECKER_ERROR_CODE_P(co));
  91. if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
  92. php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
  93. return;
  94. }
  95. }
  96. /* }}} */
  97. /* {{{ proto void Spoofchecker::setChecks( int checks )
  98. * Set the checks to run
  99. */
  100. PHP_METHOD(Spoofchecker, setChecks)
  101. {
  102. zend_long checks;
  103. SPOOFCHECKER_METHOD_INIT_VARS;
  104. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &checks)) {
  105. return;
  106. }
  107. SPOOFCHECKER_METHOD_FETCH_OBJECT;
  108. uspoof_setChecks(co->uspoof, checks, SPOOFCHECKER_ERROR_CODE_P(co));
  109. if (U_FAILURE(SPOOFCHECKER_ERROR_CODE(co))) {
  110. php_error_docref(NULL, E_WARNING, "(%d) %s", SPOOFCHECKER_ERROR_CODE(co), u_errorName(SPOOFCHECKER_ERROR_CODE(co)));
  111. }
  112. }
  113. /* }}} */
  114. #if U_ICU_VERSION_MAJOR_NUM >= 58
  115. /* {{{ proto void Spoofchecker::setRestrictionLevel( int $restriction_level )
  116. * Set the loosest restriction level allowed for strings.
  117. */
  118. PHP_METHOD(Spoofchecker, setRestrictionLevel)
  119. {
  120. zend_long level;
  121. SPOOFCHECKER_METHOD_INIT_VARS;
  122. if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &level)) {
  123. return;
  124. }
  125. SPOOFCHECKER_METHOD_FETCH_OBJECT;
  126. if (USPOOF_ASCII != level &&
  127. USPOOF_SINGLE_SCRIPT_RESTRICTIVE != level &&
  128. USPOOF_HIGHLY_RESTRICTIVE != level &&
  129. USPOOF_MODERATELY_RESTRICTIVE != level &&
  130. USPOOF_MINIMALLY_RESTRICTIVE != level &&
  131. USPOOF_UNRESTRICTIVE != level) {
  132. php_error_docref(NULL, E_WARNING, "Invalid restriction level value");
  133. return;
  134. }
  135. uspoof_setRestrictionLevel(co->uspoof, (URestrictionLevel)level);
  136. }
  137. /* }}} */
  138. #endif
  139. /*
  140. * Local variables:
  141. * tab-width: 4
  142. * c-basic-offset: 4
  143. * End:
  144. * vim600: noet sw=4 ts=4 fdm=marker
  145. * vim<600: noet sw=4 ts=4
  146. */