debug.php.dist 10.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
<?php
/**
* This file is only used for debugging purpose, this file isn't needed in
* the release version.
*/

/**
* Set this to let kopano.php exports the JSON
* but please note that you must create the DEBUG_JSONOUT_DIR directory and that
* it is writable by PHP
*/
define("DEBUG_JSONOUT", true);
define("DEBUG_JSONOUT_DIR", "debug_xml/");
define("DEBUG_JSONOUT_GZIP", false);
define("DEBUG_PLUGINS", true);
define("DEBUG_PLUGINS_DISABLE_CACHE", false);

// dump file, must be writable
define("DEBUG_DUMP_FILE", BASE_PATH . 'debug.txt');

// The different value types for the DEBUG_LOADER config option
// This can be any of the following values:
//      LOAD_RELEASE
//              Load the concatenated & compressed files
//      LOAD_DEBUG
//              Load the concatenated files
//      LOAD_SOURCE
//              Load the original source files (for developers)
define("DEBUG_LOADER", LOAD_SOURCE);

// log php errors into apache log
ini_set("log_errors", true);

// end config


//	This PHP error handler, only dumps the error to our dump file, including mapi_last_hresult
error_reporting(E_ALL | E_STRICT);
set_error_handler("kopano_error_handler");

/**
* Custom error handler, here we check to see if it is a MAPI error and dump all info
* to the dump file, and finally we redirect the error back to PHP
*/
function kopano_error_handler($errno, $errstr, $errfile, $errline, $errorContext)
{
	$error = array("msg"=>$errstr, "file"=>$errfile.":".$errline);

	dump($error, "ERROR", true);

	$filterOutErrors = array(E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE, E_WARNING, E_NOTICE);

	if(!in_array($errno, $filterOutErrors))
		throw new ZarafaErrorException($errstr, $errno, $errfile, $errline, $errorContext);
}

/**
* This function "dumps" the contents of $variable to debug.txt (and when requested also a backtrace)
*/
function dump($variable, $title="", $backtrace=false, $resource=false)
{
	$file = fopen(DEBUG_DUMP_FILE, "a+");
	$date = strftime("%d-%b-%Y");
	$time = strftime("%H:%M:%S");
	if($resource) {
		fwrite($file, ("[" . $date . " " . $time . "] " . $title. " - " . var_export(get_resource_type($variable), true) . "\r\n"));
	} else {
		fwrite($file, ("[" . $date . " " . $time . "] " . $title. " - " . var_export($variable, true) . "\r\n"));
	}

	if ($backtrace){
		dump(_debug_backtrace(false));
	}
}

// use this function when you want to dump an array of MAPI properties
function dump_props($variable,$title=""){
	global $_debug_propList;
	if ($_debug_propList===false){
		// caching
		foreach(get_defined_constants() as $key=>$value){
			if (substr($key,0,3)=='PR_'){
				$_debug_propList[$key] = $value;
			}
		}
	}

	foreach($variable as $key=>$value){
		$prop = array_keys($_debug_propList,$key);
		if (count($prop)>0){
			foreach($prop as $k=>$v){
				$variable["0x".str_pad(strtoupper(dechex($key)),8, '0', STR_PAD_LEFT).' '.$v] = $value;
			}
		}else{
			$variable["0x".str_pad(strtoupper(dechex($key)),8, '0', STR_PAD_LEFT)] = $value;
		}
		unset($variable[$key]);
	}
	dump($variable,$title);
}
$_debug_propList = false;

/**
 * This function is used for the in/output by kopano.php to store the JSON to disk
 */
function dump_json($json, $prefix){
	global $debug_json_id;

	if (DEBUG_JSONOUT && is_dir(DEBUG_JSONOUT_DIR)) {
		if (!isset($debug_json_id) || empty($debug_json_id)) {
			$debug_json_id = strftime("%Y%m%d%H%M%S").uniqid("_");
		}

		$data = json_decode($json, true);
		$data = var_export($data, true);

		$fh = fopen(DEBUG_JSONOUT_DIR . $prefix . "_" . $debug_json_id . ".json" . (DEBUG_JSONOUT_GZIP ? ".gz" : ""), "w");
		fwrite($fh, (DEBUG_JSONOUT_GZIP ? gzencode($data) : $data));
		fclose($fh);
	}
}

