1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
12 ******************************************************************************/
20 #define CGI_INPUT_UNIQUE_ID "unique_id"
21 #define CGI_INPUT_DEFAULT_UNIQUE_ID 0
22 #define CGI_INPUT_LICENSEE_EMAIL "licensee_email"
23 #define CGI_INPUT_LICENSEE_EMAIL_MAX_LENGTH 40+1
25 #define MYSQL_HOST "mwlx122"
26 #define MYSQL_DB_NAME "ttcn3"
27 #define MYSQL_USERID "ttcn3"
28 #define MYSQL_PASSWD "ttcn3"
30 #define COMMAND_LINE_BUFFER_LENGTH 80
33 The content of CGI_INPUT_UNIQUE_ID should be checked before submitting
34 the content to this program. Missing CGI_INPUT_UNIQUE_ID defaults to 0.
36 Do not forget to set mySQL database and user parameters below!
38 Requires cgic.h and libcgic.a for compiling (http://boutell.com/cgic/).
40 Requires mysql.h and libmysql.a for compiling.
44 /* Send the content type, letting the browser know this is HTML */
45 cgiHeaderContentType("text/html");
47 fprintf(cgiOut
, "<HTML><HEAD>\n");
48 fprintf(cgiOut
, "<TITLE>renew_license</TITLE></HEAD>\n");
49 fprintf(cgiOut
, "<BODY><H1>renew_license</H1>\n");
52 if(cgiFormInteger(CGI_INPUT_UNIQUE_ID
, &unique_id
,
53 CGI_INPUT_DEFAULT_UNIQUE_ID
) != cgiFormSuccess
) {
55 "<P>ERROR: Missing or invalid input field <code>%s</code>.",
60 char licensee_email
[CGI_INPUT_LICENSEE_EMAIL_MAX_LENGTH
];
61 if(cgiFormStringNoNewlines(CGI_INPUT_LICENSEE_EMAIL
, licensee_email
,
62 CGI_INPUT_LICENSEE_EMAIL_MAX_LENGTH
)
65 "<P>ERROR: Missing or invalid input field <code>%s</code>.",
66 CGI_INPUT_LICENSEE_EMAIL
);
70 /* Connect to mySQL server */
71 MYSQL
*mysql
= mysql_init(NULL
);
74 "<P>ERROR: Could not initialize mySQL handle.");
77 if(!(mysql_real_connect(mysql
, MYSQL_HOST
, MYSQL_USERID
, MYSQL_PASSWD
,
78 MYSQL_DB_NAME
, 0, NULL
, CLIENT_ODBC
))) {
80 "<P>ERROR: Could not connect to database.");
86 "SELECT * FROM licenses WHERE unique_id=%d AND "
87 "licensee_email='%s' AND "
88 "valid_until < DATE_ADD(CURDATE(), INTERVAL 1 MONTH);\0",
89 unique_id
, licensee_email
);
90 if(mysql_query(mysql
, query
)) {
92 "<P>ERROR: Problem executing query.");
96 result
= mysql_store_result(mysql
);
98 /* mysql_num_rows() returns my_ulonglong==unsigned long */
99 my_ulonglong num_records
= mysql_num_rows(result
);
100 if(num_records
== 0) {
102 "<P>ERROR: Did not find license with "
103 "number <code>%d</code>, "
104 "e-mail <code>%s</code>, expiring within 1 month. "
105 "Re-check data validity and try again.",
110 MYSQL_ROW row
= mysql_fetch_row(result
);
111 fprintf(cgiOut
, "Debug info (remove it from final build):<br>");
112 fprintf(cgiOut
, "<p>unique_id: <code>%s</code>", row
[0]);
113 fprintf(cgiOut
, "<p>licensee_email: <code>%s</code>", row
[2]);
114 fprintf(cgiOut
, "<p>valid_until: <code>%s</code>", row
[7]);
115 fprintf(cgiOut
, "<p>notes: <code>%s</code>",
116 row
[33] ? row
[33] : "NULL");
117 /* Update mySQL database with new expiry date and add note */
118 snprintf(query
, 1024,
119 "UPDATE licenses SET "
120 "valid_until=DATE_ADD(NOW(), INTERVAL 1 YEAR), "
121 "notes=CONCAT(COALESCE(notes, ''), '\nProlonged on ', CURDATE(), '.') "
122 "WHERE unique_id=%d;", unique_id
);
124 "<P>Should execute the followin query now "
125 "(uncomment next line in source to do so): <BR>"
126 "<code>%s</code>\n", query
);
127 /* if(mysql_query(mysql, query)) {
128 fprintf(cgiOut, "<P>ERROR: Problem executing query.");
131 /* Generate and email license */
132 char command_line
[COMMAND_LINE_BUFFER_LENGTH
];
133 snprintf(command_line
, COMMAND_LINE_BUFFER_LENGTH
,
134 "license_gen -m %d", unique_id
);
135 if(!(system(command_line
))) {
137 "<P>Your new license key (%s) was generated and "
138 "e-mailed to your filed e-mail address (%s).",
142 fprintf(cgiOut
, "<P>ERROR: Could not launch subshell." );
144 mysql_free_result(result
);
147 "<P>ERROR: Problem fetching results from database.");
151 /* Terminate mysql session */
155 /* Finish up the page */
157 fprintf(cgiOut
, "</BODY></HTML>\n");