/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

  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2014 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Ilia Alshanetsky <ilia@ilia.ws> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "libxl.h"
  22. #include <stdlib.h>
  23. #include "php.h"
  24. #include "php_ini.h"
  25. #include "ext/standard/info.h"
  26. #include "ext/date/php_date.h"
  27. #if defined(HAVE_XML) && defined(EXCEL_WITH_LIBXML)
  28. #include "ext/xml/php_xml.h"
  29. #endif
  30. #include "php_excel.h"
  31. #include "zend_exceptions.h"
  32. static long xlFormatBorder(FormatHandle f)
  33. {
  34. return 1;
  35. }
  36. static long xlFormatBorderColor(FormatHandle f)
  37. {
  38. return 1;
  39. }
  40. /* work-around for missing headers in LibXL */
  41. #ifndef LIBXL_VERSION
  42. #define xlSheetSetProtect xlSheetSetProtectA
  43. #ifndef HAVE_LIBXL_243_PLUS
  44. #define xlSheetProtect xlSheetProtectA
  45. #endif
  46. #endif
  47. #if LIBXL_VERSION >= 0x03020000
  48. #define xlBookSetRefR1C1 xlBookSetRefR1C1A
  49. #define xlBookRefR1C1 xlBookRefR1C1A
  50. #endif
  51. #if LIBXL_VERSION >= 0x03020000 && LIBXL_VERSION < 0x03050401
  52. enum libXLPictureType {PICTURETYPE_PNG, PICTURETYPE_JPEG, PICTURETYPE_WMF, PICTURETYPE_DIB, PICTURETYPE_EMF, PICTURETYPE_PICT, PICTURETYPE_TIFF, PICTURETYPE_ERROR = 0xFF};
  53. #endif
  54. #define PHP_EXCEL_DATE 1
  55. #define PHP_EXCEL_FORMULA 2
  56. #define PHP_EXCEL_NUMERIC_STRING 3
  57. #define PHP_EXCEL_VERSION "1.0.3dev"
  58. #ifdef COMPILE_DL_EXCEL
  59. ZEND_GET_MODULE(excel)
  60. #endif
  61. ZEND_DECLARE_MODULE_GLOBALS(excel)
  62. static PHP_GINIT_FUNCTION(excel);
  63. PHP_INI_BEGIN()
  64. #if defined(HAVE_LIBXL_SETKEY)
  65. STD_PHP_INI_ENTRY("excel.license_name", NULL, PHP_INI_ALL, OnUpdateString, ini_license_name, zend_excel_globals, excel_globals)
  66. STD_PHP_INI_ENTRY("excel.license_key", NULL, PHP_INI_ALL, OnUpdateString, ini_license_key, zend_excel_globals, excel_globals)
  67. #endif
  68. STD_PHP_INI_ENTRY("excel.skip_empty", "0", PHP_INI_ALL, OnUpdateLong, ini_skip_empty, zend_excel_globals, excel_globals)
  69. PHP_INI_END()
  70. /* {{{ OO init/structure stuff */
  71. #define REGISTER_EXCEL_CLASS(name, c_name, clone) \
  72. { \
  73. zend_class_entry ce; \
  74. INIT_CLASS_ENTRY(ce, "Excel" # name, excel_funcs_ ## c_name); \
  75. ce.create_object = excel_object_new_ ## c_name; \
  76. excel_ce_ ## c_name = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC); \
  77. memcpy(&excel_object_handlers_ ## c_name, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); \
  78. excel_object_handlers_ ## c_name.clone_obj = clone; \
  79. }
  80. zend_class_entry *excel_ce_book, *excel_ce_sheet, *excel_ce_format, *excel_ce_font;
  81. static zend_object_handlers excel_object_handlers_book;
  82. static zend_object_handlers excel_object_handlers_sheet;
  83. static zend_object_handlers excel_object_handlers_format;
  84. static zend_object_handlers excel_object_handlers_font;
  85. typedef struct _excel_book_object {
  86. zend_object std;
  87. BookHandle book;
  88. } excel_book_object;
  89. #define BOOK_FROM_OBJECT(book, object) \
  90. { \
  91. excel_book_object *obj = (excel_book_object*) zend_object_store_get_object(object TSRMLS_CC); \
  92. book = obj->book; \
  93. if (!book) { \
  94. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The book wasn't initialized"); \
  95. RETURN_FALSE; \
  96. } \
  97. }
  98. typedef struct _excel_sheet_object {
  99. zend_object std;
  100. SheetHandle sheet;
  101. BookHandle book;
  102. } excel_sheet_object;
  103. #define SHEET_FROM_OBJECT(sheet, object) \
  104. { \
  105. excel_sheet_object *obj = (excel_sheet_object*) zend_object_store_get_object(object TSRMLS_CC); \
  106. sheet = obj->sheet; \
  107. if (!sheet) { \
  108. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The sheet wasn't initialized"); \
  109. RETURN_FALSE; \
  110. } \
  111. }
  112. #define SHEET_AND_BOOK_FROM_OBJECT(sheet, book, object) \
  113. { \
  114. excel_sheet_object *obj = (excel_sheet_object*) zend_object_store_get_object(object TSRMLS_CC); \
  115. sheet = obj->sheet; \
  116. book = obj->book; \
  117. if (!sheet) { \
  118. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The sheet wasn't initialized"); \
  119. RETURN_FALSE; \
  120. } \
  121. }
  122. #define FONT_FROM_OBJECT(font, object) \
  123. { \
  124. excel_font_object *obj = (excel_font_object*) zend_object_store_get_object(object TSRMLS_CC); \
  125. font = obj->font; \
  126. if (!font) { \
  127. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The font wasn't initialized"); \
  128. RETURN_FALSE; \
  129. } \
  130. }
  131. typedef struct _excel_font_object {
  132. zend_object std;
  133. FontHandle font;
  134. BookHandle book;
  135. } excel_font_object;
  136. #define FORMAT_FROM_OBJECT(format, object) \
  137. { \
  138. excel_format_object *obj = (excel_format_object*) zend_object_store_get_object(object TSRMLS_CC); \
  139. format = obj->format; \
  140. if (!format) { \
  141. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The format wasn't initialized"); \
  142. RETURN_FALSE; \
  143. } \
  144. }
  145. typedef struct _excel_format_object {
  146. zend_object std;
  147. FormatHandle format;
  148. BookHandle book;
  149. } excel_format_object;
  150. static void excel_book_object_free_storage(void *object TSRMLS_DC)
  151. {
  152. excel_book_object *intern = (excel_book_object *)object;
  153. zend_object_std_dtor(&intern->std TSRMLS_CC);
  154. if (intern->book) {
  155. xlBookRelease(intern->book);
  156. intern->book = NULL;
  157. }
  158. efree(object);
  159. }
  160. static zend_object_value excel_object_new_book(zend_class_entry *class_type TSRMLS_DC)
  161. {
  162. excel_book_object *intern;
  163. zend_object_value retval;
  164. intern = emalloc(sizeof(excel_book_object));
  165. memset(intern, 0, sizeof(excel_book_object));
  166. zend_object_std_init(&intern->std, class_type TSRMLS_CC);
  167. #ifdef ZEND_ENGINE_2_4
  168. object_properties_init(&intern->std, class_type);
  169. #else
  170. {
  171. zval *tmp;
  172. zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
  173. }
  174. #endif
  175. intern->book = xlCreateBook();
  176. (&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);
  177. (&retval)->handlers = &excel_object_handlers_book;
  178. return retval;
  179. }
  180. static void excel_sheet_object_free_storage(void *object TSRMLS_DC)
  181. {
  182. excel_sheet_object *intern = (excel_sheet_object *)object;
  183. zend_object_std_dtor(&intern->std TSRMLS_CC);
  184. efree(object);
  185. }
  186. static zend_object_value excel_object_new_sheet(zend_class_entry *class_type TSRMLS_DC)
  187. {
  188. excel_sheet_object *intern;
  189. zend_object_value retval;
  190. intern = emalloc(sizeof(excel_sheet_object));
  191. memset(intern, 0, sizeof(excel_sheet_object));
  192. zend_object_std_init(&intern->std, class_type TSRMLS_CC);
  193. #ifdef ZEND_ENGINE_2_4
  194. object_properties_init(&intern->std, class_type);
  195. #else
  196. {
  197. zval *tmp;
  198. zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
  199. }
  200. #endif
  201. (&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);
  202. (&retval)->handlers = &excel_object_handlers_sheet;
  203. return retval;
  204. }
  205. static void excel_font_object_free_storage(void *object TSRMLS_DC)
  206. {
  207. excel_font_object *intern = (excel_font_object *)object;
  208. zend_object_std_dtor(&intern->std TSRMLS_CC);
  209. efree(object);
  210. }
  211. #define REGISTER_EXCEL_CLASS_CONST_LONG(class_name, const_name, value) \
  212. zend_declare_class_constant_long(excel_ce_ ## class_name, const_name, sizeof(const_name)-1, (long)value TSRMLS_CC);
  213. static zend_object_value excel_object_new_font_ex(zend_class_entry *class_type, excel_font_object **ptr TSRMLS_DC)
  214. {
  215. excel_font_object *intern;
  216. zend_object_value retval;
  217. intern = emalloc(sizeof(excel_font_object));
  218. memset(intern, 0, sizeof(excel_font_object));
  219. if (ptr) {
  220. *ptr = intern;
  221. }
  222. zend_object_std_init(&intern->std, class_type TSRMLS_CC);
  223. #ifdef ZEND_ENGINE_2_4
  224. object_properties_init(&intern->std, class_type);
  225. #else
  226. {
  227. zval *tmp;
  228. zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
  229. }
  230. #endif
  231. (&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);
  232. (&retval)->handlers = &excel_object_handlers_font;
  233. return retval;
  234. }
  235. static zend_object_value excel_object_new_font(zend_class_entry *class_type TSRMLS_DC)
  236. {
  237. return excel_object_new_font_ex(class_type, NULL TSRMLS_CC);
  238. }
  239. static zend_object_value excel_font_object_clone(zval *this_ptr TSRMLS_DC)
  240. {
  241. excel_font_object *new_obj = NULL;
  242. excel_font_object *old_obj = (excel_font_object *) zend_object_store_get_object(this_ptr TSRMLS_CC);
  243. zend_object_value new_ov = excel_object_new_font_ex(old_obj->std.ce, &new_obj TSRMLS_CC);
  244. FontHandle font;
  245. font = xlBookAddFont(old_obj->book, old_obj->font);
  246. if (!font) {
  247. zend_throw_exception(NULL, "Failed to copy font", 0 TSRMLS_CC);
  248. } else {
  249. new_obj->book = old_obj->book;
  250. new_obj->font = font;
  251. }
  252. zend_objects_clone_members(&new_obj->std, new_ov, &old_obj->std, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
  253. return new_ov;
  254. }
  255. static void excel_format_object_free_storage(void *object TSRMLS_DC)
  256. {
  257. excel_format_object *intern = (excel_format_object *)object;
  258. zend_object_std_dtor(&intern->std TSRMLS_CC);
  259. efree(object);
  260. }
  261. static zend_object_value excel_object_new_format_ex(zend_class_entry *class_type, excel_format_object **ptr TSRMLS_DC)
  262. {
  263. excel_format_object *intern;
  264. zend_object_value retval;
  265. intern = emalloc(sizeof(excel_format_object));
  266. memset(intern, 0, sizeof(excel_format_object));
  267. if (ptr) {
  268. *ptr = intern;
  269. }
  270. zend_object_std_init(&intern->std, class_type TSRMLS_CC);
  271. #ifdef ZEND_ENGINE_2_4
  272. object_properties_init(&intern->std, class_type);
  273. #else
  274. {
  275. zval *tmp;
  276. zend_hash_copy(intern->std.properties, &class_type->default_properties, (copy_ctor_func_t) zval_add_ref, (void *) &tmp, sizeof(zval *));
  277. }
  278. #endif
  279. (&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);
  280. (&retval)->handlers = &excel_object_handlers_format;
  281. return retval;
  282. }
  283. static zend_object_value excel_object_new_format(zend_class_entry *class_type TSRMLS_DC)
  284. {
  285. return excel_object_new_format_ex(class_type, NULL TSRMLS_CC);
  286. }
  287. static zend_object_value excel_format_object_clone(zval *this_ptr TSRMLS_DC)
  288. {
  289. excel_format_object *new_obj = NULL;
  290. excel_format_object *old_obj = (excel_format_object *) zend_object_store_get_object(this_ptr TSRMLS_CC);
  291. zend_object_value new_ov = excel_object_new_format_ex(old_obj->std.ce, &new_obj TSRMLS_CC);
  292. FormatHandle format;
  293. format = xlBookAddFormat(old_obj->book, old_obj->format);
  294. if (!format) {
  295. zend_throw_exception(NULL, "Failed to copy format", 0 TSRMLS_CC);
  296. } else {
  297. new_obj->book = old_obj->book;
  298. new_obj->format = format;
  299. }
  300. zend_objects_clone_members(&new_obj->std, new_ov, &old_obj->std, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
  301. return new_ov;
  302. }
  303. #if LIBXL_VERSION <= 0x03010000
  304. static wchar_t * _php_excel_to_wide(const char *string, size_t len, size_t *out_len)
  305. {
  306. wchar_t *buf = safe_emalloc(len, sizeof(wchar_t), 0);
  307. *out_len = mbstowcs(buf, string, len);
  308. if (*out_len == (size_t) -1) {
  309. efree(buf);
  310. return NULL;
  311. }
  312. return erealloc(buf, (*out_len + 1) * sizeof(wchar_t));
  313. }
  314. #endif
  315. #define EXCEL_METHOD(class_name, function_name) \
  316. PHP_METHOD(Excel ## class_name, function_name)
  317. /* {{{ proto bool ExcelBook::requiresKey()
  318. true if license key is required. */
  319. EXCEL_METHOD(Book, requiresKey)
  320. {
  321. #if defined(HAVE_LIBXL_SETKEY)
  322. RETURN_BOOL(1);
  323. #else
  324. RETURN_BOOL(0);
  325. #endif
  326. }
  327. /* }}} */
  328. /* {{{ proto bool ExcelBook::load(string data)
  329. Load Excel data string. */
  330. EXCEL_METHOD(Book, load)
  331. {
  332. BookHandle book;
  333. zval *object = getThis();
  334. char *data;
  335. int data_len;
  336. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) == FAILURE) {
  337. RETURN_FALSE;
  338. }
  339. if (!data_len) {
  340. RETURN_FALSE;
  341. }
  342. BOOK_FROM_OBJECT(book, object);
  343. RETURN_BOOL(xlBookLoadRaw(book, data, data_len));
  344. }
  345. /* }}} */
  346. /* {{{ proto bool ExcelBook::loadFile(string filename)
  347. Load Excel from file. */
  348. EXCEL_METHOD(Book, loadFile)
  349. {
  350. BookHandle book;
  351. zval *object = getThis();
  352. char *filename;
  353. int filename_len;
  354. php_stream *stream;
  355. int len;
  356. char *contents;
  357. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &filename, &filename_len) == FAILURE) {
  358. RETURN_FALSE;
  359. }
  360. if (!filename_len) {
  361. RETURN_FALSE;
  362. }
  363. BOOK_FROM_OBJECT(book, object);
  364. stream = php_stream_open_wrapper(filename, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
  365. if (!stream) {
  366. RETURN_FALSE;
  367. }
  368. len = php_stream_copy_to_mem(stream, &contents, PHP_STREAM_COPY_ALL, 0);
  369. php_stream_close(stream);
  370. if (len < 1) {
  371. RETURN_FALSE;
  372. }
  373. RETVAL_BOOL(xlBookLoadRaw(book, contents, len));
  374. efree(contents);
  375. }
  376. /* }}} */
  377. /* {{{ proto mixed ExcelBook::save([string filename])
  378. Save Excel file. */
  379. EXCEL_METHOD(Book, save)
  380. {
  381. BookHandle book;
  382. zval *object = getThis();
  383. char *filename = NULL;
  384. int filename_len;
  385. unsigned int len = 0;
  386. char *contents = NULL;
  387. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &filename, &filename_len) == FAILURE) {
  388. RETURN_FALSE;
  389. }
  390. BOOK_FROM_OBJECT(book, object);
  391. if (!xlBookSaveRaw(book, (const char **)&contents, &len)) {
  392. RETURN_FALSE;
  393. }
  394. if (filename) {
  395. int numbytes;
  396. php_stream *stream = php_stream_open_wrapper(filename, "wb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
  397. if (!stream) {
  398. RETURN_FALSE;
  399. }
  400. if ((numbytes = php_stream_write(stream, contents, len)) != len) {
  401. php_stream_close(stream);
  402. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Only %d of %d bytes written, possibly out of free disk space", numbytes, len);
  403. RETURN_FALSE;
  404. }
  405. php_stream_close(stream);
  406. RETURN_TRUE;
  407. } else {
  408. RETURN_STRINGL(contents, len, 1);
  409. }
  410. }
  411. /* }}} */
  412. /* {{{ proto ExcelSheet ExcelBook::getSheet([int sheet])
  413. Get an excel sheet. */
  414. EXCEL_METHOD(Book, getSheet)
  415. {
  416. BookHandle book;
  417. zval *object = getThis();
  418. long sheet = 0;
  419. SheetHandle sh;
  420. excel_sheet_object *fo;
  421. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &sheet) == FAILURE) {
  422. RETURN_FALSE;
  423. }
  424. if (sheet < 0) {
  425. RETURN_FALSE;
  426. }
  427. BOOK_FROM_OBJECT(book, object);
  428. if (!(sh = xlBookGetSheet(book, sheet))) {
  429. RETURN_FALSE;
  430. }
  431. Z_TYPE_P(return_value) = IS_OBJECT;
  432. object_init_ex(return_value, excel_ce_sheet);
  433. Z_SET_REFCOUNT_P(return_value, 1);
  434. Z_SET_ISREF_P(return_value);
  435. fo = (excel_sheet_object *) zend_object_store_get_object(return_value TSRMLS_CC);
  436. fo->sheet = sh;
  437. fo->book = book;
  438. }
  439. /* }}} */
  440. /* {{{ proto ExcelSheet ExcelBook::getSheetByName(string name [, bool case_insensitive])
  441. Get an excel sheet by name. */
  442. EXCEL_METHOD(Book, getSheetByName)
  443. {
  444. BookHandle book;
  445. zval *object = getThis();
  446. char *sheet_name;
  447. int sheet_name_len;
  448. long sheet;
  449. excel_sheet_object *fo;
  450. long sheet_count;
  451. zend_bool case_s = 0;
  452. const char *s;
  453. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|b", &sheet_name, &sheet_name_len, &case_s) == FAILURE) {
  454. RETURN_FALSE;
  455. }
  456. if (sheet_name_len == 0) {
  457. RETURN_FALSE;
  458. }
  459. BOOK_FROM_OBJECT(book, object);
  460. sheet_count = xlBookSheetCount(book);
  461. for(sheet = 0; sheet < sheet_count; sheet++) {
  462. SheetHandle sh = xlBookGetSheet(book, sheet);
  463. if (sh) {
  464. s = xlSheetName(sh);
  465. if (s) {
  466. if ((case_s && !strcasecmp(s, sheet_name)) || (!case_s && !strcmp(s, sheet_name))) {
  467. Z_TYPE_P(return_value) = IS_OBJECT;
  468. object_init_ex(return_value, excel_ce_sheet);
  469. Z_SET_REFCOUNT_P(return_value, 1);
  470. Z_SET_ISREF_P(return_value);
  471. fo = (excel_sheet_object *) zend_object_store_get_object(return_value TSRMLS_CC);
  472. fo->sheet = sh;
  473. fo->book = book;
  474. return;
  475. }
  476. }
  477. }
  478. }
  479. RETURN_FALSE;
  480. }
  481. /* }}} */
  482. /* {{{ proto bool ExcelBook::deleteSheet(int sheet)
  483. Delete an excel sheet. */
  484. EXCEL_METHOD(Book, deleteSheet)
  485. {
  486. BookHandle book;
  487. zval *object = getThis();
  488. long sheet;
  489. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &sheet) == FAILURE) {
  490. RETURN_FALSE;
  491. }
  492. if (sheet < 0) {
  493. RETURN_FALSE;
  494. }
  495. BOOK_FROM_OBJECT(book, object);
  496. RETURN_BOOL(xlBookDelSheet(book, sheet));
  497. }
  498. /* }}} */
  499. /* {{{ proto int ExcelBook::activeSheet([int sheet])
  500. Get or set an active excel sheet. */
  501. EXCEL_METHOD(Book, activeSheet)
  502. {
  503. BookHandle book;
  504. zval *object = getThis();
  505. long sheet = -1;
  506. long res;
  507. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &sheet) == FAILURE) {
  508. RETURN_FALSE;
  509. }
  510. BOOK_FROM_OBJECT(book, object);
  511. if (sheet > -1) {
  512. xlBookSetActiveSheet(book, sheet);
  513. }
  514. res = xlBookActiveSheet(book);
  515. if (sheet == -1 || res == sheet) {
  516. RETURN_LONG(res);
  517. } else {
  518. RETURN_FALSE;
  519. }
  520. }
  521. /* }}} */
  522. /* {{{ proto ExcelSheet ExcelBook::addSheet(string name)
  523. Add an excel sheet. */
  524. EXCEL_METHOD(Book, addSheet)
  525. {
  526. BookHandle book;
  527. zval *object = getThis();
  528. SheetHandle sh;
  529. excel_sheet_object *fo;
  530. char *name;
  531. int name_len;
  532. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) {
  533. RETURN_FALSE;
  534. }
  535. BOOK_FROM_OBJECT(book, object);
  536. #ifdef LIBXL_VERSION
  537. sh = xlBookAddSheet(book, name, 0);
  538. #else
  539. sh = xlBookAddSheet(book, name);
  540. #endif
  541. if (!sh) {
  542. RETURN_FALSE;
  543. }
  544. Z_TYPE_P(return_value) = IS_OBJECT;
  545. object_init_ex(return_value, excel_ce_sheet);
  546. Z_SET_REFCOUNT_P(return_value, 1);
  547. Z_SET_ISREF_P(return_value);
  548. fo = (excel_sheet_object *) zend_object_store_get_object(return_value TSRMLS_CC);
  549. fo->sheet = sh;
  550. fo->book = book;
  551. }
  552. /* }}} */
  553. /* {{{ proto ExcelSheet ExcelBook::copySheet(string name, int sheet_number)
  554. Copy an excel sheet. */
  555. EXCEL_METHOD(Book, copySheet)
  556. {
  557. BookHandle book;
  558. zval *object = getThis();
  559. SheetHandle sh;
  560. excel_sheet_object *fo;
  561. char *name;
  562. int name_len;
  563. long num;
  564. #ifdef LIBXL_VERSION
  565. SheetHandle osh;
  566. #endif
  567. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &name, &name_len, &num) == FAILURE) {
  568. RETURN_FALSE;
  569. }
  570. if (num < 0) {
  571. RETURN_FALSE;
  572. }
  573. BOOK_FROM_OBJECT(book, object);
  574. #ifdef LIBXL_VERSION
  575. if (!(osh = xlBookGetSheet(book, num))) {
  576. RETURN_FALSE;
  577. }
  578. sh = xlBookAddSheet(book, name, osh);
  579. #else
  580. sh = xlBookCopySheet(book, name, num);
  581. #endif
  582. if (!sh) {
  583. RETURN_FALSE;
  584. }
  585. Z_TYPE_P(return_value) = IS_OBJECT;
  586. object_init_ex(return_value, excel_ce_sheet);
  587. Z_SET_REFCOUNT_P(return_value, 1);
  588. Z_SET_ISREF_P(return_value);
  589. fo = (excel_sheet_object *) zend_object_store_get_object(return_value TSRMLS_CC);
  590. fo->sheet = sh;
  591. fo->book = book;
  592. }
  593. /* }}} */
  594. /* {{{ proto int ExcelBook::sheetCount()
  595. Get the number of sheets inside a file. */
  596. EXCEL_METHOD(Book, sheetCount)
  597. {
  598. BookHandle book;
  599. zval *object = getThis();
  600. if (ZEND_NUM_ARGS()) {
  601. RETURN_FALSE;
  602. }
  603. BOOK_FROM_OBJECT(book, object);
  604. RETURN_LONG(xlBookSheetCount(book));
  605. }
  606. /* }}} */
  607. /* {{{ proto string ExcelBook::getError()
  608. Get Excel error string. */
  609. EXCEL_METHOD(Book, getError)
  610. {
  611. BookHandle book;
  612. zval *object = getThis();
  613. char *err;
  614. if (ZEND_NUM_ARGS()) {
  615. RETURN_FALSE;
  616. }
  617. BOOK_FROM_OBJECT(book, object);
  618. err = (char *)xlBookErrorMessage(book);
  619. if (err) {
  620. if (!strcmp(err, "ok")) {
  621. RETURN_FALSE;
  622. } else {
  623. RETURN_STRING(err, 1);
  624. }
  625. } else {
  626. RETURN_STRING("Unknown Error", 1);
  627. }
  628. }
  629. /* }}} */
  630. /* {{{ proto ExcelFont ExcelBook::addFont([ExcelFont font])
  631. Add or Copy ExcelFont object. */
  632. EXCEL_METHOD(Book, addFont)
  633. {
  634. BookHandle book;
  635. zval *object = getThis();
  636. FontHandle nfont;
  637. FontHandle font = NULL;
  638. excel_font_object *fo;
  639. zval *fob = NULL;
  640. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &fob, excel_ce_font) == FAILURE) {
  641. RETURN_FALSE;
  642. }
  643. BOOK_FROM_OBJECT(book, object);
  644. if (fob) {
  645. FONT_FROM_OBJECT(font, fob);
  646. }
  647. nfont = xlBookAddFont(book, font);
  648. if (!nfont) {
  649. RETURN_FALSE;
  650. }
  651. Z_TYPE_P(return_value) = IS_OBJECT;
  652. object_init_ex(return_value, excel_ce_font);
  653. Z_SET_REFCOUNT_P(return_value, 1);
  654. Z_SET_ISREF_P(return_value);
  655. fo = (excel_font_object *) zend_object_store_get_object(return_value TSRMLS_CC);
  656. fo->font = nfont;
  657. fo->book = book;
  658. }
  659. /* }}} */
  660. /* {{{ proto ExcelFormat ExcelBook::addFormat([ExcelFormat format])
  661. Add or Copy ExcelFormat object. */
  662. EXCEL_METHOD(Book, addFormat)
  663. {
  664. BookHandle book;
  665. zval *object = getThis();
  666. FormatHandle nformat;
  667. FormatHandle format = NULL;
  668. excel_format_object *fo;
  669. zval *fob = NULL;
  670. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &fob, excel_ce_format) == FAILURE) {
  671. RETURN_FALSE;
  672. }
  673. BOOK_FROM_OBJECT(book, object);
  674. if (fob) {
  675. FORMAT_FROM_OBJECT(format, fob);
  676. }
  677. nformat = xlBookAddFormat(book, format);
  678. if (!nformat) {
  679. RETURN_FALSE;
  680. }
  681. Z_TYPE_P(return_value) = IS_OBJECT;
  682. object_init_ex(return_value, excel_ce_format);
  683. Z_SET_REFCOUNT_P(return_value, 1);
  684. Z_SET_ISREF_P(return_value);
  685. fo = (excel_format_object *) zend_object_store_get_object(return_value TSRMLS_CC);
  686. fo->format = nformat;
  687. fo->book = book;
  688. }
  689. /* }}} */
  690. #ifdef HAVE_LIBXL_243_PLUS
  691. /* {{{ proto array ExcelBook::getAllFormats()
  692. Get an array of all ExcelFormat objects used inside a document. */
  693. EXCEL_METHOD(Book, getAllFormats)
  694. {
  695. BookHandle book;
  696. zval *object = getThis();
  697. unsigned short fc;
  698. unsigned short c;
  699. if (ZEND_NUM_ARGS()) {
  700. RETURN_FALSE;
  701. }
  702. BOOK_FROM_OBJECT(book, object);
  703. array_init(return_value);
  704. fc = xlBookFormatSize(book);
  705. if (!fc) {
  706. return;
  707. }
  708. for (c = 0; c < fc; c++) {
  709. FormatHandle format;
  710. if ((format = xlBookFormat(book, c))) {
  711. excel_format_object *fo;
  712. zval *value;
  713. MAKE_STD_ZVAL(value);
  714. Z_TYPE_P(value) = IS_OBJECT;
  715. object_init_ex(value, excel_ce_format);
  716. Z_SET_REFCOUNT_P(value, 1);
  717. Z_SET_ISREF_P(value);
  718. fo = (excel_format_object *) zend_object_store_get_object(value TSRMLS_CC);
  719. fo->format = format;
  720. fo->book = book;
  721. add_next_index_zval(return_value, value);
  722. }
  723. }
  724. }
  725. /* }}} */
  726. #endif
  727. /* {{{ proto int ExcelBook::addCustomFormat(string format)
  728. Create a custom cell format */
  729. EXCEL_METHOD(Book, addCustomFormat)
  730. {
  731. BookHandle book;
  732. zval *object = getThis();
  733. char *format;
  734. int format_len;
  735. int id;
  736. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &format, &format_len) == FAILURE) {
  737. RETURN_FALSE;
  738. }
  739. if (!format_len) {
  740. RETURN_FALSE;
  741. }
  742. BOOK_FROM_OBJECT(book, object);
  743. if (!(id = xlBookAddCustomNumFormat(book, format))) {
  744. RETURN_FALSE;
  745. }
  746. RETURN_LONG(id);
  747. }
  748. /* }}} */
  749. /* {{{ proto string ExcelBook::getCustomFormat(int id)
  750. Get a custom cell format */
  751. EXCEL_METHOD(Book, getCustomFormat)
  752. {
  753. BookHandle book;
  754. zval *object = getThis();
  755. long id;
  756. char *data;
  757. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &id) == FAILURE) {
  758. RETURN_FALSE;
  759. }
  760. if (id < 1) {
  761. RETURN_FALSE;
  762. }
  763. BOOK_FROM_OBJECT(book, object);
  764. if (!(data = (char *)xlBookCustomNumFormat(book, id))) {
  765. RETURN_FALSE;
  766. }
  767. RETURN_STRING(data, 1);
  768. }
  769. /* }}} */
  770. static double _php_excel_date_pack(BookHandle book, long ts)
  771. {
  772. struct tm tm;
  773. if (!php_localtime_r(&ts, &tm)) {
  774. return -1;
  775. }
  776. tm.tm_year += 1900;
  777. tm.tm_mon += 1;
  778. return xlBookDatePack(book, tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec
  779. #ifdef HAVE_LIBXL_243_PLUS
  780. , 0
  781. #endif
  782. );
  783. }
  784. /* {{{ proto float ExcelBook::packDate(int timestamp)
  785. Pack a unix timestamp into an Excel Double */
  786. EXCEL_METHOD(Book, packDate)
  787. {
  788. BookHandle book;
  789. zval *object = getThis();
  790. long ts;
  791. double dt;
  792. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ts) == FAILURE) {
  793. RETURN_FALSE;
  794. }
  795. if (ts < 1) {
  796. RETURN_FALSE;
  797. }
  798. BOOK_FROM_OBJECT(book, object);
  799. if ((dt = _php_excel_date_pack(book, ts)) == -1) {
  800. RETURN_FALSE;
  801. }
  802. RETURN_DOUBLE(dt);
  803. }
  804. /* }}} */
  805. static double _php_excel_date_pack_values(BookHandle book, int year, int month, int day, int hour, int min, int sec)
  806. {
  807. return xlBookDatePack(book, year, month, day, hour, min, sec
  808. #ifdef HAVE_LIBXL_243_PLUS
  809. , 0
  810. #endif
  811. );
  812. }
  813. /* {{{ proto float ExcelBook::packDateValues(int year, int month, int day, int hour, int minute, int second)
  814. Pack a date by single values into an Excel Double */
  815. EXCEL_METHOD(Book, packDateValues)
  816. {
  817. BookHandle book;
  818. zval *object = getThis();
  819. long year, month, day, hour, min, sec;
  820. double dt;
  821. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "llllll", &year, &month, &day, &hour, &min, &sec) == FAILURE) {
  822. RETURN_FALSE;
  823. }
  824. // if it is a date or just a time - hour, min & sec must be checked
  825. if (hour < 0 || hour > 23) {
  826. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for hour", hour);
  827. RETURN_FALSE;
  828. }
  829. if (min < 0 || min > 59) {
  830. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for minute", min);
  831. RETURN_FALSE;
  832. }
  833. if (sec < 0 || sec > 59) {
  834. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for second", sec);
  835. RETURN_FALSE;
  836. }
  837. // check date only if there are values
  838. // is every value=0 - it's okay for generating a time
  839. if (year != 0 || month != 0 || day != 0) {
  840. if (year < 1) {
  841. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for year", year);
  842. RETURN_FALSE;
  843. }
  844. if (month < 1 || month > 12) {
  845. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for month", month);
  846. RETURN_FALSE;
  847. }
  848. if (day < 1 || day > 31) {
  849. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for day", day);
  850. RETURN_FALSE;
  851. }
  852. }
  853. BOOK_FROM_OBJECT(book, object);
  854. if ((dt = _php_excel_date_pack_values(book, year, month, day, hour, min, sec)) == -1) {
  855. RETURN_FALSE;
  856. }
  857. RETURN_DOUBLE(dt);
  858. }
  859. /* }}} */
  860. static long _php_excel_date_unpack(BookHandle book, double dt)
  861. {
  862. struct tm tm = {0};
  863. #ifdef HAVE_LIBXL_243_PLUS
  864. #if LIBXL_VERSION >= 0x03010000
  865. int msec;
  866. #else
  867. unsigned short msec;
  868. #endif
  869. #endif
  870. #if LIBXL_VERSION >= 0x03010000
  871. 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)
  872. #else
  873. if (!xlBookDateUnpack(book, dt, (short unsigned int *) &(tm.tm_year), (short unsigned int *) &(tm.tm_mon), (short unsigned int *) &(tm.tm_mday),
  874. (short unsigned int *) &(tm.tm_hour), (short unsigned int *) &(tm.tm_min), (short unsigned int *) &(tm.tm_sec)
  875. #endif
  876. #ifdef HAVE_LIBXL_243_PLUS
  877. , &msec
  878. #endif
  879. )) {
  880. return -1;
  881. }
  882. tm.tm_year -= 1900;
  883. tm.tm_mon -= 1;
  884. tm.tm_isdst = -1;
  885. return mktime(&tm);
  886. }
  887. /* {{{ proto int ExcelBook::unpackDate(double date)
  888. Unpack a unix timestamp from an Excel Double */
  889. EXCEL_METHOD(Book, unpackDate)
  890. {
  891. BookHandle book;
  892. zval *object = getThis();
  893. double dt;
  894. time_t t;
  895. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "d", &dt) == FAILURE) {
  896. RETURN_FALSE;
  897. }
  898. if (dt < 1) {
  899. RETURN_FALSE;
  900. }
  901. BOOK_FROM_OBJECT(book, object);
  902. if ((t = _php_excel_date_unpack(book, dt)) == -1) {
  903. RETURN_FALSE;
  904. }
  905. RETURN_LONG(t);
  906. }
  907. /* }}} */
  908. #if LIBXL_VERSION >= 0x03050300
  909. /* {{{ proto bool ExcelBook::isDate1904()
  910. Returns whether the 1904 date system is active: true - 1904 date system, false - 1900 date system */
  911. EXCEL_METHOD(Book, isDate1904)
  912. {
  913. BookHandle book;
  914. zval *object = getThis();
  915. if (ZEND_NUM_ARGS()) {
  916. RETURN_FALSE;
  917. }
  918. BOOK_FROM_OBJECT(book, object);
  919. RETURN_BOOL(xlBookIsDate1904(book));
  920. }
  921. /* }}} */
  922. /* {{{ proto bool ExcelBook::setDate1904(bool date_type)
  923. Sets the date system mode: true - 1904 date system, false - 1900 date system (default) */
  924. EXCEL_METHOD(Book, setDate1904)
  925. {
  926. BookHandle book;
  927. zval *object = getThis();
  928. zend_bool date_type;
  929. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &date_type) == FAILURE) {
  930. RETURN_FALSE;
  931. }
  932. BOOK_FROM_OBJECT(book, object);
  933. xlBookSetDate1904(book, (int)date_type);
  934. RETURN_TRUE;
  935. }
  936. /* }}} */
  937. #endif
  938. /* {{{ proto int ExcelBook::getActiveSheet()
  939. Get the active sheet inside a file. */
  940. EXCEL_METHOD(Book, getActiveSheet)
  941. {
  942. BookHandle book;
  943. zval *object = getThis();
  944. if (ZEND_NUM_ARGS()) {
  945. RETURN_FALSE;
  946. }
  947. BOOK_FROM_OBJECT(book, object);
  948. RETURN_LONG(xlBookActiveSheet(book));
  949. }
  950. /* }}} */
  951. /* {{{ proto array ExcelBook::getDefaultFont()
  952. Get the default font. */
  953. EXCEL_METHOD(Book, getDefaultFont)
  954. {
  955. BookHandle book;
  956. zval *object = getThis();
  957. const char *font;
  958. #if LIBXL_VERSION >= 0x03010000
  959. int font_size;
  960. #else
  961. unsigned short font_size;
  962. #endif
  963. if (ZEND_NUM_ARGS()) {
  964. RETURN_FALSE;
  965. }
  966. BOOK_FROM_OBJECT(book, object);
  967. if (!(font = xlBookDefaultFont(book, &font_size))) {
  968. RETURN_FALSE;
  969. }
  970. array_init(return_value);
  971. add_assoc_string(return_value, "font", (char *)font, 1);
  972. add_assoc_long(return_value, "font_size", font_size);
  973. }
  974. /* }}} */
  975. /* {{{ proto void ExcelBook::setDefaultFont(string font, int font_size)
  976. Set the default font, and size. */
  977. EXCEL_METHOD(Book, setDefaultFont)
  978. {
  979. BookHandle book;
  980. zval *object = getThis();
  981. char *font;
  982. int font_len;
  983. long font_size;
  984. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl", &font, &font_len, &font_size) == FAILURE || font_size < 1) {
  985. RETURN_FALSE;
  986. }
  987. BOOK_FROM_OBJECT(book, object);
  988. xlBookSetDefaultFont(book, font, (int)font_size);
  989. }
  990. /* }}} */
  991. /* {{{ proto void ExcelBook::setLocale(string locale)
  992. Set the locale. */
  993. EXCEL_METHOD(Book, setLocale)
  994. {
  995. BookHandle book;
  996. zval *object = getThis();
  997. char *locale;
  998. int locale_len;
  999. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &locale, &locale_len) == FAILURE || locale_len < 1) {
  1000. RETURN_FALSE;
  1001. }
  1002. BOOK_FROM_OBJECT(book, object);
  1003. xlBookSetLocale(book, locale);
  1004. }
  1005. /* }}} */
  1006. /* {{{ proto ExcelBook ExcelBook::__construct([string license_name, string license_key [, bool excel_2007 = false]])
  1007. Book Constructor. */
  1008. EXCEL_METHOD(Book, __construct)
  1009. {
  1010. BookHandle book;
  1011. zval *object = getThis();
  1012. char *name = NULL, *key;
  1013. int name_len = 0, key_len = 0;
  1014. #if LIBXL_VERSION <= 0x03010000
  1015. wchar_t *nw, *kw;
  1016. size_t nw_l, kw_l;
  1017. #endif
  1018. #if defined(HAVE_XML) && defined(EXCEL_WITH_LIBXML)
  1019. char *namep, *keyp;
  1020. int plen;
  1021. #endif
  1022. #ifdef LIBXL_VERSION
  1023. zend_bool new_excel = 0;
  1024. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ssb", &name, &name_len, &key, &key_len, &new_excel) == FAILURE) {
  1025. RETURN_FALSE;
  1026. }
  1027. #else
  1028. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &name, &name_len, &key, &key_len) == FAILURE) {
  1029. RETURN_FALSE;
  1030. }
  1031. #endif
  1032. #if defined(HAVE_LIBXL_SETKEY)
  1033. if (!name_len) {
  1034. if (INI_STR("excel.license_name") && INI_STR("excel.license_key")) {
  1035. name = INI_STR("excel.license_name");
  1036. name_len = strlen(name);
  1037. key = INI_STR("excel.license_key");
  1038. key_len = strlen(key);
  1039. } else {
  1040. #ifndef LIBXL_VERSION
  1041. return;
  1042. #endif
  1043. }
  1044. }
  1045. #endif
  1046. BOOK_FROM_OBJECT(book, object);
  1047. #ifdef LIBXL_VERSION
  1048. if (new_excel) {
  1049. excel_book_object *obj = (excel_book_object*) zend_object_store_get_object(object TSRMLS_CC);
  1050. if ((book = xlCreateXMLBook())) {
  1051. xlBookRelease(obj->book);
  1052. obj->book = book;
  1053. } else {
  1054. RETURN_FALSE;
  1055. }
  1056. #if !defined(HAVE_LIBXL_SETKEY)
  1057. return;
  1058. #endif
  1059. if (!name_len && !key_len) {
  1060. return;
  1061. }
  1062. }
  1063. #endif
  1064. if (!name_len || !key_len) {
  1065. RETURN_FALSE;
  1066. }
  1067. #if LIBXL_VERSION <= 0x03010000
  1068. if (!(nw = _php_excel_to_wide(name, name_len + 1, &nw_l))) {
  1069. RETURN_FALSE;
  1070. }
  1071. if (!(kw = _php_excel_to_wide(key, key_len + 1, &kw_l))) {
  1072. efree(nw);
  1073. RETURN_FALSE;
  1074. }
  1075. xlBookSetKey(book, nw, kw);
  1076. efree(nw);
  1077. efree(kw);
  1078. #else
  1079. #if defined(HAVE_XML) && defined(EXCEL_WITH_LIBXML)
  1080. namep = xml_utf8_decode((const XML_Char *) name, name_len, &plen, (const XML_Char *)"ISO-8859-1");
  1081. keyp = xml_utf8_decode((const XML_Char *) key, key_len, &plen, (const XML_Char *)"ISO-8859-1");
  1082. xlBookSetKey(book, namep, keyp);
  1083. efree(namep);
  1084. efree(keyp);
  1085. #else
  1086. xlBookSetKey(book, name, key);
  1087. #endif
  1088. #endif
  1089. }
  1090. /* }}} */
  1091. /* {{{ proto bool ExcelBook::setActiveSheet(int sheet)
  1092. Set the sheet active. */
  1093. EXCEL_METHOD(Book, setActiveSheet)
  1094. {
  1095. BookHandle book;
  1096. zval *object = getThis();
  1097. long id;
  1098. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &id) == FAILURE || id < 0) {
  1099. RETURN_FALSE;
  1100. }
  1101. BOOK_FROM_OBJECT(book, object);
  1102. xlBookSetActiveSheet(book, id);
  1103. RETURN_BOOL(id == xlBookActiveSheet(book));
  1104. }
  1105. /* }}} */
  1106. static void php_excel_add_picture(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
  1107. {
  1108. char *data;
  1109. int data_len;
  1110. BookHandle book;
  1111. zval *object = getThis();
  1112. int ret;
  1113. BOOK_FROM_OBJECT(book, object);
  1114. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &data_len) == FAILURE) {
  1115. RETURN_FALSE;
  1116. }
  1117. if (mode == 1) {
  1118. ret = xlBookAddPicture2(book, data, data_len);
  1119. } else {
  1120. php_stream *stream = php_stream_open_wrapper(data, "rb", ENFORCE_SAFE_MODE | REPORT_ERRORS, NULL);
  1121. int len;
  1122. char *contents;
  1123. if (!stream) {
  1124. RETURN_FALSE;
  1125. }
  1126. len = php_stream_copy_to_mem(stream, &contents, PHP_STREAM_COPY_ALL, 0);
  1127. php_stream_close(stream);
  1128. if (len < 1) {
  1129. RETURN_FALSE;
  1130. }
  1131. ret = xlBookAddPicture2(book, contents, len);
  1132. efree(contents);
  1133. }
  1134. if (ret == -1) {
  1135. RETURN_FALSE;
  1136. } else {
  1137. #if LIBXL_VERSION >= 0x03020200 && LIBXL_VERSION < 0x03020300
  1138. /* work-around for a bug inside libxl 3.2.2 */
  1139. ret -= 1;
  1140. #endif
  1141. RETURN_LONG(ret);
  1142. }
  1143. }
  1144. /* {{{ proto int ExcelBook::addPictureFromFile(string filename)
  1145. Add picture from file. */
  1146. EXCEL_METHOD(Book, addPictureFromFile)
  1147. {
  1148. php_excel_add_picture(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  1149. }
  1150. /* }}} */
  1151. /* {{{ proto int ExcelBook::addPictureFromString(string data)
  1152. Add picture from string. */
  1153. EXCEL_METHOD(Book, addPictureFromString)
  1154. {
  1155. php_excel_add_picture(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  1156. }
  1157. /* }}} */
  1158. #ifdef LIBXL_VERSION
  1159. /* {{{ proto bool ExcelBook::rgbMode()
  1160. Returns whether the RGB mode is active. */
  1161. EXCEL_METHOD(Book, rgbMode)
  1162. {
  1163. BookHandle book;
  1164. zval *object = getThis();
  1165. if (ZEND_NUM_ARGS()) {
  1166. RETURN_FALSE;
  1167. }
  1168. BOOK_FROM_OBJECT(book, object);
  1169. RETURN_BOOL(xlBookRgbMode(book));
  1170. }
  1171. /* }}} */
  1172. /* {{{ proto void ExcelBook::setRGBMode(bool mode)
  1173. Sets a RGB mode on or off. */
  1174. EXCEL_METHOD(Book, setRGBMode)
  1175. {
  1176. BookHandle book;
  1177. zval *object = getThis();
  1178. zend_bool val;
  1179. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &val) == FAILURE) {
  1180. RETURN_FALSE;
  1181. }
  1182. BOOK_FROM_OBJECT(book, object);
  1183. xlBookSetRgbMode(book, val);
  1184. }
  1185. /* }}} */
  1186. /* {{{ proto int ExcelBook::colorPack(int r, int g, int b)
  1187. Packs red, green and blue components in color value. Used for xlsx format only. */
  1188. EXCEL_METHOD(Book, colorPack)
  1189. {
  1190. BookHandle book;
  1191. zval *object = getThis();
  1192. long r, g, b;
  1193. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lll", &r, &g, &b) == FAILURE) {
  1194. RETURN_FALSE;
  1195. }
  1196. if (r < 0 || r > 255) {
  1197. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for color red", r);
  1198. RETURN_FALSE;
  1199. } else if (g < 0 || g > 255) {
  1200. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for color green", g);
  1201. RETURN_FALSE;
  1202. } else if (b < 0 || b > 255) {
  1203. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for color blue", b);
  1204. RETURN_FALSE;
  1205. }
  1206. BOOK_FROM_OBJECT(book, object);
  1207. RETURN_LONG(xlBookColorPack(book, (unsigned short)r, (unsigned short)g, (unsigned short)b));
  1208. }
  1209. /* }}} */
  1210. /* {{{ proto array ExcelBook::colorUnpack(int color)
  1211. Unpacks color value to red, green and blue components. Used for xlsx format only. */
  1212. EXCEL_METHOD(Book, colorUnpack)
  1213. {
  1214. BookHandle book;
  1215. zval *object = getThis();
  1216. #if LIBXL_VERSION >= 0x03010000
  1217. int r, g, b;
  1218. #else
  1219. unsigned short r, g, b;
  1220. #endif
  1221. long color;
  1222. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &color) == FAILURE) {
  1223. RETURN_FALSE;
  1224. }
  1225. if (color <= 0) {
  1226. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid '%ld' value for color code", color);
  1227. RETURN_FALSE;
  1228. }
  1229. BOOK_FROM_OBJECT(book, object);
  1230. xlBookColorUnpack(book, (int)color, &r, &g, &b);
  1231. array_init(return_value);
  1232. add_assoc_long(return_value, "red", r);
  1233. add_assoc_long(return_value, "green", g);
  1234. add_assoc_long(return_value, "blue", b);
  1235. }
  1236. /* }}} */
  1237. #endif
  1238. /* {{{ proto int ExcelFont::size([int size])
  1239. Get or set the font size */
  1240. EXCEL_METHOD(Font, size)
  1241. {
  1242. zval *object = getThis();
  1243. FontHandle font;
  1244. long size = -1;
  1245. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &size) == FAILURE) {
  1246. RETURN_FALSE;
  1247. }
  1248. FONT_FROM_OBJECT(font, object);
  1249. if (size > 0) {
  1250. xlFontSetSize(font, size);
  1251. }
  1252. RETURN_LONG(xlFontSize(font));
  1253. }
  1254. /* }}} */
  1255. /* {{{ proto bool ExcelFont::italics([bool italics])
  1256. Get or set the if italics are enabled */
  1257. EXCEL_METHOD(Font, italics)
  1258. {
  1259. zval *object = getThis();
  1260. FontHandle font;
  1261. zend_bool italics;
  1262. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &italics) == FAILURE) {
  1263. RETURN_FALSE;
  1264. }
  1265. FONT_FROM_OBJECT(font, object);
  1266. if (ZEND_NUM_ARGS()) {
  1267. xlFontSetItalic(font, italics);
  1268. }
  1269. RETURN_BOOL(xlFontItalic(font));
  1270. }
  1271. /* }}} */
  1272. /* {{{ proto bool ExcelFont::strike([bool strike])
  1273. Get or set the font strike-through */
  1274. EXCEL_METHOD(Font, strike)
  1275. {
  1276. zval *object = getThis();
  1277. FontHandle font;
  1278. zend_bool strike;
  1279. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &strike) == FAILURE) {
  1280. RETURN_FALSE;
  1281. }
  1282. FONT_FROM_OBJECT(font, object);
  1283. if (ZEND_NUM_ARGS()) {
  1284. xlFontSetStrikeOut(font, strike);
  1285. }
  1286. RETURN_BOOL(xlFontStrikeOut(font));
  1287. }
  1288. /* }}} */
  1289. /* {{{ proto bool ExcelFont::bold([bool bold])
  1290. Get or set the font bold */
  1291. EXCEL_METHOD(Font, bold)
  1292. {
  1293. zval *object = getThis();
  1294. FontHandle font;
  1295. zend_bool bold;
  1296. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &bold) == FAILURE) {
  1297. RETURN_FALSE;
  1298. }
  1299. FONT_FROM_OBJECT(font, object);
  1300. if (ZEND_NUM_ARGS()) {
  1301. xlFontSetBold(font, bold);
  1302. }
  1303. RETURN_BOOL(xlFontBold(font));
  1304. }
  1305. /* }}} */
  1306. /* {{{ proto int ExcelFont::color([int color])
  1307. Get or set the font color */
  1308. EXCEL_METHOD(Font, color)
  1309. {
  1310. zval *object = getThis();
  1311. FontHandle font;
  1312. long color;
  1313. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &color) == FAILURE) {
  1314. RETURN_FALSE;
  1315. }
  1316. FONT_FROM_OBJECT(font, object);
  1317. if (ZEND_NUM_ARGS()) {
  1318. xlFontSetColor(font, color);
  1319. }
  1320. RETURN_LONG(xlFontColor(font));
  1321. }
  1322. /* }}} */
  1323. /* {{{ proto int ExcelFont::mode([int mode])
  1324. Get or set the font mode */
  1325. EXCEL_METHOD(Font, mode)
  1326. {
  1327. zval *object = getThis();
  1328. FontHandle font;
  1329. long mode;
  1330. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &mode) == FAILURE) {
  1331. RETURN_FALSE;
  1332. }
  1333. FONT_FROM_OBJECT(font, object);
  1334. if (ZEND_NUM_ARGS()) {
  1335. xlFontSetScript(font, mode);
  1336. }
  1337. RETURN_LONG(xlFontScript(font));
  1338. }
  1339. /* }}} */
  1340. /* {{{ proto int ExcelFont::underline([int underline_style])
  1341. Get or set the font underline style */
  1342. EXCEL_METHOD(Font, underline)
  1343. {
  1344. zval *object = getThis();
  1345. FontHandle font;
  1346. long underline;
  1347. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &underline) == FAILURE) {
  1348. RETURN_FALSE;
  1349. }
  1350. FONT_FROM_OBJECT(font, object);
  1351. if (ZEND_NUM_ARGS()) {
  1352. xlFontSetUnderline(font, underline);
  1353. }
  1354. RETURN_LONG(xlFontUnderline(font));
  1355. }
  1356. /* }}} */
  1357. /* {{{ proto string ExcelFont::name([string name])
  1358. Get or set the font name */
  1359. EXCEL_METHOD(Font, name)
  1360. {
  1361. zval *object = getThis();
  1362. FontHandle font;
  1363. char *name = NULL;
  1364. int name_len;
  1365. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &name, &name_len) == FAILURE) {
  1366. RETURN_FALSE;
  1367. }
  1368. FONT_FROM_OBJECT(font, object);
  1369. if (name) {
  1370. xlFontSetName(font, name);
  1371. }
  1372. RETURN_STRING((char *)xlFontName(font), 1);
  1373. }
  1374. /* }}} */
  1375. /* {{{ proto ExcelFormat ExcelFormat::__construct(ExcelBook book)
  1376. Format Constructor. */
  1377. EXCEL_METHOD(Format, __construct)
  1378. {
  1379. BookHandle book;
  1380. FormatHandle format;
  1381. zval *object = getThis();
  1382. excel_format_object *obj;
  1383. zval *zbook;
  1384. PHP_EXCEL_ERROR_HANDLING();
  1385. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zbook, excel_ce_book) == FAILURE) {
  1386. PHP_EXCEL_RESTORE_ERRORS();
  1387. return;
  1388. }
  1389. PHP_EXCEL_RESTORE_ERRORS();
  1390. BOOK_FROM_OBJECT(book, zbook);
  1391. obj = (excel_format_object*) zend_object_store_get_object(object TSRMLS_CC);
  1392. format = xlBookAddFormat(book, NULL);
  1393. if (!format) {
  1394. RETURN_FALSE;
  1395. }
  1396. obj->format = format;
  1397. obj->book = book;
  1398. }
  1399. /* }}} */
  1400. /* {{{ proto ExcelFont ExcelFont::__construct(ExcelBook book)
  1401. Font Constructor. */
  1402. EXCEL_METHOD(Font, __construct)
  1403. {
  1404. BookHandle book;
  1405. FontHandle font;
  1406. zval *object = getThis();
  1407. excel_font_object *obj;
  1408. zval *zbook;
  1409. PHP_EXCEL_ERROR_HANDLING();
  1410. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zbook, excel_ce_book) == FAILURE) {
  1411. PHP_EXCEL_RESTORE_ERRORS();
  1412. return;
  1413. }
  1414. PHP_EXCEL_RESTORE_ERRORS();
  1415. BOOK_FROM_OBJECT(book, zbook);
  1416. obj = (excel_font_object*) zend_object_store_get_object(object TSRMLS_CC);
  1417. font = xlBookAddFont(book, NULL);
  1418. if (!font) {
  1419. RETURN_FALSE;
  1420. }
  1421. obj->font = font;
  1422. obj->book = book;
  1423. }
  1424. /* }}} */
  1425. /* {{{ proto bool ExcelFormat::setFont(ExcelFont font)
  1426. Set the font for a format. */
  1427. EXCEL_METHOD(Format, setFont)
  1428. {
  1429. FormatHandle format;
  1430. zval *object = getThis();
  1431. FontHandle font;
  1432. zval *zfont;
  1433. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zfont, excel_ce_font) == FAILURE) {
  1434. RETURN_FALSE;
  1435. }
  1436. FORMAT_FROM_OBJECT(format, object);
  1437. FONT_FROM_OBJECT(font, zfont);
  1438. if (!xlFormatSetFont(format, font)) {
  1439. RETURN_FALSE;
  1440. }
  1441. RETURN_TRUE;
  1442. }
  1443. /* }}} */
  1444. /* {{{ proto ExcelFont ExcelFormat::getFont()
  1445. Get the font for this format. */
  1446. EXCEL_METHOD(Format, getFont)
  1447. {
  1448. FormatHandle format;
  1449. zval *object = getThis();
  1450. FontHandle font;
  1451. excel_font_object *fo;
  1452. excel_format_object *obj = (excel_format_object*) zend_object_store_get_object(object TSRMLS_CC);
  1453. format = obj->format;
  1454. if (!format) {
  1455. php_error_docref(NULL TSRMLS_CC, E_WARNING, "The format wasn't initialized");
  1456. RETURN_FALSE;
  1457. }
  1458. if (ZEND_NUM_ARGS()) {
  1459. RETURN_FALSE;
  1460. }
  1461. FORMAT_FROM_OBJECT(format, object);
  1462. font = xlFormatFont(format);
  1463. if (!font) {
  1464. RETURN_FALSE;
  1465. }
  1466. Z_TYPE_P(return_value) = IS_OBJECT;
  1467. object_init_ex(return_value, excel_ce_font);
  1468. Z_SET_REFCOUNT_P(return_value, 1);
  1469. Z_SET_ISREF_P(return_value);
  1470. fo = (excel_font_object *) zend_object_store_get_object(return_value TSRMLS_CC);
  1471. fo->font = font;
  1472. fo->book = obj->book;
  1473. }
  1474. /* }}} */
  1475. #define PHP_EXCEL_LONG_FORMAT_OPTION(func_name, write_only) \
  1476. { \
  1477. FormatHandle format; \
  1478. zval *object = getThis(); \
  1479. long data; \
  1480. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &data) == FAILURE) { \
  1481. RETURN_FALSE; \
  1482. } \
  1483. FORMAT_FROM_OBJECT(format, object); \
  1484. if (ZEND_NUM_ARGS()) { \
  1485. xlFormatSet ## func_name (format, data); \
  1486. } \
  1487. if (!write_only) { \
  1488. RETURN_LONG(xlFormat ## func_name (format)); \
  1489. } else { \
  1490. RETURN_TRUE; \
  1491. } \
  1492. }
  1493. #define PHP_EXCEL_BOOL_FORMAT_OPTION(func_name) \
  1494. { \
  1495. FormatHandle format; \
  1496. zval *object = getThis(); \
  1497. zend_bool data; \
  1498. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &data) == FAILURE) { \
  1499. RETURN_FALSE; \
  1500. } \
  1501. FORMAT_FROM_OBJECT(format, object); \
  1502. if (ZEND_NUM_ARGS()) { \
  1503. xlFormatSet ## func_name (format, data); \
  1504. } \
  1505. RETURN_BOOL(xlFormat ## func_name (format)); \
  1506. }
  1507. /* {{{ proto int ExcelFormat::numberFormat([int format])
  1508. Get or set the cell number format */
  1509. EXCEL_METHOD(Format, numberFormat)
  1510. {
  1511. PHP_EXCEL_LONG_FORMAT_OPTION(NumFormat, 0);
  1512. }
  1513. /* }}} */
  1514. /* {{{ proto int ExcelFormat::horizontalAlign([int align_mode])
  1515. Get or set the cell horizontal alignment */
  1516. EXCEL_METHOD(Format, horizontalAlign)
  1517. {
  1518. PHP_EXCEL_LONG_FORMAT_OPTION(AlignH, 0);
  1519. }
  1520. /* }}} */
  1521. /* {{{ proto int ExcelFormat::verticalAlign([int align_mode])
  1522. Get or set the cell vertical alignment */
  1523. EXCEL_METHOD(Format, verticalAlign)
  1524. {
  1525. PHP_EXCEL_LONG_FORMAT_OPTION(AlignV, 0);
  1526. }
  1527. /* }}} */
  1528. /* {{{ proto bool ExcelFormat::wrap([bool wrap])
  1529. Get or set the cell wrapping */
  1530. EXCEL_METHOD(Format, wrap)
  1531. {
  1532. PHP_EXCEL_BOOL_FORMAT_OPTION(Wrap);
  1533. }
  1534. /* }}} */
  1535. /* {{{ proto int ExcelFormat::rotate([int angle])
  1536. Get or set the cell data rotation */
  1537. EXCEL_METHOD(Format, rotate)
  1538. {
  1539. FormatHandle format;
  1540. zval *object = getThis();
  1541. long angle;
  1542. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &angle) == FAILURE) {
  1543. RETURN_FALSE;
  1544. }
  1545. FORMAT_FROM_OBJECT(format, object);
  1546. if (ZEND_NUM_ARGS()) {
  1547. if (angle < 0 || (angle > 180 && angle != 255)) {
  1548. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Rotation can be a number between 0 and 180 or 255");
  1549. RETURN_FALSE;
  1550. }
  1551. xlFormatSetRotation(format, angle);
  1552. }
  1553. RETURN_LONG(xlFormatRotation(format));
  1554. }
  1555. /* }}} */
  1556. /* {{{ proto int ExcelFormat::indent([int indent])
  1557. Get or set the cell text indentation level */
  1558. EXCEL_METHOD(Format, indent)
  1559. {
  1560. FormatHandle format;
  1561. zval *object = getThis();
  1562. long indent;
  1563. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &indent) == FAILURE) {
  1564. RETURN_FALSE;
  1565. }
  1566. FORMAT_FROM_OBJECT(format, object);
  1567. if (ZEND_NUM_ARGS()) {
  1568. if (indent < 0 || indent > 15) {
  1569. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Text indentation level must be less than or equal to 15");
  1570. RETURN_FALSE;
  1571. }
  1572. xlFormatSetIndent(format, indent);
  1573. }
  1574. RETURN_LONG(xlFormatIndent(format));
  1575. }
  1576. /* }}} */
  1577. /* {{{ proto bool ExcelFormat::shrinkToFit([bool shrink])
  1578. Get or set whether the cell is shrink-to-fit */
  1579. EXCEL_METHOD(Format, shrinkToFit)
  1580. {
  1581. PHP_EXCEL_BOOL_FORMAT_OPTION(ShrinkToFit);
  1582. }
  1583. /* }}} */
  1584. /* {{{ proto int ExcelFormat::borderStyle([int style])
  1585. Get or set the cell border */
  1586. EXCEL_METHOD(Format, borderStyle)
  1587. {
  1588. PHP_EXCEL_LONG_FORMAT_OPTION(Border, 1);
  1589. }
  1590. /* }}} */
  1591. /* {{{ proto int ExcelFormat::borderColor([int color])
  1592. Get or set the cell color */
  1593. EXCEL_METHOD(Format, borderColor)
  1594. {
  1595. PHP_EXCEL_LONG_FORMAT_OPTION(BorderColor, 1);
  1596. }
  1597. /* }}} */
  1598. /* {{{ proto int ExcelFormat::borderLeftStyle([int style])
  1599. Get or set the cell left border */
  1600. EXCEL_METHOD(Format, borderLeftStyle)
  1601. {
  1602. PHP_EXCEL_LONG_FORMAT_OPTION(BorderLeft, 0);
  1603. }
  1604. /* }}} */
  1605. /* {{{ proto int ExcelFormat::borderLeftColor([int color])
  1606. Get or set the cell left color */
  1607. EXCEL_METHOD(Format, borderLeftColor)
  1608. {
  1609. PHP_EXCEL_LONG_FORMAT_OPTION(BorderLeftColor, 0);
  1610. }
  1611. /* }}} */
  1612. /* {{{ proto int ExcelFormat::borderRightStyle([int style])
  1613. Get or set the cell right border */
  1614. EXCEL_METHOD(Format, borderRightStyle)
  1615. {
  1616. PHP_EXCEL_LONG_FORMAT_OPTION(BorderRight, 0);
  1617. }
  1618. /* }}} */
  1619. /* {{{ proto int ExcelFormat::borderRightColor([int color])
  1620. Get or set the cell right color */
  1621. EXCEL_METHOD(Format, borderRightColor)
  1622. {
  1623. PHP_EXCEL_LONG_FORMAT_OPTION(BorderRightColor, 0);
  1624. }
  1625. /* }}} */
  1626. /* {{{ proto int ExcelFormat::borderTopStyle([int style])
  1627. Get or set the cell top border */
  1628. EXCEL_METHOD(Format, borderTopStyle)
  1629. {
  1630. PHP_EXCEL_LONG_FORMAT_OPTION(BorderTop, 0);
  1631. }
  1632. /* }}} */
  1633. /* {{{ proto int ExcelFormat::borderTopColor([int color])
  1634. Get or set the cell top color */
  1635. EXCEL_METHOD(Format, borderTopColor)
  1636. {
  1637. PHP_EXCEL_LONG_FORMAT_OPTION(BorderTopColor, 0);
  1638. }
  1639. /* }}} */
  1640. /* {{{ proto int ExcelFormat::borderBottomStyle([int style])
  1641. Get or set the cell bottom border */
  1642. EXCEL_METHOD(Format, borderBottomStyle)
  1643. {
  1644. PHP_EXCEL_LONG_FORMAT_OPTION(BorderBottom, 0);
  1645. }
  1646. /* }}} */
  1647. /* {{{ proto int ExcelFormat::borderBottomColor([int color])
  1648. Get or set the cell bottom color */
  1649. EXCEL_METHOD(Format, borderBottomColor)
  1650. {
  1651. PHP_EXCEL_LONG_FORMAT_OPTION(BorderBottomColor, 0);
  1652. }
  1653. /* }}} */
  1654. /* {{{ proto int ExcelFormat::borderDiagonalStyle([int style])
  1655. Get or set the cell diagonal border */
  1656. EXCEL_METHOD(Format, borderDiagonalStyle)
  1657. {
  1658. PHP_EXCEL_LONG_FORMAT_OPTION(BorderDiagonal, 0);
  1659. }
  1660. /* }}} */
  1661. /* {{{ proto int ExcelFormat::borderDiagonalColor([int color])
  1662. Get or set the cell diagonal color */
  1663. EXCEL_METHOD(Format, borderDiagonalColor)
  1664. {
  1665. PHP_EXCEL_LONG_FORMAT_OPTION(BorderDiagonalColor, 0);
  1666. }
  1667. /* }}} */
  1668. /* {{{ proto int ExcelFormat::fillPattern([int patern])
  1669. Get or set the cell fill pattern */
  1670. EXCEL_METHOD(Format, fillPattern)
  1671. {
  1672. PHP_EXCEL_LONG_FORMAT_OPTION(FillPattern, 0);
  1673. }
  1674. /* }}} */
  1675. /* {{{ proto int ExcelFormat::patternForegroundColor([int color])
  1676. Get or set the cell pattern foreground color */
  1677. EXCEL_METHOD(Format, patternForegroundColor)
  1678. {
  1679. PHP_EXCEL_LONG_FORMAT_OPTION(PatternForegroundColor, 0);
  1680. }
  1681. /* }}} */
  1682. /* {{{ proto int ExcelFormat::patternBackgroundColor([int color])
  1683. Get or set the cell pattern background color */
  1684. EXCEL_METHOD(Format, patternBackgroundColor)
  1685. {
  1686. PHP_EXCEL_LONG_FORMAT_OPTION(PatternBackgroundColor, 0);
  1687. }
  1688. /* }}} */
  1689. /* {{{ proto bool ExcelFormat::locked([bool locked])
  1690. Get or set whether the cell is locked */
  1691. EXCEL_METHOD(Format, locked)
  1692. {
  1693. PHP_EXCEL_BOOL_FORMAT_OPTION(Locked);
  1694. }
  1695. /* }}} */
  1696. /* {{{ proto bool ExcelFormat::hidden([bool hidden])
  1697. Get or set whether the cell is hidden */
  1698. EXCEL_METHOD(Format, hidden)
  1699. {
  1700. PHP_EXCEL_BOOL_FORMAT_OPTION(Hidden);
  1701. }
  1702. /* }}} */
  1703. /* {{{ proto ExcelSheet ExcelSheet::__construct(ExcelBook book, string name)
  1704. Sheet Constructor. */
  1705. EXCEL_METHOD(Sheet, __construct)
  1706. {
  1707. BookHandle book;
  1708. SheetHandle sh;
  1709. zval *object = getThis();
  1710. excel_sheet_object *obj;
  1711. zval *zbook;
  1712. char *name;
  1713. int name_len;
  1714. PHP_EXCEL_ERROR_HANDLING();
  1715. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Os", &zbook, excel_ce_book, &name, &name_len) == FAILURE) {
  1716. PHP_EXCEL_RESTORE_ERRORS();
  1717. return;
  1718. }
  1719. PHP_EXCEL_RESTORE_ERRORS();
  1720. BOOK_FROM_OBJECT(book, zbook);
  1721. obj = (excel_sheet_object*) zend_object_store_get_object(object TSRMLS_CC);
  1722. #ifdef LIBXL_VERSION
  1723. sh = xlBookAddSheet(book, name, 0);
  1724. #else
  1725. sh = xlBookAddSheet(book, name);
  1726. #endif
  1727. if (!sh) {
  1728. RETURN_FALSE;
  1729. }
  1730. obj->sheet = sh;
  1731. obj->book = book;
  1732. }
  1733. /* }}} */
  1734. /* {{{ proto int ExcelSheet::cellType(int row, int column)
  1735. Get cell type */
  1736. EXCEL_METHOD(Sheet, cellType)
  1737. {
  1738. zval *object = getThis();
  1739. SheetHandle sheet;
  1740. long row, col;
  1741. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &row, &col) == FAILURE) {
  1742. RETURN_FALSE;
  1743. }
  1744. SHEET_FROM_OBJECT(sheet, object);
  1745. RETURN_LONG(xlSheetCellType(sheet,