- <?php
- /*
- * Lokorin.com
- * Copyright 2004-2006
- * Licensed under the GNU LGPL. See COPYING for full terms.
- */
- /**
- * A library that contains resources related to the DKP Bank balance
- * recordings.
- * @author Andreas Launila
- * @version $Revision: 1.11 $
- * @package com.lokorin.lokorin.lib
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- */
-
- /**
- * For access to common constants and functions.
- */
- require_once('common.php');
-
- /**
- * The names of all the members that should be ignored while taking snapshots.
- * The names are comma delimited.
- * @var string
- * @access private
- */
- define('IGNORED_MEMBERS', 'Rott');
-
- /**
- * Performs a snapshot of the Europa EQDKP standings. It records all members'
- * current points and the total balance in the database.
- * @throws IOException If something went wrong when performing the snapshot.
- * @access public
- */
- function performDkpSnapshot() {
- global $db;
-
- $errno = $errstr = '';
- $fp = fsockopen("www.europaguild.net", 80, $errno, $errstr, 5);
- if(!$fp) {
- //An error occured while connecting
- throw new IOException($errstr.' ('.$errno.')');
- } else {
- //Set header
- $out = "GET /eqdkp/listmembers.php?s= HTTP/1.1\r\n";
- $out .= "Host: www.europaguild.net\r\n";
- $out .= "Cookie: eqdkp_data=a%3A2%3A%7Bs%3A13%3A%22auto_login_id%22%3Bs%3A32%3A%22332db7088291c743cfad6a828c3604c8%22%3Bs%3A7%3A%22user_id%22%3Bs%3A3%3A%22348%22%3B%7D\r\n";
- $out .=" Connection: Close\r\n\r\n";
-
- $lines = array();
- fwrite($fp, $out);
- while(!feof($fp)) {
- $lines[] = trim(fgets($fp, 256));
- }
- fclose($fp);
- }
-
- //Remove the html functionality, translate it to plain text
- //$document = preg_replace('/<.+?>/s', '', htmlspecialchars(implode("\n", $lines)));
- $matches = array();
- preg_match_all('/\n\d+\n(\w+)\n[ \w]+\n\d+\n.+?\n.+?\n.+?\n.+?\n([\d\.\-]+)/',
- strip_tags(implode("\n", $lines)), $matches);
- $names = $matches[1];
- $points = $matches[2];
-
- //Remove all members that should be ignored from the snapshot.
- $ignoredNames = explode(',', IGNORED_MEMBERS);
- foreach($ignoredNames as $memberName) {
- $id = array_search($memberName, $names);
- if($id !== false) {
- unset($names[$id]);
- unset($points[$id]);
- }
- }
-
- $totalPoints = array_sum($points);
- if($totalPoints == 0) {
- //No points were recorded, there was probably a connection problem.
- return false;
- }
-
- $vars = array(
- 'timestamp' => time(),
- 'points' => array_sum($points)
- );
-
- //Insert the global snapshot.
- $db->queryInsert(TABLE_DKPBANK, $vars);
- $snapshotId = $db->getLastInsertId(TABLE_DKPBANK, 'id');
-
- //Insert the individual recordings.
- foreach($names as $id => $name) {
- $vars = array(
- 'snapshot_id' => $snapshotId,
- 'name' => $name,
- 'points' => $points[$id]
- );
- $db->queryInsert(TABLE_DKPBANK_MEMBERS, $vars);
- }
- }
- ?>