Moodle
  1. Moodle
  2. MDL-35034

Google docs repository "Invalid JSON string" message for PDF document solution

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 2.3, 2.3.1, 2.4
    • Fix Version/s: 2.2.5, 2.3.2
    • Component/s: Repositories
    • Labels:
    • Testing Instructions:
      Hide

      Test pre-requisites

      • A Google Docs/Drive account with some files
      • Upload a file to Google Docs, and set it to 'Prevent download' using a right click
      • The Google Docs repository enabled

      Test steps

      1. Navigate to your private files
      2. Click on 'Add' and explore your Google Docs
      3. Make sure the file listing is correct
      4. Make sure you can download any file
      5. Make sure you CANNOT download the file with restricted download
      Show
      Test pre-requisites A Google Docs/Drive account with some files Upload a file to Google Docs, and set it to 'Prevent download' using a right click The Google Docs repository enabled Test steps Navigate to your private files Click on 'Add' and explore your Google Docs Make sure the file listing is correct Make sure you can download any file Make sure you CANNOT download the file with restricted download
    • Affected Branches:
      MOODLE_23_STABLE, MOODLE_24_STABLE
    • Fixed Branches:
      MOODLE_22_STABLE, MOODLE_23_STABLE
    • Pull from Repository:
    • Pull Master Branch:
      MDL-35034-master
    • Rank:
      43642

      Description

      I configured the Google Docs repository but with some accounts i get the message "Invalid JSON string".

      I think i found a problem in the file lib/googleapi.php (line 115), the code is...

      $source = (string)$gdoc->content[0]->attributes()->src;,
      

      ...but in the XML entry for PDF documents there is no "content" or "src" attributes.

      Debugging the xml file i found this

      [title] => Test file.pdf
      [link] => Array
          (
              [0] => SimpleXMLElement Object
                  (
                      [@attributes] => Array
                          (
                              [rel] => alternate
                              [type] => text/html
                              [href] => https://docs.google.com/file/test/test-test-test-test/edit
                          )
      
                  )
      ...
      

      So the correct path to parse this XML entry is different than what appears on lib/googleapi.php (line 115). I changed it to this...

      $source = (string)$gdoc->link[0]->attributes()->href;
      

      ...and now it works fine

      Sorry if this issue is duplicated, i tried to find something similar without success.

        Activity

        Hide
        Michael de Raadt added a comment -

        Thanks for reporting that. I couldn't find any duplicate issues.

        Thanks also for investigating the issue and sharing a solution.

        I'm wondering if this error was coming up sometimes or every time. Perhaps we have to introduce a test for the attribute rather than just switching to the one reported here.

        Show
        Michael de Raadt added a comment - Thanks for reporting that. I couldn't find any duplicate issues. Thanks also for investigating the issue and sharing a solution. I'm wondering if this error was coming up sometimes or every time. Perhaps we have to introduce a test for the attribute rather than just switching to the one reported here.
        Hide
        Frédéric Massart added a comment -

        Hi David, thanks for reporting this. Could you please provide some information about the version of Moodle you are using, and eventually the PDF you were having trouble with? I tried to reproduce it but I could not. Thank you!

        Show
        Frédéric Massart added a comment - Hi David, thanks for reporting this. Could you please provide some information about the version of Moodle you are using, and eventually the PDF you were having trouble with? I tried to reproduce it but I could not. Thank you!
        Hide
        David Cabrera Primo added a comment - - edited

        Hi Frédéric,

        I have been testing this issue to give a chance to reproduce it.

        First of all i have created a google account (user: moodledevtest@gmail.com, pass: moodledevtest) that actually fail on every 2.x moodle site that i have trie, also on Moodle QA Testing Site (http://qa.moodle.net). Please test it using the Google Docs repository in a course resource, you will get this error:

        Invalid JSON string
        Fatal error:  Call to a member function attributes() on a non-object in /html/lib/googleapi.php on line 113
        

        I think the problem is in this document (https://docs.google.com/file/d/0B0yWUWGvDI4-a05kRHZnUTlTald5QjFDaVNLQkNBdw/preview) i don´t know why, but is the only document i have that has this issue, so i think is some kind of problem of google docs, try this:

        1) With your personal google account go to this URL: "https://docs.google.com/file/d/0B0yWUWGvDI4-a05kRHZnUTlTald5QjFDaVNLQkNBdw/preview". This document is a normal PDF, i don´t know who create it but is public access.
        2) Now you can organize this document or put it in your favorite folder with the star icon (these icons are on the right side of the title). Do it!
        3) Go to your google drive web account, and now, you can see that your document is in your "drive".
        4) Go to moodle, try to add a resource using google docs repository with your personal google account and you get the error message!
        5) If you delete this document from your google drive account manually, you can use again the google repository on moodle.

        I don´t know if there is a problem inside pdf or is the way that google handle with it. This is the google xml response for this document, as you can see here is no $gdoc->content[0] attribute for this document.

        SimpleXMLElement Object
        (
            [id] => https://docs.google.com/feeds/id/pdf%3A0B0yWUWGvDI4-a05kRHZnUTlTald5QjFDaVNLQkNBdw
            [published] => 2012-03-26T12:55:51.195Z
            [updated] => 2012-04-02T11:21:58.307Z
            [category] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [scheme] => http://schemas.google.com/g/2005/labels
                                    [term] => http://schemas.google.com/g/2005/labels#viewed
                                    [label] => viewed
                                )
        
                        )
        
                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [scheme] => http://schemas.google.com/g/2005/labels
                                    [term] => http://schemas.google.com/g/2005/labels#restricted-download
                                    [label] => restricted-download
                                )
        
                        )
        
                    [2] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [scheme] => http://schemas.google.com/g/2005/labels
                                    [term] => http://schemas.google.com/g/2005/labels#shared
                                    [label] => shared
                                )
        
                        )
        
                    [3] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [scheme] => http://schemas.google.com/g/2005#kind
                                    [term] => http://schemas.google.com/docs/2007#pdf
                                    [label] => pdf
                                )
        
                        )
        
                )
        
            [title] => Curso Superior Metodología E-A y Tutor de prácticas.pdf
            [link] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [rel] => alternate
                                    [type] => text/html
                                    [href] => https://docs.google.com/file/d/0B0yWUWGvDI4-a05kRHZnUTlTald5QjFDaVNLQkNBdw/edit
                                )
        
                        )
        
                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [rel] => http://schemas.google.com/docs/2007#icon
                                    [type] => image/png
                                    [href] => https://ssl.gstatic.com/docs/doclist/images/icon_10_pdf_list.png
                                )
        
                        )
        
                    [2] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [rel] => http://schemas.google.com/docs/2007/thumbnail
                                    [type] => image/png
                                    [href] => https://lh4.googleusercontent.com/VPakST0877W9bV6kalNyhfVDEHBXcWDlESzcaBfdFhSC7QeCWKICy13QGDJiaz9X1SSsCMf15cN6scyK=s220
                                )
        
                        )
        
                    [3] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [rel] => self
                                    [type] => application/atom+xml
                                    [href] => https://docs.google.com/feeds/default/private/full/pdf%3A0B0yWUWGvDI4-a05kRHZnUTlTald5QjFDaVNLQkNBdw
                                )
        
                        )
        
                )
        
            [author] => SimpleXMLElement Object
                (
                    [name] => xxxxx
                    [email] => xxxxx@gmail.com
                )
        
        )
        

        Another solution to this issue on lib/googleapi.php (line 115) is to test if the $gdoc->content attribute exists, here is and example that actually work:

        case 'pdf':
        	$title  = (string)$gdoc->title;
        	if (isset($gdoc->content)) {
        		$source = (string)$gdoc->content[0]->attributes()->src;
        	}
        	else {
        		$source = (string)$gdoc->link[0]->attributes()->href;
        	}
        break;
        

        Hope this information is sufficient to reproduce and resolve this issue.

        Show
        David Cabrera Primo added a comment - - edited Hi Frédéric, I have been testing this issue to give a chance to reproduce it. First of all i have created a google account (user: moodledevtest@gmail.com, pass: moodledevtest) that actually fail on every 2.x moodle site that i have trie, also on Moodle QA Testing Site ( http://qa.moodle.net ). Please test it using the Google Docs repository in a course resource, you will get this error: Invalid JSON string Fatal error: Call to a member function attributes() on a non-object in /html/lib/googleapi.php on line 113 I think the problem is in this document ( https://docs.google.com/file/d/0B0yWUWGvDI4-a05kRHZnUTlTald5QjFDaVNLQkNBdw/preview ) i don´t know why, but is the only document i have that has this issue, so i think is some kind of problem of google docs, try this: 1) With your personal google account go to this URL: "https://docs.google.com/file/d/0B0yWUWGvDI4-a05kRHZnUTlTald5QjFDaVNLQkNBdw/preview". This document is a normal PDF, i don´t know who create it but is public access. 2) Now you can organize this document or put it in your favorite folder with the star icon (these icons are on the right side of the title). Do it! 3) Go to your google drive web account, and now, you can see that your document is in your "drive". 4) Go to moodle, try to add a resource using google docs repository with your personal google account and you get the error message! 5) If you delete this document from your google drive account manually, you can use again the google repository on moodle. I don´t know if there is a problem inside pdf or is the way that google handle with it. This is the google xml response for this document, as you can see here is no $gdoc->content [0] attribute for this document. SimpleXMLElement Object ( [id] => https: //docs.google.com/feeds/id/pdf%3A0B0yWUWGvDI4-a05kRHZnUTlTald5QjFDaVNLQkNBdw [published] => 2012-03-26T12:55:51.195Z [updated] => 2012-04-02T11:21:58.307Z [category] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [scheme] => http: //schemas.google.com/g/2005/labels [term] => http: //schemas.google.com/g/2005/labels#viewed [label] => viewed ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [scheme] => http: //schemas.google.com/g/2005/labels [term] => http: //schemas.google.com/g/2005/labels#restricted-download [label] => restricted-download ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [scheme] => http: //schemas.google.com/g/2005/labels [term] => http: //schemas.google.com/g/2005/labels#shared [label] => shared ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [scheme] => http: //schemas.google.com/g/2005#kind [term] => http: //schemas.google.com/docs/2007#pdf [label] => pdf ) ) ) [title] => Curso Superior Metodología E-A y Tutor de prácticas.pdf [link] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [rel] => alternate [type] => text/html [href] => https: //docs.google.com/file/d/0B0yWUWGvDI4-a05kRHZnUTlTald5QjFDaVNLQkNBdw/edit ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [rel] => http: //schemas.google.com/docs/2007#icon [type] => image/png [href] => https: //ssl.gstatic.com/docs/doclist/images/icon_10_pdf_list.png ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [rel] => http: //schemas.google.com/docs/2007/thumbnail [type] => image/png [href] => https: //lh4.googleusercontent.com/VPakST0877W9bV6kalNyhfVDEHBXcWDlESzcaBfdFhSC7QeCWKICy13QGDJiaz9X1SSsCMf15cN6scyK=s220 ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [rel] => self [type] => application/atom+xml [href] => https: //docs.google.com/feeds/ default / private /full/pdf%3A0B0yWUWGvDI4-a05kRHZnUTlTald5QjFDaVNLQkNBdw ) ) ) [author] => SimpleXMLElement Object ( [name] => xxxxx [email] => xxxxx@gmail.com ) ) Another solution to this issue on lib/googleapi.php (line 115) is to test if the $gdoc->content attribute exists, here is and example that actually work: case 'pdf': $title = (string)$gdoc->title; if (isset($gdoc->content)) { $source = (string)$gdoc->content[0]->attributes()->src; } else { $source = (string)$gdoc->link[0]->attributes()->href; } break ; Hope this information is sufficient to reproduce and resolve this issue.
        Hide
        Frédéric Massart added a comment -

        Thanks a lot for your thorough feedback David, it was very helpful to identify the issue!

        The PDF document has been set as 'Download restricted', although it is viewable online there is no way for the users to download it, which explains the absence of content. Unfortunately using the link element instead does not do the trick.

        I have altered the code so that all the files are displayed, whether or not they can be downloaded, but an error will be displayed to the user if he attempts to download the file.

        Show
        Frédéric Massart added a comment - Thanks a lot for your thorough feedback David, it was very helpful to identify the issue! The PDF document has been set as 'Download restricted', although it is viewable online there is no way for the users to download it, which explains the absence of content. Unfortunately using the link element instead does not do the trick. I have altered the code so that all the files are displayed, whether or not they can be downloaded, but an error will be displayed to the user if he attempts to download the file.
        Hide
        David Cabrera Primo added a comment -

        Now it works fine!.

        Thank you very much!

        Show
        David Cabrera Primo added a comment - Now it works fine!. Thank you very much!
        Hide
        Rajesh Taneja added a comment -

        Patch looks good Fred,

        Feel free to push it for integration, after adding Test instructions.

        Show
        Rajesh Taneja added a comment - Patch looks good Fred, Feel free to push it for integration, after adding Test instructions.
        Hide
        Eloy Lafuente (stronk7) added a comment -

        The main moodle.git repository has just been updated with latest weekly modifications. You may wish to rebase your PULL branches to simplify history and avoid any possible merge conflicts. This would also make integrator's life easier next week.

        TIA and ciao

        Show
        Eloy Lafuente (stronk7) added a comment - The main moodle.git repository has just been updated with latest weekly modifications. You may wish to rebase your PULL branches to simplify history and avoid any possible merge conflicts. This would also make integrator's life easier next week. TIA and ciao
        Hide
        Eloy Lafuente (stronk7) added a comment -

        Integrated (22, 23 & master), thanks!

        Show
        Eloy Lafuente (stronk7) added a comment - Integrated (22, 23 & master), thanks!
        Hide
        Eloy Lafuente (stronk7) added a comment -

        Hi Fred,

        I've followed the instructions and have uploaded 2 files to my google docs account (all.png and den.png) and have set the later to "Prevent viewers to download".

        But, then, both from the private files area or for any other place (editor...) I can view both once connected to the google docs repo and ALSO pick them without any problem (so they are copied to Moodle FS). And then I can download any without problem at all.

        So, or I'm missing something or that "Prevent viewers to download" does not have any effect from Moodle.

        Could you clarify, please?

        Show
        Eloy Lafuente (stronk7) added a comment - Hi Fred, I've followed the instructions and have uploaded 2 files to my google docs account (all.png and den.png) and have set the later to "Prevent viewers to download". But, then, both from the private files area or for any other place (editor...) I can view both once connected to the google docs repo and ALSO pick them without any problem (so they are copied to Moodle FS). And then I can download any without problem at all. So, or I'm missing something or that "Prevent viewers to download" does not have any effect from Moodle. Could you clarify, please?
        Hide
        Frédéric Massart added a comment -

        Interesting. Actually, it's not something that we are supposed to prevent, it's something that we handle. When the file is really restricted from downloading, then Google API does not return the source, and therefore we can't download it. I assume that using a PNG (which is public) but with download restriction does not make sense, otherwise you would not even be able to see the image. If you try again with a PDF, which can be visualised via Google itself, you should be able to reproduce the test instructions behaviour.

        Show
        Frédéric Massart added a comment - Interesting. Actually, it's not something that we are supposed to prevent, it's something that we handle. When the file is really restricted from downloading, then Google API does not return the source, and therefore we can't download it. I assume that using a PNG (which is public) but with download restriction does not make sense, otherwise you would not even be able to see the image. If you try again with a PDF, which can be visualised via Google itself, you should be able to reproduce the test instructions behaviour.
        Hide
        Eloy Lafuente (stronk7) added a comment -

        Passing, after some tests with Fred (it seems that they must be shared files, to get the "cannot download" message in action). Thanks!

        Show
        Eloy Lafuente (stronk7) added a comment - Passing, after some tests with Fred (it seems that they must be shared files, to get the "cannot download" message in action). Thanks!
        Hide
        Eloy Lafuente (stronk7) added a comment -

        Many thanks for the hard work.

        These changes have been spread upstream and are already available in the git and cvs repositories.

        Ciao

        Show
        Eloy Lafuente (stronk7) added a comment - Many thanks for the hard work. These changes have been spread upstream and are already available in the git and cvs repositories. Ciao

          People

          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: