/excel.c
http://github.com/iliaal/php_excel · C · 6464 lines · 4790 code · 1017 blank · 657 comment · 564 complexity · fb1b521e7296e6cbbe4722a0016c6bbe MD5 · raw file
Large files are truncated click here to view the full file
- /*
- +----------------------------------------------------------------------+
- | PHP Version 5 |
- +----------------------------------------------------------------------+
- | Copyright (c) 1997-2014 The PHP Group |
- +----------------------------------------------------------------------+
- | This source file is subject to version 3.01 of the PHP license, |
- | that is bundled with this package in the file LICENSE, and is |
- | available through the world-wide-web at the following url: |
- | http://www.php.net/license/3_01.txt |
- | If you did not receive a copy of the PHP license and are unable to |
- | obtain it through the world-wide-web, please send a note to |
- | license@php.net so we can mail you a copy immediately. |
- +----------------------------------------------------------------------+
- | Author: Ilia Alshanetsky <ilia@ilia.ws> |
- +----------------------------------------------------------------------+
- */
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif
- #include "libxl.h"
- #include <stdlib.h>
- #include "php.h"
- #include "php_ini.h"
- #include "ext/standard/info.h"
- #include "ext/date/php_date.h"
- #if defined(HAVE_XML) && defined(EXCEL_WITH_LIBXML)
- #include "ext/xml/php_xml.h"
- #endif
- #include "php_excel.h"
- #include "zend_exceptions.h"
- static long xlFormatBorder(FormatHandle f)
- {
- return 1;
- }
- static long xlFormatBorderColor(FormatHandle f)
- {
- return 1;
- }
- /* work-around for missing headers in LibXL */
- #ifndef LIBXL_VERSION
- #define xlSheetSetProtect xlSheetSetProtectA
- #ifndef HAVE_LIBXL_243_PLUS
- #define xlSheetProtect xlSheetProtectA
- #endif
- #endif
- #if LIBXL_VERSION >= 0x03020000
- #define xlBookSetRefR1C1 xlBookSetRefR1C1A
- #define xlBookRefR1C1 xlBookRefR1C1A
- #endif
- #if LIBXL_VERSION >= 0x03020000 && LIBXL_VERSION < 0x03050401
- enum libXLPictureType {PICTURETYPE_PNG, PICTURETYPE_JPEG, PICTURETYPE_WMF, PICTURETYPE_DIB, PICTURETYPE_EMF, PICTURETYPE_PICT, PICTURETYPE_TIFF, PICTURETYPE_ERROR = 0xFF};
- #endif
- #define PHP_EXCEL_DATE 1
- #define PHP_EXCEL_FORMULA 2
- #define PHP_EXCEL_NUMERIC_STRING 3
- #define PHP_EXCEL_VERSION "1.0.3dev"
- #ifdef COMPILE_DL_EXCEL
- ZEND_GET_MODULE(excel)
- #endif
- ZEND_DECLARE_MODULE_GLOBALS(excel)
- static PHP_GINIT_FUNCTION(excel);
- PHP_INI_BEGIN()
- #if defined(HAVE_LIBXL_SETKEY)
- STD_PHP_INI_ENTRY("excel.license_name", NULL, PHP_INI_ALL, OnUpdateString, ini_license_name, zend_excel_globals, excel_globals)
- STD_PHP_INI_ENTRY("excel.license_key", NULL, PHP_INI_ALL, OnUpdateString, ini_license_key, zend_excel_globals, excel_globals)
- #endif
- STD_PHP_INI_ENTRY("excel.skip_empty", "0", PHP_INI_ALL, OnUpdateLong, ini_skip_empty, zend_excel_globals, excel_globals)
- PHP_INI_END()
- /* {{{ OO init/structure stuff */
- #define REGISTER_EXCEL_CLASS(name, c_name, clone) \
- { \
- zend_class_entry ce; \
- INIT_CLASS_ENTRY(ce, "Excel" # name, excel_funcs_ ## c_name); \
- ce.create_object = excel_object_new_ ## c_name; \
- excel_ce_ ## c_name = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC); \
- memcpy(&excel_object_handlers_ ## c_name, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
- excel_object_handlers_ ## c_name.clone_obj = clone; \
- }
- zend_class_entry *excel_ce_book, *excel_ce_sheet, *excel_ce_format, *excel_ce_font;
- static zend_object_handlers excel_object_handlers_book;
- static zend_object_handlers excel_object_handlers_sheet;
- static zend_object_handlers excel_object_handlers_format;
- static zend_object_handlers excel_object_handlers_font;
- typedef struct _excel_book_object {
- zend_object std;
- BookHandle book;
- } excel_book_object;
- #define BOOK_FROM_OBJECT(book, object) \
- { \
- excel_book_object *obj = (excel_book_object*) zend_object_store_get_object(object TSRMLS_CC); \
- book = obj->book; \
- if (!book) { \
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "The book wasn't initialized"); \
- RETURN_FALSE; \
- } \
- }
- typedef struct _excel_sheet_object {
- zend_object std;
- SheetHandle sheet;
- BookHandle book;
- } excel_sheet_object;
- #define SHEET_FROM_OBJECT(sheet, object) \
- { \
- excel_sheet_object *obj = (excel_sheet_object*) zend_object_store_get_object(object TSRMLS_CC); \
- sheet = obj->sheet; \
- if (!sheet) { \
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "The sheet wasn't initialized"); \
- RETURN_FALSE; \
- } \
- }
- #define SHEET_AND_BOOK_FROM_OBJECT(sheet, book, object) \
- { \
- excel_sheet_object *obj = (excel_sheet_object*) zend_object_store_get_object(object TSRMLS_CC); \
- sheet = obj->sheet; \
- book = obj->book; \
- if (!sheet) { \
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "The sheet wasn't initialized"); \
- RETURN_FALSE; \
- } \
- }
- #define FONT_FROM_OBJECT(font, object) \
- { \
- excel_font_object *obj = (excel_font_object*) zend_object_store_get_object(object TSRMLS_CC); \
- font = obj->font; \
- if (!font) { \
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "The font wasn't initialized"); \
- RETURN_FALSE; \
- } \
- }
- typedef struct _excel_font_object {
- zend_object std;
- FontHandle font;
- BookHandle book;
- } excel_font_object;
- #define FORMAT_FROM_OBJECT(format, object) \
- { \
- excel_format_object *obj = (excel_format_object*) zend_object_store_get_object(object TSRMLS_CC); \
- format = obj->format; \
- if (!format) { \
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "The format wasn't initialized"); \
- RETURN_FALSE; \
- } \
- }
- typedef struct _excel_format_object {
- zend_object std;
- FormatHandle format;
- BookHandle book;
- } excel_format_object;
- static void excel_book_object_free_storage(void *object TSRMLS_DC)
- {
- excel_book_object *intern = (excel_book_object *)object;
- zend_object_std_dtor(&intern->std TSRMLS_CC);
- if (intern->book) {
- xlBookRelease(intern->book);
- intern->book = NULL;
- }
- efree(object);
- }
- static zend_object_value excel_object_new_book(zend_class_entry *class_type TSRMLS_DC)
- {
- excel_book_object *intern;
- zend_object_value retval;
- intern = emalloc(sizeof(excel_book_object));
- memset(intern, 0, sizeof(excel_book_object));
- zend_object_std_init(&intern->std, class_type TSRMLS_CC);
- #ifdef ZEND_ENGINE_2_4
- object_properties_init(&intern->std, class_type);
- #else
- {
- zval *tmp;
- zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
- }
- #endif
- intern->book = xlCreateBook();
- (&retval)->handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) excel_book_object_free_storage, NULL TSRMLS_CC);
- (&retval)->handlers = &excel_object_handlers_book;
- return retval;
- }
- static void excel_sheet_object_free_storage(void *object TSRMLS_DC)
- {
- excel_sheet_object *intern = (excel_sheet_object *)object;
- zend_object_std_dtor(&intern->std TSRMLS_CC);
- efree(object);
- }
- static zend_object_value excel_object_new_sheet(zend_class_entry *class_type TSRMLS_DC)
- {
- excel_sheet_object *intern;
- zend_object_value retval;
- intern = emalloc(sizeof(excel_sheet_object));
- memset(intern, 0, sizeof(excel_sheet_object));
- zend_object_std_init(&intern->std, class_type TSRMLS_CC);
- #ifdef ZEND_ENGINE_2_4
- object_properties_init(&intern->std, class_type);
- #else
- {
- zval *tmp;
- zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
- }
- #endif
- (&retval)->handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) excel_sheet_object_free_storage, NULL TSRMLS_CC);
- (&retval)->handlers = &excel_object_handlers_sheet;
- return retval;
- }
- static void excel_font_object_free_storage(void *object TSRMLS_DC)
- {
- excel_font_object *intern = (excel_font_object *)object;
- zend_object_std_dtor(&intern->std TSRMLS_CC);
- efree(object);
- }
- #define REGISTER_EXCEL_CLASS_CONST_LONG(class_name, const_name, value) \
- zend_declare_class_constant_long(excel_ce_ ## class_name, const_name, sizeof(const_name)-1, (long)value TSRMLS_CC);
- static zend_object_value excel_object_new_font_ex(zend_class_entry *class_type, excel_font_object **ptr TSRMLS_DC)
- {
- excel_font_object *intern;
- zend_object_value retval;
- intern = emalloc(sizeof(excel_font_object));
- memset(intern, 0, sizeof(excel_font_object));
- if (ptr) {
- *ptr = intern;
- }
- zend_object_std_init(&intern->std, class_type TSRMLS_CC);
- #ifdef ZEND_ENGINE_2_4
- object_properties_init(&intern->std, class_type);
- #else
- {
- zval *tmp;
- zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
- }
- #endif
- (&retval)->handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) excel_font_object_free_storage, NULL TSRMLS_CC);
- (&retval)->handlers = &excel_object_handlers_font;
- return retval;
- }
- static zend_object_value excel_object_new_font(zend_class_entry *class_type TSRMLS_DC)
- {
- return excel_object_new_font_ex(class_type, NULL TSRMLS_CC);
- }
- static zend_object_value excel_font_object_clone(zval *this_ptr TSRMLS_DC)
- {
- excel_font_object *new_obj = NULL;
- excel_font_object *old_obj = (excel_font_object *) zend_object_store_get_object(this_ptr TSRMLS_CC);
- zend_object_value new_ov = excel_object_new_font_ex(old_obj->std.ce, &new_obj TSRMLS_CC);
- FontHandle font;
- font = xlBookAddFont(old_obj->book, old_obj->font);
- if (!font) {
- zend_throw_exception(NULL, "Failed to copy font", 0 TSRMLS_CC);
- } else {
- new_obj->book = old_obj->book;
- new_obj->font = font;
- }
- zend_objects_clone_members(&new_obj->std, new_ov, &old_obj->std, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
- return new_ov;
- }
- static void excel_format_object_free_storage(void *object TSRMLS_DC)
- {
- excel_format_object *intern = (excel_format_object *)object;
- zend_object_std_dtor(&intern->std TSRMLS_CC);
- efree(object);
- }
- static zend_object_value excel_object_new_format_ex(zend_class_entry *class_type, excel_format_object **ptr TSRMLS_DC)
- {
- excel_format_object *intern;
- zend_object_value retval;
- intern = emalloc(sizeof(excel_format_object));
- memset(intern, 0, sizeof(excel_format_object));
- if (ptr) {
- *ptr = intern;
- }
- zend_object_std_init(&intern->std, class_type TSRMLS_CC);
- #ifdef ZEND_ENGINE_2_4
- object_properties_init(&intern->std, class_type);
- #else
- {
- zval *tmp;
- zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
- }
- #endif
- (&retval)->handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) excel_format_object_free_storage, NULL TSRMLS_CC);
- (&retval)->handlers = &excel_object_handlers_format;
- return retval;
- }
- static zend_object_value excel_object_new_format(zend_class_entry *class_type TSRMLS_DC)
- {
- return excel_object_new_format_ex(class_type, NULL TSRMLS_CC);
- }
- static zend_object_value excel_format_object_clone(zval *this_ptr TSRMLS_DC)
- {
- excel_format_object *new_obj = NULL;
- excel_format_object *old_obj = (excel_format_object *) zend_object_store_get_object(this_ptr TSRMLS_CC);
- zend_object_value new_ov = excel_object_new_format_ex(old_obj->std.ce, &new_obj TSRMLS_CC);
- FormatHandle format;
- format = xlBookAddFormat(old_obj->book, old_obj->format);
- if (!format) {
- zend_throw_exception(NULL, "Failed to copy format", 0 TSRMLS_CC);
- } else {
- new_obj->book = old_obj->book;
- new_obj->format = format;
- }
- zend_objects_clone_members(&new_obj->std, new_ov, &old_obj->std, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
- return new_ov;
- }
- #if LIBXL_VERSION <= 0x03010000
- static wchar_t * _php_excel_to_wide(const char *string, size_t len, size_t *out_len)
- {
- wchar_t *buf = safe_emalloc(len, sizeof(wchar_t), 0);
- *out_len = mbstowcs(buf, string, len);
- if (*out_len == (size_t) -1) {
- efree(buf);
- return NULL;
- }
- return erealloc(buf, (*out_len + 1) * sizeof(wchar_t));
- }
- #endif
- #define EXCEL_METHOD(class_name, function_name) \
- PHP_METHOD(Excel ## class_name, function_name)
- /* {{{ proto bool ExcelBook::requiresKey()
- true if license key is required. */
- EXCEL_METHOD(Book, requiresKey)
- {
- #if defined(HAVE_LIBXL_SETKEY)
- RETURN_BOOL(1);
- #else
- RETURN_BOOL(0);
- #endif
- }
- /* }}} */
- /* {{{ proto bool ExcelBook::load(string data)
- Load Excel data string. */
- EXCEL_METHOD(Book, load)
- {
- BookHandle book;
- zval *object = getThis();
- char *data;
- int data_len;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) == FAILURE) {
- RETURN_FALSE;
- }
- if (!data_len) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- RETURN_BOOL(xlBookLoadRaw(book, data, data_len));
- }
- /* }}} */
- /* {{{ proto bool ExcelBook::loadFile(string filename)
- Load Excel from file. */
- EXCEL_METHOD(Book, loadFile)
- {
- BookHandle book;
- zval *object = getThis();
- char *filename;
- int filename_len;
- php_stream *stream;
- int len;
- char *contents;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
- RETURN_FALSE;
- }
- if (!filename_len) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- stream = php_stream_open_wrapper(filename, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
- if (!stream) {
- RETURN_FALSE;
- }
- len = php_stream_copy_to_mem(stream, &contents, PHP_STREAM_COPY_ALL, 0);
- php_stream_close(stream);
- if (len < 1) {
- RETURN_FALSE;
- }
- RETVAL_BOOL(xlBookLoadRaw(book, contents, len));
- efree(contents);
- }
- /* }}} */
- /* {{{ proto mixed ExcelBook::save([string filename])
- Save Excel file. */
- EXCEL_METHOD(Book, save)
- {
- BookHandle book;
- zval *object = getThis();
- char *filename = NULL;
- int filename_len;
- unsigned int len = 0;
- char *contents = NULL;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &filename, &filename_len) == FAILURE) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- if (!xlBookSaveRaw(book, (const char **)&contents, &len)) {
- RETURN_FALSE;
- }
- if (filename) {
- int numbytes;
- php_stream *stream = php_stream_open_wrapper(filename, "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
- if (!stream) {
- RETURN_FALSE;
- }
- if ((numbytes = php_stream_write(stream, contents, len)) != len) {
- php_stream_close(stream);
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only %d of %d bytes written, possibly out of free disk space", numbytes, len);
- RETURN_FALSE;
- }
- php_stream_close(stream);
- RETURN_TRUE;
- } else {
- RETURN_STRINGL(contents, len, 1);
- }
- }
- /* }}} */
- /* {{{ proto ExcelSheet ExcelBook::getSheet([int sheet])
- Get an excel sheet. */
- EXCEL_METHOD(Book, getSheet)
- {
- BookHandle book;
- zval *object = getThis();
- long sheet = 0;
- SheetHandle sh;
- excel_sheet_object *fo;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &sheet) == FAILURE) {
- RETURN_FALSE;
- }
- if (sheet < 0) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- if (!(sh = xlBookGetSheet(book, sheet))) {
- RETURN_FALSE;
- }
- Z_TYPE_P(return_value) = IS_OBJECT;
- object_init_ex(return_value, excel_ce_sheet);
- Z_SET_REFCOUNT_P(return_value, 1);
- Z_SET_ISREF_P(return_value);
- fo = (excel_sheet_object *) zend_object_store_get_object(return_value TSRMLS_CC);
- fo->sheet = sh;
- fo->book = book;
- }
- /* }}} */
- /* {{{ proto ExcelSheet ExcelBook::getSheetByName(string name [, bool case_insensitive])
- Get an excel sheet by name. */
- EXCEL_METHOD(Book, getSheetByName)
- {
- BookHandle book;
- zval *object = getThis();
- char *sheet_name;
- int sheet_name_len;
- long sheet;
- excel_sheet_object *fo;
- long sheet_count;
- zend_bool case_s = 0;
- const char *s;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &sheet_name, &sheet_name_len, &case_s) == FAILURE) {
- RETURN_FALSE;
- }
- if (sheet_name_len == 0) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- sheet_count = xlBookSheetCount(book);
- for(sheet = 0; sheet < sheet_count; sheet++) {
- SheetHandle sh = xlBookGetSheet(book, sheet);
- if (sh) {
- s = xlSheetName(sh);
- if (s) {
- if ((case_s && !strcasecmp(s, sheet_name)) || (!case_s && !strcmp(s, sheet_name))) {
- Z_TYPE_P(return_value) = IS_OBJECT;
- object_init_ex(return_value, excel_ce_sheet);
- Z_SET_REFCOUNT_P(return_value, 1);
- Z_SET_ISREF_P(return_value);
- fo = (excel_sheet_object *) zend_object_store_get_object(return_value TSRMLS_CC);
- fo->sheet = sh;
- fo->book = book;
- return;
- }
- }
- }
- }
- RETURN_FALSE;
- }
- /* }}} */
- /* {{{ proto bool ExcelBook::deleteSheet(int sheet)
- Delete an excel sheet. */
- EXCEL_METHOD(Book, deleteSheet)
- {
- BookHandle book;
- zval *object = getThis();
- long sheet;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &sheet) == FAILURE) {
- RETURN_FALSE;
- }
- if (sheet < 0) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- RETURN_BOOL(xlBookDelSheet(book, sheet));
- }
- /* }}} */
- /* {{{ proto int ExcelBook::activeSheet([int sheet])
- Get or set an active excel sheet. */
- EXCEL_METHOD(Book, activeSheet)
- {
- BookHandle book;
- zval *object = getThis();
- long sheet = -1;
- long res;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &sheet) == FAILURE) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- if (sheet > -1) {
- xlBookSetActiveSheet(book, sheet);
- }
- res = xlBookActiveSheet(book);
- if (sheet == -1 || res == sheet) {
- RETURN_LONG(res);
- } else {
- RETURN_FALSE;
- }
- }
- /* }}} */
- /* {{{ proto ExcelSheet ExcelBook::addSheet(string name)
- Add an excel sheet. */
- EXCEL_METHOD(Book, addSheet)
- {
- BookHandle book;
- zval *object = getThis();
- SheetHandle sh;
- excel_sheet_object *fo;
- char *name;
- int name_len;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- #ifdef LIBXL_VERSION
- sh = xlBookAddSheet(book, name, 0);
- #else
- sh = xlBookAddSheet(book, name);
- #endif
- if (!sh) {
- RETURN_FALSE;
- }
- Z_TYPE_P(return_value) = IS_OBJECT;
- object_init_ex(return_value, excel_ce_sheet);
- Z_SET_REFCOUNT_P(return_value, 1);
- Z_SET_ISREF_P(return_value);
- fo = (excel_sheet_object *) zend_object_store_get_object(return_value TSRMLS_CC);
- fo->sheet = sh;
- fo->book = book;
- }
- /* }}} */
- /* {{{ proto ExcelSheet ExcelBook::copySheet(string name, int sheet_number)
- Copy an excel sheet. */
- EXCEL_METHOD(Book, copySheet)
- {
- BookHandle book;
- zval *object = getThis();
- SheetHandle sh;
- excel_sheet_object *fo;
- char *name;
- int name_len;
- long num;
- #ifdef LIBXL_VERSION
- SheetHandle osh;
- #endif
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &name, &name_len, &num) == FAILURE) {
- RETURN_FALSE;
- }
- if (num < 0) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- #ifdef LIBXL_VERSION
- if (!(osh = xlBookGetSheet(book, num))) {
- RETURN_FALSE;
- }
- sh = xlBookAddSheet(book, name, osh);
- #else
- sh = xlBookCopySheet(book, name, num);
- #endif
- if (!sh) {
- RETURN_FALSE;
- }
- Z_TYPE_P(return_value) = IS_OBJECT;
- object_init_ex(return_value, excel_ce_sheet);
- Z_SET_REFCOUNT_P(return_value, 1);
- Z_SET_ISREF_P(return_value);
- fo = (excel_sheet_object *) zend_object_store_get_object(return_value TSRMLS_CC);
- fo->sheet = sh;
- fo->book = book;
- }
- /* }}} */
- /* {{{ proto int ExcelBook::sheetCount()
- Get the number of sheets inside a file. */
- EXCEL_METHOD(Book, sheetCount)
- {
- BookHandle book;
- zval *object = getThis();
- if (ZEND_NUM_ARGS()) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- RETURN_LONG(xlBookSheetCount(book));
- }
- /* }}} */
- /* {{{ proto string ExcelBook::getError()
- Get Excel error string. */
- EXCEL_METHOD(Book, getError)
- {
- BookHandle book;
- zval *object = getThis();
- char *err;
- if (ZEND_NUM_ARGS()) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- err = (char *)xlBookErrorMessage(book);
- if (err) {
- if (!strcmp(err, "ok")) {
- RETURN_FALSE;
- } else {
- RETURN_STRING(err, 1);
- }
- } else {
- RETURN_STRING("Unknown Error", 1);
- }
- }
- /* }}} */
- /* {{{ proto ExcelFont ExcelBook::addFont([ExcelFont font])
- Add or Copy ExcelFont object. */
- EXCEL_METHOD(Book, addFont)
- {
- BookHandle book;
- zval *object = getThis();
- FontHandle nfont;
- FontHandle font = NULL;
- excel_font_object *fo;
- zval *fob = NULL;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &fob, excel_ce_font) == FAILURE) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- if (fob) {
- FONT_FROM_OBJECT(font, fob);
- }
- nfont = xlBookAddFont(book, font);
- if (!nfont) {
- RETURN_FALSE;
- }
- Z_TYPE_P(return_value) = IS_OBJECT;
- object_init_ex(return_value, excel_ce_font);
- Z_SET_REFCOUNT_P(return_value, 1);
- Z_SET_ISREF_P(return_value);
- fo = (excel_font_object *) zend_object_store_get_object(return_value TSRMLS_CC);
- fo->font = nfont;
- fo->book = book;
- }
- /* }}} */
- /* {{{ proto ExcelFormat ExcelBook::addFormat([ExcelFormat format])
- Add or Copy ExcelFormat object. */
- EXCEL_METHOD(Book, addFormat)
- {
- BookHandle book;
- zval *object = getThis();
- FormatHandle nformat;
- FormatHandle format = NULL;
- excel_format_object *fo;
- zval *fob = NULL;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &fob, excel_ce_format) == FAILURE) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- if (fob) {
- FORMAT_FROM_OBJECT(format, fob);
- }
- nformat = xlBookAddFormat(book, format);
- if (!nformat) {
- RETURN_FALSE;
- }
- Z_TYPE_P(return_value) = IS_OBJECT;
- object_init_ex(return_value, excel_ce_format);
- Z_SET_REFCOUNT_P(return_value, 1);
- Z_SET_ISREF_P(return_value);
- fo = (excel_format_object *) zend_object_store_get_object(return_value TSRMLS_CC);
- fo->format = nformat;
- fo->book = book;
- }
- /* }}} */
- #ifdef HAVE_LIBXL_243_PLUS
- /* {{{ proto array ExcelBook::getAllFormats()
- Get an array of all ExcelFormat objects used inside a document. */
- EXCEL_METHOD(Book, getAllFormats)
- {
- BookHandle book;
- zval *object = getThis();
- unsigned short fc;
- unsigned short c;
- if (ZEND_NUM_ARGS()) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- array_init(return_value);
- fc = xlBookFormatSize(book);
- if (!fc) {
- return;
- }
- for (c = 0; c < fc; c++) {
- FormatHandle format;
- if ((format = xlBookFormat(book, c))) {
- excel_format_object *fo;
- zval *value;
- MAKE_STD_ZVAL(value);
- Z_TYPE_P(value) = IS_OBJECT;
- object_init_ex(value, excel_ce_format);
- Z_SET_REFCOUNT_P(value, 1);
- Z_SET_ISREF_P(value);
- fo = (excel_format_object *) zend_object_store_get_object(value TSRMLS_CC);
- fo->format = format;
- fo->book = book;
- add_next_index_zval(return_value, value);
- }
- }
- }
- /* }}} */
- #endif
- /* {{{ proto int ExcelBook::addCustomFormat(string format)
- Create a custom cell format */
- EXCEL_METHOD(Book, addCustomFormat)
- {
- BookHandle book;
- zval *object = getThis();
- char *format;
- int format_len;
- int id;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &format, &format_len) == FAILURE) {
- RETURN_FALSE;
- }
- if (!format_len) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- if (!(id = xlBookAddCustomNumFormat(book, format))) {
- RETURN_FALSE;
- }
- RETURN_LONG(id);
- }
- /* }}} */
- /* {{{ proto string ExcelBook::getCustomFormat(int id)
- Get a custom cell format */
- EXCEL_METHOD(Book, getCustomFormat)
- {
- BookHandle book;
- zval *object = getThis();
- long id;
- char *data;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &id) == FAILURE) {
- RETURN_FALSE;
- }
- if (id < 1) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- if (!(data = (char *)xlBookCustomNumFormat(book, id))) {
- RETURN_FALSE;
- }
- RETURN_STRING(data, 1);
- }
- /* }}} */
- static double _php_excel_date_pack(BookHandle book, long ts)
- {
- struct tm tm;
- if (!php_localtime_r(&ts, &tm)) {
- return -1;
- }
- tm.tm_year += 1900;
- tm.tm_mon += 1;
- return xlBookDatePack(book, tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec
- #ifdef HAVE_LIBXL_243_PLUS
- , 0
- #endif
- );
- }
- /* {{{ proto float ExcelBook::packDate(int timestamp)
- Pack a unix timestamp into an Excel Double */
- EXCEL_METHOD(Book, packDate)
- {
- BookHandle book;
- zval *object = getThis();
- long ts;
- double dt;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ts) == FAILURE) {
- RETURN_FALSE;
- }
- if (ts < 1) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- if ((dt = _php_excel_date_pack(book, ts)) == -1) {
- RETURN_FALSE;
- }
- RETURN_DOUBLE(dt);
- }
- /* }}} */
- static double _php_excel_date_pack_values(BookHandle book, int year, int month, int day, int hour, int min, int sec)
- {
- return xlBookDatePack(book, year, month, day, hour, min, sec
- #ifdef HAVE_LIBXL_243_PLUS
- , 0
- #endif
- );
- }
- /* {{{ proto float ExcelBook::packDateValues(int year, int month, int day, int hour, int minute, int second)
- Pack a date by single values into an Excel Double */
- EXCEL_METHOD(Book, packDateValues)
- {
- BookHandle book;
- zval *object = getThis();
- long year, month, day, hour, min, sec;
- double dt;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "llllll", &year, &month, &day, &hour, &min, &sec) == FAILURE) {
- RETURN_FALSE;
- }
- // if it is a date or just a time - hour, min & sec must be checked
- if (hour < 0 || hour > 23) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for hour", hour);
- RETURN_FALSE;
- }
- if (min < 0 || min > 59) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for minute", min);
- RETURN_FALSE;
- }
- if (sec < 0 || sec > 59) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for second", sec);
- RETURN_FALSE;
- }
- // check date only if there are values
- // is every value=0 - it's okay for generating a time
- if (year != 0 || month != 0 || day != 0) {
- if (year < 1) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for year", year);
- RETURN_FALSE;
- }
- if (month < 1 || month > 12) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for month", month);
- RETURN_FALSE;
- }
- if (day < 1 || day > 31) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for day", day);
- RETURN_FALSE;
- }
- }
- BOOK_FROM_OBJECT(book, object);
- if ((dt = _php_excel_date_pack_values(book, year, month, day, hour, min, sec)) == -1) {
- RETURN_FALSE;
- }
- RETURN_DOUBLE(dt);
- }
- /* }}} */
- static long _php_excel_date_unpack(BookHandle book, double dt)
- {
- struct tm tm = {0};
- #ifdef HAVE_LIBXL_243_PLUS
- #if LIBXL_VERSION >= 0x03010000
- int msec;
- #else
- unsigned short msec;
- #endif
- #endif
- #if LIBXL_VERSION >= 0x03010000
- if (!xlBookDateUnpack(book, dt, (int *) &(tm.tm_year), (int *) &(tm.tm_mon), (int *) &(tm.tm_mday), (int *) &(tm.tm_hour), (int *) &(tm.tm_min), (int *) &(tm.tm_sec)
- #else
- if (!xlBookDateUnpack(book, dt, (short unsigned int *) &(tm.tm_year), (short unsigned int *) &(tm.tm_mon), (short unsigned int *) &(tm.tm_mday),
- (short unsigned int *) &(tm.tm_hour), (short unsigned int *) &(tm.tm_min), (short unsigned int *) &(tm.tm_sec)
- #endif
- #ifdef HAVE_LIBXL_243_PLUS
- , &msec
- #endif
- )) {
- return -1;
- }
- tm.tm_year -= 1900;
- tm.tm_mon -= 1;
- tm.tm_isdst = -1;
- return mktime(&tm);
- }
- /* {{{ proto int ExcelBook::unpackDate(double date)
- Unpack a unix timestamp from an Excel Double */
- EXCEL_METHOD(Book, unpackDate)
- {
- BookHandle book;
- zval *object = getThis();
- double dt;
- time_t t;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &dt) == FAILURE) {
- RETURN_FALSE;
- }
- if (dt < 1) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- if ((t = _php_excel_date_unpack(book, dt)) == -1) {
- RETURN_FALSE;
- }
- RETURN_LONG(t);
- }
- /* }}} */
- #if LIBXL_VERSION >= 0x03050300
- /* {{{ proto bool ExcelBook::isDate1904()
- Returns whether the 1904 date system is active: true - 1904 date system, false - 1900 date system */
- EXCEL_METHOD(Book, isDate1904)
- {
- BookHandle book;
- zval *object = getThis();
- if (ZEND_NUM_ARGS()) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- RETURN_BOOL(xlBookIsDate1904(book));
- }
- /* }}} */
- /* {{{ proto bool ExcelBook::setDate1904(bool date_type)
- Sets the date system mode: true - 1904 date system, false - 1900 date system (default) */
- EXCEL_METHOD(Book, setDate1904)
- {
- BookHandle book;
- zval *object = getThis();
- zend_bool date_type;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &date_type) == FAILURE) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- xlBookSetDate1904(book, (int)date_type);
- RETURN_TRUE;
- }
- /* }}} */
- #endif
- /* {{{ proto int ExcelBook::getActiveSheet()
- Get the active sheet inside a file. */
- EXCEL_METHOD(Book, getActiveSheet)
- {
- BookHandle book;
- zval *object = getThis();
- if (ZEND_NUM_ARGS()) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- RETURN_LONG(xlBookActiveSheet(book));
- }
- /* }}} */
- /* {{{ proto array ExcelBook::getDefaultFont()
- Get the default font. */
- EXCEL_METHOD(Book, getDefaultFont)
- {
- BookHandle book;
- zval *object = getThis();
- const char *font;
- #if LIBXL_VERSION >= 0x03010000
- int font_size;
- #else
- unsigned short font_size;
- #endif
- if (ZEND_NUM_ARGS()) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- if (!(font = xlBookDefaultFont(book, &font_size))) {
- RETURN_FALSE;
- }
- array_init(return_value);
- add_assoc_string(return_value, "font", (char *)font, 1);
- add_assoc_long(return_value, "font_size", font_size);
- }
- /* }}} */
- /* {{{ proto void ExcelBook::setDefaultFont(string font, int font_size)
- Set the default font, and size. */
- EXCEL_METHOD(Book, setDefaultFont)
- {
- BookHandle book;
- zval *object = getThis();
- char *font;
- int font_len;
- long font_size;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &font, &font_len, &font_size) == FAILURE || font_size < 1) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- xlBookSetDefaultFont(book, font, (int)font_size);
- }
- /* }}} */
- /* {{{ proto void ExcelBook::setLocale(string locale)
- Set the locale. */
- EXCEL_METHOD(Book, setLocale)
- {
- BookHandle book;
- zval *object = getThis();
- char *locale;
- int locale_len;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &locale, &locale_len) == FAILURE || locale_len < 1) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- xlBookSetLocale(book, locale);
- }
- /* }}} */
- /* {{{ proto ExcelBook ExcelBook::__construct([string license_name, string license_key [, bool excel_2007 = false]])
- Book Constructor. */
- EXCEL_METHOD(Book, __construct)
- {
- BookHandle book;
- zval *object = getThis();
- char *name = NULL, *key;
- int name_len = 0, key_len = 0;
- #if LIBXL_VERSION <= 0x03010000
- wchar_t *nw, *kw;
- size_t nw_l, kw_l;
- #endif
- #if defined(HAVE_XML) && defined(EXCEL_WITH_LIBXML)
- char *namep, *keyp;
- int plen;
- #endif
- #ifdef LIBXL_VERSION
- zend_bool new_excel = 0;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ssb", &name, &name_len, &key, &key_len, &new_excel) == FAILURE) {
- RETURN_FALSE;
- }
- #else
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &name, &name_len, &key, &key_len) == FAILURE) {
- RETURN_FALSE;
- }
- #endif
- #if defined(HAVE_LIBXL_SETKEY)
- if (!name_len) {
- if (INI_STR("excel.license_name") && INI_STR("excel.license_key")) {
- name = INI_STR("excel.license_name");
- name_len = strlen(name);
- key = INI_STR("excel.license_key");
- key_len = strlen(key);
- } else {
- #ifndef LIBXL_VERSION
- return;
- #endif
- }
- }
- #endif
- BOOK_FROM_OBJECT(book, object);
- #ifdef LIBXL_VERSION
- if (new_excel) {
- excel_book_object *obj = (excel_book_object*) zend_object_store_get_object(object TSRMLS_CC);
- if ((book = xlCreateXMLBook())) {
- xlBookRelease(obj->book);
- obj->book = book;
- } else {
- RETURN_FALSE;
- }
- #if !defined(HAVE_LIBXL_SETKEY)
- return;
- #endif
- if (!name_len && !key_len) {
- return;
- }
- }
- #endif
- if (!name_len || !key_len) {
- RETURN_FALSE;
- }
- #if LIBXL_VERSION <= 0x03010000
- if (!(nw = _php_excel_to_wide(name, name_len + 1, &nw_l))) {
- RETURN_FALSE;
- }
- if (!(kw = _php_excel_to_wide(key, key_len + 1, &kw_l))) {
- efree(nw);
- RETURN_FALSE;
- }
- xlBookSetKey(book, nw, kw);
- efree(nw);
- efree(kw);
- #else
- #if defined(HAVE_XML) && defined(EXCEL_WITH_LIBXML)
- namep = xml_utf8_decode((const XML_Char *) name, name_len, &plen, (const XML_Char *)"ISO-8859-1");
- keyp = xml_utf8_decode((const XML_Char *) key, key_len, &plen, (const XML_Char *)"ISO-8859-1");
- xlBookSetKey(book, namep, keyp);
- efree(namep);
- efree(keyp);
- #else
- xlBookSetKey(book, name, key);
- #endif
- #endif
- }
- /* }}} */
- /* {{{ proto bool ExcelBook::setActiveSheet(int sheet)
- Set the sheet active. */
- EXCEL_METHOD(Book, setActiveSheet)
- {
- BookHandle book;
- zval *object = getThis();
- long id;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &id) == FAILURE || id < 0) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- xlBookSetActiveSheet(book, id);
- RETURN_BOOL(id == xlBookActiveSheet(book));
- }
- /* }}} */
- static void php_excel_add_picture(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
- {
- char *data;
- int data_len;
- BookHandle book;
- zval *object = getThis();
- int ret;
- BOOK_FROM_OBJECT(book, object);
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) == FAILURE) {
- RETURN_FALSE;
- }
- if (mode == 1) {
- ret = xlBookAddPicture2(book, data, data_len);
- } else {
- php_stream *stream = php_stream_open_wrapper(data, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
- int len;
- char *contents;
- if (!stream) {
- RETURN_FALSE;
- }
- len = php_stream_copy_to_mem(stream, &contents, PHP_STREAM_COPY_ALL, 0);
- php_stream_close(stream);
- if (len < 1) {
- RETURN_FALSE;
- }
- ret = xlBookAddPicture2(book, contents, len);
- efree(contents);
- }
- if (ret == -1) {
- RETURN_FALSE;
- } else {
- #if LIBXL_VERSION >= 0x03020200 && LIBXL_VERSION < 0x03020300
- /* work-around for a bug inside libxl 3.2.2 */
- ret -= 1;
- #endif
- RETURN_LONG(ret);
- }
- }
- /* {{{ proto int ExcelBook::addPictureFromFile(string filename)
- Add picture from file. */
- EXCEL_METHOD(Book, addPictureFromFile)
- {
- php_excel_add_picture(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelBook::addPictureFromString(string data)
- Add picture from string. */
- EXCEL_METHOD(Book, addPictureFromString)
- {
- php_excel_add_picture(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
- }
- /* }}} */
- #ifdef LIBXL_VERSION
- /* {{{ proto bool ExcelBook::rgbMode()
- Returns whether the RGB mode is active. */
- EXCEL_METHOD(Book, rgbMode)
- {
- BookHandle book;
- zval *object = getThis();
- if (ZEND_NUM_ARGS()) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- RETURN_BOOL(xlBookRgbMode(book));
- }
- /* }}} */
- /* {{{ proto void ExcelBook::setRGBMode(bool mode)
- Sets a RGB mode on or off. */
- EXCEL_METHOD(Book, setRGBMode)
- {
- BookHandle book;
- zval *object = getThis();
- zend_bool val;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &val) == FAILURE) {
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- xlBookSetRgbMode(book, val);
- }
- /* }}} */
- /* {{{ proto int ExcelBook::colorPack(int r, int g, int b)
- Packs red, green and blue components in color value. Used for xlsx format only. */
- EXCEL_METHOD(Book, colorPack)
- {
- BookHandle book;
- zval *object = getThis();
- long r, g, b;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &r, &g, &b) == FAILURE) {
- RETURN_FALSE;
- }
- if (r < 0 || r > 255) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for color red", r);
- RETURN_FALSE;
- } else if (g < 0 || g > 255) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for color green", g);
- RETURN_FALSE;
- } else if (b < 0 || b > 255) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for color blue", b);
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- RETURN_LONG(xlBookColorPack(book, (unsigned short)r, (unsigned short)g, (unsigned short)b));
- }
- /* }}} */
- /* {{{ proto array ExcelBook::colorUnpack(int color)
- Unpacks color value to red, green and blue components. Used for xlsx format only. */
- EXCEL_METHOD(Book, colorUnpack)
- {
- BookHandle book;
- zval *object = getThis();
- #if LIBXL_VERSION >= 0x03010000
- int r, g, b;
- #else
- unsigned short r, g, b;
- #endif
- long color;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &color) == FAILURE) {
- RETURN_FALSE;
- }
- if (color <= 0) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for color code", color);
- RETURN_FALSE;
- }
- BOOK_FROM_OBJECT(book, object);
- xlBookColorUnpack(book, (int)color, &r, &g, &b);
- array_init(return_value);
- add_assoc_long(return_value, "red", r);
- add_assoc_long(return_value, "green", g);
- add_assoc_long(return_value, "blue", b);
- }
- /* }}} */
- #endif
- /* {{{ proto int ExcelFont::size([int size])
- Get or set the font size */
- EXCEL_METHOD(Font, size)
- {
- zval *object = getThis();
- FontHandle font;
- long size = -1;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &size) == FAILURE) {
- RETURN_FALSE;
- }
- FONT_FROM_OBJECT(font, object);
- if (size > 0) {
- xlFontSetSize(font, size);
- }
- RETURN_LONG(xlFontSize(font));
- }
- /* }}} */
- /* {{{ proto bool ExcelFont::italics([bool italics])
- Get or set the if italics are enabled */
- EXCEL_METHOD(Font, italics)
- {
- zval *object = getThis();
- FontHandle font;
- zend_bool italics;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &italics) == FAILURE) {
- RETURN_FALSE;
- }
- FONT_FROM_OBJECT(font, object);
- if (ZEND_NUM_ARGS()) {
- xlFontSetItalic(font, italics);
- }
- RETURN_BOOL(xlFontItalic(font));
- }
- /* }}} */
- /* {{{ proto bool ExcelFont::strike([bool strike])
- Get or set the font strike-through */
- EXCEL_METHOD(Font, strike)
- {
- zval *object = getThis();
- FontHandle font;
- zend_bool strike;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &strike) == FAILURE) {
- RETURN_FALSE;
- }
- FONT_FROM_OBJECT(font, object);
- if (ZEND_NUM_ARGS()) {
- xlFontSetStrikeOut(font, strike);
- }
- RETURN_BOOL(xlFontStrikeOut(font));
- }
- /* }}} */
- /* {{{ proto bool ExcelFont::bold([bool bold])
- Get or set the font bold */
- EXCEL_METHOD(Font, bold)
- {
- zval *object = getThis();
- FontHandle font;
- zend_bool bold;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &bold) == FAILURE) {
- RETURN_FALSE;
- }
- FONT_FROM_OBJECT(font, object);
- if (ZEND_NUM_ARGS()) {
- xlFontSetBold(font, bold);
- }
- RETURN_BOOL(xlFontBold(font));
- }
- /* }}} */
- /* {{{ proto int ExcelFont::color([int color])
- Get or set the font color */
- EXCEL_METHOD(Font, color)
- {
- zval *object = getThis();
- FontHandle font;
- long color;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &color) == FAILURE) {
- RETURN_FALSE;
- }
- FONT_FROM_OBJECT(font, object);
- if (ZEND_NUM_ARGS()) {
- xlFontSetColor(font, color);
- }
- RETURN_LONG(xlFontColor(font));
- }
- /* }}} */
- /* {{{ proto int ExcelFont::mode([int mode])
- Get or set the font mode */
- EXCEL_METHOD(Font, mode)
- {
- zval *object = getThis();
- FontHandle font;
- long mode;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &mode) == FAILURE) {
- RETURN_FALSE;
- }
- FONT_FROM_OBJECT(font, object);
- if (ZEND_NUM_ARGS()) {
- xlFontSetScript(font, mode);
- }
- RETURN_LONG(xlFontScript(font));
- }
- /* }}} */
- /* {{{ proto int ExcelFont::underline([int underline_style])
- Get or set the font underline style */
- EXCEL_METHOD(Font, underline)
- {
- zval *object = getThis();
- FontHandle font;
- long underline;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &underline) == FAILURE) {
- RETURN_FALSE;
- }
- FONT_FROM_OBJECT(font, object);
- if (ZEND_NUM_ARGS()) {
- xlFontSetUnderline(font, underline);
- }
- RETURN_LONG(xlFontUnderline(font));
- }
- /* }}} */
- /* {{{ proto string ExcelFont::name([string name])
- Get or set the font name */
- EXCEL_METHOD(Font, name)
- {
- zval *object = getThis();
- FontHandle font;
- char *name = NULL;
- int name_len;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
- RETURN_FALSE;
- }
- FONT_FROM_OBJECT(font, object);
- if (name) {
- xlFontSetName(font, name);
- }
- RETURN_STRING((char *)xlFontName(font), 1);
- }
- /* }}} */
- /* {{{ proto ExcelFormat ExcelFormat::__construct(ExcelBook book)
- Format Constructor. */
- EXCEL_METHOD(Format, __construct)
- {
- BookHandle book;
- FormatHandle format;
- zval *object = getThis();
- excel_format_object *obj;
- zval *zbook;
- PHP_EXCEL_ERROR_HANDLING();
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zbook, excel_ce_book) == FAILURE) {
- PHP_EXCEL_RESTORE_ERRORS();
- return;
- }
- PHP_EXCEL_RESTORE_ERRORS();
- BOOK_FROM_OBJECT(book, zbook);
- obj = (excel_format_object*) zend_object_store_get_object(object TSRMLS_CC);
- format = xlBookAddFormat(book, NULL);
- if (!format) {
- RETURN_FALSE;
- }
- obj->format = format;
- obj->book = book;
- }
- /* }}} */
- /* {{{ proto ExcelFont ExcelFont::__construct(ExcelBook book)
- Font Constructor. */
- EXCEL_METHOD(Font, __construct)
- {
- BookHandle book;
- FontHandle font;
- zval *object = getThis();
- excel_font_object *obj;
- zval *zbook;
- PHP_EXCEL_ERROR_HANDLING();
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zbook, excel_ce_book) == FAILURE) {
- PHP_EXCEL_RESTORE_ERRORS();
- return;
- }
- PHP_EXCEL_RESTORE_ERRORS();
- BOOK_FROM_OBJECT(book, zbook);
- obj = (excel_font_object*) zend_object_store_get_object(object TSRMLS_CC);
- font = xlBookAddFont(book, NULL);
- if (!font) {
- RETURN_FALSE;
- }
- obj->font = font;
- obj->book = book;
- }
- /* }}} */
- /* {{{ proto bool ExcelFormat::setFont(ExcelFont font)
- Set the font for a format. */
- EXCEL_METHOD(Format, setFont)
- {
- FormatHandle format;
- zval *object = getThis();
- FontHandle font;
- zval *zfont;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zfont, excel_ce_font) == FAILURE) {
- RETURN_FALSE;
- }
- FORMAT_FROM_OBJECT(format, object);
- FONT_FROM_OBJECT(font, zfont);
- if (!xlFormatSetFont(format, font)) {
- RETURN_FALSE;
- }
- RETURN_TRUE;
- }
- /* }}} */
- /* {{{ proto ExcelFont ExcelFormat::getFont()
- Get the font for this format. */
- EXCEL_METHOD(Format, getFont)
- {
- FormatHandle format;
- zval *object = getThis();
- FontHandle font;
- excel_font_object *fo;
- excel_format_object *obj = (excel_format_object*) zend_object_store_get_object(object TSRMLS_CC);
- format = obj->format;
- if (!format) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "The format wasn't initialized");
- RETURN_FALSE;
- }
- if (ZEND_NUM_ARGS()) {
- RETURN_FALSE;
- }
- FORMAT_FROM_OBJECT(format, object);
- font = xlFormatFont(format);
- if (!font) {
- RETURN_FALSE;
- }
- Z_TYPE_P(return_value) = IS_OBJECT;
- object_init_ex(return_value, excel_ce_font);
- Z_SET_REFCOUNT_P(return_value, 1);
- Z_SET_ISREF_P(return_value);
- fo = (excel_font_object *) zend_object_store_get_object(return_value TSRMLS_CC);
- fo->font = font;
- fo->book = obj->book;
- }
- /* }}} */
- #define PHP_EXCEL_LONG_FORMAT_OPTION(func_name, write_only) \
- { \
- FormatHandle format; \
- zval *object = getThis(); \
- long data; \
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &data) == FAILURE) { \
- RETURN_FALSE; \
- } \
- FORMAT_FROM_OBJECT(format, object); \
- if (ZEND_NUM_ARGS()) { \
- xlFormatSet ## func_name (format, data); \
- } \
- if (!write_only) { \
- RETURN_LONG(xlFormat ## func_name (format)); \
- } else { \
- RETURN_TRUE; \
- } \
- }
- #define PHP_EXCEL_BOOL_FORMAT_OPTION(func_name) \
- { \
- FormatHandle format; \
- zval *object = getThis(); \
- zend_bool data; \
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &data) == FAILURE) { \
- RETURN_FALSE; \
- } \
- FORMAT_FROM_OBJECT(format, object); \
- if (ZEND_NUM_ARGS()) { \
- xlFormatSet ## func_name (format, data); \
- } \
- RETURN_BOOL(xlFormat ## func_name (format)); \
- }
- /* {{{ proto int ExcelFormat::numberFormat([int format])
- Get or set the cell number format */
- EXCEL_METHOD(Format, numberFormat)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(NumFormat, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::horizontalAlign([int align_mode])
- Get or set the cell horizontal alignment */
- EXCEL_METHOD(Format, horizontalAlign)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(AlignH, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::verticalAlign([int align_mode])
- Get or set the cell vertical alignment */
- EXCEL_METHOD(Format, verticalAlign)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(AlignV, 0);
- }
- /* }}} */
- /* {{{ proto bool ExcelFormat::wrap([bool wrap])
- Get or set the cell wrapping */
- EXCEL_METHOD(Format, wrap)
- {
- PHP_EXCEL_BOOL_FORMAT_OPTION(Wrap);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::rotate([int angle])
- Get or set the cell data rotation */
- EXCEL_METHOD(Format, rotate)
- {
- FormatHandle format;
- zval *object = getThis();
- long angle;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &angle) == FAILURE) {
- RETURN_FALSE;
- }
- FORMAT_FROM_OBJECT(format, object);
- if (ZEND_NUM_ARGS()) {
- if (angle < 0 || (angle > 180 && angle != 255)) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Rotation can be a number between 0 and 180 or 255");
- RETURN_FALSE;
- }
- xlFormatSetRotation(format, angle);
- }
- RETURN_LONG(xlFormatRotation(format));
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::indent([int indent])
- Get or set the cell text indentation level */
- EXCEL_METHOD(Format, indent)
- {
- FormatHandle format;
- zval *object = getThis();
- long indent;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &indent) == FAILURE) {
- RETURN_FALSE;
- }
- FORMAT_FROM_OBJECT(format, object);
- if (ZEND_NUM_ARGS()) {
- if (indent < 0 || indent > 15) {
- php_error_docref(NULL TSRMLS_CC, E_WARNING, "Text indentation level must be less than or equal to 15");
- RETURN_FALSE;
- }
- xlFormatSetIndent(format, indent);
- }
- RETURN_LONG(xlFormatIndent(format));
- }
- /* }}} */
- /* {{{ proto bool ExcelFormat::shrinkToFit([bool shrink])
- Get or set whether the cell is shrink-to-fit */
- EXCEL_METHOD(Format, shrinkToFit)
- {
- PHP_EXCEL_BOOL_FORMAT_OPTION(ShrinkToFit);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::borderStyle([int style])
- Get or set the cell border */
- EXCEL_METHOD(Format, borderStyle)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(Border, 1);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::borderColor([int color])
- Get or set the cell color */
- EXCEL_METHOD(Format, borderColor)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(BorderColor, 1);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::borderLeftStyle([int style])
- Get or set the cell left border */
- EXCEL_METHOD(Format, borderLeftStyle)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(BorderLeft, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::borderLeftColor([int color])
- Get or set the cell left color */
- EXCEL_METHOD(Format, borderLeftColor)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(BorderLeftColor, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::borderRightStyle([int style])
- Get or set the cell right border */
- EXCEL_METHOD(Format, borderRightStyle)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(BorderRight, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::borderRightColor([int color])
- Get or set the cell right color */
- EXCEL_METHOD(Format, borderRightColor)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(BorderRightColor, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::borderTopStyle([int style])
- Get or set the cell top border */
- EXCEL_METHOD(Format, borderTopStyle)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(BorderTop, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::borderTopColor([int color])
- Get or set the cell top color */
- EXCEL_METHOD(Format, borderTopColor)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(BorderTopColor, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::borderBottomStyle([int style])
- Get or set the cell bottom border */
- EXCEL_METHOD(Format, borderBottomStyle)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(BorderBottom, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::borderBottomColor([int color])
- Get or set the cell bottom color */
- EXCEL_METHOD(Format, borderBottomColor)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(BorderBottomColor, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::borderDiagonalStyle([int style])
- Get or set the cell diagonal border */
- EXCEL_METHOD(Format, borderDiagonalStyle)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(BorderDiagonal, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::borderDiagonalColor([int color])
- Get or set the cell diagonal color */
- EXCEL_METHOD(Format, borderDiagonalColor)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(BorderDiagonalColor, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::fillPattern([int patern])
- Get or set the cell fill pattern */
- EXCEL_METHOD(Format, fillPattern)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(FillPattern, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::patternForegroundColor([int color])
- Get or set the cell pattern foreground color */
- EXCEL_METHOD(Format, patternForegroundColor)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(PatternForegroundColor, 0);
- }
- /* }}} */
- /* {{{ proto int ExcelFormat::patternBackgroundColor([int color])
- Get or set the cell pattern background color */
- EXCEL_METHOD(Format, patternBackgroundColor)
- {
- PHP_EXCEL_LONG_FORMAT_OPTION(PatternBackgroundColor, 0);
- }
- /* }}} */
- /* {{{ proto bool ExcelFormat::locked([bool locked])
- Get or set whether the cell is locked */
- EXCEL_METHOD(Format, locked)
- {
- PHP_EXCEL_BOOL_FORMAT_OPTION(Locked);
- }
- /* }}} */
- /* {{{ proto bool ExcelFormat::hidden([bool hidden])
- Get or set whether the cell is hidden */
- EXCEL_METHOD(Format, hidden)
- {
- PHP_EXCEL_BOOL_FORMAT_OPTION(Hidden);
- }
- /* }}} */
- /* {{{ proto ExcelSheet ExcelSheet::__construct(ExcelBook book, string name)
- Sheet Constructor. */
- EXCEL_METHOD(Sheet, __construct)
- {
- BookHandle book;
- SheetHandle sh;
- zval *object = getThis();
- excel_sheet_object *obj;
- zval *zbook;
- char *name;
- int name_len;
- PHP_EXCEL_ERROR_HANDLING();
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &zbook, excel_ce_book, &name, &name_len) == FAILURE) {
- PHP_EXCEL_RESTORE_ERRORS();
- return;
- }
- PHP_EXCEL_RESTORE_ERRORS();
- BOOK_FROM_OBJECT(book, zbook);
- obj = (excel_sheet_object*) zend_object_store_get_object(object TSRMLS_CC);
- #ifdef LIBXL_VERSION
- sh = xlBookAddSheet(book, name, 0);
- #else
- sh = xlBookAddSheet(book, name);
- #endif
- if (!sh) {
- RETURN_FALSE;
- }
- obj->sheet = sh;
- obj->book = book;
- }
- /* }}} */
- /* {{{ proto int ExcelSheet::cellType(int row, int column)
- Get cell type */
- EXCEL_METHOD(Sheet, cellType)
- {
- zval *object = getThis();
- SheetHandle sheet;
- long row, col;
- if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &row, &col) == FAILURE) {
- RETURN_FALSE;
- }
- SHEET_FROM_OBJECT(sheet, object);
- RETURN_LONG(xlSheetCellType(sheet,…