Fix PHP notices all around

- Improve SQL query formatting
- Revert spaces to tabs after PHP CS Fixer
This commit is contained in:
Hypolite Petovan 2017-04-20 21:41:47 -04:00
parent 750f081078
commit 783c15c207
5 changed files with 526 additions and 460 deletions

250
boot.php
View File

@ -34,9 +34,8 @@ define ( 'LOGGER_DATA', 3 );
define('LOGGER_ALL', 4);
if(! class_exists('App')) {
class App {
class App
{
public $module_loaded = false;
public $query_string;
public $config;
@ -61,8 +60,8 @@ class App {
private $baseurl;
private $db;
function __construct() {
public function __construct()
{
$this->config = array();
$this->page = array();
$this->pager= array();
@ -75,8 +74,9 @@ class App {
. PATH_SEPARATOR . 'include'
. PATH_SEPARATOR . '.');
if(substr($_SERVER['QUERY_STRING'],0,2) == "q=")
if (substr($_SERVER['QUERY_STRING'], 0, 2) == "q=") {
$_SERVER['QUERY_STRING'] = substr($_SERVER['QUERY_STRING'], 2);
}
$this->query_string = $_SERVER['QUERY_STRING'];
@ -87,8 +87,7 @@ class App {
$this->argc = count($this->argv);
if ((array_key_exists('0', $this->argv)) && strlen($this->argv[0])) {
$this->module = $this->argv[0];
}
else {
} else {
$this->module = 'directory';
}
@ -98,9 +97,11 @@ class App {
$this->pager['total'] = 0;
}
function get_baseurl($ssl = false) {
if(strlen($this->baseurl))
public function get_baseurl($ssl = false)
{
if (strlen($this->baseurl)) {
return $this->baseurl;
}
$this->baseurl = (($ssl) ? 'https' : $this->scheme) . "://" . $this->hostname
. ((isset($this->path) && strlen($this->path))
@ -108,59 +109,67 @@ class App {
return $this->baseurl;
}
function set_baseurl($url) {
public function set_baseurl($url)
{
$this->baseurl = $url;
$this->hostname = basename($url);
}
function get_hostname() {
public function get_hostname()
{
return $this->hostname;
}
function set_hostname($h) {
public function set_hostname($h)
{
$this->hostname = $h;
}
function set_path($p) {
public function set_path($p)
{
$this->path = ltrim(trim($p), '/');
}
function get_path() {
public function get_path()
{
return $this->path;
}
function set_pager_total($n) {
public function set_pager_total($n)
{
$this->pager['total'] = intval($n);
}
function set_pager_itemspage($n) {
public function set_pager_itemspage($n)
{
$this->pager['itemspage'] = intval($n);
$this->pager['start'] = ($this->pager['page'] * $this->pager['itemspage']) - $this->pager['itemspage'];
}
function init_pagehead() {
if(file_exists("view/head.tpl"))
public function init_pagehead()
{
if (file_exists("view/head.tpl")) {
$s = file_get_contents("view/head.tpl");
}
$this->page['htmlhead'] = replace_macros($s, array(
'$baseurl' => $this->get_baseurl()
));
}
}}
}
if (! function_exists('x')) {
function x($s,$k = NULL) {
if($k != NULL) {
function x($s, $k = null)
{
if ($k != null) {
if ((is_array($s)) && (array_key_exists($k, $s))) {
if($s[$k])
if ($s[$k]) {
return (int) 1;
}
return (int) 0;
}
return false;
}
else {
} else {
if (isset($s)) {
if ($s) {
return (int) 1;
@ -169,32 +178,38 @@ function x($s,$k = NULL) {
}
return false;
}
}}
}
}
if (! function_exists('system_unavailable')) {
function system_unavailable() {
function system_unavailable()
{
include('system_unavailable.php');
killme();
}}
}
}
if (! function_exists('logger')) {
function logger($msg,$level = 0) {
function logger($msg, $level = 0)
{
$debugging = 1;
$loglevel = LOGGER_ALL;
$logfile = 'logfile.out';
if((! $debugging) || (! $logfile) || ($level > $loglevel))
if ((! $debugging) || (! $logfile) || ($level > $loglevel)) {
return;
}
require_once('include/datetime.php');
@file_put_contents($logfile, datetime_convert() . ':' . ' ' . $msg . "\n", FILE_APPEND);
return;
}}
}
}
if (! function_exists('replace_macros')) {
function replace_macros($s,$r) {
function replace_macros($s, $r)
{
$search = array();
$replace = array();
@ -205,50 +220,63 @@ function replace_macros($s,$r) {
}
}
return str_replace($search, $replace, $s);
}}
}
}
if (! function_exists('load_translation_table')) {
function load_translation_table($lang) {
function load_translation_table($lang)
{
global $a;
}}
}
}
if (! function_exists('t')) {
function t($s) {
function t($s)
{
global $a;
if($a->strings[$s])
if ($a->strings[$s]) {
return $a->strings[$s];
}
return $s;
}}
}
}
if (! function_exists('fetch_url')) {
function fetch_url($url,$binary = false, $timeout=20) {
function fetch_url($url, $binary = false, $timeout=20)
{
$ch = curl_init($url);
if(! $ch) return false;
if (! $ch) {
return false;
}
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, max(intval($timeout), 1)); //Minimum of 1 second timeout.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 8);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if($binary)
if ($binary) {
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$s = curl_exec($ch);
curl_close($ch);
return($s);
}}
}
}
if (! function_exists('post_url')) {
function post_url($url,$params) {
function post_url($url, $params)
{
$ch = curl_init($url);
if(! $ch) return false;
if (! $ch) {
return false;
}
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
@ -259,84 +287,104 @@ function post_url($url,$params) {
$s = curl_exec($ch);
curl_close($ch);
return($s);
}}
}
}
if (! function_exists('random_string')) {
function random_string() {
function random_string()
{
return(hash('sha256', uniqid(rand(), true)));
}}
}
}
if (! function_exists('notags')) {
function notags($string) {
function notags($string)
{
// protect against :<> with high-bit set
return(str_replace(array("<", ">", "\xBA", "\xBC", "\xBE"), array('[', ']', '', '', ''), $string));
}}
}
}
if (! function_exists('escape_tags')) {
function escape_tags($string) {
function escape_tags($string)
{
return(htmlspecialchars($string));
}}
}
}
if (! function_exists('login')) {
function login($register = false) {
function login($register = false)
{
$o = "";
$register_html = (($register) ? file_get_contents("view/register-link.tpl") : "");
if (x($_SESSION, 'authenticated')) {
$o = file_get_contents("view/logout.tpl");
}
else {
} else {
$o = file_get_contents("view/login.tpl");
$o = replace_macros($o, array('$register_html' => $register_html ));
}
return $o;
}}
}
}
if (! function_exists('killme')) {
function killme() {
function killme()
{
session_write_close();
closedb();
exit;
}}
}
}
if (! function_exists('goaway')) {
function goaway($s) {
function goaway($s)
{
header("Location: $s");
killme();
}}
}
}
if (! function_exists('local_user')) {
function local_user() {
if((x($_SESSION,'authenticated')) && (x($_SESSION,'uid')))
function local_user()
{
if ((x($_SESSION, 'authenticated')) && (x($_SESSION, 'uid'))) {
return $_SESSION['uid'];
}
return false;
}}
}
}
if (! function_exists('notice')) {
function notice($s) {
function notice($s)
{
if (!isset($_SESSION['sysmsg'])) {
$_SESSION['sysmsg'] = '';
}
$_SESSION['sysmsg'] .= $s;
}}
}
}
if (! function_exists('hex2bin')) {
function hex2bin($s) {
function hex2bin($s)
{
return(pack("H*", $s));
}}
}
}
if (! function_exists('paginate')) {
function paginate(&$a) {
function paginate(&$a)
{
$o = '';
$stripped = ereg_replace("(&page=[0-9]*)","",$a->query_string);
$stripped = preg_replace("/&page=[0-9]*/", "", $a->query_string);
$stripped = str_replace('q=', '', $stripped);
$stripped = trim($stripped, '/');
$pagenum = $a->pager['page'];
@ -345,8 +393,9 @@ function paginate(&$a) {
if ($a->pager['total'] > $a->pager['itemspage']) {
$o .= '<div class="pager">';
if($a->pager['page'] != 1)
if ($a->pager['page'] != 1) {
$o .= '<span class="pager_prev">'."<a href=\"$url".'&page='.($a->pager['page'] - 1).'">' . t('prev') . '</a></span> ';
}
$o .= "<span class=\"pager_first\"><a href=\"$url"."&page=1\">" . t('first') . "</a></span> ";
@ -361,59 +410,74 @@ function paginate(&$a) {
}
for ($i = $numstart; $i <= $numstop; $i++) {
if($i == $a->pager['page'])
if ($i == $a->pager['page']) {
$o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
else
} else {
$o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
}
$o .= '</span> ';
}
if (($a->pager['total'] % $a->pager['itemspage']) != 0) {
if($i == $a->pager['page'])
if ($i == $a->pager['page']) {
$o .= '<span class="pager_current">'.(($i < 10) ? '&nbsp;'.$i : $i);
else
} else {
$o .= "<span class=\"pager_n\"><a href=\"$url"."&page=$i\">".(($i < 10) ? '&nbsp;'.$i : $i)."</a>";
}
$o .= '</span> ';
}
$lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
$o .= "<span class=\"pager_last\"><a href=\"$url"."&page=$lastpage\">" . t('last') . "</a></span> ";
if(($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0)
if (($a->pager['total'] - ($a->pager['itemspage'] * $a->pager['page'])) > 0) {
$o .= '<span class="pager_next">'."<a href=\"$url"."&page=".($a->pager['page'] + 1).'">' . t('next') . '</a></span>';
}
$o .= '</div>'."\r\n";
}
return $o;
}}
}
}
function get_my_url() {
if(x($_SESSION,'my_url'))
function get_my_url()
{
if (x($_SESSION, 'my_url')) {
return $_SESSION['my_url'];
}
return false;
}
function zrl($s,$force = false) {
if(! strlen($s))
function zrl($s, $force = false)
{
if (! strlen($s)) {
return $s;
if((! strpos($s,'/profile/')) && (! $force))
}
if ((! strpos($s, '/profile/')) && (! $force)) {
return $s;
}
$achar = strpos($s, '?') ? '&' : '?';
$mine = get_my_url();
if($mine and ! link_compare($mine,$s))
if ($mine and ! link_compare($mine, $s)) {
return $s . $achar . 'zrl=' . urlencode($mine);
}
return $s;
}
if (! function_exists('link_compare')) {
function link_compare($a,$b) {
if(strcasecmp(normalise_link($a),normalise_link($b)) === 0)
function link_compare($a, $b)
{
if (strcasecmp(normalise_link($a), normalise_link($b)) === 0) {
return true;
}
return false;
}}
}
}
if (! function_exists('normalise_link')) {
function normalise_link($url) {
function normalise_link($url)
{
$ret = str_replace(array('https:', '//www.'), array('http:', '//'), $url);
return(rtrim($ret, '/'));
}}
}
}

View File

@ -21,7 +21,7 @@ $db = new dba($db_host, $db_user, $db_pass, $db_data);
unset($db_host, $db_user, $db_pass, $db_data);
$a->init_pagehead();
$a->page['aside'] .= '<div id="logo"><img src="images/friendica-32.png" alt="friendica logo" /> <a href="http://friendica.com">Friendica</a></div><div id="slogan">Your friends. Your web.</div>';
$a->page['aside'] = '<div id="logo"><img src="images/friendica-32.png" alt="friendica logo" /> <a href="http://friendica.com">Friendica</a></div><div id="slogan">Your friends. Your web.</div>';
require_once 'session.php';
@ -73,17 +73,19 @@ if ($a->module_loaded) {
if ((! $a->error) && (function_exists($a->module . '_content'))) {
$func = $a->module . '_content';
$a->page['content'] .= $func($a);
$a->page['content'] = $func($a);
}
}
if (stristr($_SESSION['sysmsg'], t('Permission denied'))) {
header($_SERVER['SERVER_PROTOCOL'] . ' 403 ' . t('Permission denied.'));
}
// report anything important happening
if (x($_SESSION, 'sysmsg')) {
if (stristr($_SESSION['sysmsg'], t('Permission denied'))) {
header($_SERVER['SERVER_PROTOCOL'] . ' 403 ' . t('Permission denied.'));
}
if (!isset($a->page['content'])) {
$a->page['content'] = '';
}
$a->page['content'] = '<div id="sysmsg" class="error-message">' . $_SESSION['sysmsg'] . '</div>' . PHP_EOL
. $a->page['content'];
unset($_SESSION['sysmsg']);

View File

@ -18,13 +18,13 @@ function directory_content(App $a)
}
$alpha = false;
if ($_GET['alpha'] == 1) {
if (isset($_GET['alpha']) && $_GET['alpha'] == 1) {
$alpha = true;
}
$search = ((x($_GET, 'search')) ? notags(trim($_GET['search'])) : '');
if ($_GET['submit'] === t('Clear')) {
if (isset($_GET['submit']) && $_GET['submit'] === t('Clear')) {
goaway($a->get_baseurl());
}
@ -32,9 +32,9 @@ function directory_content(App $a)
$alpha = true;
}
$tpl .= file_get_contents('view/directory_header.tpl');
$tpl = file_get_contents('view/directory_header.tpl');
$o .= replace_macros($tpl, array(
$o = replace_macros($tpl, array(
'$search' => $search,
'$header' => t('Global Directory'),
'$submit' => t('Find'),
@ -54,7 +54,7 @@ function directory_content(App $a)
AGAINST ('$search' IN BOOLEAN MODE) " : "");
if ($forums) {
$sql_extra .= " and comm = 1 ";
$sql_extra .= " AND `comm` = 1 ";
}
$sql_extra = str_replace('%', '%%', $sql_extra);
@ -65,9 +65,9 @@ function directory_content(App $a)
}
if ($alpha) {
$order = " order by name asc ";
$order = " ORDER BY `name` ASC ";
} else {
$order = " order by updated desc, id desc ";
$order = " ORDER BY `updated` DESC, `id` DESC ";
}
$r = q("SELECT * FROM `profile` WHERE `censored` = 0 $sql_extra $order LIMIT %d , %d ",

View File

@ -1,15 +1,15 @@
<!DOCTYPE html >
<html>
<head>
<title><?php echo $page['title'] ?></title>
<?php echo $page['htmlhead'] ?>
<title><?php echo isset($page['title']) ? $page['title'] : '' ?></title>
<?php echo isset($page['htmlhead']) ? $page['htmlhead'] : '' ?>
</head>
<body>
<header><?php echo $page['header']; ?></header>
<nav><?php echo $page['nav']; ?></nav>
<aside><?php echo $page['aside']; ?></aside>
<section><?php echo $page['content']; ?></section>
<footer><?php echo $page['footer']; ?></footer>
<header><?php echo isset($page['header']) ? $page['header'] : '' ?></header>
<nav><?php echo isset($page['nav']) ? $page['nav'] : '' ?></nav>
<aside><?php echo isset($page['aside']) ? $page['aside'] : '' ?></aside>
<section><?php echo isset($page['content']) ? $page['content'] : '' ?></section>
<footer><?php echo isset($page['footer']) ? $page['footer'] : '' ?></footer>
</body>
</html>