コード例 #1
0
    function listing($base_query, $md5_get = false)
    {
        global $db_driver, $db_link;
        $md5_i = false;
        if ($md5_get) {
            preg_match('#_(\\d+)$#', $md5_get, $match);
            $md5_i = $match[1];
        }
        $base_query = trim($base_query);
        $base_query = str_cut_end($base_query, ';');
        $query = $base_query;
        $ret = array('msg' => '', 'error' => '', 'data_html' => false);
        $limit = 25;
        $offset = get('offset', 'int');
        $page = floor($offset / $limit + 1);
        if ($query) {
            if (is_select($query) && !preg_match('#\\s+LIMIT\\s+\\d+#i', $query) && !preg_match('#into\\s+outfile\\s+#', $query)) {
                $query = db_limit($query, $offset, $limit);
            } else {
                $limit = false;
            }
            $time = time_start();
            if (!db_is_safe($query, true)) {
                $ret['error'] = 'Detected UPDATE/DELETE without WHERE condition (put WHERE 1=1 if you want to execute this query)';
                return $ret;
            }
            $rs = @db_query($query);
            if ($rs) {
                if ($rs === true) {
                    if ('mysql' == $db_driver) {
                        $affected = mysql_affected_rows($db_link);
                        $time = time_end($time);
                        $ret['data_html'] = '<b>' . $affected . '</b> rows affected.<br>Time: <b>' . $time . '</b> sec';
                        return $ret;
                    }
                } else {
                    if ('pgsql' == $db_driver) {
                        $affected = @pg_affected_rows($rs);
                        if ($affected || preg_match('#^\\s*(DELETE|UPDATE)\\s+#i', $query)) {
                            $time = time_end($time);
                            $ret['data_html'] = '<p><b>' . $affected . '</b> rows affected. Time: <b>' . $time . '</b> sec</p>';
                            return $ret;
                        }
                    }
                }
                $rows = array();
                while ($row = db_row($rs)) {
                    $rows[] = $row;
                    if ($limit) {
                        if (count($rows) == $limit) {
                            break;
                        }
                    }
                }
                db_free($rs);
                if (is_select($base_query)) {
                    $found = @db_one("SELECT COUNT(*) FROM ({$base_query}) AS sub");
                    if (!is_numeric($found) || count($rows) && !$found) {
                        global $COUNT_ERROR;
                        $COUNT_ERROR = ' (COUNT ERROR) ';
                        $found = count($rows);
                    }
                } else {
                    if (count($rows)) {
                        $found = count($rows);
                    } else {
                        $found = false;
                    }
                }
                if ($limit) {
                    $pages = ceil($found / $limit);
                } else {
                    $pages = 1;
                }
                $time = time_end($time);
            } else {
                $ret['error'] = db_error();
                return $ret;
            }
        } else {
            $ret['error'] = 'No query found.';
            return $ret;
        }
        ob_start();
        ?>
	<?php 
        if (is_numeric($found)) {
            ?>
		<p>
			Found: <b><?php 
            echo $found;
            ?>
</b><?php 
            echo isset($GLOBALS['COUNT_ERROR']) ? $GLOBALS['COUNT_ERROR'] : '';
            ?>
.
			Time: <b><?php 
            echo $time;
            ?>
</b> sec.
			<?php 
            $params = array('md5' => $md5_get, 'offset' => get('offset', 'int'));
            if (get('only_marked') || post('only_marked')) {
                $params['only_marked'] = 1;
            }
            if (get('only_select') || post('only_select')) {
                $params['only_select'] = 1;
            }
            ?>
			/ <a href="<?php 
            echo url(self(), $params);
            ?>
">Refetch</a>
			/ Export to CSV:&nbsp;
			
			<a href="<?php 
            echo $_SERVER['PHP_SELF'];
            ?>
?export=csv&separator=<?php 
            echo urlencode('|');
            ?>
&query=<?php 
            echo base64_encode($base_query);
            ?>
">pipe</a>
			-
			<a href="<?php 
            echo $_SERVER['PHP_SELF'];
            ?>
?export=csv&separator=<?php 
            echo urlencode("\t");
            ?>
&query=<?php 
            echo base64_encode($base_query);
            ?>
">tab</a>
			-
			<a href="<?php 
            echo $_SERVER['PHP_SELF'];
            ?>
?export=csv&separator=<?php 
            echo urlencode(',');
            ?>
&query=<?php 
            echo base64_encode($base_query);
            ?>
">comma</a>
			-
			<a href="<?php 
            echo $_SERVER['PHP_SELF'];
            ?>
?export=csv&separator=<?php 
            echo urlencode(';');
            ?>
&query=<?php 
            echo base64_encode($base_query);
            ?>
">semicolon</a>
		</p>
	<?php 
        } else {
            ?>
		<p>Result: <b>OK</b>. Time: <b><?php 
            echo $time;
            ?>
</b> sec</p>
	<?php 
        }
        ?>

	<?php 
        if (is_numeric($found)) {
            ?>

		<?php 
            if ($pages > 1) {
                ?>
		<p>
			<?php 
                if ($page > 1) {
                    ?>
				<?php 
                    $ofs = ($page - 1) * $limit - $limit;
                    ?>
				<?php 
                    $params = array('md5' => $md5_get, 'offset' => $ofs);
                    if (get('only_marked') || post('only_marked')) {
                        $params['only_marked'] = 1;
                    }
                    if (get('only_select') || post('only_select')) {
                        $params['only_select'] = 1;
                    }
                    ?>
				<a href="<?php 
                    echo url(self(), $params);
                    ?>
">&lt;&lt; Prev</a> &nbsp;
			<?php 
                }
                ?>
			Page <b><?php 
                echo $page;
                ?>
</b> of <b><?php 
                echo $pages;
                ?>
</b> &nbsp;
			<?php 
                if ($pages > $page) {
                    ?>
				<?php 
                    $ofs = $page * $limit;
                    ?>
				<?php 
                    $params = array('md5' => $md5_get, 'offset' => $ofs);
                    if (get('only_marked') || post('only_marked')) {
                        $params['only_marked'] = 1;
                    }
                    if (get('only_select') || post('only_select')) {
                        $params['only_select'] = 1;
                    }
                    ?>
				<a href="<?php 
                    echo url(self(), $params);
                    ?>
">Next &gt;&gt;</a>
			<?php 
                }
                ?>
		</p>
		<?php 
            }
            ?>

		<script>
		function mark_row(tr)
		{
			var els = tr.getElementsByTagName('td');
			if (tr.marked) {
				for (var i = 0; i < els.length; i++) {
					els[i].style.backgroundColor = '';
				}
				tr.marked = false;
			} else {
				tr.marked = true;
				for (var i = 0; i < els.length; i++) {
					els[i].style.backgroundColor = '#ddd';
				}
			}
		}
		</script>

		<?php 
            if ($found) {
                ?>

			<?php 
                $edit_table = table_from_query($base_query);
                if ($edit_table) {
                    $edit_pk = array_first_key($rows[0]);
                    if (is_numeric($edit_pk)) {
                        $edit_table = false;
                    }
                }
                if ($edit_table) {
                    $types = table_types2($edit_table);
                    if ($types && count($types)) {
                        if (in_array($edit_pk, array_keys($types))) {
                            if (!array_col_match_unique($rows, $edit_pk, '#^\\d+$#')) {
                                $edit_pk = guess_pk($rows);
                                if (!$edit_pk) {
                                    $edit_table = false;
                                }
                            }
                        } else {
                            $edit_table = false;
                        }
                    } else {
                        $edit_table = false;
                    }
                }
                $edit_url = '';
                if ($edit_table) {
                    $edit_url = url(self(true), array('action' => 'editrow', 'table' => $edit_table, 'pk' => $edit_pk, 'id' => '%s'));
                }
                ?>

			<table class="ls" cellspacing="1">
			<tr>
				<?php 
                if ($edit_url) {
                    ?>
<th>#</th><?php 
                }
                ?>
				<?php 
                foreach ($rows[0] as $col => $v) {
                    ?>
					<th><?php 
                    echo $col;
                    ?>
</th>
				<?php 
                }
                ?>
			</tr>
			<?php 
                foreach ($rows as $row) {
                    ?>
			<tr ondblclick="mark_row(this)">
				<?php 
                    if ($edit_url) {
                        ?>
					<td><a href="javascript:void(0)" onclick="popup('<?php 
                        echo sprintf($edit_url, $row[$edit_pk]);
                        ?>
', 620, 500)">Edit</a>&nbsp;</td>
				<?php 
                    }
                    ?>
				<?php 
                    $count_cols = 0;
                    foreach ($row as $v) {
                        $count_cols++;
                    }
                    ?>
				<?php 
                    foreach ($row as $k => $v) {
                        ?>
					<?php 
                        if (preg_match('#^\\s*<a[^>]+>[^<]+</a>\\s*$#iU', $v) && strlen(strip_tags($v)) < 50) {
                            $v = strip_tags($v, '<a>');
                            $v = create_links($v);
                        } else {
                            $v = strip_tags($v);
                            $v = str_replace('&nbsp;', ' ', $v);
                            $v = preg_replace('#[ ]+#', ' ', $v);
                            $v = create_links($v);
                            if (!get('full_content') && strlen($v) > 50) {
                                if (1 == $count_cols) {
                                    $v = truncate_html($v, 255);
                                } else {
                                    $v = truncate_html($v, 50);
                                }
                            }
                            // $v = html_once($v); - create_links() disabling
                        }
                        $nl2br = get('nl2br');
                        if (get('full_content')) {
                            $v = str_wrap($v, 80, '<br>', true);
                        }
                        if (get('nl2br')) {
                            $v = nl2br($v);
                        }
                        //$v = stripslashes(stripslashes($v));
                        if (@$types[$k] == 'int' && (preg_match('#time#i', $k) || preg_match('#date#i', $k)) && preg_match('#^\\d+$#', $v)) {
                            $tmp = @date('Y-m-d H:i', $v);
                            if ($tmp) {
                                $v = $tmp;
                            }
                        }
                        global $post;
                        if (str_has($post['sql'], '@gethostbyaddr') && preg_match('#^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$#', $v)) {
                            $v = $v . '<br>' . @gethostbyaddr($v);
                        }
                        ?>
					<td onclick="mark_col(this)" <?php 
                        echo $nl2br ? 'valign="top"' : '';
                        ?>
 nowrap><?php 
                        echo is_null($row[$k]) ? '-' : $v;
                        ?>
</td>
				<?php 
                    }
                    ?>
			</tr>
			<?php 
                }
                ?>
			</table>

		<?php 
            }
            ?>

		<?php 
            if ($pages > 1) {
                ?>
		<p>
			<?php 
                if ($page > 1) {
                    ?>
				<?php 
                    $ofs = ($page - 1) * $limit - $limit;
                    ?>
				<?php 
                    $params = array('md5' => $md5_get, 'offset' => $ofs);
                    if (get('only_marked') || post('only_marked')) {
                        $params['only_marked'] = 1;
                    }
                    if (get('only_select') || post('only_select')) {
                        $params['only_select'] = 1;
                    }
                    ?>
				<a href="<?php 
                    echo url(self(), $params);
                    ?>
">&lt;&lt; Prev</a> &nbsp;
			<?php 
                }
                ?>
			Page <b><?php 
                echo $page;
                ?>
</b> of <b><?php 
                echo $pages;
                ?>
</b> &nbsp;
			<?php 
                if ($pages > $page) {
                    ?>
				<?php 
                    $ofs = $page * $limit;
                    ?>
				<?php 
                    $params = array('md5' => $md5_get, 'offset' => $ofs);
                    if (get('only_marked') || post('only_marked')) {
                        $params['only_marked'] = 1;
                    }
                    if (get('only_select') || post('only_select')) {
                        $params['only_select'] = 1;
                    }
                    ?>
				<a href="<?php 
                    echo url(self(), $params);
                    ?>
">Next &gt;&gt;</a>
			<?php 
                }
                ?>
		</p>
		<?php 
            }
            ?>

	<?php 
        }
        ?>

<?php 
        $cont = ob_get_contents();
        ob_end_clean();
        $ret['data_html'] = $cont;
        return $ret;
    }
コード例 #2
0
ファイル: liridondb.php プロジェクト: AxelFG/ckbran-inf
function str_has_any($str, $arr_needle, $ignore_case = false)
{
    if (is_string($arr_needle)) {
        $arr_needle = preg_replace('#\\s+#', ' ', $arr_needle);
        $arr_needle = explode(' ', $arr_needle);
    }
    foreach ($arr_needle as $needle) {
        if (str_has($str, $needle, $ignore_case)) {
            return true;
        }
    }
    return false;
}