Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Search_model extends CI_Model {
- public function __construct()
- {
- $zsite = $this->load->database('default', TRUE);
- $this->load->helper('url');
- $this->load->helper('file');
- }
- function GetURL($host, $port, $url)
- {
- $url = "http://$host:$port/$url";
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
- $data = curl_exec($ch);
- if (curl_errno($ch) == 0)
- {
- curl_close($ch);
- return $data;
- }
- curl_close($ch);
- return "";
- }
- function CheckHost($hostname, $port)
- {
- global $now;
- $xml = GetURL($hostname, $port, "?method=collector");
- if ($xml == "") //No data was retrieved? (CURL may have timed out)
- $failcounter = "failcounter + 1";
- else
- $failcounter = "0";
- //Update nextcheck to be 10 minutes from now. The current OS instance
- //won't be checked again until at least this much time has gone by.
- $next = $now + 600;
- $zgrid->query("UPDATE hostsregister SET nextcheck = $next," .
- " checked = 1, failcounter = " . $failcounter .
- " WHERE host = '" . mysql_real_escape_string($hostname) . "'" .
- " AND port = '" . mysql_real_escape_string($port) . "'");
- if ($xml != "")
- parse($hostname, $port, $xml);
- }
- function parse($hostname, $port, $xml)
- {
- global $now;
- ///////////////////////////////////////////////////////////////////////
- //
- // Search engine sim scanner
- //
- //
- // Load XML doc from URL
- //
- $objDOM = new DOMDocument();
- $objDOM->resolveExternals = false;
- //Don't try and parse if XML is invalid or we got an HTML 404 error.
- if ($objDOM->loadXML($xml) == False)
- return;
- //
- // Get the region data to update
- //
- $regiondata = $objDOM->getElementsByTagName("regiondata");
- //If returned length is 0, collector method may have returned an error
- if ($regiondata->length == 0)
- return;
- $regiondata = $regiondata->item(0);
- //
- // Update nextcheck so this host entry won't be checked again until after
- // the DataSnapshot module has generated a new set of data to be parsed.
- //
- $expire = $regiondata->getElementsByTagName("expire")->item(0)->nodeValue;
- $next = $now + $expire;
- $updater = $zgrid->query("UPDATE hostsregister SET nextcheck = $next " .
- "WHERE host = '" . mysql_real_escape_string($hostname) . "' AND " .
- "port = '" . mysql_real_escape_string($port) . "'");
- //
- // Get the region data to be saved in the database
- //
- $regionlist = $regiondata->getElementsByTagName("region");
- foreach ($regionlist as $region)
- {
- $regioncategory = $region->getAttributeNode("category")->nodeValue;
- //
- // Start reading the Region info
- //
- $info = $region->getElementsByTagName("info")->item(0);
- $regionuuid = $info->getElementsByTagName("uuid")->item(0)->nodeValue;
- $regionname = $info->getElementsByTagName("name")->item(0)->nodeValue;
- $regionhandle = $info->getElementsByTagName("handle")->item(0)->nodeValue;
- $url = $info->getElementsByTagName("url")->item(0)->nodeValue;
- //
- // First, check if we already have a region that is the same
- //
- $check = $zgrid->query("SELECT * FROM regions WHERE regionuuid = '" .
- mysql_real_escape_string($regionuuid) . "'");
- if ($check->num_rows() > 0) {
- $zgrid->query("DELETE FROM regions WHERE regionuuid = '" .
- mysql_real_escape_string($regionuuid) . "'");
- $zgrid->query("DELETE FROM parcels WHERE regionuuid = '" .
- mysql_real_escape_string($regionuuid) . "'");
- $zgrid->query("DELETE FROM allparcels WHERE regionUUID = '" .
- mysql_real_escape_string($regionuuid) . "'");
- $zgrid->query("DELETE FROM parcelsales WHERE regionUUID = '" .
- mysql_real_escape_string($regionuuid) . "'");
- $zgrid->query("DELETE FROM objects WHERE regionuuid = '" .
- mysql_real_escape_string($regionuuid) . "'");
- }
- $data = $region->getElementsByTagName("data")->item(0);
- $estate = $data->getElementsByTagName("estate")->item(0);
- $username = $estate->getElementsByTagName("name")->item(0)->nodeValue;
- $useruuid = $estate->getElementsByTagName("uuid")->item(0)->nodeValue;
- $estateid = $estate->getElementsByTagName("id")->item(0)->nodeValue;
- //
- // Second, add the new info to the database
- //
- $sql = "INSERT INTO regions VALUES('" .
- mysql_real_escape_string($regionname) . "','" .
- mysql_real_escape_string($regionuuid) . "','" .
- mysql_real_escape_string($regionhandle) . "','" .
- mysql_real_escape_string($url) . "','" .
- mysql_real_escape_string($username) ."','" .
- mysql_real_escape_string($useruuid) ."')";
- $zgrid->query($sql);
- //
- // Start reading the parcel info
- //
- $parcel = $data->getElementsByTagName("parcel");
- foreach ($parcel as $value)
- {
- $parcelname = $value->getElementsByTagName("name")->item(0)->nodeValue;
- $parceluuid = $value->getElementsByTagName("uuid")->item(0)->nodeValue;
- $infouuid = $value->getElementsByTagName("infouuid")->item(0)->nodeValue;
- $parcellanding = $value->getElementsByTagName("location")->item(0)->nodeValue;
- $parceldescription = $value->getElementsByTagName("description")->item(0)->nodeValue;
- $parcelarea = $value->getElementsByTagName("area")->item(0)->nodeValue;
- $parcelcategory = $value->getAttributeNode("category")->nodeValue;
- $parcelsaleprice = $value->getAttributeNode("salesprice")->nodeValue;
- $dwell = $value->getElementsByTagName("dwell")->item(0)->nodeValue;
- $owner = $value->getElementsByTagName("owner")->item(0);
- $owneruuid = $owner->getElementsByTagName("uuid")->item(0)->nodeValue;
- // Adding support for groups
- $group = $value->getElementsByTagName("group")->item(0);
- if ($group != "")
- {
- $groupuuid = $group->getElementsByTagName("groupuuid")->item(0)->nodeValue;
- }
- else
- {
- $groupuuid = "00000000-0000-0000-0000-000000000000";
- }
- //
- // Check bits on Public, Build, Script
- //
- $parcelforsale = $value->getAttributeNode("forsale")->nodeValue;
- $parceldirectory = $value->getAttributeNode("showinsearch")->nodeValue;
- $parcelbuild = $value->getAttributeNode("build")->nodeValue;
- $parcelscript = $value->getAttributeNode("scripts")->nodeValue;
- $parcelpublic = $value->getAttributeNode("public")->nodeValue;
- //
- // Save
- //
- $sql = "INSERT INTO allparcels VALUES('" .
- mysql_real_escape_string($regionuuid) . "','" .
- mysql_real_escape_string($parcelname) . "','" .
- mysql_real_escape_string($owneruuid) . "','" .
- mysql_real_escape_string($groupuuid) . "','" .
- mysql_real_escape_string($parcellanding) . "','" .
- mysql_real_escape_string($parceluuid) . "','" .
- mysql_real_escape_string($infouuid) . "','" .
- mysql_real_escape_string($parcelarea) . "' )";
- $zgrid->query($sql);
- if ($parceldirectory == "true")
- {
- $sql = "INSERT INTO parcels VALUES('" .
- mysql_real_escape_string($regionuuid) . "','" .
- mysql_real_escape_string($parcelname) . "','" .
- mysql_real_escape_string($parceluuid) . "','" .
- mysql_real_escape_string($parcellanding) . "','" .
- mysql_real_escape_string($parceldescription) . "','" .
- mysql_real_escape_string($parcelcategory) . "','" .
- mysql_real_escape_string($parcelbuild) . "','" .
- mysql_real_escape_string($parcelscript) . "','" .
- mysql_real_escape_string($parcelpublic) . "','".
- mysql_real_escape_string($dwell) . "','" .
- mysql_real_escape_string($infouuid) . "','" .
- mysql_real_escape_string($regioncategory) . "')";
- mysql_query($sql);
- }
- if ($parcelforsale == "true")
- {
- $sql = "INSERT INTO parcelsales VALUES('" .
- mysql_real_escape_string($regionuuid) . "','" .
- mysql_real_escape_string($parcelname) . "','" .
- mysql_real_escape_string($parceluuid) . "','" .
- mysql_real_escape_string($parcelarea) . "','" .
- mysql_real_escape_string($parcelsaleprice) . "','" .
- mysql_real_escape_string($parcellanding) . "','" .
- mysql_real_escape_string($infouuid) . "', '" .
- mysql_real_escape_string($dwell) . "', '" .
- mysql_real_escape_string($estateid) . "', '" .
- mysql_real_escape_string($regioncategory) . "')";
- $zgrid->query($sql);
- }
- }
- //
- // Handle objects
- //
- $objects = $data->getElementsByTagName("object");
- foreach ($objects as $value)
- {
- $uuid = $value->getElementsByTagName("uuid")->item(0)->nodeValue;
- $regionuuid = $value->getElementsByTagName("regionuuid")->item(0)->nodeValue;
- $parceluuid = $value->getElementsByTagName("parceluuid")->item(0)->nodeValue;
- $location = $value->getElementsByTagName("location")->item(0)->nodeValue;
- $title = $value->getElementsByTagName("title")->item(0)->nodeValue;
- $description = $value->getElementsByTagName("description")->item(0)->nodeValue;
- $flags = $value->getElementsByTagName("flags")->item(0)->nodeValue;
- $zgrid->query("INSERT INTO objects VALUES('" .
- mysql_real_escape_string($uuid) . "','" .
- mysql_real_escape_string($parceluuid) . "','" .
- mysql_real_escape_string($location) . "','" .
- mysql_real_escape_string($title) . "','" .
- mysql_real_escape_string($description) . "','" .
- mysql_real_escape_string($regionuuid) . "')");
- }
- }
- }
- public function parser()
- {
- $zgrid = $this->load->database('grid', TRUE);
- $now = time();
- $sql = "SELECT host, port FROM hostsregister " .
- "WHERE nextcheck < $now AND checked = 0 LIMIT 0,10";
- $jobsearch = $zgrid->query($sql);
- //
- // If the sql query returns no rows, all entries in the hostsregister
- // table have been checked. Reset the checked flag and re-run the
- // query to select the next set of hosts to be checked.
- //
- if ($jobsearch->num_rows() == 0)
- {
- $zgrid->query("UPDATE hostsregister SET checked = 0");
- $jobsearch = $zgrid->query($sql);
- }
- $r = $jobsearch->row();
- while ($jobs = $r)
- $this->CheckHost($jobs[0], $jobs[1])->result_array();
- }
- public function register($hostname,$port,$service)
- {
- $zgrid = $this->load->database('grid', TRUE);
- if ($hostname != "" && $port != "" && $service == "online")
- {
- $checkhost = $zgrid->query("SELECT register FROM hostsregister WHERE " .
- "host = '" .$zgrid->escape_str($hostname). "' AND " .
- "port = '" .$zgrid->escape_str($port). "'");
- $timestamp = $_SERVER['REQUEST_TIME'];
- if ($checkhost->num_rows() > 0)
- {
- $update = $zgrid->query("UPDATE hostsregister SET " .
- "register = '" .$zgrid->escape_str($timestamp). "', " .
- "nextcheck = '0', checked = '0', " .
- "failcounter = '0' " .
- "WHERE host = '" .$zgrid->escape_str($hostname). "' AND " .
- "port = '" .$zgrid->escape_str($port). "'");
- }
- else
- {
- $register = $zgrid->query("INSERT INTO hostsregister VALUES ".
- "('" .$zgrid->escape_str($hostname) . "', " .
- "'" .$zgrid->escape_str($port) . "', " .
- "'" .$zgrid->escape_str($timestamp) . "', 0, 0, 0)");
- }
- }
- elseif ($hostname != "" && $port != "" && $service = "offline")
- {
- $delete = $zgrid->query("DELETE FROM hostsregister " .
- "WHERE host = '" .$zgrid->escape_str($hostname) . "' AND " .
- "port = '" .$zgrid->escape_str($port) . "'");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement