From ea15574226bee467a3ce1b92bc2240c3f4e46913 Mon Sep 17 00:00:00 2001 From: "green-ponies (jgen)" Date: Wed, 11 Jan 2012 15:08:27 -0500 Subject: [PATCH 01/13] Changes and tweaks for speed. Because every microsecond counts! :P These changes are based on information from: http://phpbench.com/ http://stackoverflow.com/questions/482202/is-there-a-performance-benefit-single-quote-vs-double-quote-in-php --- core/database.class.php | 12 ++++---- core/imageboard.pack.php | 11 ++++--- core/util.inc.php | 66 +++++++++++++++++++++------------------- 3 files changed, 47 insertions(+), 42 deletions(-) diff --git a/core/database.class.php b/core/database.class.php index 53ed4b4d..6e8a0213 100644 --- a/core/database.class.php +++ b/core/database.class.php @@ -292,17 +292,17 @@ class Database { $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $db_proto = $this->db->getAttribute(PDO::ATTR_DRIVER_NAME); - if($db_proto == "mysql") { + if($db_proto === "mysql") { $this->engine = new MySQL(); } - else if($db_proto == "pgsql") { + else if($db_proto === "pgsql") { $this->engine = new PostgreSQL(); } - else if($db_proto == "sqlite") { + else if($db_proto === "sqlite") { $this->engine = new SQLite(); } else { - die("Unknown PDO driver: $db_proto"); + die('Unknown PDO driver: '.$db_proto); } if(isset($cache_dsn) && !empty($cache_dsn)) { @@ -345,8 +345,8 @@ class Database { return $stmt; } catch(PDOException $pdoe) { - print "Message: ".$pdoe->getMessage(); - print "

Error: $query"; + print 'Message: '.$pdoe->getMessage(); + print '

Error: '.$query; exit; } } diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index 5f110f07..d07a571b 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -194,12 +194,12 @@ class Image { } if(count($tags) == 0) { - $row = $database->get_row("SELECT images.* FROM images WHERE images.id $gtlt {$this->id} ORDER BY images.id $dir LIMIT 1"); + $row = $database->get_row('SELECT images.* FROM images WHERE images.id '.$gtlt.' '.$this->id.' ORDER BY images.id '.$dir.' LIMIT 1'); } else { - $tags[] = "id$gtlt{$this->id}"; + $tags[] = 'id'. $gtlt . $this->id; $querylet = Image::build_search_querylet($tags); - $querylet->append_sql(" ORDER BY images.id $dir LIMIT 1"); + $querylet->append_sql(' ORDER BY images.id '.$dir.' LIMIT 1'); $row = $database->get_row($querylet->sql, $querylet->variables); } @@ -229,14 +229,15 @@ class Image { */ public function get_tag_array() { global $database; - $cached = $database->cache->get("image-{$this->id}-tags"); + $tmp = 'image-'.$this->id.'-tags'; + $cached = $database->cache->get($tmp); if($cached) return $cached; if(!isset($this->tag_array)) { $this->tag_array = $database->get_col("SELECT tag FROM image_tags JOIN tags ON image_tags.tag_id = tags.id WHERE image_id=:id ORDER BY tag", array("id"=>$this->id)); } - $database->cache->set("image-{$this->id}-tags", $this->tag_array); + $database->cache->set($tmp, $this->tag_array); return $this->tag_array; } diff --git a/core/util.inc.php b/core/util.inc.php index 25e020f9..27ad46f2 100644 --- a/core/util.inc.php +++ b/core/util.inc.php @@ -21,6 +21,10 @@ function html_escape($input) { * @retval int */ function int_escape($input) { + /* + Side note, Casting to an integer is FASTER than using intval. + http://hakre.wordpress.com/2010/05/13/php-casting-vs-intval/ + */ return (int)$input; } @@ -56,13 +60,13 @@ function sql_escape($input) { function bool_escape($input) { $input = strtolower($input); return ( - $input == "y" || - $input == "yes" || - $input == "t" || - $input == "true" || - $input == "on" || - $input == 1 || - $input == true + $input === "y" || + $input === "yes" || + $input === "t" || + $input === "true" || + $input === "on" || + $input === 1 || + $input === true ); } @@ -86,7 +90,7 @@ function parse_shorthand_int($limit) { return (int)$limit; } - if(preg_match('/^([\d\.]+)([gmk])?b?$/i', "$limit", $m)) { + if(preg_match('/^([\d\.]+)([gmk])?b?$/i', (string)$limit, $m)) { $value = $m[1]; if (isset($m[2])) { switch(strtolower($m[2])) { @@ -118,7 +122,7 @@ function to_shorthand_int($int) { return sprintf("%.1fKB", $int / 1024); } else { - return "$int"; + return (string)$int; } } @@ -131,7 +135,7 @@ function to_shorthand_int($int) { function autodate($date, $html=true) { $cpu = date('c', strtotime($date)); $hum = date('F j, Y', strtotime($date)); - return ($html ? "" : $hum); + return ($html ? '' : $hum); } @@ -182,17 +186,17 @@ function make_link($page=null, $query=null) { } if(is_null($query)) { - return str_replace("//", "/", "$base/$page"); + return str_replace("//", "/", $base.'/'.$page ); } else { if(strpos($base, "?")) { - return "$base/$page&$query"; + return $base .'/'. $page .'&'. $query; } else if(strpos($query, "#") === 0) { - return "$base/$page$query"; + return $base .'/'. $page . $query; } else { - return "$base/$page?$query"; + return $base .'/'. $page .'?'. $query; } } } @@ -261,14 +265,14 @@ function make_http($link) { function make_form($target, $method="POST", $multipart=False, $form_id="", $onsubmit="") { global $user; $auth = $user->get_auth_html(); - $extra = empty($form_id) ? '' : " id='$form_id'"; + $extra = empty($form_id) ? '' : 'id="'. $form_id .'"'; if($multipart) { $extra .= " enctype='multipart/form-data'"; } if($onsubmit) { - $extra .= " onsubmit='$onsubmit'"; + $extra .= ' onsubmit="'.$onsubmit.'"'; } - return "

