Advertisement
Al_Faqun

/Phenya/helpers.inc.php

Aug 21st, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.29 KB | None | 0 0
  1. <?php
  2.         include_once('__php__.php');
  3.  
  4.         //этот класс не используется, и не завершен
  5.         class Db {
  6.             protected $config;          //абсолютный путь к файлу конфига
  7.             public $mysqli;
  8.             protected $charset;
  9.             protected $database;        //выбранная база данных
  10.             public $error_msg;          //текст последней ошибки  БД
  11.             public $error_code;         //последний код ошибки БД
  12.  
  13.             public function __construct($conf_path, $dbname = NULL) {
  14.                 if ((!$this->set_config($conf_path))
  15.                     OR
  16.                (!$this->open())
  17.                     OR
  18.                 (!$this->set_charset('utf8'))
  19.                     OR
  20.                 (!$this->database($dbname))) {
  21.                     $error = 'Error creating db.';
  22.                     include DOC_ROOT . '/error.html.php';
  23.                     exit();
  24.                 }
  25.             }
  26.  
  27.             public function __destruct() {
  28.                 $this->close();
  29.                 unset($this->mysqli);
  30.             }
  31.  
  32.             public function open() {
  33.                 if (isset($this->mysqli)) $this->close();       //если соединение было открыто - насильно закрываем
  34.                 //далее считываем данные из конфига, поэтому он должен быть задан
  35.                 if (!isset($this->config)) {
  36.                     $this->error_msg = 'Problem loading config for mysqli: ' . $this->mysqli->error;
  37.                     $this->error_code = $this->mysqli->errno;
  38.                     return false;
  39.                 }
  40.                 //открываем соединение
  41.                 $this->mysqli = new mysqli('localhost', $this->config['username'], $this->config['password']);
  42.                 if ($this->mysqli->connect_errno) {
  43.                     $this->error_msg = "Failed to connect to MySQL: " . $this->mysqli->connect_error;
  44.                     return false;
  45.                 }
  46.             }
  47.  
  48.             public function close() {
  49.                 if ($this->mysqli->close()) return true;
  50.                 else {
  51.                     $this->error_msg = 'Error closing db';
  52.                     return false;
  53.                 }
  54.             }
  55.  
  56.             public function get_charset() {
  57.                 return $charset;
  58.             }
  59.  
  60.             public function set_charset($charset) {
  61.                 $this->open();
  62.                 if ((!is_string($charset)) OR (!$this->mysqli->set_charset($charset)))
  63.                 {
  64.                     $this->error_msg = 'Error setting new charset: ' . $charset;
  65.                     return false;
  66.                 }
  67.             }
  68.  
  69.             public function set_config($config_path) {
  70.                 if (($this->config = parse_ini_file($config_path)) === false) {
  71.                     $this->error_msg = 'Cannot parse config file.';
  72.                     return false;
  73.                 }
  74.                 else return true;
  75.             }
  76.  
  77.             public function database($db_name) {
  78.                 if (!$this->mysqli->select_db((is_null($db_name)) ? $this->config['dbname'] : $db_name))
  79.                 {
  80.                     $this->error_msg = 'Unable to locate database.';
  81.                     return false;
  82.                 }
  83.                 else return true;
  84.             }
  85.  
  86.             public function get_database() {
  87.                 $this->open();
  88.                 if ($result = $mysqli->query("SELECT DATABASE()")) {
  89.                     $row = $result->fetch_row();
  90.                     $db_name = $row[0];
  91.                     $result->close();
  92.                     return $db_name;
  93.                 }
  94.                 else {
  95.                     $this->error_msg = 'Unable to find out selected database.';
  96.                     return false;
  97.                 }
  98.             }
  99.  
  100.             public function select($sql) {
  101.                 $result = $this->mysqli->query($sql);
  102.                 if (!$result){
  103.                     $this->error_msg = 'Error performing select query.';
  104.                     return false;
  105.                 }
  106.                 while ($row = mysqli_fetch_array($result))
  107.                 {
  108.                     $rows[] = $row;
  109.                 }
  110.                 return $rows;
  111.             }
  112.         }
  113.  
  114.         //а вот эта функция рабочая
  115.         //включает сессию, если она ещё не включена, тем самым избегаю лишних сообщений
  116.         function session_true_start() {
  117.         $status = session_status();
  118.         if($status == PHP_SESSION_NONE){
  119.             //There is no active session
  120.             session_start();
  121.         }else
  122.             if($status == PHP_SESSION_DISABLED){
  123.             echo 'Sessions are not available! Exiting';
  124.             exit();
  125.         }else
  126.             if($status == PHP_SESSION_ACTIVE){
  127.             //ничего не делать, потому что отключение сессии удалит связанные с ней данные
  128.             }
  129.         }
  130.  
  131.         //а вот эта не используется, не завершена
  132.         function checkHash($h) {
  133.             if (isset($_SESSION['hash']) && is_array($_SESSION['hash'])) {
  134.                 if (in_array($h, $_SESSION['hash'])) {
  135.                     $SESSION['output'] = 'Duplicate form sumbission';
  136.                     header('Location: ' . "/Phenya/Admin/tpl_main.php",true,303);
  137.                     exit;
  138.                 }
  139.             }
  140.         }
  141.  
  142. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement