/** array_compare_special * * If any elements of $before and $after are different, return the * element from $after, or null if the element is only in $before. * Compares arbitrarily nested arrays, but if the $levels parameter * is given, only returns whole elements (or null) from $after after * comparing that many levels of keys. */ function array_compare_special($before, $after, $levels = null) { $new_levels = $levels; if ($new_levels !== null) { $new_levels--; } $diff = false; // check all keys in $before (to find changed and deleted items) foreach ($before as $key => $value) { if (!array_key_exists($key, $after)) { // $key is no longer a key $diff[$key] = null; } else { if (is_array($value)) { // found an array if (is_array($after[$key])) { // still an array; compare recursively if ($new_levels === null || $new_levels > 0) { // still need to compare more keys $new = array_compare_special($value, $after[$key], $new_levels); if ($new !== false) { $diff[$key] = $new; } } else { if (!compare_recursive($value, $after[$key])) { // return the whole array from $after $diff[$key] = $after[$key]; } } } else { // the value for $key in $after is no longer an array $diff[$key] = $after[$key]; } } else { if ($after[$key] !== $value) { // the value for $key changed $diff[$key] = $after[$key]; } } } } // check all keys in $after (to find new items) foreach ($after as $key => $value) { if (!array_key_exists($key, $before)) { // $key is a new key $diff[$key] = $value; } } return $diff; }
// // rtGui is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with rtGui. If not, see <http://www.gnu.org/licenses/>. require_once 'config.php'; require_once 'functions.php'; rtgui_session_start(); $data = get_all_torrents(array('for_html' => true)); if (@is_array($_SESSION['last_data'])) { $last_data = $_SESSION['last_data']; $diff_torrents = array_compare_special($last_data['torrents'], $data['torrents'], 2); $diff_global = array_compare_special($last_data['global'], $data['global']); $return = array(); if ($diff_torrents !== false) { $return['torrents'] = $diff_torrents; } if ($diff_global !== false) { $return['global'] = $diff_global; } if (!count($return)) { $return = false; } echo json_encode($return); } else { echo json_encode($data); } $_SESSION['last_data'] = $data;