/**
* internal function to generate a backtrace
*/
function _debug_backtrace($html=true){
  $output = $html?"<br/>\n":"\n";
  foreach(debug_backtrace() as $t){
    if (isset($t['file']) && $t['file']!=__FILE__){
      $output .= $html?'<strong>@</strong> ':'@ ';
      if(isset($t['file'])) {
        $output .= basename($t['file']) . ':' . $t['line'];
      } else {
       $output .= '[PHP inner-code]';
      }
      $output .= ' - ';
      if(isset($t['class'])) $output .= $t['class'] . $t['type'];
      $output .= $t['function'];
      if(isset($t['args']) && sizeof($t['args']) > 0) {
        $output .= '(...)';
      } else {
        $output .= '()';
      }
      $output .= $html?"<br/>\n":"\n";
    }
  }
  return $output;
}

/**
* This function is used for dumping client side restrictions in user readable form
*/
function dump_restriction($restriction) {
	$variable = simplify_restriction($restriction);

	$file = fopen(DEBUG_DUMP_FILE, "a+");
	$date = strftime("%d-%b-%Y");
	$time = strftime("%H:%M:%S");
	fwrite($file, ("[" . $date . " " . $time . "] Restrictions - " . var_export($variable, true) . "\r\n"));
}

/**
 * This function is used to covert all constants of restriction into a human readable strings
 */
