Community Server and Google AdSense


Not many programs have managed to amaze me with their capabilities. Certainly Community Server is one. It is really a nice feeling to know, you can implement what you need on the platform you use.

It all started when a site member asked me to provide the ability to host Google AdSense in his blog. The first thing came to my mind was to create new weblog skins in order to implement AdSense banner zones, but as more webloggers were sharing the same skins, it would only confuse them. Part of the service would be to allow each member to customize his ads zone; so except weblog skin creation, I would have to implement new pages for the control panel too. Not quite easy…

Then I came up with the idea to implement a CSModule that would allow the display of an AdSense banner in weblogs posts. This idea did not seem wise at the beginning; the users would be able to use the CSModule all over the CS site and flood all posts with ads. Well, I could easy control the use of CSModule by requiring each member to belong to a certain security role I would assign.

I haven’t been a user of AdSense program, so I asked information about the program in order to implement the AdSense CSModule. I was under the impression, all I would have to implement was a BBCode tag that would include the user’s AdSense program code, in order to display ads and credit his account. Learning about the program, I found out, all the customization of the AdSense banner was done through JavaScript code. I was not happy about it, because it would be a potential exploit, as the CSModule would render code a user would provide, inside <script> tags.

In order to handle the JavaScript code inside AdSense tags, I used Community Server’s build-in functions that strip all the tags from string variables. In order to be sure, all I have left is secure code, I also removed all the lf/cr sequences and split the string buffer using the ";" character; this trick produces an array of JavaScript code lines. It was easy to decide which lines are safe, as I could "approve" lines for rendering, that will include names of the Google AdSense program variables.

While I was testing the new developed CSModule, I discovered that, an authorized member could include AdSense tag in a forum’s post. It was not the way I wanted CSModule to be used. The CSModule mechanism provides a property that stores the type of CS application is executing CSModule each time, and I had to limit the use of CSModule to render the AdSense code only for the Weblog application.

Completing this task, I deployed the assembly to the production server, and provided site members with instructions how to use the AdSense CSModule in their blogs. It was no more than two hours later, I got a message from a member, that he could not use the CSModule, because he would have his AdSense account suspended! There is a policy in Google AdSense program; a user can not display more than 3 AdSense banners in a webpage. I am not sure, if I had read this policy or not, but probably I just ignored it, as it would not help me to implement the CSModule. For sure, the CSModule was working, it was doing was it was designed to do. On each weblog page that renders a list of posts, the CSModule was rendering an AdSense banner for each post; that was definitely a violation of AdSense policy.

I must admit, I thought I was completely busted that moment! I had doubts when I decided not to implement new skins for the weblogs and just go with the CSModule implementation. I thought my decision was just wrong. But again, one of my favorite features on Community Server, url re-writing, gave me a push to explore the problem a little more. Community Server is re-writing the url of each request; it has mechanisms that parse each request url, split the address part from query data, and transform them into a new url, using the data in siteurls.config. So, if I could find a way to get the name of the template the url re-writing mechanism was using to transform the request url, I could figure out in which page the CSModule was running and make a decision, to render or not, the AdSense banner.

Half of the problem was to find the names of the templates I wanted the AdSense banner to be rendered. These were the pages that a weblog post would appear by its self. The second half of the implementation was to figure out which page had requested CSModule to render. My intention was to check the code Community Server was using to parse the requests, find some functions and use them with the request url, I would get through from CSContext object.

I must admit, I spend about half an hour checking code, without having a solution for my task; I was not checking for the obvious: Most of the times all the information processed are stored inside the CSContext object that is created with each request. Why would not the CSContext object include the name of the transformation template? I suppose that something flashed over my head that moment!

You probably guessed the answer; I was correct. There is a property in CSContext object that stores the name of the template! As for the implementation, I had to check if the name of the transformation template for the current url existed in the list of the templates I had already found.

Using AdSense CSModule is easy, but there are several lines of code the tag requires each time in order to render the AdSense banner. By creating a TextPart that holds the AdSense tag code, and using the TextPart’s tag, adding an AdSense banner to each weblog post is fast!

I would post the AdSense CSModule assembly now, but I am packing for Barcelona, to attend TechEd Developers 2007. I have little time to catch the plane to Barcelona and no time to create a package for the AdSense CSModule assembly. If you would like to see the CSModule in action, check this blog (it is Greek, so you probably won’t be able to read it, but you follow the links for each post, and see the AdSense banner on the bottom of each post). I suppose by this time next week, I will have the AdSense CSModule available for everyone to download.

Advertisements

George Capnias is working as Software Architect and Development Manager. His developing career started as soon as he left high-school and got a job as a programmer for a small software house in Athens. He started developing with Microsoft’s products as soon as Visual Basic for Windows came out. Since September '93, he was member in one of the greater BBS in Athens, where he served as a moderator for Windows related forums till '98. He started developing Web applications in '96. September '99 he started working as a trainer for Compact SA (Microsoft Gold Certified Partner for Learning Solutions - CPLS). He is certified as Microsoft Certified Professional & Site Building (MCP+SB), Microsoft Certified Solution Developer (MCSD) for Visual Studio 6 and Visual Studio 2003, Microsoft Certified Database Administrator (MCDBA) for SQL Server 2000, Microsoft Certified Technology Specialist (MCTS) for Windows & Web Applications, Microsoft Certified Professional Developer (MCPD) for Windows & Web Applications and he has the status of Microsoft Certified Trainer (MCT). He is the leader of dotNETZone.gr, an Athens, Greece, user group for Greek software developers using Microsoft’s .NET platform. He is delivering technical presentations and workshops for Microsoft Hellas to audiences like developers, ISVs. He was awarded as MVP for 'Visual Studio and Development Technologies' in '16 and '17, for 'ASP.NET/IIS technologies' in '06 and '15 and as MVP for 'VSTO technologies' in '07, '08, '09, '10, '11, '12, '13 and '14.

Posted in Community Server

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: