/**
  * Lorem Ipsum Generator. Using remote service.
  * Undocumented.
  * 
  */
 function LoremIpsum($Settings = array(), $Trim = False)
 {
     $Language = C('Plugins.UsefulFunctions.LoremIpsum.Language', 'latin');
     if (!in_array($Language, array('latin', 'noIpsum'))) {
         $OtherValue = $Language;
         $Language = 'other';
     }
     $Defaults = array('language' => $Language, 'other' => isset($OtherValue) ? $OtherValue : 'latin', 'radio' => 'limit', 'limit' => 100, 'num' => 1, 'type' => 'plain', 'Rhubarb' => 'Generate');
     if (is_numeric($Settings)) {
         // Words
         $Options['radio'] = 'limit';
         $Options['limit'] = $Settings;
         if (is_int($Settings) || $Settings == 1) {
             $Trim = '.';
         }
         // remove dot for single word
     } elseif (is_string($Settings) && substr($Settings, 0, 1) == 'p') {
         // Paragraphs
         $Paragraphs = substr($Settings, 1);
         $Number = Clamp((int) $Paragraphs, 1, 100);
         $Options['num'] = $Number;
         $Options['radio'] = 'num';
     } else {
         $Options = $Settings;
     }
     $Options = array_merge($Defaults, $Options);
     $Snoopy = Gdn::Factory('Snoopy');
     $Snoopy->Submit('http://generator.lorem-ipsum.info/lorem-ipsum-copy', $Options);
     $Doc = PqDocument($Snoopy->results, array('FixHtml' => False));
     $Result = Pq('#txt')->Text();
     if ($Trim !== False) {
         $Result = trim($Result, $Trim);
     }
     return $Result;
 }
function addHealth($id, $health)
{
    $user = getUser($id);
    $amount = Clamp($user['health'] + $health, 0, getUsersMaxHealth($id));
    $lSql = "UPDATE ip_users SET health=:hp WHERE id=:id";
    $lStmt = $db->prepare($lSql);
    $res = $lStmt->execute(array(":hp" => $amount, ":id" => $id));
    return $res;
}
 /**
  * Makes thumbnail image by ImageMagick
  * Use ConvertImage()
  */
 function Thumbnail($Source, $Attributes, &$OutData = Null)
 {
     $OutData = Null;
     $Width = ArrayValue('width', $Attributes);
     $Height = ArrayValue('height', $Attributes);
     $Crop = GetValue('Crop', $Attributes, False, True);
     $Geometry = GetValue('Geometry', $Attributes, False, True);
     $TargetFolder = GetValue('TargetFolder', $Attributes, 'uploads/cached', True);
     $ImageQuality = GetValue('ImageQuality', $Attributes, False, True);
     // ^ option since 6.3.8-2
     if ($Crop === True) {
         $Geometry = "\"{$Width}x{$Height}^\" -crop {$Width}x{$Height}+0+0 +repage";
     }
     if (!$Geometry) {
         $Geometry = $Width . 'x' . $Height;
     }
     if (is_numeric($ImageQuality)) {
         $ImageQuality = '-quality ' . Clamp($ImageQuality, 1, 100);
     }
     $Options['Options'] = "-thumbnail {$Geometry} {$ImageQuality}";
     $Options['TargetFolder'] = $TargetFolder;
     if (substr($Source, 0, 9) == '/uploads/') {
         $Source = substr($Source, 1);
     }
     $ResultImage = ConvertImage($Source, $Options);
     if (ArrayValue('OutOriginalImageSize', $Attributes, False, True)) {
         $Return = array();
         $OutData['ImageSize'] = GetImageSize($Source);
     }
     if (array_key_exists('alt', $Attributes) || GetValue('Img', $Attributes, False, True)) {
         TouchValue('alt', $Attributes, pathinfo($ResultImage, PATHINFO_FILENAME));
         $ResultImage = Img($ResultImage, $Attributes);
     }
     return $ResultImage;
 }
include "db.php";
$loginRes = getUser($_SESSION['login']);
if ($loginRes['exp'] >= nextLevelExpNeeded($loginRes['level'])) {
    $sql = "UPDATE {$usertable} SET level=:level WHERE id=:uid";
    $stmt = $db->prepare($sql);
    $res = $stmt->execute(array(":level" => $loginRes['level'] + 1, ":uid" => $loginRes['id']));
    $loginRes = getUser($_SESSION['login']);
}
//echo $loginRes['lastEnergyGiven'];
//echo "   ";
//echo time();
if (time() >= $loginRes['lastEnergyGiven'] + 5 * 60 && $loginRes['energy'] < getUsersMaxEnergy($_SESSION['login'])) {
    $add = floor((time() - $loginRes['lastEnergyGiven']) / (5 * 60));
    $sql = "UPDATE {$usertable} SET energy=:energy, lastEnergyGiven=:lastEnergy WHERE id=:uid";
    $stmt = $db->prepare($sql);
    $res = $stmt->execute(array(":energy" => Clamp($loginRes['energy'] + $add, 0, $loginRes['energyMax']), ":lastEnergy" => time(), ":uid" => $loginRes['id']));
    $loginRes = getUser($_SESSION['login']);
}
?>
	<head>
		<script src="http://code.jquery.com/jquery-git2.js"></script>
		<script>
			var energyUpdateID = setInterval(function(){updateEnergyTime()}, 1000);
				
			function updateEnergyTime()
			{
				var xmlhttp;
				var minutes = 0;
				var seconds = 0;
				var time;
				if(window.XMLHttpRequest)
 public static function Wait($Seconds = 1, $bDrawDots = True)
 {
     fwrite(STDOUT, self::TimeSeconds() . ' -!- Waiting.');
     $Seconds = Clamp((int) $Seconds, 1, 3600);
     // 1 hour max
     for ($i = 0; $i < $Seconds; $i++) {
         sleep(1);
         if ($bDrawDots) {
             fwrite(STDOUT, '.');
         }
     }
     fwrite(STDOUT, "\n");
 }