Beispiel #1
0
 public function get_field($id)
 {
     $f = false;
     if ($this->change) {
         $this->db->change('bbn');
     }
     if (is_string($id) && strpos($id, '.') && ($x = explode('.', $id)) && count($x) === 2 && \bbn\str\text::check_name($x[0], $x[1])) {
         $id = $this->get_var("\n\t\t\t\tSELECT bbn_fields.id\n\t\t\t\tFROM bbn_fields\n\t\t\t\t\tJOIN bbn_smenus\n\t\t\t\t\t\tON bbn_smenus.bbn_id_site = {$this->id}\n\t\t\t\t\t\tAND bbn_smenus.bbn_name LIKE '{$x['0']}'\n\t\t\t\tWHERE bbn_fields.bbn_name LIKE '{$x['1']}'\n\t\t\t\tLIMIT 1");
     }
     if (is_int($id) || ctype_digit($id)) {
         if ($tmp = $this->db->get_row("\n        SELECT id, bbn_position, bbn_name, bbn_tit, bbn_id_form, bbn_mand\n        FROM bbn_fields\n        WHERE bbn_fields.id = ?\n        AND bbn_id_site = ?\n        LIMIT 1", $id, $this->id)) {
             $f = new \stdClass();
             foreach ($tmp as $k => $v) {
                 if (substr($k, 0, 4) === 'bbn_') {
                     $k = substr($k, 4);
                 }
                 $f->{$k} = $v;
             }
             $f->params = array();
             $r = $this->db->query("\n\t\t\t\t\tSELECT bbn_value, \n\t\t\t\t\tIFNULL(bbn_smenus.bbn_name,'') AS reftable,\n\t\t\t\t\tIFNULL(bbn_fields.bbn_name,'') AS reffield\n\t\t\t\t\tFROM bbn_param\n\t\t\t\t\t\tLEFT OUTER JOIN bbn_fields\n\t\t\t\t\t\t\tON bbn_fields.id = bbn_value\n\t\t\t\t\t\tLEFT OUTER JOIN bbn_smenus\n\t\t\t\t\t\t\tON bbn_fields.bbn_id_smenu = bbn_smenus.id\n\t\t\t\t\tWHERE bbn_id_field = %u\n\t\t\t\t\tORDER BY bbn_param.bbn_position", $f->id);
             while ($d = $r->get_row()) {
                 array_push($f->params, empty($d['reftable']) ? $d['bbn_value'] : $d['reftable'] . '.' . $d['reffield']);
             }
         }
     }
     if ($this->change) {
         $this->db->change($this->change);
     }
     return $f;
 }
Beispiel #2
0
 public static function specs($cat, $item)
 {
     if (\bbn\str\text::check_name($cat, $item)) {
         if (!isset(self::$specs[$cat]) && is_dir(__DIR__ . '/specs/' . $cat)) {
             self::$specs[$cat] = [];
         }
         if (isset(self::$specs[$cat])) {
             if (!isset(self::$specs[$cat][$item]) && file_exists(__DIR__ . '/specs/' . $cat . '/' . $item . '.php')) {
                 self::$specs[$cat][$item] = (include_once __DIR__ . '/specs/' . $cat . '/' . $item . '.php');
             }
             if (isset(self::$specs[$cat][$item])) {
                 return self::$specs[$cat][$item];
             }
             self::$specs[$cat][$item] = false;
         }
     }
     return false;
 }
Beispiel #3
0
 private static function define($dbs, $dbs_table = '')
 {
     if (empty($dbs)) {
         $dbs = self::$default_cfg;
     } else {
         if (is_string($dbs)) {
             $db = $dbs;
             $dbs = self::$default_cfg;
             $dbs['db'] = $db;
         }
     }
     if (!self::$dbs) {
         self::$dbs = $dbs;
     }
     if (!empty($dbs_table)) {
         self::$dbs_table = $dbs_table;
     }
     if (!\bbn\str\text::check_name(self::$dbs_table)) {
         self::log("Table name not allowed", self::$dbs_table);
         die("Table name not allowed");
     }
 }
