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.4 KiB

21 years ago
24 years ago
24 years ago
22 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2005 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.0 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_0.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_streams.h"
  25. #if HAVE_LIBRECODE
  26. /* For recode 3.5 */
  27. #if HAVE_BROKEN_RECODE
  28. extern char *program_name;
  29. char *program_name = "php";
  30. #endif
  31. #ifdef HAVE_STDBOOL_H
  32. # include <stdbool.h>
  33. #else
  34. typedef enum {false = 0, true = 1} bool;
  35. #endif
  36. #include <stdio.h>
  37. #include <sys/types.h>
  38. #include <unistd.h>
  39. #include <recode.h>
  40. #include "php_recode.h"
  41. #include "ext/standard/info.h"
  42. #include "ext/standard/file.h"
  43. #include "ext/standard/php_string.h"
  44. /* }}} */
  45. ZEND_BEGIN_MODULE_GLOBALS(recode)
  46. RECODE_OUTER outer;
  47. ZEND_END_MODULE_GLOBALS(recode)
  48. #ifdef ZTS
  49. # define ReSG(v) TSRMG(recode_globals_id, zend_recode_globals *, v)
  50. #else
  51. # define ReSG(v) (recode_globals.v)
  52. #endif
  53. ZEND_DECLARE_MODULE_GLOBALS(recode);
  54. /* {{{ module stuff */
  55. static zend_function_entry php_recode_functions[] = {
  56. PHP_FE(recode_string, NULL)
  57. PHP_FE(recode_file, NULL)
  58. PHP_FALIAS(recode, recode_string, NULL)
  59. {NULL, NULL, NULL}
  60. };
  61. zend_module_entry recode_module_entry = {
  62. STANDARD_MODULE_HEADER,
  63. "recode",
  64. php_recode_functions,
  65. PHP_MINIT(recode),
  66. PHP_MSHUTDOWN(recode),
  67. NULL,
  68. NULL,
  69. PHP_MINFO(recode),
  70. NO_VERSION_YET,
  71. STANDARD_MODULE_PROPERTIES
  72. };
  73. #ifdef COMPILE_DL_RECODE
  74. ZEND_GET_MODULE(recode)
  75. #endif
  76. static void php_recode_init_globals (zend_recode_globals *rg)
  77. {
  78. rg->outer = NULL;
  79. }
  80. PHP_MINIT_FUNCTION(recode)
  81. {
  82. ZEND_INIT_MODULE_GLOBALS(recode, php_recode_init_globals, NULL);
  83. ReSG(outer) = recode_new_outer(false);
  84. if (ReSG(outer) == NULL) {
  85. return FAILURE;
  86. }
  87. return SUCCESS;
  88. }
  89. PHP_MSHUTDOWN_FUNCTION(recode)
  90. {
  91. if (ReSG(outer)) {
  92. recode_delete_outer(ReSG(outer));
  93. }
  94. return SUCCESS;
  95. }
  96. PHP_MINFO_FUNCTION(recode)
  97. {
  98. php_info_print_table_start();
  99. php_info_print_table_row(2, "Recode Support", "enabled");
  100. php_info_print_table_row(2, "Revision", "$Revision$");
  101. php_info_print_table_end();
  102. }
  103. /* {{{ proto string recode_string(string request, string str)
  104. Recode string str according to request string */
  105. PHP_FUNCTION(recode_string)
  106. {
  107. RECODE_REQUEST request = NULL;
  108. char *r = NULL;
  109. int r_len = 0, r_alen = 0;
  110. int req_len, str_len;
  111. char *req, *str;
  112. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &req, &req_len, &str, &str_len) == FAILURE) {
  113. return;
  114. }
  115. request = recode_new_request(ReSG(outer));
  116. if (request == NULL) {
  117. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot allocate request structure");
  118. RETURN_FALSE;
  119. }
  120. if (!recode_scan_request(request, req)) {
  121. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal recode request '%s'", req);
  122. goto error_exit;
  123. }
  124. recode_buffer_to_buffer(request, str, str_len, &r, &r_len, &r_alen);
  125. if (!r) {
  126. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Recoding failed.");
  127. error_exit:
  128. RETVAL_FALSE;
  129. } else {
  130. RETVAL_STRINGL(r, r_len, 1);
  131. free(r);
  132. }
  133. recode_delete_request(request);
  134. return;
  135. }
  136. /* }}} */
  137. /* {{{ proto bool recode_file(string request, resource input, resource output)
  138. Recode file input into file output according to request */
  139. PHP_FUNCTION(recode_file)
  140. {
  141. RECODE_REQUEST request = NULL;
  142. zval **req;
  143. zval **input, **output;
  144. php_stream *instream, *outstream;
  145. FILE *in_fp, *out_fp;
  146. if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &req, &input, &output) == FAILURE) {
  147. WRONG_PARAM_COUNT;
  148. }
  149. php_stream_from_zval(instream, input);
  150. php_stream_from_zval(outstream, output);
  151. if (FAILURE == php_stream_cast(instream, PHP_STREAM_AS_STDIO, (void**)&in_fp, REPORT_ERRORS)) {
  152. RETURN_FALSE;
  153. }
  154. if (FAILURE == php_stream_cast(outstream, PHP_STREAM_AS_STDIO, (void**)&out_fp, REPORT_ERRORS)) {
  155. RETURN_FALSE;
  156. }
  157. convert_to_string_ex(req);
  158. request = recode_new_request(ReSG(outer));
  159. if (request == NULL) {
  160. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot allocate request structure");
  161. RETURN_FALSE;
  162. }
  163. if (!recode_scan_request(request, Z_STRVAL_PP(req))) {
  164. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal recode request '%s'", Z_STRVAL_PP(req));
  165. goto error_exit;
  166. }
  167. if (!recode_file_to_file(request, in_fp, out_fp)) {
  168. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Recoding failed.");
  169. goto error_exit;
  170. }
  171. recode_delete_request(request);
  172. RETURN_TRUE;
  173. error_exit:
  174. recode_delete_request(request);
  175. RETURN_FALSE;
  176. }
  177. /* }}} */
  178. #endif
  179. /*
  180. * Local variables:
  181. * tab-width: 4
  182. * c-basic-offset: 4
  183. * End:
  184. */