$auth"; + return ''.$auth; } /** @@ -278,7 +282,7 @@ function make_form($target, $method="POST", $multipart=False, $form_id="", $onsu function theme_file($filepath) { global $config; $theme = $config->get_string("theme","default"); - return make_link("themes/$theme/$filepath"); + return make_link('themes/'.$theme.'/'.$filepath); } @@ -411,12 +415,12 @@ function _count_execs($db, $sql, $inputarray) { */ function get_theme_object(Extension $class, $fatal=true) { $base = get_class($class); - if(class_exists("Custom{$base}Theme")) { - $class = "Custom{$base}Theme"; + if(class_exists('Custom'.$base.'Theme')) { + $class = 'Custom'.$base.'Theme'; return new $class(); } - elseif ($fatal || class_exists("{$base}Theme")) { - $class = "{$base}Theme"; + elseif ($fatal || class_exists($base.'Theme')) { + $class = $base.'Theme'; return new $class(); } else { return false; @@ -517,14 +521,14 @@ function get_base_href() { $possible_vars = array('SCRIPT_NAME', 'PHP_SELF', 'PATH_INFO', 'ORIG_PATH_INFO'); $ok_var = null; foreach($possible_vars as $var) { - if(substr($_SERVER[$var], -4) == '.php') { + if(substr($_SERVER[$var], -4) === '.php') { $ok_var = $_SERVER[$var]; break; } } assert(!empty($ok_var)); $dir = dirname($ok_var); - if($dir == "/" || $dir == "\\") $dir = ""; + if($dir === "/" || $dir === "\\") $dir = ""; return $dir; } @@ -544,10 +548,10 @@ function warehouse_path($base, $hash, $create=true) { $ab = substr($hash, 0, 2); $cd = substr($hash, 2, 2); if(WH_SPLITS == 2) { - $pa = "$base/$ab/$cd/$hash"; + $pa = $base.'/'.$ab.'/'.$cd.'/'.$hash; } else { - $pa = "$base/$ab/$hash"; + $pa = $base.'/'.$ab.'/'.$hash; } if($create && !file_exists(dirname($pa))) mkdir(dirname($pa), 0755, true); return $pa; @@ -949,7 +953,7 @@ function _get_page_request() { global $config; $args = _get_query_parts(); - if(count($args) == 0 || strlen($args[0]) == 0) { + if( empty($args) || strlen($args[0]) === 0) { $args = explode('/', $config->get_string('front_page')); } @@ -1019,7 +1023,7 @@ function _start_cache() { $_cache_hash = md5($_SERVER["QUERY_STRING"]); $ab = substr($_cache_hash, 0, 2); $cd = substr($_cache_hash, 2, 2); - $_cache_filename = "data/$ab/$cd/$_cache_hash"; + $_cache_filename = 'data/'.$ab.'/'.$cd.'/'.$_cache_hash; if(!file_exists(dirname($_cache_filename))) { mkdir(dirname($_cache_filename), 0750, true); @@ -1038,7 +1042,7 @@ function _start_cache() { } else { header("Content-type: text/html"); - header("Last-Modified: $gmdate_mod"); + header('Last-Modified: '.$gmdate_mod); $zdata = @file_get_contents($_cache_filename); if(CACHE_MEMCACHE) { $_cache_memcache->set($_cache_hash, $zdata, 0, 600); @@ -1086,8 +1090,8 @@ function _end_coverage() { if(!file_exists("data/coverage")) mkdir("data/coverage"); $n = 0; $t = time(); - while(file_exists("data/coverage/$t.$n.log")) $n++; - file_put_contents("data/coverage/$t.$n.log", serialize(xdebug_get_code_coverage())); + while(file_exists('data/coverage/'.$t.'.'.$n.'.log')) $n++; + file_put_contents('data/coverage/'.$t.'.'.$n.'.log', serialize(xdebug_get_code_coverage())); } } ?> From 26d383198a39abbdf00d9238802e77160e42c9bb Mon Sep 17 00:00:00 2001 From: "green-ponies (jgen)" Date: Wed, 11 Jan 2012 15:57:00 -0500 Subject: [PATCH 02/13] More small changes for speed. --- core/database.class.php | 14 +++++++------- core/event.class.php | 10 +++++----- core/imageboard.pack.php | 36 +++++++++++++++++++----------------- core/page.class.php | 16 ++++++++-------- core/user.class.php | 12 ++++++------ 5 files changed, 45 insertions(+), 43 deletions(-) diff --git a/core/database.class.php b/core/database.class.php index 6e8a0213..bd97af19 100644 --- a/core/database.class.php +++ b/core/database.class.php @@ -56,7 +56,7 @@ class DBEngine { } public function create_table_sql($name, $data) { - return "CREATE TABLE $name ($data)"; + return 'CREATE TABLE '.$name.' ('.$data.')'; } } class MySQL extends DBEngine { @@ -82,7 +82,7 @@ class MySQL extends DBEngine { public function create_table_sql($name, $data) { $data = $this->scoreql_to_sql($data); $ctes = "ENGINE=InnoDB DEFAULT CHARSET='utf8'"; - return "CREATE TABLE $name ($data) $ctes"; + return 'CREATE TABLE '.$name.' ('.$data.') '.$ctes; } } class PostgreSQL extends DBEngine { @@ -103,7 +103,7 @@ class PostgreSQL extends DBEngine { public function create_table_sql($name, $data) { $data = $this->scoreql_to_sql($data); - return "CREATE TABLE $name ($data)"; + return 'CREATE TABLE '.$name.' ('.$data.')'; } } @@ -151,14 +151,14 @@ class SQLite extends DBEngine { $matches = array(); if(preg_match("/INDEX\s*\((.*)\)/", $bit, $matches)) { $col = $matches[1]; - $extras .= "CREATE INDEX {$name}_{$col} on $name($col);"; + $extras .= 'CREATE INDEX '.$name.'_'.$col.' on '.$name($col).';'; } else { $cols[] = $bit; } } $cols_redone = implode(", ", $cols); - return "CREATE TABLE $name ($cols_redone); $extras"; + return 'CREATE TABLE '.$name.' ('.$cols_redone.'); '.$extras; } } // }}} @@ -331,10 +331,10 @@ class Database { if (!array_key_exists(0, $args)) { foreach($args as $name=>$value) { if(is_numeric($value)) { - $stmt->bindValue(":$name", $value, PDO::PARAM_INT); + $stmt->bindValue(':'.$name, $value, PDO::PARAM_INT); } else { - $stmt->bindValue(":$name", $value, PDO::PARAM_STR); + $stmt->bindValue(':'.$name, $value, PDO::PARAM_STR); } } $stmt->execute(); diff --git a/core/event.class.php b/core/event.class.php index 14043de4..69a93175 100644 --- a/core/event.class.php +++ b/core/event.class.php @@ -68,7 +68,7 @@ class PageRequestEvent extends Event { } public function count_args() { - return $this->arg_count - $this->part_count; + return (int)($this->arg_count - $this->part_count); } /* @@ -76,20 +76,20 @@ class PageRequestEvent extends Event { */ public function get_search_terms() { $search_terms = array(); - if($this->count_args() == 2) { + if($this->count_args() === 2) { $search_terms = explode(' ', $this->get_arg(0)); } return $search_terms; } public function get_page_number() { $page_number = 1; - if($this->count_args() == 1) { + if($this->count_args() === 1) { $page_number = int_escape($this->get_arg(0)); } - else if($this->count_args() == 2) { + else if($this->count_args() === 2) { $page_number = int_escape($this->get_arg(1)); } - if($page_number == 0) $page_number = 1; // invalid -> 0 + if($page_number === 0) $page_number = 1; // invalid -> 0 return $page_number; } public function get_page_size() { diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index d07a571b..81ac9674 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -514,7 +514,7 @@ class Image { private static function build_search_querylet($terms) { assert(is_array($terms)); global $database; - if($database->engine->name == "mysql") + if($database->engine->name === "mysql") return Image::build_ugly_search_querylet($terms); else return Image::build_accurate_search_querylet($terms); @@ -750,11 +750,13 @@ class Image { foreach($tag_querylets as $tq) { global $tag_n; $sign = $tq->positive ? "+" : "-"; - $sql .= " $sign (tag LIKE :tag$tag_n)"; - $terms["tag$tag_n"] = $tq->tag; + //$sql .= " $sign (tag LIKE :tag$tag_n)"; + $sql .= ' '.$sign.' (tag LIKE :tag'.$tag_n.')'; + //$terms["tag$tag_n"] = $tq->tag; + $terms['tag'.$tag_n] = $tq->tag; $tag_n++; - if($sign == "+") $positive_tag_count++; + if($sign === "+") $positive_tag_count++; else $negative_tag_count++; } $tag_search = new Querylet($sql, $terms); @@ -783,7 +785,7 @@ class Image { } // one positive tag (a common case), do an optimised search - else if($positive_tag_count == 1 && $negative_tag_count == 0) { + else if($positive_tag_count === 1 && $negative_tag_count === 0) { $query = new Querylet( // MySQL is braindead, and does a full table scan on images, running the subquery once for each row -_- // "{$this->get_images} WHERE images.id IN (SELECT image_id FROM tags WHERE tag LIKE ?) ", @@ -819,22 +821,22 @@ class Image { if($tags_ok) { $tag_id_list = join(', ', $tag_id_array); - $subquery = new Querylet(" - SELECT images.*, SUM({$tag_search->sql}) AS score + $subquery = new Querylet(' + SELECT images.*, SUM('.$tag_search->sql.') AS score FROM images LEFT JOIN image_tags ON image_tags.image_id = images.id JOIN tags ON image_tags.tag_id = tags.id - WHERE tags.id IN ({$tag_id_list}) + WHERE tags.id IN ('.$tag_id_list.') GROUP BY images.id - HAVING score = :score", + HAVING score = :score', array_merge( $tag_search->variables, array("score"=>$positive_tag_count) ) ); - $query = new Querylet(" + $query = new Querylet(' SELECT *, UNIX_TIMESTAMP(posted) AS posted_timestamp - FROM ({$subquery->sql}) AS images ", $subquery->variables); + FROM ('.$subquery->sql.') AS images ', $subquery->variables); if(strlen($img_search->sql) > 0) { $query->append_sql(" WHERE "); @@ -882,9 +884,9 @@ class Tag { if(is_string($tags)) { $tags = explode(' ', $tags); } - else if(is_array($tags)) { + //else if(is_array($tags)) { // do nothing - } + //} $tags = array_map("trim", $tags); @@ -907,13 +909,13 @@ class Tag { public static function implode($tags) { assert(is_string($tags) || is_array($tags)); - if(is_string($tags)) { - // do nothing - } - else if(is_array($tags)) { + if(is_array($tags)) { sort($tags); $tags = implode(' ', $tags); } + //else if(is_string($tags)) { + // do nothing + //} return $tags; } diff --git a/core/page.class.php b/core/page.class.php index d382caf2..398fb10d 100644 --- a/core/page.class.php +++ b/core/page.class.php @@ -242,22 +242,22 @@ class Page { // caching failed, add all files to html_headers. foreach(glob("lib/*.css") as $css) { - $this->add_html_header(""); + $this->add_html_header(''); } $css_files = glob("ext/*/style.css"); if($css_files) { foreach($css_files as $css_file) { - $this->add_html_header(""); + $this->add_html_header(''); } } foreach(glob("lib/*.js") as $js) { - $this->add_html_header(""); + $this->add_html_header(''); } $js_files = glob("ext/*/script.js"); if($js_files) { foreach($js_files as $js_file) { - $this->add_html_header(""); + $this->add_html_header(''); } } } @@ -362,12 +362,12 @@ class Page { } else { // Caching of CSS disabled. foreach(glob("lib/*.css") as $css) { - $this->add_html_header(""); + $this->add_html_header(''); } $css_files = glob("ext/*/style.css"); if($css_files) { foreach($css_files as $css_file) { - $this->add_html_header(""); + $this->add_html_header(''); } } } @@ -412,12 +412,12 @@ class Page { } else { // Caching of Javascript disabled. foreach(glob("lib/*.js") as $js) { - $this->add_html_header(""); + $this->add_html_header(''); } $js_files = glob("ext/*/script.js"); if($js_files) { foreach($js_files as $js_file) { - $this->add_html_header(""); + $this->add_html_header(''); } } } diff --git a/core/user.class.php b/core/user.class.php index 7352122b..b5779020 100644 --- a/core/user.class.php +++ b/core/user.class.php @@ -40,7 +40,7 @@ class User { public static function by_session($name, $session) { global $config, $database; - if($database->engine->name == "mysql") { + if($database->engine->name === "mysql") { $query = "SELECT * FROM users WHERE name = :name AND md5(concat(pass, :ip)) = :sess"; } else { @@ -53,12 +53,12 @@ class User { public static function by_id($id) { assert(is_numeric($id)); global $database; - if($id == 1) { - $cached = $database->cache->get("user-id:$id"); + if($id === 1) { + $cached = $database->cache->get('user-id:'.$id); if($cached) return new User($cached); } $row = $database->get_row("SELECT * FROM users WHERE id = :id", array("id"=>$id)); - if($id == 1) $database->cache->set("user-id:$id", $row, 300); + if($id === 1) $database->cache->set('user-id:'.$id, $row, 300); return is_null($row) ? null : new User($row); } @@ -148,7 +148,7 @@ class User { public function get_avatar_html() { // FIXME: configurable global $config; - if($config->get_string("avatar_host") == "gravatar") { + if($config->get_string("avatar_host") === "gravatar") { if(!empty($this->email)) { $hash = md5(strtolower($this->email)); $s = $config->get_string("avatar_gravatar_size"); @@ -180,7 +180,7 @@ class User { public function get_auth_html() { $at = $this->get_auth_token(); - return ""; + return ''; } public function check_auth_token() { From 2e0e8475a1aee3ea2a1f58a74c38ce75704bd822 Mon Sep 17 00:00:00 2001 From: "green-ponies (jgen)" Date: Thu, 12 Jan 2012 14:46:58 -0500 Subject: [PATCH 03/13] A few more small changes for speed. --- core/imageboard.pack.php | 14 +++++++------- core/user.class.php | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index 81ac9674..9628b318 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -342,8 +342,8 @@ class Image { */ public function get_mime_type() { $type = strtolower($this->ext); - if($type == "jpg") $type = "jpeg"; - return "image/$type"; + if($type === "jpg") $type = "jpeg"; + return 'image/'.$type; } /** @@ -380,7 +380,7 @@ class Image { public function set_locked($tf) { global $database; $ln = $tf ? "Y" : "N"; - $sln = $database->engine->scoreql_to_sql("SCORE_BOOL_$ln"); + $sln = $database->engine->scoreql_to_sql('SCORE_BOOL_'.$ln); $sln = str_replace("'", "", $sln); $sln = str_replace('"', "", $sln); $database->execute("UPDATE images SET locked=:yn WHERE id=:id", array("yn"=>$sln, "id"=>$this->id)); @@ -442,8 +442,8 @@ class Image { array("tag"=>$tag)); } - log_info("core-image", "Tags for Image #{$this->id} set to: ".implode(" ", $tags)); - $database->cache->delete("image-{$this->id}-tags"); + log_info("core-image", 'Tags for Image #'.$this->id.' set to: '.implode(" ", $tags)); + $database->cache->delete('image-'.$this->id.'-tags'); } /** @@ -453,7 +453,7 @@ class Image { global $database; $this->delete_tags_from_image(); $database->execute("DELETE FROM images WHERE id=:id", array("id"=>$this->id)); - log_info("core-image", "Deleted Image #{$this->id} ({$this->hash})"); + log_info("core-image", 'Deleted Image #'.$this->id.' ('.$this->hash.')'); unlink($this->get_image_filename()); unlink($this->get_thumb_filename()); @@ -464,7 +464,7 @@ class Image { * It DOES NOT remove anything from the database. */ public function remove_image_only() { - log_info("core-image", "Removed Image File ({$this->hash})"); + log_info("core-image", 'Removed Image File ('.$this->hash.')'); @unlink($this->get_image_filename()); @unlink($this->get_thumb_filename()); } diff --git a/core/user.class.php b/core/user.class.php index b5779020..79b8483c 100644 --- a/core/user.class.php +++ b/core/user.class.php @@ -98,7 +98,7 @@ class User { */ public function is_anonymous() { global $config; - return ($this->id == $config->get_int('anon_id')); + return ($this->id === $config->get_int('anon_id')); } /** @@ -108,7 +108,7 @@ class User { */ public function is_logged_in() { global $config; - return ($this->id != $config->get_int('anon_id')); + return ($this->id !== $config->get_int('anon_id')); } /** @@ -125,20 +125,20 @@ class User { global $database; $yn = $admin ? 'Y' : 'N'; $database->Execute("UPDATE users SET admin=:yn WHERE id=:id", array("yn"=>$yn, "id"=>$this->id)); - log_info("core-user", "Made {$this->name} admin=$yn"); + log_info("core-user", 'Made '.$this->name.' admin='.$yn); } public function set_password($password) { global $database; $hash = md5(strtolower($this->name) . $password); $database->Execute("UPDATE users SET pass=:hash WHERE id=:id", array("hash"=>$hash, "id"=>$this->id)); - log_info("core-user", "Set password for {$this->name}"); + log_info("core-user", 'Set password for '.$this->name); } public function set_email($address) { global $database; $database->Execute("UPDATE users SET email=:email WHERE id=:id", array("email"=>$address, "id"=>$this->id)); - log_info("core-user", "Set email for {$this->name}"); + log_info("core-user", 'Set email for '.$this->name); } /** From ea6f853891d19f3a539ebe5ace2eeb5d35e7c0cb Mon Sep 17 00:00:00 2001 From: "green-ponies (jgen)" Date: Thu, 12 Jan 2012 14:47:24 -0500 Subject: [PATCH 04/13] Changes to the index file as well. --- index.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.php b/index.php index 41e28b47..fb939ed1 100644 --- a/index.php +++ b/index.php @@ -107,22 +107,22 @@ try { ctx_log_start("Loading themelets"); // load the theme parts $_theme = $config->get_string("theme", "default"); - if(!file_exists("themes/$_theme")) $_theme = "default"; - if(file_exists("themes/$_theme/custompage.class.php")) require_once "themes/$_theme/custompage.class.php"; - require_once "themes/$_theme/layout.class.php"; - require_once "themes/$_theme/themelet.class.php"; + if(!file_exists('themes/'.$_theme)) $_theme = "default"; + if(file_exists('themes/'.$_theme.'/custompage.class.php')) require_once 'themes/'.$_theme.'/custompage.class.php'; + require_once 'themes/'.$_theme.'/layout.class.php'; + require_once 'themes/'.$_theme.'/themelet.class.php'; $themelets = glob("ext/*/theme.php"); foreach($themelets as $filename) { require_once $filename; } - $custom_themelets = glob("themes/$_theme/*.theme.php"); + $custom_themelets = glob('themes/'.$_theme.'/*.theme.php'); if($custom_themelets) { $m = array(); foreach($custom_themelets as $filename) { - if(preg_match("/themes\/$_theme\/(.*)\.theme\.php/",$filename,$m) - && in_array("ext/{$m[1]}/theme.php", $themelets)) { + if(preg_match('/themes\/'.$_theme.'\/(.*)\.theme\.php/',$filename,$m) + && in_array('ext/'.$m[1].'/theme.php', $themelets)) { require_once $filename; } } From c739e5b2e84fe835bfff7f638d36410ba51fe813 Mon Sep 17 00:00:00 2001 From: "green-ponies (jgen)" Date: Thu, 12 Jan 2012 15:06:32 -0500 Subject: [PATCH 05/13] Silly typo. --- core/page.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/page.class.php b/core/page.class.php index 398fb10d..e813285b 100644 --- a/core/page.class.php +++ b/core/page.class.php @@ -417,7 +417,7 @@ class Page { $js_files = glob("ext/*/script.js"); if($js_files) { foreach($js_files as $js_file) { - $this->add_html_header(''); + $this->add_html_header(''); } } } From d7ff1b96abb258d3896fb64090bdfbb0ed137c03 Mon Sep 17 00:00:00 2001 From: "green-ponies (jgen)" Date: Thu, 12 Jan 2012 15:13:38 -0500 Subject: [PATCH 06/13] More stupid typos. Gah. --- core/page.class.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/page.class.php b/core/page.class.php index e813285b..e6be9c33 100644 --- a/core/page.class.php +++ b/core/page.class.php @@ -252,12 +252,12 @@ class Page { } foreach(glob("lib/*.js") as $js) { - $this->add_html_header(''); + $this->add_html_header(''); } $js_files = glob("ext/*/script.js"); if($js_files) { foreach($js_files as $js_file) { - $this->add_html_header(''); + $this->add_html_header(''); } } } @@ -358,7 +358,7 @@ class Page { } } // tell the client where to get the css cache file - $this->add_html_header(''); + $this->add_html_header(''); } else { // Caching of CSS disabled. foreach(glob("lib/*.css") as $css) { @@ -408,11 +408,11 @@ class Page { } } // tell the client where to get the js cache file - $this->add_html_header(''); + $this->add_html_header(''); } else { // Caching of Javascript disabled. foreach(glob("lib/*.js") as $js) { - $this->add_html_header(''); + $this->add_html_header(''); } $js_files = glob("ext/*/script.js"); if($js_files) { From fc12bbbfe524cd6808bc3a841b0d64b9f1c7d91f Mon Sep 17 00:00:00 2001 From: "green-ponies (jgen)" Date: Thu, 12 Jan 2012 15:46:34 -0500 Subject: [PATCH 07/13] More small changes to help save a few microseconds. --- core/page.class.php | 4 +-- core/util.inc.php | 2 +- ext/comment/theme.php | 70 +++++++++++++++++++++---------------------- ext/user/theme.php | 66 ++++++++++++++++++++-------------------- 4 files changed, 71 insertions(+), 71 deletions(-) diff --git a/core/page.class.php b/core/page.class.php index e6be9c33..514a1cfd 100644 --- a/core/page.class.php +++ b/core/page.class.php @@ -225,8 +225,8 @@ class Page { print $this->data; break; case "redirect": - header("Location: {$this->redirect}"); - print "You should be redirected to {$this->redirect}"; + header('Location: '.$this->redirect); + print 'You should be redirected to '.$this->redirect.''; break; default: print "Invalid page mode"; diff --git a/core/util.inc.php b/core/util.inc.php index 27ad46f2..b8883bb5 100644 --- a/core/util.inc.php +++ b/core/util.inc.php @@ -384,7 +384,7 @@ function _count_execs($db, $sql, $inputarray) { if(DEBUG) { $fp = @fopen("data/sql.log", "a"); if($fp) { - if(is_array($inputarray)) { + if(isset($inputarray) && is_array($inputarray)) { fwrite($fp, preg_replace('/\s+/msi', ' ', $sql)." -- ".join(", ", $inputarray)."\n"); } else { diff --git a/ext/comment/theme.php b/ext/comment/theme.php index 3e2f9138..93936d8b 100644 --- a/ext/comment/theme.php +++ b/ext/comment/theme.php @@ -21,12 +21,12 @@ class CommentListTheme extends Themelet { $next = $page_number + 1; $h_prev = ($page_number <= 1) ? "Prev" : - "Prev"; + 'Prev'; $h_index = "Index"; $h_next = ($page_number >= $total_pages) ? "Next" : - "Next"; + 'Next'; - $nav = "$h_prev | $h_index | $h_next"; + $nav = $h_prev.' | '.$h_index.' | '.$h_next; $page->set_title("Comments"); $page->set_heading("Comments"); @@ -46,7 +46,7 @@ class CommentListTheme extends Themelet { $comment_count = count($comments); if($comment_limit > 0 && $comment_count > $comment_limit) { $hidden = $comment_count - $comment_limit; - $comment_html .= "