function simplify_restriction($restriction) {
	if (!is_array($restriction)){
		return $restriction;
	}

	switch($restriction[0]){
		case RES_AND:
			$restriction[0] = "RES_AND";
			if(isset($restriction[1][0]) && is_array($restriction[1][0])) {
				foreach($restriction[1] as &$res) {
					$res = simplify_restriction($res);
				}
				unset($res);
			} else if(isset($restriction[1]) && $restriction[1]) {
				$restriction[1] = simplify_restriction($restriction[1]);
			}
			break;
		case RES_OR:
			$restriction[0] = "RES_OR";
			if(isset($restriction[1][0]) && is_array($restriction[1][0])) {
				foreach($restriction[1] as &$res) {
					$res = simplify_restriction($res);
				}
				unset($res);
			} else if(isset($restriction[1]) && $restriction[1]) {
				$restriction[1] = simplify_restriction($restriction[1]);
			}
			break;
		case RES_NOT:
			$restriction[0] = "RES_NOT";
			$restriction[1][0] = simplify_restriction($restriction[1][0]);
			break;
		case RES_COMMENT:
			$restriction[0] = "RES_COMMENT";
			$res = simplify_restriction($restriction[1][RESTRICTION]);
			$props = $restriction[1][PROPS];

			foreach($props as &$prop) {
				$propTag = $prop[ULPROPTAG];
				$propValue = $prop[VALUE];

				unset($prop);

				$prop["ULPROPTAG"] = is_string($propTag) ? $propTag : Conversion::property2json($propTag);
				$prop["VALUE"] = is_array($propValue) ? $propValue[$propTag] : $propValue;
			}
			unset($prop);

			unset($restriction[1]);

			$restriction[1]["RESTRICTION"] = $res;
			$restriction[1]["PROPS"] = $props;
			break;
		case RES_PROPERTY:
			$restriction[0] = "RES_PROPERTY";
			$propTag = $restriction[1][ULPROPTAG];
			$propValue = $restriction[1][VALUE];
			$relOp = $restriction[1][RELOP];

			unset($restriction[1]);

			// relop flags
			$relOpFlags = "";
			if($relOp == RELOP_LT) {
				$relOpFlags = "RELOP_LT";
			} else if($relOp == RELOP_LE) {
				$relOpFlags = "RELOP_LE";
			} else if($relOp == RELOP_GT) {
				$relOpFlags = "RELOP_GT";
			} else if($relOp == RELOP_GE) {
				$relOpFlags = "RELOP_GE";
			} else if($relOp == RELOP_EQ) {
				$relOpFlags = "RELOP_EQ";
			} else if($relOp == RELOP_NE) {
				$relOpFlags = "RELOP_NE";
			} else if($relOp == RELOP_RE) {
				$relOpFlags = "RELOP_RE";
			}

			$restriction[1]["RELOP"] = $relOpFlags;
			$restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : Conversion::property2json($propTag);
			$restriction[1]["VALUE"] = is_array($propValue) ? $propValue[$propTag] : $propValue;
			break;
		case RES_CONTENT:
			$restriction[0] = "RES_CONTENT";
			$propTag = $restriction[1][ULPROPTAG];
			$propValue = $restriction[1][VALUE];
			$fuzzyLevel = $restriction[1][FUZZYLEVEL];

			unset($restriction[1]);

			// fuzzy level flags
			$levels = array();

			if (($fuzzyLevel & FL_SUBSTRING) == FL_SUBSTRING)
				$levels[] = "FL_SUBSTRING";
			elseif (($fuzzyLevel & FL_PREFIX) == FL_PREFIX)
				$levels[] = "FL_PREFIX";
			else
				$levels[] = "FL_FULLSTRING";
						
			if (($fuzzyLevel & FL_IGNORECASE) == FL_IGNORECASE)
				$levels[] = "FL_IGNORECASE";
						
			if (($fuzzyLevel & FL_IGNORENONSPACE) == FL_IGNORENONSPACE)
				$levels[] = "FL_IGNORENONSPACE";
			
			if (($fuzzyLevel & FL_LOOSE) == FL_LOOSE)
				$levels[] = "FL_LOOSE";
			
			$fuzzyLevelFlags = implode(" | ", $levels);

			$restriction[1]["FUZZYLEVEL"] = $fuzzyLevelFlags;
			$restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : Conversion::property2json($propTag);
			$restriction[1]["VALUE"] = is_array($propValue) ? $propValue[$propTag] : $propValue;
			break;
		case RES_COMPAREPROPS:
			$propTag1 = $restriction[1][ULPROPTAG1];
			$propTag2 = $restriction[1][ULPROPTAG2];

			unset($restriction[1]);

			$restriction[1]["ULPROPTAG1"] = is_string($propTag1) ? $proptag1 : Conversion::property2json($proptag1);
			$restriction[1]["ULPROPTAG2"] = is_string($propTag2) ? $propTag2 : Conversion::property2json($propTag2);
			break;
		case RES_BITMASK:
			$restriction[0] = "RES_BITMASK";
			$propTag = $restriction[1][ULPROPTAG];
			$maskType = $restriction[1][ULTYPE];
			$maskValue = $restriction[1][ULMASK];

			unset($restriction[1]);

			// relop flags
			$maskTypeFlags = "";
			if($maskType == BMR_EQZ) {
				$maskTypeFlags = "BMR_EQZ";
			} else if($maskType == BMR_NEZ) {
				$maskTypeFlags = "BMR_NEZ";
			}

			$restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : Conversion::property2json($propTag);
			$restriction[1]["ULTYPE"] = $maskTypeFlags;
			$restriction[1]["ULMASK"] = $maskValue;
			break;
		case RES_SIZE:
			$restriction[0] = "RES_SIZE";
			$propTag = $restriction[1][ULPROPTAG];
			$propValue = $restriction[1][CB];
			$relOp = $restriction[1][RELOP];

			unset($restriction[1]);

			// relop flags
			$relOpFlags = "";
			if($relOp == RELOP_LT) {
				$relOpFlags = "RELOP_LT";
			} else if($relOp == RELOP_LE) {
				$relOpFlags = "RELOP_LE";
			} else if($relOp == RELOP_GT) {
				$relOpFlags = "RELOP_GT";
			} else if($relOp == RELOP_GE) {
				$relOpFlags = "RELOP_GE";
			} else if($relOp == RELOP_EQ) {
				$relOpFlags = "RELOP_EQ";
			} else if($relOp == RELOP_NE) {
				$relOpFlags = "RELOP_NE";
			} else if($relOp == RELOP_RE) {
				$relOpFlags = "RELOP_RE";
			}

			$restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : Conversion::property2json($propTag);
			$restriction[1]["RELOP"] = $relOpFlags;
			$restriction[1]["CB"] = $propValue;
			break;
		case RES_EXIST:
			$propTag = $restriction[1][ULPROPTAG];

			unset($restriction[1]);

			$restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : Conversion::property2json($propTag);
			break;
		case RES_SUBRESTRICTION:
			$propTag = $restriction[1][ULPROPTAG];
			$res = simplify_restriction($restriction[1][RESTRICTION]);

			unset($restriction[1]);

			$restriction[1]["ULPROPTAG"] = is_string($propTag) ? $propTag : Conversion::property2json($propTag);
			$restriction[1]["RESTRICTION"] = $res;
			break;
	}

	return $restriction;
}
?>