1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2014 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
7 ///////////////////////////////////////////////////////////////////////////////
15 #define CGI_INPUT_UNIQUE_ID "unique_id"
16 #define CGI_INPUT_DEFAULT_UNIQUE_ID 0
17 #define CGI_INPUT_LICENSEE_EMAIL "licensee_email"
18 #define CGI_INPUT_LICENSEE_EMAIL_MAX_LENGTH 40+1
20 #define MYSQL_HOST "mwlx122"
21 #define MYSQL_DB_NAME "ttcn3"
22 #define MYSQL_USERID "ttcn3"
23 #define MYSQL_PASSWD "ttcn3"
25 #define COMMAND_LINE_BUFFER_LENGTH 80
28 The content of CGI_INPUT_UNIQUE_ID should be checked before submitting
29 the content to this program. Missing CGI_INPUT_UNIQUE_ID defaults to 0.
31 Do not forget to set mySQL database and user parameters below!
33 Requires cgic.h and libcgic.a for compiling (http://boutell.com/cgic/).
35 Requires mysql.h and libmysql.a for compiling.
39 /* Send the content type, letting the browser know this is HTML */
40 cgiHeaderContentType("text/html");
42 fprintf(cgiOut
, "<HTML><HEAD>\n");
43 fprintf(cgiOut
, "<TITLE>renew_license</TITLE></HEAD>\n");
44 fprintf(cgiOut
, "<BODY><H1>renew_license</H1>\n");
47 if(cgiFormInteger(CGI_INPUT_UNIQUE_ID
, &unique_id
,
48 CGI_INPUT_DEFAULT_UNIQUE_ID
) != cgiFormSuccess
) {
50 "<P>ERROR: Missing or invalid input field <code>%s</code>.",
55 char licensee_email
[CGI_INPUT_LICENSEE_EMAIL_MAX_LENGTH
];
56 if(cgiFormStringNoNewlines(CGI_INPUT_LICENSEE_EMAIL
, licensee_email
,
57 CGI_INPUT_LICENSEE_EMAIL_MAX_LENGTH
)
60 "<P>ERROR: Missing or invalid input field <code>%s</code>.",
61 CGI_INPUT_LICENSEE_EMAIL
);
65 /* Connect to mySQL server */
66 MYSQL
*mysql
= mysql_init(NULL
);
69 "<P>ERROR: Could not initialize mySQL handle.");
72 if(!(mysql_real_connect(mysql
, MYSQL_HOST
, MYSQL_USERID
, MYSQL_PASSWD
,
73 MYSQL_DB_NAME
, 0, NULL
, CLIENT_ODBC
))) {
75 "<P>ERROR: Could not connect to database.");
81 "SELECT * FROM licenses WHERE unique_id=%d AND "
82 "licensee_email='%s' AND "
83 "valid_until < DATE_ADD(CURDATE(), INTERVAL 1 MONTH);\0",
84 unique_id
, licensee_email
);
85 if(mysql_query(mysql
, query
)) {
87 "<P>ERROR: Problem executing query.");
91 result
= mysql_store_result(mysql
);
93 /* mysql_num_rows() returns my_ulonglong==unsigned long */
94 my_ulonglong num_records
= mysql_num_rows(result
);
95 if(num_records
== 0) {
97 "<P>ERROR: Did not find license with "
98 "number <code>%d</code>, "
99 "e-mail <code>%s</code>, expiring within 1 month. "
100 "Re-check data validity and try again.",
105 MYSQL_ROW row
= mysql_fetch_row(result
);
106 fprintf(cgiOut
, "Debug info (remove it from final build):<br>");
107 fprintf(cgiOut
, "<p>unique_id: <code>%s</code>", row
[0]);
108 fprintf(cgiOut
, "<p>licensee_email: <code>%s</code>", row
[2]);
109 fprintf(cgiOut
, "<p>valid_until: <code>%s</code>", row
[7]);
110 fprintf(cgiOut
, "<p>notes: <code>%s</code>",
111 row
[33] ? row
[33] : "NULL");
112 /* Update mySQL database with new expiry date and add note */
113 snprintf(query
, 1024,
114 "UPDATE licenses SET "
115 "valid_until=DATE_ADD(NOW(), INTERVAL 1 YEAR), "
116 "notes=CONCAT(COALESCE(notes, ''), '\nProlonged on ', CURDATE(), '.') "
117 "WHERE unique_id=%d;", unique_id
);
119 "<P>Should execute the followin query now "
120 "(uncomment next line in source to do so): <BR>"
121 "<code>%s</code>\n", query
);
122 /* if(mysql_query(mysql, query)) {
123 fprintf(cgiOut, "<P>ERROR: Problem executing query.");
126 /* Generate and email license */
127 char command_line
[COMMAND_LINE_BUFFER_LENGTH
];
128 snprintf(command_line
, COMMAND_LINE_BUFFER_LENGTH
,
129 "license_gen -m %d", unique_id
);
130 if(!(system(command_line
))) {
132 "<P>Your new license key (%s) was generated and "
133 "e-mailed to your filed e-mail address (%s).",
137 fprintf(cgiOut
, "<P>ERROR: Could not launch subshell." );
139 mysql_free_result(result
);
142 "<P>ERROR: Problem fetching results from database.");
146 /* Terminate mysql session */
150 /* Finish up the page */
152 fprintf(cgiOut
, "</BODY></HTML>\n");