typeconversion.cpp 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <kopano/platform.h>
  18. #include <cmath>
  19. #include <mapiutil.h>
  20. #include "php-ext/phpconfig.h"
  21. using namespace std;
  22. extern "C" {
  23. // Remove these defines to remove warnings
  24. #undef PACKAGE_VERSION
  25. #undef PACKAGE_TARNAME
  26. #undef PACKAGE_NAME
  27. #undef PACKAGE_STRING
  28. #undef PACKAGE_BUGREPORT
  29. #if !__GNUC_PREREQ(6,0)
  30. #define zend_isnan(a) std::isnan(a)
  31. #endif
  32. #include "php.h"
  33. #include "php_globals.h"
  34. #include "ext/standard/info.h"
  35. #include "ext/standard/php_string.h"
  36. #ifdef ZTS
  37. void*** tsrm_ls;
  38. #endif
  39. }
  40. #undef inline
  41. #include <mapi.h>
  42. #include <mapix.h>
  43. #include <mapidefs.h>
  44. #include <mapitags.h>
  45. #include <mapicode.h>
  46. #include <edkmdb.h>
  47. #include "typeconversion.h"
  48. #include <kopano/charset/convert.h>
  49. // Calls MAPIAllocateMore or MAPIAllocateBuffer according to whether an lpBase was passed or not
  50. #define MAPI_ALLOC(n, lpBase, lpp) (lpBase ? MAPIAllocateMore(n, lpBase, lpp) : MAPIAllocateBuffer(n, lpp))
  51. // Frees the buffer with MAPIFreeBuffer if lpBase is NOT set, we can't directly free data allocated with MAPIAllocateMore ..
  52. #define MAPI_FREE(lpbase, lpp) { if(!lpBase) MAPIFreeBuffer(lpp); }
  53. ZEND_EXTERN_MODULE_GLOBALS(mapi)
  54. static LONG PropTagToPHPTag(ULONG ulPropTag) {
  55. LONG PHPTag = 0;
  56. if (PROP_TYPE(ulPropTag) == PT_UNICODE)
  57. PHPTag = (LONG)CHANGE_PROP_TYPE(ulPropTag, PT_STRING8);
  58. else if (PROP_TYPE(ulPropTag) == PT_MV_UNICODE)
  59. PHPTag = (LONG)CHANGE_PROP_TYPE(ulPropTag, PT_MV_STRING8);
  60. else
  61. PHPTag = (LONG)ulPropTag;
  62. return PHPTag;
  63. }
  64. /*
  65. * Converts a PHP Array into a SBinaryArray. This is the same as an ENTRYLIST which
  66. * is used with DeleteMessages();
  67. *
  68. */
  69. HRESULT PHPArraytoSBinaryArray(zval * entryid_array , void *lpBase, SBinaryArray *lpBinaryArray TSRMLS_DC)
  70. {
  71. // local
  72. HashTable *target_hash = NULL;
  73. int count;
  74. zval *pentry = NULL;
  75. int i, n = 0;
  76. MAPI_G(hr) = hrSuccess;
  77. target_hash = HASH_OF(entryid_array);
  78. if (!target_hash) {
  79. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No target_hash in PHPArraytoSBinaryArray");
  80. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  81. }
  82. count = zend_hash_num_elements(Z_ARRVAL_P(entryid_array));
  83. if (count == 0) {
  84. lpBinaryArray->lpbin = NULL;
  85. lpBinaryArray->cValues = 0;
  86. return MAPI_G(hr);
  87. }
  88. MAPI_G(hr) = MAPIAllocateMore(sizeof(SBinary) * count, lpBase,
  89. reinterpret_cast<void **>(&lpBinaryArray->lpbin));
  90. if(MAPI_G(hr) != hrSuccess)
  91. return MAPI_G(hr);
  92. // Reset php pointer
  93. zend_hash_internal_pointer_reset(target_hash);
  94. for (i = 0; i < count; ++i) {
  95. pentry = zend_hash_get_current_data(target_hash);
  96. convert_to_string_ex(pentry);
  97. MAPI_G(hr) = MAPIAllocateMore(pentry->value.str->len, lpBase, (void **) &lpBinaryArray->lpbin[n].lpb);
  98. if(MAPI_G(hr) != hrSuccess)
  99. return MAPI_G(hr);
  100. memcpy(lpBinaryArray->lpbin[n].lpb, pentry->value.str->val, pentry->value.str->len);
  101. lpBinaryArray->lpbin[n++].cb = pentry->value.str->len;
  102. zend_hash_move_forward(target_hash);
  103. }
  104. lpBinaryArray->cValues = n;
  105. return MAPI_G(hr);
  106. }
  107. HRESULT PHPArraytoSBinaryArray(zval * entryid_array , void *lpBase, SBinaryArray **lppBinaryArray TSRMLS_DC)
  108. {
  109. SBinaryArray *lpBinaryArray = NULL;
  110. MAPI_G(hr) = MAPI_ALLOC(sizeof(SBinaryArray), lpBase, (void **)&lpBinaryArray);
  111. if(MAPI_G(hr) != hrSuccess)
  112. return MAPI_G(hr);
  113. MAPI_G(hr) = PHPArraytoSBinaryArray(entryid_array, lpBase ? lpBase : lpBinaryArray, lpBinaryArray TSRMLS_CC);
  114. if(MAPI_G(hr) != hrSuccess) {
  115. MAPI_FREE(lpBase, lpBinaryArray);
  116. return MAPI_G(hr);
  117. }
  118. *lppBinaryArray = lpBinaryArray;
  119. return MAPI_G(hr);
  120. }
  121. HRESULT SBinaryArraytoPHPArray(SBinaryArray *lpBinaryArray, zval *pvalRet TSRMLS_DC)
  122. {
  123. unsigned int i = 0;
  124. MAPI_G(hr) = hrSuccess;
  125. array_init(pvalRet);
  126. for (i = 0; i < lpBinaryArray->cValues; ++i)
  127. add_next_index_stringl(pvalRet, (char *)lpBinaryArray->lpbin[i].lpb, lpBinaryArray->lpbin[i].cb);
  128. return MAPI_G(hr);
  129. }
  130. /*
  131. * Converts a PHP Array into a SortOrderSet. This SortOrderSet is used to sort a table
  132. * when a call to IMAPITable->QueryRows is made.
  133. * The zval array will look like this:
  134. * array() {
  135. * PR_SUBJECT => TABLE_SORT_ASCEND,
  136. * etc..
  137. * }
  138. *
  139. * The key is the property and the value is the sorting method for that property
  140. *
  141. * NOTE: The TABLE_SORT_COMBINE is not (yet) implemented, it should work but is not tested
  142. */
  143. HRESULT PHPArraytoSortOrderSet(zval * sortorder_array, void *lpBase, LPSSortOrderSet *lppSortOrderSet TSRMLS_DC)
  144. {
  145. // local
  146. LPSSortOrderSet lpSortOrderSet = NULL;
  147. int count;
  148. HashTable *target_hash = NULL;
  149. zval *entry = NULL;
  150. MAPI_G(hr) = hrSuccess;
  151. target_hash = HASH_OF(sortorder_array);
  152. if (!target_hash) {
  153. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No target_hash in PHPArraytoSortOrderSet");
  154. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  155. }
  156. // get the number of items in the array
  157. count = zend_hash_num_elements(Z_ARRVAL_P(sortorder_array));
  158. MAPI_G(hr) = MAPI_ALLOC(CbNewSSortOrderSet(count), lpBase, (void **) &lpSortOrderSet);
  159. if(MAPI_G(hr) != hrSuccess)
  160. return MAPI_G(hr);
  161. lpSortOrderSet->cSorts = count;
  162. lpSortOrderSet->cCategories = 0;
  163. lpSortOrderSet->cExpanded = 0;
  164. // first reset the hash, so the pointer points to the first element.
  165. zend_hash_internal_pointer_reset(target_hash);
  166. for (int i = 0; i < count; ++i) {
  167. //todo: check on FAILURE
  168. zend_string *key = NULL;
  169. zend_ulong ind = 0;
  170. entry = zend_hash_get_current_data(target_hash);
  171. // when the key is a char &key is set else ind is set
  172. zend_hash_get_current_key(target_hash, &key, &ind);
  173. if (key != NULL)
  174. lpSortOrderSet->aSort[i].ulPropTag = atoi(key->val);
  175. else
  176. lpSortOrderSet->aSort[i].ulPropTag = ind;
  177. convert_to_long_ex(entry);
  178. lpSortOrderSet->aSort[i].ulOrder = (ULONG) entry->value.lval;
  179. // get next hash pointer
  180. zend_hash_move_forward(target_hash);
  181. }
  182. *lppSortOrderSet = lpSortOrderSet;
  183. return MAPI_G(hr);
  184. }
  185. /**
  186. * Converts a php array to a PropTagArray.
  187. * The caller is responsible to free the memory using MAPIFreeBuffer
  188. */
  189. HRESULT PHPArraytoPropTagArray(zval * prop_value_array, void *lpBase, LPSPropTagArray *lppPropTagArray TSRMLS_DC)
  190. {
  191. // return value
  192. LPSPropTagArray lpPropTagArray = NULL;
  193. // local
  194. int count;
  195. HashTable *target_hash = NULL;
  196. zval *entry = NULL;
  197. MAPI_G(hr) = hrSuccess;
  198. target_hash = HASH_OF(prop_value_array);
  199. if (!target_hash) {
  200. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No target_hash in PHPArraytoPropTagArray");
  201. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  202. }
  203. // get the number of items in the array
  204. count = zend_hash_num_elements(target_hash);
  205. // allocate memory to use
  206. MAPI_G(hr) = MAPI_ALLOC(CbNewSPropTagArray(count), lpBase, (void **)&lpPropTagArray);
  207. if (MAPI_G(hr) != hrSuccess)
  208. return MAPI_G(hr);
  209. lpPropTagArray->cValues = count;
  210. // first reset the hash, so the pointer points to the first element.
  211. zend_hash_internal_pointer_reset(target_hash);
  212. for (int i = 0; i < count; ++i) {
  213. // Gets the element that exist at the current pointer.
  214. entry = zend_hash_get_current_data(target_hash);
  215. convert_to_long_ex(entry);
  216. lpPropTagArray->aulPropTag[i] = entry->value.lval;
  217. // move the pointer to the next entry
  218. zend_hash_move_forward(target_hash);
  219. }
  220. *lppPropTagArray = lpPropTagArray;
  221. return MAPI_G(hr);
  222. }
  223. /*
  224. * Converts a PHP property value array to a MAPI property value structure
  225. */
  226. HRESULT PHPArraytoPropValueArray(zval* phpArray, void *lpBase, ULONG *lpcValues, LPSPropValue *lppPropValArray TSRMLS_DC)
  227. {
  228. // return value
  229. LPSPropValue lpPropValue = NULL;
  230. ULONG cvalues = 0;
  231. // local
  232. int count;
  233. HashTable *target_hash = NULL;
  234. HashTable *dataHash = NULL;
  235. zend_string *keyIndex;
  236. zend_ulong numIndex = 0;
  237. zval *entry = NULL;
  238. ULONG countarray = 0;
  239. zval *dataEntry = NULL;
  240. HashTable *actionHash = NULL;
  241. ULONG j, h;
  242. ACTIONS *lpActions = NULL;
  243. LPSRestriction lpRestriction = NULL;
  244. ULONG ulCountTmp = 0; // temp value
  245. LPSPropValue lpPropTmp = NULL;
  246. zend_string *str_action = zend_string_init("action", sizeof("action")-1, 0);
  247. zend_string *str_flags = zend_string_init("flags", sizeof("flags")-1, 0);
  248. zend_string *str_flavor = zend_string_init("flavor", sizeof("flavor")-1, 0);
  249. zend_string *str_storeentryid = zend_string_init("storeentryid", sizeof("storeentryid")-1, 0);
  250. zend_string *str_folderentryid = zend_string_init("folderentryid", sizeof("folderentryid")-1, 0);
  251. zend_string *str_replyentryid = zend_string_init("replyentryid", sizeof("replyentryid")-1, 0);
  252. zend_string *str_replyguid = zend_string_init("replyguid", sizeof("replyguid")-1, 0);
  253. zend_string *str_dam = zend_string_init("dam", sizeof("dam")-1, 0);
  254. zend_string *str_code = zend_string_init("code", sizeof("code")-1, 0);
  255. zend_string *str_adrlist = zend_string_init("adrlist", sizeof("adrlist")-1, 0);
  256. zend_string *str_proptag = zend_string_init("proptag", sizeof("proptag")-1, 0);
  257. if (!phpArray) {
  258. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No phpArray in PHPArraytoPropValueArray");
  259. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  260. goto exit;
  261. }
  262. target_hash = HASH_OF(phpArray);
  263. if (!target_hash) {
  264. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No target_hash in PHPArraytoPropValueArray");
  265. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  266. goto exit;
  267. }
  268. // Get the number of items in the array, also the number of item to use for the LPSPropValue array
  269. count = zend_hash_num_elements(target_hash);
  270. if (count == 0) {
  271. *lppPropValArray = NULL;
  272. *lpcValues = 0;
  273. }
  274. zend_hash_internal_pointer_reset(target_hash);
  275. MAPI_G(hr) = MAPI_ALLOC(sizeof(SPropValue) * count, lpBase, (void**)&lpPropValue);
  276. for (int i = 0; i < count; ++i) {
  277. // Gets the element that exist at the current pointer.
  278. entry = zend_hash_get_current_data(target_hash);
  279. zend_hash_get_current_key(target_hash, &keyIndex, &numIndex);
  280. // assume a numeric index
  281. lpPropValue[cvalues].ulPropTag = numIndex;
  282. switch(PROP_TYPE(numIndex)) {
  283. case PT_SHORT:
  284. convert_to_long_ex(entry);
  285. lpPropValue[cvalues++].Value.i = (short)entry->value.lval;
  286. break;
  287. case PT_LONG:
  288. convert_to_long_ex(entry);
  289. lpPropValue[cvalues++].Value.l = entry->value.lval;
  290. break;
  291. case PT_FLOAT:
  292. convert_to_double_ex(entry);
  293. lpPropValue[cvalues++].Value.flt = (float)entry->value.dval;
  294. break;
  295. case PT_DOUBLE:
  296. convert_to_double_ex(entry);
  297. lpPropValue[cvalues++].Value.dbl = entry->value.dval;
  298. break;
  299. case PT_LONGLONG:
  300. convert_to_double_ex(entry);
  301. lpPropValue[cvalues++].Value.li.QuadPart = (LONGLONG)entry->value.dval;
  302. break;
  303. case PT_BOOLEAN:
  304. convert_to_boolean_ex(entry);
  305. // lval will be 1 or 0 for true or false
  306. lpPropValue[cvalues++].Value.b = (Z_TYPE_P(entry) == IS_TRUE);
  307. break;
  308. case PT_SYSTIME:
  309. convert_to_long_ex(entry);
  310. // convert timestamp to windows FileTime
  311. UnixTimeToFileTime(entry->value.lval, &lpPropValue[cvalues++].Value.ft);
  312. break;
  313. case PT_BINARY:
  314. convert_to_string_ex(entry);
  315. // Allocate and copy data
  316. MAPI_G(hr) = MAPIAllocateMore(entry->value.str->len, lpBase ? lpBase : lpPropValue, (void **)&lpPropValue[cvalues].Value.bin.lpb);
  317. if (MAPI_G(hr) != hrSuccess)
  318. goto exit;
  319. memcpy(lpPropValue[cvalues].Value.bin.lpb, entry->value.str->val, entry->value.str->len);
  320. lpPropValue[cvalues++].Value.bin.cb = entry->value.str->len;
  321. break;
  322. case PT_STRING8:
  323. convert_to_string_ex(entry);
  324. // Allocate and copy data
  325. MAPI_G(hr) = MAPIAllocateMore(entry->value.str->len+1, lpBase ? lpBase : lpPropValue, (void **)&lpPropValue[cvalues].Value.lpszA);
  326. if (MAPI_G(hr) != hrSuccess)
  327. goto exit;
  328. strncpy(lpPropValue[cvalues++].Value.lpszA, entry->value.str->val, entry->value.str->len+1);
  329. break;
  330. case PT_APPTIME:
  331. convert_to_double_ex(entry);
  332. lpPropValue[cvalues++].Value.at = entry->value.dval;
  333. break;
  334. case PT_CLSID:
  335. convert_to_string_ex(entry);
  336. if (entry->value.str->len != sizeof(GUID)) {
  337. php_error_docref(NULL TSRMLS_CC, E_WARNING, "GUID must be 16 bytes");
  338. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  339. goto exit;
  340. }
  341. MAPI_G(hr) = MAPIAllocateMore(sizeof(GUID), lpBase ? lpBase : lpPropValue, (void **)&lpPropValue[cvalues].Value.lpguid);
  342. if (MAPI_G(hr) != hrSuccess)
  343. goto exit;
  344. memcpy(lpPropValue[cvalues++].Value.lpguid, entry->value.str->val, sizeof(GUID));
  345. break;
  346. #define GET_MV_HASH() \
  347. { \
  348. dataHash = HASH_OF(entry); \
  349. if (!dataHash) { \
  350. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No MV dataHash"); \
  351. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER; \
  352. goto exit; \
  353. } \
  354. }
  355. #define CHECK_EMPTY_MV_ARRAY(mapimvmember, mapilpmember) \
  356. { \
  357. countarray = zend_hash_num_elements(dataHash); \
  358. if (countarray == 0) { \
  359. lpPropValue[cvalues].Value.mapimvmember.cValues = 0; \
  360. lpPropValue[cvalues].Value.mapimvmember.mapilpmember = NULL; \
  361. break; \
  362. } \
  363. zend_hash_internal_pointer_reset(dataHash); \
  364. }
  365. #define COPY_MV_PROPS(type, mapimvmember, mapilpmember, phpmember) \
  366. GET_MV_HASH() \
  367. CHECK_EMPTY_MV_ARRAY(mapimvmember, mapilpmember) \
  368. lpPropValue[cvalues].Value.mapimvmember.cValues = countarray; \
  369. MAPI_G(hr) = MAPIAllocateMore(sizeof(lpPropValue[cvalues].Value.mapimvmember.mapilpmember[0]) * countarray, lpBase ? lpBase : lpPropValue, (void**)&lpPropValue[cvalues].Value.mapimvmember.mapilpmember); \
  370. for (j = 0; j < countarray; ++j) { \
  371. dataEntry = zend_hash_get_current_data(dataHash); \
  372. convert_to_##type##_ex(dataEntry); \
  373. lpPropValue[cvalues].Value.mapimvmember.mapilpmember[j] = dataEntry->value.phpmember; \
  374. zend_hash_move_forward(dataHash); \
  375. }
  376. case PT_MV_I2:
  377. COPY_MV_PROPS(long, MVi, lpi, lval);
  378. ++cvalues;
  379. break;
  380. case PT_MV_LONG:
  381. COPY_MV_PROPS(long, MVl, lpl, lval);
  382. ++cvalues;
  383. break;
  384. case PT_MV_R4:
  385. COPY_MV_PROPS(double, MVflt, lpflt, dval);
  386. ++cvalues;
  387. break;
  388. case PT_MV_DOUBLE:
  389. COPY_MV_PROPS(double, MVdbl, lpdbl, dval);
  390. ++cvalues;
  391. break;
  392. case PT_MV_APPTIME:
  393. COPY_MV_PROPS(double, MVat, lpat, dval);
  394. ++cvalues;
  395. break;
  396. case PT_MV_SYSTIME:
  397. GET_MV_HASH();
  398. CHECK_EMPTY_MV_ARRAY(MVft, lpft);
  399. lpPropValue[cvalues].Value.MVft.cValues = countarray;
  400. if ((MAPI_G(hr) = MAPIAllocateMore(sizeof(FILETIME) * countarray, lpBase ? lpBase : lpPropValue, (void **)&lpPropValue[cvalues].Value.MVft.lpft)) != hrSuccess)
  401. goto exit;
  402. for (j = 0; j < countarray; ++j) {
  403. dataEntry = zend_hash_get_current_data(dataHash);
  404. convert_to_long_ex(dataEntry);
  405. UnixTimeToFileTime(dataEntry->value.lval, &lpPropValue[cvalues].Value.MVft.lpft[j]);
  406. zend_hash_move_forward(dataHash);
  407. }
  408. ++cvalues;
  409. break;
  410. case PT_MV_UNICODE: // PT_MV_UNICODE is binary-compatible with PT_MV_BINARY in this case ..
  411. case PT_MV_BINARY:
  412. GET_MV_HASH();
  413. CHECK_EMPTY_MV_ARRAY(MVbin, lpbin);
  414. if ((MAPI_G(hr) = MAPIAllocateMore(sizeof(SBinary) * countarray, lpBase ? lpBase : lpPropValue, (void **)&lpPropValue[cvalues].Value.MVbin.lpbin)) != hrSuccess)
  415. goto exit;
  416. for (h = 0, j = 0; j < countarray; ++j) {
  417. dataEntry = zend_hash_get_current_data(dataHash);
  418. convert_to_string_ex(dataEntry);
  419. lpPropValue[cvalues].Value.MVbin.lpbin[h].cb = dataEntry->value.str->len;
  420. MAPI_G(hr) = MAPIAllocateMore(dataEntry->value.str->len, lpBase ? lpBase : lpPropValue, (void **)&lpPropValue[cvalues].Value.MVbin.lpbin[h].lpb);
  421. if (MAPI_G(hr) != hrSuccess)
  422. goto exit;
  423. memcpy(lpPropValue[cvalues].Value.MVbin.lpbin[h++].lpb, dataEntry->value.str->val, dataEntry->value.str->len);
  424. zend_hash_move_forward(dataHash);
  425. }
  426. lpPropValue[cvalues++].Value.MVbin.cValues = h;
  427. break;
  428. case PT_MV_STRING8:
  429. GET_MV_HASH();
  430. CHECK_EMPTY_MV_ARRAY(MVszA, lppszA);
  431. if ((MAPI_G(hr) = MAPIAllocateMore(sizeof(char*) * countarray, lpBase ? lpBase : lpPropValue, (void **)&lpPropValue[cvalues].Value.MVszA.lppszA)) != hrSuccess)
  432. goto exit;
  433. for (h = 0, j = 0; j < countarray; ++j) {
  434. dataEntry = zend_hash_get_current_data(dataHash);
  435. convert_to_string_ex(dataEntry);
  436. MAPI_G(hr) = MAPIAllocateMore(dataEntry->value.str->len+1, lpBase ? lpBase : lpPropValue, (void **)&lpPropValue[cvalues].Value.MVszA.lppszA[h]);
  437. if (MAPI_G(hr) != hrSuccess)
  438. goto exit;
  439. strncpy(lpPropValue[cvalues].Value.MVszA.lppszA[h++], dataEntry->value.str->val, dataEntry->value.str->len + 1);
  440. zend_hash_move_forward(dataHash);
  441. }
  442. lpPropValue[cvalues++].Value.MVszA.cValues = h;
  443. break;
  444. case PT_MV_CLSID:
  445. GET_MV_HASH();
  446. CHECK_EMPTY_MV_ARRAY(MVguid, lpguid);
  447. if ((MAPI_G(hr) = MAPIAllocateMore(sizeof(GUID) * countarray, lpBase ? lpBase : lpPropValue, (void **)&lpPropValue[cvalues].Value.MVguid.lpguid)) != hrSuccess)
  448. goto exit;
  449. for (h = 0, j = 0; j < countarray; ++j) {
  450. dataEntry = zend_hash_get_current_data(dataHash);
  451. convert_to_string_ex(dataEntry);
  452. if (dataEntry->value.str->len != sizeof(GUID))
  453. php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid value for PT_MV_CLSID property in proptag 0x%08X, position %d,%d", lpPropValue[cvalues].ulPropTag, i, j);
  454. else
  455. memcpy(&lpPropValue[cvalues].Value.MVguid.lpguid[h++], dataEntry->value.str->val, sizeof(GUID));
  456. zend_hash_move_forward(dataHash);
  457. }
  458. lpPropValue[cvalues++].Value.MVguid.cValues = h;
  459. break;
  460. case PT_MV_I8:
  461. php_error_docref(NULL TSRMLS_CC, E_WARNING, "PT_MV_I8 not supported");
  462. MAPI_G(hr) = MAPI_E_NO_SUPPORT;
  463. goto exit;
  464. break;
  465. case PT_MV_CURRENCY:
  466. php_error_docref(NULL TSRMLS_CC, E_WARNING, "PT_MV_CURRENCY not supported");
  467. MAPI_G(hr) = MAPI_E_NO_SUPPORT;
  468. goto exit;
  469. break;
  470. case PT_ACTIONS:
  471. dataHash = HASH_OF(entry);
  472. if (!dataHash)
  473. break;
  474. zend_hash_internal_pointer_reset(dataHash);
  475. countarray = zend_hash_num_elements(dataHash); // # of actions
  476. if (countarray == 0) {
  477. php_error_docref(NULL TSRMLS_CC, E_WARNING, "PT_ACTIONS is empty");
  478. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  479. goto exit;
  480. }
  481. if ((MAPI_G(hr) = MAPIAllocateMore(sizeof(ACTIONS), lpBase ? lpBase : lpPropValue, (void **)&lpPropValue[cvalues].Value.lpszA)) != hrSuccess)
  482. goto exit;
  483. lpActions = (ACTIONS *)lpPropValue[cvalues].Value.lpszA;
  484. lpActions->ulVersion = EDK_RULES_VERSION;
  485. lpActions->cActions = countarray;
  486. if ((MAPI_G(hr) = MAPIAllocateMore(sizeof(ACTION)*lpActions->cActions, lpBase ? lpBase : lpPropValue, (void **)&lpActions->lpAction)) != hrSuccess)
  487. goto exit;
  488. memset(lpActions->lpAction, 0, sizeof(ACTION)*lpActions->cActions);
  489. for (j = 0; j < countarray; ++j) {
  490. entry = zend_hash_get_current_data(dataHash);
  491. ZVAL_DEREF(entry);
  492. actionHash = HASH_OF(entry);
  493. if (!actionHash) {
  494. php_error_docref(NULL TSRMLS_CC, E_WARNING, "ACTIONS structure has a wrong ACTION");
  495. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  496. goto exit;
  497. }
  498. if ((dataEntry = zend_hash_find(actionHash, str_action)) == NULL) {
  499. php_error_docref(NULL TSRMLS_CC, E_WARNING, "PT_ACTIONS type has no action type in array");
  500. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  501. goto exit;
  502. }
  503. convert_to_long_ex(dataEntry);
  504. lpActions->lpAction[j].acttype = (ACTTYPE)Z_LVAL_P(dataEntry);
  505. // Option field user defined flags, default 0
  506. if ((dataEntry = zend_hash_find(actionHash, str_flags)) != NULL) {
  507. convert_to_long_ex(dataEntry);
  508. lpActions->lpAction[j].ulFlags = Z_LVAL_P(dataEntry);
  509. }
  510. // Option field used with OP_REPLAY and OP_FORWARD, default 0
  511. if ((dataEntry = zend_hash_find(actionHash, str_flavor)) != NULL) {
  512. convert_to_long_ex(dataEntry);
  513. lpActions->lpAction[j].ulActionFlavor = Z_LVAL_P(dataEntry);
  514. }
  515. switch (lpActions->lpAction[j].acttype) {
  516. case OP_MOVE:
  517. case OP_COPY:
  518. if ((dataEntry = zend_hash_find(actionHash, str_storeentryid)) == NULL) {
  519. php_error_docref(NULL TSRMLS_CC, E_WARNING, "OP_COPY/OP_MOVE but no storeentryid entry");
  520. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  521. goto exit;
  522. }
  523. convert_to_string_ex(dataEntry);
  524. lpActions->lpAction[j].actMoveCopy.cbStoreEntryId = dataEntry->value.str->len;
  525. MAPI_G(hr) = MAPIAllocateMore(dataEntry->value.str->len, lpBase ? lpBase : lpPropValue, (void **)&lpActions->lpAction[j].actMoveCopy.lpStoreEntryId);
  526. if (MAPI_G(hr) != hrSuccess)
  527. goto exit;
  528. memcpy(lpActions->lpAction[j].actMoveCopy.lpStoreEntryId, dataEntry->value.str->val, dataEntry->value.str->len);
  529. if ((dataEntry = zend_hash_find(actionHash, str_folderentryid)) == NULL) {
  530. php_error_docref(NULL TSRMLS_CC, E_WARNING, "OP_COPY/OP_MOVE but no folderentryid entry");
  531. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  532. goto exit;
  533. }
  534. convert_to_string_ex(dataEntry);
  535. lpActions->lpAction[j].actMoveCopy.cbFldEntryId = dataEntry->value.str->len;
  536. MAPI_G(hr) = MAPIAllocateMore(dataEntry->value.str->len, lpBase ? lpBase : lpPropValue, (void **)&lpActions->lpAction[j].actMoveCopy.lpFldEntryId);
  537. if (MAPI_G(hr) != hrSuccess)
  538. goto exit;
  539. memcpy(lpActions->lpAction[j].actMoveCopy.lpFldEntryId, dataEntry->value.str->val, dataEntry->value.str->len);
  540. break;
  541. case OP_REPLY:
  542. case OP_OOF_REPLY:
  543. if ((dataEntry = zend_hash_find(actionHash, str_replyentryid)) == NULL) {
  544. php_error_docref(NULL TSRMLS_CC, E_WARNING, "OP_REPLY but no replyentryid entry");
  545. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  546. goto exit;
  547. }
  548. convert_to_string_ex(dataEntry);
  549. lpActions->lpAction[j].actReply.cbEntryId = dataEntry->value.str->len;
  550. MAPI_G(hr) = MAPIAllocateMore(dataEntry->value.str->len, lpBase ? lpBase : lpPropValue, (void **)&lpActions->lpAction[j].actReply.lpEntryId);
  551. if (MAPI_G(hr) != hrSuccess)
  552. goto exit;
  553. memcpy(lpActions->lpAction[j].actReply.lpEntryId, dataEntry->value.str->val, dataEntry->value.str->len);
  554. // optional field
  555. if ((dataEntry = zend_hash_find(actionHash, str_replyguid)) != NULL) {
  556. convert_to_string_ex(dataEntry);
  557. if (dataEntry->value.str->len != sizeof(GUID)) {
  558. php_error_docref(NULL TSRMLS_CC, E_WARNING, "OP_REPLY replyguid not sizeof(GUID)");
  559. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  560. goto exit;
  561. } else {
  562. memcpy(&lpActions->lpAction[j].actReply.guidReplyTemplate, dataEntry->value.str->val, sizeof(GUID));
  563. }
  564. }
  565. break;
  566. case OP_DEFER_ACTION:
  567. if ((dataEntry = zend_hash_find(actionHash, str_dam)) == NULL) {
  568. php_error_docref(NULL TSRMLS_CC, E_WARNING, "OP_DEFER_ACTION but no dam entry");
  569. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  570. goto exit;
  571. }
  572. convert_to_string_ex(dataEntry);
  573. lpActions->lpAction[j].actDeferAction.cbData = dataEntry->value.str->len;
  574. MAPI_G(hr) = MAPIAllocateMore(dataEntry->value.str->len, lpBase ? lpBase : lpPropValue, (void **)&lpActions->lpAction[j].actDeferAction.pbData);
  575. if (MAPI_G(hr) != hrSuccess)
  576. goto exit;
  577. memcpy(lpActions->lpAction[j].actDeferAction.pbData, dataEntry->value.str->val, dataEntry->value.str->len);
  578. break;
  579. case OP_BOUNCE:
  580. if ((dataEntry = zend_hash_find(actionHash, str_code)) == NULL) {
  581. php_error_docref(NULL TSRMLS_CC, E_WARNING, "OP_BOUNCE but no code entry");
  582. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  583. goto exit;
  584. }
  585. convert_to_long_ex(dataEntry);
  586. lpActions->lpAction[j].scBounceCode = Z_LVAL_P(dataEntry);
  587. break;
  588. case OP_FORWARD:
  589. case OP_DELEGATE:
  590. if ((dataEntry = zend_hash_find(actionHash, str_adrlist)) == NULL) {
  591. php_error_docref(NULL TSRMLS_CC, E_WARNING, "OP_FORWARD/OP_DELEGATE but no adrlist entry");
  592. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  593. goto exit;
  594. }
  595. if (Z_TYPE_P(dataEntry) != IS_ARRAY) {
  596. php_error_docref(NULL TSRMLS_CC, E_WARNING, "OP_FORWARD/OP_DELEGATE adrlist entry must be an array");
  597. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  598. goto exit;
  599. }
  600. MAPI_G(hr) = PHPArraytoAdrList(dataEntry, lpBase ? lpBase : lpPropValue, &lpActions->lpAction[j].lpadrlist TSRMLS_CC);
  601. if (MAPI_G(hr) != hrSuccess)
  602. goto exit;
  603. if (MAPI_G(hr) != hrSuccess){
  604. php_error_docref(NULL TSRMLS_CC, E_WARNING, "OP_DELEGATE/OP_FORWARD wrong data in adrlist entry");
  605. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  606. goto exit;
  607. }
  608. break;
  609. case OP_TAG:
  610. if ((dataEntry = zend_hash_find(actionHash, str_proptag)) == NULL) {
  611. php_error_docref(NULL TSRMLS_CC, E_WARNING, "OP_TAG but no proptag entry");
  612. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  613. goto exit;
  614. }
  615. MAPI_G(hr) = PHPArraytoPropValueArray(dataEntry, lpBase ? lpBase : lpPropValue, &ulCountTmp, &lpPropTmp TSRMLS_CC);
  616. if (MAPI_G(hr) != hrSuccess)
  617. goto exit;
  618. if (ulCountTmp > 1)
  619. php_error_docref(NULL TSRMLS_CC, E_WARNING, "OP_TAG has 'proptag' member which contains more than one property. Using the first in the array.");
  620. lpActions->lpAction[j].propTag = *lpPropTmp;
  621. break;
  622. case OP_DELETE:
  623. case OP_MARK_AS_READ:
  624. // Nothing to do
  625. break;
  626. };
  627. zend_hash_move_forward(dataHash);
  628. }
  629. ++cvalues;
  630. break;
  631. case PT_SRESTRICTION:
  632. MAPI_G(hr) = PHPArraytoSRestriction(entry, lpBase ? lpBase : lpPropValue, &lpRestriction TSRMLS_CC);
  633. if (MAPI_G(hr) != hrSuccess) {
  634. php_error_docref(NULL TSRMLS_CC, E_WARNING, "PHPArray to SRestriction failed");
  635. goto exit;
  636. }
  637. lpPropValue[cvalues++].Value.lpszA = (char *)lpRestriction;
  638. break;
  639. case PT_ERROR:
  640. convert_to_long_ex(entry);
  641. lpPropValue[cvalues].Value.err = entry->value.lval;
  642. break;
  643. default:
  644. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown property type %08X", PROP_TYPE(numIndex));
  645. MAPI_G(hr) = MAPI_E_INVALID_TYPE;
  646. goto exit;
  647. break;
  648. }
  649. // move the pointer to the next entry
  650. zend_hash_move_forward(target_hash);
  651. }
  652. *lpcValues = cvalues;
  653. *lppPropValArray = lpPropValue;
  654. exit:
  655. zend_string_release(str_action);
  656. zend_string_release(str_flags);
  657. zend_string_release(str_flavor);
  658. zend_string_release(str_storeentryid);
  659. zend_string_release(str_folderentryid);
  660. zend_string_release(str_replyentryid);
  661. zend_string_release(str_replyguid);
  662. zend_string_release(str_dam);
  663. zend_string_release(str_code);
  664. zend_string_release(str_adrlist);
  665. zend_string_release(str_proptag);
  666. if(MAPI_G(hr) != hrSuccess)
  667. MAPIFreeBuffer(lpPropValue);
  668. return MAPI_G(hr);
  669. }
  670. HRESULT PHPArraytoAdrList(zval *phpArray, void *lpBase, LPADRLIST *lppAdrList TSRMLS_DC)
  671. {
  672. HashTable *target_hash = NULL;
  673. ULONG countProperties = 0; // number of properties
  674. ULONG count = 0; // number of elements in the array
  675. ULONG countRecipients = 0; // number of actual recipients
  676. LPADRLIST lpAdrList = NULL;
  677. zval *entry = NULL;
  678. LPSPropValue pPropValue = NULL;
  679. MAPI_G(hr) = hrSuccess;
  680. if (!phpArray) {
  681. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No phpArray in PHPArraytoAdrList");
  682. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  683. goto exit;
  684. }
  685. target_hash = HASH_OF(phpArray);
  686. if (!target_hash) {
  687. php_error_docref(NULL TSRMLS_CC, E_WARNING, "phparraytoadrlist wrong data, unknown error");
  688. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  689. goto exit;
  690. }
  691. if(Z_TYPE_P(phpArray) != IS_ARRAY) {
  692. php_error_docref(NULL TSRMLS_CC, E_WARNING, "phparray to adrlist must include an array");
  693. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  694. goto exit;
  695. }
  696. count = zend_hash_num_elements(target_hash);
  697. // We allow allocing a 0 count addresslist, since we need this the OP_DELEGATE rule
  698. MAPI_G(hr) = MAPI_ALLOC(CbNewADRLIST(count), lpBase, (void **)&lpAdrList);
  699. if(MAPI_G(hr) != hrSuccess)
  700. goto exit;
  701. zend_hash_internal_pointer_reset(target_hash);
  702. // FIXME: It is possible that the memory allocated is more than actually needed. We should first
  703. // count the number of elements needed then allocate memory and then fill the memory.
  704. // but since this waste is probably very minimal, we could not care less about this.
  705. for (unsigned int i = 0; i < count; ++i) {
  706. entry = zend_hash_get_current_data(target_hash);
  707. if(Z_TYPE_P(entry) != IS_ARRAY) {
  708. php_error_docref(NULL TSRMLS_CC, E_WARNING, "phparraytoadrlist array must include an array with array of propvalues");
  709. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  710. goto exit;
  711. }
  712. MAPI_G(hr) = PHPArraytoPropValueArray(entry, lpBase, &countProperties, &pPropValue TSRMLS_CC);
  713. if(MAPI_G(hr) != hrSuccess)
  714. goto exit;
  715. lpAdrList->aEntries[countRecipients].ulReserved1 = 0;
  716. lpAdrList->aEntries[countRecipients].rgPropVals = pPropValue;
  717. lpAdrList->aEntries[countRecipients].cValues = countProperties;
  718. // move the pointer to the next entry
  719. zend_hash_move_forward(target_hash);
  720. ++countRecipients;
  721. }
  722. lpAdrList->cEntries = countRecipients;
  723. *lppAdrList = lpAdrList;
  724. exit:
  725. if(MAPI_G(hr) != hrSuccess && lpBase == NULL && lpAdrList != NULL)
  726. FreePadrlist(lpAdrList);
  727. return MAPI_G(hr);
  728. }
  729. HRESULT PHPArraytoRowList(zval *phpArray, void *lpBase, LPROWLIST *lppRowList TSRMLS_DC) {
  730. HashTable *target_hash = NULL;
  731. ULONG countProperties = 0; // number of properties
  732. ULONG count = 0; // number of elements in the array
  733. ULONG countRows = 0; // number of actual recipients
  734. LPROWLIST lpRowList = NULL;
  735. zval *entry = NULL;
  736. zval *data = NULL;
  737. LPSPropValue pPropValue = NULL;
  738. MAPI_G(hr) = hrSuccess;
  739. zend_string *str_properties = zend_string_init("properties", sizeof("properties")-1, 0);
  740. zend_string *str_rowflags = zend_string_init("rowflags", sizeof("rowflags")-1, 0);
  741. if (!phpArray || Z_TYPE_P(phpArray) != IS_ARRAY) {
  742. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No phpArray in PHPArraytoRowList");
  743. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  744. goto exit;
  745. }
  746. target_hash = HASH_OF(phpArray);
  747. if (!target_hash) {
  748. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No target_hash in PHPArraytoRowList");
  749. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  750. goto exit;
  751. }
  752. count = zend_hash_num_elements(target_hash);
  753. // allocate memory to store the array of pointers
  754. MAPI_G(hr) = MAPIAllocateBuffer(CbNewROWLIST(count),
  755. reinterpret_cast<void **>(&lpRowList));
  756. if (MAPI_G(hr) != hrSuccess)
  757. goto exit;
  758. zend_hash_internal_pointer_reset(target_hash);
  759. // FIXME: It is possible that the memory allocated is more than actually needed. We should first
  760. // count the number of elements needed then allocate memory and then fill the memory.
  761. // but since this waste is probably very minimal, we could not care less about this.
  762. for (unsigned int i = 0; i < count; ++i) {
  763. entry = zend_hash_get_current_data(target_hash);
  764. if (Z_TYPE_P(entry) != IS_ARRAY) {
  765. php_error_docref(NULL TSRMLS_CC, E_WARNING, "PHPArraytoRowList, Row not wrapped in array");
  766. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  767. goto exit;
  768. }
  769. if ((data = zend_hash_find(HASH_OF(entry), str_properties)) != NULL) {
  770. MAPI_G(hr) = PHPArraytoPropValueArray(data, NULL, &countProperties, &pPropValue TSRMLS_CC);
  771. if(MAPI_G(hr) != hrSuccess)
  772. goto exit;
  773. }else {
  774. php_error_docref(NULL TSRMLS_CC, E_WARNING, "PHPArraytoRowList, Missing field properties");
  775. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  776. goto exit;
  777. }
  778. if (pPropValue) {
  779. if ((data = zend_hash_find(HASH_OF(entry), str_rowflags)) != NULL) {
  780. lpRowList->aEntries[countRows].ulRowFlags = Z_LVAL_P(data);
  781. } else {
  782. php_error_docref(NULL TSRMLS_CC, E_WARNING, "PHPArraytoRowList, Missing field rowflags");
  783. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  784. goto exit;
  785. }
  786. lpRowList->aEntries[countRows].rgPropVals = pPropValue;
  787. lpRowList->aEntries[countRows++].cValues = countProperties;
  788. }else {
  789. php_error_docref(NULL TSRMLS_CC, E_WARNING, "PHPArraytoRowList, critical error");
  790. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  791. goto exit;
  792. }
  793. // move the pointer to the next entry
  794. zend_hash_move_forward(target_hash);
  795. }
  796. lpRowList->cEntries = countRows;
  797. *lppRowList = lpRowList;
  798. exit:
  799. if (MAPI_G(hr) != hrSuccess)
  800. MAPIFreeBuffer(lpRowList);
  801. zend_string_release(str_properties);
  802. zend_string_release(str_rowflags);
  803. return MAPI_G(hr);
  804. }
  805. /**
  806. Function to convert a PHP array to a SRestriction structure.
  807. The SRectriction structure is a structure with multiple level which defines a sort of
  808. tree with restrictions for a table view. It is used with de setSearchCriteria, findRow and other
  809. functions to select rows for a specified criteria.
  810. The PHP array should be in the following form:
  811. array(RES_AND,
  812. array(
  813. array(RES_OR,
  814. array(
  815. array(RES_PROPERTY,
  816. array(
  817. RELOP => RELOP_LE,
  818. ULPROPTAG => PR_BODY,
  819. VALUE => "test"
  820. ),
  821. ),
  822. array(RES_PROPERTY,
  823. array(
  824. RELOP => RELOP_RE,
  825. ULPROPTAG => PR_DISPLAY_NAME,
  826. VALUE => "leon"
  827. ),
  828. ),
  829. ), // OR array
  830. ),
  831. array(RES_EXIST,
  832. array(
  833. ULPROPTAG => PR_HASATTACH
  834. ),
  835. ),
  836. ), // AND array
  837. )
  838. This will mean the following in some pseudocode:
  839. (((PROPERTY PR_BODY = "test") or (PROPERTY PR_DISPLAY_NAME like "leon")) and (PROPERTY TAG EXIST PR_ATTACH))
  840. To fix the collision problem, an array is added. This gives the following assumption:
  841. An entry always consists of:
  842. array(RES_xxxx, array(values))
  843. values = array(RES_xxxx, array(values)) || array(restriction data)
  844. when we read the php array, we use this assumption.
  845. */
  846. // see include/mapi/mapidefs.php restriction array index values
  847. #define VALUE 0
  848. #define RELOP 1
  849. #define FUZZYLEVEL 2
  850. #define CB 3
  851. #define ULTYPE 4
  852. #define ULMASK 5
  853. #define ULPROPTAG 6
  854. #define ULPROPTAG1 7
  855. #define ULPROPTAG2 8
  856. #define PROPS 9
  857. #define RESTRICTION 10
  858. /*
  859. * A lpRes pointer should be given here, the idea is that every time this
  860. * function is run the SRestriction struct will become bigger, using lpBase as base pointer for MAPIAllocateMore().
  861. * TODO: combine code from here and from PHPArraytoPropValueArray in a pval/zval to Proptag function (?)
  862. */
  863. HRESULT PHPArraytoSRestriction(zval *phpVal, void* lpBase, LPSRestriction lpRes TSRMLS_DC)
  864. {
  865. HashTable *resHash = NULL;
  866. HashTable *dataHash = NULL;
  867. zval *typeEntry = NULL;
  868. zval *valueEntry = NULL;
  869. ULONG cValues = 0;
  870. int count;
  871. int i;
  872. if (!phpVal || lpRes == NULL) {
  873. php_error_docref(NULL TSRMLS_CC, E_WARNING, "critical error");
  874. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  875. }
  876. ZVAL_DEREF(phpVal);
  877. resHash = HASH_OF(phpVal);
  878. if (!resHash || zend_hash_num_elements(resHash) != 2) { // should always be array(RES_ , array(values))
  879. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Wrong array should be array(RES_, array(values))");
  880. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  881. }
  882. zend_hash_internal_pointer_reset(resHash);
  883. // structure assumption: add more checks that valueEntry becomes php array pointer?
  884. typeEntry = zend_hash_get_current_data(resHash); // 0=type, 1=array
  885. zend_hash_move_forward(resHash);
  886. valueEntry = zend_hash_get_current_data(resHash);
  887. lpRes->rt = typeEntry->value.lval; // set restriction type (RES_AND, RES_OR, ...)
  888. ZVAL_DEREF(valueEntry);
  889. dataHash = HASH_OF(valueEntry); // from resHash
  890. if (!dataHash) {
  891. php_error_docref(NULL TSRMLS_CC, E_WARNING, "critical error, wrong array");
  892. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  893. }
  894. count = zend_hash_num_elements(dataHash);
  895. zend_hash_internal_pointer_reset(dataHash);
  896. switch(lpRes->rt) {
  897. /*
  898. * type restrictions
  899. */
  900. case RES_AND:
  901. // Recursively add all AND-ed restrictions
  902. lpRes->res.resAnd.cRes = count;
  903. MAPI_G(hr) = MAPIAllocateMore(sizeof(SRestriction) * count, lpBase, (void **) &lpRes->res.resAnd.lpRes);
  904. if (MAPI_G(hr) != hrSuccess)
  905. return MAPI_G(hr);
  906. for (i = 0; i < count; ++i) {
  907. valueEntry = zend_hash_get_current_data(dataHash);
  908. MAPI_G(hr) = PHPArraytoSRestriction(valueEntry, lpBase, &lpRes->res.resAnd.lpRes[i] TSRMLS_CC);
  909. if (MAPI_G(hr) != hrSuccess)
  910. return MAPI_G(hr);
  911. zend_hash_move_forward(dataHash);
  912. }
  913. break;
  914. case RES_OR:
  915. // Recursively add all OR-ed restrictions
  916. lpRes->res.resOr.cRes = count;
  917. MAPI_G(hr) = MAPIAllocateMore(sizeof(SRestriction) * count, lpBase, (void **) &lpRes->res.resOr.lpRes);
  918. if (MAPI_G(hr) != hrSuccess)
  919. return MAPI_G(hr);
  920. for (i = 0; i < count; ++i) {
  921. valueEntry = zend_hash_get_current_data(dataHash);
  922. MAPI_G(hr) = PHPArraytoSRestriction(valueEntry, lpBase, &lpRes->res.resOr.lpRes[i] TSRMLS_CC);
  923. if (MAPI_G(hr) != hrSuccess)
  924. return MAPI_G(hr);
  925. zend_hash_move_forward(dataHash);
  926. }
  927. break;
  928. case RES_NOT:
  929. // NOT has only one restriction
  930. MAPI_G(hr) = MAPIAllocateMore(sizeof(SRestriction), lpBase, (void **) &lpRes->res.resNot.lpRes);
  931. if (MAPI_G(hr) != hrSuccess)
  932. return MAPI_G(hr);
  933. valueEntry = zend_hash_get_current_data(dataHash);
  934. MAPI_G(hr) = PHPArraytoSRestriction(valueEntry, lpBase, lpRes->res.resNot.lpRes TSRMLS_CC);
  935. if (MAPI_G(hr) != hrSuccess)
  936. return MAPI_G(hr);
  937. break;
  938. case RES_SUBRESTRICTION:
  939. if ((valueEntry = zend_hash_index_find(dataHash, RESTRICTION)) == NULL) {
  940. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_SUBRESTRICTION, Missing field RESTRICTION");
  941. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  942. }
  943. MAPI_G(hr) = PHPArraytoSRestriction(valueEntry, lpBase, &lpRes->res.resSub.lpRes TSRMLS_CC);
  944. if (MAPI_G(hr) != hrSuccess)
  945. return MAPI_G(hr);
  946. // ULPROPTAG as resSubObject
  947. if ((valueEntry = zend_hash_index_find(dataHash, ULPROPTAG)) == NULL) {
  948. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_SUBRESTRICTION, Missing field ULPROPTAG");
  949. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  950. }
  951. lpRes->res.resSub.ulSubObject = valueEntry->value.lval;
  952. break;
  953. case RES_COMMENT:
  954. if ((valueEntry = zend_hash_index_find(dataHash, RESTRICTION)) == NULL) {
  955. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_COMMENT, Missing field RESTRICTION");
  956. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  957. }
  958. MAPI_G(hr) = PHPArraytoSRestriction(valueEntry, lpBase, &lpRes->res.resComment.lpRes TSRMLS_CC);
  959. if (MAPI_G(hr) != hrSuccess) {
  960. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_COMMENT, Wrong data in field RESTRICTION");
  961. return MAPI_G(hr);
  962. }
  963. if ((valueEntry = zend_hash_index_find(dataHash, PROPS)) == NULL) {
  964. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_COMMENT, Missing field PROPS");
  965. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  966. }
  967. MAPI_G(hr) = PHPArraytoPropValueArray(valueEntry, lpBase, &lpRes->res.resComment.cValues, &lpRes->res.resComment.lpProp TSRMLS_CC);
  968. if(MAPI_G(hr) != hrSuccess) {
  969. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_COMMENT, Wrong data in field PROPS");
  970. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  971. }
  972. break;
  973. /*
  974. * content restrictions
  975. */
  976. case RES_CONTENT:
  977. case RES_PROPERTY:
  978. {
  979. LPSPropValue lpProp;
  980. if (lpRes->rt == RES_PROPERTY) {
  981. // ULPROPTAG
  982. if ((valueEntry = zend_hash_index_find(dataHash, ULPROPTAG)) == NULL) {
  983. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_PROPERTY, Missing field ULPROPTAG");
  984. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  985. }
  986. convert_to_long_ex(valueEntry);
  987. lpRes->res.resProperty.ulPropTag = valueEntry->value.lval;
  988. // RELOP
  989. if ((valueEntry = zend_hash_index_find(dataHash, RELOP)) == NULL) {
  990. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_PROPERTY, Missing field RELOP");
  991. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  992. }
  993. convert_to_long_ex(valueEntry);
  994. lpRes->res.resProperty.relop = valueEntry->value.lval;
  995. } else {
  996. // ULPROPTAG
  997. if ((valueEntry = zend_hash_index_find(dataHash, ULPROPTAG)) == NULL) {
  998. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_CONTENT, Missing field ULPROPTAG");
  999. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1000. }
  1001. convert_to_long_ex(valueEntry);
  1002. lpRes->res.resContent.ulPropTag = valueEntry->value.lval;
  1003. // possible FUZZYLEVEL
  1004. switch (PROP_TYPE(lpRes->res.resContent.ulPropTag)) {
  1005. case PT_STRING8:
  1006. case PT_UNICODE:
  1007. case PT_BINARY:
  1008. case PT_MV_BINARY:
  1009. case PT_MV_STRING8:
  1010. if ((valueEntry = zend_hash_index_find(dataHash, FUZZYLEVEL)) == NULL) {
  1011. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_CONTENT, Missing field FUZZYLEVEL");
  1012. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1013. }
  1014. convert_to_long_ex(valueEntry);
  1015. lpRes->res.resContent.ulFuzzyLevel = valueEntry->value.lval;
  1016. break;
  1017. default:
  1018. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_CONTENT, Not supported property type");
  1019. return MAPI_G(hr) = MAPI_E_TOO_COMPLEX;
  1020. };
  1021. }
  1022. // VALUE
  1023. if ((valueEntry = zend_hash_index_find(dataHash, VALUE)) == NULL) {
  1024. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_PROPERTY or RES_CONTENT, Missing field VALUE");
  1025. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1026. }
  1027. if(Z_TYPE_P(valueEntry) == IS_ARRAY) {
  1028. MAPI_G(hr) = PHPArraytoPropValueArray(valueEntry, lpBase, &cValues, &lpProp TSRMLS_CC);
  1029. if(MAPI_G(hr) != hrSuccess) {
  1030. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_PROPERTY or RES_CONTENT, Wrong data in field VALUE ");
  1031. return MAPI_G(hr);
  1032. }
  1033. }else{
  1034. // backward compatibility code <= 5.20
  1035. MAPI_G(hr) = MAPIAllocateMore(sizeof(SPropValue), lpBase, (void **)&lpProp);
  1036. if (MAPI_G(hr) != hrSuccess)
  1037. return MAPI_G(hr);
  1038. lpProp->dwAlignPad = 0;
  1039. if (lpRes->rt == RES_PROPERTY)
  1040. lpProp->ulPropTag = lpRes->res.resProperty.ulPropTag;
  1041. else
  1042. lpProp->ulPropTag = lpRes->res.resContent.ulPropTag;
  1043. switch (PROP_TYPE(lpProp->ulPropTag)) { // sets in either resContent or resProperty
  1044. case PT_STRING8:
  1045. convert_to_string_ex(valueEntry);
  1046. MAPI_G(hr) = MAPIAllocateMore(valueEntry->value.str->len+1, lpBase, (void **)&lpProp->Value.lpszA);
  1047. if(MAPI_G(hr) != hrSuccess)
  1048. return MAPI_G(hr);
  1049. strncpy(lpProp->Value.lpszA, valueEntry->value.str->val, valueEntry->value.str->len+1);
  1050. break;
  1051. case PT_UNICODE:
  1052. return MAPI_G(hr) = MAPI_E_NO_SUPPORT;
  1053. case PT_LONG:
  1054. convert_to_long_ex(valueEntry);
  1055. lpProp->Value.l = valueEntry->value.lval;
  1056. break;
  1057. case PT_LONGLONG:
  1058. convert_to_double_ex(valueEntry);
  1059. lpProp->Value.li.QuadPart = (LONGLONG)valueEntry->value.dval;
  1060. break;
  1061. case PT_SHORT:
  1062. convert_to_long_ex(valueEntry);
  1063. lpProp->Value.i = (short) valueEntry->value.lval;
  1064. break;
  1065. case PT_DOUBLE:
  1066. convert_to_double_ex(valueEntry);
  1067. lpProp->Value.dbl = valueEntry->value.dval;
  1068. break;
  1069. case PT_FLOAT:
  1070. convert_to_double_ex(valueEntry);
  1071. lpProp->Value.flt = (float) valueEntry->value.dval;
  1072. break;
  1073. case PT_BOOLEAN:
  1074. convert_to_boolean_ex(valueEntry);
  1075. lpProp->Value.b = (Z_TYPE_P(valueEntry) == IS_TRUE);
  1076. break;
  1077. case PT_SYSTIME:
  1078. convert_to_long_ex(valueEntry);
  1079. UnixTimeToFileTime(valueEntry->value.lval, &lpProp->Value.ft);
  1080. break;
  1081. case PT_BINARY:
  1082. convert_to_string_ex(valueEntry);
  1083. lpProp->Value.bin.cb = valueEntry->value.str->len;
  1084. MAPI_G(hr) = MAPIAllocateMore(valueEntry->value.str->len, lpBase, (void **) &lpProp->Value.bin.lpb);
  1085. if(MAPI_G(hr) != hrSuccess)
  1086. return MAPI_G(hr);
  1087. memcpy(lpProp->Value.bin.lpb, valueEntry->value.str->val, valueEntry->value.str->len);
  1088. break;
  1089. case PT_APPTIME:
  1090. convert_to_double_ex(valueEntry);
  1091. lpProp->Value.at = valueEntry->value.dval;
  1092. break;
  1093. case PT_CLSID:
  1094. convert_to_string_ex(valueEntry);
  1095. if (valueEntry->value.str->len != sizeof(GUID)) {
  1096. php_error_docref(NULL TSRMLS_CC, E_WARNING, "invalid value for PT_CLSID property in proptag 0x%08X", lpProp->ulPropTag);
  1097. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1098. }
  1099. MAPI_G(hr) = MAPIAllocateMore(sizeof(GUID), lpBase, (void **)&lpProp->Value.lpguid);
  1100. memcpy(lpProp->Value.lpguid, valueEntry->value.str->val, sizeof(GUID));
  1101. break;
  1102. default:
  1103. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_PROPERTY or RES_CONTENT, field VALUE no backward compatibility support");
  1104. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1105. break;
  1106. }
  1107. }
  1108. if (lpRes->rt == RES_PROPERTY)
  1109. lpRes->res.resProperty.lpProp = lpProp;
  1110. else
  1111. lpRes->res.resContent.lpProp = lpProp;
  1112. }
  1113. break;
  1114. case RES_COMPAREPROPS:
  1115. // RELOP
  1116. if ((valueEntry = zend_hash_index_find(dataHash, RELOP)) == NULL) {
  1117. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_COMPAREPROPS, Missing field RELOP");
  1118. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1119. }
  1120. convert_to_long_ex(valueEntry);
  1121. lpRes->res.resCompareProps.relop = valueEntry->value.lval;
  1122. // ULPROPTAG1
  1123. if ((valueEntry = zend_hash_index_find(dataHash, ULPROPTAG1)) == NULL) {
  1124. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_COMPAREPROPS, Missing field ULPROPTAG1");
  1125. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1126. }
  1127. convert_to_long_ex(valueEntry);
  1128. lpRes->res.resCompareProps.ulPropTag1 = valueEntry->value.lval;
  1129. // ULPROPTAG2
  1130. if ((valueEntry = zend_hash_index_find(dataHash, ULPROPTAG2)) == NULL) {
  1131. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_COMPAREPROPS, Missing field ULPROPTAG2");
  1132. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1133. }
  1134. convert_to_long_ex(valueEntry);
  1135. lpRes->res.resCompareProps.ulPropTag2 = valueEntry->value.lval;
  1136. break;
  1137. case RES_BITMASK:
  1138. // ULTYPE
  1139. if ((valueEntry = zend_hash_index_find(dataHash, ULTYPE)) == NULL) {
  1140. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_BITMASK, Missing field ULTYPE");
  1141. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1142. }
  1143. convert_to_long_ex(valueEntry);
  1144. lpRes->res.resBitMask.relBMR = valueEntry->value.lval;
  1145. // ULMASK
  1146. if ((valueEntry = zend_hash_index_find(dataHash, ULMASK)) == NULL) {
  1147. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_BITMASK, Missing field ULMASK");
  1148. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1149. }
  1150. convert_to_long_ex(valueEntry);
  1151. lpRes->res.resBitMask.ulMask = valueEntry->value.lval;
  1152. // ULPROPTAG
  1153. if ((valueEntry = zend_hash_index_find(dataHash, ULPROPTAG)) == NULL) {
  1154. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_BITMASK, Missing field ULPROPTAG");
  1155. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1156. }
  1157. convert_to_long_ex(valueEntry);
  1158. lpRes->res.resBitMask.ulPropTag = valueEntry->value.lval;
  1159. break;
  1160. case RES_SIZE:
  1161. // CB
  1162. if ((valueEntry = zend_hash_index_find(dataHash, CB)) == NULL) {
  1163. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_SIZE, Missing field CB");
  1164. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1165. }
  1166. convert_to_long_ex(valueEntry);
  1167. lpRes->res.resSize.cb = valueEntry->value.lval;
  1168. // RELOP
  1169. if ((valueEntry = zend_hash_index_find(dataHash, RELOP)) == NULL) {
  1170. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_SIZE, Missing field RELOP");
  1171. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1172. }
  1173. convert_to_long_ex(valueEntry);
  1174. lpRes->res.resSize.relop = valueEntry->value.lval;
  1175. // ULPROPTAG
  1176. if ((valueEntry = zend_hash_index_find(dataHash, ULPROPTAG)) == NULL) {
  1177. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_SIZE, Missing field ULPROPTAG");
  1178. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1179. }
  1180. convert_to_long_ex(valueEntry);
  1181. lpRes->res.resSize.ulPropTag = valueEntry->value.lval;
  1182. break;
  1183. case RES_EXIST:
  1184. // ULPROPTAG
  1185. if ((valueEntry = zend_hash_index_find(dataHash, ULPROPTAG)) == NULL) {
  1186. php_error_docref(NULL TSRMLS_CC, E_WARNING, "RES_EXIST, Missing field ULPROPTAG");
  1187. return MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1188. }
  1189. convert_to_long_ex(valueEntry);
  1190. lpRes->res.resExist.ulPropTag = valueEntry->value.lval;
  1191. break;
  1192. default:
  1193. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown restriction type");
  1194. break;
  1195. }
  1196. return MAPI_G(hr);
  1197. }
  1198. HRESULT PHPArraytoSRestriction(zval *phpVal, void* lpBase, LPSRestriction *lppRes TSRMLS_DC) {
  1199. LPSRestriction lpRes = NULL;
  1200. MAPI_G(hr) = MAPI_ALLOC(sizeof(SRestriction), lpBase, (void **)&lpRes);
  1201. if(MAPI_G(hr) != hrSuccess)
  1202. goto exit;
  1203. MAPI_G(hr) = PHPArraytoSRestriction(phpVal, lpBase ? lpBase : lpRes, lpRes TSRMLS_CC);
  1204. if(MAPI_G(hr) != hrSuccess)
  1205. goto exit;
  1206. *lppRes = lpRes;
  1207. exit:
  1208. if (MAPI_G(hr) != hrSuccess && lpBase == NULL)
  1209. MAPIFreeBuffer(lpRes);
  1210. return MAPI_G(hr);
  1211. }
  1212. HRESULT SRestrictiontoPHPArray(LPSRestriction lpRes, int level, zval *ret TSRMLS_DC) {
  1213. zval entry;
  1214. char key[16];
  1215. zval array;
  1216. zval props;
  1217. zval restriction;
  1218. if (!lpRes) {
  1219. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No restriction in SRestrictiontoPHPArray");
  1220. return MAPI_E_INVALID_PARAMETER;
  1221. }
  1222. // use define depth
  1223. if (level > 16)
  1224. return MAPI_G(hr) = MAPI_E_TOO_COMPLEX;
  1225. array_init(ret);
  1226. switch (lpRes->rt) {
  1227. case RES_AND:
  1228. array_init(&array);
  1229. for (ULONG c = 0; c < lpRes->res.resAnd.cRes; ++c) {
  1230. sprintf(key, "%i", c);
  1231. MAPI_G(hr) = SRestrictiontoPHPArray(&lpRes->res.resAnd.lpRes[c], level+1, &entry TSRMLS_CC);
  1232. if (MAPI_G(hr) != hrSuccess)
  1233. return MAPI_G(hr);
  1234. add_assoc_zval(&array, key, &entry);
  1235. }
  1236. add_assoc_long(ret, "0", RES_AND);
  1237. add_assoc_zval(ret, "1", &array);
  1238. break;
  1239. case RES_OR:
  1240. array_init(&array);
  1241. for (ULONG c = 0; c < lpRes->res.resOr.cRes; ++c) {
  1242. sprintf(key, "%i", c);
  1243. MAPI_G(hr) = SRestrictiontoPHPArray(&lpRes->res.resOr.lpRes[c], level+1, &entry TSRMLS_CC);
  1244. if (MAPI_G(hr) != hrSuccess)
  1245. return MAPI_G(hr);
  1246. add_assoc_zval(&array, key, &entry);
  1247. }
  1248. add_assoc_long(ret, "0", RES_OR);
  1249. add_assoc_zval(ret, "1", &array);
  1250. break;
  1251. case RES_NOT:
  1252. // although it's only one value, it still is wrapped in an array.
  1253. array_init(&array);
  1254. MAPI_G(hr) = SRestrictiontoPHPArray(lpRes->res.resNot.lpRes, level+1, &entry TSRMLS_CC);
  1255. if (MAPI_G(hr) != hrSuccess)
  1256. return MAPI_G(hr);
  1257. add_assoc_zval(&array, "0", &entry);
  1258. add_assoc_long(ret, "0", RES_NOT);
  1259. add_assoc_zval(ret, "1", &array);
  1260. break;
  1261. case RES_CONTENT:
  1262. MAPI_G(hr) = PropValueArraytoPHPArray(1, lpRes->res.resContent.lpProp, &props TSRMLS_CC);
  1263. if (MAPI_G(hr) != hrSuccess)
  1264. return MAPI_G(hr);
  1265. array_init(&array);
  1266. sprintf(key, "%i", VALUE);
  1267. add_assoc_zval(&array, key, &props);
  1268. sprintf(key, "%i", ULPROPTAG);
  1269. add_assoc_long(&array, key, PropTagToPHPTag(lpRes->res.resContent.ulPropTag));
  1270. sprintf(key, "%i", FUZZYLEVEL);
  1271. add_assoc_long(&array, key, (LONG)lpRes->res.resContent.ulFuzzyLevel);
  1272. add_assoc_long(ret, "0", RES_CONTENT);
  1273. add_assoc_zval(ret, "1", &array);
  1274. break;
  1275. case RES_PROPERTY:
  1276. MAPI_G(hr) = PropValueArraytoPHPArray(1, lpRes->res.resProperty.lpProp, &props TSRMLS_CC);
  1277. if (MAPI_G(hr) != hrSuccess)
  1278. return MAPI_G(hr);
  1279. array_init(&array);
  1280. sprintf(key, "%i", RELOP);
  1281. add_assoc_long(&array, key, (LONG)lpRes->res.resProperty.relop);
  1282. sprintf(key, "%i", ULPROPTAG);
  1283. add_assoc_long(&array, key, PropTagToPHPTag(lpRes->res.resProperty.ulPropTag));
  1284. sprintf(key, "%i", VALUE);
  1285. add_assoc_zval(&array, key, &props);
  1286. add_assoc_long(ret, "0", RES_PROPERTY);
  1287. add_assoc_zval(ret, "1", &array);
  1288. break;
  1289. case RES_COMPAREPROPS:
  1290. array_init(&array);
  1291. sprintf(key, "%i", RELOP);
  1292. add_assoc_long(&array, key, (LONG)lpRes->res.resCompareProps.relop);
  1293. sprintf(key, "%i", ULPROPTAG1);
  1294. add_assoc_long(&array, key, PropTagToPHPTag(lpRes->res.resCompareProps.ulPropTag1));
  1295. sprintf(key, "%i", ULPROPTAG2);
  1296. add_assoc_long(&array, key, PropTagToPHPTag(lpRes->res.resCompareProps.ulPropTag2));
  1297. add_assoc_long(ret, "0", RES_COMPAREPROPS);
  1298. add_assoc_zval(ret, "1", &array);
  1299. break;
  1300. case RES_BITMASK:
  1301. array_init(&array);
  1302. sprintf(key, "%i", ULTYPE);
  1303. add_assoc_long(&array, key, (LONG)lpRes->res.resBitMask.relBMR);
  1304. sprintf(key, "%i", ULPROPTAG);
  1305. add_assoc_long(&array, key, PropTagToPHPTag(lpRes->res.resBitMask.ulPropTag));
  1306. sprintf(key, "%i", ULMASK);
  1307. add_assoc_long(&array, key, (LONG)lpRes->res.resBitMask.ulMask);
  1308. add_assoc_long(ret, "0", RES_BITMASK);
  1309. add_assoc_zval(ret, "1", &array);
  1310. break;
  1311. case RES_SIZE:
  1312. array_init(&array);
  1313. sprintf(key, "%i", RELOP);
  1314. add_assoc_long(&array, key, (LONG)lpRes->res.resSize.relop);
  1315. sprintf(key, "%i", ULPROPTAG);
  1316. add_assoc_long(&array, key, PropTagToPHPTag(lpRes->res.resSize.ulPropTag));
  1317. sprintf(key, "%i", CB);
  1318. add_assoc_long(&array, key, (LONG)lpRes->res.resSize.cb);
  1319. add_assoc_long(ret, "0", RES_SIZE);
  1320. add_assoc_zval(ret, "1", &array);
  1321. break;
  1322. case RES_EXIST:
  1323. array_init(&array);
  1324. sprintf(key, "%i", ULPROPTAG);
  1325. add_assoc_long(&array, key, PropTagToPHPTag(lpRes->res.resExist.ulPropTag));
  1326. add_assoc_long(ret, "0", RES_EXIST);
  1327. add_assoc_zval(ret, "1", &array);
  1328. break;
  1329. case RES_SUBRESTRICTION:
  1330. MAPI_G(hr) = SRestrictiontoPHPArray(lpRes->res.resSub.lpRes, level+1, &restriction TSRMLS_CC);
  1331. if (MAPI_G(hr) != hrSuccess)
  1332. return MAPI_G(hr);
  1333. array_init(&array);
  1334. sprintf(key, "%i", ULPROPTAG);
  1335. add_assoc_long(&array, key, PropTagToPHPTag(lpRes->res.resSub.ulSubObject));
  1336. sprintf(key, "%i", RESTRICTION);
  1337. add_assoc_zval(&array, key, &restriction);
  1338. add_assoc_long(ret, "0", RES_SUBRESTRICTION);
  1339. add_assoc_zval(ret, "1", &array);
  1340. break;
  1341. case RES_COMMENT:
  1342. MAPI_G(hr) = PropValueArraytoPHPArray(lpRes->res.resComment.cValues, lpRes->res.resComment.lpProp, &props TSRMLS_CC);
  1343. if (MAPI_G(hr) != hrSuccess)
  1344. return MAPI_G(hr);
  1345. MAPI_G(hr) = SRestrictiontoPHPArray(lpRes->res.resComment.lpRes, level+1, &restriction TSRMLS_CC);
  1346. if (MAPI_G(hr) != hrSuccess)
  1347. return MAPI_G(hr);
  1348. array_init(&array);
  1349. sprintf(key, "%i", PROPS);
  1350. add_assoc_zval(&array, key, &props);
  1351. sprintf(key, "%i", RESTRICTION);
  1352. add_assoc_zval(&array, key, &restriction);
  1353. add_assoc_long(ret, "0", RES_COMMENT);
  1354. add_assoc_zval(ret, "1", &array);
  1355. break;
  1356. };
  1357. return MAPI_G(hr);
  1358. }
  1359. /**
  1360. * Function to conver a PropTagArray to a PHP Array
  1361. *
  1362. */
  1363. HRESULT PropTagArraytoPHPArray(ULONG cValues, LPSPropTagArray lpPropTagArray, zval *zvalRet TSRMLS_DC)
  1364. {
  1365. unsigned int i = 0;
  1366. MAPI_G(hr) = hrSuccess;
  1367. array_init(zvalRet);
  1368. for (i = 0; i < cValues; ++i)
  1369. add_next_index_long(zvalRet, PropTagToPHPTag(lpPropTagArray->aulPropTag[i]));
  1370. return MAPI_G(hr);
  1371. }
  1372. /**
  1373. * Function to convert a PropValueArray to a PHP Array
  1374. *
  1375. *
  1376. */
  1377. HRESULT PropValueArraytoPHPArray(ULONG cValues, LPSPropValue pPropValueArray, zval *zval_prop_value TSRMLS_DC)
  1378. {
  1379. // local
  1380. zval zval_mvprop_value; // mvprops converts
  1381. zval zval_action_array; // action converts
  1382. zval zval_action_value; // action converts
  1383. zval zval_alist_value; // adrlist in action convert
  1384. LPSPropValue pPropValue;
  1385. ULONG col, j;
  1386. char ulKey[16];
  1387. ACTIONS *lpActions = NULL;
  1388. LPSRestriction lpRestriction = NULL;
  1389. convert_context converter;
  1390. MAPI_G(hr) = hrSuccess;
  1391. array_init(zval_prop_value);
  1392. for (col = 0; col < cValues; ++col) {
  1393. char pulproptag[16];
  1394. pPropValue = &pPropValueArray[col];
  1395. /*
  1396. * PHP wants a string as array key. PHP will transform this to zval integer when possible.
  1397. * Because MAPI works with ULONGS, some properties (namedproperties) are bigger than LONG_MAX
  1398. * and they will be stored as a zval string.
  1399. * To prevent this we cast the ULONG to a signed long. The number will look a bit weird but it
  1400. * will work.
  1401. */
  1402. sprintf(pulproptag, "%i",PropTagToPHPTag(pPropValue->ulPropTag));
  1403. switch(PROP_TYPE(pPropValue->ulPropTag)) {
  1404. case PT_NULL:
  1405. add_assoc_null(zval_prop_value, pulproptag);
  1406. break;
  1407. case PT_LONG:
  1408. add_assoc_long(zval_prop_value, pulproptag, pPropValue->Value.l);
  1409. break;
  1410. case PT_SHORT:
  1411. add_assoc_long(zval_prop_value, pulproptag, pPropValue->Value.i);
  1412. break;
  1413. case PT_DOUBLE:
  1414. add_assoc_double(zval_prop_value, pulproptag, pPropValue->Value.dbl);
  1415. break;
  1416. case PT_LONGLONG:
  1417. add_assoc_double(zval_prop_value, pulproptag, pPropValue->Value.li.QuadPart);
  1418. break;
  1419. case PT_FLOAT:
  1420. add_assoc_double(zval_prop_value, pulproptag, pPropValue->Value.flt);
  1421. break;
  1422. case PT_BOOLEAN:
  1423. add_assoc_bool(zval_prop_value, pulproptag, pPropValue->Value.b);
  1424. break;
  1425. case PT_STRING8:
  1426. add_assoc_string(zval_prop_value, pulproptag, pPropValue->Value.lpszA);
  1427. break;
  1428. case PT_UNICODE:
  1429. add_assoc_string(zval_prop_value, pulproptag, converter.convert_to<char*>(pPropValue->Value.lpszW));
  1430. break;
  1431. case PT_BINARY:
  1432. add_assoc_stringl(zval_prop_value, pulproptag, (char *)pPropValue->Value.bin.lpb,pPropValue->Value.bin.cb);
  1433. break;
  1434. case PT_CURRENCY:
  1435. // to implement
  1436. break;
  1437. case PT_ERROR:
  1438. add_assoc_long(zval_prop_value, pulproptag, (LONG)pPropValue->Value.err);
  1439. break;
  1440. case PT_APPTIME:
  1441. add_assoc_double(zval_prop_value, pulproptag, pPropValue->Value.at);
  1442. break;
  1443. case PT_SYSTIME:
  1444. // convert time to Unix timestamp
  1445. add_assoc_long(zval_prop_value, pulproptag,
  1446. FileTimeToUnixTime(pPropValue->Value.ft.dwHighDateTime,pPropValue->Value.ft.dwLowDateTime));
  1447. break;
  1448. case PT_CLSID:
  1449. add_assoc_stringl(zval_prop_value, pulproptag, (char *)pPropValue->Value.lpguid, sizeof(GUID));
  1450. break;
  1451. case PT_MV_I2:
  1452. array_init(&zval_mvprop_value);
  1453. for (j = 0; j < pPropValue->Value.MVi.cValues; ++j) {
  1454. sprintf(ulKey, "%i", j);
  1455. add_assoc_long(&zval_mvprop_value, ulKey, pPropValue->Value.MVi.lpi[j]);
  1456. }
  1457. add_assoc_zval(zval_prop_value, pulproptag, &zval_mvprop_value);
  1458. break;
  1459. case PT_MV_LONG:
  1460. array_init(&zval_mvprop_value);
  1461. for (j = 0; j < pPropValue->Value.MVl.cValues; ++j) {
  1462. sprintf(ulKey, "%i", j);
  1463. add_assoc_long(&zval_mvprop_value, ulKey, pPropValue->Value.MVl.lpl[j]);
  1464. }
  1465. add_assoc_zval(zval_prop_value, pulproptag, &zval_mvprop_value);
  1466. break;
  1467. case PT_MV_R4:
  1468. array_init(&zval_mvprop_value);
  1469. for (j = 0; j < pPropValue->Value.MVflt.cValues; ++j) {
  1470. sprintf(ulKey, "%i", j);
  1471. add_assoc_double(&zval_mvprop_value, ulKey, pPropValue->Value.MVflt.lpflt[j]);
  1472. }
  1473. add_assoc_zval(zval_prop_value, pulproptag, &zval_mvprop_value);
  1474. break;
  1475. case PT_MV_DOUBLE:
  1476. array_init(&zval_mvprop_value);
  1477. for (j = 0; j < pPropValue->Value.MVdbl.cValues; ++j) {
  1478. sprintf(ulKey, "%i", j);
  1479. add_assoc_double(&zval_mvprop_value, ulKey, pPropValue->Value.MVdbl.lpdbl[j]);
  1480. }
  1481. add_assoc_zval(zval_prop_value, pulproptag, &zval_mvprop_value);
  1482. break;
  1483. case PT_MV_APPTIME:
  1484. array_init(&zval_mvprop_value);
  1485. for (j = 0; j < pPropValue->Value.MVat.cValues; ++j) {
  1486. sprintf(ulKey, "%i", j);
  1487. add_assoc_double(&zval_mvprop_value, ulKey, pPropValue->Value.MVat.lpat[j]);
  1488. }
  1489. add_assoc_zval(zval_prop_value, pulproptag, &zval_mvprop_value);
  1490. break;
  1491. case PT_MV_SYSTIME:
  1492. array_init(&zval_mvprop_value);
  1493. for (j = 0; j < pPropValue->Value.MVft.cValues; ++j) {
  1494. sprintf(ulKey, "%i", j);
  1495. add_assoc_long(&zval_mvprop_value, ulKey,
  1496. FileTimeToUnixTime(pPropValue->Value.MVft.lpft[j].dwHighDateTime,
  1497. pPropValue->Value.MVft.lpft[j].dwLowDateTime));
  1498. }
  1499. add_assoc_zval(zval_prop_value, pulproptag, &zval_mvprop_value);
  1500. break;
  1501. case PT_MV_BINARY:
  1502. array_init(&zval_mvprop_value);
  1503. for (j = 0; j < pPropValue->Value.MVbin.cValues; ++j) {
  1504. sprintf(ulKey, "%i", j);
  1505. add_assoc_stringl(&zval_mvprop_value, ulKey,
  1506. (char*)pPropValue->Value.MVbin.lpbin[j].lpb, pPropValue->Value.MVbin.lpbin[j].cb);
  1507. }
  1508. add_assoc_zval(zval_prop_value, pulproptag, &zval_mvprop_value);
  1509. break;
  1510. case PT_MV_STRING8:
  1511. array_init(&zval_mvprop_value);
  1512. for (j = 0; j < pPropValue->Value.MVszA.cValues; ++j) {
  1513. sprintf(ulKey, "%i", j);
  1514. add_assoc_string(&zval_mvprop_value, ulKey, pPropValue->Value.MVszA.lppszA[j]);
  1515. }
  1516. add_assoc_zval(zval_prop_value, pulproptag, &zval_mvprop_value);
  1517. break;
  1518. case PT_MV_UNICODE:
  1519. array_init(&zval_mvprop_value);
  1520. for (j = 0; j < pPropValue->Value.MVszW.cValues; ++j) {
  1521. sprintf(ulKey, "%i", j);
  1522. add_assoc_string(&zval_mvprop_value, ulKey, converter.convert_to<char*>(pPropValue->Value.MVszW.lppszW[j]));
  1523. }
  1524. add_assoc_zval(zval_prop_value, pulproptag, &zval_mvprop_value);
  1525. break;
  1526. case PT_MV_CLSID:
  1527. array_init(&zval_mvprop_value);
  1528. for (j = 0; j < pPropValue->Value.MVguid.cValues; ++j) {
  1529. sprintf(ulKey, "%i", j);
  1530. add_assoc_stringl(&zval_mvprop_value, pulproptag, (char *)&pPropValue->Value.MVguid.lpguid[j], sizeof(GUID));
  1531. }
  1532. add_assoc_zval(zval_prop_value, pulproptag, &zval_mvprop_value);
  1533. break;
  1534. //case PT_MV_CURRENCY:
  1535. //case PT_MV_I8:
  1536. // rules table properties
  1537. case PT_ACTIONS:
  1538. lpActions = (ACTIONS*)pPropValue->Value.lpszA;
  1539. array_init(&zval_action_array);
  1540. for (j = 0; j < lpActions->cActions; ++j) {
  1541. array_init(&zval_action_value);
  1542. add_assoc_long(&zval_action_value, "action", lpActions->lpAction[j].acttype);
  1543. add_assoc_long(&zval_action_value, "flags", lpActions->lpAction[j].ulFlags);
  1544. add_assoc_long(&zval_action_value, "flavor", lpActions->lpAction[j].ulActionFlavor);
  1545. switch (lpActions->lpAction[j].acttype) {
  1546. case OP_MOVE:
  1547. case OP_COPY:
  1548. add_assoc_stringl(&zval_action_value, "storeentryid",
  1549. (char*)lpActions->lpAction[j].actMoveCopy.lpStoreEntryId,
  1550. lpActions->lpAction[j].actMoveCopy.cbStoreEntryId);
  1551. add_assoc_stringl(&zval_action_value, "folderentryid",
  1552. (char*)lpActions->lpAction[j].actMoveCopy.lpFldEntryId,
  1553. lpActions->lpAction[j].actMoveCopy.cbFldEntryId);
  1554. break;
  1555. case OP_REPLY:
  1556. case OP_OOF_REPLY:
  1557. add_assoc_stringl(&zval_action_value, "replyentryid",
  1558. (char*)lpActions->lpAction[j].actReply.lpEntryId,
  1559. lpActions->lpAction[j].actReply.cbEntryId);
  1560. add_assoc_stringl(&zval_action_value, "replyguid", (char*)&lpActions->lpAction[j].actReply.guidReplyTemplate, sizeof(GUID));
  1561. break;
  1562. case OP_DEFER_ACTION:
  1563. add_assoc_stringl(&zval_action_value, "dam",
  1564. (char*)lpActions->lpAction[j].actDeferAction.pbData,
  1565. lpActions->lpAction[j].actDeferAction.cbData);
  1566. break;
  1567. case OP_BOUNCE:
  1568. add_assoc_long(&zval_action_value, "code", lpActions->lpAction[j].scBounceCode);
  1569. break;
  1570. case OP_FORWARD:
  1571. case OP_DELEGATE:
  1572. MAPI_G(hr) = RowSettoPHPArray((LPSRowSet)lpActions->lpAction[j].lpadrlist, &zval_alist_value TSRMLS_CC); // binary compatible
  1573. if(MAPI_G(hr) != hrSuccess)
  1574. return MAPI_G(hr);
  1575. add_assoc_zval(&zval_action_value, "adrlist", &zval_alist_value);
  1576. break;
  1577. case OP_TAG:
  1578. MAPI_G(hr) = PropValueArraytoPHPArray(1, &lpActions->lpAction[j].propTag, &zval_alist_value TSRMLS_CC);
  1579. if(MAPI_G(hr) != hrSuccess)
  1580. return MAPI_G(hr);
  1581. add_assoc_zval(&zval_action_value, "proptag", &zval_alist_value);
  1582. break;
  1583. case OP_DELETE:
  1584. case OP_MARK_AS_READ:
  1585. // nothing to add
  1586. break;
  1587. };
  1588. sprintf(ulKey, "%i", j);
  1589. add_assoc_zval(&zval_action_array, ulKey, &zval_action_value);
  1590. }
  1591. add_assoc_zval(zval_prop_value, pulproptag, &zval_action_array);
  1592. break;
  1593. case PT_SRESTRICTION:
  1594. lpRestriction = (LPSRestriction)pPropValue->Value.lpszA;
  1595. MAPI_G(hr) = SRestrictiontoPHPArray(lpRestriction, 0, &zval_action_value TSRMLS_CC);
  1596. if (MAPI_G(hr) != hrSuccess)
  1597. continue;
  1598. add_assoc_zval(zval_prop_value, pulproptag, &zval_action_value);
  1599. break;
  1600. }
  1601. }
  1602. return MAPI_G(hr);
  1603. }
  1604. HRESULT RowSettoPHPArray(LPSRowSet lpRowSet, zval *ret TSRMLS_DC) {
  1605. zval zval_prop_value;
  1606. ULONG crow = 0;
  1607. MAPI_G(hr) = hrSuccess;
  1608. array_init(ret);
  1609. // make a PHP-array from the rowset resource.
  1610. for (crow = 0; crow < lpRowSet->cRows; ++crow) {
  1611. PropValueArraytoPHPArray(lpRowSet->aRow[crow].cValues, lpRowSet->aRow[crow].lpProps, &zval_prop_value TSRMLS_CC);
  1612. zend_hash_next_index_insert_new(HASH_OF(ret), &zval_prop_value);
  1613. }
  1614. return MAPI_G(hr);
  1615. }
  1616. /*
  1617. * Convert from READSTATE array to PHP. Returns a list of arrays, each containing "sourcekey" and "flags" per entry
  1618. */
  1619. HRESULT ReadStateArraytoPHPArray(ULONG cValues, LPREADSTATE lpReadStates, zval *pvalRet TSRMLS_DC)
  1620. {
  1621. unsigned int i=0;
  1622. MAPI_G(hr) = hrSuccess;
  1623. array_init(pvalRet);
  1624. for (i = 0; i < cValues; ++i) {
  1625. zval pvalEntry;
  1626. array_init(&pvalEntry);
  1627. add_assoc_stringl(&pvalEntry, "sourcekey", (char *)lpReadStates[i].pbSourceKey, lpReadStates[i].cbSourceKey);
  1628. add_assoc_long(&pvalEntry, "flags", lpReadStates[i].ulFlags);
  1629. add_next_index_zval(pvalRet, &pvalEntry);
  1630. }
  1631. return MAPI_G(hr);
  1632. }
  1633. /*
  1634. * Convert from PHP to READSTATE array.
  1635. */
  1636. HRESULT PHPArraytoReadStateArray(zval *zvalReadStates, void *lpBase, ULONG *lpcValues, LPREADSTATE *lppReadStates TSRMLS_DC)
  1637. {
  1638. LPREADSTATE lpReadStates = NULL;
  1639. HashTable *target_hash = NULL;
  1640. int count;
  1641. zval *pentry = NULL;
  1642. zval *valueEntry = NULL;
  1643. int n = 0, i = 0;
  1644. zend_string *str_sourcekey = zend_string_init("sourcekey", sizeof("sourcekey")-1, 0);
  1645. zend_string *str_flags = zend_string_init("flags", sizeof("flags")-1, 0);
  1646. MAPI_G(hr) = hrSuccess;
  1647. target_hash = HASH_OF(zvalReadStates);
  1648. if (!target_hash) {
  1649. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No target_hash in PHPArraytoReadStateArray");
  1650. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1651. goto exit;
  1652. }
  1653. count = zend_hash_num_elements(Z_ARRVAL_P(zvalReadStates));
  1654. MAPI_G(hr) = MAPI_ALLOC(sizeof(READSTATE) * count, lpBase, (void **) &lpReadStates);
  1655. if(MAPI_G(hr) != hrSuccess)
  1656. goto exit;
  1657. // Reset php pointer
  1658. zend_hash_internal_pointer_reset(target_hash);
  1659. for (i = 0; i < count; ++i) {
  1660. pentry = zend_hash_get_current_data(target_hash);
  1661. if((valueEntry = zend_hash_find(HASH_OF(pentry), str_sourcekey)) == NULL) {
  1662. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No 'sourcekey' entry for one of the entries in the readstate list");
  1663. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1664. goto exit;
  1665. }
  1666. convert_to_string_ex(valueEntry);
  1667. MAPI_G(hr) = MAPIAllocateMore(valueEntry->value.str->len, lpBase ? lpBase : lpReadStates, (void **) &lpReadStates[n].pbSourceKey);
  1668. if(MAPI_G(hr) != hrSuccess)
  1669. goto exit;
  1670. memcpy(lpReadStates[n].pbSourceKey, valueEntry->value.str->val, valueEntry->value.str->len);
  1671. lpReadStates[n].cbSourceKey = valueEntry->value.str->len;
  1672. if((valueEntry = zend_hash_find(HASH_OF(pentry), str_flags)) == NULL) {
  1673. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No 'flags' entry for one of the entries in the readstate list");
  1674. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1675. goto exit;
  1676. }
  1677. convert_to_long_ex(valueEntry);
  1678. lpReadStates[n++].ulFlags = valueEntry->value.lval;
  1679. }
  1680. *lppReadStates = lpReadStates;
  1681. *lpcValues = n;
  1682. exit:
  1683. if (MAPI_G(hr) != hrSuccess && lpBase == NULL)
  1684. MAPIFreeBuffer(lpReadStates);
  1685. zend_string_release(str_sourcekey);
  1686. zend_string_release(str_flags);
  1687. return MAPI_G(hr);
  1688. }
  1689. HRESULT PHPArraytoGUIDArray(zval *phpVal, void *lpBase, ULONG *lpcValues, LPGUID *lppGUIDs TSRMLS_DC)
  1690. {
  1691. HashTable *target_hash = NULL;
  1692. LPGUID lpGUIDs = NULL;
  1693. int count = 0;
  1694. int n = 0;
  1695. int i = 0;
  1696. zval *pentry = NULL;
  1697. MAPI_G(hr) = hrSuccess;
  1698. target_hash = HASH_OF(phpVal);
  1699. if (!target_hash) {
  1700. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No target_hash in PHPArraytoGUIDArray");
  1701. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1702. goto exit;
  1703. }
  1704. count = zend_hash_num_elements(Z_ARRVAL_P(phpVal));
  1705. if (count == 0) {
  1706. *lppGUIDs = NULL;
  1707. *lpcValues = 0;
  1708. goto exit;
  1709. }
  1710. MAPI_G(hr) = MAPI_ALLOC(sizeof(GUID) * count, lpBase, (void **) &lpGUIDs);
  1711. if(MAPI_G(hr) != hrSuccess)
  1712. goto exit;
  1713. zend_hash_internal_pointer_reset(target_hash);
  1714. for (i = 0; i < count; ++i) {
  1715. pentry = zend_hash_get_current_data(target_hash);
  1716. convert_to_string_ex(pentry);
  1717. if(pentry->value.str->len != sizeof(GUID)){
  1718. php_error_docref(NULL TSRMLS_CC, E_WARNING, "GUID must be 16 bytes");
  1719. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1720. goto exit;
  1721. }
  1722. memcpy(&lpGUIDs[n++], pentry->value.str->val, sizeof(GUID));
  1723. zend_hash_move_forward(target_hash);
  1724. }
  1725. *lppGUIDs = lpGUIDs;
  1726. *lpcValues = n;
  1727. exit:
  1728. if (MAPI_G(hr) != hrSuccess && lpBase == NULL)
  1729. MAPIFreeBuffer(lpGUIDs);
  1730. return MAPI_G(hr);
  1731. }
  1732. HRESULT NotificationstoPHPArray(ULONG cNotifs, LPNOTIFICATION lpNotifs, zval *zvalRet TSRMLS_DC)
  1733. {
  1734. zval zvalProps;
  1735. unsigned int i = 0;
  1736. MAPI_G(hr) = hrSuccess;
  1737. array_init(zvalRet);
  1738. for (i = 0; i < cNotifs; ++i) {
  1739. zval zvalNotif;
  1740. array_init(&zvalNotif);
  1741. add_assoc_long(&zvalNotif, "eventtype", lpNotifs[i].ulEventType);
  1742. switch(lpNotifs[i].ulEventType) {
  1743. case fnevNewMail:
  1744. add_assoc_stringl(&zvalNotif, "entryid", (char *)lpNotifs[i].info.newmail.lpEntryID, lpNotifs[i].info.newmail.cbEntryID);
  1745. add_assoc_stringl(&zvalNotif, "parentid", (char *)lpNotifs[i].info.newmail.lpParentID, lpNotifs[i].info.newmail.cbParentID);
  1746. add_assoc_long(&zvalNotif, "flags", lpNotifs[i].info.newmail.ulFlags);
  1747. add_assoc_string(&zvalNotif, "messageclass", (char *)lpNotifs[i].info.newmail.lpszMessageClass);
  1748. add_assoc_long(&zvalNotif, "messageflags", lpNotifs[i].info.newmail.ulMessageFlags);
  1749. break;
  1750. case fnevObjectCreated:
  1751. case fnevObjectDeleted:
  1752. case fnevObjectModified:
  1753. case fnevObjectMoved:
  1754. case fnevObjectCopied:
  1755. case fnevSearchComplete:
  1756. if (lpNotifs[i].info.obj.lpEntryID)
  1757. add_assoc_stringl(&zvalNotif, "entryid", (char *)lpNotifs[i].info.obj.lpEntryID, lpNotifs[i].info.obj.cbEntryID);
  1758. add_assoc_long(&zvalNotif, "objtype", lpNotifs[i].info.obj.ulObjType);
  1759. if (lpNotifs[i].info.obj.lpParentID)
  1760. add_assoc_stringl(&zvalNotif, "parentid", (char *)lpNotifs[i].info.obj.lpParentID, lpNotifs[i].info.obj.cbParentID);
  1761. if (lpNotifs[i].info.obj.lpOldID)
  1762. add_assoc_stringl(&zvalNotif, "oldid", (char *)lpNotifs[i].info.obj.lpOldID, lpNotifs[i].info.obj.cbOldID);
  1763. if (lpNotifs[i].info.obj.lpOldParentID)
  1764. add_assoc_stringl(&zvalNotif, "oldparentid", (char *)lpNotifs[i].info.obj.lpOldParentID, lpNotifs[i].info.obj.cbOldParentID);
  1765. if (lpNotifs[i].info.obj.lpPropTagArray) {
  1766. MAPI_G(hr) = PropTagArraytoPHPArray(lpNotifs[i].info.obj.lpPropTagArray->cValues, lpNotifs[i].info.obj.lpPropTagArray, &zvalProps TSRMLS_CC);
  1767. if (MAPI_G(hr) != hrSuccess)
  1768. return MAPI_G(hr);
  1769. add_assoc_zval(&zvalNotif, "proptagarray", &zvalProps);
  1770. }
  1771. break;
  1772. default:
  1773. break;
  1774. }
  1775. add_next_index_zval(zvalRet, &zvalNotif);
  1776. }
  1777. return MAPI_G(hr);
  1778. }
  1779. HRESULT PHPArraytoSendingOptions(zval *phpArray, sending_options *lpSOPT)
  1780. {
  1781. HRESULT hr = hrSuccess;
  1782. // local
  1783. int count;
  1784. HashTable *target_hash = NULL;
  1785. zval *entry = NULL;
  1786. zend_string *keyIndex;
  1787. zend_ulong numIndex = 0;
  1788. if (!phpArray) {
  1789. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No phpArray in PHPArraytoSendingOptions");
  1790. // not an error
  1791. return hr;
  1792. }
  1793. target_hash = HASH_OF(phpArray);
  1794. if (!target_hash) {
  1795. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No target_hash in PHPArraytoSendingOptions");
  1796. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1797. return hr;
  1798. }
  1799. count = zend_hash_num_elements(target_hash);
  1800. zend_hash_internal_pointer_reset(target_hash);
  1801. for (int i = 0; i < count; ++i) {
  1802. // Gets the element that exist at the current pointer.
  1803. entry = zend_hash_get_current_data(target_hash);
  1804. zend_hash_get_current_key(target_hash, &keyIndex, &numIndex);
  1805. if (strcmp(keyIndex->val, "alternate_boundary") == 0) {
  1806. convert_to_string_ex(entry);
  1807. lpSOPT->alternate_boundary = Z_STRVAL_P(entry);
  1808. } else if (strcmp(keyIndex->val, "no_recipients_workaround") == 0) {
  1809. convert_to_boolean_ex(entry);
  1810. lpSOPT->no_recipients_workaround = (Z_TYPE_P(entry) == IS_TRUE);
  1811. } else if (strcmp(keyIndex->val, "headers_only") == 0) {
  1812. convert_to_boolean_ex(entry);
  1813. lpSOPT->headers_only = (Z_TYPE_P(entry) == IS_TRUE);
  1814. } else if (strcmp(keyIndex->val, "add_received_date") == 0) {
  1815. convert_to_boolean_ex(entry);
  1816. lpSOPT->add_received_date = (Z_TYPE_P(entry) == IS_TRUE);
  1817. } else if (strcmp(keyIndex->val, "use_tnef") == 0) {
  1818. convert_to_long_ex(entry);
  1819. lpSOPT->use_tnef = Z_LVAL_P(entry);
  1820. } else if (strcmp(keyIndex->val, "force_utf8") == 0) {
  1821. convert_to_boolean_ex(entry);
  1822. lpSOPT->force_utf8 = (Z_TYPE_P(entry) == IS_TRUE);
  1823. } else if (strcmp(keyIndex->val, "charset_upgrade") == 0) {
  1824. convert_to_string_ex(entry);
  1825. lpSOPT->charset_upgrade = Z_STRVAL_P(entry);
  1826. } else if (strcmp(keyIndex->val, "allow_send_to_everyone") == 0) {
  1827. convert_to_boolean_ex(entry);
  1828. lpSOPT->force_utf8 = (Z_TYPE_P(entry) == IS_TRUE);
  1829. } else {
  1830. // msg_in_msg and enable_dsn not allowed, others unknown
  1831. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown or disallowed sending option %s", keyIndex->val);
  1832. }
  1833. zend_hash_move_forward(target_hash);
  1834. }
  1835. return hr;
  1836. }
  1837. HRESULT PHPArraytoDeliveryOptions(zval *phpArray, delivery_options *lpDOPT)
  1838. {
  1839. HRESULT hr = hrSuccess;
  1840. // local
  1841. int count;
  1842. HashTable *target_hash = NULL;
  1843. zval *entry = NULL;
  1844. zend_string *keyIndex;
  1845. zend_ulong numIndex = 0;
  1846. if (!phpArray) {
  1847. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No phpArray in PHPArraytoDeliveryOptions");
  1848. // not an error
  1849. return hr;
  1850. }
  1851. target_hash = HASH_OF(phpArray);
  1852. if (!target_hash) {
  1853. php_error_docref(NULL TSRMLS_CC, E_WARNING, "No target_hash in PHPArraytoDeliveryOptions");
  1854. MAPI_G(hr) = MAPI_E_INVALID_PARAMETER;
  1855. return hr;
  1856. }
  1857. count = zend_hash_num_elements(target_hash);
  1858. zend_hash_internal_pointer_reset(target_hash);
  1859. for (int i = 0; i < count; ++i) {
  1860. // Gets the element that exist at the current pointer.
  1861. entry = zend_hash_get_current_data(target_hash);
  1862. zend_hash_get_current_key(target_hash, &keyIndex, &numIndex);
  1863. if (strcmp(keyIndex->val, "use_received_date") == 0) {
  1864. convert_to_boolean_ex(entry);
  1865. lpDOPT->use_received_date = (Z_TYPE_P(entry) == IS_TRUE);
  1866. } else if (strcmp(keyIndex->val, "mark_as_read") == 0) {
  1867. convert_to_boolean_ex(entry);
  1868. lpDOPT->mark_as_read = (Z_TYPE_P(entry) == IS_TRUE);
  1869. } else if (strcmp(keyIndex->val, "add_imap_date") == 0) {
  1870. convert_to_boolean_ex(entry);
  1871. lpDOPT->add_imap_data = (Z_TYPE_P(entry) == IS_TRUE);
  1872. } else if (strcmp(keyIndex->val, "parse_smime_signed") == 0) {
  1873. convert_to_boolean_ex(entry);
  1874. lpDOPT->parse_smime_signed = (Z_TYPE_P(entry) == IS_TRUE);
  1875. } else if (strcmp(keyIndex->val, "default_charset") == 0) {
  1876. convert_to_string_ex(entry);
  1877. lpDOPT->ascii_upgrade = Z_STRVAL_P(entry);
  1878. } else {
  1879. // user_entryid not supported, others unknown
  1880. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown or disallowed delivery option %s", keyIndex->val);
  1881. }
  1882. zend_hash_move_forward(target_hash);
  1883. }
  1884. return hr;
  1885. }