diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6796ac8..cdd15e0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,9 @@ - - + + + @@ -24,11 +25,46 @@ - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -42,20 +78,21 @@ @@ -286,12 +323,12 @@ - + - @@ -435,13 +472,6 @@ - - - - - - - @@ -449,13 +479,6 @@ - - - - - - - @@ -545,12 +568,41 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/php/helper.php b/php/helper.php new file mode 100644 index 0000000..187c73c --- /dev/null +++ b/php/helper.php @@ -0,0 +1,63 @@ + + * 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); + } +} \ No newline at end of file diff --git a/php/module.contact.php b/php/module.contact.php index 1e471c9..93e3042 100644 --- a/php/module.contact.php +++ b/php/module.contact.php @@ -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\VCardParser; +use contactimporter\Helper; + class ContactModule extends Module { @@ -93,28 +96,6 @@ class ContactModule extends Module 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 * @param $actionType @@ -295,7 +276,7 @@ class ContactModule extends Module $error_msg = ""; // write csv - $token = $this->randomstring(16); + $token = Helper::randomstring(16); $file = PLUGIN_CONTACTIMPORTER_TMP_UPLOAD . "vcf_" . $token . ".vcf"; file_put_contents($file, ""); @@ -427,7 +408,7 @@ class ContactModule extends Module if ($attachnum >= 0) { $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"; $this->storeSavedAttachment($tmpphoto, $attachment); $vcard->addPhoto($tmpphoto, true); @@ -936,7 +917,7 @@ class ContactModule extends Module if (!is_writable(TMP_PATH . "/")) { error_log("Can not write to export tmp directory!"); } else { - $tmppath = TMP_PATH . "/" . $this->randomstring(15); + $tmppath = TMP_PATH . "/" . Helper::randomstring(15); if (isset($vCard->rawPhoto)) { if (file_put_contents($tmppath, $vCard->rawPhoto)) { $properties["internal_fields"]["x_photo_path"] = $tmppath; diff --git a/php/upload.php b/php/upload.php index ab9facb..e05736b 100644 --- a/php/upload.php +++ b/php/upload.php @@ -1,4 +1,5 @@ false, 'error' => dgettext("plugin_contactimporter", "Not authenticated!"))); + die(); } -/** - * 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'])) { - $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) { - respondJSON(array('success' => true, 'vcf_file' => $destpath)); + Helper::respondJSON(array('success' => true, 'vcf_file' => $dstPath)); } 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 { - 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!"))); } \ No newline at end of file