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.

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