|
| 1 | +#include "php_git2.h" |
| 2 | +#include "git2_exception.h" |
| 3 | +#include "git2_cred.h" |
| 4 | +#include "git2_php_util.h" |
| 5 | + |
| 6 | +static zend_class_entry *php_git2_cred_ce; |
| 7 | +static zend_object_handlers php_git2_cred_handler; |
| 8 | + |
| 9 | +typedef struct _git2_cred_object { |
| 10 | + zend_object std; |
| 11 | + git_cred *cred; |
| 12 | +} git2_cred_object_t; |
| 13 | + |
| 14 | +ZEND_BEGIN_ARG_INFO_EX(arginfo_cred_create_plaintext, 0, 0, 2) |
| 15 | + ZEND_ARG_INFO(0, username) |
| 16 | + ZEND_ARG_INFO(0, password) |
| 17 | +ZEND_END_ARG_INFO() |
| 18 | + |
| 19 | +static PHP_METHOD(Cred, create_plaintext) { |
| 20 | + char *username, *password; |
| 21 | + size_t username_len, password_len; |
| 22 | + git2_cred_object_t *intern; |
| 23 | + |
| 24 | + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &username, &username_len, &password, &password_len) == FAILURE) |
| 25 | + return; |
| 26 | + |
| 27 | + object_init_ex(return_value, php_git2_cred_ce); |
| 28 | + intern = (git2_cred_object_t*)Z_OBJ_P(return_value); |
| 29 | + int res = git_cred_userpass_plaintext_new(&intern->cred, username, password); |
| 30 | + |
| 31 | + if (res != 0) { |
| 32 | + git2_throw_last_error(); |
| 33 | + return; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +#define GIT2_CRED_FETCH() git2_cred_object_t *intern = (git2_cred_object_t*)Z_OBJ_P(getThis()); \ |
| 38 | + if (intern->cred == NULL) { \ |
| 39 | + git2_throw_exception(0 TSRMLS_CC, "Git2\\Cred object in invalid state"); \ |
| 40 | + return; \ |
| 41 | + } |
| 42 | + |
| 43 | +#define GIT2_CRED_GET_BOOL(_x) ZEND_BEGIN_ARG_INFO_EX(arginfo_cred_ ## _x, 0, 0, 0) \ |
| 44 | + ZEND_END_ARG_INFO() \ |
| 45 | + static PHP_METHOD(Cred, _x) { \ |
| 46 | + if (zend_parse_parameters_none() == FAILURE) return; \ |
| 47 | + GIT2_CRED_FETCH(); \ |
| 48 | + RETURN_BOOL(git_cred_ ## _x(intern->cred)); \ |
| 49 | + } |
| 50 | + |
| 51 | +GIT2_CRED_GET_BOOL(has_username) |
| 52 | + |
| 53 | +void git2_cred_spawn(zval *return_value, git_cred *cred TSRMLS_DC) { |
| 54 | + git2_cred_object_t *intern; |
| 55 | + |
| 56 | + object_init_ex(return_value, php_git2_cred_ce); |
| 57 | + intern = (git2_cred_object_t*)Z_OBJ_P(return_value); |
| 58 | + intern->cred = cred; |
| 59 | +} |
| 60 | + |
| 61 | +zend_object *php_git2_cred_create_object(zend_class_entry *class_type TSRMLS_DC) { |
| 62 | + git2_cred_object_t *intern = NULL; |
| 63 | + |
| 64 | + intern = emalloc(sizeof(git2_cred_object_t)); |
| 65 | + memset(intern, 0, sizeof(git2_cred_object_t)); |
| 66 | + |
| 67 | + zend_object_std_init(&intern->std, class_type TSRMLS_CC); |
| 68 | + object_properties_init(&intern->std, class_type); |
| 69 | + |
| 70 | + intern->std.handlers = &php_git2_cred_handler; |
| 71 | + |
| 72 | + return &intern->std; |
| 73 | +} |
| 74 | + |
| 75 | +static void php_git2_cred_free_object(zend_object *object TSRMLS_DC) { |
| 76 | + git2_cred_object_t *intern = (git2_cred_object_t*)object; |
| 77 | + |
| 78 | + zend_object_std_dtor(&intern->std TSRMLS_CC); |
| 79 | + |
| 80 | + if (intern->cred) { |
| 81 | + git_cred_free(intern->cred); |
| 82 | + intern->cred = NULL; |
| 83 | + } |
| 84 | + |
| 85 | + // no need with PHP7 to free intern |
| 86 | +} |
| 87 | + |
| 88 | +#define PHP_GIT2_CRED_ME_P(_x) PHP_ME(Cred, _x, arginfo_cred_##_x, ZEND_ACC_PUBLIC) |
| 89 | + |
| 90 | +static zend_function_entry git2_cred_methods[] = { |
| 91 | + PHP_ME(Cred, create_plaintext, arginfo_cred_create_plaintext, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) |
| 92 | + PHP_GIT2_CRED_ME_P(has_username) |
| 93 | +/* PHP_ME(Cred, __construct, arginfo___construct, ZEND_ACC_PUBLIC) */ |
| 94 | + { NULL, NULL, NULL } |
| 95 | +}; |
| 96 | + |
| 97 | +void git2_cred_init(TSRMLS_D) { |
| 98 | + zend_class_entry ce; |
| 99 | + |
| 100 | + INIT_NS_CLASS_ENTRY(ce, "Git2", "Cred", git2_cred_methods); |
| 101 | + php_git2_cred_ce = zend_register_internal_class(&ce TSRMLS_CC); |
| 102 | + php_git2_cred_ce->create_object = php_git2_cred_create_object; |
| 103 | + |
| 104 | + memcpy(&php_git2_cred_handler, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); |
| 105 | + php_git2_cred_handler.clone_obj = NULL; |
| 106 | + php_git2_cred_handler.free_obj = php_git2_cred_free_object; |
| 107 | + |
| 108 | +#define GIT2_CRED_CONST(_x) zend_declare_class_constant_long(php_git2_cred_ce, ZEND_STRL(#_x), GIT_CRED ## _x TSRMLS_CC) |
| 109 | + |
| 110 | + GIT2_CRED_CONST(TYPE_USERPASS_PLAINTEXT); |
| 111 | + GIT2_CRED_CONST(TYPE_SSH_KEY); |
| 112 | + GIT2_CRED_CONST(TYPE_SSH_CUSTOM); |
| 113 | + GIT2_CRED_CONST(TYPE_DEFAULT); |
| 114 | + GIT2_CRED_CONST(TYPE_SSH_INTERACTIVE); |
| 115 | + GIT2_CRED_CONST(TYPE_USERNAME); |
| 116 | + GIT2_CRED_CONST(TYPE_SSH_MEMORY); |
| 117 | +} |
| 118 | + |
0 commit comments