| 1 |
aga |
1.1 |
/* |
| 2 |
|
|
VPAL: Visiome Platform Abstract Layer |
| 3 |
|
|
|
| 4 |
|
|
zend_parse_parametersはphp4.1.0が必要 |
| 5 |
|
|
.soファイルは外から見えない場所に置くべき。 |
| 6 |
|
|
C++では、extern "C"{}, BEGIN/END_EXTERN_C()が必要。 |
| 7 |
|
|
*/ |
| 8 |
|
|
|
| 9 |
|
|
|
| 10 |
|
|
#include <stdio.h> |
| 11 |
|
|
#include "vpal.h" |
| 12 |
|
|
|
| 13 |
|
|
// global variables |
| 14 |
|
|
MYSQL *mysql = NULL; |
| 15 |
|
|
|
| 16 |
|
|
|
| 17 |
|
|
/* declaration of functions to be exported */ |
| 18 |
|
|
extern "C" { |
| 19 |
|
|
ZEND_FUNCTION(first_module); |
| 20 |
|
|
ZEND_FUNCTION(vp_initialize_db); |
| 21 |
|
|
ZEND_FUNCTION(vp_login_user); |
| 22 |
|
|
ZEND_FUNCTION(vp_logout_user); |
| 23 |
|
|
}; |
| 24 |
|
|
/* compiled function list so Zend knows what's in this module */ |
| 25 |
|
|
zend_function_entry vpalmod_functions[] = |
| 26 |
|
|
{ |
| 27 |
|
|
ZEND_FE(first_module, NULL) |
| 28 |
|
|
ZEND_FE(vp_initialize_db, NULL) |
| 29 |
|
|
ZEND_FE(vp_login_user, NULL) |
| 30 |
|
|
ZEND_FE(vp_logout_user, NULL) |
| 31 |
|
|
{NULL, NULL, NULL} |
| 32 |
|
|
}; |
| 33 |
|
|
|
| 34 |
|
|
/* compiled module information */ |
| 35 |
|
|
zend_module_entry vpalmod_module_entry = |
| 36 |
|
|
{ |
| 37 |
|
|
STANDARD_MODULE_HEADER, |
| 38 |
|
|
"Visiome Platform Abstract Layer", |
| 39 |
|
|
vpalmod_functions, |
| 40 |
|
|
NULL, |
| 41 |
|
|
NULL, |
| 42 |
|
|
NULL, |
| 43 |
|
|
NULL, |
| 44 |
|
|
NULL, |
| 45 |
|
|
NO_VERSION_YET, |
| 46 |
|
|
STANDARD_MODULE_PROPERTIES |
| 47 |
|
|
}; |
| 48 |
|
|
|
| 49 |
|
|
/* implement standard "stub" routine to introduce ourselves to Zend */ |
| 50 |
|
|
#if COMPILE_DL_FIRST_MODULE |
| 51 |
|
|
BEGIN_EXTERN_C() |
| 52 |
|
|
ZEND_GET_MODULE(vpalmod) |
| 53 |
|
|
END_EXTERN_C() |
| 54 |
|
|
#endif |
| 55 |
|
|
|
| 56 |
|
|
/* implement function that is meant to be made available to PHP */ |
| 57 |
|
|
ZEND_FUNCTION(first_module) |
| 58 |
|
|
{ |
| 59 |
|
|
long parameter; |
| 60 |
|
|
// if(ZEND_NUM_ARGS() != 2) WRONG_PARAM_COUNT; |
| 61 |
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", ¶meter) == FAILURE) { |
| 62 |
|
|
return; |
| 63 |
|
|
} |
| 64 |
|
|
parameter++; |
| 65 |
|
|
RETURN_LONG(parameter); |
| 66 |
|
|
} |
| 67 |
|
|
/** zvalからCの文字列を得る。 |
| 68 |
|
|
*/ |
| 69 |
|
|
char *getZvalString( zval **p ){ |
| 70 |
|
|
convert_to_string_ex(p); |
| 71 |
|
|
return Z_STRVAL_PP(p); //(**p).value.str.val; |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
|
/** zvalからintを得る。 |
| 75 |
|
|
*/ |
| 76 |
|
|
int getZvalInt( zval **p ){ |
| 77 |
|
|
convert_to_long_ex(p); |
| 78 |
|
|
return Z_LVAL_PP(p); // (int)(**p).value.lval; |
| 79 |
|
|
} |
| 80 |
|
|
|
| 81 |
|
|
/** DBに接続する。既に接続中の接続は閉じられる。 |
| 82 |
|
|
@param host 接続先。省略時はlocalhost |
| 83 |
|
|
@param user DB接続時のユーザ名。省略時はNULL |
| 84 |
|
|
@param password DB接続時のパスワード。省略時はNULL |
| 85 |
|
|
@param dbname DB接続時のDB名。省略時はNULL |
| 86 |
|
|
@param port 接続先のポート。省略時は3336 |
| 87 |
|
|
@return 0 success <br> |
| 88 |
|
|
@return 1 cannot mysql_init <br> |
| 89 |
|
|
@return 2 cannot mysql_real_connect <br> |
| 90 |
|
|
int initialize_db ( char* host="localhost", char* user=NULL, char* password=NULL, char* dbname=NULL, int port=3336 ) |
| 91 |
|
|
*/ |
| 92 |
|
|
ZEND_FUNCTION(vp_initialize_db) |
| 93 |
|
|
{ |
| 94 |
|
|
zval **parameters[5]; |
| 95 |
|
|
char *host; |
| 96 |
|
|
char *user; |
| 97 |
|
|
char *passwd; |
| 98 |
|
|
char *db; |
| 99 |
|
|
int port; |
| 100 |
|
|
|
| 101 |
|
|
/* get the number of arguments */ |
| 102 |
|
|
int argNum = ZEND_NUM_ARGS(); |
| 103 |
|
|
if (argNum > 5) |
| 104 |
|
|
WRONG_PARAM_COUNT; |
| 105 |
|
|
|
| 106 |
|
|
/* argument count is correct, now retrieve arguments */ |
| 107 |
|
|
if(zend_get_parameters_array_ex(argNum, parameters) != SUCCESS) |
| 108 |
|
|
WRONG_PARAM_COUNT; |
| 109 |
|
|
|
| 110 |
|
|
if (argNum < 1) host = "localhost"; |
| 111 |
|
|
else host = getZvalString( parameters[0] ); |
| 112 |
|
|
|
| 113 |
|
|
if (argNum < 2) user = NULL; |
| 114 |
|
|
else user = getZvalString( parameters[1] ); |
| 115 |
|
|
|
| 116 |
|
|
if (argNum < 3) passwd = NULL; |
| 117 |
|
|
else passwd = getZvalString( parameters[2] ); |
| 118 |
|
|
|
| 119 |
|
|
if (argNum < 4) db = NULL; |
| 120 |
|
|
else db = getZvalString( parameters[3] ); |
| 121 |
|
|
|
| 122 |
|
|
if (argNum < 5) port = 3336; |
| 123 |
|
|
else port = getZvalInt( parameters[4] ); |
| 124 |
|
|
|
| 125 |
|
|
// 既に接続中なら、一旦切断 |
| 126 |
|
|
if ( mysql != NULL ) |
| 127 |
|
|
mysql_close( mysql ); |
| 128 |
|
|
|
| 129 |
|
|
// 初期化と接続 |
| 130 |
|
|
mysql = mysql_init(NULL); |
| 131 |
|
|
if ( mysql == NULL ){ |
| 132 |
|
|
RETURN_LONG(1); |
| 133 |
|
|
} |
| 134 |
|
|
char *unix_socket = NULL; // ? |
| 135 |
|
|
uint flag = 0; // ? |
| 136 |
|
|
if ( NULL == mysql_real_connect( mysql, host, user, passwd, db, port, unix_socket, flag ) ){ |
| 137 |
|
|
RETURN_LONG(2); |
| 138 |
|
|
} |
| 139 |
|
|
|
| 140 |
|
|
RETURN_LONG(0); |
| 141 |
|
|
} |
| 142 |
|
|
|
| 143 |
|
|
// session_t* login_user(char* uname, char* passwd) |
| 144 |
|
|
ZEND_FUNCTION(vp_login_user) |
| 145 |
|
|
{ |
| 146 |
|
|
char *uname; |
| 147 |
|
|
int unameLen; |
| 148 |
|
|
char *passwd; |
| 149 |
|
|
int passwdLen; |
| 150 |
|
|
|
| 151 |
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", |
| 152 |
|
|
&uname, &unameLen, &passwd, &passwdLen) == FAILURE) { |
| 153 |
|
|
return; |
| 154 |
|
|
} |
| 155 |
|
|
|
| 156 |
|
|
//ここでログイン |
| 157 |
|
|
|
| 158 |
|
|
|
| 159 |
|
|
RETURN_LONG(0); |
| 160 |
|
|
} |
| 161 |
|
|
|
| 162 |
|
|
// void logout_user( session_t* sid ) |
| 163 |
|
|
ZEND_FUNCTION(vp_logout_user) |
| 164 |
|
|
{ |
| 165 |
|
|
long sid; |
| 166 |
|
|
|
| 167 |
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &sid) == FAILURE) { |
| 168 |
|
|
return; |
| 169 |
|
|
} |
| 170 |
|
|
|
| 171 |
|
|
// ここでログアウト |
| 172 |
|
|
|
| 173 |
|
|
RETURN_NULL(); |
| 174 |
|
|
} |
| 175 |
|
|
|
| 176 |
|
|
|
| 177 |
|
|
|
| 178 |
|
|
|
| 179 |
|
|
|
| 180 |
|
|
|
| 181 |
|
|
|
| 182 |
|
|
|
| 183 |
|
|
|
| 184 |
|
|
|
| 185 |
|
|
|
| 186 |
|
|
/* |
| 187 |
|
|
MYSQL *g_mysql_a = NULL; // test db |
| 188 |
|
|
ZEND_FUNCTION(mysql_test1) |
| 189 |
|
|
{ |
| 190 |
|
|
char *host = NULL; // localhost |
| 191 |
|
|
char *user = NULL; // default |
| 192 |
|
|
char *passwd = NULL; // default |
| 193 |
|
|
char *db = "test"; |
| 194 |
|
|
uint port = 0; // default |
| 195 |
|
|
char *unix_socket = NULL; // ? |
| 196 |
|
|
uint flag = 0; // ? |
| 197 |
|
|
|
| 198 |
|
|
|
| 199 |
|
|
if ( g_mysql_a != NULL ) |
| 200 |
|
|
mysql_close( g_mysql_a ); |
| 201 |
|
|
g_mysql_a = mysql_init(NULL); |
| 202 |
|
|
if ( g_mysql_a == NULL ){ |
| 203 |
|
|
zend_error(E_WARNING, "mysql_test1: cannot mysql_init()"); |
| 204 |
|
|
RETURN_NULL(); |
| 205 |
|
|
} |
| 206 |
|
|
|
| 207 |
|
|
if (NULL == mysql_real_connect(g_mysql_a, host, user, passwd, db, port, unix_socket, flag)){ |
| 208 |
|
|
zend_error(E_WARNING, "mysql_test1: cannot connect to mysql"); |
| 209 |
|
|
RETURN_NULL(); |
| 210 |
|
|
} |
| 211 |
|
|
|
| 212 |
|
|
int result = mysql_query(g_mysql_a, "select * from test1"); |
| 213 |
|
|
unsigned int err = mysql_errno(g_mysql_a); |
| 214 |
|
|
if (result || err){ |
| 215 |
|
|
zend_error(E_WARNING, "mysql_test1: mysql_query() failed."); |
| 216 |
|
|
RETURN_NULL(); |
| 217 |
|
|
} |
| 218 |
|
|
|
| 219 |
|
|
MYSQL_RES *res = mysql_store_result(g_mysql_a); |
| 220 |
|
|
int num_fields = mysql_num_fields(res); |
| 221 |
|
|
int rows = (int)mysql_num_rows(res); |
| 222 |
|
|
|
| 223 |
|
|
zval *ar1; |
| 224 |
|
|
MAKE_STD_ZVAL(ar1); |
| 225 |
|
|
array_init(ar1); |
| 226 |
|
|
|
| 227 |
|
|
for ( int i = 0; i < rows; i++ ){ |
| 228 |
|
|
MYSQL_ROW row = mysql_fetch_row(res); |
| 229 |
|
|
unsigned long *lengths = mysql_fetch_lengths(res); |
| 230 |
|
|
|
| 231 |
|
|
zval *ar2; |
| 232 |
|
|
MAKE_STD_ZVAL(ar2); |
| 233 |
|
|
array_init(ar2); |
| 234 |
|
|
|
| 235 |
|
|
for ( int j = 0; j < num_fields; j++ ){ |
| 236 |
|
|
add_index_stringl(ar2, (uint)j, row[j], lengths[j], 1); |
| 237 |
|
|
} |
| 238 |
|
|
add_index_zval(ar1, (uint)i, ar2); |
| 239 |
|
|
} |
| 240 |
|
|
|
| 241 |
|
|
mysql_free_result(res); |
| 242 |
|
|
*return_value = *ar1; |
| 243 |
|
|
zval_copy_ctor(return_value); |
| 244 |
|
|
|
| 245 |
|
|
} |
| 246 |
|
|
*/ |
| 247 |
|
|
|