示例#1
0
 /**
  * @NoAdminRequired
  */
 public function start()
 {
     $request = $this->request;
     $response = new JSONResponse();
     $params = $this->request->urlParams;
     $app = new App($this->api->getUserId());
     $addressBook = $app->getAddressBook($params['backend'], $params['addressBookId']);
     if (!$addressBook->hasPermission(\OCP\PERMISSION_CREATE)) {
         $response->setStatus('403');
         $response->bailOut(App::$l10n->t('You do not have permissions to import into this address book.'));
         return $response;
     }
     $filename = isset($request->post['filename']) ? $request->post['filename'] : null;
     $progresskey = isset($request->post['progresskey']) ? $request->post['progresskey'] : null;
     if (is_null($filename)) {
         $response->bailOut(App::$l10n->t('File name missing from request.'));
         return $response;
     }
     if (is_null($progresskey)) {
         $response->bailOut(App::$l10n->t('Progress key missing from request.'));
         return $response;
     }
     $filename = strtr($filename, array('/' => '', "\\" => ''));
     if (\OC\Files\Filesystem::isFileBlacklisted($filename)) {
         $response->bailOut(App::$l10n->t('Attempt to access blacklisted file:') . $filename);
         return $response;
     }
     $view = \OCP\Files::getStorage('contacts');
     $proxyStatus = \OC_FileProxy::$enabled;
     \OC_FileProxy::$enabled = false;
     $file = $view->file_get_contents('/imports/' . $filename);
     \OC_FileProxy::$enabled = $proxyStatus;
     $writeProgress = function ($pct) use($progresskey) {
         \OC_Cache::set($progresskey, $pct, 300);
     };
     $cleanup = function () use($view, $filename, $progresskey) {
         if (!$view->unlink('/imports/' . $filename)) {
             $response->debug('Unable to unlink /imports/' . $filename);
         }
         \OC_Cache::remove($progresskey);
     };
     $writeProgress('20');
     $nl = "\n";
     $file = str_replace(array("\r", "\n\n"), array("\n", "\n"), $file);
     $lines = explode($nl, $file);
     $inelement = false;
     $parts = array();
     $card = array();
     foreach ($lines as $line) {
         if (strtoupper(trim($line)) == 'BEGIN:VCARD') {
             $inelement = true;
         } elseif (strtoupper(trim($line)) == 'END:VCARD') {
             $card[] = $line;
             $parts[] = implode($nl, $card);
             $card = array();
             $inelement = false;
         }
         if ($inelement === true && trim($line) != '') {
             $card[] = $line;
         }
     }
     if (count($parts) === 0) {
         $response->bailOut(App::$l10n->t('No contacts found in: ') . $filename);
         $cleanup();
         return $response;
     }
     //import the contacts
     $imported = 0;
     $failed = 0;
     $partially = 0;
     $processed = 0;
     // TODO: Add a new group: "Imported at {date}"
     foreach ($parts as $part) {
         try {
             $vcard = VObject\Reader::read($part);
         } catch (VObject\ParseException $e) {
             try {
                 $vcard = VObject\Reader::read($part, VObject\Reader::OPTION_IGNORE_INVALID_LINES);
                 $partially += 1;
                 $response->debug('Import: Retrying reading card. Error parsing VCard: ' . $e->getMessage());
             } catch (\Exception $e) {
                 $failed += 1;
                 $response->debug('Import: skipping card. Error parsing VCard: ' . $e->getMessage());
                 continue;
                 // Ditch cards that can't be parsed by Sabre.
             }
         }
         try {
             $vcard->validate(MyVCard::REPAIR | MyVCard::UPGRADE);
         } catch (\Exception $e) {
             \OCP\Util::writeLog('contacts', __METHOD__ . ' ' . 'Error validating vcard: ' . $e->getMessage(), \OCP\Util::ERROR);
             $failed += 1;
         }
         /**
          * TODO
          * - Check if a contact with identical UID exists.
          * - If so, fetch that contact and call $contact->mergeFromVCard($vcard);
          * - Increment $updated var (not present yet.)
          * - continue
          */
         try {
             if ($addressBook->addChild($vcard)) {
                 $imported += 1;
             } else {
                 $failed += 1;
             }
         } catch (\Exception $e) {
             $response->debug('Error importing vcard: ' . $e->getMessage() . $nl . $vcard->serialize());
             $failed += 1;
         }
         $processed += 1;
         $writeProgress($processed);
     }
     //done the import
     sleep(3);
     // Give client side a chance to read the progress.
     $response->setParams(array('backend' => $params['backend'], 'addressBookId' => $params['addressBookId'], 'imported' => $imported, 'partially' => $partially, 'failed' => $failed));
     return $response;
 }
