Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2022-05-10 21:39:17

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 10,791
Website GitHub

Re: Connecting a publishing client aka using MarsEdit?

Thanks, Pete. That’s interesting.

So the Undefined constant ihu errors are going to take some thought. Presumably our Textile wrapper has changed and it now uses those constants for flexibility, but we assume (wrongly) that the context is going to be executed from within Textpattern. In this case, the IXRClass bypasses Textpattern admin-side context so it can inject directly. Which means publish.php is never called. Which means those constants aren’t defined. There’s an outside long shot that maybe they are defined but are out of scope and need a prefix to escape the class. But I suspect the former situation is more likely.

Needs fixing somehow, but we can’t just include publish.php because it has all sorts of other baggage it brings along for the ride. So the sanest route might be to take those definitions out of publish.php and put them somewhere more central if we can (constants.php?) so they are defined in all contexts. Or, better for longer term, wean ourselves off globals.

The undefined array key is just a bit of defensive coding required. Either to ensure the key exists when invoked, or to defend against situations where it might not exist when accessed, e.g. in TXP_RPCServer.php _buildMetaWeblogStruct():

$cat1 = empty($cat['title']) ? "" : $cat['title'];
...
$cat2 = empty($cat['title']) ? "" : $cat['title'];

Same in IXRClass: some defensive coding is required at the top of the call() function to explicitly set $args if it isn’t passed in. The test at the very top of the function is skipped if $args is null, meaning count() fails later.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#14 2022-05-10 21:55:09

Melonking
Member
Registered: 2022-05-10
Posts: 21
Website

Re: Connecting a publishing client aka using MarsEdit?

Iv been poking around in the code and I setup the metaWeblog.newMediaObject endpoint. I can hit it with a request successfully, however it keeps telling me “server error. wrong number of method parameters” when I try to return;

function mt_uploadImage($params)
     {
        $returnValue = array(
           'url' => 'http://test.com/'
        );
        return $returnValue;
}

The issue is I have no idea how its deciding how many parameters should be returned, as far as I can see from the info here: https://laurentkempe.com/2006/08/28/C-implementation-of-newMediaObject-for-the-MetaWeblog-API/ this should be correct.

Edit: Sort of resolved? For some reason you need to add an extra pram to the callback definition. So its “array(‘string’, ‘string’, ‘string’, ‘string’, ‘struct’),” even though there are actually only 4 prams, the first string is just for fun? Someone will have to explain that one to me!

Last edited by Melonking (2022-05-11 00:10:06)


The world will tremble

Offline

#15 2022-05-11 00:06:55

Melonking
Member
Registered: 2022-05-10
Posts: 21
Website

Re: Connecting a publishing client aka using MarsEdit?

Im gonna call it for today here is a code chunk that almost works but I could still use some pointers about how image_data works:

function mt_uploadImage($params)
     {
        list($blogid, $username, $password, $file) = $params;
        $txp = new TXP_Wrapper($username, $password);
        if (!$txp->loggedin) {
            return new IXR_Error(100, gTxt('bad_login'));
        }
        //Temp File Upload
        $tempImageFolder = getcwd().'/../images/mt-tmp/';
        if (!file_exists($tempImageFolder)) {
            mkdir($tempImageFolder, 0777, true);
        }
        file_put_contents($tempImageFolder.$file['name'], $file['bits']);
        //Final Setup
        image_data($tempImageFolder.$file['name'], false, 0, false);
        $returnValue = array(
           'url' => 'http://'.$_SERVER["HTTP_HOST"].'/images/'.$file['name']
        );
        return $returnValue;
     }

It gets as far as the image_data function, but that returns an error saying the image is not a supported filetype even when it is. I’m not sure how image_data is checking file types exactly. But it seems to be very unhappy about importing a file that has been uploaded to the server via this function!


The world will tremble

Offline

#16 2022-05-11 09:26:06

etc
Developer
Registered: 2010-11-11
Posts: 4,704
Website GitHub

Re: Connecting a publishing client aka using MarsEdit?

Melonking wrote #333238:

