intecture-api 0.3.2

API component for Intecture infrastructure. Intecture is the developer friendly, multi-lingual configuration management tool.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
 Copyright 2015-2017 Intecture Developers. See the COPYRIGHT file at the
 top-level directory of this distribution and at
 https://intecture.io/COPYRIGHT.

 Licensed under the Mozilla Public License 2.0 <LICENSE or
 https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied,
 modified, or distributed except according to those terms.
*/

#include "file.h"
#include "host.h"
#include <zend_exceptions.h>

/* PHP 5.4 */
#if PHP_VERSION_ID < 50399
# define object_properties_init(zo, class_type) { \
    zval *tmp; \
    zend_hash_copy((*zo).properties, \
        &class_type->default_properties, \
        (copy_ctor_func_t) zval_add_ref, \
        (void *) &tmp, \
        sizeof(zval *)); \
    }
#endif

/*
 * File Class
 */

zend_class_entry *inapi_ce_file;

static zend_function_entry file_methods[] = {
    PHP_ME(File, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
    PHP_ME(File, exists, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(File, upload, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(File, upload_file, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(File, delete, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(File, mv, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(File, copy, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(File, get_owner, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(File, set_owner, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(File, get_mode, NULL, ZEND_ACC_PUBLIC)
    PHP_ME(File, set_mode, NULL, ZEND_ACC_PUBLIC)
    {NULL, NULL, NULL}
};

void inapi_init_file(TSRMLS_D) {
    zend_class_entry ce;

    INIT_CLASS_ENTRY(ce, "Intecture\\File", file_methods);
    ce.create_object = create_php_file;
    inapi_ce_file = zend_register_internal_class(&ce TSRMLS_CC);
    zend_declare_class_constant_long(inapi_ce_file, "OPT_BACKUP_EXISTING", 19, OPT_BACKUP_EXISTING TSRMLS_CC);
    zend_declare_class_constant_long(inapi_ce_file, "OPT_CHUNK_SIZE", 14, OPT_CHUNK_SIZE TSRMLS_CC);
}

zend_object_value create_php_file(zend_class_entry *class_type TSRMLS_DC) {
    zend_object_value retval;
    php_file  *intern;
    zval *tmp;

    intern = (php_file*)emalloc(sizeof(php_file));
    memset(intern, 0, sizeof(php_file));

    zend_object_std_init(&intern->std, class_type TSRMLS_CC);
    object_properties_init(&intern->std, class_type);

    retval.handle = zend_objects_store_put(
        intern,
        (zend_objects_store_dtor_t) zend_objects_destroy_object,
        free_php_file,
        NULL TSRMLS_CC
    );
    retval.handlers = zend_get_std_object_handlers();

    return retval;
}

void free_php_file(void *object TSRMLS_DC) {
    php_file *file = (php_file*)object;
    if (file->file) {
        int rc = file_free(file->file);
        assert(rc == 0);
    }
    efree(file);
}

/*
 * Exception Class
 */

zend_class_entry *inapi_ce_file_exception;

void inapi_init_file_exception(TSRMLS_D) {
    zend_class_entry e;

    INIT_CLASS_ENTRY(e, "Intecture\\FileException", NULL);
    inapi_ce_file_exception = zend_register_internal_class_ex(&e, (zend_class_entry*)zend_exception_get_default(TSRMLS_C), NULL TSRMLS_CC);
}

/*
 * File Methods
 */

PHP_METHOD(File, __construct) {
    php_file *intern;
    zval *phost;
    php_host *host;
    char *path;
    int path_len;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs", &phost, &path, &path_len) == FAILURE) {
        return;
    }

    intern = (php_file*)zend_object_store_get_object(getThis() TSRMLS_CC);

    int rtn = get_check_host(phost, &host TSRMLS_CC);
    if (rtn != 0) {
        zend_throw_exception(inapi_ce_file_exception, "The first argument must be an instance of Intecture\\Host", 1000 TSRMLS_CC);
        return;
    }

    File *file = file_new(host->host, path);

    if (!file) {
        zend_throw_exception(inapi_ce_file_exception, geterr(), 1000 TSRMLS_CC);
        return;
    }

    intern->file = file;
}

PHP_METHOD(File, exists) {
    php_file *intern;
    zval *phost;
    php_host *host;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &phost) == FAILURE) {
        return;
    }

    intern = (php_file*)zend_object_store_get_object(getThis() TSRMLS_CC);

    int rtn = get_check_host(phost, &host TSRMLS_CC);
    if (rtn != 0) {
        zend_throw_exception(inapi_ce_file_exception, "The first argument must be an instance of Intecture\\Host", 1000 TSRMLS_CC);
        return;
    }

    int exists = file_exists(intern->file, host->host);

    if (exists < 0) {
        zend_throw_exception(inapi_ce_file_exception, geterr(), 1000 TSRMLS_CC);
    }
    else if (exists == 1) {
        RETURN_TRUE;
    } else {
        RETURN_FALSE;
    }
}

PHP_METHOD(File, upload) {
    php_file *intern;
    zval *phost;
    php_host *host;
    char *path;
    int path_len;
    zval *opts = NULL;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs|a", &phost, &path, &path_len, &opts) == FAILURE) {
        return;
    }

    intern = (php_file*)zend_object_store_get_object(getThis() TSRMLS_CC);

    int rc = get_check_host(phost, &host TSRMLS_CC);
    if (rc != 0) {
        zend_throw_exception(inapi_ce_file_exception, "The first argument must be an instance of Intecture\\Host", 1000 TSRMLS_CC);
        return;
    }

    FileOptions *c_opts = parse_opts(opts TSRMLS_CC);
    if (!c_opts) {
        return;
    }

    rc = file_upload(intern->file, host->host, path, c_opts);

    if (rc != 0) {
        zend_throw_exception(inapi_ce_file_exception, geterr(), 1000 TSRMLS_CC);
        return;
    }
}

PHP_METHOD(File, upload_file) {
    php_file *intern;
    zval *phost;
    php_host *host;
    long fd;
    zval *opts = NULL;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl|a", &phost, &fd, &opts) == FAILURE) {
        return;
    }

    intern = (php_file*)zend_object_store_get_object(getThis() TSRMLS_CC);

    int rc = get_check_host(phost, &host TSRMLS_CC);
    if (rc != 0) {
        zend_throw_exception(inapi_ce_file_exception, "The first argument must be an instance of Intecture\\Host", 1000 TSRMLS_CC);
        return;
    }

    FileOptions *c_opts = parse_opts(opts TSRMLS_CC);
    if (!c_opts) {
        return;
    }

    rc = file_upload_file(intern->file, host->host, fd, c_opts);

    if (rc != 0) {
        zend_throw_exception(inapi_ce_file_exception, geterr(), 1000 TSRMLS_CC);
        return;
    }
}

PHP_METHOD(File, delete) {
    php_file *intern;
    zval *phost;
    php_host *host;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &phost) == FAILURE) {
        return;
    }

    intern = (php_file*)zend_object_store_get_object(getThis() TSRMLS_CC);

    int rtn = get_check_host(phost, &host TSRMLS_CC);
    if (rtn != 0) {
        zend_throw_exception(inapi_ce_file_exception, "The first argument must be an instance of Intecture\\Host", 1000 TSRMLS_CC);
        return;
    }

    int rc = file_delete(intern->file, host->host);

    if (rc != 0) {
        zend_throw_exception(inapi_ce_file_exception, geterr(), 1000 TSRMLS_CC);
        return;
    }
}

PHP_METHOD(File, mv) {
    php_file *intern;
    zval *phost;
    php_host *host;
    char *new_path;
    int new_path_len;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs", &phost, &new_path, &new_path_len) == FAILURE) {
        return;
    }

    intern = (php_file*)zend_object_store_get_object(getThis() TSRMLS_CC);

    int rtn = get_check_host(phost, &host TSRMLS_CC);
    if (rtn != 0) {
        zend_throw_exception(inapi_ce_file_exception, "The first argument must be an instance of Intecture\\Host", 1000 TSRMLS_CC);
        return;
    }

    int rc = file_mv(intern->file, host->host, new_path);

    if (rc != 0) {
        zend_throw_exception(inapi_ce_file_exception, geterr(), 1000 TSRMLS_CC);
        return;
    }
}

PHP_METHOD(File, copy) {
    php_file *intern;
    zval *phost;
    php_host *host;
    char *new_path;
    int new_path_len;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs", &phost, &new_path, &new_path_len) == FAILURE) {
        return;
    }

    intern = (php_file*)zend_object_store_get_object(getThis() TSRMLS_CC);

    int rtn = get_check_host(phost, &host TSRMLS_CC);
    if (rtn != 0) {
        zend_throw_exception(inapi_ce_file_exception, "The first argument must be an instance of Intecture\\Host", 1000 TSRMLS_CC);
        return;
    }

    int rc = file_copy(intern->file, host->host, new_path);

    if (rc != 0) {
        zend_throw_exception(inapi_ce_file_exception, geterr(), 1000 TSRMLS_CC);
        return;
    }
}