示例#2
0
 public function cacheThumbnail(\OCP\Image $image = null, $remove = false, $update = false)
 {
     $key = self::THUMBNAIL_PREFIX . $this->combinedKey();
     //\OC_Cache::remove($key);
     if (\OC_Cache::hasKey($key) && $image === null && $remove === false && $update === false) {
         return \OC_Cache::get($key);
     }
     if ($remove) {
         \OC_Cache::remove($key);
         if (!$update) {
             return false;
         }
     }
     if (is_null($image)) {
         $this->retrieve();
         $image = new \OCP\Image();
         if (!isset($this->PHOTO) && !isset($this->LOGO)) {
             return false;
         }
         if (!$image->loadFromBase64((string) $this->PHOTO)) {
             if (!$image->loadFromBase64((string) $this->LOGO)) {
                 return false;
             }
         }
     }
     if (!$image->centerCrop()) {
         \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t crop thumbnail for ID ' . $key, \OCP\Util::ERROR);
         return false;
     }
     if (!$image->resize(self::THUMBNAIL_SIZE)) {
         \OCP\Util::writeLog('contacts', __METHOD__ . '. Couldn\'t resize thumbnail for ID ' . $key, \OCP\Util::ERROR);
         return false;
     }
     // Cache as base64 for around a month
     \OC_Cache::set($key, strval($image), 3000000);
     \OCP\Util::writeLog('contacts', 'Caching ' . $key, \OCP\Util::DEBUG);
     return \OC_Cache::get($key);
 }
示例#3
0
        }
    }
    exit;
}
foreach ($parts as $part) {
    try {
        $vcard = Sabre\VObject\Reader::read($part);
    } catch (Exception $e) {
        $failed += 1;
        OCP\Util::writeLog('contacts', 'Import: skipping card. Error parsing VCard: ' . $e->getMessage(), OCP\Util::ERROR);
        continue;
        // Ditch cards that can't be parsed by Sabre.
    }
    try {
        OCA\Contacts\VCard::add($id, $vcard);
        $imported += 1;
    } catch (Exception $e) {
        OCP\Util::writeLog('contacts', 'Error importing vcard: ' . $e->getMessage() . $nl . $vcard, OCP\Util::ERROR);
        $failed += 1;
    }
}
//done the import
writeProgress('100');
sleep(3);
OC_Cache::remove($progresskey);
if (isset($_POST['fstype']) && $_POST['fstype'] == 'OC_FilesystemView') {
    if (!$view->unlink('/imports/' . $_POST['file'])) {
        OCP\Util::writeLog('contacts', 'Import: Error unlinking OC_FilesystemView ' . '/' . $_POST['file'], OCP\Util::ERROR);
    }
}
OCP\JSON::success(array('data' => array('imported' => $imported, 'failed' => $failed, 'file' => $_POST['file'])));
示例#4
0
 public function import()
 {
     if (!$this->isValid()) {
         return false;
     }
     $numofcomponents = count($this->calobject->getComponents());
     if ($this->overwrite) {
         foreach (OC_Calendar_Object::all($this->id) as $obj) {
             OC_Calendar_Object::delete($obj['id']);
         }
     }
     foreach ($this->calobject->getComponents() as $object) {
         if (!$object instanceof Sabre\VObject\Component\VEvent && !$object instanceof Sabre\VObject\Component\VJournal && !$object instanceof Sabre\VObject\Component\VTodo) {
             continue;
         }
         if (!is_null($object->DTSTART)) {
             $dtend = OC_Calendar_Object::getDTEndFromVEvent($object);
             if ($object->DTEND) {
                 $object->DTEND->setDateTime($dtend->getDateTime(), $object->DTSTART->getDateType());
             }
         }
         $vcalendar = $this->createVCalendar($object->serialize());
         $insertid = OC_Calendar_Object::add($this->id, $vcalendar);
         $this->abscount++;
         if ($this->isDuplicate($insertid)) {
             OC_Calendar_Object::delete($insertid);
         } else {
             $this->count++;
         }
         $this->updateProgress(intval($this->abscount / $numofcomponents * 100));
     }
     OC_Cache::remove($this->progresskey);
     return true;
 }
