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

24 years ago
24 years ago
22 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2006 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 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_01.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. static PHP_GINIT_FUNCTION(recode);
  55. /* {{{ module stuff */
  56. static zend_function_entry php_recode_functions[] = {
  57. PHP_FE(recode_string, NULL)
  58. PHP_FE(recode_file, NULL)
  59. PHP_FALIAS(recode, recode_string, NULL)
  60. {NULL, NULL, NULL}
  61. };
  62. zend_module_entry recode_module_entry = {
  63. STANDARD_MODULE_HEADER,
  64. "recode",
  65. php_recode_functions,
  66. PHP_MINIT(recode),
  67. PHP_MSHUTDOWN(recode),
  68. NULL,
  69. NULL,
  70. PHP_MINFO(recode),
  71. NO_VERSION_YET,
  72. PHP_MODULE_GLOBALS(recode),
  73. PHP_GINIT(recode),
  74. NULL,
  75. NULL,
  76. STANDARD_MODULE_PROPERTIES_EX
  77. };
  78. #ifdef COMPILE_DL_RECODE
  79. ZEND_GET_MODULE(recode)
  80. #endif
  81. static PHP_GINIT_FUNCTION(recode)
  82. {
  83. recode_globals->outer = NULL;
  84. }
  85. PHP_MINIT_FUNCTION(recode)
  86. {
  87. ReSG(outer) = recode_new_outer(false);
  88. if (ReSG(outer) == NULL) {
  89. return FAILURE;
  90. }
  91. return SUCCESS;
  92. }
  93. PHP_MSHUTDOWN_FUNCTION(recode)
  94. {
  95. if (ReSG(outer)) {
  96. recode_delete_outer(ReSG(outer));
  97. }
  98. return SUCCESS;
  99. }
  100. PHP_MINFO_FUNCTION(recode)
  101. {
  102. php_info_print_table_start();
  103. php_info_print_table_row(2, "Recode Support", "enabled");
  104. php_info_print_table_row(2, "Revision", "$Revision$");
  105. php_info_print_table_end();
  106. }
  107. /* {{{ proto string recode_string(string request, string str)
  108. Recode string str according to request string */
  109. PHP_FUNCTION(recode_string)
  110. {
  111. RECODE_REQUEST request = NULL;
  112. char *r = NULL;
  113. int r_len = 0, r_alen = 0;
  114. int req_len, str_len;
  115. char *req, *str;
  116. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &req, &req_len, &str, &str_len) == FAILURE) {
  117. return;
  118. }
  119. request = recode_new_request(ReSG(outer));
  120. if (request == NULL) {
  121. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot allocate request structure");
  122. RETURN_FALSE;
  123. }
  124. if (!recode_scan_request(request, req)) {
  125. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal recode request '%s'", req);
  126. goto error_exit;
  127. }
  128. recode_buffer_to_buffer(request, str, str_len, &r, &r_len, &r_alen);
  129. if (!r) {
  130. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Recoding failed.");
  131. error_exit:
  132. RETVAL_FALSE;
  133. } else {
  134. RETVAL_STRINGL(r, r_len, 1);
  135. free(r);
  136. }
  137. recode_delete_request(request);
  138. return;
  139. }
  140. /* }}} */
  141. /* {{{ proto bool recode_file(string request, resource input, resource output)
  142. Recode file input into file output according to request */
  143. PHP_FUNCTION(recode_file)
  144. {
  145. RECODE_REQUEST request = NULL;
  146. zval **req;
  147. zval **input, **output;
  148. php_stream *instream, *outstream;
  149. FILE *in_fp, *out_fp;
  150. if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &req, &input, &output) == FAILURE) {
  151. WRONG_PARAM_COUNT;
  152. }
  153. php_stream_from_zval(instream, input);
  154. php_stream_from_zval(outstream, output);
  155. if (FAILURE == php_stream_cast(instream, PHP_STREAM_AS_STDIO, (void**)&in_fp, REPORT_ERRORS)) {
  156. RETURN_FALSE;
  157. }
  158. if (FAILURE == php_stream_cast(outstream, PHP_STREAM_AS_STDIO, (void**)&out_fp, REPORT_ERRORS)) {
  159. RETURN_FALSE;
  160. }
  161. convert_to_string_ex(req);
  162. request = recode_new_request(ReSG(outer));
  163. if (request == NULL) {
  164. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot allocate request structure");
  165. RETURN_FALSE;
  166. }
  167. if (!recode_scan_request(request, Z_STRVAL_PP(req))) {
  168. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal recode request '%s'", Z_STRVAL_PP(req));
  169. goto error_exit;
  170. }
  171. if (!recode_file_to_file(request, in_fp, out_fp)) {
  172. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Recoding failed.");
  173. goto error_exit;
  174. }
  175. recode_delete_request(request);
  176. RETURN_TRUE;
  177. error_exit:
  178. recode_delete_request(request);
  179. RETURN_FALSE;
  180. }
  181. /* }}} */
  182. #endif
  183. /*
  184. * Local variables:
  185. * tab-width: 4
  186. * c-basic-offset: 4
  187. * End:
  188. */