<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sean Monahan &#187; PHP</title>
	<atom:link href="http://seanmonahan.org/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanmonahan.org</link>
	<description></description>
	<lastBuildDate>Wed, 07 Dec 2011 04:20:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Create HTML Forms with Object-Oriented PHP</title>
		<link>http://seanmonahan.org/2008/12/18/create-html-forms-with-object-oriented-php/</link>
		<comments>http://seanmonahan.org/2008/12/18/create-html-forms-with-object-oriented-php/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 23:23:04 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[HTML Forms]]></category>
		<category><![CDATA[Object-Oriented]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP5]]></category>

		<guid isPermaLink="false">http://seanmonahan.org/?p=122</guid>
		<description><![CDATA[One of the things I really dislike about PHP is it&#8217;s reliance on spaghetti code.  Every PHP developer has had to deal with &#60;?php ?&#62; tags strewn about HTML in a seeming mess.  The worst situation is updating someone else&#8217;s code.  Often it takes longer to decipher everything than it does to make the actual [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things I really dislike about PHP is it&#8217;s reliance on spaghetti code.  Every PHP developer has had to deal with &lt;?php ?&gt; tags strewn about HTML in a seeming mess.  The worst situation is updating someone else&#8217;s code.  Often it takes longer to decipher everything than it does to make the actual update.<br />
<span id="more-122"></span><br />
I&#8217;ve been working on a project that takes a lot of user input from HTML forms and I decided at the beginning that I was not going to deal with a bunch of spaghetti code to get things done.  Rather I wrote a serious of form classes in PHP5.  These classes allow me to write HTML forms without having to write a single line of HTML &#8212; everything is handled in PHP and it has definitely made life easier.</p>
<p>Now I&#8217;m going to share the love so no one else has to suffer through a mess of convoluted code.</p>
<h3>The Classes</h3>
<p>My form class libary has seven classes.  Each distinct form element type (e.g., &lt;input&gt;, &lt;select&gt;, etc.) is represented by its own class.  All these elements are then pulled together in the Form class that handles the actual construction and ouputting of the form to the screen.</p>
<p>Download the Form class library <a href="http://seanmonahan.org/wp-content/uploads/2008/12/formclasslibrary.zip">here</a>.</p>
<p>See the complete documentation for the Form class library <a href="http://seanmonahan.org/code/php/docs">here</a>.</p>
<p>Download the examples <a href="http://seanmonahan.org/wp-content/uploads/2008/12/formexamples.zip">here</a>.</p>
<h3>The Examples</h3>
<p>So, you&#8217;re thinking, &#8220;this is cool, but can I see it in action?&#8221; Just read on.</p>
<h4>A Simple Form</h4>
<p>The first example is a simple form with one text input and a submit button.</p>
<pre class="brush: php; title: ; notranslate">

// Create a new Form object

$form = new Form(&quot;ExampleForm&quot;);

// Add a text input

$form-&gt;addField(new FormInput(&quot;&quot;, &quot;text&quot;, &quot;firstname&quot;, &quot;What is your name?&quot;));

// Add a submit button

$form-&gt;addField(new FormInput(&quot;Submit&quot;, &quot;submit&quot;, &quot;submit&quot;));

// Print the form to the screen

$form-&gt;printForm();
</pre>
<p><a href="http://seanmonahan.org/code/php/examples/form/SimpleForm.php" target="_blank">See the example</a> in action.</p>
<h4>A Simple Form with a Select</h4>
<p>The second example adds a form select (drop down) menu, sets the default item in the select menu and shows off a different way to print forms.</p>
<pre class="brush: php; title: ; notranslate">

// Create a new Form object
$form = new Form(&quot;ExampleForm&quot;);

// Add a text input
$form-&gt;addField(new FormInput(&quot;&quot;, &quot;text&quot;, &quot;firstname&quot;, &quot;What is your name?&quot;));

// Create some data for our select.
$genders = array();

$genders[] = array(&quot;value&quot; =&gt; &quot;M&quot;, // The value your script will see when the form is submitted.
&quot;name&quot; =&gt; &quot;Man&quot;, // The name that will be displayed to users.
&quot;selected&quot; =&gt; false);

$genders[] = array(&quot;value&quot; =&gt; &quot;W&quot;,
&quot;name&quot; =&gt; &quot;Woman&quot;,
&quot;selected&quot; =&gt; true); // Set this item to be selected by default

// Create a select.
$genderSelect = new FormSelect($genders, &quot;gender&quot;, &quot;Your are a: &quot;);

// Add our select
$form-&gt;addField($genderSelect);

// Add the submit button.
$form-&gt;addField(new FormInput(&quot;Submit&quot;, &quot;submit&quot;, &quot;submit&quot;));

// Save the form as an HTML string.
$html = $form-&gt;buildForm();

// Print the form later.
echo $html;
</pre>
<p><a href="http://seanmonahan.org/code/php/examples/form/SimpleFormWithSelect.php" target="_blank">See the example</a> in action.</p>
<h4>A Less Simple Form with TextArea</h4>
<p>This next example adds a textarea and provides some default text for it as well as the number of columns and rows.</p>
<pre class="brush: php; title: ; notranslate">

// Create the text area.
$defaultText = &quot;Tell us about yourself.  In 500 words or less.&quot;;
$textarea = new FormTextArea($defaultText, &quot;biography&quot;);

// You can also set properties like this.
$textarea-&gt;setColumns(20);
$textarea-&gt;setRows(5);

// Add the text area.
$form-&gt;addField($textarea);
</pre>
<p><a href="http://seanmonahan.org/code/php/examples/form/SimpleFormWithTextarea.php" target="_blank">See the example</a> in action.</p>
<h4>Organize Things with Separators</h4>
<p>Our example form is starting to get big.  Time to bring some organization into the mix.  Enter separators.  Separtors are purely organizational elements and will not be submitted along with your form.  They simple allow us to visually break up the form so it&#8217;s easier to use.</p>
<pre class="brush: php; title: ; notranslate">

// Add a separator with the text &quot;Bio&quot;

$form-&gt;addField(new FormSeparator(&quot;Bio&quot;)); // Want the separator without any text?  Just take out &quot;Bio&quot; and you've got it.
</pre>
<p><a href="http://seanmonahan.org/code/php/examples/form/SimpleFormWithSeparators.php" target="_blank">See the example</a> in action.</p>
<h4>Block Bots with CAPTCHA</h4>
<p>The final example adds CAPTCHA to our form.  This class uses Securimage CAPTCHA and you&#8217;ll need to download it from <a href="http://www.phpcaptcha.org/" target="_blank">here</a> if you want to use the CAPTCHA features of this class library.  The FormCaptcha object is a little more complicated than the rest so it&#8217;s easier to create a new object and set the properties afterward.</p>
<pre class="brush: php; title: ; notranslate">

// Create a new FormCaptcha object.
// NOTE: the source you pass to the contructor is the URI.  In other words,
// you could slap http://www.seanmonahan.org on front of the source and find that script.
$captcha = new FormCaptcha(&quot;/code/php/examples/form/Captcha/securimage_show.php&quot;);

// Position the reload text to be placed after the image.
$captcha-&gt;setReloadLocation(&quot;AfterImage&quot;);

// Set the label for the CAPTCHA input field.
$captcha-&gt;setLabel(&quot;Prove you aren't a bot.&quot;);

// Add the CAPTCHA to the form.
$form-&gt;addField($captcha);
</pre>
<p><a href="http://seanmonahan.org/code/php/examples/form/SimpleFormWithCaptcha.php" target="_blank">See the example</a> in action.</p>
]]></content:encoded>
			<wfw:commentRss>http://seanmonahan.org/2008/12/18/create-html-forms-with-object-oriented-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Externally Create and Manage MediaWiki Accounts with PHP</title>
		<link>http://seanmonahan.org/2008/12/13/externally-create-and-manage-mediawiki-accounts-with-php/</link>
		<comments>http://seanmonahan.org/2008/12/13/externally-create-and-manage-mediawiki-accounts-with-php/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 02:17:16 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[MediaWiki]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP5]]></category>

		<guid isPermaLink="false">http://seanmonahan.org/?p=26</guid>
		<description><![CDATA[Recently I was charged with integrating our content management system our company is developing with MediaWiki.  Specifically, we wanted the appearance of a single account for both our CMS and MediaWiki.  Since our CMS is were all the action happens and MediaWiki is serving as a user manual I decided to have our CMS also handle account management for MediaWiki.]]></description>
			<content:encoded><![CDATA[<p>Recently I was charged with integrating our content management system our company is developing with MediaWiki.  Specifically, we wanted the appearance of a single account for both our CMS and MediaWiki.  Since our CMS is were all the action happens and MediaWiki is serving as a user manual I decided to have our CMS also handle account management for MediaWiki.<br />
<span id="more-26"></span><br />
To accomplish this our CMS requires three additional features.  It must:</p>
<ol>
<li>Create user accounts for our MediaWiki user manual.</li>
<li>Update user accounts for our MediaWiki user manual.</li>
<li>Disable (<em>not</em> delete &#8211; <a href="#disable-note">more on this</a> later) user accounts for our MediaWiki user manual.</li>
</ol>
<p>These three features will in turn require some helper function to do some behind-the-scenes work for us.  These are:</p>
<ol>
<li>A getUserId function to return user IDs.</li>
<li>A generatePass function to generate random passwords.</li>
<li>A connectToDb function to connect to the MediaWiki database.</li>
</ol>
<p>To accomplish my goal I built a PHP5 class MediaWikiManager.  The class provides public methods for creating users, updating users, updating passwords and disabling users.</p>
<h4>Usage</h4>
<p>The first thing you need to do is set the constants DBPASS, DBUSER, DBHOST and DBNAME to match your MediaWiki database information.  Once you&#8217;ve done that simply create yourself an instance of the MediaWikiManager class.</p>
<pre class="brush: php; title: ; notranslate">

$manager = new MediaWikiManager();
</pre>
<p>To create a new user you need the user&#8217;s first name, last name, user name, email address and password.  Once you have this information, creating the MediaWiki account takes just a single line.</p>
<pre class="brush: php; title: ; notranslate">

$mediaWikiId = $manager-&gt;CreateUser($firstname, $lastname, $username, $email, $password);
</pre>
<p>CreateUser() will return the user ID that MediaWiki assigns your newly created user.  You can store the MediaWiki user ID and use it to update and disable the user later.  This isn&#8217;t a requirement though, as you only need the user name to manage user accounts with this class.  An important note here is that the MediaWikiManager class does not enforce unique user names.  Our CMS does all the work to ensure that user names are unique, so I can simply pass CreateUser() any user name and know it will be unique.  Your situation may vary.</p>
<p>To update an existing user you will need the user&#8217;s first name, last name, user name, email and, optionally, password and MediaWiki user ID.</p>
<pre class="brush: php; title: ; notranslate">

// Update with no optional parameters.

$updated = $manager-&gt;UpdateUser($firstname, $lastname, $username, $email);

// Update with password

$updated = $manager-&gt;UpdateUser($firstname, $lastname, $username, $email, $password);

// Update with MediaWiki user ID and no password.  Pass null to any optional parameter you want to skip.

$updated = $manager-&gt;UpdateUser($firstname, $lastname, $username, $email, null, $uid);
</pre>
<p>Our CMS requires user account passwords to be changed periodically.  So our most common account updates are to the password alone.  As such, MediaWikiManager provides methods for updating only the account password.</p>
<pre class="brush: php; title: ; notranslate">

// Update the password with the user name

$updated = $manager-&gt;UpdatePasswordByName($username, $password);

// Update the password with the MediaWiki user ID

$updated = $manager-&gt;UpdatePasswordById($uid, $password);
</pre>
<p>Finally, we need to be able to disable accounts entirely.</p>
<pre class="brush: php; title: ; notranslate">

// Disable account by user name

$disabled = $manager-&gt;DisableUserByName($username);

// Disable account by MediaWiki ID

$disabled = $manager-&gt;DisableUserById($uid);
</pre>
<p>Full documentation for the class can be found <a href="http://seanmonahan.org/code/php/docs/org.seanmonahan/_MediaWikiManager.php.html">here</a>.</p>
<p>The class can be downloaded from <a href="/wp-content/uploads/2008/12/mediawikimanager.zip">here</a>.</p>
<p><a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/us/"><img style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/us/88x31.png" alt="Creative Commons License" /></a><br />
<span>MediaWikiManager</span> by <a rel="cc:attributionURL" href="http://seanmonahan.org/code/php/docs/org.seanmonahan/MediaWikiManager.html">Sean Monahan</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/us/">Creative Commons Attribution-Share Alike 3.0 United States License</a>.</p>
<h4 id="disable-note">A note on disabling user accounts.</h4>
<p>On of the strengths of MediaWiki is that it stores a complete history of all additions, revision, deletions, etc made by users.  The caveat to this is that user accounts cannot be deleted due to the relational nature of the database behind MediaWiki.  If you want to &#8220;delete&#8221; an account the recommended method to produce a garbage password and update the account in question with said password.  By doing this, everyone is locked out of the account effectively deleting it.</p>
]]></content:encoded>
			<wfw:commentRss>http://seanmonahan.org/2008/12/13/externally-create-and-manage-mediawiki-accounts-with-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