showing $comment_limit of $comment_count comments

"; + $comment_html .= '

showing '.$comment_limit.' of '.$comment_count.' comments

'; $comments = array_slice($comments, -$comment_limit); } $this->anon_id = 1; @@ -68,14 +68,14 @@ class CommentListTheme extends Themelet { } } - $html = " - - - + $html = ' +
$thumb_html$comment_html
+ +
'.$thumb_html.''.$comment_html.'
- "; + '; - $page->add_block(new Block("{$image->id}: ".($image->get_tag_list()), $html, "main", $position++)); + $page->add_block(new Block( $image->id.': '.$image->get_tag_list(), $html, "main", $position++)); } } @@ -154,23 +154,23 @@ class CommentListTheme extends Themelet { $anoncode = ""; if($h_name == "Anonymous" && $this->anon_id >= 0) { - $anoncode = "{$this->anon_id}"; + $anoncode = ''.$this->anon_id.''; $this->anon_id++; } - $h_userlink = "$h_name$anoncode"; + $h_userlink = ''.$h_name.''.$anoncode; $stripped_nonl = str_replace("\n", "\\n", substr($tfe->stripped, 0, 50)); $stripped_nonl = str_replace("\r", "\\r", $stripped_nonl); $h_dellink = $user->is_admin() ? - "
($h_poster_ip, $h_timestamp, Del)" : ""; + '
('.$h_poster_ip.', '.$h_timestamp.', Del)' : ''; if($trim) { - return " - $h_userlink: $h_comment - >>> - $h_dellink - "; + return ' + '.$h_userlink.': '.$h_comment.' + >>> + '.$h_dellink.' + '; } else { //$avatar = ""; @@ -179,14 +179,14 @@ class CommentListTheme extends Themelet { // $avatar = "
"; //} $oe = ($this->comments_shown++ % 2 == 0) ? "even" : "odd"; - return " - -
- - $h_userlink: $h_comment - $h_dellink + return ' + +
+ + '.$h_userlink.': '.$h_comment.' + '.$h_dellink.'
- "; + '; } } @@ -197,15 +197,15 @@ class CommentListTheme extends Themelet { $hash = CommentList::get_hash(); $captcha = $config->get_bool("comment_captcha") ? captcha_get_html() : ""; - return " - ".make_form(make_link("comment/add"))." - - - - $captcha -
+ return ' + '.make_form(make_link("comment/add")).' + + + + '.$captcha.' +
- "; + '; } } ?> diff --git a/ext/user/theme.php b/ext/user/theme.php index 5f1491e1..3e0fe990 100644 --- a/ext/user/theme.php +++ b/ext/user/theme.php @@ -30,9 +30,9 @@ class UserPageTheme extends Themelet { public function display_user_block(Page $page, User $user, $parts) { $h_name = html_escape($user->name); - $html = "Logged in as $h_name"; + $html = 'Logged in as '.$h_name; foreach($parts as $part) { - $html .= "
{$part["name"]}"; + $html .= '
'.$part["name"].''; } $page->add_block(new Block("User Links", $html, "left", 90)); } @@ -48,12 +48,12 @@ class UserPageTheme extends Themelet { } if(empty($tac)) {$html = "";} - else {$html = "

