spoofchecker_class.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #include "spoofchecker_class.h"
  17. #include "spoofchecker_main.h"
  18. #include "spoofchecker_create.h"
  19. #include "php_intl.h"
  20. #include "intl_error.h"
  21. #include <unicode/uspoof.h>
  22. zend_class_entry *Spoofchecker_ce_ptr = NULL;
  23. static zend_object_handlers Spoofchecker_handlers;
  24. /*
  25. * Auxiliary functions needed by objects of 'Spoofchecker' class
  26. */
  27. /* {{{ Spoofchecker_objects_free */
  28. void Spoofchecker_objects_free(zend_object *object)
  29. {
  30. Spoofchecker_object* co = php_intl_spoofchecker_fetch_object(object);
  31. zend_object_std_dtor(&co->zo);
  32. spoofchecker_object_destroy(co);
  33. }
  34. /* }}} */
  35. /* {{{ Spoofchecker_object_create */
  36. zend_object *Spoofchecker_object_create(zend_class_entry *ce)
  37. {
  38. Spoofchecker_object* intern;
  39. intern = zend_object_alloc(sizeof(Spoofchecker_object), ce);
  40. intl_error_init(SPOOFCHECKER_ERROR_P(intern));
  41. zend_object_std_init(&intern->zo, ce);
  42. object_properties_init(&intern->zo, ce);
  43. intern->zo.handlers = &Spoofchecker_handlers;
  44. return &intern->zo;
  45. }
  46. /* }}} */
  47. /*
  48. * 'Spoofchecker' class registration structures & functions
  49. */
  50. /* {{{ Spoofchecker methods arguments info */
  51. ZEND_BEGIN_ARG_INFO_EX(spoofchecker_0_args, 0, 0, 0)
  52. ZEND_END_ARG_INFO()
  53. ZEND_BEGIN_ARG_INFO_EX(spoofchecker_set_checks, 0, 0, 1)
  54. ZEND_ARG_INFO(0, checks)
  55. ZEND_END_ARG_INFO()
  56. ZEND_BEGIN_ARG_INFO_EX(spoofchecker_set_allowed_locales, 0, 0, 1)
  57. ZEND_ARG_INFO(0, locale_list)
  58. ZEND_END_ARG_INFO()
  59. ZEND_BEGIN_ARG_INFO_EX(spoofchecker_is_suspicous, 0, 0, 1)
  60. ZEND_ARG_INFO(0, text)
  61. ZEND_ARG_INFO(1, error)
  62. ZEND_END_ARG_INFO()
  63. ZEND_BEGIN_ARG_INFO_EX(spoofchecker_are_confusable, 0, 0, 2)
  64. ZEND_ARG_INFO(0, s1)
  65. ZEND_ARG_INFO(0, s2)
  66. ZEND_ARG_INFO(1, error)
  67. ZEND_END_ARG_INFO()
  68. #if U_ICU_VERSION_MAJOR_NUM >= 58
  69. ZEND_BEGIN_ARG_INFO_EX(spoofchecker_set_restriction_level, 0, 0, 1)
  70. ZEND_ARG_INFO(0, level)
  71. ZEND_END_ARG_INFO()
  72. #endif
  73. /* }}} */
  74. /* {{{ Spoofchecker_class_functions
  75. * Every 'Spoofchecker' class method has an entry in this table
  76. */
  77. static const zend_function_entry Spoofchecker_class_functions[] = {
  78. PHP_ME(Spoofchecker, __construct, spoofchecker_0_args, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  79. PHP_ME(Spoofchecker, isSuspicious, spoofchecker_is_suspicous, ZEND_ACC_PUBLIC)
  80. PHP_ME(Spoofchecker, areConfusable, spoofchecker_are_confusable, ZEND_ACC_PUBLIC)
  81. PHP_ME(Spoofchecker, setAllowedLocales, spoofchecker_set_allowed_locales, ZEND_ACC_PUBLIC)
  82. PHP_ME(Spoofchecker, setChecks, spoofchecker_set_checks, ZEND_ACC_PUBLIC)
  83. #if U_ICU_VERSION_MAJOR_NUM >= 58
  84. PHP_ME(Spoofchecker, setRestrictionLevel, spoofchecker_set_restriction_level, ZEND_ACC_PUBLIC)
  85. #endif
  86. PHP_FE_END
  87. };
  88. /* }}} */
  89. static zend_object *spoofchecker_clone_obj(zval *object) /* {{{ */
  90. {
  91. zend_object *new_obj_val;
  92. Spoofchecker_object *sfo, *new_sfo;
  93. sfo = Z_INTL_SPOOFCHECKER_P(object);
  94. intl_error_reset(SPOOFCHECKER_ERROR_P(sfo));
  95. new_obj_val = Spoofchecker_ce_ptr->create_object(Z_OBJCE_P(object));
  96. new_sfo = php_intl_spoofchecker_fetch_object(new_obj_val);
  97. /* clone standard parts */
  98. zend_objects_clone_members(&new_sfo->zo, &sfo->zo);
  99. /* clone internal object */
  100. new_sfo->uspoof = uspoof_clone(sfo->uspoof, SPOOFCHECKER_ERROR_CODE_P(new_sfo));
  101. if(U_FAILURE(SPOOFCHECKER_ERROR_CODE(new_sfo))) {
  102. /* set up error in case error handler is interested */
  103. intl_error_set( NULL, SPOOFCHECKER_ERROR_CODE(new_sfo), "Failed to clone SpoofChecker object", 0 );
  104. Spoofchecker_objects_free(&new_sfo->zo); /* free new object */
  105. zend_error(E_ERROR, "Failed to clone SpoofChecker object");
  106. }
  107. return new_obj_val;
  108. }
  109. /* }}} */
  110. /* {{{ spoofchecker_register_Spoofchecker_class
  111. * Initialize 'Spoofchecker' class
  112. */
  113. void spoofchecker_register_Spoofchecker_class(void)
  114. {
  115. zend_class_entry ce;
  116. /* Create and register 'Spoofchecker' class. */
  117. INIT_CLASS_ENTRY(ce, "Spoofchecker", Spoofchecker_class_functions);
  118. ce.create_object = Spoofchecker_object_create;
  119. Spoofchecker_ce_ptr = zend_register_internal_class(&ce);
  120. memcpy(&Spoofchecker_handlers, &std_object_handlers,
  121. sizeof Spoofchecker_handlers);
  122. Spoofchecker_handlers.offset = XtOffsetOf(Spoofchecker_object, zo);
  123. Spoofchecker_handlers.clone_obj = spoofchecker_clone_obj;
  124. Spoofchecker_handlers.free_obj = Spoofchecker_objects_free;
  125. }
  126. /* }}} */
  127. /* {{{ void spoofchecker_object_init( Spoofchecker_object* co )
  128. * Initialize internals of Spoofchecker_object.
  129. * Must be called before any other call to 'spoofchecker_object_...' functions.
  130. */
  131. void spoofchecker_object_init(Spoofchecker_object* co)
  132. {
  133. if (!co) {
  134. return;
  135. }
  136. intl_error_init(SPOOFCHECKER_ERROR_P(co));
  137. }
  138. /* }}} */
  139. /* {{{ void spoofchecker_object_destroy( Spoofchecker_object* co )
  140. * Clean up mem allocted by internals of Spoofchecker_object
  141. */
  142. void spoofchecker_object_destroy(Spoofchecker_object* co)
  143. {
  144. if (!co) {
  145. return;
  146. }
  147. if (co->uspoof) {
  148. uspoof_close(co->uspoof);
  149. co->uspoof = NULL;
  150. }
  151. intl_error_reset(SPOOFCHECKER_ERROR_P(co));
  152. }
  153. /* }}} */
  154. /*
  155. * Local variables:
  156. * tab-width: 4
  157. * c-basic-offset: 4
  158. * End:
  159. * vim600: noet sw=4 ts=4 fdm=marker
  160. * vim<600: noet sw=4 ts=4
  161. */