示例#5
0
 /**
  * delete info from the cache
  * @param string path
  * @param string root (optional)
  */
 public static function delete($path, $root = false)
 {
     if ($root === false) {
         $root = OC_Filesystem::getRoot();
     }
     $query = OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE `path_hash`=?');
     $query->execute(array(md5($root . $path)));
     //delete everything inside the folder
     $query = OC_DB::prepare('DELETE FROM `*PREFIX*fscache` WHERE `path` LIKE ?');
     $query->execute(array($root . $path . '/%'));
     OC_Cache::remove('fileid/' . $root . $path);
 }
示例#6
0
                    $property->parameters[] = new Sabre\VObject\Parameter('TYPE', $image->mimeType());
                    $vcard->__set('PHOTO', $property);
                } else {
                    OCP\Util::writeLog('contacts', 'savecrop.php: files: Adding PHOTO property.', OCP\Util::DEBUG);
                    // For vCard 3.0 the type must be e.g. JPEG or PNG
                    // For version 4.0 the full mimetype should be used.
                    // https://tools.ietf.org/html/rfc2426#section-3.1.4
                    $type = $vcard->VERSION == '4.0' ? $image->mimeType() : strtoupper(array_pop(explode('/', $image->mimeType())));
                    $vcard->add('PHOTO', $image->__toString(), array('ENCODING' => 'b', 'TYPE' => $type));
                }
                $now = new DateTime();
                $vcard->{'REV'} = $now->format(DateTime::W3C);
                if (!OCA\Contacts\VCard::edit($id, $vcard)) {
                    bailOut(OCA\Contacts\App::$l10n->t('Error saving contact.'));
                }
                OCA\Contacts\App::cacheThumbnail($id, $image);
                OCP\JSON::success(array('data' => array('id' => $id, 'width' => $image->width(), 'height' => $image->height(), 'lastmodified' => OCA\Contacts\App::lastModified($vcard)->format('U'))));
            } else {
                bailOut(OCA\Contacts\App::$l10n->t('Error resizing image'));
            }
        } else {
            bailOut(OCA\Contacts\App::$l10n->t('Error cropping image'));
        }
    } else {
        bailOut(OCA\Contacts\App::$l10n->t('Error creating temporary image'));
    }
} else {
    bailOut(OCA\Contacts\App::$l10n->t('Error finding image: ') . $tmpkey);
}
OC_Cache::remove($tmpkey);
示例#7
0
 public static function postCroppedAvatar($args)
 {
     \OC_JSON::checkLoggedIn();
     \OC_JSON::callCheck();
     $user = \OC_User::getUser();
     if (isset($_POST['crop'])) {
         $crop = $_POST['crop'];
     } else {
         $l = new \OC_L10n('core');
         \OC_JSON::error(array("data" => array("message" => $l->t("No crop data provided"))));
         return;
     }
     $tmpavatar = \OC_Cache::get('tmpavatar');
     if (is_null($tmpavatar)) {
         $l = new \OC_L10n('core');
         \OC_JSON::error(array("data" => array("message" => $l->t("No temporary profile picture available, try again"))));
         return;
     }
     $image = new \OC_Image($tmpavatar);
     $image->crop($crop['x'], $crop['y'], $crop['w'], $crop['h']);
     try {
         $avatar = new \OC_Avatar($user);
         $avatar->set($image->data());
         // Clean up
         \OC_Cache::remove('tmpavatar');
         \OC_JSON::success();
     } catch (\Exception $e) {
         \OC_JSON::error(array("data" => array("message" => $e->getMessage())));
     }
 }
