Why does Yii render "<?php" into every backend controller output? - php

Reproduceable problem
In 2.0.3 "advanced" (Yii offers basic and advanced setup of a project) create an empty controller in the backend, like
public function actionTest()
{
}
and call it in the browser. You'll see an empty page. When looking at the source code, you'll see a lonely php tag.
<?php
This looks like a bug to me, as it does not happen in the frontend folder, just in backend folder. This is critical when you render out CSV files or so, it will break the file.
I'm unsure if this is a bug on my side or a real bug inside the framework.

This is a bug in Yii 2.0.3!
... caused by a lower-level and not-so-obvious whitespace problem in naked PHP files.
Tickets & more information:
https://github.com/githubjeka/yii2-rest/issues/3
https://github.com/yiisoft/yii2-app-advanced/issues/24
http://php.net/manual/en/language.basic-syntax.phptags.php#116883
How to fix this:
The problem in a nutshell: A PHP tag is <?php[whitespace character], not just <?php. Holy! Totally makes sense, but let's be honest, who would have known this instantly ?
Yii 2.x uses (in the advanced demo application) the "empty" file config/bootstrap.php, just containing a php tag <?php WITHOUT a whitespace directly after. When bootstrap.php is now loaded via index.php it is loaded as a text file containing <?php, not parsed as a PHP file.
You can fix this by simply added a whitespace directly after the tag.

Related

How to Secure PHP code in htm Pages and Partials?

I'm using OctoberCMS, based on Laravel and Twig, with Nginx and PHP7.0-FPM.
I make a Page or Partial using the CMS Backend Editor. Can edit the HTML Markup and Code.
The Page will render as localhost/mypage and hides the php source code.
But I'm able to go to localhost/themes/mysite/pages/mypage.htm in the browser and view the Twig markup and PHP comments in plain text.
And on some pages I can view all of the PHP and Laravel code like connection to database names and tables.
Anything that is in function onStart() in the Code editor, even though its wrapped in php tags in the htm file.
mypage.htm output:
However when viewing a .php file, it only shows the output and not the source.
I tried to change the page file extension to php instead of htm but get the error.
Invalid file extension: php. Allowed extensions are: htm.
Have you setup nginx correctly to blacklist those and other unpermitted files? http://octobercms.com/docs/setup/configuration#nginx-configuration
Alternatively, you can take a whitelist approach to security and utilize the october:mirror command: http://octobercms.com/docs/setup/configuration#public-folder

How can I format PHP files with HTML markup in Visual Studio Code?

