Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

2009/12/07

Using your own Uploader in CKEditor

CKEditor still doesn't include its own file manager, but the hooks to integrate your own uploader script or file manager are already in place, as you can check win CKFinder or any number of third party scripts that people have been writing.

Upgrading from FCKeditor

Maybe you want to use a previous uploader that you've used in FCKeditor and you are not sure about what are the differences, and the current documentation isn't clear enough for you. I'm pointing here the differences for the upload (or "quick upload") part, I might try to cover the slight differences in the "File browser" in other post.

  1. The file is sent as "upload" instead of "NewFile". Note that this is the value that you'll get using the default dialogs, but the name of the input is the id applied to the uiElement used to create the widget so in some situations you might get a different name.
  2. Some additional info is sent along the request as GET parameters: the name of the CKEditor instance, the current language code and a parameter to specify the callback function.
  3. The callback function is dynamic, you must read the "CKEditorFuncNum" parameter and send it back.
  4. There are no special error numbers in the callback function. Your script can return a url to use and a message to show to the user, both are optional so you can return just an URL that will be used in the dialog, or you can return a message that will be shown to the user, or a URL and a message (for example a warning that the file has been renamed and the new URL).
  5. Thanks to the message and the "langCode" parameter you can use localized messages instead of having them in English.

So I think that that is the summary of differences showing that the limitations in the previous API (hardcoded error numbers and messages, no info about the current instance...) have been fixed.

Practical example

Maybe that's not enough for you, so here's an example "upload.php" file that you can use to study and adjust your code. This code doesn't save any file, doesn't include any security check, it doesn't check for errors... It's just some basic sample explaining what you can do.

<?php

// This is just a simple example, not to be used in production!!!

// ------------------------
// Input parameters: optional means that you can ignore it, and required means that you
// must use it to provide the data back to CKEditor.
// ------------------------

// Optional: instance name (might be used to adjust the server folders for example)
$CKEditor = $_GET['CKEditor'] ;

// Required: Function number as indicated by CKEditor.
$funcNum = $_GET['CKEditorFuncNum'] ;

// Optional: To provide localized messages
$langCode = $_GET['langCode'] ;

// ------------------------
// Data processing
// ------------------------

// The returned url of the uploaded file
$url = '' ;

// Optional message to show to the user (file renamed, invalid file, not authenticated...)
$message = '';

// In FCKeditor the uploaded file was sent as 'NewFile' but in CKEditor is 'upload'
if (isset($_FILES['upload'])) {
    // ToDo: save the file :-)
    // Be careful about all the data that it's sent!!!
    // Check that the user is authenticated, that the file isn't too big,
    // that it matches the kind of allowed resources...
    $name = $_FILES['upload']['name'];

    // example: Build the url that should be used for this file   
    $url = "/images/" . $name ;
    // Usually you don't need any message when everything is OK.
//    $message = 'new file uploaded';   
}
else
{
    $message = 'No file has been sent';
}
// ------------------------
// Write output
// ------------------------
// We are in an iframe, so we must talk to the object in window.parent
echo "<script type='text/javascript'> window.parent.CKEDITOR.tools.callFunction($funcNum, '$url', '$message')</script>";

?>

If the code is deemed worth, it might be added to the wiki (for example in Custom File Browser, or creating a new article "Custom Uploader") but before doing so I would like to hear a little feedback.

 

Update 10/08/2013:

I've added the missing part to save the file, use it at your own risk. Go to the new post explaining it.

2009/08/08

Installing PHP in IIS 6 (windows 2003)

Some time ago I tried to install PHP in a test machine with Windows 2003 and IIS, so I could do some tests more easily. At that time I failed to get it working. The theory is simple, I found several tutorials explaining how to do it, but I wasn't able to get nothing but a "404 page not found" whenever I requested a .php page in that server.

Today I decided to try again. Quite a lot of things have changed in this time.

PHP has setup a separate site to handle installing PHP in Windows. Unfortunately the site is not as friendly as it seems that they are planning, but at least it's clear that they acknowledge the fact that there's a need for help installing PHP in windows. Besides the downloads it seems that there's little more in that site at the moment, but that download page is a little daunting:

Reading the sidebar you can see that you need to get the VC9 versions to install it in IIS, and the VC6 are meant to be used with the Apache server. But:
Thread safe or Not Thread safe?
Zip, Installer or Debug Pack?

Wouldn't it be better to move that Debug pack to somewhere else?

I'll skip some steps, and I would like you to introduce you this page that explains how to configure FastCGI and PHP in IIS.

You could have done the two first steps like I did, I downloaded the installer for PHP-non threaded-vc9 and when I tried to run it and configure for "IIS FastCGI" it warned to first download and install the FastCGI extension.

But you won't get the explanation about how to configure the FastCGI extension, so keep reading that page. I tried to do the automatic configuration using the script, but I got an error, so I don't know what's wrong. I tried to verify then all the steps according to he manual configuration and it seemed to be OK. But I got again a "404 page not found" trying to load any php page.

The next step was removing all of it and try with this automated installer for PHP in IIS . Yes, now Microsoft is hosting a whole site devoted to support for PHP in IIS, so this looks like a first stop when you try to get it working or troubleshoot something related to IIS and PHP. Wow.

After installing that "Web plataform installer" it took care of the rest by itself, each product was downloaded and installed, and I guess that it might have done that "configure FastCGI" in the bundle.

But again it didn't work!!!

The difference for me was that this time I knew that I was using the correct files, it's a tested installer, so I can't blame it on me due to having picked the wrong file in the PHP downloads (in this case I've noticed that they are using PHP 5.2.10 instead of the latest one PHP 5.3 that I picked).

So I tried to look more carefuly at it and then I noticed that under the "Default web site" properties it doesn't have assigned the .php script mapping. It was done correctly at the "Web sites" (parent folder), but not on the site itself. For some reason that setting didn't propagate to the child nodes, so I "edited" it and pressed OK, and it asked whether to update the value in the child sites. I said YES, and Now PHP Works!!!

As a summary:

The problem that I had was that the setting was correct for the "Web sites" node, but the real website node didn't get that setting applied, I guess that I might have messed previously (as I said this is a test machine) and those automatic scripts didn't try to update it.