Code cleanup, fixed upload.php

This commit is contained in:
Christoph Haas 2016-11-30 12:31:42 +01:00
parent 456565c89c
commit 44c0435175
4 changed files with 167 additions and 88 deletions

View File

@ -2,8 +2,9 @@
<project version="4"> <project version="4">
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="aae153d8-808f-4371-b0e9-9ee7ff904cab" name="Default" comment=""> <list default="true" id="aae153d8-808f-4371-b0e9-9ee7ff904cab" name="Default" comment="">
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" /> <change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/php/helper.php" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/php/download.php" afterPath="$PROJECT_DIR$/php/download.php" /> <change type="MODIFICATION" beforePath="$PROJECT_DIR$/php/module.contact.php" afterPath="$PROJECT_DIR$/php/module.contact.php" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/php/upload.php" afterPath="$PROJECT_DIR$/php/upload.php" />
</list> </list>
<ignored path="contactimporter.iws" /> <ignored path="contactimporter.iws" />
<ignored path=".idea/workspace.xml" /> <ignored path=".idea/workspace.xml" />
@ -24,11 +25,46 @@
</component> </component>
<component name="FileEditorManager"> <component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300"> <leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file leaf-file-name="download.php" pinned="false" current-in-tab="true"> <file leaf-file-name="download.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/php/download.php"> <entry file="file://$PROJECT_DIR$/php/download.php">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="90"> <state relative-caret-position="300">
<caret line="27" column="22" selection-start-line="27" selection-start-column="22" selection-end-line="27" selection-end-column="22" /> <caret line="41" column="9" selection-start-line="41" selection-start-column="9" selection-end-line="41" selection-end-column="9" />
<folding />
</state>
</provider>
</entry>
</file>
<file leaf-file-name="upload.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/php/upload.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="271">
<caret line="22" column="3" selection-start-line="22" selection-start-column="3" selection-end-line="22" selection-end-column="3" />
<folding>
<element signature="e#7#912#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file leaf-file-name="module.contact.php" pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/php/module.contact.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="234">
<caret line="275" column="22" selection-start-line="275" selection-start-column="22" selection-end-line="275" selection-end-column="22" />
<folding>
<element signature="e#6#919#0#PHP" expanded="true" />
<element signature="e#1009#1042#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file leaf-file-name="helper.php" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/php/helper.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="30">
<caret line="2" column="55" selection-start-line="2" selection-start-column="55" selection-end-line="2" selection-end-column="55" />
<folding /> <folding />
</state> </state>
</provider> </provider>
@ -42,20 +78,21 @@
<component name="IdeDocumentHistory"> <component name="IdeDocumentHistory">
<option name="CHANGED_PATHS"> <option name="CHANGED_PATHS">
<list> <list>
<option value="$PROJECT_DIR$/php/module.contact.php" />
<option value="$PROJECT_DIR$/php/plugin.contactimporter.php" /> <option value="$PROJECT_DIR$/php/plugin.contactimporter.php" />
<option value="$PROJECT_DIR$/config.php" /> <option value="$PROJECT_DIR$/config.php" />
<option value="$PROJECT_DIR$/js/ABOUT.js" /> <option value="$PROJECT_DIR$/js/ABOUT.js" />
<option value="$PROJECT_DIR$/js/dialogs/ImportContentPanel.js" /> <option value="$PROJECT_DIR$/js/dialogs/ImportContentPanel.js" />
<option value="$PROJECT_DIR$/js/dialogs/ImportPanel.js" /> <option value="$PROJECT_DIR$/js/dialogs/ImportPanel.js" />
<option value="$PROJECT_DIR$/js/data/ResponseHandler.js" /> <option value="$PROJECT_DIR$/js/data/ResponseHandler.js" />
<option value="$PROJECT_DIR$/php/upload.php" />
<option value="$PROJECT_DIR$/build.xml" /> <option value="$PROJECT_DIR$/build.xml" />
<option value="$PROJECT_DIR$/js/ui/ContextMenu.js" /> <option value="$PROJECT_DIR$/js/ui/ContextMenu.js" />
<option value="$PROJECT_DIR$/js/plugin.contactimporter.js" /> <option value="$PROJECT_DIR$/js/plugin.contactimporter.js" />
<option value="$PROJECT_DIR$/changelog.txt" /> <option value="$PROJECT_DIR$/changelog.txt" />
<option value="$PROJECT_DIR$/manifest.xml" /> <option value="$PROJECT_DIR$/manifest.xml" />
<option value="$PROJECT_DIR$/php/download.php" /> <option value="$PROJECT_DIR$/php/download.php" />
<option value="$PROJECT_DIR$/php/helper.php" />
<option value="$PROJECT_DIR$/php/upload.php" />
<option value="$PROJECT_DIR$/php/module.contact.php" />
</list> </list>
</option> </option>
</component> </component>
@ -286,12 +323,12 @@
<workItem from="1480428199144" duration="44000" /> <workItem from="1480428199144" duration="44000" />
<workItem from="1480428259775" duration="69000" /> <workItem from="1480428259775" duration="69000" />
<workItem from="1480428330068" duration="6691000" /> <workItem from="1480428330068" duration="6691000" />
<workItem from="1480504955898" duration="153000" /> <workItem from="1480504955898" duration="525000" />
</task> </task>
<servers /> <servers />
</component> </component>
<component name="TimeTrackingManager"> <component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="6957000" /> <option name="totallyTimeSpent" value="7329000" />
</component> </component>
<component name="ToolWindowManager"> <component name="ToolWindowManager">
<frame x="65" y="24" width="1792" height="999" extended-state="6" /> <frame x="65" y="24" width="1792" height="999" extended-state="6" />
@ -435,13 +472,6 @@
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/php/module.contact.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-16319">
<caret line="25" column="0" selection-start-line="25" selection-start-column="0" selection-end-line="25" selection-end-column="0" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/php/plugin.contactimporter.php"> <entry file="file://$PROJECT_DIR$/php/plugin.contactimporter.php">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="675"> <state relative-caret-position="675">
@ -449,13 +479,6 @@
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/php/upload.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="780">
<caret line="72" column="1" selection-start-line="72" selection-start-column="1" selection-end-line="72" selection-end-column="1" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/languages/en_US.UTF-8/LC_MESSAGES/plugin_calendarimporter.po"> <entry file="file://$PROJECT_DIR$/languages/en_US.UTF-8/LC_MESSAGES/plugin_calendarimporter.po">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="0"> <state relative-caret-position="0">
@ -545,12 +568,41 @@
</entry> </entry>
<entry file="file://$PROJECT_DIR$/php/download.php"> <entry file="file://$PROJECT_DIR$/php/download.php">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="90"> <state relative-caret-position="300">
<caret line="27" column="22" selection-start-line="27" selection-start-column="22" selection-end-line="27" selection-end-column="22" /> <caret line="41" column="9" selection-start-line="41" selection-start-column="9" selection-end-line="41" selection-end-column="9" />
<folding /> <folding />
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/php/helper.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="30">
<caret line="2" column="55" selection-start-line="2" selection-start-column="55" selection-end-line="2" selection-end-column="55" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/php/upload.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="271">
<caret line="22" column="3" selection-start-line="22" selection-start-column="3" selection-end-line="22" selection-end-column="3" />
<folding>
<element signature="e#7#912#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/php/module.contact.php">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="234">
<caret line="275" column="22" selection-start-line="275" selection-start-column="22" selection-end-line="275" selection-end-column="22" />
<folding>
<element signature="e#6#919#0#PHP" expanded="true" />
<element signature="e#1009#1042#0#PHP" expanded="true" />
</folding>
</state>
</provider>
</entry>
</component> </component>
<component name="masterDetails"> <component name="masterDetails">
<states> <states>

