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.

244 lines
5.8 KiB

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