diff --git a/boot.php b/boot.php index f3d1f6f649..9d72863cc8 100644 --- a/boot.php +++ b/boot.php @@ -341,12 +341,13 @@ function get_app() /** * @brief Multi-purpose function to check variable state. * - * Usage: x($var) or $x($array, 'key') + * Usage: x($var) or x($array, 'key') * * returns false if variable/key is not set * if variable is set, returns 1 if has 'non-zero' value, otherwise returns 0. * e.g. x('') or x(0) returns 0; * + * @deprecated since version 2018.12 * @param string|array $s variable to check * @param string $k key inside the array to check * @@ -383,13 +384,12 @@ function x($s, $k = null) * - defaults($var, $default) * - defaults($array, 'key', $default) * + * @param array $args * @brief Returns a defaut value if the provided variable or array key is falsy - * @see x() * @return mixed */ -function defaults() { - $args = func_get_args(); - +function defaults(...$args) +{ if (count($args) < 2) { throw new BadFunctionCallException('defaults() requires at least 2 parameters'); } @@ -400,16 +400,15 @@ function defaults() { throw new BadFunctionCallException('defaults($arr, $key, $def) $key is null'); } - $default = array_pop($args); + // The default value always is the last argument + $return = array_pop($args); - if (call_user_func_array('x', $args)) { - if (count($args) === 1) { - $return = $args[0]; - } else { - $return = $args[0][$args[1]]; - } - } else { - $return = $default; + if (count($args) == 2 && is_array($args[0]) && !empty($args[0][$args[1]])) { + $return = $args[0][$args[1]]; + } + + if (count($args) == 1 && !empty($args[0])) { + $return = $args[0]; } return $return; @@ -446,15 +445,15 @@ function public_contact() { static $public_contact_id = false; - if (!$public_contact_id && x($_SESSION, 'authenticated')) { - if (x($_SESSION, 'my_address')) { + if (!$public_contact_id && !empty($_SESSION['authenticated'])) { + if (!empty($_SESSION['my_address'])) { // Local user $public_contact_id = intval(Contact::getIdForURL($_SESSION['my_address'], 0, true)); - } elseif (x($_SESSION, 'visitor_home')) { + } elseif (!empty($_SESSION['visitor_home'])) { // Remote user $public_contact_id = intval(Contact::getIdForURL($_SESSION['visitor_home'], 0, true)); } - } elseif (!x($_SESSION, 'authenticated')) { + } elseif (empty($_SESSION['authenticated'])) { $public_contact_id = false; } @@ -479,7 +478,7 @@ function remote_user() return false; } - if (x($_SESSION, 'authenticated') && x($_SESSION, 'visitor_id')) { + if (!empty($_SESSION['authenticated']) && !empty($_SESSION['visitor_id'])) { return intval($_SESSION['visitor_id']); } return false; @@ -499,7 +498,7 @@ function notice($s) } $a = get_app(); - if (!x($_SESSION, 'sysmsg')) { + if (empty($_SESSION['sysmsg'])) { $_SESSION['sysmsg'] = []; } if ($a->interactive) { @@ -522,7 +521,7 @@ function info($s) return; } - if (!x($_SESSION, 'sysmsg_info')) { + if (empty($_SESSION['sysmsg_info'])) { $_SESSION['sysmsg_info'] = []; } if ($a->interactive) { diff --git a/doc/themes.md b/doc/themes.md index d0d74a92c6..d2e4c59be3 100644 --- a/doc/themes.md +++ b/doc/themes.md @@ -181,13 +181,13 @@ Next take the default.php file found in the /view direcotry and exchange the asi So the central part of the file now looks like this:
- - -' . L10n::t('Current timezone: %s', $_REQUEST['timezone']) . '
'; } - if (x($a->data, 'mod-localtime')) { + if (!empty($a->data['mod-localtime'])) { $o .= '' . L10n::t('Converted localtime: %s', $a->data['mod-localtime']) . '
'; } diff --git a/mod/manage.php b/mod/manage.php index f92a945490..b42b990aad 100644 --- a/mod/manage.php +++ b/mod/manage.php @@ -21,7 +21,7 @@ function manage_post(App $a) { $uid = local_user(); $orig_record = $a->user; - if((x($_SESSION,'submanage')) && intval($_SESSION['submanage'])) { + if(!empty($_SESSION['submanage'])) { $r = q("select * from user where uid = %d limit 1", intval($_SESSION['submanage']) ); @@ -37,7 +37,7 @@ function manage_post(App $a) { $submanage = $r; - $identity = (x($_POST['identity']) ? intval($_POST['identity']) : 0); + $identity = (!empty($_POST['identity']) ? intval($_POST['identity']) : 0); if (!$identity) { return; } @@ -101,13 +101,13 @@ function manage_post(App $a) { unset($_SESSION['mobile-theme']); unset($_SESSION['page_flags']); unset($_SESSION['return_path']); - if (x($_SESSION, 'submanage')) { + if (!empty($_SESSION['submanage'])) { unset($_SESSION['submanage']); } - if (x($_SESSION, 'sysmsg')) { + if (!empty($_SESSION['sysmsg'])) { unset($_SESSION['sysmsg']); } - if (x($_SESSION, 'sysmsg_info')) { + if (!empty($_SESSION['sysmsg_info'])) { unset($_SESSION['sysmsg_info']); } diff --git a/mod/message.php b/mod/message.php index 7491cd1bcc..e0d9f4404f 100644 --- a/mod/message.php +++ b/mod/message.php @@ -59,10 +59,10 @@ function message_post(App $a) return; } - $replyto = x($_REQUEST, 'replyto') ? Strings::escapeTags(trim($_REQUEST['replyto'])) : ''; - $subject = x($_REQUEST, 'subject') ? Strings::escapeTags(trim($_REQUEST['subject'])) : ''; - $body = x($_REQUEST, 'body') ? Strings::escapeHtml(trim($_REQUEST['body'])) : ''; - $recipient = x($_REQUEST, 'messageto') ? intval($_REQUEST['messageto']) : 0; + $replyto = !empty($_REQUEST['replyto']) ? Strings::escapeTags(trim($_REQUEST['replyto'])) : ''; + $subject = !empty($_REQUEST['subject']) ? Strings::escapeTags(trim($_REQUEST['subject'])) : ''; + $body = !empty($_REQUEST['body']) ? Strings::escapeHtml(trim($_REQUEST['body'])) : ''; + $recipient = !empty($_REQUEST['messageto']) ? intval($_REQUEST['messageto']) : 0; $ret = Mail::send($recipient, $body, $subject, $replyto); $norecip = false; @@ -253,8 +253,8 @@ function message_content(App $a) '$prefill' => $prefill, '$preid' => $preid, '$subject' => L10n::t('Subject:'), - '$subjtxt' => x($_REQUEST, 'subject') ? strip_tags($_REQUEST['subject']) : '', - '$text' => x($_REQUEST, 'body') ? Strings::escapeHtml(htmlspecialchars($_REQUEST['body'])) : '', + '$subjtxt' => !empty($_REQUEST['subject']) ? strip_tags($_REQUEST['subject']) : '', + '$text' => !empty($_REQUEST['body']) ? Strings::escapeHtml(htmlspecialchars($_REQUEST['body'])) : '', '$readonly' => '', '$yourmessage' => L10n::t('Your message:'), '$select' => $select, diff --git a/mod/modexp.php b/mod/modexp.php index 966c111d56..5819268e9d 100644 --- a/mod/modexp.php +++ b/mod/modexp.php @@ -28,7 +28,7 @@ function modexp_init(App $a) { $e = $r[0]->asnData[1]->asnData[0]->asnData[1]->asnData; header("Content-type: application/magic-public-key"); - echo 'RSA' . '.' . $m . '.' . $e ; + echo 'RSA' . '.' . $m . '.' . $e; killme(); diff --git a/mod/network.php b/mod/network.php index 594a557997..52daa2eefe 100644 --- a/mod/network.php +++ b/mod/network.php @@ -42,19 +42,19 @@ function network_init(App $a) Hook::add('head', __FILE__, 'network_infinite_scroll_head'); - $search = (x($_GET, 'search') ? Strings::escapeHtml($_GET['search']) : ''); + $search = (!empty($_GET['search']) ? Strings::escapeHtml($_GET['search']) : ''); if (($search != '') && !empty($_GET['submit'])) { $a->internalRedirect('search?search=' . urlencode($search)); } - if (x($_GET, 'save')) { + if (!empty($_GET['save'])) { $exists = DBA::exists('search', ['uid' => local_user(), 'term' => $search]); if (!$exists) { DBA::insert('search', ['uid' => local_user(), 'term' => $search]); } } - if (x($_GET, 'remove')) { + if (!empty($_GET['remove'])) { DBA::delete('search', ['uid' => local_user(), 'term' => $search]); } @@ -63,7 +63,7 @@ function network_init(App $a) $group_id = (($a->argc > 1 && is_numeric($a->argv[1])) ? intval($a->argv[1]) : 0); $cid = 0; - if (x($_GET, 'cid') && intval($_GET['cid']) != 0) { + if (!empty($_GET['cid'])) { $cid = $_GET['cid']; $_GET['nets'] = 'all'; $group_id = 0; @@ -152,33 +152,33 @@ function network_init(App $a) } // If nets is set to all, unset it - if (x($_GET, 'nets') && $_GET['nets'] === 'all') { + if (!empty($_GET['nets']) && $_GET['nets'] === 'all') { unset($_GET['nets']); } - if (!x($a->page, 'aside')) { + if (empty($a->page['aside'])) { $a->page['aside'] = ''; } $a->page['aside'] .= Group::sidebarWidget('network/0', 'network', 'standard', $group_id); $a->page['aside'] .= ForumManager::widget(local_user(), $cid); $a->page['aside'] .= posted_date_widget('network', local_user(), false); - $a->page['aside'] .= Widget::networks('network', (x($_GET, 'nets') ? $_GET['nets'] : '')); + $a->page['aside'] .= Widget::networks('network', defaults($_GET, 'nets', '') ); $a->page['aside'] .= saved_searches($search); - $a->page['aside'] .= Widget::fileAs('network', (x($_GET, 'file') ? $_GET['file'] : '')); + $a->page['aside'] .= Widget::fileAs('network', defaults($_GET, 'file', '') ); } function saved_searches($search) { $srchurl = '/network?f=' - . ((x($_GET, 'cid')) ? '&cid=' . rawurlencode($_GET['cid']) : '') - . ((x($_GET, 'star')) ? '&star=' . rawurlencode($_GET['star']) : '') - . ((x($_GET, 'bmark')) ? '&bmark=' . rawurlencode($_GET['bmark']) : '') - . ((x($_GET, 'conv')) ? '&conv=' . rawurlencode($_GET['conv']) : '') - . ((x($_GET, 'nets')) ? '&nets=' . rawurlencode($_GET['nets']) : '') - . ((x($_GET, 'cmin')) ? '&cmin=' . rawurlencode($_GET['cmin']) : '') - . ((x($_GET, 'cmax')) ? '&cmax=' . rawurlencode($_GET['cmax']) : '') - . ((x($_GET, 'file')) ? '&file=' . rawurlencode($_GET['file']) : ''); + . (!empty($_GET['cid']) ? '&cid=' . rawurlencode($_GET['cid']) : '') + . (!empty($_GET['star']) ? '&star=' . rawurlencode($_GET['star']) : '') + . (!empty($_GET['bmark']) ? '&bmark=' . rawurlencode($_GET['bmark']) : '') + . (!empty($_GET['conv']) ? '&conv=' . rawurlencode($_GET['conv']) : '') + . (!empty($_GET['nets']) ? '&nets=' . rawurlencode($_GET['nets']) : '') + . (!empty($_GET['cmin']) ? '&cmin=' . rawurlencode($_GET['cmin']) : '') + . (!empty($_GET['cmax']) ? '&cmax=' . rawurlencode($_GET['cmax']) : '') + . (!empty($_GET['file']) ? '&file=' . rawurlencode($_GET['file']) : ''); ; $terms = DBA::select('search', ['id', 'term'], ['uid' => local_user()]); @@ -233,15 +233,15 @@ function network_query_get_sel_tab(App $a) $new_active = 'active'; } - if (x($_GET, 'star')) { + if (!empty($_GET['star'])) { $starred_active = 'active'; } - if (x($_GET, 'bmark')) { + if (!empty($_GET['bmark'])) { $bookmarked_active = 'active'; } - if (x($_GET, 'conv')) { + if (!empty($_GET['conv'])) { $conv_active = 'active'; } @@ -249,7 +249,7 @@ function network_query_get_sel_tab(App $a) $no_active = 'active'; } - if ($no_active == 'active' && x($_GET, 'order')) { + if ($no_active == 'active' && !empty($_GET['order'])) { switch($_GET['order']) { case 'post' : $postord_active = 'active'; $no_active=''; break; case 'comment' : $all_active = 'active'; $no_active=''; break; @@ -914,7 +914,7 @@ function networkThreadedView(App $a, $update, $parent) $parents_str = implode(', ', $parents_arr); } - if (x($_GET, 'offset')) { + if (!empty($_GET['offset'])) { $date_offset = $_GET['offset']; } @@ -968,7 +968,7 @@ function network_tabs(App $a) $tabs = [ [ 'label' => L10n::t('Commented Order'), - 'url' => str_replace('/new', '', $cmd) . '?f=&order=comment' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''), + 'url' => str_replace('/new', '', $cmd) . '?f=&order=comment' . (!empty($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''), 'sel' => $all_active, 'title' => L10n::t('Sort by Comment Date'), 'id' => 'commented-order-tab', @@ -976,7 +976,7 @@ function network_tabs(App $a) ], [ 'label' => L10n::t('Posted Order'), - 'url' => str_replace('/new', '', $cmd) . '?f=&order=post' . ((x($_GET,'cid')) ? '&cid=' . $_GET['cid'] : ''), + 'url' => str_replace('/new', '', $cmd) . '?f=&order=post' . (!empty($_GET['cid']) ? '&cid=' . $_GET['cid'] : ''), 'sel' => $postord_active, 'title' => L10n::t('Sort by Post Date'), 'id' => 'posted-order-tab', @@ -986,7 +986,7 @@ function network_tabs(App $a) $tabs[] = [ 'label' => L10n::t('Personal'), - 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&conv=1', + 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&conv=1', 'sel' => $conv_active, 'title' => L10n::t('Posts that mention or involve you'), 'id' => 'personal-tab', @@ -996,7 +996,7 @@ function network_tabs(App $a) if (Feature::isEnabled(local_user(), 'new_tab')) { $tabs[] = [ 'label' => L10n::t('New'), - 'url' => 'network/new' . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : ''), + 'url' => 'network/new' . (!empty($_GET['cid']) ? '/?f=&cid=' . $_GET['cid'] : ''), 'sel' => $new_active, 'title' => L10n::t('Activity Stream - by date'), 'id' => 'activitiy-by-date-tab', @@ -1007,7 +1007,7 @@ function network_tabs(App $a) if (Feature::isEnabled(local_user(), 'link_tab')) { $tabs[] = [ 'label' => L10n::t('Shared Links'), - 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1', + 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&bmark=1', 'sel' => $bookmarked_active, 'title' => L10n::t('Interesting Links'), 'id' => 'shared-links-tab', @@ -1017,7 +1017,7 @@ function network_tabs(App $a) $tabs[] = [ 'label' => L10n::t('Starred'), - 'url' => str_replace('/new', '', $cmd) . ((x($_GET,'cid')) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1', + 'url' => str_replace('/new', '', $cmd) . (!empty($_GET['cid']) ? '/?f=&cid=' . $_GET['cid'] : '/?f=') . '&star=1', 'sel' => $starred_active, 'title' => L10n::t('Favourite Posts'), 'id' => 'starred-posts-tab', @@ -1025,7 +1025,7 @@ function network_tabs(App $a) ]; // save selected tab, but only if not in file mode - if (!x($_GET, 'file')) { + if (empty($_GET['file'])) { PConfig::set(local_user(), 'network.view', 'tab.selected', [ $all_active, $postord_active, $conv_active, $new_active, $starred_active, $bookmarked_active ]); diff --git a/mod/noscrape.php b/mod/noscrape.php index 87367d5369..e1d51e5a80 100644 --- a/mod/noscrape.php +++ b/mod/noscrape.php @@ -48,7 +48,7 @@ function noscrape_init(App $a) exit; } - $keywords = ((x($a->profile, 'pub_keywords')) ? $a->profile['pub_keywords'] : ''); + $keywords = defaults($a->profile, 'pub_keywords', ''); $keywords = str_replace(['#',',',' ',',,'], ['',' ',',',','], $keywords); $keywords = explode(',', $keywords); diff --git a/mod/oexchange.php b/mod/oexchange.php index 62d0bba078..6d682d6adf 100644 --- a/mod/oexchange.php +++ b/mod/oexchange.php @@ -33,13 +33,13 @@ function oexchange_content(App $a) { return; } - $url = ((x($_REQUEST,'url') && strlen($_REQUEST['url'])) + $url = ((!empty($_REQUEST['url'])) ? urlencode(Strings::escapeTags(trim($_REQUEST['url']))) : ''); - $title = ((x($_REQUEST,'title') && strlen($_REQUEST['title'])) + $title = ((!empty($_REQUEST['title'])) ? '&title=' . urlencode(Strings::escapeTags(trim($_REQUEST['title']))) : ''); - $description = ((x($_REQUEST,'description') && strlen($_REQUEST['description'])) + $description = ((!empty($_REQUEST['description'])) ? '&description=' . urlencode(Strings::escapeTags(trim($_REQUEST['description']))) : ''); - $tags = ((x($_REQUEST,'tags') && strlen($_REQUEST['tags'])) + $tags = ((!empty($_REQUEST['tags'])) ? '&tags=' . urlencode(Strings::escapeTags(trim($_REQUEST['tags']))) : ''); $s = Network::fetchUrl(System::baseUrl() . '/parse_url?f=&url=' . $url . $title . $description . $tags); diff --git a/mod/openid.php b/mod/openid.php index 209960ee58..4e247b384f 100644 --- a/mod/openid.php +++ b/mod/openid.php @@ -20,7 +20,7 @@ function openid_content(App $a) { Logger::log('mod_openid ' . print_r($_REQUEST,true), Logger::DATA); - if((x($_GET,'openid_mode')) && (x($_SESSION,'openid'))) { + if(!empty($_GET['openid_mode']) && !empty($_SESSION['openid'])) { $openid = new LightOpenID($a->getHostName()); diff --git a/mod/parse_url.php b/mod/parse_url.php index a1bacb0d8f..07f319fdca 100644 --- a/mod/parse_url.php +++ b/mod/parse_url.php @@ -47,8 +47,8 @@ function parse_url_content(App $a) // Add url scheme if it is missing $arrurl = parse_url($url); - if (!x($arrurl, 'scheme')) { - if (x($arrurl, 'host')) { + if (empty($arrurl['scheme'])) { + if (!empty($arrurl['host'])) { $url = 'http:' . $url; } else { $url = 'http://' . $url; diff --git a/mod/photos.php b/mod/photos.php index 70e0e1882d..d1dffd4d05 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -283,7 +283,7 @@ function photos_post(App $a) if (DBA::isResult($r)) { foreach ($r as $rr) { - $res[] = "'" . DBA::escape($rr['rid']) . "'" ; + $res[] = "'" . DBA::escape($rr['rid']) . "'"; } } else { $a->internalRedirect($_SESSION['photo_return']); @@ -365,10 +365,10 @@ function photos_post(App $a) return; // NOTREACHED } - if ($a->argc > 2 && (!empty($_POST['desc']) || !empty($_POST['newtag']) || !empty($_POST['albname']) !== false)) { + if ($a->argc > 2 && (!empty($_POST['desc']) || !empty($_POST['newtag']) || isset($_POST['albname']))) { $desc = !empty($_POST['desc']) ? Strings::escapeTags(trim($_POST['desc'])) : ''; $rawtags = !empty($_POST['newtag']) ? Strings::escapeTags(trim($_POST['newtag'])) : ''; - $item_id = !empty($_POST['item_id']) ? intval($_POST['item_id']) : 0; + $item_id = !empty($_POST['item_id']) ? intval($_POST['item_id']) : 0; $albname = !empty($_POST['albname']) ? Strings::escapeTags(trim($_POST['albname'])) : ''; $origaname = !empty($_POST['origaname']) ? Strings::escapeTags(trim($_POST['origaname'])) : ''; @@ -681,8 +681,8 @@ function photos_post(App $a) $arr['tag'] = $tagged[4]; $arr['inform'] = $tagged[2]; $arr['origin'] = 1; - $arr['body'] = L10n::t('%1$s was tagged in %2$s by %3$s', '[url=' . $tagged[1] . ']' . $tagged[0] . '[/url]', '[url=' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . L10n::t('a photo') . '[/url]', '[url=' . $owner_record['url'] . ']' . $owner_record['name'] . '[/url]') ; - $arr['body'] .= "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . '[img]' . System::baseUrl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '[/img][/url]' . "\n" ; + $arr['body'] = L10n::t('%1$s was tagged in %2$s by %3$s', '[url=' . $tagged[1] . ']' . $tagged[0] . '[/url]', '[url=' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . L10n::t('a photo') . '[/url]', '[url=' . $owner_record['url'] . ']' . $owner_record['name'] . '[/url]'); + $arr['body'] .= "\n\n" . '[url=' . System::baseUrl() . '/photos/' . $owner_record['nickname'] . '/image/' . $p[0]['resource-id'] . ']' . '[img]' . System::baseUrl() . "/photo/" . $p[0]['resource-id'] . '-' . $best . '.' . $ext . '[/img][/url]' . "\n"; $arr['object'] = '