63
php/helper.php Normal file
View File

@ -0,0 +1,63 @@
<?php
/**
* helper.php, Kopano Webapp contact to vcf im/exporter
*
* Author: Christoph Haas <christoph.h@sprinternet.at>
* Copyright (C) 2012-2016 Christoph Haas
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
namespace contactimporter;
class Helper
{
/**
* Generates a random string with variable length.
*
* @param $length the lenght of the generated string, defaults to 6
* @return string a random string
*/
public static function randomstring($length = 6)
{
// $chars - all allowed charakters
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
srand((double)microtime() * 1000000);
$i = 0;
$pass = "";
while ($i < $length) {
$num = rand() % strlen($chars);
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
/**
* respond/echo JSON
*
* @param $arr
* @return string JSON encoded string
*/
public static function respondJSON($arr)
{
echo json_encode($arr);
}
}

View File

@ -21,11 +21,14 @@
* *
*/ */
include_once('vendor/autoload.php'); include_once(__DIR__ . "/vendor/autoload.php");
require_once(__DIR__ . "/helper.php");
use JeroenDesloovere\VCard\VCard; use JeroenDesloovere\VCard\VCard;
use JeroenDesloovere\VCard\VCardParser; use JeroenDesloovere\VCard\VCardParser;
use contactimporter\Helper;
class ContactModule extends Module class ContactModule extends Module
{ {
@ -93,28 +96,6 @@ class ContactModule extends Module
return $result; return $result;
} }
/**
* Generates a random string with variable length.
* @param $length the lenght of the generated string
* @return string a random string
*/
private function randomstring($length = 6)
{
// $chars - all allowed charakters
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
srand((double)microtime() * 1000000);
$i = 0;
$pass = "";
while ($i < $length) {
$num = rand() % strlen($chars);
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
/** /**
* Add an attachment to the give contact * Add an attachment to the give contact
* @param $actionType * @param $actionType
@ -295,7 +276,7 @@ class ContactModule extends Module
$error_msg = ""; $error_msg = "";
// write csv // write csv
$token = $this->randomstring(16); $token = Helper::randomstring(16);
$file = PLUGIN_CONTACTIMPORTER_TMP_UPLOAD . "vcf_" . $token . ".vcf"; $file = PLUGIN_CONTACTIMPORTER_TMP_UPLOAD . "vcf_" . $token . ".vcf";
file_put_contents($file, ""); file_put_contents($file, "");
@ -427,7 +408,7 @@ class ContactModule extends Module
if ($attachnum >= 0) { if ($attachnum >= 0) {
$attachment = $this->getAttachmentByAttachNum($message, $attachnum); // get first attachment only $attachment = $this->getAttachmentByAttachNum($message, $attachnum); // get first attachment only
$phototoken = $this->randomstring(16); $phototoken = Helper::randomstring(16);
$tmpphoto = PLUGIN_CONTACTIMPORTER_TMP_UPLOAD . "photo_" . $phototoken . ".jpg"; $tmpphoto = PLUGIN_CONTACTIMPORTER_TMP_UPLOAD . "photo_" . $phototoken . ".jpg";
$this->storeSavedAttachment($tmpphoto, $attachment); $this->storeSavedAttachment($tmpphoto, $attachment);
$vcard->addPhoto($tmpphoto, true); $vcard->addPhoto($tmpphoto, true);
@ -936,7 +917,7 @@ class ContactModule extends Module
if (!is_writable(TMP_PATH . "/")) { if (!is_writable(TMP_PATH . "/")) {
error_log("Can not write to export tmp directory!"); error_log("Can not write to export tmp directory!");
} else { } else {
$tmppath = TMP_PATH . "/" . $this->randomstring(15); $tmppath = TMP_PATH . "/" . Helper::randomstring(15);
if (isset($vCard->rawPhoto)) { if (isset($vCard->rawPhoto)) {
if (file_put_contents($tmppath, $vCard->rawPhoto)) { if (file_put_contents($tmppath, $vCard->rawPhoto)) {
$properties["internal_fields"]["x_photo_path"] = $tmppath; $properties["internal_fields"]["x_photo_path"] = $tmppath;

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* upload.php, Kopano Webapp contact to vcf im/exporter * upload.php, Kopano Webapp contact to vcf im/exporter
* *
@ -21,53 +22,35 @@
* *
*/ */
require_once("../config.php"); require_once(__DIR__ . "/../config.php");
require_once(__DIR__ . "/helper.php");
require_once(__DIR__ . '/../../../init.php');
require_once(__DIR__ . "/../../../server/includes/core/class.webappauthentication.php"); // for checking the session
use contactimporter\Helper;
/* disable error printing - otherwise json communication might break... */ /* disable error printing - otherwise json communication might break... */
ini_set('display_errors', '0'); ini_set('display_errors', '0');
/** // check session
* respond/echo JSON // otherwise a DOS attack might be possible
* @param $arr if (!WebAppAuthentication::getUserName() || WebAppAuthentication::getUserName() == "") {
*/ Helper::respondJSON(array('success' => false, 'error' => dgettext("plugin_contactimporter", "Not authenticated!")));
function respondJSON($arr) die();
{
echo json_encode($arr);
} }
/**
* Generates a random string with variable length.
* @param $length the lenght of the generated string
* @return string a random string
*/
function randomstring($length = 6)
{
// $chars - all allowed charakters
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
srand((double)microtime() * 1000000);
$i = 0;
$pass = "";
while ($i < $length) {
$num = rand() % strlen($chars);
$tmp = substr($chars, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
$destpath = PLUGIN_CONTACTIMPORTER_TMP_UPLOAD;
$destpath .= $_FILES['vcfdata']['name'] . randomstring();
if (is_readable($_FILES['vcfdata']['tmp_name'])) { if (is_readable($_FILES['vcfdata']['tmp_name'])) {
$result = move_uploaded_file($_FILES['vcfdata']['tmp_name'], $destpath); $dstPath = PLUGIN_CONTACTIMPORTER_TMP_UPLOAD;
$dstPath .= $_FILES['vcfdata']['name'] . Helper::randomstring();
$result = move_uploaded_file($_FILES['vcfdata']['tmp_name'], $dstPath);
if ($result) { if ($result) {
respondJSON(array('success' => true, 'vcf_file' => $destpath)); Helper::respondJSON(array('success' => true, 'vcf_file' => $dstPath));
} else { } else {
respondJSON(array('success' => false, 'error' => dgettext("plugin_contactimporter", "File could not be moved to TMP path! Check plugin config and folder permissions!"))); Helper::respondJSON(array('success' => false, 'error' => dgettext("plugin_contactimporter", "File could not be moved to TMP path! Check plugin config and folder permissions!")));
} }
} else { } else {
respondJSON(array('success' => false, 'error' => dgettext("plugin_contactimporter", "File could not be read by server, upload error!"))); Helper::respondJSON(array('success' => false, 'error' => dgettext("plugin_contactimporter", "File could not be read by server, upload error!")));
} }