LONTE SHELL EDITION


Dashboard -*- SHOW INFO -*- HASH identify -*- Config -*- Jumping

You Are Here : /var/www/virtual/tvarditsa.org/classes/
Upload File :
Current File : /var/www/virtual/tvarditsa.org/classes/poll.class.php

<?php // _$_ poll.class.php _ poll _ version 1.3.2 _ valio _$_ //
if (!defined("_POLL_CLASS_")) {
define("_POLL_CLASS_", 1);

// ---  Configuration   --- //

$Poll_Configuration     = array (
  'tables_prefix'       => 'poll_',
  'db_themes'           => 'themes',
  'themes_fields'       => array (
    'id'        => 'id',
    'theme'     => 'theme',
	'theme_en'     => 'theme_en',
  ),
  'db_votes'            => 'results',
  'votes_fields'        => array (
    'id'        => 'id',
    'pid'       => 'pid',
    'vot'       => 'vot',
    'counter'   => 'counter',
	'vot_en'       => 'vot_en',
  ),
  'db_iplog'            => 'iplog',
  'iplog_fields'        => array (
    'id'        => 'id',
    'pid'       => 'pid',
    'ip'        => 'iplog',
  ),
  'delete_iplog'        => 10, // save iplog only for last 10 polls //
  'use_cookie'          => 'minews.poll',
  'use_client_ip'       => 'path', // 'local', 'real', 'path' //
);

// ----  Auto Include  ---- //

if (!defined("_PAGER_CLASS_")) {
  $this_directory = dirname(__FILE__) . DIRECTORY_SEPARATOR;
  include ($this_directory . 'pager.class.php');
}

// --  Class Definition  -- //

  class Poll extends Pager {

// ----  Private Vars  ---- //

    var $_cfg;

// ----  Constructor   ---- //

    function Poll () {
      $this->__construct();
    }

    function __construct() {
      GLOBAL $Poll_Configuration;
      // Load configuration
      $this->_cfg = $Poll_Configuration;
      Pager::__construct();
    }

// ---  Public Methods  --- //

    function vote ($vote) {
	  
      if ($vote <= 0) return false;
      
      $t1 = $this->_cfg['tables_prefix'].$this->_cfg['db_votes'];
      $t2 = $this->_cfg['tables_prefix'].$this->_cfg['db_iplog'];
      $t3 = $this->_cfg['tables_prefix'].$this->_cfg['db_themes'];
      
      if ($this->_cfg['delete_iplog'] > 0) {
        $this->set_offset ($this->_cfg['delete_iplog']);
        $this->set_records (1);
        $this->set_condition (NULL);
        $this->set_order ('id DESC');
        $data = $this->list_themes (true);
        if ($data['total'] > 0) {
          $this->set_condition (
            $this->_cfg['iplog_fields']['pid'].' <= '.
            (int)$data['data'][0][$this->_cfg['themes_fields']['id']]
          );
          $this->delete ($t3);
        }
      }
      
      $this->set_offset (0);
      $this->set_records (1);
      $this->set_condition ($this->_cfg['votes_fields']['id'].' = '.(int)$vote);
      $this->set_order (NULL);
      
      $fields = $this->_cfg['votes_fields']['id'].' as id';
      $fields .= ', '.$this->_cfg['votes_fields']['pid'].' as pid';
      
      $data = $this->create ($t1, $fields, true);
      
      if ($data['total'] <= 0) return false;
      
      $pollid = $data['data'][0]['pid'];
      
      if ($this->is_voted($pollid)) return false;
      
	  $sql1 = 'UPDATE '.$t1.' SET '.$this->_cfg['votes_fields']['counter'].' = '.$this->_cfg['votes_fields']['counter'].'+1 WHERE id = '.(int)$vote;
      $this->Execute($sql1);
      
      $iplog[$this->_cfg['iplog_fields']['id']] = NULL;
      $iplog[$this->_cfg['iplog_fields']['pid']] = $pollid;
      $iplog[$this->_cfg['iplog_fields']['ip']] =
        $this->_get_ip_address ($this->_cfg['use_client_ip']);;
      
      $this->update ($t2, $iplog);
      
      if (($this->_cfg['use_cookie'] != '') && !headers_sent())
        setcookie ($this->_cfg['use_cookie'].'.'.(int)$pollid, '1', time()+25920000);
    }

    function is_voted ($id) {
      GLOBAL $_COOKIE, $db;
	  
      if ($id <= 0) return false;
      
      if (($this->_cfg['use_cookie'] != '')
        && ($_COOKIE[$this->_cfg['use_cookie'].'.'.(int)$id])) return true;
      
      $t = $this->_cfg['tables_prefix'].$this->_cfg['db_iplog'];
      $ip = $this->_get_ip_address ($this->_cfg['use_client_ip']);
      
      $this->set_offset (0);
      $this->set_records (1);
      $condition = $this->_cfg['iplog_fields']['pid'].' = '.(int)$id;
      $condition .= ' AND '.$this->_cfg['iplog_fields']['ip'].' = '.$this->qstr($ip);
      $this->set_condition ($condition);
      
      $data = $this->create($t, '*', true);
      
      if ($data['total'] <= 0) return false;
      
      return true;
    }

    function get_poll ($id = 0, $type = 2) {
      
      $t1 = $this->_cfg['tables_prefix'].$this->_cfg['db_themes'];
      $t2 = $this->_cfg['tables_prefix'].$this->_cfg['db_votes'];
      $r = array();
      
	  $this->set_offset (0);
      $this->set_records (1);
	  if ($id <= 0) {
        $this->set_condition (NULL);
        $this->set_order ('id DESC');
      } else {
        $this->set_condition ($this->_cfg['themes_fields']['id'].' = '.(int)$id);
        $this->set_order (NULL);
      }

      $fields = $this->_cfg['themes_fields']['id'].' as id';
      $fields .= ', '.$this->_cfg['themes_fields']['theme'].' as theme';
      $data = $this->create($t1, $fields, true);
      
      if (!is_array($data['data'])) return NULL;
      
      $r['id'] = $data['data'][0]['id'];
      $r['theme'] = $data['data'][0]['theme'];
      $this->set_records (-1);
      $this->set_condition ($this->_cfg['votes_fields']['pid'].' = '.(int)$r['id']);
      $this->set_order ('id ASC');
      $fields = $this->_cfg['votes_fields']['id'].' as id';
      $fields .= ', '.$this->_cfg['votes_fields']['pid'].' as pid';
      $fields .= ', '.$this->_cfg['votes_fields']['vot'].' as vot';
      $fields .= ', '.$this->_cfg['votes_fields']['counter'].' as counter';
      
      unset($data);
      $data = $this->create($t2, $fields, true);
      
      if (!is_array($data['data'])) return $r;
      
      $r['min'] = -1;
      $r['max'] = -1;
      $r['total'] = 0;
      
      foreach ($data['data'] as $vid => $vd) {
        $r['total'] += $vd['counter'];
        
        if (($r['min'] == -1) || ($vd['counter'] < $r['min']))
          $r['min'] = $vd['counter'];
        
        if (($r['max'] == -1) || ($vd['counter'] > $r['max']))
          $r['max'] = $vd['counter'];
        
        $r['votes'][] = array (
          'id' => $vd['id'],
          'vot' => $vd['vot'],
          'counter' => $vd['counter']
        );
      }
      
      if (($type > 0) && is_array($r['votes']))
        foreach ($r['votes'] as $vid => $vd) {
          if ($r['total'] == 0)
            $r['votes'][$vid]['percent'] = 0;
          else
            $r['votes'][$vid]['percent'] =
              round(($vd['counter']*100) / $r['total']);    
          if ($type == 2) {
            if ($r['max'] == $r['min'])
              $r['votes'][$vid]['op'] = 0;
            else
              $r['votes'][$vid]['op'] =
                round((($vd['counter'] - $r['min']) * 100) / ($r['max'] - $r['min']));
          }
        }
      return $r;
    }
	
function get_poll_en ($id = 0, $type = 2) {
      
      $t1 = $this->_cfg['tables_prefix'].$this->_cfg['db_themes'];
      $t2 = $this->_cfg['tables_prefix'].$this->_cfg['db_votes'];
      $r = array();
      
	  $this->set_offset (0);
      $this->set_records (1);
	  if ($id <= 0) {
        $this->set_condition (NULL);
        $this->set_order ('id DESC');
      } else {
        $this->set_condition ($this->_cfg['themes_fields']['id'].' = '.(int)$id);
        $this->set_order (NULL);
      }

      $fields = $this->_cfg['themes_fields']['id'].' as id';
      $fields .= ', '.$this->_cfg['themes_fields']['theme_en'].' as theme';
      $data = $this->create($t1, $fields, true);
      
      if (!is_array($data['data'])) return NULL;
      
      $r['id'] = $data['data'][0]['id'];
      $r['theme'] = $data['data'][0]['theme'];
      $this->set_records (-1);
      $this->set_condition ($this->_cfg['votes_fields']['pid'].' = '.(int)$r['id']);
      $this->set_order ('id ASC');
      $fields = $this->_cfg['votes_fields']['id'].' as id';
      $fields .= ', '.$this->_cfg['votes_fields']['pid'].' as pid';
      $fields .= ', '.$this->_cfg['votes_fields']['vot_en'].' as vot';
      $fields .= ', '.$this->_cfg['votes_fields']['counter'].' as counter';
      
      unset($data);
      $data = $this->create($t2, $fields, true);
      
      if (!is_array($data['data'])) return $r;
      
      $r['min'] = -1;
      $r['max'] = -1;
      $r['total'] = 0;
      
      foreach ($data['data'] as $vid => $vd) {
        $r['total'] += $vd['counter'];
        
        if (($r['min'] == -1) || ($vd['counter'] < $r['min']))
          $r['min'] = $vd['counter'];
        
        if (($r['max'] == -1) || ($vd['counter'] > $r['max']))
          $r['max'] = $vd['counter'];
        
        $r['votes'][] = array (
          'id' => $vd['id'],
          'vot' => $vd['vot'],
          'counter' => $vd['counter']
        );
      }
      
      if (($type > 0) && is_array($r['votes']))
        foreach ($r['votes'] as $vid => $vd) {
          if ($r['total'] == 0)
            $r['votes'][$vid]['percent'] = 0;
          else
            $r['votes'][$vid]['percent'] =
              round(($vd['counter']*100) / $r['total']);    
          if ($type == 2) {
            if ($r['max'] == $r['min'])
              $r['votes'][$vid]['op'] = 0;
            else
              $r['votes'][$vid]['op'] =
                round((($vd['counter'] - $r['min']) * 100) / ($r['max'] - $r['min']));
          }
        }
      return $r;
    }

    function list_themes ($nonav = false) {
      $t = $this->_cfg['tables_prefix'].$this->_cfg['db_themes'];
      $f = $this->_cfg['themes_fields']['id'].' as id, ';
      $f .= $this->_cfg['themes_fields']['theme'].' as theme,';
	  $f .= $this->_cfg['themes_fields']['theme_en'].' as theme_en';
      return $this->create ($t, $f, $nonav);
    }

    function update_poll ($data) {
      if (isset($data['id'])) {
        $data['id'] = (int)$data['id'];
        if ($data['id'] == 0) $data['id'] = NULL;
      }
      if (isset($data['theme'])) {
        $td[$this->_cfg['themes_fields']['id']] = $data['id'];
        $td[$this->_cfg['themes_fields']['theme']] = $data['theme'];
        $this->set_condition ($this->_cfg['themes_fields']['id'].' = '.(int)$data['id']);
        $this->update ($this->_cfg['tables_prefix'].$this->_cfg['db_themes'], $td);
      }
      if (is_array($data['votes']))
        foreach ($data['votes'] as $vid => $v) {
          $vd[$this->_cfg['votes_fields']['id']] = $v['id'];
          $vd[$this->_cfg['votes_fields']['pid']] = $data['id'];
          $vd[$this->_cfg['votes_fields']['vot']] = $v['vot'];
          $vd[$this->_cfg['votes_fields']['counter']] = $v['counter'];
          $this->set_condition ($this->_cfg['votes_fields']['id'].' = '.(int)$v['id']);
          $this->update ($this->_cfg['tables_prefix'].$this->_cfg['db_votes'], $vd);
        }
    }

    function delete_vot ($vot) {
      $this->set_condition ($this->_cfg['votes_fields']['id'].' = '.(int)$vot);
      $this->delete ($this->_cfg['tables_prefix'].$this->_cfg['db_votes']);
      return true;
    }

    function delete_poll ($id) {
      if ($id <= 0) return false;
      $this->set_condition ($this->_cfg['themes_fields']['id'].' = '.(int)$id);
      $this->delete ($this->_cfg['tables_prefix'].$this->_cfg['db_themes']);
      $this->set_condition ($this->_cfg['votes_fields']['pid'].' = '.(int)$id);
      $this->delete ($this->_cfg['tables_prefix'].$this->_cfg['db_votes']);
      return true;
    }

// --  Private Methods   -- //

    function _get_ip_address ($detect_proxy = 'path') {
      GLOBAL $_SERVER;
      
      $ipaddress = $_SERVER["REMOTE_ADDR"];
      
      if ($detect_proxy == 'real') return $ipaddress;
      
	  $realipaddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
      
      if (!$realipaddress) return $ipaddress;
      
	  if ($detect_proxy != 'local') return $ipaddress .= "/".$realipaddress;
      
      if (preg_match ("/(\d+(\.\d+){3})$/", $realipaddress, $match)) {
        return $match[1];
      } else {
        return $realipaddress;
      }
    }

// -----  Destructor  ----- //

	function __destruct() {
      Pager::__destruct();
	}

  }

} // END POLL_CLASS
?>