1 #define OV_DEBUG
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 #include <OpenVanilla/OpenVanilla.h>
8 #include <OpenVanilla/OVLibrary.h>
9 #include <OpenVanilla/OVUtility.h>
10 
11 #define PHP_PATH "/usr/bin/php"
12 #define OUT_PATH "/tmp/ovphp-out.tmp"
13 #define IN_PATH "/tmp/ovphp-in.tmp"
14 
convert(const char * string,const char * command,char * buf)15 const char *convert(const char *string, const char *command, char *buf){
16 	struct stat st;
17 	char buffer[1024];
18 	stat(PHP_PATH, &st);
19 	if(!st.st_size)
20 		return "";
21 
22 	FILE *fp=fopen(OUT_PATH, "w");
23 	if (!fp) return "";
24 	fprintf(fp, "<?php \n error_reporting(0);\n");
25 	fprintf(fp, "echo %s(\"%s\");", command, string);
26 	fprintf(fp, "\n?>");
27 	fclose(fp);
28 
29 	sprintf(buffer, "%s %s > %s", PHP_PATH, OUT_PATH, IN_PATH);
30 	system(buffer);
31 
32 	strcpy(buffer, "");
33 
34 	fp=fopen(IN_PATH, "r");
35 	if (!fp) return 0;
36 	while (!feof(fp))
37 	{
38 		fgets(buffer, sizeof(buffer), fp);
39 		strcat(buf, buffer);
40 	}
41 	fclose(fp);
42 	unlink(OUT_PATH);
43 	return (const char*)buf;
44 }
45 
46 
47 class OVOFmd5 : public OVOutputFilter {
48 public:
OVOFmd5()49     OVOFmd5() {
50     }
51 
initialize(OVDictionary * moduleCfg,OVService * srv,const char * modulePath)52     virtual int initialize(OVDictionary *moduleCfg, OVService *srv,
53         const char *modulePath) {
54         return 1;
55     }
56 
identifier()57     virtual const char *identifier() { return "OVOFmd5"; }
localizedName(const char * locale)58     virtual const char *localizedName(const char *locale)
59     {return "PHP md5";}
process(const char * src,OVService * srv)60     virtual const char *process (const char *src, OVService *srv) {
61         if (buf) free(buf);
62         buf=(char*)calloc(1, strlen(src)+1);
63 	strcpy(buf, convert(src, "md5", buf));
64         return buf;
65     }
66 
67 protected:
68     char *buf;
69 };
70 
71 class OVOFurlencode : public OVOutputFilter {
72 public:
OVOFurlencode()73     OVOFurlencode() {
74     }
75 
initialize(OVDictionary * moduleCfg,OVService * srv,const char * modulePath)76     virtual int initialize(OVDictionary *moduleCfg, OVService *srv,
77 						   const char *modulePath) {
78         return 1;
79     }
80 
identifier()81     virtual const char *identifier() { return "OVOFurlencode"; }
localizedName(const char * locale)82     virtual const char *localizedName(const char *locale)
83     {return "PHP urlencode";}
process(const char * src,OVService * srv)84     virtual const char *process (const char *src, OVService *srv) {
85         if (buf) free(buf);
86         buf=(char*)calloc(1, strlen(src)+1);
87 	strcpy(buf, convert(src, "urlencode", buf));
88 	return buf;
89     }
90 
91 protected:
92     char *buf;
93 };
94 
95 class OVOFhtmlentities : public OVOutputFilter {
96 public:
OVOFhtmlentities()97     OVOFhtmlentities() {
98     }
99 
initialize(OVDictionary * moduleCfg,OVService * srv,const char * modulePath)100     virtual int initialize(OVDictionary *moduleCfg, OVService *srv,
101 						   const char *modulePath) {
102         return 1;
103     }
104 
identifier()105     virtual const char *identifier() { return "OVOFhtmlentities"; }
localizedName(const char * locale)106     virtual const char *localizedName(const char *locale)
107     {return "PHP htmlentities";}
process(const char * src,OVService * srv)108     virtual const char *process (const char *src, OVService *srv) {
109         if (buf) free(buf);
110         buf=(char*)calloc(1, strlen(src)+1);
111 	strcpy(buf, convert(src, "htmlentities", buf));
112 	return buf;
113     }
114 
115 protected:
116     char *buf;
117 };
118 
119 // OV_SINGLE_MODULE_WRAPPER(OVOFBasicFilter);
120 
OVGetLibraryVersion()121 extern "C" unsigned int OVGetLibraryVersion() {
122     return OV_VERSION;
123 }
124 
OVInitializeLibrary(OVService * s,const char * p)125 extern "C" int OVInitializeLibrary(OVService* s, const char* p) {
126     return 1;
127 }
128 
OVGetModuleFromLibrary(int idx)129 extern "C" OVModule *OVGetModuleFromLibrary(int idx) {
130 	switch (idx) {
131 		case 0: return new OVOFmd5;
132 		case 1: return new OVOFurlencode;
133 		case 2: return new OVOFhtmlentities;
134 	}
135 	return NULL;
136 }
137 
138