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.

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