EIOS/inc/docs_google.php

110 lines
4.0 KiB
PHP
Raw Normal View History

2023-12-28 15:39:22 +03:00
<?php
error_reporting(E_ALL);
set_include_path(get_include_path(). PATH_SEPARATOR .'/var/www/user1999310/data/www/mkgtu.ru/learn/inc/Zend/');
require_once('/var/www/user1999310/data/www/mkgtu.ru/learn/inc/Zend/Loader.php');
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_Query');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Docs');
Zend_Loader::loadClass('Zend_Gdata_App_IOException');
$username='docs.mkgtu';
$password='1Q2w3e4r5t';
if (isset($_GET['upload'])){//загрузка
$service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
$httpClient = Zend_Gdata_ClientLogin::getHttpClient($username, $password, $service);
$docs = new Zend_Gdata_Docs($httpClient);
$googledoc = 'Resume.doc';
$out = uploadDocument($docs, $googledoc,'');
//echo '<pre>'.print_r($out,1).'</pre>';
}
if (isset($_GET['view'])){//просмотр
$service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
$httpClient = Zend_Gdata_ClientLogin::getHttpClient($username,$password,$service);
$docs = new Zend_Gdata_Docs($httpClient);
$feed = $docs->getDocumentListFeed();
foreach ($feed->entries as $entry) {
$titleid=$entry->title;
$fileid=$entry->id;
$cut = substr($fileid, 64);
echo '<a href="?view&id='.$cut.'">'.$titleid.'</a> '.$entry->id.'<BR>';
$contentLink = $entry->content->getSrc();
$fileContents = download($docs, $contentLink, 'html');
echo 'Contents of document "' . $entry->title . '":<hr>';
echo '<pre>'.$fileContents.'</pre>';
}
if (isset($_GET['id'])){//просмотр шв
//echo '<iframe src="http://docs.google.com/viewer?url=http://mysite.asd.com/test.zip&embedded=true" style="margin:0 auto; width:800px; height:800px;" frameborder="0"></iframe>';
//echo '<iframe src="https://docs.google.com/document/d/'.$_GET['id'].'/view" style="margin:0 auto; width:800px; height:800px;" frameborder="0"></iframe>';
//echo '<pre>'.print_r($this->getDoc($_GET['id'], 'document'),1).'</pre>';
// TODO 1: setup a Zend_Gdata_Docs client in $docs_client
// TODO 2: fetch a $feed or $entry
/*
$contentLink = $feed->entries[0]->content->getSrc();
$fileContents = download($docs, $contentLink, 'doc');
echo 'Contents of document "' . $feed->entries[0]->title . '":<hr>';
echo '<pre>'.print_r($fileContents).'</pre>';
*/
}
/*
A1iLc-Uiy2nHHM00x_0XjJ_uS3QSWwL4TiIKxGws9ZtNA - Resume
A1_zGiFD6FX2P4Gx0Z8krP17Bh28zYgbTDk363SnyAmfs - Resume
A1Dg04E36gIB1-OSopBOtQiggTsPzQ_IGvbyLF6pgYzIY - Resume
A1Tp5jI1v5xZMYNZgRgyYxV_hLAJsfIiMiHW4NtTEj1Ss - Новый документ
https://docs.google.com/document/d/1Tp5jI1v5xZMYNZgRgyYxV_hLAJsfIiMiHW4NtTEj1Ss/edit
*/
}
//ПОЛУЧЕНИЕ ФАЙЛА В ВИДЕ $format - txt, html
function download($client, $url, $format=null) {
$sessionToken = $client->getHttpClient()->getAuthSubToken();
$opts = array(
'http' => array(
'method' => 'GET',
'header' => "GData-Version: 3.0\r\n".
"Authorization: AuthSub token=\"$sessionToken\"\r\n"
)
);
if ($url != null) {
$url = $url . "&exportFormat=$format";
}
return file_get_contents($url, false, stream_context_create($opts));
}
//загрузка файла в доки
function uploadDocument($docs, $originalFileName,$temporaryFileLocation) {
$out = array();
$fileToUpload = $originalFileName;
if ($temporaryFileLocation) { $fileToUpload = $temporaryFileLocation; }
/*//удаление файлов перед загрузкой
$query = new Zend_Gdata_Docs_Query();
$query->setTitle($originalFileName);
$query->setTitleExact("true");
try {
$doc = $docs->getDocumentListEntry($query);
$docExists = true;
} catch (Exception $e) {
$docExists = false;
}
if ($docExists) {
//echo 'Удаляю предыдущую версию документа '.$doc->title.'\n';
$doc->delete();
}
*/
$newDocumentEntry = $docs->uploadFile($fileToUpload, $originalFileName,null, Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI);
$out['title'] = $newDocumentEntry->title;
foreach ($newDocumentEntry->link as $link) {
if ($link->getRel() === 'alternate') {
$out['link'] = $link->getHref();
}
}
return $out;
}
?>