Esempio n. 1
0
 /**
  * Deletes all contents of the specified folder.
  * This is generally used to empty the trash (wastebasked), but could also be used on any
  * other folder.
  *
  * @param string        $folderid
  * @param boolean       $includeSubfolders      (opt) also delete sub folders, default true
  *
  * @access public
  * @return boolean
  * @throws StatusException
  */
 public function EmptyFolder($folderid, $includeSubfolders = true)
 {
     $folderentryid = mapi_msgstore_entryidfromsourcekey($this->store, hex2bin($folderid));
     if (!$folderentryid) {
         throw new StatusException(sprintf("BackendZarafa->EmptyFolder('%s','%s'): Error, unable to open folder (no entry id)", $folderid, Utils::PrintAsString($includeSubfolders)), SYNC_ITEMOPERATIONSSTATUS_SERVERERROR);
     }
     $folder = mapi_msgstore_openentry($this->store, $folderentryid);
     if (!$folder) {
         throw new StatusException(sprintf("BackendZarafa->EmptyFolder('%s','%s'): Error, unable to open parent folder (open entry)", $folderid, Utils::PrintAsString($includeSubfolders)), SYNC_ITEMOPERATIONSSTATUS_SERVERERROR);
     }
     $flags = 0;
     if ($includeSubfolders) {
         $flags = DEL_ASSOCIATED;
     }
     ZLog::Write(LOGLEVEL_DEBUG, sprintf("BackendZarafa->EmptyFolder('%s','%s'): emptying folder", $folderid, Utils::PrintAsString($includeSubfolders)));
     // empty folder!
     mapi_folder_emptyfolder($folder, $flags);
     if (mapi_last_hresult()) {
         throw new StatusException(sprintf("BackendZarafa->EmptyFolder('%s','%s'): Error, mapi_folder_emptyfolder() failed: 0x%X", $folderid, Utils::PrintAsString($includeSubfolders), mapi_last_hresult()), SYNC_ITEMOPERATIONSSTATUS_SERVERERROR);
     }
     return true;
 }
