Skip to content

Commit

Permalink
Added Core Files and Core Installer Files
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenoneal committed Feb 9, 2013
1 parent f0e022b commit a72bbe6
Show file tree
Hide file tree
Showing 8 changed files with 502 additions and 1 deletion.
54 changes: 54 additions & 0 deletions GravatarSync.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GravatarSync", "GravatarSync\GravatarSync.csproj", "{90AA82F5-68A9-431B-9680-EC1991D5E437}"
EndProject
Project("{930C7802-8A8C-48F9-8165-68863BCCD9DD}") = "Installer", "Installer\Installer.wixproj", "{8B80098A-57CC-4118-8702-B9B18D2D3FA4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|ARM = Debug|ARM
Debug|Mixed Platforms = Debug|Mixed Platforms
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|ARM = Release|ARM
Release|Mixed Platforms = Release|Mixed Platforms
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{90AA82F5-68A9-431B-9680-EC1991D5E437}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{90AA82F5-68A9-431B-9680-EC1991D5E437}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90AA82F5-68A9-431B-9680-EC1991D5E437}.Debug|ARM.ActiveCfg = Debug|Any CPU
{90AA82F5-68A9-431B-9680-EC1991D5E437}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{90AA82F5-68A9-431B-9680-EC1991D5E437}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{90AA82F5-68A9-431B-9680-EC1991D5E437}.Debug|x64.ActiveCfg = Debug|Any CPU
{90AA82F5-68A9-431B-9680-EC1991D5E437}.Debug|x86.ActiveCfg = Debug|Any CPU
{90AA82F5-68A9-431B-9680-EC1991D5E437}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90AA82F5-68A9-431B-9680-EC1991D5E437}.Release|Any CPU.Build.0 = Release|Any CPU
{90AA82F5-68A9-431B-9680-EC1991D5E437}.Release|ARM.ActiveCfg = Release|Any CPU
{90AA82F5-68A9-431B-9680-EC1991D5E437}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{90AA82F5-68A9-431B-9680-EC1991D5E437}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{90AA82F5-68A9-431B-9680-EC1991D5E437}.Release|x64.ActiveCfg = Release|Any CPU
{90AA82F5-68A9-431B-9680-EC1991D5E437}.Release|x86.ActiveCfg = Release|Any CPU
{8B80098A-57CC-4118-8702-B9B18D2D3FA4}.Debug|Any CPU.ActiveCfg = Debug|x86
{8B80098A-57CC-4118-8702-B9B18D2D3FA4}.Debug|ARM.ActiveCfg = Debug|x86
{8B80098A-57CC-4118-8702-B9B18D2D3FA4}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{8B80098A-57CC-4118-8702-B9B18D2D3FA4}.Debug|Mixed Platforms.Build.0 = Debug|x86
{8B80098A-57CC-4118-8702-B9B18D2D3FA4}.Debug|x64.ActiveCfg = Debug|x86
{8B80098A-57CC-4118-8702-B9B18D2D3FA4}.Debug|x86.ActiveCfg = Debug|x86
{8B80098A-57CC-4118-8702-B9B18D2D3FA4}.Debug|x86.Build.0 = Debug|x86
{8B80098A-57CC-4118-8702-B9B18D2D3FA4}.Release|Any CPU.ActiveCfg = Release|x86
{8B80098A-57CC-4118-8702-B9B18D2D3FA4}.Release|ARM.ActiveCfg = Release|x86
{8B80098A-57CC-4118-8702-B9B18D2D3FA4}.Release|Mixed Platforms.ActiveCfg = Release|x86
{8B80098A-57CC-4118-8702-B9B18D2D3FA4}.Release|Mixed Platforms.Build.0 = Release|x86
{8B80098A-57CC-4118-8702-B9B18D2D3FA4}.Release|x64.ActiveCfg = Release|x86
{8B80098A-57CC-4118-8702-B9B18D2D3FA4}.Release|x86.ActiveCfg = Release|x86
{8B80098A-57CC-4118-8702-B9B18D2D3FA4}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions GravatarSync/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<configuration>

<appSettings>
<!-- Default Gravatar uses the default Gravatar settings here http://en.gravatar.com/site/implement/images/ . 404 is not supported yet. options include mm,identicon,monsterid,wavatar,retro,blank -->
<add key="DefaultGravatar" value="mm"/>
<!-- OUFilter is the filter used to search for accounts to update with Photos if you want to constrain to a single OU. Leave blank to apply to all OUs"-->
<add key="OUFilter" value=""/>
</appSettings>
</configuration>
71 changes: 71 additions & 0 deletions GravatarSync/GravatarSync.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{90AA82F5-68A9-431B-9680-EC1991D5E437}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>GravatarSync</RootNamespace>
<AssemblyName>GravatarSync</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Configuration.Install" />
<Reference Include="System.Core" />
<Reference Include="System.DirectoryServices" />
<Reference Include="System.Drawing" />
<Reference Include="System.Management" />
<Reference Include="System.Net" />
<Reference Include="System.Security" />
<Reference Include="System.ServiceProcess" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
212 changes: 212 additions & 0 deletions GravatarSync/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Net;
using System.IO;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Threading;
using System.ServiceProcess;




