phpalfrescoapache-chemistry

Apache Chemistry fails when creating a folder with blank spaces


I'm using API_Alfresco which is a library to connect to Alfresco via Apache Chemistry, but I'm getting the following error when I try to create a folder with blank spaces:

 Uncaught CmisRuntimeException in C:\xampp\htdocs\API_Alfresco\cmis_repository_wrapper.php:176 Stack trace: 
    #0 C:\xampp\htdocs\API_Alfresco\cmis_repository_wrapper.php(207): CMISRepositoryWrapper->convertStatusCode(505, '') 
    #1 C:\xampp\htdocs\API_Alfresco\cmis_service.php(791): CMISRepositoryWrapper->doGet('...') 
    #2 C:\xampp\htdocs\API_Alfresco\APIAlfresco.php(98): CMISService->getObjectByPath('...', Array)
    #3 C:\xampp\htdocs\phpCMISalfresco.php(33): APIAlfresco->setFolderByPath('...') 
    #4 {main} thrown in C:\xampp\htdocs\API_Alfresco

When I try to create a folder with no blank spaces it works fine. Here's my code:

<?php
    require 'APIAlfresco/APIAlfresco.php';
    include 'connectAlfresco.php';

    $parent_name = $_POST['parent']; //FAILS IF IT HAS BLANK SPACES
    $uploaded_file = basename($_FILES['att_file']['name']); //Uploaded
    $base_folder = "/path/to/site/folder/"

    if (move_uploaded_file($_FILES['att_file']['tmp_name'], $uploaded_file)) {

            $conexion->setFolderByPath($base_folder); //Set base folder where files will be uploaded 

        /*Check if dir already exists and create it if it doesn't */
            if($conexion->existsFolder($parent_name)) echo "Parent".$parent_name." already exists";
            else $conexion->createFolder($parent_name);

      /*Move uploaded file into folder */
            $conexion->setFolderByPath($base_folder."/".$parent_name); //ERROR HERE IF $parent_name HAS BLANK SPACES
            $conexion->uploadFile($uploaded_file);

    }

    echo 'File infor:';
    print_r($_FILES);
?>

I took a look to the error trace and realized that doRequest() call inside doGet() (in cmis_repository_wrapper.php) is returning an error within the HTTP request (only fails when folders contain blank spaces). Since I haven't coded the library I can't seem to figure out what could be going wrong. Any clues? Could it be a bug?

Also made some debugging and realized that doRequest() is taking an URL such as:

http://localhost:8080/alfresco/api/-default-/public/cmis/versions/1.1/atom/path?path=/Sites/mysite/documentLibrary/Folder WithBlankSpaces&filter=&includeAllowableActions=&includeACL=&includePolicyIds=&includeRelationships=&renditionFilter=

And in this link I'm noticing two things that could be triggering the error:

1) "Folder WithBlankSpaces" folder is splitting the URL in two (most likely to be the actual issue)

2) Vars in the last part of the url are unset

I suspect that 1) could be the real issue, since if I force that folder to have no blank spaces (calling str_replace(' ', '', $parent_folder)) then it'll work fine.

How could I get it to work with folders with blank spaces? Is there any way I can modify that URL so blank spaces don't divide it in two? Of course if user tries to create "My Folder", what he wants to see is a "My Folder" directory and not a "MyFolder" one.

Thanks in advance!


Solution

  • I figured it out myself. The idea is forcing blank spaces to be "%20" before making the HTTP request, and then replacing "%20" with blank spaces again.

    Basically $conexion->setFolderByPath($parent_name) makes an HTTP request (that's where the error was being triggered when $parent_name contained spaces).

    So, before calling that method, $parent_name spaces need to be replaced by "%20" so HTTP requests will know that there's a "blank space" in the url expressed as %20.

    Once the HTTP request is done (after calling setFolderByPath()), "%20" needs to be replaced back to blank spaces to then call the $conexion->createFolder($parent_name); so the folder is created with actual blank spaces and not "%20".

    EDIT: I'm posting the fixed code to make it clear.

     <?php
            require 'APIAlfresco/APIAlfresco.php';
            include 'connectAlfresco.php';
    
            $parent_name = $_POST['parent'];
            $parent_name = str_replace(' ', '%20', $parent_name); //PREPARE IT FOR THE HTTP REQUEST
            $uploaded_file = basename($_FILES['att_file']['name']); //Uploaded file
            $base_folder = "/path/to/site/folder/";
    
            if (move_uploaded_file($_FILES['att_file']['tmp_name'], $uploaded_file)) {
    
                    $conexion->setFolderByPath($base_folder); //Set base folder where files will be uploaded 
    
                /*Check if dir already exists and create it if it doesn't */
                    if($conexion->existsFolder($parent_name)){
                        echo "Parent".$parent_name." already exists";
                    }else{
                        $parent_name = str_replace('%20', ' ', $parent_name); //CHANGE IT BACK TO BLANK SPACES
                        $conexion->createFolder($parent_name);
                    }
              /*Move uploaded file into folder */
                    $final_folder = $base_folder."/".$parent_name;
                    $final_folder = str_replace('%20', ' ', $final_folder); //ANOTHER HTTP REQUEST IS NEEDED
    
                    $conexion->setFolderByPath($finalFolder); //ERROR HERE IF $parent_name HAS BLANK SPACES
                    $conexion->uploadFile($uploaded_file);
    
            }
    
            echo 'File infor:';
            print_r($_FILES);
        ?>
    

    EDIT AFTER SOME DAYS: Just in case anybody is interested, this is no longer needed to do in programmer's PHP code. I cloned the library, fixed this issue, pushed it to local branch and started a pull request. Now special characters as well as blank spaces are handled within the library. You can simply use $conexion->setFolderByPath($parent_name) and $parent_name can contain all sorts of special characters and spaces, and programmers don't need to take care of it.