示例#8
0
 public static function cacheThumbnail($id, \OC_Image $image = null, $remove = false, $update = false)
 {
     $key = self::THUMBNAIL_PREFIX . $id;
     if (\OC_Cache::hasKey($key) && $image === null && $remove === false && $update === false) {
         return \OC_Cache::get($key);
     }
     if ($remove) {
         \OC_Cache::remove($key);
         if (!$update) {
             return false;
         }
     }
     if (is_null($image)) {
         $vcard = self::getContactVCard($id);
         // invalid vcard
         if (is_null($vcard)) {
             \OCP\Util::writeLog('contacts', __METHOD__ . ' The VCard for ID ' . $id . ' is not RFC compatible', \OCP\Util::ERROR);
             return false;
         }
         $image = new \OC_Image();
         if (!isset($vcard->PHOTO)) {
             return false;
         }
         if (!$image->loadFromBase64((string) $vcard->PHOTO)) {
             return false;
         }
     }
     if (!$image->centerCrop()) {
         \OCP\Util::writeLog('contacts', 'thumbnail.php. Couldn\'t crop thumbnail for ID ' . $id, \OCP\Util::ERROR);
         return false;
     }
     if (!$image->resize(self::THUMBNAIL_SIZE)) {
         \OCP\Util::writeLog('contacts', 'thumbnail.php. Couldn\'t resize thumbnail for ID ' . $id, \OCP\Util::ERROR);
         return false;
     }
     // Cache for around a month
     \OC_Cache::set($key, $image->data(), 3000000);
     \OCP\Util::writeLog('contacts', 'Caching ' . $id, \OCP\Util::DEBUG);
     return \OC_Cache::get($key);
 }
示例#9
0
$l10n = OCA\Contacts\App::$l10n;
$multi_properties = array('EMAIL', 'TEL', 'IMPP', 'ADR', 'URL');
if (!$id) {
    bailOut(OCA\Contacts\App::$l10n->t('id is not set.'));
}
if (!$name) {
    bailOut(OCA\Contacts\App::$l10n->t('element name is not set.'));
}
if (!$checksum && in_array($name, $multi_properties)) {
    bailOut(OCA\Contacts\App::$l10n->t('checksum is not set.'));
}
$vcard = OCA\Contacts\App::getContactVCard($id);
if (!is_null($checksum)) {
    $line = OCA\Contacts\App::getPropertyLineByChecksum($vcard, $checksum);
    if (is_null($line)) {
        bailOut($l10n->t('Information about vCard is incorrect. Please reload the page.'));
        exit;
    }
    unset($vcard->children[$line]);
} else {
    unset($vcard->{$name});
    if ($name === 'PHOTO') {
        \OC_Cache::remove(OCA\Contacts\App::THUMBNAIL_PREFIX . $id);
    }
}
try {
    OCA\Contacts\VCard::edit($id, $vcard);
} catch (Exception $e) {
    bailOut($e->getMessage());
}
OCP\JSON::success(array('data' => array('id' => $id, 'lastmodified' => OCA\Contacts\App::lastModified($vcard)->format('U'))));