From af2909bf8fa971c222bb7b0fca2ccb373c570988 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Tue, 20 Dec 2016 21:13:50 +0100 Subject: [PATCH] Continued with coding convention: - added curly braces around conditional code blocks - added space between if/foreach/... and brace - rewrote a code block so if dbm::is_result() fails it will abort, else the id is fetched from INSERT statement - made some SQL keywords upper-cased and added back-ticks to columns/table names Signed-off-by: Roland Haeder --- include/acl_selectors.php | 22 ++++++++++++-------- include/api.php | 4 ++-- include/bbcode.php | 2 +- include/contact_selectors.php | 2 +- include/contact_widgets.php | 8 ++++--- include/conversation.php | 2 +- include/datetime.php | 2 +- include/diaspora.php | 14 ++++++------- include/expire.php | 2 +- include/group.php | 6 +++--- include/identity.php | 6 +++--- include/message.php | 39 +++++++++++++++++++---------------- include/notifier.php | 4 ++-- include/plugin.php | 3 ++- include/pubsubpublish.php | 14 ++++++++----- include/queue.php | 13 ++++++------ include/text.php | 4 ++-- include/user.php | 2 +- 18 files changed, 82 insertions(+), 67 deletions(-) diff --git a/include/acl_selectors.php b/include/acl_selectors.php index c1edc8cc0..b2e66c98f 100644 --- a/include/acl_selectors.php +++ b/include/acl_selectors.php @@ -34,7 +34,7 @@ function group_select($selname,$selclass,$preselected = false,$size = 4) { call_hooks($a->module . '_pre_' . $selname, $arr); if (dbm::is_result($r)) { - foreach($r as $rr) { + foreach ($r as $rr) { if((is_array($preselected)) && in_array($rr['id'], $preselected)) $selected = " selected=\"selected\" "; else @@ -145,7 +145,7 @@ function contact_selector($selname, $selclass, $preselected = false, $options) { call_hooks($a->module . '_pre_' . $selname, $arr); if (dbm::is_result($r)) { - foreach($r as $rr) { + foreach ($r as $rr) { if((is_array($preselected)) && in_array($rr['id'], $preselected)) $selected = " selected=\"selected\" "; else @@ -221,16 +221,20 @@ function contact_select($selname, $selclass, $preselected = false, $size = 4, $p $receiverlist = array(); if (dbm::is_result($r)) { - foreach($r as $rr) { - if((is_array($preselected)) && in_array($rr['id'], $preselected)) + foreach ($r as $rr) { + if ((is_array($preselected)) && in_array($rr['id'], $preselected)) { $selected = " selected=\"selected\" "; - else + } + else { $selected = ''; + } - if($privmail) + if ($privmail) { $trimmed = GetProfileUsername($rr['url'], $rr['name'], false); - else + } + else { $trimmed = mb_substr($rr['name'],0,20); + } $receiverlist[] = $trimmed; @@ -260,7 +264,7 @@ function prune_deadguys($arr) { return $arr; $str = dbesc(implode(',',$arr)); $r = q("SELECT `id` FROM `contact` WHERE `id` IN ( " . $str . ") AND `blocked` = 0 AND `pending` = 0 AND `archive` = 0 "); - if($r) { + if ($r) { $ret = array(); foreach($r as $rr) $ret[] = intval($rr['id']); @@ -554,7 +558,7 @@ function acl_lookup(&$a, $out_type = 'json') { // autocomplete for global contact search (e.g. navbar search) $r = navbar_complete($a); $contacts = array(); - if($r) { + if ($r) { foreach($r as $g) { $contacts[] = array( "photo" => proxy_url($g['photo'], false, PROXY_SIZE_MICRO), diff --git a/include/api.php b/include/api.php index a450f867a..35efccf09 100644 --- a/include/api.php +++ b/include/api.php @@ -3068,8 +3068,8 @@ 'image/gif' => 'gif' ); $data = array('photo'=>array()); - if($r) { - foreach($r as $rr) { + if ($r) { + foreach ($r as $rr) { $photo = array(); $photo['id'] = $rr['resource-id']; $photo['album'] = $rr['album']; diff --git a/include/bbcode.php b/include/bbcode.php index c05173f47..74dde2fdf 100644 --- a/include/bbcode.php +++ b/include/bbcode.php @@ -343,7 +343,7 @@ function bb_replace_images($body, $images) { $newbody = $body; $cnt = 0; - foreach($images as $image) { + foreach ($images as $image) { // We're depending on the property of 'foreach' (specified on the PHP website) that // it loops over the array starting from the first element and going sequentially // to the last element diff --git a/include/contact_selectors.php b/include/contact_selectors.php index 0790e503e..ec9dff61d 100644 --- a/include/contact_selectors.php +++ b/include/contact_selectors.php @@ -13,7 +13,7 @@ function contact_profile_assign($current,$foreign_net) { intval($_SESSION['uid'])); if (dbm::is_result($r)) { - foreach($r as $rr) { + foreach ($r as $rr) { $selected = (($rr['id'] == $current) ? " selected=\"selected\" " : ""); $o .= "\r\n"; } diff --git a/include/contact_widgets.php b/include/contact_widgets.php index 71a75d431..36675da87 100644 --- a/include/contact_widgets.php +++ b/include/contact_widgets.php @@ -97,9 +97,11 @@ function networks_widget($baseurl,$selected = '') { $nets = array(); if (dbm::is_result($r)) { require_once('include/contact_selectors.php'); - foreach($r as $rr) { - if($rr['network']) - $nets[] = array('ref' => $rr['network'], 'name' => network_to_name($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' )); + foreach ($r as $rr) { + /// @TODO If 'network' is not there, this triggers an E_NOTICE + if ($rr['network']) { + $nets[] = array('ref' => $rr['network'], 'name' => network_to_name($rr['network']), 'selected' => (($selected == $rr['network']) ? 'selected' : '' )); + } } } diff --git a/include/conversation.php b/include/conversation.php index ccfc070d4..36eded8e8 100644 --- a/include/conversation.php +++ b/include/conversation.php @@ -78,7 +78,7 @@ function item_redir_and_replace_images($body, $images, $cid) { $newbody .= $origbody; $cnt = 0; - foreach($images as $image) { + foreach ($images as $image) { // We're depending on the property of 'foreach' (specified on the PHP website) that // it loops over the array starting from the first element and going sequentially // to the last element diff --git a/include/datetime.php b/include/datetime.php index e88c274ab..a17c405dc 100644 --- a/include/datetime.php +++ b/include/datetime.php @@ -553,7 +553,7 @@ function update_contact_birthdays() { $r = q("SELECT * FROM contact WHERE `bd` != '' AND `bd` != '0000-00-00' AND SUBSTRING(`bd`,1,4) != `bdyear` "); if (dbm::is_result($r)) { - foreach($r as $rr) { + foreach ($r as $rr) { logger('update_contact_birthday: ' . $rr['bd']); diff --git a/include/diaspora.php b/include/diaspora.php index 3b4832e74..cfb624fdf 100644 --- a/include/diaspora.php +++ b/include/diaspora.php @@ -319,8 +319,8 @@ class diaspora { dbesc(NETWORK_DIASPORA), dbesc($msg["author"]) ); - if($r) { - foreach($r as $rr) { + if ($r) { + foreach ($r as $rr) { logger("delivering to: ".$rr["username"]); self::dispatch($rr,$msg); } @@ -806,7 +806,7 @@ class diaspora { dbesc($guid) ); - if($r) { + if ($r) { logger("message ".$guid." already exists for user ".$uid); return $r[0]["id"]; } @@ -1577,7 +1577,7 @@ class diaspora { dbesc($message_uri), intval($importer["uid"]) ); - if($r) { + if ($r) { logger("duplicate message already delivered.", LOGGER_DEBUG); return false; } @@ -2022,7 +2022,7 @@ class diaspora { FROM `item` WHERE `guid` = '%s' AND `visible` AND NOT `deleted` AND `body` != '' LIMIT 1", dbesc($guid)); - if($r) { + if ($r) { logger("reshared message ".$guid." already exists on system."); // Maybe it is already a reshared item? @@ -2623,7 +2623,7 @@ class diaspora { logger("transmit: ".$logid."-".$guid." returns: ".$return_code); - if(!$return_code || (($return_code == 503) && (stristr($a->get_curl_headers(), "retry-after")))) { + if (!$return_code || (($return_code == 503) && (stristr($a->get_curl_headers(), "retry-after")))) { logger("queue message"); $r = q("SELECT `id` FROM `queue` WHERE `cid` = %d AND `network` = '%s' AND `content` = '%s' AND `batch` = %d LIMIT 1", @@ -2632,7 +2632,7 @@ class diaspora { dbesc($slap), intval($public_batch) ); - if($r) { + if ($r) { logger("add_to_queue ignored - identical item already in queue"); } else { // queue message for redelivery diff --git a/include/expire.php b/include/expire.php index eca2b1c42..e3313a78b 100644 --- a/include/expire.php +++ b/include/expire.php @@ -40,7 +40,7 @@ function expire_run(&$argv, &$argc){ $r = q("SELECT `uid`,`username`,`expire` FROM `user` WHERE `expire` != 0"); if (dbm::is_result($r)) { - foreach($r as $rr) { + foreach ($r as $rr) { logger('Expire: ' . $rr['username'] . ' interval: ' . $rr['expire'], LOGGER_DEBUG); item_expire($rr['uid'],$rr['expire']); } diff --git a/include/group.php b/include/group.php index a2a55c444..6332c45da 100644 --- a/include/group.php +++ b/include/group.php @@ -53,7 +53,7 @@ function group_rmv($uid,$name) { $r = q("SELECT def_gid, allow_gid, deny_gid FROM user WHERE uid = %d LIMIT 1", intval($uid) ); - if($r) { + if ($r) { $user_info = $r[0]; $change = false; @@ -199,7 +199,7 @@ function mini_group_select($uid,$gid = 0, $label = "") { ); $grps[] = array('name' => '', 'id' => '0', 'selected' => ''); if (dbm::is_result($r)) { - foreach($r as $rr) { + foreach ($r as $rr) { $grps[] = array('name' => $rr['name'], 'id' => $rr['id'], 'selected' => (($gid == $rr['id']) ? 'true' : '')); } @@ -257,7 +257,7 @@ function group_side($every="contacts",$each="group",$editmode = "standard", $gro } if (dbm::is_result($r)) { - foreach($r as $rr) { + foreach ($r as $rr) { $selected = (($group_id == $rr['id']) ? ' group-selected' : ''); if ($editmode == "full") { diff --git a/include/identity.php b/include/identity.php index 380560228..35516efbe 100644 --- a/include/identity.php +++ b/include/identity.php @@ -294,7 +294,7 @@ function profile_sidebar($profile, $block = 0) { if (dbm::is_result($r)) { - foreach($r as $rr) { + foreach ($r as $rr) { $profile['menu']['entries'][] = array( 'photo' => $rr['thumb'], 'id' => $rr['id'], @@ -469,7 +469,7 @@ function get_birthdays() { $cids = array(); $istoday = false; - foreach($r as $rr) { + foreach ($r as $rr) { if(strlen($rr['name'])) $total ++; if((strtotime($rr['start'] . ' +00:00') < $now) && (strtotime($rr['finish'] . ' +00:00') > $now)) @@ -549,7 +549,7 @@ function get_events() { if (dbm::is_result($r)) { $now = strtotime('now'); $istoday = false; - foreach($r as $rr) { + foreach ($r as $rr) { if(strlen($rr['name'])) $total ++; diff --git a/include/message.php b/include/message.php index e5ebe6f91..5bd611f22 100644 --- a/include/message.php +++ b/include/message.php @@ -130,12 +130,13 @@ function send_message($recipient=0, $body='', $subject='', $replyto=''){ $match = null; - if(preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) { + if (preg_match_all("/\[img\](.*?)\[\/img\]/",$body,$match)) { $images = $match[1]; - if(count($images)) { - foreach($images as $image) { - if(! stristr($image,App::get_baseurl() . '/photo/')) + if (count($images)) { + foreach ($images as $image) { + if (! stristr($image,App::get_baseurl() . '/photo/')) { continue; + } $image_uri = substr($image,strrpos($image,'/') + 1); $image_uri = substr($image_uri,0, strpos($image_uri,'-')); $r = q("UPDATE `photo` SET `allow_cid` = '%s' @@ -149,25 +150,25 @@ function send_message($recipient=0, $body='', $subject='', $replyto=''){ } } - if($post_id) { + if ($post_id) { proc_run(PRIORITY_HIGH, "include/notifier.php", "mail", $post_id); return intval($post_id); - } else { + } + else { return -3; } } - - - - function send_wallmessage($recipient='', $body='', $subject='', $replyto=''){ - if(! $recipient) return -1; + if (! $recipient) { + return -1; + } - if(! strlen($subject)) + if (! strlen($subject)) { $subject = t('[no subject]'); + } $guid = get_guid(32); $uri = 'urn:X-dfrn:' . App::get_baseurl() . ':' . local_user() . ':' . $guid; @@ -179,8 +180,9 @@ function send_wallmessage($recipient='', $body='', $subject='', $replyto=''){ $me = probe_url($replyto); - if(! $me['name']) + if (! $me['name']) { return -2; + } $conv_guid = get_guid(32); @@ -193,7 +195,7 @@ function send_wallmessage($recipient='', $body='', $subject='', $replyto=''){ $handles = $recip_handle . ';' . $sender_handle; - $r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ", + $r = q("INSERT INTO `conv` (`uid`,`guid`,`creator`,`created`,`updated`,`subject`,`recips`) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ", intval($recipient['uid']), dbesc($conv_guid), dbesc($sender_handle), @@ -203,18 +205,19 @@ function send_wallmessage($recipient='', $body='', $subject='', $replyto=''){ dbesc($handles) ); - $r = q("select * from conv where guid = '%s' and uid = %d limit 1", + $r = q("SELECT * FROM `conv` WHERE `guid` = '%s' AND `uid` = %d LIMIT 1", dbesc($conv_guid), intval($recipient['uid']) ); - if (dbm::is_result($r)) - $convid = $r[0]['id']; - if(! $convid) { + + if (! dbm::is_result($r)) { logger('send message: conversation not found.'); return -4; } + $convid = $r[0]['id']; + $r = q("INSERT INTO `mail` ( `uid`, `guid`, `convid`, `from-name`, `from-photo`, `from-url`, `contact-id`, `title`, `body`, `seen`, `reply`, `replied`, `uri`, `parent-uri`, `created`, `unknown`) VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, '%s', '%s', %d, %d, %d, '%s', '%s', '%s', %d )", diff --git a/include/notifier.php b/include/notifier.php index d78db4055..72387c98d 100644 --- a/include/notifier.php +++ b/include/notifier.php @@ -598,7 +598,7 @@ function notifier_run(&$argv, &$argc){ // throw everything into the queue in case we get killed - foreach($r as $rr) { + foreach ($r as $rr) { if((! $mail) && (! $fsuggest) && (! $followup)) { q("INSERT INTO `deliverq` (`cmd`,`item`,`contact`) VALUES ('%s', %d, %d) ON DUPLICATE KEY UPDATE `cmd` = '%s', `item` = %d, `contact` = %d", @@ -608,7 +608,7 @@ function notifier_run(&$argv, &$argc){ } } - foreach($r as $rr) { + foreach ($r as $rr) { // except for Diaspora batch jobs // Don't deliver to folks who have already been delivered to diff --git a/include/plugin.php b/include/plugin.php index 89c783f90..0b9c0166a 100644 --- a/include/plugin.php +++ b/include/plugin.php @@ -187,8 +187,9 @@ function load_hooks() { $a = get_app(); $a->hooks = array(); $r = q("SELECT * FROM `hook` WHERE 1 ORDER BY `priority` DESC, `file`"); + if (dbm::is_result($r)) { - foreach($r as $rr) { + foreach ($r as $rr) { if(! array_key_exists($rr['hook'],$a->hooks)) $a->hooks[$rr['hook']] = array(); $a->hooks[$rr['hook']][] = array($rr['file'],$rr['function']); diff --git a/include/pubsubpublish.php b/include/pubsubpublish.php index abf973a28..6bd90bfc2 100644 --- a/include/pubsubpublish.php +++ b/include/pubsubpublish.php @@ -76,16 +76,19 @@ function pubsubpublish_run(&$argv, &$argc){ load_config('system'); // Don't check this stuff if the function is called by the poller - if (App::callstack() != "poller_run") - if (App::is_already_running("pubsubpublish", "include/pubsubpublish.php", 540)) + if (App::callstack() != "poller_run") { + if (App::is_already_running("pubsubpublish", "include/pubsubpublish.php", 540)) { return; + } + } $a->set_baseurl(get_config('system','url')); load_hooks(); - if($argc > 1) + if ($argc > 1) { $pubsubpublish_id = intval($argv[1]); + } else { // We'll push to each subscriber that has push > 0, // i.e. there has been an update (set in notifier.php). @@ -95,10 +98,11 @@ function pubsubpublish_run(&$argv, &$argc){ $interval = Config::get("system", "delivery_interval", 2); // If we are using the worker we don't need a delivery interval - if (get_config("system", "worker")) + if (get_config("system", "worker")) { $interval = false; + } - foreach($r as $rr) { + foreach ($r as $rr) { logger("Publish feed to ".$rr["callback_url"], LOGGER_DEBUG); proc_run(PRIORITY_HIGH, 'include/pubsubpublish.php', $rr["id"]); diff --git a/include/queue.php b/include/queue.php index 1cc2ee095..2ef97fecd 100644 --- a/include/queue.php +++ b/include/queue.php @@ -58,20 +58,21 @@ function queue_run(&$argv, &$argc){ $interval = false; $r = q("select * from deliverq where 1"); - if($r) { - foreach($r as $rr) { + if ($r) { + foreach ($r as $rr) { logger('queue: deliverq'); proc_run(PRIORITY_HIGH,'include/delivery.php',$rr['cmd'],$rr['item'],$rr['contact']); - if($interval) - @time_sleep_until(microtime(true) + (float) $interval); + if($interval) { + time_sleep_until(microtime(true) + (float) $interval); + } } } $r = q("SELECT `queue`.*, `contact`.`name`, `contact`.`uid` FROM `queue` INNER JOIN `contact` ON `queue`.`cid` = `contact`.`id` WHERE `queue`.`created` < UTC_TIMESTAMP() - INTERVAL 3 DAY"); - if($r) { - foreach($r as $rr) { + if ($r) { + foreach ($r as $rr) { logger('Removing expired queue item for ' . $rr['name'] . ', uid=' . $rr['uid']); logger('Expired queue data :' . $rr['content'], LOGGER_DATA); } diff --git a/include/text.php b/include/text.php index 8d3b6a805..6672b0d32 100644 --- a/include/text.php +++ b/include/text.php @@ -912,7 +912,7 @@ function contact_block() { if (dbm::is_result($r)) { $contacts = sprintf( tt('%d Contact','%d Contacts', $total),$total); $micropro = Array(); - foreach($r as $rr) { + foreach ($r as $rr) { $micropro[] = micropro($rr,true,'mpfriend'); } } @@ -1717,7 +1717,7 @@ function bb_translate_video($s) { $matches = null; $r = preg_match_all("/\[video\](.*?)\[\/video\]/ism",$s,$matches,PREG_SET_ORDER); - if($r) { + if ($r) { foreach($matches as $mtch) { if((stristr($mtch[1],'youtube')) || (stristr($mtch[1],'youtu.be'))) $s = str_replace($mtch[0],'[youtube]' . $mtch[1] . '[/youtube]',$s); diff --git a/include/user.php b/include/user.php index d6970d475..df871c546 100644 --- a/include/user.php +++ b/include/user.php @@ -216,7 +216,7 @@ function create_user($arr) { dbesc($default_service_class) ); - if($r) { + if ($r) { $r = q("SELECT * FROM `user` WHERE `username` = '%s' AND `password` = '%s' LIMIT 1", dbesc($username),