I'm using Laravel so all the views are .blade.php files. Visual Studio Code won't format the HTML because of the PHP extension. I removed the "blade" part of the filename, but it still isn't formatting the files properly (via Alt+Shift+F).
I also tried about five extensions, but none of them do the reformatting.
How can I format .blade.php files in Visual Studio Code?
The extension Beautify (from HookyQR) just does it very well. Either add PHP, and any other file extension type, to the configuration. As said by Nico, here is an example:
Go to user settings (Ctrl + Shift + P → User settings (UI) or Ctrl + , (comma)
Search for Beautify in the field above. And click on "Edit in settings.json" for "Beautify: Config".
For the "html" section, add "php" and "blade".
###Usage
You can invoke it directly. Press F1, and then write Beautify. The auto completion gives you two choices, "Beautify file" and "Beautify selection". Choose the one you need, and it will do the job. That's a straightforward direct way.
You can also add a keybinding to have a keyboard shortcut. Here is how to do it:
Open keybindings.json (go to menu File → Preferences → Keyboard Shortcuts)
Click in the above. Open and edit file keybindings.json
Add the following into the closed brackets, []
{
"key": "alt+b",
"command": "HookyQR.beautify",
"when": "editorFocus"
}
Choose any key you want, and make sure you don't override an existing one. Search first in the left side if it exists or not.
Note that all of those things are well documented in the description of the extension.
Extra: Note about blade
(suite to #Peter Mortensen clarification pinpoint)
blade or blade.php
If you are confused if it's blade or blade.php for the setting! I'll clear it up for you! It's blade! That's vscode keyword for the language!
How do you know ?
First if you open the list of languages as by the image bellow:
Write blade
You can see Laravel Blade (blade)! The language keyword is in the paratheses! blade!
Well but how to check!
Try with blade.php in the settings!
Try to beautify
You'll get an asking context menu for what language (html, css, js)!
So it doesn't works!
To really know ! Put back blade!
And it will work and beautify directly!
How well it works with blade
The awesome answer to that! Is try it, and see for yourself!
But if you ask me! I'll say it does work as with html! It can be too handy! And you may need to fix some lines! Depending on your expectations and your style!
Here an illustration! I screwed the indentation in purpose
And here the beautification result:
Beautify (from HookyQR) does solve this problem!
Many people add "blade.php" and "php" to the HTML configuration. Beautify does not recognize and manually choose an HTML template. Instead, just adding "blade" to the HTML configuration fixes all issues.
I think Beautify (by HookyQR) might do the trick. Just add 'php' and/or 'blade.php' to the HTML section of its configuration to beautify PHP / Blade view files.
It works for me!
If you want something that just works out of the box, add the format nested HTML in PHP files support Visual Studio Code should already have.
It uses all the native settings for html.format, format on save, etc. Give the extension Format HTML in PHP a try. I made it because every other solution made me annoyed, because it didn't work 100%.
I found this extension called Format HTML in PHP that works well for me.
Andrew Schultz mentioned the extension: "Format HTML in PHP".
This is indeed a great extension for format PHP files with HTML markup. It also formats JavaScript code included in PHP files.
Moreover. Good news! The developer is already working successfully to include Laravel blade.php files. In the meantime I format Laravel blade files in Visual Studio Code by switching language manually from Blade to PHP then I use the extension "Format HTML in PHP" with Ctrl + Alt + F or right click. It works great for me.
Using a newish feature in php, vscode seems to format and highlight embedded HTML beautifully. (php 7.3, released after the question was posted) adds what are called "heredoc" and "neardoc" strings. They are well described here: https://andy-carter.com/blog/what-are-php-heredoc-nowdoc.
Basically, you put your HTML in like this:
[php code.....]
echo
<<<HTML
<div>
<h1>This is a headline</h1>
<p> this is a paragraph. lorem ipsum lorem ipsum blah black </p>
</div>
HTML;
[^ don't forget the ; follow with more php if you want]
it looks just great on screen.
I use Format HTML in PHP which works well in a file containing both HTML and PHP. However it does not appear to format a 'pure' php file such as a Class. So I have also installed Phpfmt, which does a great job with the pure php file but wrecks the indenting in a file with mixed content. Here's an example of what I get:
<?php
if (!$logged_in) {
$login_form = 'data-toggle="modal" data-target="#Modal1"';
$href = '#';
} else {
$login_form = '';
$href = 'members';
}
?>
The opening php tag is correctly aligned with the html. 'I see that under 'Supported Transformations' it says:
AlignPHPCode: Align PHP code within HTML block.
But as you can see from the above, it's not working for me. Any suggestions??
This can be done by using HTML formatting for ".blade.php":
Open Command Palette (Ctrl + Shift + P (on a PC))
Type "Preferences:Open Settings (JSON)" (this is for opening settings.json)
Add
"files.associations": {
"*.blade.php": "html",
"*.tpl": "html"
},
This will format .blade.php files as HTML, but it will not remove Blade snippets.

PageKit CMS - How to include a JS file in a view from a module

I've started working with the PageKit CMS (which I think is based on the symfony framework) but can't figure out how to include JS files in my view.
the documentation tells me to use <?php $view->script('theme', 'theme:js/theme.js') ?>
When I adjust this to match my module name it ends up looking like this: <?php $view->script('shoutzor', 'shoutzor:js/visualizer/main.js') ?>
But when I then load the page there's no <script> tag (or anything for that matter) regarding that js file.
What am I doing wrong here? since Pagekit is a fairly new CMS there's unfortunately hardly anything to find.
Turned out I did 2 things wrong.
First off, the 1st parameter is a unique ID apparently, which I didn't realize causing it to only add the last script from the list.
After that I modified the line to resemble something like this:
<?php $view->script('shoutzor1', 'shoutzor:assets/js/visualizer/main.js'); ?>
Which included the correct asset file

Cakephp i18n extracts all but one template file

I have a site with dynamic content, with only some button labels and headers in the template files. In the View folder I have multiple *.ctp files, for example profile.ctp and index.ctp. My problem is that when I run the cake console's i18n extract function, then the translateable strings from the controller.php and profile.ctp are picked up, but index.ctp is ignored.
I tried running
cake i18n extract --files .\View\ControllerName\index.ctp --ignore-model-validation --output .\Locale\ --paths . --overwrite yes --extract-core no --merge no
This however gives no results, and no *.pom files are created, unlike when I run it without the --files argument. In that case just the strings from the view are missing from the *.pom file.
The interesting thing is that in the console output it shows that the index.ctp is parsed, but still no results are given.
Output Directory: C:\Dev\app\Locale\
---------------------------------------------------------------
Processing C:\Dev\app\View\ControllerName\index.ctp...
Processing C:\Dev\app\View\ControllerName\profile.ctp...
Done.
So, my question is - does anyone have any idea on why it is so? Is there a setting to disable certain templates, or is it the default behavior to ignore index.ctp files? Or am I just doing something really wrong?
Thank you!
Edit: Solved. Don't use shorthand PHP tags just in case :)
Maybe a stupid remark, but does your index.ctp actually contain translatable strings (i.e. strings wrapped into __("sentence to translate"); function) ?
So, after lots of headwracking I decided to try something out. I copied profile.ctp content into index.ctp, then it worked. After that I just started stripping down the index.ctp until it worked. Turns out that if I used shorthand PHP tags (<? ?>) then for some reason the translation strings detection didn't work, but when I used full tags (<?php ?>) then everything was fine. Figures :)

CSS failure when using INCLUDE Function

I downloaded a Template + CSS File for a Website that I'm Building, the template worked well until I tried to break it down and put every code in its own file (for easy modification and editing in the future).
So, when I cut the head part which included (Title + Meta Data .. etc ), and put it in its own file, and replaced it (for sure) with an include() function, I lost the CSS styles and returned to the basic & standard style (Black & white with no extra format .. etc)
Where did I Go wrong? Knowing that here is the include function that I've used:
<?php
include 'files/head.php';
?>
With an URL like file:///C:/xampp/htdocs/test6/index.php PHP is NOT executed. You must run it with apache being involved. Currently you are opening your PHP script as a regular txt or html file - it is just passed to browser without processing.
In order to make include function work you must run it with apache. As you are using xamp, I think you should simply open it with URL like http://localhost/test6/index.php In this case, apache will get that request and pass it to PHP. PHP engine will interpret your PHP script and "replace" include files/head.php with a content of head.php.
If everything is Ok, after pressing Ctrl+U (or looking at HTML with Developer Tools or Firebug) you should see a content of head.php instead of <?php include ....
Please note that css files should be linked with relative URL like css/screen.css. Or absolute URL like http://localhost/test6/css/screen.css. like Search for relative and absolute URLs in google for more info.

Categories