It gets as far as the image_data function, but that returns an error saying the image is not a supported filetype even when it is. I’m not sure how image_data is checking file types exactly. But it seems to be very unhappy about importing a file that has been uploaded to the server via this function!

Hi, thanks for testing.

it looks like image_data() expects an associative array of name, error, tmp_name as its first argument. You might try to pass $file itself.

Offline

#17 2022-05-11 10:55:44

Melonking
Member
Registered: 2022-05-10
Posts: 21
Website

Re: Connecting a publishing client aka using MarsEdit?

I was going off the line “@param bool $uploaded If FALSE, $file takes a filename instead of upload vars” in the image_data definition comment, is a file name not just a string?

And the $file var in this case is a struct that contains, a filename, base64 code, an mme type and a blogid. So what I’m doing is converting it into a local file on the server and then trying to pass the name of that file to the existing image upload system so that it can do its thing..

I’m starting to think I might have to call it soon, I just wanted a blog that worked with MarsEdit!


The world will tremble

Offline

#18 2022-05-11 11:15:36

etc
Developer
Registered: 2010-11-11
Posts: 4,704
Website GitHub

Re: Connecting a publishing client aka using MarsEdit?

Melonking wrote #333241:

I was going off the line “@param bool $uploaded If FALSE, $file takes a filename instead of upload vars” in the image_data definition comment, is a file name not just a string?

That’s right, but the filename comes from $file[‘tmp_name’] in this case. I guess you need to pass something like

array(
    'name' => $file['name'],
    'error' => UPLOAD_ERR_OK,
    'tmp_name' => $tempImageFolder.$file['name']
)

as the first argument to image_data().

I’m starting to think I might have to call it soon, I just wanted a blog that worked with MarsEdit!

I’m very much willing to help, but have no MarsEdit to test, sorry.

Offline

#19 2022-05-11 11:28:29

Melonking
Member
Registered: 2022-05-10
Posts: 21
Website

Re: Connecting a publishing client aka using MarsEdit?

Aha thank you! I actually tried doing that but then thought I was doing it wrong.

Let see, here is the current code: https://pastebin.com/rY6BH8vP

  • What is happening so far; the image is being uploaded to the mt-tpm folder.
  • I can see a database entry is being made for the new images, the database entries have the correct name, and dimensions, but they are missing an author.
  • However the actual image file is not being moved from the mt-tmp folder to the images folder and is not being given its sequential name.

If you can help me figure out what steps I’m missing to correctly make image_data place the image in the images folder, and how to correct the author in the database entry that would be a huge help!

Also the return url I think is wrong because its returning the image name instead of the sequential number the image should get in the images folder?

EDIT: I can see it is actually removing the images from the mt-tmp folder, so its moving/removing them but not putting them in the images folder.. there are no errors.. is there somewhere else they could be going? YES! Its putting them in the rpc folder! And its naming them “IMPATH3.jpg”. I have no idea what to do about this, image_data has no specs listed to modify its save location?

Last edited by Melonking (2022-05-11 12:36:00)


The world will tremble

Offline

#20 2022-05-11 13:13:08

Melonking
Member
Registered: 2022-05-10
Posts: 21
Website

Re: Connecting a publishing client aka using MarsEdit?

Ladies and gents, I present to you the worlds worst implementation of MetaWeblog image upload!

You’re welcome to use/modify it. You must rename the database and table name in code. Change ‘thoughs’ to your database name. (If someone knows how to get the correct database and table prefix automaticity lemmy know!)

This is melon signing off with a minimum viable product!

Add the following to /rpc/TXP_RPCServer.php

$this->addCallback(
                'metaWeblog.newMediaObject',
                'this:mt_uploadImage',
                array('boolean', 'string', 'string', 'string', 'struct'),
                'uploads a media object'
            );
