<?php

$mageRoot = __DIR__;

while (!(file_exists($mageRoot . '/app/Mage.php') || file_exists($mageRoot . '/bin/magento'))) {
    $mageRoot = dirname($mageRoot);
}

return $mageRoot;