You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
5.1 KiB

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP version 4.0 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997, 1998, 1999, 2000 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.02 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://www.php.net/license/2_02.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. | Authors: Kristian Koehntopp <kris@koehntopp.de> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. /* {{{ includes & prototypes */
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "php.h"
  24. #include "php_recode.h"
  25. #if HAVE_LIBRECODE
  26. #include "ext/standard/info.h"
  27. #include "ext/standard/file.h"
  28. #include "ext/standard/php_string.h"
  29. #include "zend_list.h"
  30. #ifdef HAVE_BROKEN_RECODE
  31. extern char *program_name;
  32. char *program_name = "php";
  33. #endif
  34. /* }}} */
  35. #define SAFE_STRING(s) ((s)?(s):"")
  36. php_recode_globals recode_globals;
  37. extern int le_fp,le_pp;
  38. /* {{{ module stuff */
  39. static zend_function_entry php_recode_functions[] = {
  40. PHP_FE(recode_string, NULL)
  41. PHP_FE(recode_file, NULL)
  42. PHP_FALIAS(recode, recode_string, NULL)
  43. {NULL, NULL, NULL}
  44. };
  45. zend_module_entry recode_module_entry = {
  46. "Recode",
  47. php_recode_functions,
  48. PHP_MINIT(recode),
  49. PHP_MSHUTDOWN(recode),
  50. NULL,
  51. NULL,
  52. PHP_MINFO(recode),
  53. STANDARD_MODULE_PROPERTIES
  54. };
  55. #if APACHE
  56. extern void timeout(int sig);
  57. #endif
  58. #ifdef COMPILE_DL_RECODE
  59. ZEND_GET_MODULE(recode)
  60. #endif
  61. PHP_MINIT_FUNCTION(recode)
  62. {
  63. TSRMLS_FETCH();
  64. ReSG(outer) = recode_new_outer(true);
  65. if (ReSG(outer) == NULL)
  66. return FAILURE;
  67. return SUCCESS;
  68. }
  69. PHP_MSHUTDOWN_FUNCTION(recode)
  70. {
  71. TSRMLS_FETCH();
  72. if (ReSG(outer))
  73. recode_delete_outer(ReSG(outer));
  74. return SUCCESS;
  75. }
  76. PHP_MINFO_FUNCTION(recode)
  77. {
  78. TSRMLS_FETCH();
  79. php_info_print_table_start();
  80. php_info_print_table_row(2, "Recode Support", "enabled");
  81. php_info_print_table_row(2, "Revision", "$Revision$");
  82. php_info_print_table_end();
  83. }
  84. /* {{{ proto string recode_string(string request, string str)
  85. Recode string str according to request string */
  86. PHP_FUNCTION(recode_string)
  87. {
  88. RECODE_REQUEST request = NULL;
  89. char *r = NULL;
  90. pval **str;
  91. pval **req;
  92. bool success;
  93. int r_len=0, r_alen =0;
  94. if (ZEND_NUM_ARGS() != 2
  95. || zend_get_parameters_ex(2, &req, &str) == FAILURE) {
  96. WRONG_PARAM_COUNT;
  97. }
  98. convert_to_string_ex(str);
  99. convert_to_string_ex(req);
  100. request = recode_new_request(ReSG(outer));
  101. if (request == NULL) {
  102. php_error(E_WARNING, "Cannot allocate request structure");
  103. RETURN_FALSE;
  104. }
  105. success = recode_scan_request(request, (*req)->value.str.val);
  106. if (!success) {
  107. php_error(E_WARNING, "Illegal recode request '%s'", (*req)->value.str.val);
  108. goto error_exit;
  109. }
  110. recode_buffer_to_buffer(request, Z_STRVAL_PP(str), Z_STRLEN_PP(str), &r, &r_len, &r_alen);
  111. if (!r) {
  112. php_error(E_WARNING, "Recoding failed.");
  113. goto error_exit;
  114. }
  115. RETVAL_STRINGL(r, r_len, 1);
  116. free(r);
  117. /* FALLTHROUGH */
  118. error_exit:
  119. if (request)
  120. recode_delete_request(request);
  121. if (!r)
  122. RETURN_FALSE;
  123. return;
  124. }
  125. /* }}} */
  126. /* {{{ proto bool recode_file(string request, resource input, resource output)
  127. Recode file input into file output according to request */
  128. PHP_FUNCTION(recode_file)
  129. {
  130. RECODE_REQUEST request = NULL;
  131. int success;
  132. pval **req;
  133. pval **input, **output;
  134. FILE *in_fp, *out_fp;
  135. int in_type, out_type;
  136. if (ZEND_NUM_ARGS() != 3
  137. || zend_get_parameters_ex(3, &req, &input, &output) == FAILURE) {
  138. WRONG_PARAM_COUNT;
  139. }
  140. in_fp = zend_fetch_resource(input,-1, "File-Handle", &in_type,
  141. 2, php_file_le_fopen(), php_file_le_popen());
  142. if (!in_fp) {
  143. php_error(E_WARNING,"Unable to find input file identifier");
  144. RETURN_FALSE;
  145. }
  146. out_fp = zend_fetch_resource(output,-1, "File-Handle", &out_type,
  147. 2, php_file_le_fopen(), php_file_le_popen());
  148. if (!out_fp) {
  149. php_error(E_WARNING,"Unable to find output file identifier");
  150. RETURN_FALSE;
  151. }
  152. convert_to_string_ex(req);
  153. request = recode_new_request(ReSG(outer));
  154. if (request == NULL) {
  155. php_error(E_WARNING, "Cannot allocate request structure");
  156. RETURN_FALSE;
  157. }
  158. success = recode_scan_request(request, (*req)->value.str.val);
  159. if (!success) {
  160. php_error(E_WARNING, "Illegal recode request '%s'", (*req)->value.str.val);
  161. goto error_exit;
  162. }
  163. success = recode_file_to_file(request, in_fp, out_fp);
  164. if (!success) {
  165. php_error(E_WARNING, "Recoding failed.");
  166. goto error_exit;
  167. }
  168. if (request)
  169. recode_delete_request(request);
  170. RETURN_TRUE;
  171. error_exit:
  172. if (request)
  173. recode_delete_request(request);
  174. RETURN_FALSE;
  175. }
  176. /* }}} */
  177. #endif
  178. /*
  179. * Local variables:
  180. * tab-width: 4
  181. * c-basic-offset: 4
  182. * End:
  183. */