public static function connect($server = 'localhost:3306', $username = '******', $password = '', $database = '') { //microtime(get_as_float) //参数 描述 //get_as_float 如果给出了 get_as_float 参数并且其值等价于 TRUE,该函数将返回一个浮点数。 $timestamp = microtime(true); SQL::$connection = mysqli_connect($server, $username, $password, $database); //mysqli_connect(host,username,password,dbname,port,socket); //参数 描述 //host 可选。规定主机名或 IP 地址。 //username 可选。规定 MySQL 用户名。 //password 可选。规定 MySQL 密码。 //dbname 可选。规定默认使用的数据库。 //port 可选。规定尝试连接到 MySQL 服务器的端口号。 //socket 可选。规定 socket 或要使用的已命名 pipe。 //round(x,prec) //参数 描述 //x 可选。规定要舍入的数字。 //prec 可选。规定小数点后的位数。 //说明 //返回将 x 根据指定精度 prec (十进制小数点后数字的数目)进行四舍五入的结果。prec 也可以是负数或零(默认值)。 DEBUG::put('Connected: ' . $username . '@' . $server . ' spent: ' . round((microtime(true) - $timestamp) * 1000, 3) . 'ms', 'MySQL'); $err = SQL::error(); if ($err['id'] > 0) { DEBUG::put(' - Error: #' . $err['id'] . ' ' . $err['error'], 'MySQL'); } return SQL::$connection; }
/** * execute query and return all data in a reader * * @return SQLDataReader */ public function execute_reader() { $this->parse_query(); if (empty($this->queryParsed)) { return new SQLDataReader(); } $close = $this->conn->status() == 'closed'; $this->conn->open(); $res = $this->conn->query($this->queryParsed); $data = array(); while ($row = $this->conn->fetch_array($res)) { $data[] = $row; } // SQLite causes 'unknown error' after successful fetch of all data. // Don't have a clue why... $ret = empty($this->conn->error()) || $this->conn->error() == 'unknown error'; if ($close) { $this->conn->close(); } return $ret ? new SQLDataReader($data) : new SQLDataReader(); }
function run_sql_script($dbhost, $dbuser, $dbpass, $dbname, $path, $unlink) { global $lang_global; $fp = fopen($path, 'r') or die(error("Couldn't Open File!")); $sql_1 = new SQL(); $sql_1->connect($dbhost, $dbuser, $dbpass, $dbname); $query = ""; $queries = 0; $linenumber = 0; $inparents = false; while (!feof($fp)) { $dumpline = ""; while (!feof($fp) && substr($dumpline, -1) != "\n") { $dumpline .= fgets($fp, 16384); } $dumpline = ereg_replace("\r\n\$", "\n", $dumpline); $dumpline = ereg_replace("\r\$", "\n", $dumpline); if (!$inparents) { $skipline = false; if (!$inparents && (trim($dumpline) == "" || strpos($dumpline, '#') === 0 || strpos($dumpline, '-- ') === 0)) { $skipline = true; } if ($skipline) { $linenumber++; continue; } } $dumpline_deslashed = str_replace("\\\\", "", $dumpline); $parents = substr_count($dumpline_deslashed, "'") - substr_count($dumpline_deslashed, "\\'"); if ($parents % 2 != 0) { $inparents = !$inparents; } $query .= $dumpline; if (ereg(";\$", trim($dumpline)) && !$inparents) { if (!$sql_1->query(trim($query))) { fclose($fp); if ($unlink) { unlink($path); } $err = ereg_replace("\n", "", $sql_1->error()); $err = ereg_replace("\r\n\$", "", $err); $err = ereg_replace("\r\$", "", $err); error("SQL Error at the line: {$linenumber} in {$path} <br /> {$err}"); break; } $queries++; $query = ""; } $linenumber++; } $sql_1->close(); fclose($fp); return $queries; }
/** * 连接到数据库 * 成功返回true, 失败返回false * * @param string $server * @param string $username * @param string $password * @param string $database * @return bool */ public static function connect($server = 'localhost:3306', $username = '******', $password = '', $database = '') { $timestamp = microtime(true); SQL::$connection = mysqli_connect($server, $username, $password, $database); DEBUG::put('Connected: ' . $username . '@' . $server . ' spent: ' . round((microtime(true) - $timestamp) * 1000, 3) . 'ms', 'MySQL'); $err = SQL::error(); if ($err['id'] > 0) { DEBUG::put(' - Error: #' . $err['id'] . ' ' . $err['error'], 'MySQL'); } return SQL::$connection; }
/** * build one table * * Accept following variants: * - csv - to provide a downloadable csv page * - json - to provide all values in one column * - inline - to render tables within articles * - simple - the legacy fixed table * - sortable - click on column to sort the row * * @param the id of the table to build * @param string the variant to provide - default is 'simple' * @return a displayable string */ public static function build($id, $variant = 'simple') { global $context; // split parameters $attributes = preg_split("/\\s*,\\s*/", $id, 3); $id = $attributes[0]; // get the table object if (!($table = Tables::get($id))) { return NULL; } // do the SELECT statement if (!($rows = SQL::query($table['query']))) { Logger::error(sprintf(i18n::s('Error in table query %s'), $id) . BR . htmlspecialchars($table['query']) . BR . SQL::error()); return NULL; } // build the resulting string $text = ''; switch ($variant) { // produce a table readable into MS-Excel case 'csv': // comma separated values $separator = ","; // one row for the title if ($table['title']) { $label = preg_replace('/\\s/', ' ', $table['title']); // encode to ASCII $label = utf8::to_ascii($label, PRINTABLE_SAFE_ALPHABET); // escape quotes to preserve them in the data $label = str_replace('"', '""', $label); $text .= '"' . $label . '"'; $text .= "\n"; } // one row for header fields $index = 0; while ($field = SQL::fetch_field($rows)) { if ($index++) { $text .= $separator; } $label = trim(preg_replace('/\\s/', ' ', ucfirst($field->name))); // encode $label = utf8::to_ascii($label, PRINTABLE_SAFE_ALPHABET); // escape quotes to preserve them in the data $label = str_replace('"', '""', $label); $text .= '"' . $label . '"'; } $text .= "\n"; // process every table row $row_index = 0; while ($row = SQL::fetch($rows)) { // one cell at a time $index = 0; foreach ($row as $name => $value) { // glue cells if ($index++) { $text .= $separator; } // remove HTML tags $value = strip_tags(str_replace('</', ' </', str_replace(BR, ' / ', $value))); // clean spaces $label = trim(preg_replace('/\\s+/', ' ', $value)); // encode $label = utf8::to_ascii($label, PRINTABLE_SAFE_ALPHABET); // escape quotes to preserve them in the data $label = str_replace('"', '""', $label); // make a link if this is a reference if ($index == 1 && $table['with_zoom'] == 'Y') { $label = $context['url_to_home'] . $context['url_to_root'] . $label; } // quote data $text .= '"' . $label . '"'; } // new line $text .= "\n"; } return $text; // a JSON set of values // a JSON set of values case 'json': // get header labels $labels = array(); while ($field = SQL::fetch_field($rows)) { $labels[] = trim(preg_replace('/[^\\w]+/', '', ucfirst($field->name))); } // all items $data = array(); $data['items'] = array(); while ($row = SQL::fetch_row($rows)) { // all rows $datum = array(); $label = FALSE; $index = 0; $link = NULL; foreach ($row as $name => $value) { $index++; // first column is only a link if ($index == 1 && $table['with_zoom'] == 'Y') { $link = $context['url_to_home'] . $context['url_to_root'] . ltrim($value, '/'); continue; } // adjust types to not fool the json encoder if (preg_match('/^(\\+|-){0,1}[0-9]+$/', $value)) { $value = intval($value); } elseif (preg_match('/^(\\+|-){0,1}[0-9\\.,]+$/', $value)) { $value = floatval($value); } elseif (preg_match('/^(true|false)$/i', $value)) { $value = intval($value); } // ensure we have some label for SIMILE Exhibit if (!$label) { $label = $value; } // combine first and second columns if ($index == 2 && $link) { $value = Skin::build_link($link, $value, 'basic'); } // save this value $datum[$labels[$name]] = utf8::to_ascii($value, PRINTABLE_SAFE_ALPHABET); } if ($label && !in_array('label', $labels)) { $datum['label'] = utf8::to_ascii($label, PRINTABLE_SAFE_ALPHABET); } // add a tip, if any $data['items'][] = $datum; } include_once $context['path_to_root'] . 'included/json.php'; $text .= json_encode2($data); return $text; // list of facets for SIMILE Exhibit // list of facets for SIMILE Exhibit case 'json-facets': // columns are actual facets $facets = array(); $index = 0; while ($field = SQL::fetch_field($rows)) { $index++; // first column is only a link if ($index == 1 && $table['with_zoom'] == 'Y') { continue; } // first column has a link if ($index == 2 && $table['with_zoom'] == 'Y') { continue; } // column is a facet $label = '.' . trim(preg_replace('/[^\\w]+/', '', ucfirst($field->name))); $title = trim(str_replace(',', '', ucfirst($field->name))); $facets[] = '<div ex:role="facet" ex:expression="' . $label . '" ex:facetLabel="' . $title . '"></div>'; // only last columns can be faceted if (count($facets) > 7) { array_shift($facets); } } // reverse facet order $facets = array_reverse($facets); // job done $text = join("\n", $facets); return $text; // list of columns for SIMILE Exhibit // list of columns for SIMILE Exhibit case 'json-labels': // get header labels $labels = array(); $index = 0; while ($field = SQL::fetch_field($rows)) { $index++; // first column is only a link if ($index == 1 && $table['with_zoom'] == 'Y') { continue; } // column id $labels[] = '.' . trim(preg_replace('/[^\\w]+/', '', ucfirst($field->name))); // limit the number of columns put on screen if (count($labels) >= 7) { break; } } // job done $text = join(', ', $labels); return $text; // titles of columns for SIMILE Exhibit // titles of columns for SIMILE Exhibit case 'json-titles': // get header labels $labels = array(); $index = 0; while ($field = SQL::fetch_field($rows)) { $index++; // first column is only a link if ($index == 1 && $table['with_zoom'] == 'Y') { continue; } // column header $labels[] = trim(str_replace(',', '', ucfirst($field->name))); } $text = join(', ', $labels); return $text; // produce an HTML table // produce an HTML table default: case 'inline': case 'sortable': // a table with a grid $text .= Skin::table_prefix('grid'); // the title, with a menu to download the table into Excel if ($variant == 'inline') { $item_bar = array(); $item_bar += array(Tables::get_url($id) => $table['title']); $item_bar += array(Tables::get_url($id, 'fetch_as_csv') => 'CSV (Excel)'); if (Surfer::is_associate()) { $item_bar += array(Tables::get_url($id, 'edit') => i18n::s('edit')); } if (count($item_bar)) { $text .= '<caption>' . Skin::build_list($item_bar, 'menu') . "</caption>\n"; } } // column headers are clickable links $cells = array(); $index = 0; while ($field = SQL::fetch_field($rows)) { if ($index++ != 0 || $table['with_zoom'] != 'Y') { $cells[] = ucfirst($field->name); } } $text .= "\t\t" . Skin::table_row($cells, 'sortable'); // the table body $count = 0; $row_index = 0; while ($row = SQL::fetch_row($rows)) { $cells = array(); $link = ''; for ($index = 0; $index < count($row); $index++) { if ($index == 0 && $table['with_zoom'] == 'Y') { $link = $row[$index]; } elseif ($link) { $cells[] = Skin::build_link($link, $row[$index]); $link = ''; } else { $cells[] = $row[$index]; } } $text .= "\t\t" . Skin::table_row($cells, $count++); } $text .= Skin::table_suffix(); return $text; // adapted to open chart flash // adapted to open chart flash case 'chart': // get title for y series $y_title = $y2_title = $y3_title = NULL; $y_index = $y2_index = $y3_index = 0; $index = 0; while ($field = SQL::fetch_field($rows)) { // time will be used for x labels if ($index == 0 && $table['with_zoom'] == 'T') { } elseif ($index == 0 && $table['with_zoom'] == 'Y') { } elseif (!$y_title) { $y_title = '"' . ucfirst($field->name) . '"'; $y_index = $index; } elseif (!$y2_title) { $y2_title = '"' . ucfirst($field->name) . '"'; $y2_index = $index; } elseif (!$y3_title) { $y3_title = '"' . ucfirst($field->name) . '"'; $y3_index = $index; break; } $index++; } // process every table row $x_labels = array(); $y_values = array(); $y2_values = array(); $y3_values = array(); $y_min = $y_max = NULL; $count = 1; while ($row = SQL::fetch($rows)) { // one cell at a time $index = 0; foreach ($row as $name => $value) { // clean spaces $label = trim(preg_replace('/\\s/', ' ', $value)); // encode in iso8859 $label = utf8::to_iso8859($label); // escape quotes to preserve them in the data $label = str_replace('"', '""', $label); // quote data if (preg_match('/-*[^0-9\\,\\.]/', $label)) { $label = '"' . $label . '"'; } // x labels if ($index == 0) { if ($table['with_zoom'] == 'T') { array_unshift($x_labels, $label); } else { $x_labels[] = $count++; } // y value } elseif ($index == $y_index) { if ($table['with_zoom'] == 'T') { array_unshift($y_values, $label); } else { $y_values[] = $label; } if (!isset($y_min) || intval($label) < $y_min) { $y_min = intval($label); } if (!isset($y_max) || intval($label) > $y_max) { $y_max = intval($label); } // y2 value } elseif ($index == $y2_index) { if ($table['with_zoom'] == 'T') { array_unshift($y2_values, $label); } else { $y_values[] = $label; } if (!isset($y_min) || intval($label) < $y_min) { $y_min = intval($label); } if (!isset($y_max) || intval($label) > $y_max) { $y_max = intval($label); } // y3 value } elseif ($index == $y3_index) { if ($table['with_zoom'] == 'T') { array_unshift($y3_values, $label); } else { $y_values[] = $label; } if (!isset($y_min) || intval($label) < $y_min) { $y_min = intval($label); } if (!isset($y_max) || intval($label) > $y_max) { $y_max = intval($label); } // we won't process the rest break; } // next column $index++; } } // y minimum if ($y_min > 0) { $y_min = 0; } // y maximum $y_max = strval($y_max); if (strlen($y_max) == 1) { $y_max = 10; } elseif (strlen($y_max) == 2) { $y_max = (intval(substr($y_max, 0, 1)) + 1) * 10; } elseif (strlen($y_max) == 3) { $y_max = (intval(substr($y_max, 0, 2)) + 1) * 10; } else { $y_max = strval(intval(substr($y_max, 0, 2)) + 1) . substr('0000000000000000000000000000000000000000000000000000', 0, strlen($y_max) - 2); } // data series $elements = array(); if (count($y_values)) { $elements[] = '{ "type":"bar_glass", "colour":"#BF3B69", "values": [ ' . join(',', $y_values) . ' ], "text": ' . $y_title . ', "font-size": 12 }'; } if (count($y2_values)) { $elements[] = '{ "type": "line", "width": 1, "colour": "#5E4725", "values": [ ' . join(',', $y2_values) . ' ], "text": ' . $y2_title . ', "font-size": 12 }'; } if (count($y3_values)) { $elements[] = '{ "type":"bar_glass", "colour":"#5E0722", "values": [ ' . join(',', $y3_values) . ' ], "text": ' . $y3_title . ', "font-size": 12 }'; } // the full setup $text = '{ "elements": [ ' . join(',', $elements) . ' ], "x_axis": { "offset": false, "steps": 1, "labels": { "steps": 3, "rotate": 310, "labels": [ ' . join(',', $x_labels) . ' ] } }, "y_axis": { "min": ' . $y_min . ', "max": ' . $y_max . ' } }'; return $text; // first number // first number case 'column': // comma separated values $separator = ","; // process every table row while ($row = SQL::fetch($rows)) { // not always the first column $index = 0; foreach ($row as $name => $value) { $index++; // skip dates and links if ($index == 1 && $table['with_zoom'] != 'N') { continue; } // glue cells if ($text) { $text .= $separator; } // clean spaces $label = trim(preg_replace('/\\s/', ' ', $value)); // encode in iso8859 $label = utf8::to_iso8859($label); // escape quotes to preserve them in the data $label = str_replace('"', '""', $label); // quote data if (preg_match('/[^a-zA-Z0-9\\,\\.\\-_]/', $label)) { $text .= '"' . $label . '"'; } else { $text .= $label; } // only first column break; } } return $text; // produce a raw table // produce a raw table case 'raw': // comma separated values $separator = ","; // process every table row while ($row = SQL::fetch($rows)) { // one cell at a time $index = 0; foreach ($row as $name => $value) { // glue cells if ($index++) { $text .= $separator; } // clean spaces $label = trim(preg_replace('/\\s/', ' ', $value)); // encode in iso8859 $label = utf8::to_iso8859($label); // escape quotes to preserve them in the data $label = str_replace('"', '""', $label); // make a link if this is a reference if ($index == 0 && $table['with_zoom'] == 'Y') { $label = $context['url_to_home'] . $context['url_to_root'] . $label; } // quote data if (preg_match('/[^a-zA-Z0-9\\-_]/', $label)) { $text .= '"' . $label . '"'; } else { $text .= $label; } } // new line $text .= "\n"; } return $text; // a simple table // a simple table case 'simple': $text .= Skin::table_prefix('table'); // columns headers $index = 0; while ($field = SQL::fetch_field($rows)) { $cells[] = ucfirst($field->name); } $text .= Skin::table_row($cells, 'header'); // other rows while ($row = SQL::fetch_row($rows)) { $text .= Skin::table_row($row, $count++); } $text .= Skin::table_suffix(); return $text; // xml table // xml table case 'xml': $text = ''; while ($row = SQL::fetch($rows)) { $text .= ' <item>' . "\n"; foreach ($row as $name => $value) { $type = preg_replace('/[^a-z0-9]+/i', '_', $name); if (preg_match('/^[^a-z]/i', $type)) { $type = '_' . $type; } $text .= ' <' . $type . '>' . preg_replace('/&(?!(amp|#\\d+);)/i', '&', utf8::transcode(str_replace(array('left=', 'right='), '', $value))) . '</' . $type . '>' . "\n"; } $text .= ' </item>' . "\n\n"; } return '<?xml version="1.0" encoding="' . $context['charset'] . '"?>' . "\n" . '<items>' . "\n" . $text . '</items>' . "\n"; } }
/** * 更新moreinfo * @param unknown_type $id * @param unknown_type $play_method * @param unknown_type $codes * @param unknown_type $ticket_type */ public function update_jc_ticket_sp($id, $play_method, $codes, $ticket_type = 1) { require_once WEBROOT . 'cron_script/SQL.php'; $sql_obj = new SQL(); $return = array(); $code_a = explode(';', $codes); $code = $code_a[0]; $match_detail = explode('/', $code); for ($i = 0; $i < count($match_detail); $i++) { $match_info = explode('|', $match_detail[$i]); $match_id = $match_info[0]; preg_match_all("/\\[(.*)\\]/", $match_info[1], $match_result, PREG_SET_ORDER); $match_results = $match_result[0][1]; $match_results_a = explode(',', $match_results); $match_result_sp = array(); $select_match_query = 'select comb,goalline from match_datas where ticket_type="' . $ticket_type . '" and play_type="' . $play_method . '" and match_id="' . $match_id . '" limit 1'; $sql_obj->query($select_match_query); $match_data = $sql_obj->fetch_array(); $sp = $match_data['comb']; $goalline = $match_data['goalline']; //var_dump($sp); $play_config = array(); if ($ticket_type == 1) { switch ($play_method) { case 1: $play_config = array('3' => 'H', '1' => 'D', '0' => 'A'); break; case 2: $play_config = array('0' => '0', '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5', '6' => '6', '7' => '7'); break; case 3: $play_config = array('负其它' => '-1:-A', '胜其它' => '-1:-H', '平其它' => '-1:-D', '0:0' => '00:00', '0:1' => '00:01', '0:2' => '00:02', '0:3' => '00:03', '0:4' => '00:04', '0:5' => '00:05', '1:0' => '01:00', '1:1' => '01:01', '1:2' => '01:02', '1:3' => '01:03', '1:4' => '01:04', '1:5' => '01:05', '2:0' => '02:00', '2:1' => '02:01', '2:2' => '02:02', '2:3' => '02:03', '2:4' => '02:04', '2:5' => '02:05', '3:0' => '03:00', '3:1' => '03:01', '3:2' => '03:02', '3:3' => '03:03', '4:0' => '04:00', '4:1' => '04:01', '4:2' => '04:02', '5:0' => '05:00', '5:1' => '05:01', '5:2' => '05:02'); break; case 4: $play_config = array('0-0' => 'cc', '0-1' => 'cb', '0-3' => 'ca', '1-0' => 'bc', '1-1' => 'bb', '1-3' => 'ba', '3-0' => 'ac', '3-1' => 'ab', '3-3' => 'aa'); break; default: break; } } if ($ticket_type == 6) { switch ($play_method) { case 1: $play_config = array('2' => 'H', '1' => 'D'); break; case 2: $play_config = array('2' => 'H', '1' => 'D'); break; case 3: $play_config = array('01' => 'u4e3bu80dc1-5', '02' => 'u4e3bu80dc6-10', '03' => 'u4e3bu80dc11-15', '04' => 'u4e3bu80dc16-20', '05' => 'u4e3bu80dc21-25', '06' => 'u4e3bu80dc26+', '11' => 'u5ba2u80dc1-5', '12' => 'u5ba2u80dc6-10', '13' => 'u5ba2u80dc11-15', '14' => 'u5ba2u80dc16-20', '15' => 'u5ba2u80dc21-25', '16' => 'u5ba2u80dc26+'); break; case 4: $play_config = array('1' => 'H', '2' => 'D'); break; default: break; } } //$sp = '{"cc":{"c":"cc","v":"4.30","s":"1","d":"2011-09-06","t":"05:59:00"},"cb":{"c":"cb","v":"15.00","s":"1","d":"2011-09-06","t":"05:59:00"},"ca":{"c":"ca","v":"28.00","s":"1","d":"2011-09-06","t":"05:59:00"},"bc":{"c":"bc","v":"6.50","s":"1","d":"2011-09-06","t":"05:59:00"},"bb":{"c":"bb","v":"4.50","s":"1","d":"2011-09-06","t":"05:59:00"},"ba":{"c":"ba","v":"5.40","s":"1","d":"2011-09-06","t":"05:59:00"},"ac":{"c":"ac","v":"34.00","s":"1","d":"2011-09-06","t":"05:59:00"},"ab":{"c":"ab","v":"15.00","s":"1","d":"2011-09-06","t":"05:59:00"},"aa":{"c":"aa","v":"3.85","s":"1","d":"2011-09-06","t":"05:59:00"}}'; $sp = json_decode($sp); $result_sp = array(); foreach ($sp as $key => $val) { if (isset($val->c)) { $result_sp[$val->c] = $val->v; } else { $result_sp[] = $val->v; } } //var_dump($result_sp); for ($j = 0; $j < count($match_results_a); $j++) { $key = $play_config[$match_results_a[$j]]; if (array_key_exists($key, $result_sp)) { $match_result_sp[] = $result_sp[$key]; } } $match_result_sp = implode(',', $match_result_sp); //$return[] = $match_id.':'.$match_result_sp; if ($ticket_type == 6 && ($play_method == 2 || $play_method == 4)) { if ($play_method == 2 && $goalline > 0) { $goalline = '+' . $goalline; } $return[] = $match_id . '(' . $goalline . '):' . $match_result_sp; } else { $return[] = $match_id . ':' . $match_result_sp; } //var_dump($return); //echo $i; } $return = implode('|', $return); //var_dump($return); $sql_obj->query('update ticket_nums set moreinfo="' . $return . '" where id="' . $id . '"'); if (!$sql_obj->error()) { return true; } else { return false; } }
/** * post a new server or an updated server * * @see servers/edit.php * @see servers/populate.php * * @param array an array of fields * @return string either a null string, or some text describing an error to be inserted into the html response **/ public static function post(&$fields) { global $context; // no title if (!$fields['title']) { return i18n::s('No title has been provided.'); } // clear the cache for servers Cache::clear('servers'); if (isset($fields['id'])) { Cache::clear('server:' . $fields['id']); } // protect from hackers if (isset($fields['main_url'])) { $fields['main_url'] = encode_link($fields['main_url']); } if (isset($fields['feed_url'])) { $fields['feed_url'] = encode_link($fields['feed_url']); } if (isset($fields['ping_url'])) { $fields['ping_url'] = encode_link($fields['ping_url']); } if (isset($fields['search_url'])) { $fields['search_url'] = encode_link($fields['search_url']); } if (isset($fields['monitor_url'])) { $fields['monitor_url'] = encode_link($fields['monitor_url']); } // make a host name if (!isset($fields['host_name'])) { $fields['host_name'] = ''; } if (!$fields['host_name']) { if (($parts = parse_url($fields['main_url'])) && isset($parts['host'])) { $fields['host_name'] = $parts['host']; } } if (!$fields['host_name']) { if (($parts = parse_url($fields['feed_url'])) && isset($parts['host'])) { $fields['host_name'] = $parts['host']; } } if (!$fields['host_name']) { if (($parts = parse_url($fields['ping_url'])) && isset($parts['host'])) { $fields['host_name'] = $parts['host']; } } if (!$fields['host_name']) { if (($parts = parse_url($fields['monitor_url'])) && isset($parts['host'])) { $fields['host_name'] = $parts['host']; } } if (!$fields['host_name']) { if (($parts = parse_url($fields['search_url'])) && isset($parts['host'])) { $fields['host_name'] = $parts['host']; } } // set default values if (!isset($fields['active']) || !$fields['active']) { $fields['active'] = 'Y'; } if (!isset($fields['process_ping']) || $fields['process_ping'] != 'Y') { $fields['process_ping'] = 'N'; } if (!isset($fields['process_monitor']) || $fields['process_monitor'] != 'Y') { $fields['process_monitor'] = 'N'; } if (!isset($fields['process_search']) || $fields['process_search'] != 'Y') { $fields['process_search'] = 'N'; } // set default values for this editor Surfer::check_default_editor($fields); // update the existing record if (isset($fields['id'])) { // id cannot be empty if (!isset($fields['id']) || !is_numeric($fields['id'])) { return i18n::s('No item has the provided id.'); } // update the existing record $query = "UPDATE " . SQL::table_name('servers') . " SET " . "title='" . SQL::escape($fields['title']) . "', " . "description='" . SQL::escape($fields['description']) . "', " . "main_url='" . SQL::escape($fields['main_url']) . "', " . "anchor='" . SQL::escape(isset($fields['anchor']) ? $fields['anchor'] : '') . "', " . "submit_feed='" . SQL::escape($fields['submit_feed'] == 'Y' ? 'Y' : 'N') . "', " . "feed_url='" . SQL::escape($fields['feed_url']) . "', " . "submit_ping='" . SQL::escape($fields['submit_ping'] == 'Y' ? 'Y' : 'N') . "', " . "ping_url='" . SQL::escape($fields['ping_url']) . "', " . "process_ping='" . SQL::escape($fields['process_ping'] == 'Y' ? 'Y' : 'N') . "', " . "submit_monitor='" . SQL::escape($fields['submit_monitor'] == 'Y' ? 'Y' : 'N') . "', " . "monitor_url='" . SQL::escape($fields['monitor_url']) . "', " . "process_monitor='" . SQL::escape($fields['process_monitor'] == 'Y' ? 'Y' : 'N') . "', " . "submit_search='" . SQL::escape($fields['submit_search'] == 'Y' ? 'Y' : 'N') . "', " . "search_url='" . SQL::escape($fields['search_url']) . "', " . "process_search='" . SQL::escape($fields['process_search'] == 'Y' ? 'Y' : 'N') . "'," . "host_name='" . SQL::escape($fields['host_name']) . "'," . "active='" . SQL::escape($fields['active']) . "'"; // maybe a silent update if (!isset($fields['silent']) || $fields['silent'] != 'Y') { $query .= ", " . "edit_name='" . SQL::escape($fields['edit_name']) . "', " . "edit_id=" . SQL::escape($fields['edit_id']) . ", " . "edit_address='" . SQL::escape($fields['edit_address']) . "', " . "edit_date='" . SQL::escape($fields['edit_date']) . "'"; } $query .= " WHERE id = " . SQL::escape($fields['id']); if (SQL::query($query) === FALSE) { return $query . BR . SQL::error(); } // insert a new record } else { // always remember the date $query = "INSERT INTO " . SQL::table_name('servers') . " SET "; if (isset($fields['id']) && $fields['id']) { $query .= "id='" . SQL::escape($fields['id']) . "',"; } $query .= "title='" . SQL::escape($fields['title']) . "', " . "host_name='" . SQL::escape($fields['host_name']) . "', " . "description='" . SQL::escape(isset($fields['description']) ? $fields['description'] : '') . "', " . "main_url='" . SQL::escape($fields['main_url']) . "', " . "anchor='" . SQL::escape(isset($fields['anchor']) ? $fields['anchor'] : 'category:1') . "', " . "submit_feed='" . SQL::escape($fields['submit_feed'] == 'Y' ? 'Y' : 'N') . "', " . "feed_url='" . SQL::escape($fields['feed_url']) . "', " . "submit_ping='" . SQL::escape($fields['submit_ping'] == 'Y' ? 'Y' : 'N') . "', " . "ping_url='" . SQL::escape($fields['ping_url']) . "', " . "process_ping='" . SQL::escape($fields['process_ping'] == 'Y' ? 'Y' : 'N') . "', " . "submit_monitor='" . SQL::escape($fields['submit_monitor'] == 'Y' ? 'Y' : 'N') . "', " . "monitor_url='" . SQL::escape($fields['monitor_url']) . "', " . "process_monitor='" . SQL::escape($fields['process_monitor'] == 'Y' ? 'Y' : 'N') . "', " . "submit_search='" . SQL::escape($fields['submit_search'] == 'Y' ? 'Y' : 'N') . "', " . "search_url='" . SQL::escape($fields['search_url']) . "', " . "process_search='" . SQL::escape($fields['process_search'] == 'Y' ? 'Y' : 'N') . "', " . "active='" . SQL::escape($fields['active']) . "', " . "edit_name='" . SQL::escape($fields['edit_name']) . "', " . "edit_id=" . SQL::escape($fields['edit_id']) . ", " . "edit_address='" . SQL::escape($fields['edit_address']) . "', " . "edit_date='" . SQL::escape($fields['edit_date']) . "'"; if (SQL::query($query) === FALSE) { return $query . BR . SQL::error(); } } // end of job return NULL; }
function do_run_patch() { global $output, $lang_run_patch, $world_db, $realm_db, $characters_db, $mmfpm_db; if (empty($_POST['query']) || empty($_POST['use_db'])) { redirect('run_patch.php?error=1'); } $sql = new SQL(); $sql->connect($realm_db['addr'], $realm_db['user'], $realm_db['pass'], $realm_db['name']); $use_db = $sql->quote_smart($_POST['use_db']); $query = $_POST['query']; if ($use_db == $realm_db['name']) { $sql->db($realm_db['name']); } elseif ($use_db == $mmfpm_db['name']) { $sql->connect($mmfpm_db['addr'], $mmfpm_db['user'], $mmfpm_db['pass'], $mmfpm_db['name']); } else { foreach ($world_db as $db) { if ($use_db == $db['name']) { $sql->connect($db['addr'], $db['user'], $db['pass'], $db['name']); } } foreach ($characters_db as $db) { if ($use_db == $db['name']) { $sql->connect($db['addr'], $db['user'], $db['pass'], $db['name']); } } } unset($use_db); $new_queries = array(); $line = 0; $queries = explode("\n", $query); unset($query); $n_queries = count($queries); for ($i = 0; $i < $n_queries; ++$i) { $queries[$i] = trim($queries[$i]); if (0 === strpos($queries[$i], '#') || 0 === strpos($queries[$i], '--')) { ++$line; } else { array_push($new_queries, $queries[$i]); } } unset($n_queries); $qr = split(";\n", implode("\n", $new_queries)); unset($new_queries); $good = 0; $bad = 0; foreach ($qr as $qry) { ++$line; if (trim($qry)) { $sql->query(trim($qry)) ? ++$good : ++$bad; } if ($bad) { $err = ereg_replace('\\n', '', $sql->error()); $err = ereg_replace('\\r\\n$', '', $err); $err = ereg_replace('\\r$', '', $err); error($lang_run_patch['err_in_line'] . ': ' . $line . ' <br />' . $err); exit; } } unset($qry); unset($qr); unset($line); unset($bad); if ($queries) { redirect('run_patch.php?error=2&tot=' . $good . ''); } else { redirect('run_patch.php?error=3'); } }
/** * Create or alter the structure of one table * * @param string the name of the table to setup * @param array of $field_name => $field_declaration * @param array of $index_name => $index_declaration * @param array of SQL statements to be executed * @return a text string to print */ public static function setup_table($table, $fields, $indexes, $statements = NULL) { global $context; // sanity check if (!$table) { return ''; } // if the table does not exist if (!SQL::has_table($table)) { // create it $query = "CREATE TABLE " . SQL::table_name($table) . " ( "; $count = 0; foreach ($fields as $field => $definition) { if ($count++) { $query .= ", "; } $query .= '`' . $field . '` ' . $definition; } foreach ($indexes as $index => $definition) { if ($count++) { $query .= ", "; } $query .= $index . ' ' . $definition; } $query .= " ) ENGINE MyISAM"; // else if the table exists } else { // check its structure $query = "ALTER TABLE " . SQL::table_name($table) . " "; // analyse table structure $query2 = "DESCRIBE " . SQL::table_name($table); if (!($result = SQL::query($query2))) { return '<p>' . Logger::error_pop() . "</p>\n"; } // build the list of fields while ($row = SQL::fetch($result)) { $actual[] = $row['Field']; } // check all fields $count = 0; foreach ($fields as $field => $definition) { if ($count++) { $query .= ", "; } if (in_array($field, $actual)) { $query .= "MODIFY"; } else { $query .= "ADD"; } $query .= ' `' . $field . '` ' . $definition; } // drop the primary index $query .= ", DROP PRIMARY KEY"; // list existing indexes $query2 = "SHOW INDEX FROM " . SQL::table_name($table); if (!($result = SQL::query($query2))) { return '<p>' . Logger::error_pop() . "</p>\n"; } // drop other indexes while ($row = SQL::fetch($result)) { if ($row['Seq_in_index'] == 1 && $row['Key_name'] != 'PRIMARY') { $query .= ', DROP INDEX ' . $row['Key_name']; } } SQL::free($result); // build new indexes foreach ($indexes as $index => $definition) { $query .= ", ADD " . $index . ' ' . $definition; } } // execute the query if (SQL::query($query) !== FALSE) { // message to the user $text = BR . i18n::s('The table') . " '" . $table . "'"; // it's a success if (strpos($query, 'CREATE') === 0) { $text .= ' ' . i18n::s('has been created'); } else { $text .= ' ' . i18n::s('has been updated'); } // ensure utf8 character set for this table $query = "ALTER TABLE " . SQL::table_name($table) . " DEFAULT CHARACTER SET utf8"; if (SQL::query($query) !== FALSE) { $text .= ' (utf8)'; } // silently analyze table $query = "ANALYZE TABLE " . SQL::table_name($table); if (($result = SQL::query($query)) && ($row = SQL::fetch($result)) && $row['Msg_type'] == 'status') { $text .= ' ' . i18n::s('and analyzed'); SQL::free($result); } // optimize the table $query = "OPTIMIZE TABLE " . SQL::table_name($table); if (($result = SQL::query($query)) && ($row = SQL::fetch($result)) && $row['Msg_type'] == 'status') { $text .= ' ' . i18n::s('and optimized'); SQL::free($result); } // add views, eventually if ($statements && is_array($statements)) { // process each statement in sequence foreach ($statements as $statement) { // detect errors, if any if (SQL::query($statement) === FALSE) { $text .= '<p>' . sprintf(i18n::s('ERROR for the table %s'), $table) . BR . $statement . BR . SQL::error() . '</p>'; } } } // houston, we got a problem } else { // message to the user $text = '<p>' . sprintf(i18n::s('ERROR for the table %s'), $table) . BR . $query . BR . SQL::error() . '</p>'; } return $text; }
while ($tokens = fgetcsv($handle, 2048, $delimiter, $enclosure)) { // insert one record at a time $query = "INSERT INTO " . SQL::escape($_REQUEST['table_name']) . " (" . $headers . ") VALUES ("; // use all provided tokens $index = 0; foreach ($tokens as $token) { if ($index++) { $query .= ', '; } $query .= "'" . SQL::escape($token) . "'"; } // finalize the statement $query .= ')'; // execute the statement if (!SQL::query($query, TRUE) && SQL::errno()) { $context['text'] .= '<p>' . $here . ': ' . $query . BR . SQL::error() . "</p>\n"; } $queries++; // ensure we have enough time if (!($queries % 50)) { Safe::set_time_limit(30); } } // clear the cache Cache::clear(); // report of script data $time = round(get_micro_time() - $context['start_time'], 2); $context['text'] .= '<p>' . sprintf(i18n::s('%d SQL statements have been processed in %.2f seconds.'), $queries, $time) . '</p>'; // remember this in log as well Logger::remember('tables/import.php: Data has been imported into ' . $_REQUEST['table_name'], $queries . ' SQL statements have been processed in ' . $time . ' seconds.'); }
/** * delete all documentation pages */ public static function purge() { global $context; // purge the old documentation $query = "DELETE FROM " . SQL::table_name('phpdoc'); if (SQL::query($query, TRUE) === FALSE && SQL::errno()) { echo $query . BR . SQL::error() . BR . "\n"; } }