if (!isset($storeEntryid)) {
    trigger_error("Default store not found", E_USER_ERROR);
}
$store = mapi_openmsgstore($session, $storeEntryid);
$root = mapi_msgstore_openentry($store, null);
$rootProps = mapi_getprops($root, array(PR_IPM_CONTACT_ENTRYID));
$folder = mapi_msgstore_openentry($store, $rootProps[PR_IPM_CONTACT_ENTRYID]);
isUnicodeStore($store);
// open the csv file and start reading
$fh = fopen($csv_file, "r");
if (!$fh) {
    trigger_error("Can't open CSV file \"" . $csv_file . "\"", E_USER_ERROR);
}
// empty folder if needed
if (EMPTY_FOLDER) {
    mapi_folder_emptyfolder($folder, DEL_ASSOCIATED);
}
$properties = array();
loadProperties($properties);
$properties = getPropIdsFromStrings($store, $properties);
//composed properties which require more work
$special_properties = array("email_address_1", "email_address_2", "email_address_3");
$i = 1;
while (!feof($fh)) {
    $line = fgetcsv($fh, CSV_MAX_LENGTH, CSV_DELIMITER, CSV_ENCLOSURE);
    if (!$line) {
        continue;
    }
    if ($i == 1 && defined('FIELD_NAMES') && FIELD_NAMES) {
        $i++;
        continue;
 /**
  * Deletes an entire addressbook and all its contents
  *
  * @param mixed $addressBookId 
  * @return void
  */
 public function deleteAddressBook($addressBookId)
 {
     $this->logger->info("deleteAddressBook(" . bin2hex($addressBookId) . ")");
     if (READ_ONLY || !ALLOW_DELETE_FOLDER) {
         $this->logger->warn("Cannot delete address book: permission denied by config");
         return false;
     }
     $folders = $this->bridge->getAdressBooks();
     $parentFolderId = $folders[$addressBookId]['parentId'];
     $folder = mapi_msgstore_openentry($this->bridge->getStore($addressBookId), $addressBookId);
     $parentFolder = mapi_msgstore_openentry($this->bridge->getStore($addressBookId), $parentFolderId);
     // Delete folder content
     mapi_folder_emptyfolder($folder, DEL_ASSOCIATED);
     mapi_folder_deletefolder($parentFolder, $addressBookId);
     if (mapi_last_hresult() > 0) {
         $this->logger->fatal("Error deleting addressbook: " . get_mapi_error_name());
     }
 }
function import($store, $csv_file, $delete_old_items, $categories)
{
    $folder = getContactsFolder($store);
    // open the csv file and start reading
    $fh = fopen($csv_file, "r");
    if (!$fh) {
        trigger_error("Can't read CSV file \"" . $csv_file . "\".", E_USER_ERROR);
    }
    // Delete all existing items if requested.
    if ($delete_old_items) {
        mapi_folder_emptyfolder($folder, DEL_ASSOCIATED);
        printf("Old items deleted.\n");
    }
    $properties = getProperties();
    $properties = replaceStringPropertyTags($store, $properties);
    //composed properties which require more work
    $special_properties = getSpecialPropertyNames();
    $csv_mapping = getMapping();
    $i = 1;
    while (!feof($fh)) {
        $line = fgetcsv($fh, CSV_MAX_LENGTH, CSV_DELIMITER, CSV_ENCLOSURE);
        // print_r($line);
        if (!$line) {
            continue;
        }
        if ($i == 1 && defined('FIELD_NAMES') && FIELD_NAMES) {
            $i++;
            continue;
        }
        $propValues = array();
        //set "simple" properties
        foreach ($csv_mapping as $property => $cnt) {
            if (!in_array($property, $special_properties)) {
                setProperty($property, $line[$csv_mapping[$property]], $propValues, $properties);
            }
        }
        // set display name
        if (isset($csv_mapping["display_name"]) && isset($line[$csv_mapping["display_name"]])) {
            $name = to_windows1252($line[$csv_mapping["display_name"]]);
            $propValues[$properties["display_name"]] = $propValues[$properties["subject"]] = $propValues[$properties["fileas"]] = $name;
            $propValues[$properties["fileas_selection"]] = -1;
        } else {
            $propValues[$properties["display_name"]] = $propValues[$properties["subject"]] = $propValues[$properties["fileas"]] = "";
            if (isset($propValues[$properties["given_name"]])) {
                $propValues[$properties["display_name"]] .= $propValues[$properties["given_name"]];
                $propValues[$properties["subject"]] .= $propValues[$properties["given_name"]];
            }
            if (isset($propValues[$properties["surname"]])) {
                if (strlen($propValues[$properties["display_name"]]) > 0) {
                    $propValues[$properties["display_name"]] .= " " . $propValues[$properties["surname"]];
                    $propValues[$properties["subject"]] .= " " . $propValues[$properties["surname"]];
                } else {
                    $propValues[$properties["display_name"]] .= $propValues[$properties["surname"]];
                    $propValues[$properties["subject"]] .= $propValues[$properties["surname"]];
                }
            }
            if (isset($propValues[$properties["surname"]])) {
                $propValues[$properties["fileas"]] .= $propValues[$properties["surname"]];
            }
            if (isset($propValues[$properties["given_name"]])) {
                if (strlen($propValues[$properties["fileas"]]) > 0) {
                    $propValues[$properties["fileas"]] .= ", " . $propValues[$properties["given_name"]];
                } else {
                    $propValues[$properties["fileas"]] .= $propValues[$properties["given_name"]];
                }
            }
        }
        $nremails = array();
        $abprovidertype = 0;
        if (isset($csv_mapping["email_address_1"]) && isset($line[$csv_mapping["email_address_1"]])) {
            setEmailAddress($line[$csv_mapping["email_address_1"]], $propValues[$properties["display_name"]], 1, $propValues, $properties, $nremails, $abprovidertype);
        }
        if (isset($csv_mapping["email_address_2"]) && isset($line[$csv_mapping["email_address_2"]])) {
            setEmailAddress($line[$csv_mapping["email_address_2"]], $propValues[$properties["display_name"]], 2, $propValues, $properties, $nremails, $abprovidertype);
        }
        if (isset($csv_mapping["email_address_3"]) && isset($line[$csv_mapping["email_address_3"]])) {
            setEmailAddress($line[$csv_mapping["email_address_3"]], $propValues[$properties["display_name"]], 3, $propValues, $properties, $nremails, $abprovidertype);
        }
        if (!empty($nremails)) {
            $propValues[$properties["address_book_mv"]] = $nremails;
        }
        $propValues[$properties["address_book_long"]] = $abprovidertype;
        //set addresses
        if (isset($csv_mapping["home_address_street2"])) {
            mergeStreet("home", $line[$csv_mapping["home_address_street2"]], $propValues, $properties);
        }
        if (isset($csv_mapping["home_address_street3"])) {
            mergeStreet("home", $line[$csv_mapping["home_address_street3"]], $propValues, $properties);
        }
        if (!isset($propValues[$properties["home_address"]]) && isset($propValues[$properties["home_address_street"]]) && isset($propValues[$properties["home_address_postal_code"]]) && isset($propValues[$properties["home_address_city"]]) && isset($propValues[$properties["home_address_state"]]) && isset($propValues[$properties["home_address_country"]])) {
            buildAddressString("home", $propValues[$properties["home_address_street"]], $propValues[$properties["home_address_postal_code"]], $propValues[$properties["home_address_city"]], $propValues[$properties["home_address_state"]], $propValues[$properties["home_address_country"]], $propValues, $properties);
        }
        if (isset($csv_mapping["business_address_street2"])) {
            mergeStreet("business", $line[$csv_mapping["business_address_street2"]], $propValues, $properties);
        }
        if (isset($csv_mapping["business_address_street3"])) {
            mergeStreet("business", $line[$csv_mapping["business_address_street3"]], $propValues, $properties);
        }
        if (!isset($propValues[$properties["business_address"]]) && isset($propValues[$properties["business_address_street"]]) && isset($propValues[$properties["business_address_postal_code"]]) && isset($propValues[$properties["business_address_city"]]) && isset($propValues[$properties["business_address_state"]]) && isset($propValues[$properties["business_address_country"]])) {
            buildAddressString("business", $propValues[$properties["business_address_street"]], $propValues[$properties["business_address_postal_code"]], $propValues[$properties["business_address_city"]], $propValues[$properties["business_address_state"]], $propValues[$properties["business_address_country"]], $propValues, $properties);
        }
        if (isset($csv_mapping["other_address_street2"])) {
            mergeStreet("other", $line[$csv_mapping["other_address_street2"]], $propValues, $properties);
        }
        if (isset($csv_mapping["other_address_street3"])) {
            mergeStreet("other", $line[$csv_mapping["other_address_street3"]], $propValues, $properties);
        }
        if (!isset($propValues[$properties["other_address"]]) && isset($propValues[$properties["other_address_street"]]) && isset($propValues[$properties["other_address_postal_code"]]) && isset($propValues[$properties["other_address_city"]]) && isset($propValues[$properties["other_address_state"]]) && isset($propValues[$properties["other_address_country"]])) {
            buildAddressString("other", $propValues[$properties["other_address_street"]], $propValues[$properties["other_address_postal_code"]], $propValues[$properties["other_address_city"]], $propValues[$properties["other_address_state"]], $propValues[$properties["other_address_country"]], $propValues, $properties);
        }
        if (isset($propValues[$properties["home_address"]])) {
            $propValues[$properties["mailing_address"]] = 1;
            setMailingAdress($propValues[$properties["home_address_street"]], $propValues[$properties["home_address_postal_code"]], $propValues[$properties["home_address_city"]], $propValues[$properties["home_address_state"]], $propValues[$properties["home_address_country"]], $propValues[$properties["home_address"]], $propValues, $properties);
        } elseif (isset($propValues[$properties["business_address"]])) {
            $propValues[$properties["mailing_address"]] = 2;
            setMailingAdress($propValues[$properties["business_address_street"]], $propValues[$properties["business_address_postal_code"]], $propValues[$properties["business_address_city"]], $propValues[$properties["business_address_state"]], $propValues[$properties["business_address_country"]], $propValues[$properties["business_address"]], $propValues, $properties);
        } elseif (isset($propValues[$properties["other_address"]])) {
            $propValues[$properties["mailing_address"]] = 3;
            setMailingAdress($propValues[$properties["other_address_street"]], $propValues[$properties["other_address_postal_code"]], $propValues[$properties["other_address_city"]], $propValues[$properties["other_address_state"]], $propValues[$properties["other_address_country"]], $propValues[$properties["other_address"]], $propValues, $properties);
        }
        if (isset($categories) && !empty($categories)) {
            setProperty("categories", $categories, $propValues, $properties);
        }
        // if the display name is set, then it is a valid contact: save it to the folder
        if (isset($propValues[$properties["display_name"]])) {
            $propValues[$properties["message_class"]] = "IPM.Contact";
            $propValues[$properties["icon_index"]] = "512";
            $message = mapi_folder_createmessage($folder);
            mapi_setprops($message, $propValues);
            mapi_savechanges($message);
            printf("New contact added: \"%s\".\n", $propValues[$properties["display_name"]]);
        }
        $i++;
    }
}
Esempio n. 5
0
 /**
  * Removes all messages that have not the same chunkType (chunk configuration changed!).
  *
  * @param string $folderid
  * @param string $gabId         the id of the gab where the hidden folder should be searched. If not set (null) the default gab is used.
  * @param string $gabName       the name of the gab where the hidden folder should be searched. If not set (null) the default gab is used.
  *
  * @access protected
  * @return boolean
  */
 protected function clearFolderContents($folderid, $gabId = null, $gabName = 'default')
 {
     $this->Log(sprintf("Kopano->clearFolderContents: emptying folder in GAB '%s': %s", $gabName, $folderid));
     $store = $this->getStore($gabId, $gabName);
     if (!$store) {
         return false;
     }
     $folder = $this->getFolder($store, $folderid);
     // empty folder!
     $flags = 0;
     mapi_folder_emptyfolder($folder, $flags);
     if (mapi_last_hresult()) {
         $this->Terminate(sprintf("Kopano->clearFolderContents: Error, mapi_folder_emptyfolder() failed on '%s': 0x%08X", $gabName, mapi_last_hresult()));
     }
     return true;
 }