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.

237 lines
5.4 KiB

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