From 7f4159ac258c501f9666efc465a78cb2aec9d177 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Mon, 21 Sep 2020 21:21:30 +0200 Subject: Code cleanup - Remove unused variables - Remove unused utility functions - Asserting presence and typehinting global variables - Remove unused variables - Add missing global - Add parameter type constraint - Fix some references to nonexisting variables --- ratatoeskr/backend.php | 52 ++++++++------ ratatoeskr/frontend.php | 7 +- ratatoeskr/main.php | 4 +- ratatoeskr/setup/setup.php | 6 ++ ratatoeskr/sys/models.php | 15 ++-- ratatoeskr/sys/plugin_api.php | 2 +- ratatoeskr/sys/pwhash.php | 2 +- ratatoeskr/sys/translation.php | 3 + ratatoeskr/sys/utils.php | 155 ----------------------------------------- 9 files changed, 55 insertions(+), 191 deletions(-) diff --git a/ratatoeskr/backend.php b/ratatoeskr/backend.php index 658bcc1..c050922 100644 --- a/ratatoeskr/backend.php +++ b/ratatoeskr/backend.php @@ -94,6 +94,10 @@ function build_backend_subactions() $url_next = ["login"]; }, "login" => url_action_simple(function ($data) { + /** + * @var \ste\STECore $ste + * @var Group|null $admin_grp + */ global $ste, $admin_grp; if (!empty($_POST["user"])) { try { @@ -130,6 +134,14 @@ function build_backend_subactions() }), "content" => url_action_subactions([ "write" => function (&$data, $url_now, &$url_next) { + /** + * @var \ste\STECore $ste + * @var array $translation + * @var array $textprocessors + * @var Settings $ratatoeskr_settings + * @var array $languages + * @var array $articleeditor_plugins + */ global $ste, $translation, $textprocessors, $ratatoeskr_settings, $languages, $articleeditor_plugins; list($article, $editlang) = array_slice($url_next, 0); @@ -393,7 +405,7 @@ function build_backend_subactions() echo $ste->exectemplate("/systemtemplates/content_write.html"); }, "tags" => function (&$data, $url_now, &$url_next) { - global $translation, $languages, $ste, $rel_path_to_root; + global $translation, $languages, $ste; $url_next = []; @@ -511,7 +523,7 @@ function build_backend_subactions() echo $ste->exectemplate("/systemtemplates/tags_overview.html"); }, "articles" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $languages, $rel_path_to_root; + global $ste, $translation; $url_next = []; @@ -660,7 +672,7 @@ function build_backend_subactions() echo $ste->exectemplate("/systemtemplates/articles.html"); }, "images" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $languages, $rel_path_to_root; + global $ste, $translation; list($imgid, $imageaction) = $url_next; @@ -742,7 +754,7 @@ function build_backend_subactions() echo $ste->exectemplate("/systemtemplates/image_list.html"); }, "comments" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $languages, $rel_path_to_root; + global $ste, $translation; list($comment_id) = $url_next; @@ -920,7 +932,7 @@ function build_backend_subactions() ]), "design" => url_action_subactions([ "templates" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $languages, $rel_path_to_root; + global $ste, $translation; list($template) = $url_next; @@ -986,7 +998,7 @@ function build_backend_subactions() echo $ste->exectemplate("/systemtemplates/templates.html"); }, "styles" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $languages, $rel_path_to_root; + global $ste, $translation; list($style) = $url_next; @@ -1049,9 +1061,7 @@ function build_backend_subactions() echo $ste->exectemplate("/systemtemplates/styles.html"); }, "sections" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $languages, $rel_path_to_root, $ratatoeskr_settings; - - list($style) = $url_next; + global $ste, $translation, $languages, $ratatoeskr_settings; $url_next = []; @@ -1204,7 +1214,7 @@ function build_backend_subactions() ]), "admin" => url_action_subactions([ "settings" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $languages, $rel_path_to_root, $ratatoeskr_settings, $textprocessors; + global $ste, $translation, $languages, $ratatoeskr_settings, $textprocessors; $url_next = []; @@ -1290,7 +1300,7 @@ function build_backend_subactions() }, "users" => url_action_subactions([ "_index" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $languages, $rel_path_to_root, $ratatoeskr_settings, $textprocessors; + global $ste, $translation; $url_next = []; @@ -1307,7 +1317,7 @@ function build_backend_subactions() Group::by_name($_POST["group_name"]); $ste->vars["error"] = $translation["group_already_exists"]; } catch (DoesNotExistError $e) { - $group = Group::create($_POST["group_name"]); + Group::create($_POST["group_name"]); $ste->vars["success"] = $translation["successfully_created_group"]; } } @@ -1322,7 +1332,7 @@ function build_backend_subactions() User::by_name($_POST["username"]); $ste->vars["error"] = $translation["user_already_exists"]; } catch (DoesNotExistError $e) { - $group = User::create($_POST["username"], PasswordHash::create($_POST["initial_password"])); + User::create($_POST["username"], PasswordHash::create($_POST["initial_password"])); $ste->vars["success"] = $translation["successfully_created_user"]; } } @@ -1394,7 +1404,7 @@ function build_backend_subactions() echo $ste->exectemplate("/systemtemplates/users.html"); }, "u" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $languages, $rel_path_to_root, $admin_grp; + global $ste, $translation; try { $user = User::by_id($url_next[0]); @@ -1476,7 +1486,7 @@ function build_backend_subactions() } ]), "repos" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $languages, $rel_path_to_root; + global $ste, $translation; $url_next = []; @@ -1549,7 +1559,7 @@ function build_backend_subactions() ]), "plugin" => url_action_subactions([ "list" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $languages, $rel_path_to_root, $plugin_objs, $api_compat; + global $ste, $translation, $plugin_objs, $api_compat; $url_next = []; @@ -1675,7 +1685,7 @@ function build_backend_subactions() echo $ste->exectemplate("/systemtemplates/pluginlist.html"); }, "help" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $languages, $rel_path_to_root; + global $ste; try { $plugin = Plugin::by_id($url_next[0]); @@ -1696,7 +1706,7 @@ function build_backend_subactions() echo $ste->exectemplate("/systemtemplates/pluginhelp.html"); }, "install" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $languages, $rel_path_to_root, $api_compat; + global $ste, $translation, $api_compat; $url_next = []; @@ -1766,9 +1776,7 @@ function build_backend_subactions() echo $ste->exectemplate("/systemtemplates/plugininstall.html"); }, "repoinstall" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $rel_path_to_root; - - $stream_ctx = stream_context_create(["http" => ["timeout" => 5]]); + global $ste, $translation; try { $repo = Repository::by_id($_GET["repo"]); @@ -1788,7 +1796,7 @@ function build_backend_subactions() } }, "confirminstall" => function (&$data, $url_now, &$url_next) { - global $ste, $translation, $languages, $rel_path_to_root; + global $ste, $translation; list($plugin_id) = $url_next; $url_next = []; diff --git a/ratatoeskr/frontend.php b/ratatoeskr/frontend.php index 51e13f7..de017dc 100644 --- a/ratatoeskr/frontend.php +++ b/ratatoeskr/frontend.php @@ -15,6 +15,9 @@ require_once(dirname(__FILE__) . "/sys/models.php"); require_once(dirname(__FILE__) . "/sys/textprocessors.php"); require_once(dirname(__FILE__) . "/libs/kses.php"); +/** @var \ste\STECore $ste */ +assert(isset($ste)); + /* * Function: section_transform_ste * Transforms an
object to an array, so it can be accessed via a STE template. @@ -140,8 +143,6 @@ function article_transform_ste($article, $lang) */ function comment_transform_ste($comment) { - global $rel_path_to_root, $ratatoeskr_settings; - return [ "id" => $comment->get_id(), "text" => $comment->create_html(), @@ -747,7 +748,7 @@ $on_comment_store = []; */ function frontend_url_handler(&$data, $url_now, &$url_next) { - global $ste, $ratatoeskr_settings, $languages, $metasections, $comment_validators, $on_comment_store; + global $ste, $ratatoeskr_settings, $languages, $comment_validators, $on_comment_store; $path = array_merge([$url_now], $url_next); $url_next = []; diff --git a/ratatoeskr/main.php b/ratatoeskr/main.php index 491c739..9bbe504 100644 --- a/ratatoeskr/main.php +++ b/ratatoeskr/main.php @@ -46,8 +46,6 @@ function _ratatoeskr() { global $backend_subactions, $ste, $url_handlers, $ratatoeskr_settings, $plugin_objs, $api_compat; - $ts_start = microtime(true); - session_start(); db_connect(); clean_database(); @@ -60,6 +58,7 @@ function _ratatoeskr() $activeplugins = array_filter(Plugin::all(), function ($plugin) { return $plugin->active; }); + /** @var Plugin $plugin */ foreach ($activeplugins as $plugin) { if (!in_array($plugin->api, $api_compat)) { $plugin->active = false; @@ -68,6 +67,7 @@ function _ratatoeskr() } eval($plugin->code); + /** @var RatatoeskrPlugin $plugin_obj */ $plugin_obj = new $plugin->classname($plugin->get_id()); if ($plugin->update) { $plugin_obj->update(); diff --git a/ratatoeskr/setup/setup.php b/ratatoeskr/setup/setup.php index 3e3be9c..749315a 100644 --- a/ratatoeskr/setup/setup.php +++ b/ratatoeskr/setup/setup.php @@ -9,6 +9,9 @@ require_once(dirname(__FILE__) . "/../sys/pwhash.php"); require_once(dirname(__FILE__) . "/../languages.php"); require_once(dirname(__FILE__) . "/create_tables.php"); +/** @var \ste\STECore $ste */ +assert(isset($ste)); + $rel_path_to_root = "."; $ste->vars["rel_path_to_root"] = $rel_path_to_root; @@ -21,6 +24,9 @@ foreach ($languages as $langcode => $langinfo) { if (isset($_GET["lang"]) and (@$languages[$_GET["lang"]]["translation_exist"])) { load_language($_GET["lang"]); + + assert(isset($translation)); + $lang = $_GET["lang"]; $ste->vars["lang"] = $_GET["lang"]; } else { diff --git a/ratatoeskr/sys/models.php b/ratatoeskr/sys/models.php index 626b237..9f81fcf 100644 --- a/ratatoeskr/sys/models.php +++ b/ratatoeskr/sys/models.php @@ -278,10 +278,9 @@ class User extends BySQLRowEnabled */ public static function create($username, $pwhash) { - global $ratatoeskr_settings; global $db_con; try { - $obj = self::by_name($username); + self::by_name($username); } catch (DoesNotExistError $e) { global $ratatoeskr_settings; qdb( @@ -301,7 +300,7 @@ class User extends BySQLRowEnabled return $obj; } - throw new AlreadyExistsError("\"$name\" is already in database."); + throw new AlreadyExistsError("\"$username\" is already in database."); } protected function populate_by_sqlrow($sqlrow) @@ -504,7 +503,7 @@ class Group extends BySQLRowEnabled { global $db_con; try { - $obj = self::by_name($name); + self::by_name($name); } catch (DoesNotExistError $e) { qdb("INSERT INTO `PREFIX_groups` (`name`) VALUES (?)", $name); $obj = new self(); @@ -1787,7 +1786,7 @@ class Section extends BySQLRowEnabled } try { - $obj = self::by_name($name); + self::by_name($name); } catch (DoesNotExistError $e) { $obj = new self(); $obj->name = $name; @@ -2056,7 +2055,7 @@ class Tag extends BySQLRowEnabled } try { - $obj = self::by_name($name); + self::by_name($name); } catch (DoesNotExistError $e) { $obj = new self(); @@ -2634,7 +2633,7 @@ class Repository extends BySQLRowEnabled $this->name = $repometa["name"]; $this->description = $repometa["description"]; - $this->packages = @unserialize(@file_get_contents($this->baseurl . "/packagelist", false, $ctx)); + $this->packages = @unserialize(@file_get_contents($this->baseurl . "/packagelist", false, $this->stream_ctx)); $this->lastrefresh = time(); @@ -3259,6 +3258,8 @@ class ArticleExtradata extends KVStorage */ function dbversion() { + global $config; + /* Is the meta table present? If no, the version is 0. */ $stmt = qdb( "SELECT COUNT(*) FROM `information_schema`.`tables` WHERE `table_schema` = ? AND `table_name` = ?", diff --git a/ratatoeskr/sys/plugin_api.php b/ratatoeskr/sys/plugin_api.php index 65b43e2..18e129b 100644 --- a/ratatoeskr/sys/plugin_api.php +++ b/ratatoeskr/sys/plugin_api.php @@ -35,7 +35,7 @@ $url_handlers = []; /* master URL handler */ * $name - The name of the new URL * $callback - The Function to be called (see ). */ -function register_url_handler($name, $callback) +function register_url_handler($name, callable $callback) { global $url_handlers; $url_handlers[$name] = $callback; diff --git a/ratatoeskr/sys/pwhash.php b/ratatoeskr/sys/pwhash.php index 3d3c589..f52ae03 100644 --- a/ratatoeskr/sys/pwhash.php +++ b/ratatoeskr/sys/pwhash.php @@ -70,7 +70,7 @@ class PasswordHash */ public static function validate($password, $pwhash) { - list($iterations, $hexsalt, $hash) = explode('$', $pwhash); + list($iterations, $hexsalt) = explode('$', $pwhash); return self::hash($password, pack("H*", $hexsalt), $iterations) == $pwhash; } } diff --git a/ratatoeskr/sys/translation.php b/ratatoeskr/sys/translation.php index 5a763ce..fd494c1 100644 --- a/ratatoeskr/sys/translation.php +++ b/ratatoeskr/sys/translation.php @@ -12,6 +12,9 @@ require_once(dirname(__FILE__) . "/utils.php"); require_once(dirname(__FILE__) . "/init_ste.php"); +/** @var \ste\STECore $ste */ +assert(isset($ste)); + if (!defined("SETUP")) { require_once(dirname(__FILE__) . "/models.php"); } diff --git a/ratatoeskr/sys/utils.php b/ratatoeskr/sys/utils.php index 89f779d..a285bcd 100644 --- a/ratatoeskr/sys/utils.php +++ b/ratatoeskr/sys/utils.php @@ -31,100 +31,6 @@ function array_repeat($val, $n) return $rv; } -/* - * Function: array_blend - * - * Blend multiple arrays together. - * - * Example: - * - * array_blend(array(1,2,3), array(4,5,6), array(7,8,9)); - * will return array(1,4,7,2,5,8,3,6,9) - */ -function array_blend() -{ - $arrays = array_filter(func_get_args(), "is_array"); - - switch (count($arrays)) { - case 0: return []; break; - case 1: return $arrays[0]; break; - default: - $rv = []; - while (array_sum(array_map("count", $arrays)) > 0) { - for ($i = 0; $i < count($arrays); ++$i) { - $val = array_shift($arrays[$i]); - if ($val === null) { - continue; - } - array_push($rv, $val); - } - } - return $rv; - break; - } -} - -/* - * Function: array_filter_empty - * - * Filters all empty elements out of an array. - * - * Parameters: - * - * $input - The input array - * - * Returns: - * - * The $input without its empty elements. - */ -function array_filter_empty($input) -{ - return array_filter($input, function ($x) { - return !empty($x); - }); -} - -/* - * Function: array_filter_keys - * - * Like PHPs `array_filter`, but callback will get the key, not the value of the array element. - */ -function array_filter_keys($input, $callback) -{ - if (!is_array($input)) { - throw new InvalidArgumentException("Argument 1 must be an array"); - } - if (empty($input)) { - return []; - } - $delete_keys = array_filter(array_keys($input), function ($x) use ($callback) { - return !$callback($x); - }); - foreach ($delete_keys as $key) { - unset($input[$key]); - } - return $input; -} - -/* - * Function: array_kvpairs_to_assoc - * Convert array of key-value pairs to an associative array. - * - * Parameters: - * $input - Array of key-value pairs - * - * Returns: - * An associative array. - */ -function array_kvpairs_to_assoc($input) -{ - $rv = []; - foreach ($input as $kvpair) { - $rv[$kvpair[0]] = $kvpair[1]; - } - return $rv; -} - /* * Function: intcmp * Compare integers (equavilent to strcmp) @@ -134,67 +40,6 @@ function intcmp($a, $b) return ($a == $b) ? 0 : (($a < $b) ? -1 : 1); } -/* - * Function: ucount - * - * Count elements of an array matching unser-defined rules. - * - * Parameters: - * $array - The input array. - * $callback - A callback function. It will be called with the current value as the only parameter. The value is counted, if callback returns TRUE. - * - * Returns: - * - * Number of elements where $callback returned TRUE. - */ -function ucount($array, $callback) -{ - return count(array_filter($array, $callback)); -} - -/* - * Function: vcount - * - * Counts how often $value appears in $array. - * - * Parameters: - * - * $array - - * $value - - * - * Returns: - * - * How often $value appears in $array. - */ -function vcount($array, $value) -{ - return ucount($array, function ($x) { - return $x===$value; - }); -} - -/* - * Function: self_url - * - * Gets current URL. - * - * From: http://dev.kanngard.net/Permalinks/ID_20050507183447.html - */ -function self_url() -{ - $s = empty($_SERVER["HTTPS"]) ? '' - : ($_SERVER["HTTPS"] == "on") ? "s" - : ""; - $protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s; - $port = ($_SERVER["SERVER_PORT"] == "80") ? "" - : (":".$_SERVER["SERVER_PORT"]); - return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI']; -} -function strleft($s1, $s2) -{ - return substr($s1, 0, strpos($s1, $s2)); -} - /* * Function: htmlesc * Escape HTML (shorter than htmlspecialchars) -- cgit v1.2.3-54-g00ecf