]> git.mxchange.org Git - friendica.git/blobdiff - mod/admin.php
Replace dba::select(limit => 1) by dba::selectOne
[friendica.git] / mod / admin.php
index 0b44fbd6a85db8667c4cd6de11c7bcc5f9869845..952956440ac8d54f7e95db93183c05a579132037 100644 (file)
@@ -13,6 +13,7 @@ use Friendica\Database\DBM;
 use Friendica\Database\DBStructure;
 use Friendica\Model\Contact;
 use Friendica\Model\User;
+use Friendica\Module\Login;
 
 require_once 'include/enotify.php';
 require_once 'include/text.php';
@@ -43,7 +44,7 @@ function admin_post(App $a)
                return;
        }
 
-       // urls
+       $return_path = 'admin';
        if ($a->argc > 1) {
                switch ($a->argv[1]) {
                        case 'site':
@@ -61,8 +62,7 @@ function admin_post(App $a)
                                                $func($a);
                                        }
                                }
-                               goaway('admin/plugins/' . $a->argv[2]);
-                               return; // NOTREACHED
+                               $return_path = 'admin/plugins/' . $a->argv[2];
                                break;
                        case 'themes':
                                if ($a->argc < 2) {
@@ -75,38 +75,31 @@ function admin_post(App $a)
 
                                $theme = $a->argv[2];
                                if (is_file("view/theme/$theme/config.php")) {
-
-                                       function __call_theme_admin_post(App $a, $theme)
-                                       {
-                                               $orig_theme = $a->theme;
-                                               $orig_page = $a->page;
-                                               $orig_session_theme = $_SESSION['theme'];
-                                               require_once("view/theme/$theme/theme.php");
-                                               require_once("view/theme/$theme/config.php");
-                                               $_SESSION['theme'] = $theme;
-
-
-                                               $init = $theme . "_init";
-                                               if (function_exists($init)) {
-                                                       $init($a);
-                                               }
-                                               if (function_exists("theme_admin_post")) {
-                                                       $admin_form = theme_admin_post($a);
-                                               }
-
-                                               $_SESSION['theme'] = $orig_session_theme;
-                                               $a->theme = $orig_theme;
-                                               $a->page = $orig_page;
-                                               return $admin_form;
+                                       $orig_theme = $a->theme;
+                                       $orig_page = $a->page;
+                                       $orig_session_theme = $_SESSION['theme'];
+                                       require_once "view/theme/$theme/theme.php";
+                                       require_once "view/theme/$theme/config.php";
+                                       $_SESSION['theme'] = $theme;
+
+                                       $init = $theme . '_init';
+                                       if (function_exists($init)) {
+                                               $init($a);
                                        }
-                                       __call_theme_admin_post($a, $theme);
+                                       if (function_exists('theme_admin_post')) {
+                                               theme_admin_post($a);
+                                       }
+
+                                       $_SESSION['theme'] = $orig_session_theme;
+                                       $a->theme = $orig_theme;
+                                       $a->page = $orig_page;
                                }
+
                                info(t('Theme settings updated.'));
                                if (is_ajax()) {
                                        return;
                                }
-                               goaway('admin/themes/' . $theme);
-                               return;
+                               $return_path = 'admin/themes/' . $theme;
                                break;
                        case 'features':
                                admin_page_features_post($a);
@@ -114,9 +107,6 @@ function admin_post(App $a)
                        case 'logs':
                                admin_page_logs_post($a);
                                break;
-                       case 'dbsync':
-                               admin_page_dbsync_post($a);
-                               break;
                        case 'contactblock':
                                admin_page_contactblock_post($a);
                                break;
@@ -129,7 +119,7 @@ function admin_post(App $a)
                }
        }
 
-       goaway('admin');
+       goaway($return_path);
        return; // NOTREACHED
 }
 