/* Other stuff TODO:
*
*
*
*
*
*
*
* */

namespace GravatarSync
{
public partial class Program : ServiceBase
{
static void Main(string[] args)
{
if (Environment.UserInteractive)
{
UpdateGravatar(true);
}

else
{
ServiceBase.Run(new Program());
}

}

public Program()
{
this.ServiceName = "GravatarSync";
System.Timers.Timer gravatarSyncTimer = new System.Timers.Timer();
gravatarSyncTimer.Enabled = true;
gravatarSyncTimer.Interval = 86400000;
gravatarSyncTimer.Elapsed += new System.Timers.ElapsedEventHandler(gravatarSyncTimer_Elapsed);
}

void gravatarSyncTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try
{ UpdateGravatar(); }
catch (Exception ex)
{
}

}


/// <summary>
/// Call Gravatar Update Loop without the Console
/// </summary>
public static void UpdateGravatar()
{
UpdateGravatar(false);
}

/// <summary>
/// Call Gravatar Update Loop and pass a boolean on whether to enable/disable console output.
/// </summary>
/// <param name="console">true = show console</param>
public static void UpdateGravatar(bool console)
{

if (console) Console.WriteLine("Starting");

IPGlobalProperties netproperties = IPGlobalProperties.GetIPGlobalProperties();



DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain,netproperties.DomainName);
Domain d = Domain.GetDomain(context);
DirectoryEntry de = d.GetDirectoryEntry();

if (console) Console.WriteLine("domain is " + netproperties.DomainName);

if (console) Console.WriteLine("LDAP is " + de.Properties["DistinguishedName"].Value.ToString());

if (console) Console.ReadKey();

DirectorySearcher ds = new DirectorySearcher("LDAP://" + de.Properties["DistinguishedName"].Value.ToString());

ds.Filter = "(&(objectClass=user)(objectCategory=person))";


//Attempt to append the LDAP search string specified in the app.config
if (System.Configuration.ConfigurationManager.AppSettings["OUFilter"] != null)
{
if (System.Configuration.ConfigurationManager.AppSettings["OUFilter"].Length > 0)
{
ds.Filter = "(&" + ds.Filter + System.Configuration.ConfigurationManager.AppSettings["OUFilter"] + ")";
}
}

if (console) Console.WriteLine("Filter is" + ds.Filter);



ds.PropertiesToLoad.Add("sAMAccountName");
ds.PropertiesToLoad.Add("mail");
ds.PageSize = 0;



SearchResultCollection results = ds.FindAll();


foreach (SearchResult result in results)
{
foreach (string PropertyName in result.Properties.PropertyNames)
{

foreach (Object key in result.GetDirectoryEntry().Properties[PropertyName])
{

try
{
switch (PropertyName.ToUpper())
{


case "SAMACCOUNTNAME":
if (console) Console.WriteLine(result.Path);
if (console) Console.WriteLine(key.ToString());
break;

case "MAIL":
if (console) Console.WriteLine(key.ToString());
using (MD5 md5Hash = MD5.Create())
{

String gravatarURI = "http://www.gravatar.com/avatar/" + GetMd5Hash(md5Hash,key.ToString()) + "?size=96&rating=G&d=" + System.Configuration.ConfigurationManager.AppSettings["DefaultGravatar"];

WebRequest requestPNG = WebRequest.Create(gravatarURI);
WebResponse responsePNG = requestPNG.GetResponse();
Image webImage = Image.FromStream(responsePNG.GetResponseStream());
MemoryStream ms = new MemoryStream();
webImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
System.DirectoryServices.DirectoryEntry myUser = new System.DirectoryServices.DirectoryEntry(result.Path);
myUser.Properties["thumbnailPhoto"].Clear();
myUser.Properties["thumbnailPhoto"].Add(ms.ToArray());
gravatarURI = "http://www.gravatar.com/avatar/" + GetMd5Hash(md5Hash, key.ToString()) + "?size=256&rating=G&d=" + System.Configuration.ConfigurationManager.AppSettings["DefaultGravatar"];
requestPNG = WebRequest.Create(gravatarURI);
responsePNG = requestPNG.GetResponse();
webImage = Image.FromStream(responsePNG.GetResponseStream());
ms = new MemoryStream();
webImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
myUser.Properties["jpegPhoto"].Clear();
myUser.Properties["jpegPhoto"].Add(ms.ToArray());
myUser.CommitChanges();
myUser.Close();
myUser.Dispose();
if (console) Console.WriteLine(GetMd5Hash(md5Hash, key.ToString()));
}

break;
}

}

catch { }


}
}
}
de.Close();
de.Dispose();
results.Dispose();
if (console) Console.WriteLine("Done Updating");
//if (console) Console.ReadKey();
}

static string GetMd5Hash(MD5 md5Hash, string input)
{

// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();

// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}

// Return the hexadecimal string.
return sBuilder.ToString();
}
}
}
36 changes: 36 additions & 0 deletions GravatarSync/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("GravatarSync")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("GravatarSync")]
[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("5ce9ae48-0167-435f-bdcc-e13de60f46f7")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading

0 comments on commit a72bbe6

Please sign in to comment.