Beispiel #4
0
 /**
  * Empties the structure tables for a given database and refill them with the current structure
  * 
  * @param string $db
  * @return void
  */
 public function update($db = '')
 {
     apc_clear_cache();
     apc_clear_cache("user");
     if (empty($db)) {
         $db = $this->db->current;
     }
     if (\bbn\str\text::check_name($db)) {
         $change = $this->db->current === $db ? false : $this->db->current;
         if ($change) {
             $this->db->change($db);
         }
         $schema = $this->db->modelize();
         if ($change) {
             $this->db->change($change);
         }
         /*
         $projects = [];
         $r1 = $this->db->query("SELECT *
         FROM `{$this->admin_db}`.`{$this->prefix}projects`
         WHERE `db` LIKE '$db'");
         while ( $d1 = $r1->get_row() ){
         	$projects[$d1['id']] = $d1;
         	$projects[$d1['id']]['forms'] = [];
         	$r2 = $this->db->query("
         		SELECT id
         		FROM `{$this->admin_db}`.`{$this->prefix}forms`
         		WHERE `id_project` = ?",
         		$d1['id']);
         	while ( $d2 = $r2->get_row() ){
         		$projects[$d1['id']]['forms'][$d2['id']] = $this->get_form_config();
         	}
         }
         */
         $this->db->query("DELETE IGNORE FROM `{$this->prefix}dbs` WHERE id LIKE '{$db}.%'");
         $this->db->query("DELETE IGNORE FROM `{$this->prefix}tables` WHERE id LIKE '{$db}.%'");
         $this->db->query("DELETE IGNORE FROM `{$this->prefix}columns` WHERE id LIKE '{$db}.%'");
         $this->db->query("DELETE IGNORE FROM `{$this->prefix}keys` WHERE id LIKE '{$db}.%'");
         $this->db->raw_query("\n        INSERT IGNORE INTO `{$this->admin_db}`.`{$this->prefix}dbs`\n        (`id`, `host`, `db`)\n        VALUES\n        ('{$db}', '{$this->db->host}', '{$db}')");
         $has_history = false;
         if (\bbn\appui\history::$is_used && isset($schema[\bbn\appui\history::$htable])) {
             $has_history = 1;
         }
         foreach ($schema as $t => $vars) {
             if (strpos($t, '.' . $this->prefix) === false) {
                 $tmp = explode(".", $t);
                 $db = $tmp[0];
                 $table = $tmp[1];
                 $this->db->insert_update($this->admin_db . '.' . $this->prefix . 'tables', ['id' => $t, 'db' => $db, 'table' => $table]);
                 foreach ($vars['fields'] as $col => $f) {
                     $config = new \stdClass();
                     if ($has_history && array_key_exists(\bbn\appui\history::$hcol, $vars['fields']) && $col !== \bbn\appui\history::$hcol) {
                         $config->history = 1;
                     }
                     if (isset($f['default'])) {
                         $config->default = $f['default'];
                     }
                     if (!empty($f['extra'])) {
                         $config->extra = $f['extra'];
                     }
                     if (isset($f['signed']) && $f['signed'] == 1) {
                         $config->signed = 1;
                     }
                     if (isset($f['null']) && $f['null'] == '1') {
                         $config->null = 1;
                     }
                     if (isset($f['maxlength']) && $f['maxlength'] > 0) {
                         $config->maxlength = (int) $f['maxlength'];
                     }
                     if (isset($f['keys'])) {
                         $config->keys = [];
                         foreach ($f['keys'] as $key) {
                             $config->keys[$key] = $vars['keys'][$key];
                         }
                     }
                     $this->db->insert_update($this->admin_db . '.' . $this->prefix . 'columns', ['id' => $t . '.' . $col, 'table' => $t, 'column' => $col, 'position' => $f['position'], 'type' => $f['type'], 'null' => $f['null'], 'key' => $f['key'], 'config' => json_encode($config)]);
                 }
             }
         }
         foreach ($schema as $t => $vars) {
             if (strpos($t, '.' . $this->prefix) === false) {
                 if (is_array($vars['keys'])) {
                     foreach ($vars['keys'] as $k => $arr) {
                         $pos = 1;
                         foreach ($arr['columns'] as $c) {
                             $this->db->insert_update($this->admin_db . '.' . $this->prefix . 'keys', ['id' => $t . '.' . $c . '.' . $k, 'key' => $k, 'column' => $t . '.' . $c, 'position' => $pos, 'ref_column' => is_null($arr['ref_column']) ? null : $arr['ref_db'] . '.' . $arr['ref_table'] . '.' . $arr['ref_column']]);
                             $pos++;
                         }
                     }
                 } else {
                     \bbn\tools::log($t);
                     \bbn\tools::log($vars['keys']);
                 }
             }
         }
         /*
         			foreach ( $projects as $i => $p ){
         				$this->db->insert($this->admin_db.'.'.$this->prefix.'projects',[
         					'id' => $i,
         					'id_client' => $p['id_client'],
         					'db' => $p['db'],
         					'name' => $p['name'],
         					'config' => $p['config']]);
         				foreach ( $p['forms'] as $j => $form ){
         					$this->db->insert($this->admin_db.'.'.$this->prefix.'forms',[
         						'id' => $j,
         						'id_project' => $i
         					]);
         					foreach ( $form as $field ){
         						$this->db->insert_ignore($this->admin_db.'.'.$this->prefix.'fields',[
         							'id' => $field['id'],
         							'id_form' => $j,
         							'column' => $field['column'],
         							'title' => $field['title'],
         							'position' => $field['position'],
         							'configuration' => json_encode($field['configuration'])
         						]);
         					}
         				}
         			}
         * 
         */
     }
 }
