2

I need to stop Joomla from converting PHP code to a comment (or otherwise disabling it).

I copied a working page with a basic [directory listing] function, and the duplicate wouldn't work.

I am nearly positive Joomla is "intelligently" modifying the PHP to disable it--how can I stop this?

X/Y Problem: A working extension to list directory contents would sidestep this issue, because that's really what I need--though I'd prefer to also learn what I'm doing wrong.

TL;DR

I periodically provide updates for a site (Joomla 2.5) I neither own nor host. I've recommended we update or rebuild it, so far a non-option.

I don't really know much PHP. However the below code polls a given directory and links the files. The customer has backend access and can quickly distribute information via this page, uploading PDF files as required and removing the obsolete ones. Simple for the customer and works great.

Yesterday she requested a new page for a different category of information.

I created a separate directory for the new page (Turnover) and updated the copied code to reflect this. The original page (Output) worked fine, but the duplicate page was blank except for title. I took a step back but not even the unmodified copy would work on the new page--though it still worked on the old.

Before realizing the PHP was not even being executed, I verified that

  1. The articles are identical except by title and index number
  2. Permissions on the new directory match the original AFAICT
  3. It's not file, filename, or size (new file immediately worked in the old directory)
  4. Also created a second duplicate page to rule out human error

Since one page works and another doesn't, it's clearly not an incompatibility between Joomla and PHP versions, and most likely not a server misconfiguration.

The page source on the broken page show the PHP as a comment; the working page served working links:

file1
file2
file3

After extended troubleshooting, I altered only the title of the original page--and it subsequently failed. I now have two pages that won't work, meaning the customer will need to upload files and then wait until I can log in and manually create individual links. Then she'll wait again when I remove old ones.

I don't have shell access, nor to backups, if they even exist. I learned last night that Joomla lacks a revision history. I normally use Wordpress, and this hasn't convinced me to switch. :,/

I suspect the issue is with Joomla and not the code itself. Code examples I've seen begin with <?php (and not <!--?php), but my assumption that this is the sole issue could be wrong. When I manually remove those three characters, the code is simply converted to text as part of the page.

The code below is exactly how I first saw it in the HTML editor (without the HTML editor I don't even see the PHP at all). Joomla adds the paragraph tags automatically and converts <b> tags to <strong>. (I checked page source of both pages before and after I broke the original page).

<p><strong>Output</strong></p>
<!--?php 
//path to directory to scan
$directory = "images/download/output/";
$files = glob($directory . "*.pdf");
array_multisort(array_map( 'filemtime', $files ),SORT_NUMERIC,SORT_DESC,$files);

//print each file name
foreach($files as $f) {
  $path_parts = pathinfo($f);
  echo '&lt;p&gt;&lt;a href="'.$f.'"&gt;'.$path_parts['filename'].'&lt;/a&gt;&lt;/p&gt;';
}
?-->
<p> </p>

(thanks to the mod who migrated for me--I should've checked to see if there was a Joomla stack)

With a busy weekend already underway, fixing this is pretty important--thanks for any help/advice offered. I figured this should take fifteen or twenty minutes--but I've burned hours on it.

migrated from serverfault.com May 25 at 22:03

This question came from our site for system and network administrators.

  • 1
    I can't accept my own answer yet, but I plan to (can't for two days). I didn't realize there's an editor choice in the Joomla UI. – zedmelon May 25 at 22:19
  • 1
    Good comprehensive posts @zedmelon I love to see question which show exhaustive investigation and toil (I wish all posted questions were this way). Please continue to post your Joomla questions in this community. Please take the tour. – mickmackusa May 25 at 22:56
  • Yes, great to see such an exhaustive and well documented question. – Neil Robertson May 27 at 0:16
  • Thank you gentlemen. It's nice when my tendency to talk (way) too much actually proves useful :,) – zedmelon May 27 at 18:21
  • On a more serious note, I do try to (a) fix stuff on my own (b) research before requesting help, and (c) remember that others are helping me out of benevolence, and making it easier for them not only (1) gets it done sooner, but (2) is respectful, which is non-negotiable. – zedmelon May 27 at 18:38
3

FIXED: The culprit was a personal editor choice set in the user profile section.

Screenshot: User Profile Editor Settings

I changed this to Editor - None, resubmitted the disobedient code (without comment tags), and it worked like a charm!

Clearly the user who created the original article did not use the TinyMCE, but my account had that selected. I did not create the user account I'm using--though I suspect I wouldn't have noticed this anyway and would still likely have found myself in the same boat.

It's worth noting that TinyMCE was editing the code before I opened it, rather than as I was submitting/saving the post. The PHP was converted to a comment up front, and since I'm a PHP n00b it didn't stand out to me like it should've. I duplicated a comment I assumed was code, and I lacked the programming chops to recognize that.

I'm glad it was something simple, but holy smokes this had me barking up the wrong tree for awhile!


Note for posterity:

This has similar symptoms (but is not the same) as assigning text filters in the global config, which imposes filters upon user groups based on type/class: administrator, editor, publisher, et cetera (Neil's answer covers this and some alternate approaches).

The above screenshot is personal preference (in my own user profile) offered to a user with administrator/super user status. I expect all users get some choices, but they may not match these--and of course newer Joomla versions may differ.

1

As you have found, Joomla filters content to control the HTML content authors submit for security and maybe other reasons.

You can change Text Filter options for each user group in the Global Configuration but I think most editors filter out PHP code anyway.

There are a few ways around this problem.

Disable the Editor

Temporarily changing your user settings to use no editor is a good approach as no additional third party extensions are needed, although (as you have found) content authors who come along later and edit the content may be in for a surprise.

Joomla Extension to List Directory Contents

You can install a third party extension to list the contents of a folder. I tend to use the free or the paid version of Easy Folder Listing and there are others.

Joomla Extension to Retain PHP Code

You can install a third party extension that allows PHP code to be retained without disabling your editor. I tend to use Regular Labs Sourcerer and there are others.

  • Yes, I found a blog describing Global Config, but I was already set to "no filter." And I'd spent a triumphant nine seconds navigating to that page, certain I'd found the problem--bummer. However it did at least open a new line of thinking, validating my hunch that Joomla was editing on my behalf. Of course from Joomla's perspective I'd requested it. I'd been checking directory perms, file sizes, and other stuff which in the past have proven to produce similar symptoms.Though once I saw the source code I at least left the football field and found myself in the right ballpark. – zedmelon May 27 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.