diff --git a/contrib/artists/main.php b/contrib/artists/main.php index cd9190cb..0bde987d 100644 --- a/contrib/artists/main.php +++ b/contrib/artists/main.php @@ -914,8 +914,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"]); } @@ -931,8 +933,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"]); } @@ -1048,8 +1052,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 .= "
"; - 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 = "
Error: $query"; + print 'Message: '.$pdoe->getMessage(); + print '
Error: '.$query; exit; } } 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 42e28637..48ba8065 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -196,12 +196,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); } @@ -251,8 +251,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')); @@ -280,8 +283,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')); @@ -338,8 +344,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; } /** @@ -379,7 +385,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); if($sln != $this->locked) { @@ -458,7 +464,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()); @@ -469,7 +475,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()); } @@ -550,7 +556,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); @@ -593,7 +599,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); } @@ -641,7 +647,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); } @@ -658,7 +664,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); } @@ -760,7 +766,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); } @@ -789,11 +795,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); @@ -815,14 +823,14 @@ 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); } } // 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 ?) ", @@ -836,7 +844,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); } @@ -858,24 +866,24 @@ 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) { + if(!empty($img_search->sql)) { $query->append_sql(" WHERE "); $query->append($img_search); } @@ -921,15 +929,15 @@ 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); $tag_array = array(); foreach($tags as $tag) { - if(is_string($tag) && strlen($tag) > 0) { + if(is_string($tag) && !empty($tag)) { $tag_array[] = $tag; } } @@ -946,13 +954,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 e6e92a9a..6570632b 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"; @@ -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(''); } } } @@ -358,16 +358,16 @@ 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) { - $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(''); } } } @@ -408,16 +408,16 @@ 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) { 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 9ab57a57..0220ef23 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); } @@ -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); } /** @@ -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() { diff --git a/core/util.inc.php b/core/util.inc.php index 07152e72..af11952a 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; } } @@ -214,17 +218,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; } } } @@ -293,14 +297,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 "