Example #1
0
        // Return true if an open XML document is enforcing read-only
        private static bool IsOpenXMLReadOnlyEnforced(string filename)
        {
            // Read an OpenXML type document
            using (Package package = Package.Open(path: filename, packageMode: FileMode.Open, packageAccess: FileAccess.Read))
            {
                if (null == package)
                {
                    return(false);
                }
                try
                {
                    // Document security is set in the extended properties
                    // https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2010/cc845474(v%3doffice.14)
                    string extendedType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties";
                    PackageRelationshipCollection extendedProps = package.GetRelationshipsByType(extendedType);
                    if (null != extendedProps)
                    {
                        IEnumerator extendedPropsList = extendedProps.GetEnumerator();
                        if (extendedPropsList.MoveNext())
                        {
                            Uri         extendedPropsUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), ((PackageRelationship)extendedPropsList.Current).TargetUri);
                            PackagePart props            = package.GetPart(extendedPropsUri);
                            if (null != props)
                            {
                                // Read the internal docProps/app.xml XML file
                                XDocument xmlDoc     = XDocument.Load(props.GetStream());
                                XElement  securityEl = xmlDoc.Root.Element(XName.Get("DocSecurity", xmlDoc.Root.GetDefaultNamespace().NamespaceName));
                                if (null != securityEl)
                                {
                                    if (!String.IsNullOrWhiteSpace(securityEl.Value))
                                    {
                                        // See https://docs.microsoft.com/en-us/previous-versions/office/developer/office-2010/cc840043%28v%3doffice.14%29
                                        return((Int16.Parse(securityEl.Value) & 4) == 4);
                                    }
                                }

                                package.Close();
                                // PowerPoint doesn't use DocSecurity (*sigh*) so need another check
                                XElement appEl = xmlDoc.Root.Element(XName.Get("Application", xmlDoc.Root.GetDefaultNamespace().NamespaceName));
                                if (null != appEl)
                                {
                                    if (!String.IsNullOrWhiteSpace(appEl.Value) &&
                                        appEl.Value.IndexOf("PowerPoint", StringComparison.InvariantCultureIgnoreCase) >= 0)
                                    {
                                        PresentationDocument presentationDocument = PresentationDocument.Open(path: filename, isEditable: false);
                                        if (null != presentationDocument &&
                                            presentationDocument.PresentationPart.Presentation.ModificationVerifier != null)
                                        {
                                            return(true);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception) { }
                return(false);
            }
        }
Example #2
0
        /**
         *  Fetches the InputStream to read the contents, based
         *  of the specified core part, for which we are defined
         *  as a suitable relationship
         */
        public Stream GetContents(PackagePart corePart)
        {
            PackageRelationshipCollection prc =
                corePart.GetRelationshipsByType(_relation);
            IEnumerator <PackageRelationship> it = prc.GetEnumerator();

            if (it.MoveNext())
            {
                PackageRelationship rel     = it.Current;
                PackagePartName     relName = PackagingUriHelper.CreatePartName(rel.TargetUri);
                PackagePart         part    = corePart.Package.GetPart(relName);
                return(part.GetInputStream());
            }
            log.Log(POILogger.WARN, "No part " + _defaultName + " found");
            return(null);
        }
Example #3
0
        // Token: 0x06003F32 RID: 16178 RVA: 0x00120F84 File Offset: 0x0011F184
        private PackagePart GetWpfEntryPart()
        {
            PackagePart result = null;
            PackageRelationshipCollection relationshipsByType = this._package.GetRelationshipsByType("http://schemas.microsoft.com/wpf/2005/10/xaml/entry");
            PackageRelationship           packageRelationship = null;

            using (IEnumerator <PackageRelationship> enumerator = relationshipsByType.GetEnumerator())
            {
                if (enumerator.MoveNext())
                {
                    PackageRelationship packageRelationship2 = enumerator.Current;
                    packageRelationship = packageRelationship2;
                }
            }
            if (packageRelationship != null)
            {
                Uri targetUri = packageRelationship.TargetUri;
                result = this._package.GetPart(targetUri);
            }
            return(result);
        }