PHP_METHOD(File, get_owner) {
    php_file *intern;
    zval *phost;
    php_host *host;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &phost) == FAILURE) {
        return;
    }

    intern = (php_file*)zend_object_store_get_object(getThis() TSRMLS_CC);

    int rtn = get_check_host(phost, &host TSRMLS_CC);
    if (rtn != 0) {
        zend_throw_exception(inapi_ce_file_exception, "The first argument must be an instance of Intecture\\Host", 1000 TSRMLS_CC);
        return;
    }

    FileOwner *owner = file_get_owner(intern->file, host->host);

    if (!owner) {
        zend_throw_exception(inapi_ce_file_exception, geterr(), 1000 TSRMLS_CC);
        return;
    }

    array_init(return_value);
    add_assoc_string(return_value, "user_name", owner->user_name, 1);
    add_assoc_long(return_value, "user_uid", owner->user_uid);
    add_assoc_string(return_value, "group_name", owner->group_name, 1);
    add_assoc_long(return_value, "group_gid", owner->group_gid);
}

PHP_METHOD(File, set_owner) {
    php_file *intern;
    zval *phost;
    php_host *host;
    char *user, *group;
    int user_len, group_len;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zss", &phost, &user, &user_len, &group, &group_len) == FAILURE) {
        return;
    }

    intern = (php_file*)zend_object_store_get_object(getThis() TSRMLS_CC);

    int rtn = get_check_host(phost, &host TSRMLS_CC);
    if (rtn != 0) {
        zend_throw_exception(inapi_ce_file_exception, "The first argument must be an instance of Intecture\\Host", 1000 TSRMLS_CC);
        return;
    }

    int rc = file_set_owner(intern->file, host->host, user, group);

    if (rc != 0) {
        zend_throw_exception(inapi_ce_file_exception, geterr(), 1000 TSRMLS_CC);
        return;
    }
}