$tac

";} + else {$html = '

'.$tac.'

';} $reca = "".captcha_get_html().""; - $html .= " - ".make_form(make_link("user_admin/create"))." + $html .= ' + '.make_form(make_link("user_admin/create"))." @@ -81,8 +81,8 @@ class UserPageTheme extends Themelet { public function display_login_block(Page $page) { global $config; - $html = " - ".make_form(make_link("user_admin/login"))." + $html = ' + '.make_form(make_link("user_admin/login"))."
Name
Password
@@ -107,7 +107,7 @@ class UserPageTheme extends Themelet { $html .= "
Uploaded from: "; $n = 0; foreach($uploads as $ip => $count) { - $html .= "
$ip ($count)"; + $html .= '
'.$ip.' ('.$count.')'; if(++$n >= 20) { $html .= "
..."; break; @@ -117,7 +117,7 @@ class UserPageTheme extends Themelet { $html .= "
Commented from:"; $n = 0; foreach($comments as $ip => $count) { - $html .= "
$ip ($count)"; + $html .= '
'.$ip.' ('.$count.')'; if(++$n >= 20) { $html .= "
..."; break; @@ -133,10 +133,10 @@ class UserPageTheme extends Themelet { public function display_user_page(User $duser, $stats) { global $page, $user; assert(is_array($stats)); - $stats[] = "User ID: {$duser->id}"; + $stats[] = 'User ID: '.$duser->id; - $page->set_title("{$duser->name}'s Page"); - $page->set_heading("{$duser->name}'s Page"); + $page->set_title($duser->name."'s Page"); + $page->set_heading($duser->name."'s Page"); $page->add_block(new NavBlock()); $page->add_block(new Block("Stats", join("
", $stats), "main", 0)); @@ -150,37 +150,37 @@ class UserPageTheme extends Themelet { protected function build_options(User $duser) { global $config, $database, $user; - $html = " - ".make_form(make_link("user_admin/change_pass"))." - - - - - - + $html = ' + '.make_form(make_link("user_admin/change_pass")).' + +
Change Password
Password
Repeat Password
+ + + +
Change Password
Password
Repeat Password
-

".make_form(make_link("user_admin/change_email"))." - - - - - +

'.make_form(make_link("user_admin/change_email")).' + +

Change Email
Address
+ + +
Change Email
Address
- "; + '; if($user->is_admin()) { $i_user_id = int_escape($duser->id); $h_is_admin = $duser->is_admin() ? " checked" : ""; - $html .= " -

".make_form(make_link("user_admin/set_more"))." - - Admin: - + $html .= ' +

'.make_form(make_link("user_admin/set_more")).' + + Admin: + - "; + '; } return $html; } From aa9c8c2097b9e3231ae9a4371b389a20850a734e Mon Sep 17 00:00:00 2001 From: "green-ponies (jgen)" Date: Thu, 12 Jan 2012 20:28:16 -0500 Subject: [PATCH 08/13] Small change to index. --- index.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.php b/index.php index fb939ed1..9de7fb73 100644 --- a/index.php +++ b/index.php @@ -159,17 +159,17 @@ catch(Exception $e) { $message = $e->getMessage(); //$trace = var_dump($e->getTrace()); header("HTTP/1.0 500 Internal Error"); - print << - Internal error - SCore-$version + Internal error - SCore-'.$version.'

Internal Error

-

$message +

'.$message.' -EOD; +'; $database->db->rollback(); ctx_log_ender(); } From 3f7646bc8bbb5f4f68eed24184e03596189374a4 Mon Sep 17 00:00:00 2001 From: "green-ponies (jgen)" Date: Thu, 12 Jan 2012 20:30:26 -0500 Subject: [PATCH 09/13] typo --- core/page.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/page.class.php b/core/page.class.php index 514a1cfd..2718fcd0 100644 --- a/core/page.class.php +++ b/core/page.class.php @@ -226,7 +226,7 @@ class Page { break; case "redirect": header('Location: '.$this->redirect); - print 'You should be redirected to '.$this->redirect.''; + print 'You should be redirected to '.$this->redirect.''; break; default: print "Invalid page mode"; From 2c6b5128c67317cbdb4d503fec7e52a711c572d9 Mon Sep 17 00:00:00 2001 From: "green-ponies (jgen)" Date: Thu, 12 Jan 2012 21:17:37 -0500 Subject: [PATCH 10/13] More tweaks for minor speed gains. --- core/imageboard.pack.php | 7 +++++-- ext/index/theme.php | 8 ++++---- themes/default/themelet.class.php | 32 +++++++++++++++---------------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index 9628b318..fcd23954 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -284,8 +284,11 @@ class Image { */ public function get_thumb_link() { global $config; - if(strlen($config->get_string('image_tlink')) > 0) { - return $this->parse_link_template($config->get_string('image_tlink')); + + $image_tlink = $config->get_string('image_tlink'); // store a copy for speed. + + if( !empty($image_tlink) ) { /* empty is faster than strlen */ + return $this->parse_link_template($image_tlink); } else if($config->get_bool('nice_urls', false)) { return $this->parse_link_template(make_link('_thumbs/$hash/thumb.jpg')); diff --git a/ext/index/theme.php b/ext/index/theme.php index 56775736..2263d3db 100644 --- a/ext/index/theme.php +++ b/ext/index/theme.php @@ -69,12 +69,12 @@ EOD; $next = $page_number + 1; $u_tags = url_escape(implode(" ", $search_terms)); - $query = empty($u_tags) ? "" : "/$u_tags"; + $query = empty($u_tags) ? "" : '/'.$u_tags; - $h_prev = ($page_number <= 1) ? "Prev" : "Prev"; + $h_prev = ($page_number <= 1) ? "Prev" : 'Prev'; $h_index = "Index"; - $h_next = ($page_number >= $total_pages) ? "Next" : "Next"; + $h_next = ($page_number >= $total_pages) ? "Next" : 'Next'; $h_search_string = html_escape(implode(" ", $search_terms)); $h_search_link = make_link(); @@ -102,7 +102,7 @@ EOD;

"; - return "$h_prev | $h_index | $h_next
$h_search"; + return $h_prev.' | '.$h_index.' | '.$h_next.'
'.$h_search; } protected function build_table($images, $query) { diff --git a/themes/default/themelet.class.php b/themes/default/themelet.class.php index 08b4b694..27a6a280 100644 --- a/themes/default/themelet.class.php +++ b/themes/default/themelet.class.php @@ -29,13 +29,13 @@ class Themelet { */ public function build_thumb_html(Image $image, $query=null) { global $config; - $i_id = int_escape($image->id); - $h_view_link = make_link("post/view/$i_id", $query); + $i_id = (int) $image->id; + $h_view_link = make_link('post/view/'.$i_id, $query); $h_thumb_link = $image->get_thumb_link(); // Removes the size tag if the file is an mp3 - if($image->ext == 'mp3'){ + if($image->ext === 'mp3'){ $iitip = $image->get_tooltip(); $mp3tip = array("0x0"); $h_tip = str_replace($mp3tip, " ", $iitip); @@ -45,29 +45,29 @@ class Themelet { if(strstr($h_tip, " ")){ $h_tip = html_escape(str_replace($justincase, "", $h_tip)); }else{ - $h_tip = html_escape($h_tip); + $h_tip = html_escape($h_tip); } }else{ - $h_tip = html_escape($image->get_tooltip()); + $h_tip = html_escape($image->get_tooltip()); } // If file is flash or svg then sets thumbnail to max size. - if($image->ext == 'swf' || $image->ext == 'svg'){ + if($image->ext === 'swf' || $image->ext === 'svg'){ $tsize = get_thumbnail_size($config->get_int('thumb_width'), $config->get_int('thumb_height')); } else{ $tsize = get_thumbnail_size($image->width, $image->height); } - return " + return ' -
-
- - $h_tip + - "; + '; } @@ -81,8 +81,8 @@ class Themelet { } private function gen_page_link($base_url, $query, $page, $name) { - $link = make_link("$base_url/$page", $query); - return "$name"; + $link = make_link($base_url.'/'.$page, $query); + return ''.$name.''; } private function gen_page_link_block($base_url, $query, $page, $current_page, $name) { @@ -116,8 +116,8 @@ class Themelet { } $pages_html = implode(" | ", $pages); - return "

$first_html | $prev_html | $random_html | $next_html | $last_html". - "
<< $pages_html >>

"; + return '

'.$first_html.' | '.$prev_html.' | '.$random_html.' | '.$next_html.' | '.$last_html + .'
<< '.$pages_html.' >>

'; } } ?> From 7195d3d1f3fd418b21ca19f7b4a84335cb9205d7 Mon Sep 17 00:00:00 2001 From: "green-ponies (jgen)" Date: Thu, 12 Jan 2012 23:07:14 -0500 Subject: [PATCH 11/13] Changes using empty instead of strlen. --- core/imageboard.pack.php | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index fcd23954..c6f8048c 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -255,8 +255,11 @@ class Image { */ public function get_image_link() { global $config; - if(strlen($config->get_string('image_ilink')) > 0) { - return $this->parse_link_template($config->get_string('image_ilink')); + + $image_ilink = $config->get_string('image_ilink'); // store a copy for speed. + + if( !empty($image_ilink) ) { /* empty is faster than strlen */ + return $this->parse_link_template($image_ilink); } else if($config->get_bool('nice_urls', false)) { return $this->parse_link_template(make_link('_images/$hash/$id - $tags.$ext')); @@ -560,7 +563,7 @@ class Image { // various types of querylet foreach($terms as $term) { $positive = true; - if((strlen($term) > 0) && ($term[0] == '-')) { + if( is_string($term) && !empty($term) && ($term[0] == '-')) { $positive = false; $term = substr($term, 1); } @@ -605,7 +608,7 @@ class Image { if(count($tag_querylets) == 0) { $query = new Querylet("SELECT images.* FROM images "); - if(strlen($img_search->sql) > 0) { + if(!empty($img_search->sql)) { $query->append_sql(" WHERE "); $query->append($img_search); } @@ -622,7 +625,7 @@ class Image { ) "), array("tag"=>$tag_querylets[0]->tag)); - if(strlen($img_search->sql) > 0) { + if(!empty($img_search->sql)) { $query->append_sql(" AND "); $query->append($img_search); } @@ -724,7 +727,7 @@ class Image { // turn each term into a specific type of querylet foreach($terms as $term) { $negative = false; - if((strlen($term) > 0) && ($term[0] == '-')) { + if( !empty($term) && ($term[0] == '-')) { $negative = true; $term = substr($term, 1); } @@ -781,7 +784,7 @@ class Image { if($positive_tag_count + $negative_tag_count == 0) { $query = new Querylet("SELECT images.*,UNIX_TIMESTAMP(posted) AS posted_timestamp FROM images "); - if(strlen($img_search->sql) > 0) { + if(!empty($img_search->sql)) { $query->append_sql(" WHERE "); $query->append($img_search); } @@ -802,7 +805,7 @@ class Image { ", $tag_search->variables); - if(strlen($img_search->sql) > 0) { + if(!empty($img_search->sql)) { $query->append_sql(" AND "); $query->append($img_search); } @@ -841,7 +844,7 @@ class Image { SELECT *, UNIX_TIMESTAMP(posted) AS posted_timestamp FROM ('.$subquery->sql.') AS images ', $subquery->variables); - if(strlen($img_search->sql) > 0) { + if(!empty($img_search->sql)) { $query->append_sql(" WHERE "); $query->append($img_search); } @@ -895,7 +898,7 @@ class Tag { $tag_array = array(); foreach($tags as $tag) { - if(is_string($tag) && strlen($tag) > 0) { + if(is_string($tag) && !empty($tag)) { $tag_array[] = $tag; } } From 8252534cff10c3f8fc03733263487b606a12dba1 Mon Sep 17 00:00:00 2001 From: "green-ponies (jgen)" Date: Sun, 15 Jan 2012 23:03:27 -0500 Subject: [PATCH 12/13] Small changes with quotes. --- ext/index/main.php | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/ext/index/main.php b/ext/index/main.php index 5bf46d98..5e2dc5cd 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -147,7 +147,7 @@ class Index extends SimpleExtension { } else { $page->set_mode("redirect"); - $page->set_redirect(make_link("post/list/$search/1")); + $page->set_redirect(make_link('post/list/'.$search.'/1')); } return; } @@ -171,7 +171,7 @@ class Index extends SimpleExtension { } else if(count($search_terms) > 0 && count($images) == 1 && $page_number == 1) { $page->set_mode("redirect"); - $page->set_redirect(make_link("post/view/{$images[0]->id}")); + $page->set_redirect(make_link('post/view/'.$images[0]->id)); } else { send_event(new PostListBuildingEvent($search_terms)); @@ -197,15 +197,16 @@ class Index extends SimpleExtension { public function onSearchTermParse($event) { $matches = array(); - if(preg_match("/^size(<|>|<=|>=|=)(\d+)x(\d+)$/", $event->term, $matches)) { + // check for tags first as tag based searches are more common. + if(preg_match("/tags(<|>|<=|>=|=)(\d+)/", $event->term, $matches)) { $cmp = $matches[1]; - $args = array("width"=>int_escape($matches[2]), "height"=>int_escape($matches[3])); - $event->add_querylet(new Querylet("width $cmp :width AND height $cmp :height", $args)); + $tags = $matches[2]; + $event->add_querylet(new Querylet('images.id IN (SELECT DISTINCT image_id FROM image_tags GROUP BY image_id HAVING count(image_id) '.$cmp.' '.$tags.')')); } else if(preg_match("/^ratio(<|>|<=|>=|=)(\d+):(\d+)$/", $event->term, $matches)) { $cmp = $matches[1]; $args = array("width"=>int_escape($matches[2]), "height"=>int_escape($matches[3])); - $event->add_querylet(new Querylet("width / height $cmp :width / :height", $args)); + $event->add_querylet(new Querylet('width / height '.$cmp.' :width / :height', $args)); } else if(preg_match("/^(filesize|id)(<|>|<=|>=|=)(\d+[kmg]?b?)$/i", $event->term, $matches)) { $col = $matches[1]; @@ -215,24 +216,24 @@ class Index extends SimpleExtension { } else if(preg_match("/^(hash|md5)=([0-9a-fA-F]*)$/i", $event->term, $matches)) { $hash = strtolower($matches[2]); - $event->add_querylet(new Querylet("images.hash = '$hash'")); + $event->add_querylet(new Querylet('images.hash = "'.$hash.'"')); } else if(preg_match("/^(filetype|ext)=([a-zA-Z0-9]*)$/i", $event->term, $matches)) { $ext = strtolower($matches[2]); - $event->add_querylet(new Querylet("images.ext = '$ext'")); + $event->add_querylet(new Querylet('images.ext = "'.$ext.'"')); } else if(preg_match("/^(filename|name)=([a-zA-Z0-9]*)$/i", $event->term, $matches)) { $filename = strtolower($matches[2]); - $event->add_querylet(new Querylet("images.filename LIKE '%$filename%'")); + $event->add_querylet(new Querylet('images.filename LIKE "%'.$filename.'%"')); } else if(preg_match("/^posted=(([0-9\*]*)?(-[0-9\*]*)?(-[0-9\*]*)?)$/", $event->term, $matches)) { $val = str_replace("*", "%", $matches[1]); - $event->add_querylet(new Querylet("images.posted LIKE '%$val%'")); + $event->add_querylet(new Querylet('images.posted LIKE "%'.$val.'%"')); } - else if(preg_match("/tags(<|>|<=|>=|=)(\d+)/", $event->term, $matches)) { + else if(preg_match("/^size(<|>|<=|>=|=)(\d+)x(\d+)$/", $event->term, $matches)) { $cmp = $matches[1]; - $tags = $matches[2]; - $event->add_querylet(new Querylet("images.id IN (SELECT DISTINCT image_id FROM image_tags GROUP BY image_id HAVING count(image_id) $cmp $tags)")); + $args = array("width"=>int_escape($matches[2]), "height"=>int_escape($matches[3])); + $event->add_querylet(new Querylet('width '.$cmp.' :width AND height '.$cmp.' :height', $args)); } } } From cd7de93a0aa040f377aa4bd515cb9335ecfca85b Mon Sep 17 00:00:00 2001 From: "green-ponies (jgen)" Date: Mon, 16 Jan 2012 00:07:04 -0500 Subject: [PATCH 13/13] Changing for-loops to use pre-calculated values. Rather than calculating the value each time. --- contrib/artists/main.php | 14 ++++++++++---- contrib/blotter/theme.php | 13 ++++++++----- contrib/handle_flash/main.php | 3 ++- contrib/home/main.php | 3 ++- contrib/rating/main.php | 6 ++++-- core/util.inc.php | 3 ++- ext/comment/main.php | 10 +++++----- ext/ext_manager/main.php | 3 ++- ext/upload/theme.php | 8 ++++++-- lib/securimage/securimage.php | 4 +++- 10 files changed, 44 insertions(+), 23 deletions(-) diff --git a/contrib/artists/main.php b/contrib/artists/main.php index f035224a..9f057bc6 100644 --- a/contrib/artists/main.php +++ b/contrib/artists/main.php @@ -904,8 +904,10 @@ class Artists implements Extension { , array( $artistID )); - - for ($i = 0 ; $i < count($result) ; $i++) + + $num = count($result); + + for ($i = 0 ; $i < $num ; $i++) { $result[$i]["name"] = stripslashes($result[$i]["name"]); } @@ -921,8 +923,10 @@ class Artists implements Extension { , array( $artistID )); + + $num = count($result); - for ($i = 0 ; $i < count($result) ; $i++) + for ($i = 0 ; $i < $num ; $i++) { $result[$i]["url"] = stripslashes($result[$i]["url"]); } @@ -1038,8 +1042,10 @@ class Artists implements Extension { $pageNumber * $artistsPerPage , $artistsPerPage )); + + $number_of_listings = count($listing); - for ($i = 0 ; $i < count($listing) ; $i++) + for ($i = 0 ; $i < $number_of_listings ; $i++) { $listing[$i]["name"] = stripslashes($listing[$i]["name"]); $listing[$i]["user_name"] = stripslashes($listing[$i]["user_name"]); diff --git a/contrib/blotter/theme.php b/contrib/blotter/theme.php index e8d82860..fd78b467 100644 --- a/contrib/blotter/theme.php +++ b/contrib/blotter/theme.php @@ -55,7 +55,8 @@ class BlotterTheme extends Themelet { // Now, time for entries list. $table_rows = ""; - for ($i = 0 ; $i < count($entries) ; $i++) + $num_entries = count($entries); + for ($i = 0 ; $i < $num_entries ; $i++) { /** * Add table rows @@ -106,7 +107,8 @@ class BlotterTheme extends Themelet { $html .= "Blotter
";
 
-		for ($i = 0 ; $i < count($entries) ; $i++)
+		$num_entries = count($entries);
+		for ($i = 0 ; $i < $num_entries ; $i++)
 		{
 			/**
 			 * Blotter entries
@@ -156,7 +158,8 @@ $(document).ready(function() {
 });
 //-->";
 		$entries_list = "";
-		for ($i = 0 ; $i < count($entries) ; $i++)
+		$num_entries = count($entries);
+		for ($i = 0 ; $i < $num_entries ; $i++)
 		{
 			/**
 			 * Blotter entries
@@ -175,8 +178,8 @@ $(document).ready(function() {
 		$in_text = "";
 		$pos_break = "";
 		$pos_align = "text-align: right; position: absolute; right: 0px;";
-		if($position == "left") { $pos_break = "
"; $pos_align = ""; } - if(count($entries) == 0) { $out_text = "No blotter entries yet."; $in_text = "Empty.";} + if($position === "left") { $pos_break = "
"; $pos_align = ""; } + if(count($entries) === 0) { $out_text = "No blotter entries yet."; $in_text = "Empty.";} else { $clean_date = date("m/d/y",strtotime($entries[0]['entry_date'])); $out_text = "Blotter updated: {$clean_date}"; $in_text = "
    $entries_list
"; diff --git a/contrib/handle_flash/main.php b/contrib/handle_flash/main.php index 8ee32fc0..eec6b1c1 100644 --- a/contrib/handle_flash/main.php +++ b/contrib/handle_flash/main.php @@ -56,7 +56,8 @@ class FlashFileHandler extends DataHandlerExtension { private function str_to_binarray($string) { $binary = array(); - for($j=0; $j=0; $i--) { $binary[] = ($c >> $i) & 0x01; diff --git a/contrib/home/main.php b/contrib/home/main.php index f46c6238..855bc0ab 100644 --- a/contrib/home/main.php +++ b/contrib/home/main.php @@ -69,7 +69,8 @@ class Home extends SimpleExtension { $num_comma = number_format($total); $counter_text = ""; - for($n=0; $nterm, $matches)) { $sqes = $matches[1]; $arr = array(); - for($i=0; $ipage_matches("comment")) { - if($event->get_arg(0) == "add") { + if($event->get_arg(0) === "add") { if(isset($_POST['image_id']) && isset($_POST['comment'])) { try { $cpe = new CommentPostingEvent($_POST['image_id'], $user, $_POST['comment']); @@ -127,10 +127,10 @@ class CommentList extends SimpleExtension { } } } - else if($event->get_arg(0) == "delete") { + else if($event->get_arg(0) === "delete") { if($user->is_admin()) { // FIXME: post, not args - if($event->count_args() == 3) { + if($event->count_args() === 3) { send_event(new CommentDeletionEvent($event->get_arg(1))); $page->set_mode("redirect"); if(!empty($_SERVER['HTTP_REFERER'])) { @@ -145,7 +145,7 @@ class CommentList extends SimpleExtension { $this->theme->display_permission_denied($page); } } - else if($event->get_arg(0) == "list") { + else if($event->get_arg(0) === "list") { $this->build_page($event->get_arg(1)); } } @@ -367,7 +367,7 @@ class CommentList extends SimpleExtension { global $database; // sqlite fails at intervals - if($database->engine->name == "sqlite") return false; + if($database->engine->name === "sqlite") return false; $window = int_escape($config->get_int('comment_window')); $max = int_escape($config->get_int('comment_limit')); diff --git a/ext/ext_manager/main.php b/ext/ext_manager/main.php index b1763f2a..1e24b794 100644 --- a/ext/ext_manager/main.php +++ b/ext/ext_manager/main.php @@ -25,12 +25,13 @@ class ExtensionInfo { function ExtensionInfo($main) { $matches = array(); $lines = file($main); + $number_of_lines = count($lines); preg_match("#(ext|contrib)/(.*)/main.php#", $main, $matches); $this->ext_name = $matches[2]; $this->name = $this->ext_name; $this->enabled = $this->is_enabled($this->ext_name); - for($i=0; $iname = $matches[1]; diff --git a/ext/upload/theme.php b/ext/upload/theme.php index 1b13bf9e..25c22242 100644 --- a/ext/upload/theme.php +++ b/ext/upload/theme.php @@ -15,7 +15,9 @@ class UploadTheme extends Themelet { // Uploader 2.0! $upload_list = ""; - for($i=0; $i<$config->get_int('upload_count'); $i++) + $upload_count = $config->get_int('upload_count'); + + for($i=0; $i<$upload_count; $i++) { $a=$i+1; $s=$i-1; @@ -244,7 +246,9 @@ class UploadTheme extends Themelet { global $config; $upload_list = ""; - for($i=0; $i<$config->get_int('upload_count'); $i++) { + $upload_count = $config->get_int('upload_count'); + + for($i=0; $i<$upload_count; $i++) { if($i == 0) $style = ""; // "style='display:visible'"; else $style = "style='display:none'"; $upload_list .= "