Beispiel #5
0
 /**
  * @return void 
  */
 public function delete_db_index($table, $column)
 {
     if (($table = $this->table_full_name($table, 1)) && text::check_name($column)) {
         $this->db->raw_query("\n\t\t\t\tALTER TABLE {$table}\n\t\t\t\tDROP INDEX `{$column}`");
     }
     return $this;
 }
Beispiel #6
0
 /**
  * Returns an integer candidate for being a new ID in the given table.
  * 
  * <code>
  * $this->db->new_id("table_users");
  * </code>
  *
  * @param string $table The table name.
  * @param string $id_field The id field name, default: 'id'.
  * @param int $min The minimum value for new id.
  * @param int $max The maximum value for new id.
  * 
  * @return int | false
  */
 public function new_id($table, $id_field = 'id', $min = 11111, $max = 499998999)
 {
     if ($max > $min && text::check_name($id_field) && ($table = $this->table_full_name($table, 1))) {
         $id = mt_rand($min, $max);
         while ($this->select($table, [$id_field], [$id_field => $id])) {
             $id = mt_rand($min, $max);
         }
         return $id;
     }
     return false;
 }
Beispiel #7
0
 public static function get_row_back($table, array $columns, array $where, $when)
 {
     if (!is_int($when)) {
         $when = strtotime($when);
     }
     $when = (int) $when;
     if (\bbn\str\text::check_name($table) && $when > 0 && count($where) === 1) {
         $when = date('Y-m-d H:i:s', $when);
         if (count($columns) === 0) {
             $columns = array_keys(self::$db->get_columns($table));
         }
         foreach ($columns as $col) {
             $fc = self::$db->current . '.' . self::$db->col_full_name($col, $table);
             if (!($r[$col] = self::$db->get_one("\n          SELECT old\n          FROM bbn_history\n          WHERE `column` LIKE ?\n          AND `line` = ?\n          AND ( `operation` LIKE 'UPDATE' OR `operation` LIKE 'DELETE')\n          AND last_mod >= ?\n          ORDER BY last_mod ASC", $fc, end($where), $when))) {
                 $r[$col] = self::$db->get_val($table, $col, $where);
             }
         }
         return $r;
     }
     return false;
 }
Beispiel #8
0
 public function get_users($user = '', $host = '')
 {
     $cond = '';
     if (!empty($user) && \bbn\str\text::check_name($user)) {
         $cond .= " AND  user LIKE '{$user}' ";
     }
     if (!empty($host) && \bbn\str\text::check_name($host)) {
         $cond .= " AND  host LIKE '{$host}' ";
     }
     $us = $this->db->get_rows("\n      SELECT DISTINCT host, user\n      FROM mysql.user\n      WHERE 1\n      {$cond}");
     $q = [];
     foreach ($us as $u) {
         $gs = $this->db->get_col_array("SHOW GRANTS FOR '{$u['user']}'@'{$u['host']}'");
         foreach ($gs as $g) {
             array_push($q, $g);
         }
     }
     return $q;
 }