Various small changes found in orphaned version from tmitchell.
Moved  to only occur at end of ReturnSummon
returnSummon now properly recurses, then returns

An error in calculating the current ob_level was causing endCapture to
always return instead of waiting until the final level was closed to
return, and echoing otherwise. A few small changes fixed that, to my
testing.

Note this only caused a problem on nested template calls from AJAX
calls.
replaced getExtension() with PHP < 5.3.6-friendly version.
Resolve merge conflicts.