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.

242 lines
5.7 KiB

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