@@ -153,7 +143,7 @@ function admin_post(App $a)
 function admin_content(App $a)
 {
        if (!is_site_admin()) {
-               return login(false);
+               return Login::form();
        }
 
        if (x($_SESSION, 'submanage') && intval($_SESSION['submanage'])) {
@@ -651,7 +641,7 @@ function admin_page_federation(App $a)
                '$autoactive' => Config::get('system', 'poco_completion'),
                '$counts' => $counts,
                '$version' => FRIENDICA_VERSION,
-               '$legendtext' => sprintf(t('Currently this node is aware of %d nodes with %d registered users from the following platforms:'), $total, $users),
+               '$legendtext' => t('Currently this node is aware of %d nodes with %d registered users from the following platforms:', $total, $users),
                '$baseurl' => System::baseUrl(),
        ));
 }
@@ -681,7 +671,7 @@ function admin_page_queue(App $a)
        return replace_macros($t, array(
                '$title' => t('Administration'),
                '$page' => t('Inspect Queue'),
-               '$count' => sizeof($r),
+               '$count' => count($r),
                'id_header' => t('ID'),
                '$to_header' => t('Recipient Name'),
                '$url_header' => t('Recipient Profile'),
@@ -712,14 +702,14 @@ function admin_page_summary(App $a)
        $warningtext = array();
        if (DBM::is_result($r)) {
                $showwarning = true;
-               $warningtext[] = sprintf(t('Your DB still runs with MyISAM tables. You should change the engine type to InnoDB. As Friendica will use InnoDB only features in the future, you should change this! See <a href="%s">here</a> for a guide that may be helpful converting the table engines. You may also use the command <tt>php scripts/dbstructure.php toinnodb</tt> of your Friendica installation for an automatic conversion.<br />'), 'https://dev.mysql.com/doc/refman/5.7/en/converting-tables-to-innodb.html');
+               $warningtext[] = t('Your DB still runs with MyISAM tables. You should change the engine type to InnoDB. As Friendica will use InnoDB only features in the future, you should change this! See <a href="%s">here</a> for a guide that may be helpful converting the table engines. You may also use the command <tt>php scripts/dbstructure.php toinnodb</tt> of your Friendica installation for an automatic conversion.<br />', 'https://dev.mysql.com/doc/refman/5.7/en/converting-tables-to-innodb.html');
        }
        // Check if github.com/friendica/master/VERSION is higher then
        // the local version of Friendica. Check is opt-in, source may be master or devel branch
        if (Config::get('system', 'check_new_version_url', 'none') != 'none') {
                $gitversion = Config::get('system', 'git_friendica_version');
                if (version_compare(FRIENDICA_VERSION, $gitversion) < 0) {
-                       $warningtext[] = sprintf(t('There is a new version of Friendica available for download. Your current version is %1$s, upstream version is %2$s'), $FRIENDICA_VERSION, $gitversion);
+                       $warningtext[] = t('There is a new version of Friendica available for download. Your current version is %1$s, upstream version is %2$s', FRIENDICA_VERSION, $gitversion);
                        $showwarning = true;
                }
        }
@@ -738,7 +728,7 @@ function admin_page_summary(App $a)
                $warningtext[] = t('The worker was never executed. Please check your database structure!');
        } elseif ((strtotime(datetime_convert()) - strtotime($last_worker_call)) > 60 * 60) {
                $showwarning = true;
-               $warningtext[] = sprintf(t('The last worker execution was on %s UTC. This is older than one hour. Please check your crontab settings.'), $last_worker_call);
+               $warningtext[] = t('The last worker execution was on %s UTC. This is older than one hour. Please check your crontab settings.', $last_worker_call);
        }
 
        $r = q("SELECT `page-flags`, COUNT(`uid`) AS `count` FROM `user` GROUP BY `page-flags`");
@@ -816,7 +806,7 @@ function admin_page_site_post(App $a)
                $new_url = rtrim($new_url, "/");
 
                $parsed = @parse_url($new_url);
-               if (!$parsed || (!x($parsed, 'host') || !x($parsed, 'scheme'))) {
+               if (!is_array($parsed) || !x($parsed, 'host') || !x($parsed, 'scheme')) {
                        notice(t("Can not parse base url. Must have at least <scheme>://<domain>"));
                        goaway('admin/site');
                }
@@ -846,11 +836,8 @@ function admin_page_site_post(App $a)
 
                        $upds = implode(", ", $upd);
 
-
-
-                       $q = sprintf("UPDATE %s SET %s;", $table_name, $upds);
-                       $r = q($q);
-                       if (!$r) {
+                       $r = q("UPDATE %s SET %s;", $table_name, $upds);
+                       if (!DBM::is_result($r)) {
                                notice("Failed updating '$table_name': " . dba::errorMessage());
                                goaway('admin/site');
                        }
@@ -907,6 +894,8 @@ function admin_page_site_post(App $a)
 
        $allowed_sites          =       ((x($_POST,'allowed_sites'))            ? notags(trim($_POST['allowed_sites']))         : '');
        $allowed_email          =       ((x($_POST,'allowed_email'))            ? notags(trim($_POST['allowed_email']))         : '');
+       $no_oembed_rich_content = x($_POST,'no_oembed_rich_content');
+       $allowed_oembed         =       ((x($_POST,'allowed_oembed'))           ? notags(trim($_POST['allowed_oembed']))                : '');
        $block_public           =       ((x($_POST,'block_public'))             ? True                                          : False);
        $force_publish          =       ((x($_POST,'publish_all'))              ? True                                          : False);
        $global_directory       =       ((x($_POST,'directory'))                ? notags(trim($_POST['directory']))             : '');
@@ -1063,6 +1052,8 @@ function admin_page_site_post(App $a)
        Config::set('config', 'register_text', $register_text);
        Config::set('system', 'allowed_sites', $allowed_sites);
        Config::set('system', 'allowed_email', $allowed_email);
+       Config::set('system', 'no_oembed_rich_content', $no_oembed_rich_content);
+       Config::set('system', 'allowed_oembed', $allowed_oembed);
        Config::set('system', 'block_public', $block_public);
        Config::set('system', 'publish_all', $force_publish);
        Config::set('system', 'newuser_private', $newuser_private);
@@ -1146,8 +1137,7 @@ function admin_page_site(App $a)
        $theme_choices_mobile = array();
        $theme_choices_mobile["---"] = t("No special theme for mobile devices");
        $files = glob('view/theme/*');
-       if ($files) {
-
+       if (is_array($files)) {
                $allowed_theme_list = Config::get('system', 'allowed_themes');
 
                foreach ($files as $file) {
@@ -1180,16 +1170,6 @@ function admin_page_site(App $a)
                CP_USERS_AND_GLOBAL => t("Public postings from local users and the federated network")
        );
 
-       /* OStatus conversation poll choices */
-       $ostatus_poll_choices = array(
-               "-2" => t("Never"),
-               "-1" => t("At post arrival"),
-               "0" => t("Frequently"),
-               "60" => t("Hourly"),
-               "720" => t("Twice daily"),
-               "1440" => t("Daily")
-       );
-
        $poco_discovery_choices = array(
                "0" => t("Disabled"),
                "1" => t("Users"),
@@ -1283,7 +1263,7 @@ function admin_page_site(App $a)
                '$banner'               => array('banner', t("Banner/Logo"), $banner, ""),
                '$shortcut_icon'        => array('shortcut_icon', t("Shortcut icon"), Config::get('system','shortcut_icon'),  t("Link to an icon that will be used for browsers.")),
                '$touch_icon'           => array('touch_icon', t("Touch icon"), Config::get('system','touch_icon'),  t("Link to an icon that will be used for tablets and mobiles.")),
-               '$info'                 => array('info', t('Additional Info'), $info, sprintf(t('For public servers: you can add additional information here that will be listed at %s/servers.'), get_server())),
+               '$info'                 => array('info', t('Additional Info'), $info, t('For public servers: you can add additional information here that will be listed at %s/servers.', get_server())),
                '$language'             => array('language', t("System language"), Config::get('system','language'), "", $lang_choices),
                '$theme'                => array('theme', t("System theme"), Config::get('system','theme'), t("Default system theme - may be over-ridden by user profiles - <a href='#' id='cnftheme'>change theme settings</a>"), $theme_choices),
                '$theme_mobile'         => array('theme_mobile', t("Mobile system theme"), Config::get('system', 'mobile-theme', '---'), t("Theme for mobile devices"), $theme_choices_mobile),
@@ -1301,6 +1281,8 @@ function admin_page_site(App $a)
                '$abandon_days'         => array('abandon_days', t('Accounts abandoned after x days'), Config::get('system','account_abandon_days'), t('Will not waste system resources polling external sites for abandonded accounts. Enter 0 for no time limit.')),
                '$allowed_sites'        => array('allowed_sites', t("Allowed friend domains"), Config::get('system','allowed_sites'), t("Comma separated list of domains which are allowed to establish friendships with this site. Wildcards are accepted. Empty to allow any domains")),
                '$allowed_email'        => array('allowed_email', t("Allowed email domains"), Config::get('system','allowed_email'), t("Comma separated list of domains which are allowed in email addresses for registrations to this site. Wildcards are accepted. Empty to allow any domains")),
+               '$no_oembed_rich_content' => array('no_oembed_rich_content', t("No OEmbed rich content"), Config::get('system','no_oembed_rich_content'), t("Don't show the rich content (e.g. embedded PDF), except from the domains listed below.")),
+               '$allowed_oembed'       => array('allowed_oembed', t("Allowed OEmbed domains"), Config::get('system','allowed_oembed'), t("Comma separated list of domains which oembed content is allowed to be displayed. Wildcards are accepted.")),
                '$block_public'         => array('block_public', t("Block public"), Config::get('system','block_public'), t("Check to block public access to all otherwise public personal pages on this site unless you are currently logged in.")),
                '$force_publish'        => array('publish_all', t("Force publish"), Config::get('system','publish_all'), t("Check to force all profiles on this site to be listed in the site directory.")),
                '$global_directory'     => array('directory', t("Global directory URL"), Config::get('system','directory'), t("URL to the global directory. If this is not set, the global directory is completely unavailable to the application.")),
@@ -1356,7 +1338,7 @@ function admin_page_site(App $a)
                '$worker_queues'        => array('worker_queues', t("Maximum number of parallel workers"), Config::get('system','worker_queues'), t("On shared hosters set this to 2. On larger systems, values of 10 are great. Default value is 4.")),
                '$worker_dont_fork'     => array('worker_dont_fork', t("Don't use 'proc_open' with the worker"), Config::get('system','worker_dont_fork'), t("Enable this if your system doesn't allow the use of 'proc_open'. This can happen on shared hosters. If this is enabled you should increase the frequency of worker calls in your crontab.")),
                '$worker_fastlane'      => array('worker_fastlane', t("Enable fastlane"), Config::get('system','worker_fastlane'), t("When enabed, the fastlane mechanism starts an additional worker if processes with higher priority are blocked by processes of lower priority.")),
-               '$worker_frontend'      => array('worker_frontend', t('Enable frontend worker'), Config::get('system','frontend_worker'), sprintf(t('When enabled the Worker process is triggered when backend access is performed (e.g. messages being delivered). On smaller sites you might want to call %s/worker on a regular basis via an external cron job. You should only enable this option if you cannot utilize cron/scheduled jobs on your server.'), System::baseUrl())),
+               '$worker_frontend'      => array('worker_frontend', t('Enable frontend worker'), Config::get('system','frontend_worker'), t('When enabled the Worker process is triggered when backend access is performed (e.g. messages being delivered). On smaller sites you might want to call %s/worker on a regular basis via an external cron job. You should only enable this option if you cannot utilize cron/scheduled jobs on your server.', System::baseUrl())),
 
                '$form_security_token'  => get_form_security_token("admin_site")
        ));
@@ -1390,11 +1372,11 @@ function admin_page_dbsync(App $a)
 
        if (($a->argc > 2) && (intval($a->argv[2]) || ($a->argv[2] === 'check'))) {
                $retval = DBStructure::update(false, true);
-               if (!$retval) {
-                       $o .= sprintf(t("Database structure update %s was successfully applied."), DB_UPDATE_VERSION) . "<br />";
+               if ($retval === '') {
+                       $o .= t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
                        Config::set('database', 'dbupdate_' . DB_UPDATE_VERSION, 'success');
                } else {
-                       $o .= sprintf(t("Executing of database structure update %s failed with error: %s"), DB_UPDATE_VERSION, $retval) . "<br />";
+                       $o .= t("Executing of database structure update %s failed with error: %s", DB_UPDATE_VERSION, $retval) . "<br />";
                }
                if ($a->argv[2] === 'check') {
                        return $o;
@@ -1407,15 +1389,15 @@ function admin_page_dbsync(App $a)
                if (function_exists($func)) {
                        $retval = $func();
                        if ($retval === UPDATE_FAILED) {
-                               $o .= sprintf(t("Executing %s failed with error: %s"), $func, $retval);
+                               $o .= t("Executing %s failed with error: %s", $func, $retval);
                        } elseif ($retval === UPDATE_SUCCESS) {
-                               $o .= sprintf(t('Update %s was successfully applied.', $func));
+                               $o .= t('Update %s was successfully applied.', $func);
                                Config::set('database', $func, 'success');
                        } else {
-                               $o .= sprintf(t('Update %s did not return a status. Unknown if it succeeded.'), $func);
+                               $o .= t('Update %s did not return a status. Unknown if it succeeded.', $func);
                        }
                } else {
-                       $o .= sprintf(t('There was no additional update function %s that needed to be called.'), $func) . "<br />";
+                       $o .= t('There was no additional update function %s that needed to be called.', $func) . "<br />";
                        Config::set('database', $func, 'success');
                }
                return $o;
@@ -1459,11 +1441,11 @@ function admin_page_dbsync(App $a)
  */
 function admin_page_users_post(App $a)
 {
-       $pending     = (x($_POST, 'pending')           ? $_POST['pending']           : array());
-       $users       = (x($_POST, 'user')              ? $_POST['user']               : array());
-       $nu_name     = (x($_POST, 'new_user_name')     ? $_POST['new_user_name']     : '');
-       $nu_nickname = (x($_POST, 'new_user_nickname') ? $_POST['new_user_nickname'] : '');
-       $nu_email    = (x($_POST, 'new_user_email')    ? $_POST['new_user_email']    : '');
+       $pending     = defaults($_POST, 'pending'          , array());
+       $users       = defaults($_POST, 'user'             , array());
+       $nu_name     = defaults($_POST, 'new_user_name'    , '');
+       $nu_nickname = defaults($_POST, 'new_user_nickname', '');
+       $nu_email    = defaults($_POST, 'new_user_email'   , '');
        $nu_language = Config::get('system', 'language');
 
        check_form_security_token_redirectOnErr('/admin/users', 'admin_users');
@@ -1518,7 +1500,7 @@ function admin_page_users_post(App $a)
                notification(array(
                        'type' => SYSTEM_EMAIL,
                        'to_email' => $user['email'],
-                       'subject' => sprintf(t('Registration details for %s'), $a->config['sitename']),
+                       'subject' => t('Registration details for %s', $a->config['sitename']),
                        'preamble' => $preamble,
                        'body' => $body));
        }
@@ -1528,23 +1510,23 @@ function admin_page_users_post(App $a)
                        q("UPDATE `user` SET `blocked` = 1-`blocked` WHERE `uid` = %s", intval($uid)
                        );
                }
-               notice(sprintf(tt("%s user blocked/unblocked", "%s users blocked/unblocked", count($users)), count($users)));
+               notice(tt("%s user blocked/unblocked", "%s users blocked/unblocked", count($users)));
        }
        if (x($_POST, 'page_users_delete')) {
                foreach ($users as $uid) {
                        User::remove($uid);
                }
-               notice(sprintf(tt("%s user deleted", "%s users deleted", count($users)), count($users)));
+               notice(tt("%s user deleted", "%s users deleted", count($users)));
        }
 
        if (x($_POST, 'page_users_approve')) {
-               require_once("mod/regmod.php");
+               require_once "mod/regmod.php";
                foreach ($pending as $hash) {
                        user_allow($hash);
                }
        }
        if (x($_POST, 'page_users_deny')) {
-               require_once("mod/regmod.php");
+               require_once "mod/regmod.php";
                foreach ($pending as $hash) {
                        user_deny($hash);
                }
@@ -1569,8 +1551,8 @@ function admin_page_users(App $a)
 {
        if ($a->argc > 2) {
                $uid = $a->argv[3];
-               $user = q("SELECT `username`, `blocked` FROM `user` WHERE `uid` = %d", intval($uid));
-               if (count($user) == 0) {
+               $user = dba::selectOne('user', ['username', 'blocked'], ['uid' => $uid]);
+               if (DBM::is_result($user)) {
                        notice('User not found' . EOL);
                        goaway('admin/users');
                        return ''; // NOTREACHED
@@ -1581,13 +1563,15 @@ function admin_page_users(App $a)
                                // delete user
                                User::remove($uid);
 
-                               notice(sprintf(t("User '%s' deleted"), $user[0]['username']) . EOL);
+                               notice(t("User '%s' deleted", $user['username']) . EOL);
                                break;
                        case "block":
                                check_form_security_token_redirectOnErr('/admin/users', 'admin_users', 't');
-                               q("UPDATE `user` SET `blocked` = %d WHERE `uid` = %s", intval(1 - $user[0]['blocked']), intval($uid)
+                               q("UPDATE `user` SET `blocked` = %d WHERE `uid` = %s",
+                                       intval(1 - $user['blocked']),
+                                       intval($uid)
                                );
-                               notice(sprintf(($user[0]['blocked'] ? t("User '%s' unblocked") : t("User '%s' blocked")), $user[0]['username']) . EOL);
+                               notice(sprintf(($user['blocked'] ? t("User '%s' unblocked") : t("User '%s' blocked")), $user['username']) . EOL);
                                break;
                }
                goaway('admin/users');
@@ -1630,9 +1614,6 @@ function admin_page_users(App $a)
                if (in_array($new_order, $valid_orders)) {
                        $order = $new_order;
                }
-               if (x($_GET, 'd')) {
-                       $new_direction = $_GET['d'];
-               }
        }
        $sql_order = "`" . str_replace('.', '`.`', $order) . "`";
        $sql_order_direction = ($order_direction === "+") ? "ASC" : "DESC";
@@ -1776,11 +1757,11 @@ function admin_page_plugins(App $a)
                        if ($idx !== false) {
                                unset($a->plugins[$idx]);
                                uninstall_plugin($plugin);
-                               info(sprintf(t("Plugin %s disabled."), $plugin));
+                               info(t("Plugin %s disabled.", $plugin));
                        } else {
                                $a->plugins[] = $plugin;
                                install_plugin($plugin);
-                               info(sprintf(t("Plugin %s enabled."), $plugin));
+                               info(t("Plugin %s enabled.", $plugin));
                        }
                        Config::set("system", "addon", implode(", ", $a->plugins));
                        goaway('admin/plugins');
@@ -1788,8 +1769,6 @@ function admin_page_plugins(App $a)
                }
 
                // display plugin details
-               require_once('library/markdown.php');
-
                if (in_array($plugin, $a->plugins)) {
                        $status = "on";
                        $action = t("Disable");
@@ -1800,6 +1779,7 @@ function admin_page_plugins(App $a)
 
                $readme = Null;
                if (is_file("addon/$plugin/README.md")) {
+                       require_once 'library/markdown.php';
                        $readme = file_get_contents("addon/$plugin/README.md");
                        $readme = Markdown($readme, false);
                } elseif (is_file("addon/$plugin/README")) {
@@ -1807,7 +1787,7 @@ function admin_page_plugins(App $a)
                }
 
                $admin_form = "";
-               if (is_array($a->plugins_admin) && in_array($plugin, $a->plugins_admin)) {
+               if (in_array($plugin, $a->plugins_admin)) {
                        @require_once("addon/$plugin/$plugin.php");
                        $func = $plugin . '_plugin_admin';
                        $func($a, $admin_form);
@@ -1850,7 +1830,7 @@ function admin_page_plugins(App $a)
 
        $plugins = array();
        $files = glob("addon/*/");
-       if ($files) {
+       if (is_array($files)) {
                foreach ($files as $file) {
                        if (is_dir($file)) {
                                list($tmp, $id) = array_map("trim", explode("/", $file));
@@ -1884,7 +1864,7 @@ function admin_page_plugins(App $a)
                '$function' => 'plugins',
                '$plugins' => $plugins,
                '$pcount' => count($plugins),
-               '$noplugshint' => sprintf(t('There are currently no plugins available on your node. You can find the official plugin repository at %1$s and might find other interesting plugins in the open plugin registry at %2$s'), 'https://github.com/friendica/friendica-addons', 'http://addons.friendi.ca'),
+               '$noplugshint' => t('There are currently no plugins available on your node. You can find the official plugin repository at %1$s and might find other interesting plugins in the open plugin registry at %2$s', 'https://github.com/friendica/friendica-addons', 'http://addons.friendi.ca'),
                '$form_security_token' => get_form_security_token("admin_themes"),
        ));
 }
@@ -1896,7 +1876,8 @@ function admin_page_plugins(App $a)
  */
 function toggle_theme(&$themes, $th, &$result)
 {
-       for ($x = 0; $x < count($themes); $x ++) {
+       $count = count($themes);
+       for ($x = 0; $x < $count; $x ++) {
                if ($themes[$x]['name'] === $th) {
                        if ($themes[$x]['allowed']) {
                                $themes[$x]['allowed'] = 0;
@@ -1916,7 +1897,8 @@ function toggle_theme(&$themes, $th, &$result)
  */
 function theme_status($themes, $th)
 {
-       for ($x = 0; $x < count($themes); $x ++) {
+       $count = count($themes);
+       for ($x = 0; $x < $count; $x ++) {
                if ($themes[$x]['name'] === $th) {
                        if ($themes[$x]['allowed']) {
                                return 1;
@@ -1979,7 +1961,7 @@ function admin_page_themes(App $a)
 
        $themes = array();
        $files = glob('view/theme/*');
-       if ($files) {
+       if (is_array($files)) {
                foreach ($files as $file) {
                        $f = basename($file);
 
@@ -2038,8 +2020,6 @@ function admin_page_themes(App $a)
                }
 
                // display theme details
-               require_once 'library/markdown.php';
-
                if (theme_status($themes, $theme)) {
                        $status = "on";
                        $action = t("Disable");
@@ -2048,41 +2028,36 @@ function admin_page_themes(App $a)
                        $action = t("Enable");
                }
 
-               $readme = Null;
+               $readme = null;
                if (is_file("view/theme/$theme/README.md")) {
+                       require_once 'library/markdown.php';
                        $readme = file_get_contents("view/theme/$theme/README.md");
                        $readme = Markdown($readme, false);
                } elseif (is_file("view/theme/$theme/README")) {
                        $readme = "<pre>" . file_get_contents("view/theme/$theme/README") . "</pre>";
                }
 
-               $admin_form = "";
+               $admin_form = '';
                if (is_file("view/theme/$theme/config.php")) {
+                       $orig_theme = $a->theme;
+                       $orig_page = $a->page;
+                       $orig_session_theme = $_SESSION['theme'];
+                       require_once "view/theme/$theme/theme.php";
+                       require_once "view/theme/$theme/config.php";
+                       $_SESSION['theme'] = $theme;
+
+                       $init = $theme . "_init";
+                       if (function_exists($init)) {
+                               $init($a);
+                       }
 
-                       function __get_theme_admin_form(App $a, $theme)
-                       {
-                               $orig_theme = $a->theme;
-                               $orig_page = $a->page;
-                               $orig_session_theme = $_SESSION['theme'];
-                               require_once("view/theme/$theme/theme.php");
-                               require_once("view/theme/$theme/config.php");
-                               $_SESSION['theme'] = $theme;
-
-
-                               $init = $theme . "_init";
-                               if (function_exists($init)) {
-                                       $init($a);
-                               }
-                               if (function_exists("theme_admin")) {
-                                       $admin_form = theme_admin($a);
-                               }
-
-                               $_SESSION['theme'] = $orig_session_theme;
-                               $a->theme = $orig_theme;
-                               $a->page = $orig_page;
-                               return $admin_form;
+                       if (function_exists('theme_admin')) {
+                               $admin_form = theme_admin($a);
                        }
-                       $admin_form = __get_theme_admin_form($a, $theme);
+
+                       $_SESSION['theme'] = $orig_session_theme;
+                       $a->theme = $orig_theme;
+                       $a->page = $orig_page;
                }
 
                $screenshot = array(get_theme_screenshot($theme), t('Screenshot'));
@@ -2116,12 +2091,10 @@ function admin_page_themes(App $a)
        // reload active themes
        if (x($_GET, "a") && $_GET['a'] == "r") {
                check_form_security_token_redirectOnErr(System::baseUrl() . '/admin/themes', 'admin_themes', 't');
-               if ($themes) {
-                       foreach ($themes as $th) {
-                               if ($th['allowed']) {
-                                       uninstall_theme($th['name']);
-                                       install_theme($th['name']);
-                               }
+               foreach ($themes as $th) {
+                       if ($th['allowed']) {
+                               uninstall_theme($th['name']);
+                               install_theme($th['name']);
                        }
                }
                info("Themes reloaded");
@@ -2132,11 +2105,9 @@ function admin_page_themes(App $a)
         * List themes
         */
 
-       $xthemes = array();
-       if ($themes) {
-               foreach ($themes as $th) {
-                       $xthemes[] = array($th['name'], (($th['allowed']) ? "on" : "off"), get_theme_info($th['name']));
-               }
+       $plugins = array();
+       foreach ($themes as $th) {
+               $plugins[] = array($th['name'], (($th['allowed']) ? "on" : "off"), get_theme_info($th['name']));
        }
 
        $t = get_markup_template('admin/plugins.tpl');
@@ -2147,9 +2118,9 @@ function admin_page_themes(App $a)
                '$reload'              => t('Reload active themes'),
                '$baseurl'             => System::baseUrl(true),
                '$function'            => 'themes',
-               '$plugins'             => $xthemes,
+               '$plugins'             => $plugins,
                '$pcount'              => count($themes),
-               '$noplugshint'         => sprintf(t('No themes found on the system. They should be paced in %1$s'),'<code>/view/themes</code>'),
+               '$noplugshint'         => t('No themes found on the system. They should be placed in %1$s', '<code>/view/themes</code>'),
                '$experimental'        => t('[Experimental]'),
                '$unsupported'         => t('[Unsupported]'),
                '$form_security_token' => get_form_security_token("admin_themes"),
@@ -2300,7 +2271,6 @@ function admin_page_features_post(App $a)
 
        logger('postvars: ' . print_r($_POST, true), LOGGER_DATA);
 
-       $arr = array();
        $features = Feature::get(false);
 
        foreach ($features as $fname => $fdata) {
@@ -2355,13 +2325,13 @@ function admin_page_features(App $a)
                                $set = Config::get('feature', $f[0], $f[3]);
                                $arr[$fname][1][] = array(
                                        array('feature_' . $f[0], $f[1], $set, $f[2], array(t('Off'), t('On'))),
-                                       array('featurelock_' . $f[0], sprintf(t('Lock feature %s'), $f[1]), (($f[4] !== false) ? "1" : ''), '', array(t('Off'), t('On')))
+                                       array('featurelock_' . $f[0], t('Lock feature %s', $f[1]), (($f[4] !== false) ? "1" : ''), '', array(t('Off'), t('On')))
                                );
                        }
                }
 
                $tpl = get_markup_template('admin/settings_features.tpl');
-               $o .= replace_macros($tpl, array(
+               $o = replace_macros($tpl, array(
                        '$form_security_token' => get_form_security_token("admin_manage_features"),
                        '$title' => t('Manage Additional Features'),
                        '$features' => $arr,