/*
     metaWeblog.newMediaObject
     Description: Uploads a file to your webserver.
     Parameters: String blogid, String username, String password, struct file
     Return value: URL to the uploaded file.
     Notes: the struct file should contain two keys: base64 bits (the base64-encoded contents of the file)
     and String name (the name of the file). The type key (media type of the file) is currently ignored.
     */
     function mt_uploadImage($params)
     {
        list($blogid, $username, $password, $file) = $params;
        $txp = new TXP_Wrapper($username, $password);
        if (!$txp->loggedin) {
            return new IXR_Error(100, gTxt('bad_login'));
        }
        //Temp File Upload
        $tempImageFolder = getcwd().'/../images/mt-tmp/';
        if (!file_exists($tempImageFolder)) {
            mkdir($tempImageFolder, 0777, true);
        }
        file_put_contents($tempImageFolder.$file['name'], $file['bits']);
        //Create the final file and input into database
        $newfile = array(
            'name' => $file['name'],
            'error' => false,
            'tmp_name' => $tempImageFolder.$file['name']
        );
        image_data($newfile, false, 0, false);
        //Crappy code to make it work
        //Get the count of images from the database
        $aiQuery = safe_query("SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'thoughts' AND TABLE_NAME = 'txp_image';");
        $aiRow = mysqli_fetch_row($aiQuery);
        $count = $aiRow[0]-1; //Retracted by 1 because we just added a row
        $filetype = end(explode('.', $file['name'])); //Get the uploaded filetype
        rename(getcwd()."/IMPATH".$count.'.'.$filetype, getcwd()."/../images/".$count.'.'.$filetype);
        //Return
        $returnValue = array(
           'url' => '/images/'.$count.'.'.$filetype
        );
        return $returnValue;
     }

Last edited by Melonking (2022-05-11 14:53:14)


The world will tremble

Offline

#21 2022-05-11 13:51:07

redsweater
Member
From: Boston, MA
Registered: 2007-05-26
Posts: 12
Website Twitter

Re: Connecting a publishing client aka using MarsEdit?

Nice work, melonking!

Offline

#22 2022-05-11 14:14:04

Melonking
Member
Registered: 2022-05-10
Posts: 21
Website

Re: Connecting a publishing client aka using MarsEdit?

Thanks! Iv fixed most of the major issues (its still crappy code). It now works with multiple protocols, can handle . file name and url changes.

But if someone can find a way to easily get the correct sequence number (the auto increment number of the txp_images table) for images that would infinity improve this code! My system of guessing it based on file count is awful.

EDIT: This has been fixed, but there are still issues, see my post before this!

Last edited by Melonking (2022-05-11 14:56:09)


The world will tremble

Offline

#23 2022-05-11 19:18:02

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,412
Website

Re: Connecting a publishing client aka using MarsEdit?

Melonking wrote #333244:

If someone knows how to get the correct database and table prefix automaticity lemmy know!)

I’m not sure how much access you have to the rest of the Textpattern functions in the XML RPC server, but your database name and prefix is stored in $txpcfg['db'] and $txpcfg['table_prefix'] in your config.php file (sample config.php on GitHub) and if you don’t already have access to $txpcfg there you could include that file as that’s pretty much all it contains. It’s already included in /rpc/index.php but not in /RPC/TXP_RPCServer.php as far as I can see.

There’s also the built-in safe_pfx() function for automatically adding the table prefix in database queries. There’s an example in the code documentation and you can find other instances of it in use if you search txplib_db.php.

Melonking wrote #333246:

if someone can find a way to easily get the correct sequence number (the auto increment number of the txp_images table) for images

If I’m not mistaken, the image_data() function should return the new image’s ID number which you could then use for file renaming. See how it’s used here in the image_insert() function. That may obviate the need for your auto_increment lookup code and what follows it.


TXP Builders – finely-crafted code, design and txp

Offline

#24 2022-05-11 19:59:39

Melonking
Member
Registered: 2022-05-10
Posts: 21
Website

Re: Connecting a publishing client aka using MarsEdit?

Thanks! I’ll mess around with that a little more.

Iv also run into a new issue that does not seem to be related to image uploads. When an article is published via XMLRPC it arrives in the database correctly, however textpattern does not seem to refresh it on the site correctly. The article shows up on the site, but its blank about 80% of the time, however when you edit it on the online interface you can see all the content is there.

In order to actually get your article to display, you have to publish it, then login to the textpattern admin page, open the article, click save and close it again.

Is there some event that’s not being fired correctly somewhere?


The world will tremble

Offline

Board footer

Powered by FluxBB