PHP_METHOD(File, get_mode) {
    php_file *intern;
    zval *phost;
    php_host *host;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &phost) == FAILURE) {
        return;
    }

    intern = (php_file*)zend_object_store_get_object(getThis() TSRMLS_CC);

    int rtn = get_check_host(phost, &host TSRMLS_CC);
    if (rtn != 0) {
        zend_throw_exception(inapi_ce_file_exception, "The first argument must be an instance of Intecture\\Host", 1000 TSRMLS_CC);
        return;
    }

    int16_t mode = file_get_mode(intern->file, host->host);

    if (mode < 0) {
        zend_throw_exception(inapi_ce_file_exception, geterr(), 1000 TSRMLS_CC);
        return;
    }

    RETURN_LONG(mode);
}

PHP_METHOD(File, set_mode) {
    php_file *intern;
    zval *phost;
    php_host *host;
    long mode;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zl", &phost, &mode) == FAILURE) {
        return;
    }

    intern = (php_file*)zend_object_store_get_object(getThis() TSRMLS_CC);

    int rtn = get_check_host(phost, &host TSRMLS_CC);
    if (rtn != 0) {
        zend_throw_exception(inapi_ce_file_exception, "The first argument must be an instance of Intecture\\Host", 1000 TSRMLS_CC);
        return;
    }

    int rc = file_set_mode(intern->file, host->host, mode);

    if (rc != 0) {
        zend_throw_exception(inapi_ce_file_exception, geterr(), 1000 TSRMLS_CC);
        return;
    }
}

FileOptions *parse_opts(zval *opts TSRMLS_DC) {
    zval **data;
    HashTable *arr_hash;
    HashPosition pointer;
    int array_count;

    FileOptions *c_opts = emalloc(sizeof(FileOptions));
    c_opts->backup_existing = NULL;
    c_opts->chunk_size = 0;

    if (opts != NULL) {
        arr_hash = Z_ARRVAL_P(opts);
        array_count = zend_hash_num_elements(arr_hash);

        for (zend_hash_internal_pointer_reset_ex(arr_hash, &pointer); zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS; zend_hash_move_forward_ex(arr_hash, &pointer)) {
            char *key;
            unsigned int key_len;
            unsigned long index;

            if (zend_hash_get_current_key_ex(arr_hash, &key, &key_len, &index, 0, &pointer) == HASH_KEY_IS_LONG) {
                switch (index) {
                    case OPT_BACKUP_EXISTING:
                        c_opts->backup_existing = strdup(Z_STRVAL_PP(data));
                        break;
                    case OPT_CHUNK_SIZE:
                        c_opts->chunk_size = (unsigned long long)Z_LVAL_PP(data);
                    default:
                        zend_throw_exception(inapi_ce_file_exception, "Invalid option key - must be File constant", 1001 TSRMLS_CC);
                        return NULL;
                }
            } else {
                zend_throw_exception(inapi_ce_file_exception, "Invalid option key - must be File constant", 1001 TSRMLS_CC);
                return NULL;
            }
        }
    }

    return c_opts;
}