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.

240 lines
5.6 KiB

24 years ago
24 years ago
24 years ago
24 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 4 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2003 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. bool success;
  110. int r_len = 0, r_alen = 0;
  111. int req_len, str_len;
  112. char *req, *str;
  113. if (zend_parse_parameters(TSRMLS_CC ZEND_NUM_ARGS(), "ss", &req, &req_len, &str, &str_len) == FAILURE) {
  114. return;
  115. }
  116. request = recode_new_request(ReSG(outer));
  117. if (request == NULL) {
  118. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot allocate request structure");
  119. RETURN_FALSE;
  120. }
  121. success = recode_scan_request(request, req);
  122. if (!success) {
  123. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal recode request '%s'", req);
  124. goto error_exit;
  125. }
  126. recode_buffer_to_buffer(request, str, str_len, &r, &r_len, &r_alen);
  127. if (!r) {
  128. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Recoding failed.");
  129. goto error_exit;
  130. }
  131. RETVAL_STRINGL(r, r_len, 1);
  132. free(r);
  133. /* FALLTHROUGH */
  134. error_exit:
  135. if (request)
  136. recode_delete_request(request);
  137. if (!r)
  138. RETURN_FALSE;
  139. return;
  140. }
  141. /* }}} */
  142. /* {{{ proto bool recode_file(string request, resource input, resource output)
  143. Recode file input into file output according to request */
  144. PHP_FUNCTION(recode_file)
  145. {
  146. RECODE_REQUEST request = NULL;
  147. int success;
  148. zval **req;
  149. zval **input, **output;
  150. php_stream *instream, *outstream;
  151. FILE *in_fp, *out_fp;
  152. if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &req, &input, &output) == FAILURE) {
  153. WRONG_PARAM_COUNT;
  154. }
  155. php_stream_from_zval(instream, input);
  156. php_stream_from_zval(outstream, output);
  157. if (FAILURE == php_stream_cast(instream, PHP_STREAM_AS_STDIO, (void**)&in_fp, REPORT_ERRORS)) {
  158. RETURN_FALSE;
  159. }
  160. if (FAILURE == php_stream_cast(outstream, PHP_STREAM_AS_STDIO, (void**)&out_fp, REPORT_ERRORS)) {
  161. RETURN_FALSE;
  162. }
  163. convert_to_string_ex(req);
  164. request = recode_new_request(ReSG(outer));
  165. if (request == NULL) {
  166. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot allocate request structure");
  167. RETURN_FALSE;
  168. }
  169. success = recode_scan_request(request, Z_STRVAL_PP(req));
  170. if (!success) {
  171. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal recode request '%s'", Z_STRVAL_PP(req));
  172. goto error_exit;
  173. }
  174. success = recode_file_to_file(request, in_fp, out_fp);
  175. if (!success) {
  176. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Recoding failed.");
  177. goto error_exit;
  178. }
  179. if (request)
  180. recode_delete_request(request);
  181. RETURN_TRUE;
  182. error_exit:
  183. if (request)
  184. recode_delete_request(request);
  185. RETURN_FALSE;
  186. }
  187. /* }}} */
  188. #endif
  189. /*
  190. * Local variables:
  191. * tab-width: 4
  192. * c-basic-offset: 4
  193. * End:
  194. */