public static function checkForFilterUpdates() { //Perform input rules search. // Get a db connection. $db = JFactory::getDbo(); //Fetch the output filters from the database. $query = $db->getQuery(true); $query->select($db->quoteName(array('id', 'core_id', 'core_version'))); $query->from($db->quoteName('#__jhackguard_input_filters')); $query->where($db->quoteName('core_id') . ' > 0'); $query->order('ordering ASC'); $db->setQuery($query); $list = $db->loadObjectList(); //Include the JSONRPC client file. require_once JPATH_ADMINISTRATOR . '/components/com_jhackguard/jsonrpc.php'; $success = true; $msg = ""; //Tracking vars $update = 0; $insert = 0; $client = new JHackGuard_JSONRPC_Client('http://www.jhackguard.com/api/index.php'); $result = $client->execute('filters', array('get', 0, 'free-version')); if ($client->last_err == null and is_array($result) and isset($result['success']) and $result['success'] == true and isset($result['data']) and is_array($result['data'])) { //Define core list array $core_rules = array(); foreach ($list as $core_item) { $core_rules[$core_item->core_id] = array('version' => $core_item->core_version, 'id' => $core_item->id); } foreach ($result['data'] as $cid => $cob) { if (array_key_exists($cid, $core_rules)) { if ($cob['version'] > $core_rules[$cid]['version']) { $update++; } } else { $insert++; } } } else { $success = false; $msg = "Unable to fetch update rules from www.jhackguard.com. " . $client->last_err; } file_put_contents(JPATH_ADMINISTRATOR . '/components/com_jhackguard/data/.rules_updated_timestamp', serialize(array('success' => $success, 'msg' => $msg, 'update' => $update, 'insert' => $insert, 'expires' => time() + 86400))); return array('success' => $success, 'msg' => $msg, 'update' => $update, 'insert' => $insert, 'expires' => 0); }
public function jhc_update() { if (!JSession::checkToken()) { echo json_encode(array('success' => false, 'msg' => 'Invalid CSRF Token.')); return; } //Include the JSONRPC client file. require_once JPATH_ADMINISTRATOR . '/components/com_jhackguard/jsonrpc.php'; $success = true; $msg = ""; //Tracking vars $updated = 0; $inserted = 0; $client = new JHackGuard_JSONRPC_Client('http://www.jhackguard.com/api/index.php'); //TODO: use the actual configuration value for the license key $result = $client->execute('filters', array('get', 0, 'free-version')); if ($client->last_err != null) { $success = false; $msg = $client->last_err; } else { if (is_array($result) and isset($result['success']) and $result['success'] == true and isset($result['data']) and is_array($result['data'])) { //Perform a DB query fetching all core_id's here // Get a db connection. $db = JFactory::getDbo(); //Fetch the output filters from the database. $query = $db->getQuery(true); $query->select($db->quoteName(array('id', 'core_id', 'core_version'))); $query->from($db->quoteName('#__jhackguard_input_filters')); $query->where($db->quoteName('core_id') . ' > 0'); $query->order('ordering ASC'); $db->setQuery($query); $list = $db->loadObjectList(); //Define core list array $core_rules = array(); foreach ($list as $core_item) { $core_rules[$core_item->core_id] = array('version' => $core_item->core_version, 'id' => $core_item->id); } foreach ($result['data'] as $cid => $cob) { if (array_key_exists($cid, $core_rules)) { if ($cob['version'] > $core_rules[$cid]['version']) { //There is a new version of this particular rule. We need to update it. $object = new stdClass(); // Must be a valid primary key value. $object->id = $core_rules[$cid]['id']; $object->name = $cob['name']; $object->core_version = $cob['version']; $object->code = $cob['code']; $result = JFactory::getDbo()->updateObject('#__jhackguard_input_filters', $object, 'id'); $updated++; } } else { $object = new stdClass(); // Must be a valid primary key value. $object->id = null; $object->name = $cob['name']; $object->core_version = $cob['version']; $object->core_id = $cid; $object->code = $cob['code']; $object->state = 1; $result = JFactory::getDbo()->insertObject('#__jhackguard_input_filters', $object); $inserted++; } } } echo json_encode(array('success' => $success, 'updated' => $updated, 'inserted' => $inserted, 'msg' => $msg)); } }