]> source.dussan.org Git - nextcloud-server.git/commitdiff
publiclink plugin
authorRobin Appelman <icewind1991@gmail.com>
Sat, 16 Apr 2011 10:35:21 +0000 (12:35 +0200)
committerRobin Appelman <icewind1991@gmail.com>
Sat, 16 Apr 2011 10:37:13 +0000 (12:37 +0200)
Allows sharing files by creating a public link, no gui yet.

plugins/publiclink/db_structure.xml [new file with mode: 0644]
plugins/publiclink/getfile.php [new file with mode: 0644]
plugins/publiclink/lib_public.php [new file with mode: 0644]
plugins/publiclink/makelink.php [new file with mode: 0644]
plugins/publiclink/plugin.xml [new file with mode: 0755]

diff --git a/plugins/publiclink/db_structure.xml b/plugins/publiclink/db_structure.xml
new file mode 100644 (file)
index 0000000..de63b03
--- /dev/null
@@ -0,0 +1,47 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<database>
+        <name>*dbname*</name>
+        <create>true</create>
+        <overwrite>false</overwrite>
+        <charset>latin1</charset>
+        <table>
+               <name>*dbprefix*publiclink</name>
+               <declaration>
+                       <field>
+                               <name>token</name>
+                               <type>text</type>
+                               <default></default>
+                               <notnull>true</notnull>
+                               <length>40</length>
+                       </field>
+                       <field>
+                               <name>path</name>
+                               <type>text</type>
+                               <default></default>
+                               <notnull>true</notnull>
+                               <length>128</length>
+                       </field>
+                       <field>
+                               <name>user</name>
+                               <type>text</type>
+                               <default>
+                               </default>
+                               <notnull>true</notnull>
+                               <length>64</length>
+                        </field>
+                        <field>
+                               <name>expire_time</name>
+                               <type>timestamp</type>
+                               <notnull>true</notnull>
+                       </field>
+                       <index>
+                               <name>token</name>
+                               <unique>true</unique>
+                               <field>
+                                       <name>token</name>
+                                       <sorting>ascending</sorting>
+                               </field>
+                       </index>
+               </declaration>
+       </table>
+</database>
diff --git a/plugins/publiclink/getfile.php b/plugins/publiclink/getfile.php
new file mode 100644 (file)
index 0000000..c579dc9
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+$RUNTIME_NOAPPS=true; //no need to load the apps
+
+require_once '../../lib/base.php';
+
+require_once 'lib_public.php';
+
+$token=$_GET['token'];
+OC_PublicLink::downloadFile($token);
+?>
\ No newline at end of file
diff --git a/plugins/publiclink/lib_public.php b/plugins/publiclink/lib_public.php
new file mode 100644 (file)
index 0000000..494f84f
--- /dev/null
@@ -0,0 +1,77 @@
+<?php
+class OC_PublicLink{
+       /**
+        * create a new public link
+        * @param string path
+        * @param int (optional) expiretime time the link expires, as timestamp
+        */
+       public function __construct($path,$expiretime=0){
+               if($path && OC_FILESYSTEM::file_exists($path)){
+                       $token=sha1("$path-$expiretime");
+                       $user=$_SESSION['user_id'];
+                       $query=OC_DB::prepare("INSERT INTO *PREFIX*publiclink VALUES(?,?,?,?)");
+                       $result=$query->execute(array($token,$path,$user,$expiretime));
+                       if( PEAR::isError($result)) {
+                               $entry = 'DB Error: "'.$result->getMessage().'"<br />';
+                               $entry .= 'Offending command was: '.$result->getDebugInfo().'<br />';
+                               error_log( $entry );
+                               die( $entry );
+                       }
+                       $this->token=$token;
+               }
+       }
+       
+       /**
+        * download a file shared by a public link
+        * @param string token
+        */
+       public static function downloadFile($token){
+               //remove expired links
+               $query=OC_DB::prepare("DELETE FROM *PREFIX*publiclink WHERE expire_time < NOW() AND expire_time!=0");
+               $query->execute();
+               
+               //get the path and the user
+               $query=OC_DB::prepare("SELECT user,path FROM *PREFIX*publiclink WHERE token=?");
+               $result=$query->execute(array($token));
+               $data=$result->fetchAll();
+               if(count($data)>0){
+                       $path=$data[0]['path'];
+                       $user=$data[0]['user'];
+                       
+                       //login
+                       $_SESSION['user_id']=$user;
+                       
+                       //prepare the filesystem
+                       OC_UTIL::setupFS();
+                       
+                       //get time mimetype and set the headers
+                       $mimetype=OC_FILESYSTEM::getMimeType($path);
+       //              header('Content-Disposition: attachment; filename="'.basename($path).'"');
+                       header('Content-Transfer-Encoding: binary');
+                       header('Expires: 0');
+                       header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
+                       header('Pragma: public');
+                       header('Content-Type: ' . $mimetype);
+                       header('Content-Length: ' . OC_FILESYSTEM::filesize($path));
+                       
+                       //download the file
+                       ob_clean();
+                       OC_FILESYSTEM::readfile($path);
+               }else{
+                       header("HTTP/1.0 404 Not Found");
+                       echo '404 Not Found';
+                       die();
+               }
+       }
+       
+       /**
+        * get the token for the public link
+        * @return string
+        */
+       public function getToken(){
+               return $this->token;
+       }
+       
+       private $token;
+}
+?>
\ No newline at end of file
diff --git a/plugins/publiclink/makelink.php b/plugins/publiclink/makelink.php
new file mode 100644 (file)
index 0000000..1de65e7
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+$RUNTIME_NOAPPS=true; //no need to load the apps
+
+require_once '../../lib/base.php';
+
+require_once 'lib_public.php';
+
+$path=$_GET['path'];
+$expire=(isset($_GET['expire']))?$_GET['expire']:0;
+
+$link=new OC_PublicLink($path,$expire);
+echo $link->getToken();
+?>
\ No newline at end of file
diff --git a/plugins/publiclink/plugin.xml b/plugins/publiclink/plugin.xml
new file mode 100755 (executable)
index 0000000..75abed6
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0"?>
+<plugin version="1.0">
+       <info>
+               <id>publiclink</id>
+               <name>Simple file sharing by creating a public link to a file</name>
+               <version>0.1</version>
+               <licence>AGPL</licence>
+               <author>Robin Appelman</author>
+               <require>1.1</require>
+       </info>
+       <runtime>
+               <include>lib_public.php</include>
+       </runtime>
+       <install>
+               <database>db_structure.xml</database>
+       </install>
+</plugin>