PHP fileUpoad

From OpenKM Documentation
Jump to: navigation, search

Configurable shell script that uses PHP support to import files.


Nota clasica.png This code works on OpenKM 5.1.x but won't run on OpenKM 6.2.x.

  <?php
  $okmurl="http://localhost:8080/OpenKM/";
  $okmid="username";
  $okmpw="password";
  $impfile = $argv[1];
 
  $OKMAuth = new SoapClient($okmurl."OKMAuth?wsdl");
  $OKMDocument = new SoapClient($okmurl."OKMDocument?wsdl");
 
  // open file and encode if necessary
  $handle = fopen($impfile,'rb');
  $file_content = fread($handle,filesize($impfile));
  fclose($handle);
  $encoded = base64_encode($file_content);
 
  // Login
  $token = $OKMAuth->login($okmid, $okmpw);
  $OKMDocument->createSimple($token,"/okm:root/file.to.upload",$file_content);
 
  // Logout
  $OKMAuth->logout($token);
?>
#!/bin/bash
# JOAKO Import 1.0.0 - Copyright 2011 Andrew Joakimsen
# Distributed under the terms of the GNU General Public
# License Version 2 and no future versions.
#
# Script to import files into OpenKM and report error/success.
# Requires the PHP import script written by snowman, with a
# few small tweaks.
 
###################################
###                             ###
###  C O N F I G U R A T I O N  ###
###                             ###
###################################
 
# OpenKM directory where files are imported to
OKM_IMPORT_DIR=NEW
 
# Filesystem directory (no trailing slash) where we put imported files (it will be created)
STORE_DIR=~/we-test
 
# Filesystem directory (no trailing slash) where we put files that encountered import errors
ERROR_STORE_DIR=~/error-test
 
####################################
# First let's see if the runtime condition is sane
if [ -z "$1" ]; then
    echo "JOAKO Import: Missing file name"
    echo "Usage: import.sh [filename] [target-directory]"
    exit 1
fi
 
# If ARG1 is a real file
if [ -d "$1" ]; then
    echo "ERROR: \"$1\" is a directory!"
    exit 1
fi
if [ ! -f "$1" ]; then
    echo "ERROR: \"$1\" Invalid file name "
    exit 1
else
    INFILE=$1
fi
 
# Check if OKM_IMPORT_DIR was passed as argument
if [ -z "$2" ]; then
    OKM_DIR=$OKM_IMPORT_DIR
else
    OKM_DIR=$2
fi
 
# Check if the imported files directory exsists
if [ ! -d $STORE_DIR/$OKM_DIR ]; then
    if [ ! -d ~/$STORE_DIR ]; then
        mkdir $STORE_DIR
    fi
    mkdir $STORE_DIR/$OKM_DIR
fi
 
# Run the import Process
RESULT=$(php /opt/openkm/import.php $INFILE ${INFILE##*/} $OKM_DIR 2>&1)
RESULTCODE=$?
 
# Deal with the consequences
function importer-error {
    # Check if the import error directory exsists
    if [ ! -d $ERROR_STORE_DIR/$OKM_DIR ]; then
        if [ ! -d $ERROR_STORE_DIR ]; then
            mkdir $ERROR_STORE_DIR
        fi
        mkdir $ERROR_STORE_DIR/$OKM_DIR
    fi
    echo "`date` JOAKO Import Encountered an error type $RESULTCODE."
    echo "$RESULT"
    mv $INFILE $ERROR_STORE_DIR/$OKM_DIR &> /dev/null
    exit $?
}
 
if [ $RESULTCODE -ne 0 ]; then # There was an error
    importer-error
else # We check further
    if [ -n "$RESULT" ]; then # There was an error
        importer-error
    else # No error detected
        echo "`date` JOAKO Import: Imported file [${INFILE##*/}] to [$OKM_DIR] directory"
        mv $INFILE $STORE_DIR/$OKM_DIR &> /dev/null
        exit 0
    fi
fi