Skip to content

Commit 6a55873

Browse files
committed
Merge branch 'simplify_restriction' into 'master'
Util to convert restriction consts into strings See merge request grommunio/mapi-header-php!45
2 parents 9f86f4f + ec7b917 commit 6a55873

File tree

1 file changed

+256
-0
lines changed

1 file changed

+256
-0
lines changed

mapi.util.php

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,259 @@ function getUidFromGoid($goid) {
350350

351351
return null;
352352
}
353+
354+
/**
355+
* Converts a MAPI property tag into a human readable value.
356+
*
357+
* This depends on the definition of the property tag in core
358+
*
359+
* @example prop2Str(0x0037001e) => 'PR_SUBJECT'
360+
*
361+
* @param mixed $property
362+
*
363+
* @return string the symbolic name of the property tag
364+
*/
365+
function prop2Str($property) {
366+
if (is_integer($property)) {
367+
// Retrieve constants categories, zcore provides them in 'Core'
368+
foreach (get_defined_constants(true)['Core'] as $key => $value) {
369+
if ($property == $value && substr($key, 0, 3) == 'PR_') {
370+
return $key;
371+
}
372+
}
373+
374+
return sprintf("0x%08X", $property);
375+
}
376+
377+
return $property;
378+
}
379+
380+
/**
381+
* Converts all constants of restriction into a human readable strings.
382+
*
383+
* @param mixed $restriction
384+
*/
385+
function simplifyRestriction($restriction) {
386+
if (!is_array($restriction)) {
387+
return $restriction;
388+
}
389+
390+
switch ($restriction[0]) {
391+
case RES_AND:
392+
$restriction[0] = "RES_AND";
393+
if (isset($restriction[1][0]) && is_array($restriction[1][0])) {
394+
foreach ($restriction[1] as &$res) {
395+
$res = simplifyRestriction($res);
396+
}
397+
unset($res);
398+
}
399+
elseif (isset($restriction[1]) && $restriction[1]) {
400+
$restriction[1] = simplifyRestriction($restriction[1]);
401+
}
402+
break;
403+
404+
case RES_OR:
405+
$restriction[0] = "RES_OR";
406+
if (isset($restriction[1][0]) && is_array($restriction[1][0])) {
407+
foreach ($restriction[1] as &$res) {
408+
$res = simplifyRestriction($res);
409+
}
410+
unset($res);
411+
}
412+
elseif (isset($restriction[1]) && $restriction[1]) {
413+
$restriction[1] = simplifyRestriction($restriction[1]);
414+
}
415+
break;
416+
417+
case RES_NOT:
418+
$restriction[0] = "RES_NOT";
419+
$restriction[1][0] = simplifyRestriction($restriction[1][0]);
420+
break;
421+
422+
case RES_COMMENT:
423+
$restriction[0] = "RES_COMMENT";
424+
$res = simplifyRestriction($restriction[1][RESTRICTION]);
425+
$props = $restriction[1][PROPS];
426+
427+
foreach ($props as &$prop) {
428+
$propTag = $prop[ULPROPTAG];
429+
$propValue = $prop[VALUE];
430+
431+
unset($prop);
432+
433+
$prop["ULPROPTAG"] = is_string($propTag) ? $propTag : prop2Str($propTag);
434+
$prop["VALUE"] = is_array($propValue) ? $propValue[$propTag] : $propValue;
435+
}
436+
unset($prop, $restriction[1]);
437+
438+
$restriction[1]["RESTRICTION"] = $res;
439+
$restriction[1]["PROPS"] = $props;
440+
break;
441+
442+
case RES_PROPERTY:
443+
$restriction[0] = "RES_PROPERTY";
444+
$propTag = $restriction[1][ULPROPTAG];
445+
$propValue = $restriction[1][VALUE];
446+
$relOp = $restriction[1][RELOP];
447+
448+
unset($restriction[1]);
449+
450+
// relop flags
451+
$relOpFlags = "";
452+
if ($relOp == RELOP_LT) {
453+
$relOpFlags = "RELOP_LT";
454+
}
455+
elseif ($relOp == RELOP_LE) {
456+
$relOpFlags = "RELOP_LE";
457+
}
458+
elseif ($relOp == RELOP_GT) {
459+
$relOpFlags = "RELOP_GT";
460+
}
461+
elseif ($relOp == RELOP_GE) {
462+
$relOpFlags = "RELOP_GE";
463+
}
464+
elseif ($relOp == RELOP_EQ) {
465+
$relOpFlags = "RELOP_EQ";
466+
}
467+
elseif ($relOp == RELOP_NE) {
468+
$relOpFlags = "RELOP_NE";
469+
}
470+
elseif ($relOp == RELOP_RE) {
471+
$relOpFlags = "RELOP_RE";
472+
}
473+
474+
$restriction[1]["RELOP"] = $relOpFlags;
475+
$restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : prop2Str($propTag);
476+
$restriction[1]["VALUE"] = is_array($propValue) ? $propValue[$propTag] : $propValue;
477+
break;
478+
479+
case RES_CONTENT:
480+
$restriction[0] = "RES_CONTENT";
481+
$propTag = $restriction[1][ULPROPTAG];
482+
$propValue = $restriction[1][VALUE];
483+
$fuzzyLevel = $restriction[1][FUZZYLEVEL];
484+
485+
unset($restriction[1]);
486+
487+
// fuzzy level flags
488+
$levels = [];
489+
490+
if (($fuzzyLevel & FL_SUBSTRING) == FL_SUBSTRING) {
491+
$levels[] = "FL_SUBSTRING";
492+
}
493+
elseif (($fuzzyLevel & FL_PREFIX) == FL_PREFIX) {
494+
$levels[] = "FL_PREFIX";
495+
}
496+
else {
497+
$levels[] = "FL_FULLSTRING";
498+
}
499+
500+
if (($fuzzyLevel & FL_IGNORECASE) == FL_IGNORECASE) {
501+
$levels[] = "FL_IGNORECASE";
502+
}
503+
504+
if (($fuzzyLevel & FL_IGNORENONSPACE) == FL_IGNORENONSPACE) {
505+
$levels[] = "FL_IGNORENONSPACE";
506+
}
507+
508+
if (($fuzzyLevel & FL_LOOSE) == FL_LOOSE) {
509+
$levels[] = "FL_LOOSE";
510+
}
511+
512+
$fuzzyLevelFlags = implode(" | ", $levels);
513+
514+
$restriction[1]["FUZZYLEVEL"] = $fuzzyLevelFlags;
515+
$restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : prop2Str($propTag);
516+
$restriction[1]["VALUE"] = is_array($propValue) ? $propValue[$propTag] : $propValue;
517+
break;
518+
519+
case RES_COMPAREPROPS:
520+
$propTag1 = $restriction[1][ULPROPTAG1];
521+
$propTag2 = $restriction[1][ULPROPTAG2];
522+
523+
unset($restriction[1]);
524+
525+
$restriction[1]["ULPROPTAG1"] = is_string($propTag1) ? $proptag1 : prop2Str($proptag1);
526+
$restriction[1]["ULPROPTAG2"] = is_string($propTag2) ? $propTag2 : prop2Str($propTag2);
527+
break;
528+
529+
case RES_BITMASK:
530+
$restriction[0] = "RES_BITMASK";
531+
$propTag = $restriction[1][ULPROPTAG];
532+
$maskType = $restriction[1][ULTYPE];
533+
$maskValue = $restriction[1][ULMASK];
534+
535+
unset($restriction[1]);
536+
537+
// relop flags
538+
$maskTypeFlags = "";
539+
if ($maskType == BMR_EQZ) {
540+
$maskTypeFlags = "BMR_EQZ";
541+
}
542+
elseif ($maskType == BMR_NEZ) {
543+
$maskTypeFlags = "BMR_NEZ";
544+
}
545+
546+
$restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : prop2Str($propTag);
547+
$restriction[1]["ULTYPE"] = $maskTypeFlags;
548+
$restriction[1]["ULMASK"] = $maskValue;
549+
break;
550+
551+
case RES_SIZE:
552+
$restriction[0] = "RES_SIZE";
553+
$propTag = $restriction[1][ULPROPTAG];
554+
$propValue = $restriction[1][CB];
555+
$relOp = $restriction[1][RELOP];
556+
557+
unset($restriction[1]);
558+
559+
// relop flags
560+
$relOpFlags = "";
561+
if ($relOp == RELOP_LT) {
562+
$relOpFlags = "RELOP_LT";
563+
}
564+
elseif ($relOp == RELOP_LE) {
565+
$relOpFlags = "RELOP_LE";
566+
}
567+
elseif ($relOp == RELOP_GT) {
568+
$relOpFlags = "RELOP_GT";
569+
}
570+
elseif ($relOp == RELOP_GE) {
571+
$relOpFlags = "RELOP_GE";
572+
}
573+
elseif ($relOp == RELOP_EQ) {
574+
$relOpFlags = "RELOP_EQ";
575+
}
576+
elseif ($relOp == RELOP_NE) {
577+
$relOpFlags = "RELOP_NE";
578+
}
579+
elseif ($relOp == RELOP_RE) {
580+
$relOpFlags = "RELOP_RE";
581+
}
582+
583+
$restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : prop2Str($propTag);
584+
$restriction[1]["RELOP"] = $relOpFlags;
585+
$restriction[1]["CB"] = $propValue;
586+
break;
587+
588+
case RES_EXIST:
589+
$propTag = $restriction[1][ULPROPTAG];
590+
591+
unset($restriction[1]);
592+
593+
$restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : prop2Str($propTag);
594+
break;
595+
596+
case RES_SUBRESTRICTION:
597+
$propTag = $restriction[1][ULPROPTAG];
598+
$res = simplifyRestriction($restriction[1][RESTRICTION]);
599+
600+
unset($restriction[1]);
601+
602+
$restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : prop2Str($propTag);
603+
$restriction[1]["RESTRICTION"] = $res;
604+
break;
605+
}
606+
607+
return $restriction;
608+
}

0 commit comments

Comments
 (0)